Exemplo n.º 1
0
int csp_cmp(uint8_t node, uint32_t timeout, uint8_t code, int membsize, struct csp_cmp_message * msg) {
	msg->type = CSP_CMP_REQUEST;
	msg->code = code;
	int status = csp_transaction(CSP_PRIO_NORM, node, CSP_CMP, timeout, msg, membsize, msg, membsize);
	if (status == 0)
		return CSP_ERR_TIMEDOUT;

	return CSP_ERR_NONE;
}
Exemplo n.º 2
0
int  obc_boot_count_get(uint32_t *boot_count, int timeout) {

	int ret = csp_transaction(CSP_PRIO_NORM, node_obc, OBC_PORT_BOOT_COUNT, timeout, NULL, 0, boot_count, sizeof(uint32_t));
	if (ret > 0)
		*boot_count = csp_ntoh32(*boot_count);
	else
		*boot_count = 0;
	return ret;
}
Exemplo n.º 3
0
void obc_load_image(const char * path) {

	uint32_t remote_crc;
	if (!csp_transaction(CSP_PRIO_NORM, node_obc, OBC_PORT_LOAD_IMG, 10000, (void *) path, strlen(path)+1, &remote_crc, sizeof(uint32_t)))
		return;

	remote_crc = csp_ntoh32(remote_crc);
	printf("Remote CRC %"PRIX32"\r\n", remote_crc);

}
Exemplo n.º 4
0
/** Copy image from RAM (src) to ROM (typically 0x48000000) */
void obc_ram_to_rom(uint32_t size, uint32_t checksum, uint32_t src, uint32_t dst) {

	obc_ram_to_rom_t txbuf;

	txbuf.size = csp_hton32(size);
	txbuf.checksum = csp_hton32(checksum);
	txbuf.src = csp_hton32(src);
	txbuf.dst = csp_hton32(dst);

	csp_transaction(CSP_PRIO_NORM, node_obc, OBC_PORT_RAM_TO_ROM, 0, &txbuf, sizeof(obc_ram_to_rom_t), NULL, 0);

}
Exemplo n.º 5
0
void csp_uptime(uint8_t node, uint32_t timeout) {

	uint32_t uptime = 0;

	int status = csp_transaction(CSP_PRIO_NORM, node, CSP_UPTIME, timeout, NULL, 0, &uptime, sizeof(uptime));
	if (status == 0) {
		printf("Network error\r\n");
		return;
	}
	uptime = csp_ntoh32(uptime);
	printf("Uptime of node %u is %u s\r\n", (unsigned int) node, (unsigned int) uptime);

}
Exemplo n.º 6
0
void csp_buf_free(uint8_t node, uint32_t timeout) {

	uint32_t size = 0;

	int status = csp_transaction(CSP_PRIO_NORM, node, CSP_BUF_FREE, timeout, NULL, 0, &size, sizeof(size));
	if (status == 0) {
		printf("Network error\r\n");
		return;
	}
	size = csp_ntoh32(size);
	printf("Free buffers at node %u is %u\r\n", (unsigned int) node, (unsigned int) size);

}
Exemplo n.º 7
0
void obc_boot_conf(uint32_t checksum, uint32_t boot_counts, const char * path) {

	char buf[100];

	checksum = csp_hton32(checksum);
	boot_counts = csp_hton32(boot_counts);
	memcpy(buf, &checksum, 4);
	memcpy(&buf[4], &boot_counts, 4);
	strncpy(buf + 8, path, 100-8);

	csp_transaction(CSP_PRIO_NORM, node_obc, OBC_PORT_BOOT_CONF, 0, buf, strlen(path)+1+4+4, NULL, 0);

}
Exemplo n.º 8
0
/**
 * Execute an OBC timesync
 * Sends a timespec to the OBC and returns the value back from the OBC.
 * Use the value tv_sec = 0, if you don't wish to update time but only get time back.
 * @param time timespec_t seconds and nanoseconds
 * @param timeout timeout in [ms]
 */
void obc_timesync(timestamp_t * time, int timeout) {

	time->tv_sec = csp_hton32(time->tv_sec);
	time->tv_nsec = csp_hton32(time->tv_nsec);
	if (csp_transaction(CSP_PRIO_NORM, node_obc, OBC_PORT_TIMESYNC, timeout, time, sizeof(timestamp_t), time, sizeof(timestamp_t)) > 0) {
		time->tv_sec = csp_ntoh32(time->tv_sec);
		time->tv_nsec = csp_ntoh32(time->tv_nsec);
	} else {
		time->tv_sec = 0;
		time->tv_nsec = 0;
	}

}
Exemplo n.º 9
0
void csp_memfree(uint8_t node, uint32_t timeout) {

	uint32_t memfree;

	int status = csp_transaction(CSP_PRIO_NORM, node, CSP_MEMFREE, timeout, NULL, 0, &memfree, sizeof(memfree));
	if (status == 0) {
		printf("Network error\r\n");
		return;
	}

	/* Convert from network to host order */
	memfree = csp_ntoh32(memfree);

	printf("Free Memory at node %u is %u bytes\r\n", node, (unsigned int) memfree);

}
Exemplo n.º 10
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;
}
Exemplo n.º 11
0
void csp_reboot(uint8_t node) {
	uint32_t magic_word = csp_hton32(0x80078007);
	csp_transaction(CSP_PRIO_NORM, node, CSP_REBOOT, 0, &magic_word, sizeof(magic_word), NULL, 0);
}
Exemplo n.º 12
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;
}
Exemplo n.º 13
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;

}
Exemplo n.º 14
0
/**
 * @brief Requests and prints remote routing table
 * @param[in]	uint8_t	first node in chunk of nodes requested
 * @param[out]	int8_t 	return code
*/
int8_t csp_route_print_remote_table(uint8_t node) {
	//sizeof(csp_route_info) = 11, so 14*12 = 164 bytes
	int routes_requested = 14;
	int status;
	uint32_t timeout = 1000;//just chose smallish value, can change
	uint8_t routes_left,k,i = 0;
	uint8_t params[2];
	status = csp_transaction(CSP_PRIO_NORM,node,CSP_GET_ROUTE,timeout, NULL,
	 0, &routes_left, sizeof(routes_left));
	if(status <= 0){
		printf("Timeout or other error\r\n");
		return CSP_ERR_TX;
	}
	//routes_requested is our default number of nodes to ask for,
	//so if fewer routes exist, change it to routes_left
	if(routes_left < routes_requested){
		routes_requested = routes_left;
	}
	csp_route_info returned_routes[routes_requested];
	uint8_t return_buf[sizeof(routes_left)+sizeof(returned_routes)];
	printf("Routing table for %u\r\n",node);
	printf("Node  Interface  Address\r\n");
	//request routing table of node, asking for routes_requested per transaction
	while(1){
		params[0] = i;
		params[1] = routes_requested;
		memset(returned_routes,0x00,sizeof(returned_routes));
		status = csp_transaction(CSP_PRIO_NORM,node,CSP_GET_ROUTE,timeout,
			&params[0],sizeof(params),&return_buf[0], (sizeof(routes_left)
				+sizeof(csp_route_info)*routes_requested));
		if(status <= 0){
			printf("Timeout or other error\r\n");
			return CSP_ERR_TX;
		}
		memcpy(&routes_left,&return_buf[0],sizeof(routes_left));
		memcpy(&returned_routes[0], &return_buf[1],
			(sizeof(csp_route_info)*routes_requested));
		for(k = 0; k < routes_requested; k++){
			if((i+k) > CSP_ROUTE_COUNT){
				goto out_of_nodes;
			}
			//no route if name_buffer NULL
			if(returned_routes[k].name_buffer[0] != 0x00) {
				if(returned_routes[k].node != CSP_DEFAULT_ROUTE){
					printf("%4u %-9s %u\r\n",returned_routes[k].node,
						returned_routes[k].name_buffer,
						returned_routes[k].nexthop_mac_addr);
				}
				else printf("  *  %-9s %u\r\n",returned_routes[k].name_buffer,
					returned_routes[k].nexthop_mac_addr);
			}
		else goto out_of_nodes;//if entry NULL, out of nodes
		}
		if(routes_left == 0){
			goto out_of_nodes;
		}
		else if(routes_left < routes_requested){
			routes_requested = routes_left;
		}
		i = returned_routes[k].node + 1;//next index 1 after last received
	}
	out_of_nodes:
	return CSP_ERR_NONE;
}
Exemplo n.º 15
0
void obc_boot_del(void) {

	uint32_t checksum = csp_hton32(0x80078007);
	csp_transaction(CSP_PRIO_NORM, node_obc, OBC_PORT_BOOT_CONF, 0, &checksum, 4, NULL, 0);

}
Exemplo n.º 16
0
/**
 * Jump to an address in RAM
 * This function is only useful after the load command has been executed, or an image
 * has been uploaded to RAM.
 * @param addr
 */
void obc_jump_ram(uint32_t addr) {

	addr = csp_hton32(addr);
	csp_transaction(CSP_PRIO_NORM, node_obc, OBC_PORT_JUMP, 0, &addr, sizeof(uint32_t), NULL, 0);

}