uint16_t battery_read() {
	uint8_t txbuf[1];
	uint8_t rxbuf[43 + 2];
	uint16_t Vbat = 0;
	txbuf[0] = 0x08;

	if (i2c_master_transaction_2(0, stm_eps_node_v, &txbuf, 1, &rxbuf, 43 + 2, eps_delay) == E_NO_ERR) {
		memcpy(&Vbat, &rxbuf[10], 2);
		i2c_lock_flag = 0;
	}
	else {
		i2c_lock_flag ++;
		if (i2c_lock_flag >= 3) {
			power_control(5, OFF);
			i2c_lock_flag = 0;
		}
	}
	return csp_ntoh16(Vbat);
}
Esempio n. 2
0
/*
 * Send a remote shell command to obc.
 * @param command: the command to be sent.
 * @param response: the remote response from obc, NULL for no response needed.
 * @param returned: the returned value of the command.
 * Function returns the csp_transaction() value, which is normally the size of the payload.
 */
int send_remote_shell_command(char * command, char * response, int * returned) {
	int timeout = 5000;
	int8_t dest = 1;
	int8_t d_port = OBC_PORT_CUSTOM_REMOTE_SHELL;
	remoteShell_packet_t command_out;
	memset(&command_out, 0, sizeof(remoteShell_packet_t));
	printf("sending a message to host %d:%d.\"%s\".\r\n", dest, d_port, command);
	int reply_size = sizeof(remoteShell_packet_t);
	memcpy(command_out.command_text, command, strlen(command));
	command_out.returned = csp_hton16(-42); // yeah.

	remoteShell_packet_t * reply;
	if (!response) {
		timeout = 100;
		reply = NULL;
		reply_size = 0;
	} else {
		reply = calloc(sizeof(remoteShell_packet_t), 1);
	}

	printf("packet size: %lu.\nBuffer content: %s.\n", sizeof(remoteShell_packet_t), command_out.command_text);
	int packet_status = csp_transaction(CSP_PRIO_NORM, dest, d_port, timeout, &command_out , sizeof(remoteShell_packet_t), reply, reply_size);

	if (response) {
		reply->returned = csp_ntoh16(reply->returned);
		*returned = reply->returned;
		response = reply->command_text;
		printf("status = %d, original message:%s\nFunction returned = %d\n", packet_status, reply->command_text, reply->returned);
	} else {
		*returned = 0;
		printf("status = %d, NULL passed, not waiting for reply.\n", packet_status);
	}

	if (reply)
		free(reply);
	return packet_status;
}
Esempio n. 3
0
int eps_slave_hk(struct command_context *ctx) {
	int bintmp;
	printf("Requesting EPS HK data\r\n");
	eps_hk_1_t * chkparam;

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_HK; // Ping port
	frame->len = 1;
	frame->len_rx = 2 + (uint8_t) sizeof(eps_hk_1_t);
	frame->retries = 0;

	if (i2c_send(0, frame, 0) != E_NO_ERR) {
		csp_buffer_free(frame);
		return CMD_ERROR_NOMEM;
	}

	if (i2c_receive(0, &frame, 20) != E_NO_ERR)
		return CMD_ERROR_FAIL;

	chkparam = (eps_hk_1_t *)&frame->data[2];

	chkparam->pv[0] = csp_ntoh16(chkparam->pv[0]);
	chkparam->pv[1] = csp_ntoh16(chkparam->pv[1]);
	chkparam->pv[2] = csp_ntoh16(chkparam->pv[2]);
	chkparam->pc = csp_ntoh16(chkparam->pc);
	chkparam->bv = csp_ntoh16(chkparam->bv);
	chkparam->sc = csp_ntoh16(chkparam->sc);
	chkparam->temp[0] = csp_ntoh16(chkparam->temp[0]);
	chkparam->temp[1] = csp_ntoh16(chkparam->temp[1]);
	chkparam->temp[2] = csp_ntoh16(chkparam->temp[2]);
	chkparam->temp[3] = csp_ntoh16(chkparam->temp[3]);
	chkparam->temp[4] = csp_ntoh16(chkparam->temp[4]);
	chkparam->temp[5] = csp_ntoh16(chkparam->temp[5]);
	chkparam->latchup[0] = csp_ntoh16(chkparam->latchup[0]);
	chkparam->latchup[1] = csp_ntoh16(chkparam->latchup[1]);
	chkparam->latchup[2] = csp_ntoh16(chkparam->latchup[2]);
	chkparam->latchup[3] = csp_ntoh16(chkparam->latchup[3]);
	chkparam->latchup[4] = csp_ntoh16(chkparam->latchup[4]);
	chkparam->latchup[5] = csp_ntoh16(chkparam->latchup[5]);
	chkparam->sw_errors = csp_ntoh16(chkparam->sw_errors);
	chkparam->bootcount = csp_ntoh16(chkparam->bootcount);

	printf("ADC sample:\r\n");
	printf("Temp 1 %"PRId16"\r\n", chkparam->temp[0]);
	printf("Temp 2 %"PRId16"\r\n", chkparam->temp[1]);
	printf("Temp 3 %"PRId16"\r\n", chkparam->temp[2]);
	printf("Temp PCB %"PRId16"\r\n", chkparam->temp[3]);
	printf("BV %u\r\n", chkparam->bv);
	printf("PC %u\r\n", chkparam->pc);
	printf("SC %u\r\n", chkparam->sc);
	printf("PV1 %u\r\n", chkparam->pv[0]);
	printf("PV2 %u\r\n", chkparam->pv[1]);
	printf("PV3 %u\r\n", chkparam->pv[2]);
	printf("Latch up 1 %u\r\n", chkparam->latchup[0]);
	printf("Latch up 2 %u\r\n", chkparam->latchup[1]);
	printf("Latch up 3 %u\r\n", chkparam->latchup[2]);
	printf("Latch up 4 %u\r\n", chkparam->latchup[3]);
	printf("Latch up 5 %u\r\n", chkparam->latchup[4]);
	printf("Latch up 6 %u\r\n", chkparam->latchup[5]);
	printf("User Channel Status %02X ; \r\n", chkparam->channel_status);
	bintmp = chkparam->channel_status;
	int n;
	for(n=0; n<8; n++)
	   {
	      if((bintmp & 0x80) !=0)
	      {
	         printf("1");
	      }
	      else
	      {
	         printf("0");
	      }
	      if (n==3)
	      {
	         printf(" "); /* insert a space between nybbles */
	      }
	      bintmp = bintmp<<1;
	   }
	printf("\r\n");
	printf("Battery temperature 1 %"PRId16"\r\n", chkparam->temp[4]);
	printf("Battery temperature 2 %"PRId16"\r\n", chkparam->temp[5]);
	printf("Reset %d   BootCount %d   SW Err %d   PPT mode %d\r\n",chkparam->reset,chkparam->bootcount,chkparam->sw_errors,chkparam->ppt_mode);

	csp_buffer_free(frame);
	return CMD_ERROR_NONE;

}
Esempio n. 4
0
int cmd_testtools_ls2sd(struct command_context *ctx) {

	remoteShell_packet_t * message_send;
	remoteShell_packet_t * message_reply;
	char remote_path[REMOTE_MESSAGE_SIZE];

	message_send = (remoteShell_packet_t *) calloc(sizeof(remoteShell_packet_t), sizeof(char));
	message_reply = (remoteShell_packet_t *) calloc(sizeof(remoteShell_packet_t), sizeof(char));
	unsigned int now;

	char * out_buffer = calloc(10240, 1);

	if (!(message_send && message_reply)) {
		printf("Houston: calloc error.\n");
		goto err;
	}

	if (!(out_buffer)) {
		printf("Houston: malloc error.\n");
		goto err;
	}

	memset(remote_path, 0, REMOTE_MESSAGE_SIZE);

	int start = 0; // 0 means from start.
	int file_count = 0;
	int lastfilecount = 0;

	do {
		message_send->returned = csp_hton16(start);
		memcpy(message_reply, message_send, sizeof(remoteShell_packet_t));

		sprintf(remote_path, "/sd/");
		memcpy(message_send->command_text, remote_path, REMOTE_MESSAGE_SIZE);

		int packet_status = csp_transaction(CSP_PRIO_NORM, NODE_OBC, OBC_PORT_FTP_LIST, 6000,
		                                    message_send , sizeof(remoteShell_packet_t), message_reply, sizeof(remoteShell_packet_t));

		message_reply->returned = csp_ntoh16(message_reply->returned);
		file_count = message_reply->returned;

		printf("status = %d, ", packet_status);

		if (packet_status == sizeof(remoteShell_packet_t)) {
			now = (unsigned)time(NULL);
			if (message_reply->returned < 0) {
				printf("returned value = %d, ls failed. Re-formatting may required.\n", message_reply->returned);
				goto err;
			}
			memcpy(remote_path, message_reply->command_text, REMOTE_MESSAGE_SIZE);
			printf("%d files, path in sd is %s\n", message_reply->returned, remote_path);
			printf("successed listing files.\n");
		} else {
			printf("failed!\nNETWORK ERROR\n");
			goto err;
		}

		/* Then download list file. */

		char local_path[PATH_MAX]; // no need to malloc since it is part of remote_path
		memset(local_path, 0, PATH_MAX);
		getcwd(local_path, sizeof(local_path));
		strcat(local_path, "/");
		strcat(local_path, my_basename_1(remote_path));

		printf("local path is: %s\n", local_path);

		int download_status = ftp_download(NODE_OBC, OBC_PORT_FTP, local_path, ftp_backend, ftp_chunk_size, 0, 0, remote_path, &ftp_size);

		if (download_status != 0) {
			ftp_done(0);
			goto err;
		}

		if (ftp_status_reply() != 0) {
			ftp_done(0);
			goto err;
		}
		if (ftp_crc() != 0) {
			ftp_done(0);
			goto err;
		}

		ftp_done(1);

		usleep(100 * 1000); // 100 ms
		char remove_command[50];
		memset(remove_command, 0, 50);
		sprintf(remove_command, "<rm|%s>", remote_path);
		int remove_status = -1;
		send_remote_shell_command(remove_command, NULL, &remove_status);
		printf("remove status: %d\n", remove_status);


		int decompress_result = decompress_file_to_buffer(local_path, out_buffer, (uint32_t) 10240);

		if (decompress_result < 0) {
			printf("Decompress of %s failed. Maybe try to decompress this file in shell?\n", local_path);
			goto err;
		}

		char * pc_start;
		char * pc_end;
		int found_timestamp = 0;


		pc_start = strchr(out_buffer, parse_start);
		pc_end = strchr(out_buffer, parse_end);

		if (!(pc_start && pc_end))
			printf("timestamp not found. ugh.\n");
		else {
			found_timestamp = 1;
			pc_start += sizeof(char);
		}

		if (found_timestamp) {
			char list_time[32];
			memset(&list_time, 0, sizeof(list_time));
			memcpy(&list_time, pc_start, (pc_end - pc_start));

			printf("current time:%u, list time:%s\nTime difference: %d seconds.\n",
			       now , list_time, (now - (unsigned)atoi(list_time)));

			printf("%s\n", pc_end + sizeof(char));
		} else {
			printf("current time:%u, list time: unavailable\n", (unsigned)time(NULL));
			printf("%s\n", out_buffer);
		}

		char * last_legal_line = NULL;
		char * second_last_line = NULL;
		last_legal_line = strrchr(out_buffer, '\n');
		if (!last_legal_line) {
			printf("cannot find new line what happend?\n");
			break;
		}
		int i;
		for (i = 1; i < 32; ++i) {
			if (*(last_legal_line - i) == '\n' ) {
				second_last_line = last_legal_line - i;
				break;
			}
		}

		if (!second_last_line) {
			printf("cannot find new line what happend? quitting..\n");
			goto err;
		}

		char line[32] = {0};

		memcpy(line, second_last_line + 1, last_legal_line - second_last_line);
		sscanf (line, "%d\t", &lastfilecount);
		if (lastfilecount == file_count)
			printf("Finished listing.\n");
		else
			printf("downloading next listing file...\n");
		start = lastfilecount;
		memset(out_buffer, 0, 10240);

	}
	while (lastfilecount < file_count);

	/* cleaning starts. */
	if (out_buffer)
		free(out_buffer);
	if (message_send)
		free(message_send);
	if (message_reply)
		free(message_reply);

	return 0;

	/* Exception handlers */
err:
	if (out_buffer)
		free(out_buffer);
	if (message_send)
		free(message_send);
	if (message_reply)
		free(message_reply);
	return CMD_ERROR_FAIL;
}
Esempio n. 5
0
int cmd_testtools_packet(struct command_context *ctx) {

	if (ctx->argc != 2)
		return CMD_ERROR_SYNTAX;

	int8_t dest = atoi(ctx->argv[1]);
	int8_t d_port = OBC_PORT_CUSTOM_REMOTE_SHELL;
	char *message; // this goes to the space
	char *command; // got this from input
	command = calloc(REMOTE_MESSAGE_SIZE, sizeof(char));
	char c;
	int quit = 0, execute = 0;
	unsigned int cursor = 0;

	printf("Type the command here, hit enter to send, ctrl+x to cancel:\r\n");

	/* Wait for ^q to quit. */
	while (quit == 0) {
		/* Get character */
		c = getchar();
		switch (c) {
		/* CTRL + X */
		case 0x18:
			quit = 1;
			break;
		/* Backspace */
		case CONTROL('H'):
		case 0x7f:
			if (cursor > 0) {
				putchar('\b');
				putchar(' ');
				putchar('\b');
				cursor--;
			}
			break;
		/* RETURN */
		case '\r':
			execute = 1;
			quit = 1;
			break;
		default:
			putchar(c);
			if (command == NULL) {
				command = calloc(REMOTE_MESSAGE_SIZE, 1);
			}

			if ((command != NULL) && (cursor < REMOTE_MESSAGE_SIZE))
				command[cursor++] = c;
			break;
		}
	}
	if (execute) {
		printf("\n----------\nPress enter to send this command, or any key to abort:\r\n");
		printf("%s", command);
		c = getchar();
		if (c != '\r') {
			return CMD_ERROR_INVALID;
		}
	}
	putchar('\n');
	int overhead = 3;
	message = (char*)calloc(REMOTE_MESSAGE_SIZE + overhead, sizeof(char));
	//reply = (char * )calloc(REMOTE_MESSAGE_SIZE + overhead, sizeof(char));
	//the overhead of <,> and \0
	if (!message) {
		printf("calloc() failed!\n");
		return CMD_ERROR_FAIL;
	}
	message[0] = '<';
	//message[0] = '<'; message[1] = '|';

	for (uint8_t i = 0; i < cursor; i++) {
		if (command[i] == ' ') {
			message[i + 1] = '|';
		} else {
			message[i + 1] = command[i];
		}
	}
	/*
	message[cursor + 2] = '|'; message[cursor + 3] = '>';
	message[cursor + 4] = '\0';
	*/
	message[cursor + 1] = '>'; message[cursor + 2] = '\0'; message[cursor + 3] = '\0';
	printf("sending a message to host %d:%d.\"%s\".\r\n", dest, d_port, message);

	//int buffer_result = sprintf(buffer,"%s", message);
	//csp_packet_t * reply_packet;
	remoteShell_packet_t sending;
	memcpy(sending.command_text, message, 50);
	sending.returned = csp_hton16(-42); // yeah.


	remoteShell_packet_t reply;
	reply.returned = csp_hton16(-43);
	reply.command_text[0] = '\0';

	printf("packet size: %lu.\nBuffer content: %s.\n", sizeof(remoteShell_packet_t), sending.command_text);
	int packet_status = csp_transaction(CSP_PRIO_NORM, dest, d_port, 5000, &sending , sizeof(remoteShell_packet_t), &reply, sizeof(remoteShell_packet_t));
	reply.returned = csp_ntoh16(reply.returned);

	printf("status = %d, original message:%s\nReply = %d\n", packet_status, reply.command_text, reply.returned);


	free(message);
	free(command);
	return reply.returned;

}
Esempio n. 6
0
static int csp_can_process_frame(can_frame_t *frame) {

	pbuf_element_t *buf;
	uint8_t offset;

	can_id_t id = frame->id;

	/* Bind incoming frame to a packet buffer */
	buf = pbuf_find(id, CFP_ID_CONN_MASK, NULL);

	/* Check returned buffer */
	if (buf == NULL) {
		if (CFP_TYPE(id) == CFP_BEGIN) {
			buf = pbuf_new(id, NULL);
			if (buf == NULL) {
				csp_log_warn("No available packet buffer for CAN\r\n");
				csp_if_can.rx_error++;
				return CSP_ERR_NOMEM;
			}
        } else {
			csp_log_warn("Out of order MORE frame received for can id 0x%"PRIx32"; remain is %u\r\n",
                         (uint32_t)id, CFP_REMAIN(id));
			csp_if_can.frame++;
			return CSP_ERR_INVAL;
		}
	}

	/* Reset frame data offset */
	offset = 0;

	switch (CFP_TYPE(id)) {

		case CFP_BEGIN:

			/* Discard packet if DLC is less than CSP id + CSP length fields */
			if (frame->dlc < sizeof(csp_id_t) + sizeof(uint16_t)) {
				csp_log_warn("Short BEGIN frame received\r\n");
				csp_if_can.frame++;
				pbuf_free(buf, NULL);
				break;
			}

			/* Check for incomplete frame */
			if (buf->packet != NULL) {
				/* Reuse the buffer */
				csp_log_warn("Incomplete frame\r\n");
				csp_if_can.frame++;
			} else {
				/* Allocate memory for frame */
				buf->packet = csp_buffer_get(csp_buffer_size() - CSP_BUFFER_PACKET_OVERHEAD);
				if (buf->packet == NULL) {
					csp_log_error("Failed to get buffer for CSP_BEGIN packet\r\n");
					csp_if_can.frame++;
					pbuf_free(buf, NULL);
					break;
				}
			}

			/* Copy CSP identifier and length*/
			memcpy(&(buf->packet->id), frame->data, sizeof(csp_id_t));
			buf->packet->id.ext = csp_ntoh32(buf->packet->id.ext);
			memcpy(&(buf->packet->length), frame->data + sizeof(csp_id_t), sizeof(uint16_t));
			buf->packet->length = csp_ntoh16(buf->packet->length);

			/* Reset RX count */
			buf->rx_count = 0;

			/* Set offset to prevent CSP header from being copied to CSP data */
			offset = sizeof(csp_id_t) + sizeof(uint16_t);

			/* Set remain field - increment to include begin packet */
			buf->remain = CFP_REMAIN(id) + 1;

			/* Note fall through! */

		case CFP_MORE:

			/* Check 'remain' field match */
			if (CFP_REMAIN(id) != buf->remain - 1) {
				csp_log_error("CAN frame lost in CSP packet, %u vs. %u\r\n",
                              CFP_REMAIN(id),buf->remain - 1);
				pbuf_free(buf, NULL);
				csp_if_can.frame++;
				break;
			}

			/* Decrement remaining frames */
			buf->remain--;

			/* Check for overflow */
			if ((buf->rx_count + frame->dlc - offset) > buf->packet->length) {
				csp_log_error("RX buffer overflow\r\n");
				csp_if_can.frame++;
				pbuf_free(buf, NULL);
				break;
			}

			/* Copy dlc bytes into buffer */
			memcpy(&buf->packet->data[buf->rx_count], frame->data + offset, frame->dlc - offset);
			buf->rx_count += frame->dlc - offset;

			/* Check if more data is expected */
			if (buf->rx_count != buf->packet->length)
				break;

			/* Data is available */
			csp_new_packet(buf->packet, &csp_if_can, NULL);

			/* Drop packet buffer reference */
			buf->packet = NULL;

			/* Free packet buffer */
			pbuf_free(buf, NULL);

			break;

		default:
			csp_log_warn("Received unknown CFP message type\r\n");
			pbuf_free(buf, NULL);
			break;

	}

	return CSP_ERR_NONE;

}