From 1a92ed5075aba4b41fe34422d21a2c66cdf1d4c9 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Fri, 24 May 2024 17:47:00 +0200 Subject: WIP `send` command --- client/parse.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 client/parse.cpp (limited to 'client/parse.cpp') diff --git a/client/parse.cpp b/client/parse.cpp new file mode 100644 index 0000000..d15207c --- /dev/null +++ b/client/parse.cpp @@ -0,0 +1,32 @@ +#include +#include + +#include "parse.h" + +static unsigned ifsrun(const char* input, const char* ift) { + unsigned i; + for (i = 0; input[i] != '\0' && strchr(ift, input[i]); i++); + return i; +} + +int strtodata(const char* str, char** data, size_t* size) { + const char* ifs = IFS; + *size = 0; + size_t i; + size_t str_len = strlen(str); + + // TODO: finish this parser + // for (i = 0; i < str_len; i++) { + // unsigned ifs_run = ifsrun(&str[i], ifs); + // + // } + + *size = str_len; + + *data = (char*) malloc(*size); + + memcpy(*data, str, *size); + + return 0; +} + -- cgit v1.2.3 From 5d5b186a5a82b7e2415eddd77ef93af851034a5b Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 25 May 2024 15:31:42 +0200 Subject: WIP send command parser --- client/cmd.cpp | 5 ++- client/parse.cpp | 111 ++++++++++++++++++++++++++++++++++++++++++++++++------- client/parse.h | 12 ++++-- client/readme.md | 7 ++++ 4 files changed, 116 insertions(+), 19 deletions(-) (limited to 'client/parse.cpp') diff --git a/client/cmd.cpp b/client/cmd.cpp index 4a2c8a3..1ec2cb8 100644 --- a/client/cmd.cpp +++ b/client/cmd.cpp @@ -48,9 +48,10 @@ void cmd_send(char* addr_str) { char* data; size_t data_size; - if (strtodata(data_str, &data, &data_size)) { + int err = strtodata(data_str, &data, &data_size); + if (err <= 0) { printf("data format error at index %d:\n%s\n%*s^\n", - (int) data_size, data_str, (int) data_size, ""); + -err, data_str, -err, ""); return; } diff --git a/client/parse.cpp b/client/parse.cpp index d15207c..223dc5d 100644 --- a/client/parse.cpp +++ b/client/parse.cpp @@ -1,31 +1,116 @@ +#include #include #include +#include #include "parse.h" -static unsigned ifsrun(const char* input, const char* ift) { - unsigned i; - for (i = 0; input[i] != '\0' && strchr(ift, input[i]); i++); - return i; +static int parse_str(const char* str, char* data, size_t* size) { + char closing = str[0]; + char escape = false; + bool scan = data == NULL; + int i = 0; + size_t len = strlen(str); + + switch (str[i]) { + case '\'': + escape = false; + break; + case '\"': + escape = true; + break; + default: + return -i; + } + + for (i = 1; i < len && str[i] != '\0'; i++) { + char c = str[i]; + + if (c == closing) { + if (scan) printf("string%s of length %d\n", escape ? " (w/ escape)" : "", i - 1); + return i + 1; // +1 for closing quote + } + + if (scan) *size += 1; + } + + return -i; +} + +static int parse_num(const char* str, char* data, size_t* size) { + const char* ifs = IFS; + size_t len = strcspn(str, ifs); + bool scan = data == NULL; + int i = 0; + int base = 10; + bool bytestring = false; + + const char* colon = strchr(str, ':'); + if (colon != NULL && colon < str + len) { // byte string + base = 16; + bytestring = true; + } else if (len > 2 && strncmp(str, "0x", 2) == 0) { // hexadecimal prefix + base = 16; + i += 2; + }/* else if (len > 1 && strncmp(str, "0", 1) == 0) { // octal prefix + base = 8; + i += 1; + }*/ + + const char* set; + // if (base == 8) set = SET_OCT; + if (base == 10) set = SET_DEC; + if (base == 16) { + if (bytestring) set = SET_HEX_STR; + else set = SET_HEX; + } + + size_t len_ok = strspn(str + i, set) + i; + if (len != len_ok) return -len_ok; + + if (scan) { + if (base == 10) *size += 1; + else if (base == 16) { + if (!bytestring) { + *size += (len - i + 1) / 2; + } else { + for (; colon != NULL && colon < str + len; colon = strchr(str, ':')) { + *size += 1; + } + } + } + } + + if (scan) printf("number (base %d%s) of length %lu\n", base, bytestring ? " as bytestring" : "", len - i); + return len; } int strtodata(const char* str, char** data, size_t* size) { const char* ifs = IFS; *size = 0; size_t i; - size_t str_len = strlen(str); + size_t len = strlen(str); - // TODO: finish this parser - // for (i = 0; i < str_len; i++) { - // unsigned ifs_run = ifsrun(&str[i], ifs); - // - // } + for (i = 0; i < len;) { + // skip whitespace + int run; + run = strspn(&str[i], ifs); + if (run > 0) printf("skipping whitespace for %d bytes...\n", run); + i += run; + // end of string + if (str[i] == '\0') break; - *size = str_len; + if ((run = parse_str(str + i, NULL, size)) > 0) { i += run; continue; } + if ((run = parse_num(str + i, NULL, size)) > 0) { i += run; continue; } - *data = (char*) malloc(*size); + // no format detected + return -i + run; + } + printf("end of string w/o parse errors\n"); + printf("buffer size is now %lu\n", *size); + exit(0); - memcpy(*data, str, *size); + *data = (char*) malloc(*size); return 0; } diff --git a/client/parse.h b/client/parse.h index ac06446..10274e7 100644 --- a/client/parse.h +++ b/client/parse.h @@ -4,6 +4,11 @@ #define IFS " \t\n" +#define SET_OCT "01234567" +#define SET_DEC "0123456789" +#define SET_HEX SET_DEC"abcdefABCDEF" +#define SET_HEX_STR SET_HEX":" + /** * \brief modify \p token to point to the first token when broken up on \p ifs * and return the remaining data @@ -26,13 +31,12 @@ char* consume_token(char* token, const char* ifs); * \param data pointer to \c char* that will store the resulting data * \param size size of \p data * - * \return 0 if the string was parsed succesfully, or 1 if the string could not - * be parsed succesfully + * \return 0 or a negative integer representing the index where there is a + * syntax error if there was an error, or a positive integer representing the + * amount of bytes parsed from \p str * * \note The pointer that \p data refers to will not be initialized by this * function if parsing fails - * \note \p size will contain the index of \p str where the first invalid data - * was found if parsing fails */ int strtodata(const char* str, char** data, size_t* size); diff --git a/client/readme.md b/client/readme.md index 9d755aa..04471d2 100644 --- a/client/readme.md +++ b/client/readme.md @@ -12,3 +12,10 @@ goal (in order of implementation): ls list connected puzzle modules ``` + +``` +send 0x39 "Hello world!" de:ad:be:ef 0xff 5 0a 0750 + ^~~~~~~~~~~~~~ ^~~~~~~~~~~ ~^~~ ~^ ~^ ~~~~^ + STR_INTP BYTE_ARR UNSIGNED UNSIGNED UNSIGNED UNSIGNED + (hex+0x) (dec) (hex) (oct) +``` -- cgit v1.2.3 From c5295004db5970ce898a91f4147eb7bc1c40a7ed Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 25 May 2024 19:38:34 +0200 Subject: finish parser scanning part --- client/parse.cpp | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) (limited to 'client/parse.cpp') diff --git a/client/parse.cpp b/client/parse.cpp index 223dc5d..16a7afc 100644 --- a/client/parse.cpp +++ b/client/parse.cpp @@ -26,6 +26,8 @@ static int parse_str(const char* str, char* data, size_t* size) { for (i = 1; i < len && str[i] != '\0'; i++) { char c = str[i]; + // TODO: handle escaped characters + if (c == closing) { if (scan) printf("string%s of length %d\n", escape ? " (w/ escape)" : "", i - 1); return i + 1; // +1 for closing quote @@ -72,46 +74,61 @@ static int parse_num(const char* str, char* data, size_t* size) { if (base == 10) *size += 1; else if (base == 16) { if (!bytestring) { - *size += (len - i + 1) / 2; - } else { - for (; colon != NULL && colon < str + len; colon = strchr(str, ':')) { + size_t prefixless = len - i; + switch (prefixless) { + case 2: // 8-bit (2 hex characters) + case 4: // 16-bit + case 8: // 32-bit + case 16: // 64-bit + break; + default: + return -i; + } + *size += prefixless / 2; + } else { // if bytestring + size_t c = 0, field = strcspn(str, ifs); // length until end of field + while (c < field) { // count bytes in bytestring + if (strspn(str + c, SET_HEX) != 2) + return -i -c; + c += 2; *size += 1; + + if (str[c] == ':') { + c += 1; + continue; + } + break; } } } } - if (scan) printf("number (base %d%s) of length %lu\n", base, bytestring ? " as bytestring" : "", len - i); - return len; + i += len; + return i; } int strtodata(const char* str, char** data, size_t* size) { const char* ifs = IFS; *size = 0; - size_t i; + size_t i = 0; size_t len = strlen(str); - for (i = 0; i < len;) { + while (i < len) { // skip whitespace - int run; - run = strspn(&str[i], ifs); - if (run > 0) printf("skipping whitespace for %d bytes...\n", run); - i += run; + i += strspn(&str[i], ifs); // end of string if (str[i] == '\0') break; + int run; if ((run = parse_str(str + i, NULL, size)) > 0) { i += run; continue; } if ((run = parse_num(str + i, NULL, size)) > 0) { i += run; continue; } // no format detected return -i + run; } - printf("end of string w/o parse errors\n"); - printf("buffer size is now %lu\n", *size); - exit(0); *data = (char*) malloc(*size); - return 0; + return *size; } -- cgit v1.2.3 From 6cb0ea50e1829c0c6c2e0179d3c6b7573c4a1b24 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sat, 25 May 2024 20:10:45 +0200 Subject: split up hex string parser and number parser + small refactoring --- client/parse.cpp | 136 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 78 insertions(+), 58 deletions(-) (limited to 'client/parse.cpp') diff --git a/client/parse.cpp b/client/parse.cpp index 16a7afc..6eca774 100644 --- a/client/parse.cpp +++ b/client/parse.cpp @@ -5,7 +5,7 @@ #include "parse.h" -static int parse_str(const char* str, char* data, size_t* size) { +static int parse_string(const char* str, char* data, size_t* offset) { char closing = str[0]; char escape = false; bool scan = data == NULL; @@ -28,18 +28,49 @@ static int parse_str(const char* str, char* data, size_t* size) { // TODO: handle escaped characters - if (c == closing) { - if (scan) printf("string%s of length %d\n", escape ? " (w/ escape)" : "", i - 1); + if (c == closing) return i + 1; // +1 for closing quote - } - if (scan) *size += 1; + *offset += 1; } return -i; } -static int parse_num(const char* str, char* data, size_t* size) { +static int parse_hexstr(const char* str, char* data, size_t* offset) { + const char* ifs = IFS; + size_t len = strcspn(str, ifs); + bool scan = data == NULL; + int i = 0; + + // check if token contains at least one colon + const char* colon = strchr(str, ':'); + if (colon == NULL) return -i; + if (colon >= str + len) return -i; + + // check if token only contains allowed characters [0-9a-fA-F:] + size_t len_ok = strspn(str + i, SET_HEX_STR) + i; + if (len != len_ok) return -len_ok; + + size_t c = 0; + while (c < len) { // count bytes in bytestring + if (strspn(str + c, SET_HEX) != 2) + return -i -c; + c += 2; + *offset += 1; + + if (str[c] == ':') { + c += 1; + continue; + } + break; + } + + i += len; + return i; +} + +static int parse_number(const char* str, char* data, size_t* offset) { const char* ifs = IFS; size_t len = strcspn(str, ifs); bool scan = data == NULL; @@ -47,11 +78,7 @@ static int parse_num(const char* str, char* data, size_t* size) { int base = 10; bool bytestring = false; - const char* colon = strchr(str, ':'); - if (colon != NULL && colon < str + len) { // byte string - base = 16; - bytestring = true; - } else if (len > 2 && strncmp(str, "0x", 2) == 0) { // hexadecimal prefix + if (len > 2 && strncmp(str, "0x", 2) == 0) { // hexadecimal prefix base = 16; i += 2; }/* else if (len > 1 && strncmp(str, "0", 1) == 0) { // octal prefix @@ -62,73 +89,66 @@ static int parse_num(const char* str, char* data, size_t* size) { const char* set; // if (base == 8) set = SET_OCT; if (base == 10) set = SET_DEC; - if (base == 16) { - if (bytestring) set = SET_HEX_STR; - else set = SET_HEX; - } + if (base == 16) set = SET_HEX; size_t len_ok = strspn(str + i, set) + i; if (len != len_ok) return -len_ok; - if (scan) { - if (base == 10) *size += 1; - else if (base == 16) { - if (!bytestring) { - size_t prefixless = len - i; - switch (prefixless) { - case 2: // 8-bit (2 hex characters) - case 4: // 16-bit - case 8: // 32-bit - case 16: // 64-bit - break; - default: - return -i; - } - *size += prefixless / 2; - } else { // if bytestring - size_t c = 0, field = strcspn(str, ifs); // length until end of field - while (c < field) { // count bytes in bytestring - if (strspn(str + c, SET_HEX) != 2) - return -i -c; - c += 2; - *size += 1; - - if (str[c] == ':') { - c += 1; - continue; - } - break; - } - } + if (base == 10) *offset += 1; + else if (base == 16) { + size_t prefixless = len - i; + switch (prefixless) { + case 2: // 8-bit (2 hex characters) + case 4: // 16-bit + case 8: // 32-bit + case 16: // 64-bit + break; + default: + return -i; } + *offset += prefixless / 2; } i += len; return i; } -int strtodata(const char* str, char** data, size_t* size) { +static int _strtodata_main(const char* str, char* _data, size_t* offset) { const char* ifs = IFS; - *size = 0; - size_t i = 0; size_t len = strlen(str); + size_t i = 0; + while (i < len) { - // skip whitespace - i += strspn(&str[i], ifs); - // end of string - if (str[i] == '\0') break; + i += strspn(&str[i], ifs); // skip whitespace + if (str[i] == '\0') break; // end of string int run; - if ((run = parse_str(str + i, NULL, size)) > 0) { i += run; continue; } - if ((run = parse_num(str + i, NULL, size)) > 0) { i += run; continue; } + char* data = _data == NULL ? NULL : _data + *offset; + if ((run = parse_string(str + i, data, offset)) > 0) goto format_ok; + if ((run = parse_hexstr(str + i, data, offset)) > 0) goto format_ok; + if ((run = parse_number(str + i, data, offset)) > 0) goto format_ok; + + return -i + run; // no format detected - // no format detected - return -i + run; +format_ok: + i += run; + continue; } - *data = (char*) malloc(*size); + return i; +} + +int strtodata(const char* str, char** data, size_t* size) { + *size = 0; - return *size; + // 1st pass: check data format + int ret = _strtodata_main(str, NULL, size); + if (ret <= 0) return ret; // on error + + // 2nd pass: convert string literals into binary data + *data = (char*) malloc(*size); + size_t written = 0; + return _strtodata_main(str, *data, &written); } -- cgit v1.2.3 From c3491119759462aeb3eed4b39aa34f6f98ab8a4f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 26 May 2024 11:15:56 +0200 Subject: WIP convert parse.cpp data into raw data --- client/CMakeLists.txt | 1 + client/cmd.cpp | 6 ++++- client/parse.cpp | 74 ++++++++++++++++++++++++++++++++------------------- client/parse.h | 4 +-- client/xxd.c | 44 ++++++++++++++++++++++++++++++ client/xxd.h | 17 ++++++++++++ 6 files changed, 116 insertions(+), 30 deletions(-) create mode 100644 client/xxd.c create mode 100644 client/xxd.h (limited to 'client/parse.cpp') diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 35a55b6..5da93e4 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -14,6 +14,7 @@ add_executable(pbc sock.cpp cmd.cpp parse.cpp + xxd.c ) target_link_libraries(pbc diff --git a/client/cmd.cpp b/client/cmd.cpp index 1ec2cb8..78a6c3c 100644 --- a/client/cmd.cpp +++ b/client/cmd.cpp @@ -5,6 +5,7 @@ #include "cmd.h" #include "sock.h" #include "parse.h" +#include "xxd.h" char* consume_token(char* input, const char* ifs) { strtok(input, ifs); @@ -55,8 +56,11 @@ void cmd_send(char* addr_str) { return; } + printf("char data[%lu = 0x%02lx]:\n", data_size, data_size); + xxd(data, data_size); + // printf("(0x%02x) -> \"%.*s\"\n", addr, data_size, data); - i2c_send(addr, data, data_size); + // i2c_send(addr, data, data_size); free(data); } diff --git a/client/parse.cpp b/client/parse.cpp index 6eca774..5672ff2 100644 --- a/client/parse.cpp +++ b/client/parse.cpp @@ -1,14 +1,13 @@ -#include #include #include #include +#include #include "parse.h" -static int parse_string(const char* str, char* data, size_t* offset) { +static int parse_string(const char * str, char * data, size_t * offset) { char closing = str[0]; char escape = false; - bool scan = data == NULL; int i = 0; size_t len = strlen(str); @@ -23,7 +22,7 @@ static int parse_string(const char* str, char* data, size_t* offset) { return -i; } - for (i = 1; i < len && str[i] != '\0'; i++) { + for (i = 1; i < len && str[i] != '\0'; i++, *offset += 1) { char c = str[i]; // TODO: handle escaped characters @@ -31,16 +30,16 @@ static int parse_string(const char* str, char* data, size_t* offset) { if (c == closing) return i + 1; // +1 for closing quote - *offset += 1; + if (data != NULL) + data[*offset] = c; } return -i; } -static int parse_hexstr(const char* str, char* data, size_t* offset) { +static int parse_hexstr(const char * str, char * data, size_t * offset) { const char* ifs = IFS; size_t len = strcspn(str, ifs); - bool scan = data == NULL; int i = 0; // check if token contains at least one colon @@ -56,6 +55,10 @@ static int parse_hexstr(const char* str, char* data, size_t* offset) { while (c < len) { // count bytes in bytestring if (strspn(str + c, SET_HEX) != 2) return -i -c; + + if (data != NULL) + data[*offset] = strtol(str + c, NULL, 16) & 0xff; + c += 2; *offset += 1; @@ -70,10 +73,9 @@ static int parse_hexstr(const char* str, char* data, size_t* offset) { return i; } -static int parse_number(const char* str, char* data, size_t* offset) { +static int parse_number(const char * str, char * data, size_t * offset) { const char* ifs = IFS; size_t len = strcspn(str, ifs); - bool scan = data == NULL; int i = 0; int base = 10; bool bytestring = false; @@ -94,8 +96,8 @@ static int parse_number(const char* str, char* data, size_t* offset) { size_t len_ok = strspn(str + i, set) + i; if (len != len_ok) return -len_ok; - if (base == 10) *offset += 1; - else if (base == 16) { + size_t size = 1; // default integer size in bytes + if (base == 16) { size_t prefixless = len - i; switch (prefixless) { case 2: // 8-bit (2 hex characters) @@ -106,40 +108,58 @@ static int parse_number(const char* str, char* data, size_t* offset) { default: return -i; } - *offset += prefixless / 2; + size = prefixless / 2; } + if (data != NULL) { + unsigned long number = strtol(str + i, NULL, base); + long long mask = (1 << 8 * size) - 1; + number &= mask; + switch (size) { + case 1: + data[*offset] = number & 0xff; + break; + case 2: + number = htons(number); + data[*offset + 1] = (number) & 0xff; + data[*offset + 0] = (number >>= 8) & 0xff; + break; + case 4: + number = htonl(number); + data[*offset + 3] = (number) & 0xff; + data[*offset + 2] = (number >>= 8) & 0xff; + data[*offset + 1] = (number >>= 8) & 0xff; + data[*offset + 0] = (number >>= 8) & 0xff; + break; + } + } + + *offset += size; i += len; return i; } -static int _strtodata_main(const char* str, char* _data, size_t* offset) { +static int _strtodata_main(const char * str, char* data, size_t * offset) { const char* ifs = IFS; size_t len = strlen(str); - size_t i = 0; - - while (i < len) { + int i, run; + for (i = 0; i < len; i += run) { i += strspn(&str[i], ifs); // skip whitespace if (str[i] == '\0') break; // end of string - int run; - char* data = _data == NULL ? NULL : _data + *offset; - if ((run = parse_string(str + i, data, offset)) > 0) goto format_ok; - if ((run = parse_hexstr(str + i, data, offset)) > 0) goto format_ok; - if ((run = parse_number(str + i, data, offset)) > 0) goto format_ok; - - return -i + run; // no format detected + if ((run = parse_string(str + i, data, offset)) > 0) continue; + if ((run = parse_hexstr(str + i, data, offset)) > 0) continue; + if ((run = parse_number(str + i, data, offset)) > 0) continue; -format_ok: - i += run; - continue; + // no format detected + return -i + run; } return i; } -int strtodata(const char* str, char** data, size_t* size) { +int strtodata(const char * str, char ** data, size_t * size) { *size = 0; // 1st pass: check data format diff --git a/client/parse.h b/client/parse.h index 10274e7..94afe70 100644 --- a/client/parse.h +++ b/client/parse.h @@ -22,7 +22,7 @@ * * \return the remaining data after \p token and the first \p ifs */ -char* consume_token(char* token, const char* ifs); +char* consume_token(char * token, const char * ifs); /** * \brief convert string with literals into raw data @@ -38,5 +38,5 @@ char* consume_token(char* token, const char* ifs); * \note The pointer that \p data refers to will not be initialized by this * function if parsing fails */ -int strtodata(const char* str, char** data, size_t* size); +int strtodata(const char * str, char ** data, size_t * size); diff --git a/client/xxd.c b/client/xxd.c new file mode 100644 index 0000000..06b9960 --- /dev/null +++ b/client/xxd.c @@ -0,0 +1,44 @@ +#include +#include + +#include "parse.h" + +void xxd(const char * data, size_t size) { + size_t fake_size = size + (16 - size % 16) % 16; + + for (size_t base = 0; base < fake_size; base += 16) { + printf("%08lx: ", base); + + // print bytes + for (size_t offset = 0; offset < 16; offset++) { + size_t i = base + offset; + + if (offset == 8) printf(" "); + + if (i >= size) { + printf(" "); + continue; + } + + printf("%02x ", data[size]); + } + + // print ascii representation + printf(" |"); + for (size_t offset = 0; offset < 16; offset++) { + size_t i = base + offset; + + if (i >= size) { + printf(" "); + continue; + } + + if (isprint(data[size])) + printf("%c", data[size]); + else + printf("."); + } + printf("|\n"); + } +} + diff --git a/client/xxd.h b/client/xxd.h new file mode 100644 index 0000000..fb28bb1 --- /dev/null +++ b/client/xxd.h @@ -0,0 +1,17 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * \brief utility function that prints hexdump of data + */ +void xxd(const char * data, size_t size); + +#ifdef __cplusplus +} +#endif + -- cgit v1.2.3 From cad919018ed72005d2bc110247087201b0dea7ab Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 26 May 2024 11:28:24 +0200 Subject: fix silly typo --- client/cmd.cpp | 8 ++------ client/parse.cpp | 3 +++ client/xxd.c | 8 ++++---- 3 files changed, 9 insertions(+), 10 deletions(-) (limited to 'client/parse.cpp') diff --git a/client/cmd.cpp b/client/cmd.cpp index 78a6c3c..a098a14 100644 --- a/client/cmd.cpp +++ b/client/cmd.cpp @@ -5,7 +5,6 @@ #include "cmd.h" #include "sock.h" #include "parse.h" -#include "xxd.h" char* consume_token(char* input, const char* ifs) { strtok(input, ifs); @@ -56,11 +55,8 @@ void cmd_send(char* addr_str) { return; } - printf("char data[%lu = 0x%02lx]:\n", data_size, data_size); - xxd(data, data_size); - - // printf("(0x%02x) -> \"%.*s\"\n", addr, data_size, data); - // i2c_send(addr, data, data_size); + printf("sending char data[%lu = 0x%02lx] to 0x%02x\n", data_size, data_size, addr); + i2c_send(addr, data, data_size); free(data); } diff --git a/client/parse.cpp b/client/parse.cpp index 5672ff2..300df7c 100644 --- a/client/parse.cpp +++ b/client/parse.cpp @@ -115,12 +115,15 @@ static int parse_number(const char * str, char * data, size_t * offset) { unsigned long number = strtol(str + i, NULL, base); long long mask = (1 << 8 * size) - 1; number &= mask; + // NOTE: the hton? functions are used to convert host endianness to network + // endianness (big), and are required switch (size) { case 1: data[*offset] = number & 0xff; break; case 2: number = htons(number); + // TODO: check if the endianness is OK, or reverse these *offset indices* data[*offset + 1] = (number) & 0xff; data[*offset + 0] = (number >>= 8) & 0xff; break; diff --git a/client/xxd.c b/client/xxd.c index 06b9960..5d83635 100644 --- a/client/xxd.c +++ b/client/xxd.c @@ -1,7 +1,7 @@ #include #include -#include "parse.h" +#include "xxd.h" void xxd(const char * data, size_t size) { size_t fake_size = size + (16 - size % 16) % 16; @@ -20,7 +20,7 @@ void xxd(const char * data, size_t size) { continue; } - printf("%02x ", data[size]); + printf("%02x ", data[i] & 0xff); } // print ascii representation @@ -33,8 +33,8 @@ void xxd(const char * data, size_t size) { continue; } - if (isprint(data[size])) - printf("%c", data[size]); + if (isprint(data[i])) + printf("%c", data[i]); else printf("."); } -- cgit v1.2.3 From cd1b5097d6683355bbe2b96add8740ffc738ef8f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Sun, 26 May 2024 11:45:53 +0200 Subject: fix another silly bug --- client/CMakeLists.txt | 1 + client/parse.cpp | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'client/parse.cpp') diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 5da93e4..6aa4b4f 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.29) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 17) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) +set(CMAKE_BUILD_TYPE Debug) project(puzzlebox_client C CXX) diff --git a/client/parse.cpp b/client/parse.cpp index 300df7c..f31e802 100644 --- a/client/parse.cpp +++ b/client/parse.cpp @@ -138,8 +138,7 @@ static int parse_number(const char * str, char * data, size_t * offset) { } *offset += size; - i += len; - return i; + return len; } static int _strtodata_main(const char * str, char* data, size_t * offset) { -- cgit v1.2.3