diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/cmd.cpp | 53 | ||||
-rw-r--r-- | client/cmd.h | 9 | ||||
-rw-r--r-- | client/rl.cpp | 14 | ||||
-rw-r--r-- | client/rl.h | 1 | ||||
-rw-r--r-- | client/sock.cpp | 13 |
5 files changed, 76 insertions, 14 deletions
diff --git a/client/cmd.cpp b/client/cmd.cpp index b7adfae..e3c9eb9 100644 --- a/client/cmd.cpp +++ b/client/cmd.cpp @@ -43,7 +43,7 @@ void cmd_help(char*) { ); } -void cmd_send(char* addr_str) { +void cmd_send(char * addr_str) { char* data_str = consume_token(addr_str, IFS); char* end; @@ -68,16 +68,6 @@ void cmd_send(char* addr_str) { free(data); } -// void cmd_status(char*) { -// const char msg[] = { -// PB_CMD_READ, -// 0x00, // addr 0 = global state -// }; -// i2c_send(BUSADDR_MAIN, msg, sizeof(msg)); -// // NOTE: the reply handler will automatically print the state once it's -// // received -// } - void cmd_reset(char*) { const char msg[] = { PB_CMD_WRITE, @@ -105,3 +95,44 @@ void cmd_ls(char*) { i2c_send(BUSADDR_MAIN, msg, sizeof(msg)); } +extern bool i2c_dump_send; +extern bool i2c_dump_recv; +const char * dump_modes[] = { + "none", + "send", + "recv", + "both", + NULL, +}; +void cmd_dump(char * mode) { + consume_token(mode, IFS); + mode += strspn(mode, IFS); + + for (int i = 0; dump_modes[i] != NULL; i++) { + if (strcmp(mode, dump_modes[i]) == 0) { + i2c_dump_send = (i >> 0) & 1; + i2c_dump_recv = (i >> 1) & 1; + return; + } + } + + printf("mode \"%s\" unknown\n", mode); +} +char** cmd_dump_complete(const char * text, int begin, int end) { + int word = rl_word(rl_line_buffer, begin); + if (word != 1) return NULL; + + return rl_completion_matches(text, [](const char * text, int state) -> char * { + static size_t i = 0; + if (state == 0) i = 0; + + while (dump_modes[i] != NULL) { + const char * mode = dump_modes[i++]; + if (strncmp(text, mode, strlen(text)) == 0) + return strdup(mode); + } + return NULL; + }); + + return NULL; +} diff --git a/client/cmd.h b/client/cmd.h index 7fefe98..961ef89 100644 --- a/client/cmd.h +++ b/client/cmd.h @@ -16,11 +16,12 @@ typedef struct cmd cmd_t; cmd_handle_t cmd_exit; cmd_handle_t cmd_test; cmd_handle_t cmd_help; -cmd_complete_t cmd_help_complete; cmd_handle_t cmd_reset; cmd_handle_t cmd_ls; cmd_handle_t cmd_send; cmd_handle_t cmd_skip; +cmd_handle_t cmd_dump; +cmd_complete_t cmd_dump_complete; static const cmd_t cmds[] = { { @@ -59,6 +60,12 @@ static const cmd_t cmds[] = { .name = "test", .info = "[debug] send a test puzbus message", }, + { + .handle = cmd_dump, + .name = "dump", + .info = "[debug] dump sent or received messages", + .complete = cmd_dump_complete, + }, #endif }; static const size_t cmds_length = sizeof(cmds) / sizeof(cmds[0]); diff --git a/client/rl.cpp b/client/rl.cpp index 2fdd356..b8113aa 100644 --- a/client/rl.cpp +++ b/client/rl.cpp @@ -82,7 +82,7 @@ static char** rl_attempted_completion(const char * text, int start, int end) { cmd_t cmd = cmds[i]; if (cmd.complete == NULL) continue; if (strncmp(cmd.name, rl_line_buffer + cmd_start, cmd_len) != 0) continue; - return cmd.complete(rl_line_buffer, start, end); + return cmd.complete(text, start, end); } // else, no completion available @@ -107,3 +107,15 @@ int cli_main() { return EXIT_SUCCESS; } +int rl_word(const char * line, int cursor) { + int word = -1; + for (int i = 0; line[i] != '\0';) { + i += strspn(line + i, IFS); + int len = strcspn(line + i, IFS); + word++; + i += len; + if (i > cursor) break; + } + return word; +} + diff --git a/client/rl.h b/client/rl.h index 5e80d1a..c3bf2c7 100644 --- a/client/rl.h +++ b/client/rl.h @@ -7,4 +7,5 @@ int cli_main(); void rl_printf(const char * fmt, ...); +int rl_word(const char * line, int cursor); diff --git a/client/sock.cpp b/client/sock.cpp index 2d5787d..f5bd564 100644 --- a/client/sock.cpp +++ b/client/sock.cpp @@ -13,6 +13,7 @@ #include "i2ctcpv1.h" #include "sock.h" #include "rl.h" +#include "xxd.h" using std::logic_error; using std::thread; @@ -105,6 +106,9 @@ void PBSocket::sock_task() { sock_close(); } +bool i2c_dump_send = false; +bool i2c_dump_recv = true; + void i2c_send(uint16_t addr, const char * data, size_t data_size) { i2ctcp_msg_t msg = { .addr = addr, @@ -117,9 +121,16 @@ void i2c_send(uint16_t addr, const char * data, size_t data_size) { if (!i2ctcp_write(&msg, &packed, &size)) return; sock->send(packed, size); + if (i2c_dump_send) { + printf("[i2c send] data(0x%02lx):\n", data_size); + xxd(data, data_size); + } } void i2c_recv(uint16_t addr, const char * data, size_t data_size) { - rl_printf("[0x%02x]: %.*s\n", addr, data_size, data); + if (i2c_dump_recv) { + printf("[i2c recv] data(0x%02lx):\n", data_size); + xxd(data, data_size); + } } |