Skip to content
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

CoAP: early token generation #668

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open

Conversation

szczys
Copy link
Contributor

@szczys szczys commented Nov 8, 2024

The calling function (internal to SDK, not in userspace) is now responsible for generating CoAP tokens.

  • Add a token generator used by both libcoap and Zephyr coap
  • Use a compile-time define for token length instead of storing the value in the request_msg struct
  • Remove block_token from Golioth Client and use the token already stored in the request

Most of these commits will be squashed before merge. I left it this way for (hopefully) easier review.

Resolves https://github.com/golioth/firmware-issue-tracker/issues/724

Remove prototype for function that is never defined.

Signed-off-by: Mike Szczys <[email protected]>
Use a compile-time constant for the length of the CoAP token (in bytes).

Signed-off-by: Mike Szczys <[email protected]>
Copy link

github-actions bot commented Nov 8, 2024

Visit the preview URL for this PR (updated for commit 78973cc):

https://golioth-firmware-sdk-doxygen-dev--pr668-szczys-coap-ea-xdhe9u8i.web.app

(expires Tue, 19 Nov 2024 15:29:53 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: a9993e61697a3983f3479e468bcb0b616f9a0578

@@ -675,7 +675,7 @@ enum golioth_status golioth_coap_client_observe_release(struct golioth_client *c
}
strncpy(request_msg.path, path, sizeof(request_msg.path) - 1);

size_t t_len = min(token_len, sizeof(request_msg.token));
size_t t_len = min(token_len, GOLIOTH_COAP_TOKEN_LEN);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is removed in one of the last commits on this PR that changes the param list for this function.

@szczys szczys force-pushed the szczys/coap-early-token-gen branch from 540c9f0 to 8794984 Compare November 8, 2024 21:35
Copy link

github-actions bot commented Nov 8, 2024

Code Coverage

Code Coverage

Package Line Rate Branch Rate Health
include.golioth 75% 50%
port.linux 59% 32%
port.zephyr 55% 22%
src 69% 31%
Summary 68% (2544 / 3762) 31% (1078 / 3461)

Copy link

codecov bot commented Nov 8, 2024

Codecov Report

Attention: Patch coverage is 89.77273% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/coap_client.c 84.61% 0 Missing and 4 partials ⚠️
src/coap_client_libcoap.c 85.71% 0 Missing and 3 partials ⚠️
src/coap_blockwise.c 50.00% 2 Missing ⚠️
Files with missing lines Coverage Δ
src/coap_client_zephyr.c 57.50% <100.00%> (-1.03%) ⬇️
src/lightdb_state.c 85.07% <100.00%> (+2.16%) ⬆️
src/log.c 50.00% <100.00%> (+1.28%) ⬆️
src/ota.c 58.59% <100.00%> (+0.99%) ⬆️
src/rpc.c 57.14% <100.00%> (+1.26%) ⬆️
src/settings.c 61.96% <100.00%> (+0.66%) ⬆️
src/stream.c 100.00% <100.00%> (ø)
src/zephyr_coap_req.c 54.74% <100.00%> (-3.10%) ⬇️
src/coap_blockwise.c 31.79% <50.00%> (+0.43%) ⬆️
src/coap_client_libcoap.c 54.22% <85.71%> (-1.12%) ⬇️
... and 1 more

... and 2 files with indirect coverage changes

Add golioth_coap_next_token() to randomly generate the first token and
increment a stored value for all future tokens.

Signed-off-by: Mike Szczys <[email protected]>
Squash before merging

Signed-off-by: Mike Szczys <[email protected]>
Squash before merging

Signed-off-by: Mike Szczys <[email protected]>
Squash before merging

Signed-off-by: Mike Szczys <[email protected]>
TODO: unit tests for RPC

Signed-off-by: Mike Szczys <[email protected]>
Squash before merge.

Signed-off-by: Mike Szczys <[email protected]>
Squash before merge

Signed-off-by: Mike Szczys <[email protected]>
Squah before merging

Signed-off-by: Mike Szczys <[email protected]>
Tokens are now generated by the calling function. The same token will
automatically be used for each subsequent block because of the
blockwise_transfer context struct.

The workaround that overwrite the initial token for these requests is no
longer necessary and has been removed.

Squash before merging

Signed-off-by: Mike Szczys <[email protected]>
Directly use the token stored in the request message instead of creating a
new one. Remove the block_token storage from the client struct.

Squash before merging

Signed-off-by: Mike Szczys <[email protected]>
CoAP tokens are now generated by the Golioth SDK outside of the libcoap
library. The call to golioth_sys_srand() in the libcoap client is no longer
crucial as it will be called by Golioth's token generation function.

However, the random seed initialization for libcoap's token generation
should still be called just to ensure token generation is ready in the rare
(and currently unanticipated) case where libcoap internally fetches a new
token. One place that this is likely to occur is when libcoap uses etags.
We are not currently using etags in our implementation.

Use dynamically allocated memory to seed the token. This value will be
stored in the libcoap coap_session_t struct and the seed array will no
longer be needed.

Note, both Golioth and libcoap token generation uses a simple increment
after generating the initial random token. Avoid collision by getting a
token from Golioth and incrementing it by half its max value before seeding
libcoap.

Signed-off-by: Mike Szczys <[email protected]>
This value is now available as a compile-time constant.

Squash before merge

Signed-off-by: Mike Szczys <[email protected]>
Remove the token_len parameter as the compile-time define is now used
throughout the SDK.

Change the appearance and type of the token param to match what is used
with other functions that create coap requests.

Signed-off-by: Mike Szczys <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant