static void load_cb(proto_t *proto) { FILE *fp; fp = fopen(proto->list[1], "r"); if (fp) { char line[1024]; msg_t msg; msg.sender_id = STDOUT_FILENO; while (fgets(line, sizeof(line), fp)) { printf("%s", line); /* removes the \n at end of line */ line[strlen(line)-1] = 0; /* fills the message struct and parse it */ msg.data = line; msg.data_size = strlen(line); protocol_parse(&msg); printf("\n"); } fclose(fp); } else { protocol_response("error: can't open the file", proto); } }
void interactive_mode(void) { msg_t msg; char *input; completer_init(); msg.sender_id = STDOUT_FILENO; while (1) { input = readline("mod-host> "); rl_bind_key('\t', rl_complete); if (input) { if (*input) { /* adds the line on history */ add_history(input); /* fills the message struct and parse it */ msg.data = input; msg.data_size = strlen(input); protocol_parse(&msg); printf("\n"); free(input); input = NULL; } } else break; } }
void sensor_handler(void) { static uint32_t buffer_position = 0; while (buffer_position != serial_uart_count) { protocol_parse((char) serial_uart_buffer[buffer_position], &sensor_cmd); buffer_position++; if (buffer_position == SERIAL_BUFSIZE) buffer_position = 0; } }
int main(int argc, char* argv[]) { unsigned int len; len = sizeof(buf); memset(&vv, 0, sizeof(vv)); memset(buf, 0xff, sizeof(buf)); protocol_binary_write(&PROTOCOL_NAME(B), &vv, buf, &len); memset(&vv, 0xff, sizeof(vv)); protocol_binary_read(&PROTOCOL_NAME(B), buf, &len, &vv); len = sizeof(buf); memset(buf, 0xff, sizeof(buf)); protocol_text_write(&PROTOCOL_NAME(B), NULL, &vv, buf, &len); memset(&vv, 0xff, sizeof(vv)); len = (unsigned int)strlen(buf); protocol_text_read(&PROTOCOL_NAME(B), NULL, buf, &len, &vv); protocol_parse(buf, &callback, NULL); return 0; }
void protocol_run(KFile *fd) { /** * \todo to be removed, we could probably access the serial FIFO * directly */ static char linebuf[80]; if (!interactive) { kfile_gets(fd, linebuf, sizeof(linebuf)); // reset serial port error anyway kfile_clearerr(fd); // check message minimum length if (linebuf[0]) { /* If we enter lines beginning with sharp(#) they are stripped out from commands */ if(linebuf[0] != '#') { if (linebuf[0] == 0x1B && linebuf[1] == 0x1B) // ESC { interactive = true; kfile_printf(fd, "Entering interactive mode\r\n"); } else { protocol_parse(fd, linebuf); } } } } else { const char *buf; /* * Read a line from serial. We use a temporary buffer * because otherwise we would have to extract a message * from the port immediately: there might not be any * available, and one might get free while we read * the line. We also add a fake ID at the start to * fool the parser. */ buf = rl_readline(&rl_ctx); /* If we enter lines beginning with sharp(#) they are stripped out from commands */ if(buf && buf[0] != '#') { if (buf[0] != '\0') { // exit special case to immediately change serial input if (!strcmp(buf, "exit") || !strcmp(buf, "quit")) { rl_clear_history(&rl_ctx); kfile_printf(fd, "Leaving interactive mode...\r\n"); interactive = FORCE_INTERACTIVE; } else { //TODO: remove sequence numbers linebuf[0] = '0'; linebuf[1] = ' '; strncpy(linebuf + 2, buf, sizeof(linebuf) - 3); linebuf[sizeof(linebuf) - 1] = '\0'; protocol_parse(fd, linebuf); } } } } }