예제 #1
0
파일: cmd_eps_slave.c 프로젝트: lirihe/arm
int eps_slave_volt(struct command_context *ctx) {

	char * args = command_args(ctx);
	unsigned int pv1, pv2, pv3;
	if (sscanf(args, "%u %u %u", &pv1, &pv2, &pv3) != 3)
		return CMD_ERROR_SYNTAX;
	printf("PV1: %04d PV2: %04d PV3: %04d\r\n", pv1, pv2, pv3);

	uint16_t pvolt[3];
	pvolt[0] = csp_hton16(pv1);
	pvolt[1] = csp_hton16(pv2);
	pvolt[2] = csp_hton16(pv3);

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_SET_VBOOST; // Ping port
	memcpy(&frame->data[1], &pvolt, 3 * sizeof(uint16_t));
	frame->len = 1 + 3 * sizeof(uint16_t);
	frame->len_rx = 0;
	frame->retries = 0;

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

	return CMD_ERROR_NONE;

}
예제 #2
0
파일: cmd_eps_slave.c 프로젝트: lirihe/arm
int eps_slave_single_output(struct command_context *ctx) {

	char * args = command_args(ctx);
	unsigned int channel;
	unsigned int mode;
	int delay;
	printf("Input channel, mode (0=off, 1=on), and delay\r\n");
	if (sscanf(args, "%u %u %d", &channel, &mode, &delay) != 3)
		return CMD_ERROR_SYNTAX;
	printf("Channel %d is set to %d with delay %d\r\n", channel, mode, delay);

	eps_output_set_single_req eps_switch;
	eps_switch.channel = (uint8_t)channel;
	eps_switch.mode = (uint8_t)mode;
	eps_switch.delay = csp_hton16((int16_t)delay);

	i2c_frame_t * frame;
	frame = csp_buffer_get(I2C_MTU);
	frame->dest = slave_node;
	frame->data[0] = EPS_PORT_SET_SINGLE_OUTPUT; // Ping port
	memcpy(&frame->data[1], &eps_switch, sizeof(eps_switch));
	frame->len = 1 + sizeof(eps_switch);
	frame->len_rx = 0;
	frame->retries = 0;

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

	return CMD_ERROR_NONE;

}
예제 #3
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;
}
예제 #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;
}
예제 #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;

}
예제 #6
0
int csp_can_tx(csp_iface_t * interface, csp_packet_t *packet, uint32_t timeout) {

	uint8_t bytes, overhead, avail, dest;
	uint8_t frame_buf[8];

	/* Get CFP identification number */
	int ident = id_get();
	if (ident < 0) {
		csp_log_warn("Failed to get CFP identification number\r\n");
		return CSP_ERR_INVAL;
	}

	/* Calculate overhead */
	overhead = sizeof(csp_id_t) + sizeof(uint16_t);

	/* Insert destination node mac address into the CFP destination field */
	dest = csp_route_get_nexthop_mac(packet->id.dst);
	if (dest == CSP_NODE_MAC)
		dest = packet->id.dst;

	/* Create CAN identifier */
	can_id_t id = 0;
	id |= CFP_MAKE_SRC(packet->id.src);
	id |= CFP_MAKE_DST(dest);
	id |= CFP_MAKE_ID(ident);
	id |= CFP_MAKE_TYPE(CFP_BEGIN);
	id |= CFP_MAKE_REMAIN((packet->length + overhead - 1) / 8);

	/* Get packet buffer */
	pbuf_element_t *buf = pbuf_new(id, NULL);

	if (buf == NULL) {
		csp_log_warn("Failed to get packet buffer for CAN\r\n");
		return CSP_ERR_NOMEM;
	}

	/* Set packet */
	buf->packet = packet;

	/* Calculate first frame data bytes */
	avail = 8 - overhead;
	bytes = (packet->length <= avail) ? packet->length : avail;

	/* Copy CSP headers and data */
	uint32_t csp_id_be = csp_hton32(packet->id.ext);
	uint16_t csp_length_be = csp_hton16(packet->length);

	memcpy(frame_buf, &csp_id_be, sizeof(csp_id_be));
	memcpy(frame_buf + sizeof(csp_id_be), &csp_length_be, sizeof(csp_length_be));
	memcpy(frame_buf + overhead, packet->data, bytes);

	/* Increment tx counter */
	buf->tx_count += bytes;

	/* Take semaphore so driver can post it later */
	csp_bin_sem_wait(&buf->tx_sem, 0);

	/* Send frame */
	if (can_send(id, frame_buf, overhead + bytes, NULL) != 0) {
		csp_log_info("Failed to send CAN frame in csp_tx_can\r\n");
		return CSP_ERR_DRIVER;
	}

	/* Non blocking mode */
	if (timeout == 0)
		return CSP_ERR_NONE;

	/* Blocking mode */
	if (csp_bin_sem_wait(&buf->tx_sem, timeout) != CSP_SEMAPHORE_OK) {
		csp_bin_sem_post(&buf->tx_sem);
		return CSP_ERR_TIMEDOUT;
	} else {
		csp_bin_sem_post(&buf->tx_sem);
		return CSP_ERR_NONE;
	}

}
예제 #7
0
int csp_can_tx(csp_iface_t * interface, csp_packet_t *packet, uint32_t timeout) {

	uint8_t bytes, overhead, avail, dest;
	uint8_t frame_buf[8];

	/* Get CFP identification number */
	int ident = id_get();
	if (ident < 0) {
		csp_log_warn("Failed to get CFP identification number");
		return CSP_ERR_INVAL;
	}
	
	/* Calculate overhead */
	overhead = sizeof(csp_id_t) + sizeof(uint16_t);

	/* Insert destination node mac address into the CFP destination field */
	dest = csp_rtable_find_mac(packet->id.dst);
	if (dest == CSP_NODE_MAC)
		dest = packet->id.dst;

	/* Create CAN identifier */
	can_id_t id = 0;
	id |= CFP_MAKE_SRC(packet->id.src);
	id |= CFP_MAKE_DST(dest);
	id |= CFP_MAKE_ID(ident);
	id |= CFP_MAKE_TYPE(CFP_BEGIN);
	id |= CFP_MAKE_REMAIN((packet->length + overhead - 1) / 8);

	/* Get packet buffer */
	pbuf_element_t *buf = pbuf_new(id, NULL);

	if (buf == NULL) {
		csp_log_warn("Failed to get packet buffer for CAN");
		return CSP_ERR_NOMEM;
	}

	/* Set packet */
	buf->packet = packet;

	/* Calculate first frame data bytes */
	avail = 8 - overhead;
	bytes = (packet->length <= avail) ? packet->length : avail;

	/* Copy CSP headers and data */
	uint32_t csp_id_be = csp_hton32(packet->id.ext);
	uint16_t csp_length_be = csp_hton16(packet->length);

	memcpy(frame_buf, &csp_id_be, sizeof(csp_id_be));
	memcpy(frame_buf + sizeof(csp_id_be), &csp_length_be, sizeof(csp_length_be));
	memcpy(frame_buf + overhead, packet->data, bytes);

	/* Increment tx counter */
	buf->tx_count += bytes;

	/* Take semaphore so driver can post it later */
	if (csp_bin_sem_wait(&buf->tx_sem, 0) != CSP_SEMAPHORE_OK) {
		csp_log_error("Failed to take CAN pbuf TX sem!");
		pbuf_free(buf, NULL, false);
		return CSP_ERR_DRIVER;
	}

	/* Send frame. We must free packet buffer is this fails,
	 * but the packet itself should be freed by the caller */
	if (can_send(id, frame_buf, overhead + bytes, NULL) != 0) {
		csp_log_warn("Failed to send CAN frame in csp_tx_can");
		csp_bin_sem_post(&buf->tx_sem);
		pbuf_free(buf, NULL, false);
		return CSP_ERR_DRIVER;
	}

	/* NOTE: The transmit packet is now owned by the transmission MOB and
	 * must NOT be freed by the calling thread. */

	/* Non blocking mode */
	if (timeout == 0)
		return CSP_ERR_NONE;

	/* Blocking mode */
	if (csp_bin_sem_wait(&buf->tx_sem, timeout) != CSP_SEMAPHORE_OK) {
		/* tx_sem is posted by transmission callback. The packet
		 * could still be in use by the transmission MOB, so
		 * we can not return CSP_ERR_TIMEOUT and risk that the
		 * calling thread frees the packet. */
		return CSP_ERR_NONE;
	} else {
		csp_bin_sem_post(&buf->tx_sem);
		return CSP_ERR_NONE;
	}

}