|
| 1 | +/* ============================================================================ |
| 2 | + Generated from t27 spec: MeshWire |
| 3 | + DO NOT EDIT - generated by t27c gen-c |
| 4 | + phi^2 + 1/phi^2 = 3 | TRINITY |
| 5 | + ============================================================================ */ |
| 6 | + |
| 7 | +#include <stdint.h> |
| 8 | +#include <stdbool.h> |
| 9 | +#include <stddef.h> |
| 10 | +#include <assert.h> |
| 11 | + |
| 12 | +#ifndef MESHWIRE_H |
| 13 | +#define MESHWIRE_H |
| 14 | + |
| 15 | +/* ------------------------------------------------------- |
| 16 | + Constants |
| 17 | + ------------------------------------------------------- */ |
| 18 | + |
| 19 | +#define VERSION 1 |
| 20 | +#define KIND_HELLO 0 |
| 21 | +#define KIND_DATA 1 |
| 22 | +#define HEADER_LEN 11 |
| 23 | + |
| 24 | +/* ------------------------------------------------------- |
| 25 | + Function prototypes |
| 26 | + ------------------------------------------------------- */ |
| 27 | + |
| 28 | +bool frame_kind_valid(uint8_t k); |
| 29 | +uint8_t be_byte(uint32_t w, size_t i); |
| 30 | +uint32_t u32_be(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3); |
| 31 | +uint8_t header_byte(uint8_t kind, uint32_t src, uint32_t dst, uint8_t ttl, size_t idx); |
| 32 | +bool parse_accepts(uint8_t b0, uint8_t b1); |
| 33 | + |
| 34 | +/* ------------------------------------------------------- |
| 35 | + Function implementations |
| 36 | + ------------------------------------------------------- */ |
| 37 | + |
| 38 | +bool frame_kind_valid(uint8_t k) { |
| 39 | + return (k <= KIND_DATA); |
| 40 | +} |
| 41 | + |
| 42 | +uint8_t be_byte(uint32_t w, size_t i) { |
| 43 | + if ((i == 0)) { |
| 44 | + return ((uint8_t)(((w >> 24) & 255))); |
| 45 | + } else if ((i == 1)) { |
| 46 | + return ((uint8_t)(((w >> 16) & 255))); |
| 47 | + } else if ((i == 2)) { |
| 48 | + return ((uint8_t)(((w >> 8) & 255))); |
| 49 | + } else { |
| 50 | + return ((uint8_t)((w & 255))); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +uint32_t u32_be(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) { |
| 55 | + return ((((((uint32_t)(b0)) << 24) | (((uint32_t)(b1)) << 16)) | (((uint32_t)(b2)) << 8)) | ((uint32_t)(b3))); |
| 56 | +} |
| 57 | + |
| 58 | +uint8_t header_byte(uint8_t kind, uint32_t src, uint32_t dst, uint8_t ttl, size_t idx) { |
| 59 | + if ((idx == 0)) { |
| 60 | + return VERSION; |
| 61 | + } else if ((idx == 1)) { |
| 62 | + return kind; |
| 63 | + } else if ((idx <= 5)) { |
| 64 | + return be_byte(src, (idx - 2)); |
| 65 | + } else if ((idx <= 9)) { |
| 66 | + return be_byte(dst, (idx - 6)); |
| 67 | + } else { |
| 68 | + return ttl; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +bool parse_accepts(uint8_t b0, uint8_t b1) { |
| 73 | + if ((b0 == VERSION)) { |
| 74 | + return frame_kind_valid(b1); |
| 75 | + } else { |
| 76 | + return false; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/* ------------------------------------------------------- |
| 81 | + Invariants (compile-time assertions) |
| 82 | + ------------------------------------------------------- */ |
| 83 | + |
| 84 | +/* invariant: header_is_11_bytes */ |
| 85 | +/* _Static_assert(1, "invariant: header_is_11_bytes"); */ |
| 86 | + |
| 87 | +/* invariant: kinds_distinct */ |
| 88 | +/* _Static_assert(1, "invariant: kinds_distinct"); */ |
| 89 | + |
| 90 | + |
| 91 | +/* ------------------------------------------------------- |
| 92 | + Tests |
| 93 | + ------------------------------------------------------- */ |
| 94 | + |
| 95 | +void test_byte0_is_version(void) { |
| 96 | + /* TODO: implement test */ |
| 97 | +} |
| 98 | + |
| 99 | +void test_byte1_is_kind(void) { |
| 100 | + /* TODO: implement test */ |
| 101 | +} |
| 102 | + |
| 103 | +void test_src_be_first_and_last_byte(void) { |
| 104 | + /* TODO: implement test */ |
| 105 | +} |
| 106 | + |
| 107 | +void test_ttl_is_last_byte(void) { |
| 108 | + /* TODO: implement test */ |
| 109 | +} |
| 110 | + |
| 111 | +void test_src_roundtrips_through_bytes(void) { |
| 112 | + /* TODO: implement test */ |
| 113 | +} |
| 114 | + |
| 115 | +void test_parse_accepts_valid(void) { |
| 116 | + /* TODO: implement test */ |
| 117 | +} |
| 118 | + |
| 119 | +void test_parse_rejects_bad_version(void) { |
| 120 | + /* TODO: implement test */ |
| 121 | +} |
| 122 | + |
| 123 | +void test_parse_rejects_bad_kind(void) { |
| 124 | + /* TODO: implement test */ |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +#endif /* MESHWIRE_H */ |
0 commit comments