diff options
Diffstat (limited to 'client/main.cpp')
-rw-r--r-- | client/main.cpp | 61 |
1 files changed, 45 insertions, 16 deletions
diff --git a/client/main.cpp b/client/main.cpp index 7a05049..30d7045 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,25 +1,54 @@ #include <cstdio> -#include <string> - -#include "puzbusv1.pb.h" - -int main() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - puzbus::I2CMsg test_msg; +#include <cstdlib> +#include <cstring> +#include <unistd.h> + +#include "puzbusv1.h" + +int send_message() { + const char* data = "Test message data!"; + struct pb_msg output = { + .addr = 0x39, + .data = (char*) data, + .length = strlen(data), + }; + + char* packed; + size_t size; + if (!pb_write(&output, &packed, &size)) { + printf("error writing!\n"); + return 1; + } - test_msg.set_address(0x39); - test_msg.set_data("Test message data!"); + fwrite(packed, sizeof(packed[0]), size, stdout); + fflush(stdout); - std::string output; - test_msg.SerializeToString(&output); + return 0; +} - printf("output[%lu]:\n", output.size()); - for (size_t i = 0; i < output.size(); i++) { - printf("%02x ", output[i]); +int read_message() { + freopen(NULL, "rb", stdin); // allow binary on stdin + struct pb_msg input; + + char buf[8]; // extremely small buffer to test chunked message parsing + size_t bytes = 0; + while ((bytes = fread(buf, sizeof(buf[0]), sizeof(buf), stdin)) > 0) { + if (!pb_read(&input, buf, bytes)) continue; + + printf("address: 0x%02x\n", input.addr); + printf("data: \"%.*s\"\n", input.length, input.data); + free(input.data); + return 0; } - printf("\n"); + return 1; +} + +int main() { + if (!isatty(fileno(stdout))) return send_message(); + if (!isatty(fileno(stdin))) return read_message(); + + printf("please pipe some data in or out to use this program\n"); return 0; } |