diff options
author | lonkaars <loek@pipeframe.xyz> | 2024-05-24 17:47:00 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2024-05-24 17:47:00 +0200 |
commit | 1a92ed5075aba4b41fe34422d21a2c66cdf1d4c9 (patch) | |
tree | a66d2dae13ab7c214da7b9ccf4701f0b7817c2e5 /client/cmd.cpp | |
parent | 31c30df2a24a45c69a7c5c2f594fa3a9a835b1fb (diff) |
WIP `send` command
Diffstat (limited to 'client/cmd.cpp')
-rw-r--r-- | client/cmd.cpp | 58 |
1 files changed, 44 insertions, 14 deletions
diff --git a/client/cmd.cpp b/client/cmd.cpp index 0a73dad..4a2c8a3 100644 --- a/client/cmd.cpp +++ b/client/cmd.cpp @@ -4,6 +4,12 @@ #include "cmd.h" #include "sock.h" +#include "parse.h" + +char* consume_token(char* input, const char* ifs) { + strtok(input, ifs); + return strtok(NULL, "\0"); +} void cmd_exit(char*) { exit(EXIT_SUCCESS); @@ -11,22 +17,46 @@ void cmd_exit(char*) { void cmd_test(char*) { const char* data = "Hello world!"; - i2c_send(0x39, (char*) data, strlen(data)); + i2c_send(0x39, data, strlen(data)); } void cmd_help(char*) { - printf("List of available commands:\n"); - for (size_t i = 0; i < cmds_length; i++) { - struct cmd cmd = cmds[i]; - printf(" %-*s", 10, cmd.name); - if (cmd.info != NULL) - printf(" %s", cmd.info); - printf("\n"); - } - - printf( - "\n" - "You can also use the TAB key to autocomplete commands\n" - ); + printf("List of available commands:\n"); + for (size_t i = 0; i < cmds_length; i++) { + struct cmd cmd = cmds[i]; + printf(" %-*s", 10, cmd.name); + if (cmd.info != NULL) + printf(" %s", cmd.info); + printf("\n"); + } + + printf( + "\n" + "You can also use the TAB key to autocomplete commands\n" + ); +} + +void cmd_send(char* addr_str) { + char* data_str = consume_token(addr_str, IFS); + + char* end; + uint16_t addr = strtol(addr_str, &end, 0); + if (addr_str + strlen(addr_str) != end) { + printf("address format error\n"); + return; + } + + char* data; + size_t data_size; + if (strtodata(data_str, &data, &data_size)) { + printf("data format error at index %d:\n%s\n%*s^\n", + (int) data_size, data_str, (int) data_size, ""); + return; + } + + // printf("(0x%02x) -> \"%.*s\"\n", addr, data_size, data); + i2c_send(addr, data, data_size); + + free(data); } |