fork download
  1. enum {
  2. WAIT_MAGIC0,
  3. WAIT_MAGIC1,
  4. WAIT_HEADER,
  5. WAIT_PAYLOAD,
  6. DONE,
  7. } state;
  8.  
  9. struct packet {
  10. uint8_t id;
  11. uint8_t length;
  12. uint8_t seq_num;
  13. uint8_t payload[231];
  14. }
  15.  
  16. // return the size of the consumed data.
  17. size_t parse_packet(uint8_t* data, size_t length, state *state, packet *packet) {
  18. size_t idx = 0;
  19. while (idx < length) {
  20. switch (*state) {
  21. WAIT_MAGIC0:
  22. if (data[idx] == 0xab) {
  23. *state = WAIT_MAGIC1;
  24. }
  25. idx++;
  26. break;
  27.  
  28. WAIT_MAGIC1:
  29. if (data[idx] == 0xba) {
  30. *state = WAIT_HEADER;
  31. } else {
  32. *state = WAIT_MAGIC0;
  33. }
  34. idx++;
  35. break;
  36.  
  37. WAIT_HEADER:
  38. if (idx + 3 >= length) {
  39. return idx;
  40. }
  41.  
  42. memcpy(packet, data + idx, 3);
  43. idx += 3;
  44. *state = WAIT_PAYLOAD;
  45. break;
  46.  
  47. WAIT_PAYLOAD:
  48. if (idx + packet->length >= length) {
  49. return idx;
  50. }
  51.  
  52. memcpy(packet->payload, data + idx, packet->length);
  53. idx += packet->length;
  54. *state = DONE;
  55. return idx;
  56. }
  57. }
  58.  
  59. return idx;
  60. }
  61.  
  62.  
  63. int main() {
  64. uint8_t buf[MAX_PACKET_LEN * 3];
  65. size_t buf_size = 0;
  66.  
  67. state state = WAIT_MAGIC0;
  68. packet packet;
  69.  
  70. while (1) {
  71. size_t rcv_len = uart_read(); // recieve buffer and store to (buf + buf_size)
  72. buf_size += rcv_len;
  73.  
  74. size_t consumed_len = parse_packet(buf, buf_size, &state, &packet);
  75. if (state == DONE) {
  76. // you get a packet.
  77. } else {
  78. // remove the consumed buffers.
  79. }
  80.  
  81. }
  82. }
  83.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:10:3: error: unknown type name ‘uint8_t’
   uint8_t id;
   ^~~~~~~
prog.c:11:3: error: unknown type name ‘uint8_t’
   uint8_t length;
   ^~~~~~~
prog.c:12:3: error: unknown type name ‘uint8_t’
   uint8_t seq_num;
   ^~~~~~~
prog.c:13:3: error: unknown type name ‘uint8_t’
   uint8_t payload[231];
   ^~~~~~~
prog.c:17:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘parse_packet’
 size_t parse_packet(uint8_t* data, size_t length, state *state, packet *packet) {
        ^~~~~~~~~~~~
prog.c: In function ‘main’:
prog.c:64:3: error: unknown type name ‘uint8_t’
   uint8_t buf[MAX_PACKET_LEN * 3];
   ^~~~~~~
prog.c:64:15: error: ‘MAX_PACKET_LEN’ undeclared (first use in this function)
   uint8_t buf[MAX_PACKET_LEN * 3];
               ^~~~~~~~~~~~~~
prog.c:64:15: note: each undeclared identifier is reported only once for each function it appears in
prog.c:65:3: error: unknown type name ‘size_t’
   size_t buf_size = 0;
   ^~~~~~
prog.c:65:3: note: ‘size_t’ is defined in header ‘<stddef.h>’; did you forget to ‘#include <stddef.h>’?
prog.c:1:1:
+#include <stddef.h>
 enum {
prog.c:65:3:
   size_t buf_size = 0;
   ^~~~~~
prog.c:67:3: warning: statement with no effect [-Wunused-value]
   state state = WAIT_MAGIC0;
   ^~~~~
prog.c:67:8: error: expected ‘;’ before ‘state’
   state state = WAIT_MAGIC0;
        ^~~~~~
        ;
prog.c:68:3: error: unknown type name ‘packet’; use ‘struct’ keyword to refer to the type
   packet packet;
   ^~~~~~
   struct 
prog.c:71:5: error: unknown type name ‘size_t’
     size_t rcv_len = uart_read(); // recieve buffer and store to (buf + buf_size)
     ^~~~~~
prog.c:71:5: note: ‘size_t’ is defined in header ‘<stddef.h>’; did you forget to ‘#include <stddef.h>’?
prog.c:71:22: warning: implicit declaration of function ‘uart_read’ [-Wimplicit-function-declaration]
     size_t rcv_len = uart_read(); // recieve buffer and store to (buf + buf_size)
                      ^~~~~~~~~
prog.c:74:5: error: unknown type name ‘size_t’
     size_t consumed_len = parse_packet(buf, buf_size, &state, &packet);
     ^~~~~~
prog.c:74:5: note: ‘size_t’ is defined in header ‘<stddef.h>’; did you forget to ‘#include <stddef.h>’?
prog.c:74:27: warning: implicit declaration of function ‘parse_packet’ [-Wimplicit-function-declaration]
     size_t consumed_len = parse_packet(buf, buf_size, &state, &packet);
                           ^~~~~~~~~~~~
prog.c:74:12: warning: unused variable ‘consumed_len’ [-Wunused-variable]
     size_t consumed_len = parse_packet(buf, buf_size, &state, &packet);
            ^~~~~~~~~~~~
prog.c:64:11: warning: unused variable ‘buf’ [-Wunused-variable]
   uint8_t buf[MAX_PACKET_LEN * 3];
           ^~~
stdout
Standard output is empty