-
Notifications
You must be signed in to change notification settings - Fork 132
Add initial implementation of Postcard-C #220
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
Open
sphw
wants to merge
2
commits into
jamesmunns:main
Choose a base branch
from
sphw:postcard-c-init
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| /target | ||
| **/*.rs.bk | ||
| Cargo.lock | ||
| .vscode | ||
| .vscode | ||
| **/*.gch | ||
| a.out |
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,121 @@ | ||
| #include "postcard.h" | ||
| #include <stdint.h> | ||
| #include <stdio.h> | ||
| #include <string.h> | ||
|
|
||
| /// helper function that prints out a byte buffer to stdout | ||
| void print_buffer(uint8_t* buffer, size_t len) | ||
| { | ||
| printf("serialized data ["); | ||
| for (size_t i = 0; i < len; i++) { | ||
| printf("%d", buffer[i]); | ||
| if (i < len - 1) { | ||
| printf(", "); | ||
| } | ||
| } | ||
| printf("]\n"); | ||
| } | ||
|
|
||
| /// helper function that prints out an array of int16 values to stdout | ||
| void print_values(int16_t* values, size_t len) | ||
| { | ||
| printf("values: ["); | ||
| for (size_t i = 0; i < len; i++) { | ||
| printf("%d", values[i]); | ||
| if (i < len - 1) { | ||
| printf(", "); | ||
| } | ||
| } | ||
| printf("]\n"); | ||
| } | ||
|
|
||
| int main() | ||
| { | ||
|
|
||
| // This example shows manually serializing and deserializing the Foo struct below. | ||
| // | ||
| // ```rust | ||
| // struct Foo { | ||
| // id: u32, | ||
| // name: String, | ||
| // values: Vec<i16> // len 3 | ||
| // is_active: bool | ||
| // } | ||
| // ``` | ||
|
|
||
| // allocate a buffer large enough to fit the serialized data | ||
| uint8_t buffer[128]; | ||
|
|
||
| // create a new `postcard_slice_t` that uses `buffer` as the underlying storage | ||
| // `postcard_slice_t` is a growable reference to some underlying buffer. | ||
| postcard_slice_t slice; | ||
| postcard_init_slice(&slice, buffer, sizeof(buffer)); | ||
|
|
||
| // encode id | ||
| postcard_encode_u32(&slice, 1234); | ||
|
|
||
| // encode name | ||
| const char* name = "PostcardTest"; | ||
| postcard_encode_string(&slice, name, strlen(name)); | ||
|
|
||
| // encode the 3 values | ||
| postcard_start_seq(&slice, 3); | ||
| postcard_encode_i16(&slice, -10); | ||
| postcard_encode_i16(&slice, 20); | ||
| postcard_encode_i16(&slice, -30); | ||
|
|
||
| // encode is_active | ||
| postcard_encode_bool(&slice, true); | ||
|
|
||
| // print the encoded data | ||
| // slice.len now contains the length of the serialized | ||
| // data from the serialization function | ||
| print_buffer(slice.data, slice.len); | ||
|
|
||
| // to decode the data we will create a new slice | ||
| // in the decode path, `postcard_slice_t.len` is used | ||
| // as a cursor for the decoded data. So we need a new slice | ||
| // that will only reference the serialized data, and | ||
| // have len = 0 | ||
| postcard_slice_t decode_slice; | ||
| postcard_init_slice(&decode_slice, buffer, slice.len); | ||
|
|
||
| // decode id | ||
| uint32_t id; | ||
| postcard_decode_u32(&decode_slice, &id); | ||
| printf("id: %u\n", id); | ||
|
|
||
| // decode name | ||
| size_t actual_len; | ||
| // first we decode the length of the string | ||
| // this is pulled out as a different function, | ||
| // so you can heap-allocate a string based on the actual | ||
| // length of the string | ||
| postcard_decode_string_len(&decode_slice, &actual_len); | ||
| char name_buffer[actual_len + 1]; | ||
| postcard_decode_string(&decode_slice, name_buffer, sizeof(name_buffer), | ||
| actual_len); | ||
| name_buffer[actual_len] = '\0'; // null terminate the string | ||
| // strings in postcard are encoded as byte arrays of valid utf8 data, | ||
| // and then do not include a null terminator. We add this terminator | ||
| // manually so we can print the string using `printf` | ||
| printf("name: %s\n", name_buffer); | ||
|
|
||
| // decode values | ||
| size_t seq_len; | ||
| postcard_decode_seq_len(&decode_slice, &seq_len); | ||
| printf("values len: %zu\n", seq_len); | ||
|
|
||
| int16_t values[seq_len]; | ||
| for (size_t i = 0; i < seq_len; i++) { | ||
| postcard_decode_i16(&decode_slice, &values[i]); | ||
| } | ||
| print_values(values, seq_len); | ||
|
|
||
| // decode is_active | ||
| bool is_active; | ||
| postcard_decode_bool(&decode_slice, &is_active); | ||
| printf("is_active: %s\n", is_active ? "true" : "false"); | ||
|
|
||
| return 0; | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should probably be an SPDX header here. I do plan to use Postcard-C in projects where this is required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that makes sense. I can add a SPDX header for MIT + Apache dual license to match the rest of the repo