-
Notifications
You must be signed in to change notification settings - Fork 0
Add test protobuf encoding with CRC and COBS #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c8255d7
Add rocket protos as west dependency
EdwardJXLi ff68fc1
Enable nanopb in kconfig
EdwardJXLi 38787e9
Build nanopb during cmake
EdwardJXLi 62cf5d9
Add test radio thread
EdwardJXLi 3e75c6c
Fixed merge
EdwardJXLi 23a2dfb
Add COBS and CRC encoding to radio packet
EdwardJXLi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| #include <zephyr/kernel.h> | ||
| #include <zephyr/logging/log.h> | ||
| #include <zephyr/sys/crc.h> | ||
| #include <zephyr/data/cobs.h> | ||
| #include <zephyr/net_buf.h> | ||
| #include <pb_encode.h> | ||
| #include <HelloWorldPacket.pb.h> | ||
|
|
||
| LOG_MODULE_REGISTER(radio_thread, LOG_LEVEL_INF); | ||
|
|
||
| #define RADIO_THREAD_STACK_SIZE 2048 | ||
| #define RADIO_THREAD_PRIORITY 5 | ||
| #define RADIO_THREAD_PERIOD_MS 1000 | ||
|
|
||
| #define MAX_COBS_SIZE 256 /* max size of COBS encoded data, defined by GNSS/RADIO SPI spec */ | ||
| #define MAX_FRAME_SIZE ((MAX_COBS_SIZE - 2) * 254 / 255) /* 253 */ | ||
| #define MAX_PAYLOAD_SIZE (MAX_FRAME_SIZE - sizeof(uint16_t)) /* 251 */ | ||
|
|
||
| NET_BUF_POOL_DEFINE(cobs_src_pool, 1, MAX_FRAME_SIZE, 0, NULL); | ||
| NET_BUF_POOL_DEFINE(cobs_dst_pool, 1, MAX_COBS_SIZE, 0, NULL); | ||
|
|
||
| K_THREAD_STACK_DEFINE(radio_stack, RADIO_THREAD_STACK_SIZE); | ||
| static struct k_thread radio_thread; | ||
|
|
||
| BUILD_ASSERT(MAX_FRAME_SIZE + MAX_FRAME_SIZE / 254 + 2 <= MAX_COBS_SIZE, | ||
| "MAX_FRAME_SIZE too large for MAX_COBS_SIZE"); | ||
|
|
||
| static void radio_thread_fn(void *p1, void *p2, void *p3) | ||
| { | ||
| uint32_t counter = 0; | ||
|
|
||
| while (1) { | ||
| HelloWorldPacket message = HelloWorldPacket_init_zero; | ||
| uint8_t buffer[MAX_PAYLOAD_SIZE]; | ||
|
|
||
| message.counter = counter; | ||
| strncpy(message.message, "hello world", sizeof(message.message) - 1); | ||
|
|
||
| pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer)); | ||
| bool status = pb_encode(&stream, HelloWorldPacket_fields, &message); | ||
| size_t message_length = stream.bytes_written; | ||
|
|
||
| if (!status) { | ||
| LOG_ERR("Failed to encode HelloWorldPacket: %s", | ||
| PB_GET_ERROR(&stream)); | ||
| counter++; | ||
| k_sleep(K_MSEC(RADIO_THREAD_PERIOD_MS)); | ||
| continue; | ||
| } | ||
|
|
||
| /* CRC16-CCITT over protobuf payload */ | ||
| uint16_t crc = crc16_ccitt(0x0000, buffer, message_length); | ||
|
|
||
| /* Pack protobuf data + CRC into net_buf for COBS encoding */ | ||
| struct net_buf *src_buf = net_buf_alloc(&cobs_src_pool, K_NO_WAIT); | ||
| struct net_buf *dst_buf = net_buf_alloc(&cobs_dst_pool, K_NO_WAIT); | ||
|
|
||
| if (!src_buf || !dst_buf) { | ||
| LOG_ERR("Failed to allocate net_buf for COBS encoding"); | ||
| if (src_buf) { | ||
| net_buf_unref(src_buf); | ||
| } | ||
| if (dst_buf) { | ||
| net_buf_unref(dst_buf); | ||
| } | ||
| counter++; | ||
| k_sleep(K_MSEC(RADIO_THREAD_PERIOD_MS)); | ||
| continue; | ||
| } | ||
|
|
||
| net_buf_add_mem(src_buf, buffer, message_length); | ||
| net_buf_add_le16(src_buf, crc); | ||
|
|
||
| /* COBS encode with trailing 0x00 delimiter */ | ||
| int ret = cobs_encode(src_buf, dst_buf, COBS_FLAG_TRAILING_DELIMITER); | ||
|
|
||
| if (ret == 0) { | ||
| LOG_INF("Encoded packet: counter=%u, pb=%zu, cobs=%u bytes", | ||
| counter, message_length, dst_buf->len); | ||
| LOG_HEXDUMP_DBG(dst_buf->data, dst_buf->len, | ||
| "COBS encoded packet"); | ||
| } else { | ||
| LOG_ERR("COBS encoding failed: %d", ret); | ||
| } | ||
|
|
||
| net_buf_unref(src_buf); | ||
| net_buf_unref(dst_buf); | ||
|
|
||
| counter++; | ||
| k_sleep(K_MSEC(RADIO_THREAD_PERIOD_MS)); | ||
| } | ||
| } | ||
|
|
||
| void start_radio_thread(void) | ||
| { | ||
| k_thread_create(&radio_thread, radio_stack, K_THREAD_STACK_SIZEOF(radio_stack), | ||
| radio_thread_fn, NULL, NULL, NULL, RADIO_THREAD_PRIORITY, 0, K_NO_WAIT); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #ifndef RADIO_THREAD_H | ||
| #define RADIO_THREAD_H | ||
|
|
||
| void start_radio_thread(void); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.