summaryrefslogtreecommitdiff
path: root/prototype/fw/src
diff options
context:
space:
mode:
Diffstat (limited to 'prototype/fw/src')
-rw-r--r--prototype/fw/src/crc32_test.c20
-rw-r--r--prototype/fw/src/microcobs.c99
-rw-r--r--prototype/fw/src/microcobs.h15
3 files changed, 114 insertions, 20 deletions
diff --git a/prototype/fw/src/crc32_test.c b/prototype/fw/src/crc32_test.c
deleted file mode 100644
index e23bb37..0000000
--- a/prototype/fw/src/crc32_test.c
+++ /dev/null
@@ -1,20 +0,0 @@
-
-#include <stdio.h>
-
-#include "crc32.h"
-
-int main(void) {
- crc32_t state = crc32_reset();
-
- do {
- char c;
- if (fread(&c, 1, 1, stdin) != 1)
- break;
-
- state = crc32_update(state, (uint8_t)c);
- } while(23);
-
- state = crc32_finalize(state);
- printf("%08x\n", state);
- return 0;
-}
diff --git a/prototype/fw/src/microcobs.c b/prototype/fw/src/microcobs.c
new file mode 100644
index 0000000..58d5e7b
--- /dev/null
+++ b/prototype/fw/src/microcobs.c
@@ -0,0 +1,99 @@
+
+#include <stdint.h>
+#include "microcobs.h"
+#include <stdio.h>
+
+ssize_t cobs_encode_sg(const struct sg_entry input[], uint8_t *output, size_t output_len)
+{
+ size_t idx_pos = 0;
+ size_t out_pos = 1;
+ size_t sg_idx = 0;
+
+ const struct sg_entry *e = input;
+ fprintf(stderr, "\nsg_entry %016x %zd\n", e->target, e->size);
+ while (e->size >= 0) {
+ if (sg_idx == e->size) {
+ sg_idx = 0;
+ e++;
+ fprintf(stderr, "\nsg_entry %016x %zd\n", e->target, e->size);
+ continue;
+ }
+
+ if (out_pos >= output_len)
+ return -1;
+
+ uint8_t inbyte = e->target[sg_idx];
+ fprintf(stderr, "%02x ", inbyte);
+ sg_idx += 1;
+
+ if (out_pos - idx_pos >= 255) {
+ output[idx_pos] = 255;
+ idx_pos = out_pos;
+ out_pos += 1;
+
+ if (out_pos >= output_len)
+ return -1;
+ }
+
+ if (inbyte) {
+ output[out_pos] = inbyte;
+ out_pos += 1;
+ } else {
+ output[idx_pos] = out_pos - idx_pos;
+ idx_pos = out_pos;
+ out_pos += 1;
+ }
+ }
+
+ if (out_pos >= output_len)
+ return -1;
+ output[idx_pos] = out_pos - idx_pos;
+ output[out_pos] = 0x00;
+ fprintf(stderr, "\n");
+ fprintf(stderr, "Finishing %d %d %d\n", idx_pos, out_pos, out_pos - idx_pos);
+
+ return out_pos + 1;
+}
+
+ssize_t cobs_encode(const uint8_t *input, size_t input_len, uint8_t *output, size_t output_len)
+{
+ size_t idx_pos = 0;
+ size_t out_pos = 1;
+ size_t in_pos = 0;
+
+ while (in_pos < input_len) {
+ if (out_pos >= output_len)
+ return -1;
+
+ uint8_t inbyte = input[in_pos];
+ fprintf(stderr, "%02x ", inbyte);
+ in_pos += 1;
+
+ if (out_pos - idx_pos >= 255) {
+ output[idx_pos] = 255;
+ idx_pos = out_pos;
+ out_pos += 1;
+
+ if (out_pos >= output_len)
+ return -1;
+ }
+
+ if (inbyte) {
+ output[out_pos] = inbyte;
+ out_pos += 1;
+ } else {
+ output[idx_pos] = out_pos - idx_pos;
+ idx_pos = out_pos;
+ out_pos += 1;
+ }
+ }
+
+ if (out_pos >= output_len)
+ return -1;
+ output[idx_pos] = out_pos - idx_pos;
+ output[out_pos] = 0x00;
+ fprintf(stderr, "\n");
+ fprintf(stderr, "Finishing %d %d %d\n", idx_pos, out_pos, out_pos - idx_pos);
+
+ return out_pos + 1;
+}
diff --git a/prototype/fw/src/microcobs.h b/prototype/fw/src/microcobs.h
new file mode 100644
index 0000000..774a27b
--- /dev/null
+++ b/prototype/fw/src/microcobs.h
@@ -0,0 +1,15 @@
+#ifndef __MICROCOBS_H__
+#define __MICROCOBS_H__
+
+#include <stdint.h>
+#include <unistd.h>
+
+struct sg_entry {
+ uint8_t *target;
+ ssize_t size;
+};
+
+ssize_t cobs_encode_sg(const struct sg_entry input[], uint8_t *output, size_t output_len);
+ssize_t cobs_encode(const uint8_t *input, size_t input_len, uint8_t *output, size_t output_len);
+
+#endif /* __MICROCOBS_H__ */