Skip to content

Commit

Permalink
net: coap: clean up token usage in samples and tests
Browse files Browse the repository at this point in the history
This patch replaces magic numbers with COAP_TOKEN_MAX_LEN value and
removes unnecessary castings of token buffer type.

Signed-off-by: Hubert Miś <[email protected]>
  • Loading branch information
hubertmis authored and nashif committed Feb 2, 2021
1 parent 22687c3 commit 77c68aa
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 37 deletions.
8 changes: 4 additions & 4 deletions samples/net/sockets/coap_client/src/coap-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ static int send_simple_coap_request(uint8_t method)

r = coap_packet_init(&request, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, COAP_TYPE_CON,
8, coap_next_token(),
COAP_TOKEN_MAX_LEN, coap_next_token(),
method, coap_next_id());
if (r < 0) {
LOG_ERR("Failed to init CoAP message");
Expand Down Expand Up @@ -328,7 +328,7 @@ static int send_large_coap_request(void)

r = coap_packet_init(&request, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, COAP_TYPE_CON,
8, coap_next_token(),
COAP_TOKEN_MAX_LEN, coap_next_token(),
COAP_METHOD_GET, coap_next_id());
if (r < 0) {
LOG_ERR("Failed to init CoAP message");
Expand Down Expand Up @@ -486,7 +486,7 @@ static int send_obs_coap_request(void)

r = coap_packet_init(&request, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, COAP_TYPE_CON,
8, coap_next_token(),
COAP_TOKEN_MAX_LEN, coap_next_token(),
COAP_METHOD_GET, coap_next_id());
if (r < 0) {
LOG_ERR("Failed to init CoAP message");
Expand Down Expand Up @@ -532,7 +532,7 @@ static int send_obs_reset_coap_request(void)

r = coap_packet_init(&request, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, COAP_TYPE_RESET,
8, coap_next_token(),
COAP_TOKEN_MAX_LEN, coap_next_token(),
0, coap_next_id());
if (r < 0) {
LOG_ERR("Failed to init CoAP message");
Expand Down
41 changes: 15 additions & 26 deletions samples/net/sockets/coap_server/src/coap-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ static int piggyback_get(struct coap_resource *resource,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, type, tkl,
(uint8_t *)token,
COAP_VERSION_1, type, tkl, token,
COAP_RESPONSE_CODE_CONTENT, id);
if (r < 0) {
goto end;
Expand Down Expand Up @@ -307,8 +306,7 @@ static int test_del(struct coap_resource *resource,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, type, tkl,
(uint8_t *)token,
COAP_VERSION_1, type, tkl, token,
COAP_RESPONSE_CODE_DELETED, id);
if (r < 0) {
goto end;
Expand Down Expand Up @@ -363,8 +361,7 @@ static int test_put(struct coap_resource *resource,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, type, tkl,
(uint8_t *)token,
COAP_VERSION_1, type, tkl, token,
COAP_RESPONSE_CODE_CHANGED, id);
if (r < 0) {
goto end;
Expand Down Expand Up @@ -424,8 +421,7 @@ static int test_post(struct coap_resource *resource,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, type, tkl,
(uint8_t *)token,
COAP_VERSION_1, type, tkl, token,
COAP_RESPONSE_CODE_CREATED, id);
if (r < 0) {
goto end;
Expand Down Expand Up @@ -501,8 +497,7 @@ static int query_get(struct coap_resource *resource,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, COAP_TYPE_ACK, tkl,
(uint8_t *) token,
COAP_VERSION_1, COAP_TYPE_ACK, tkl, token,
COAP_RESPONSE_CODE_CONTENT, id);
if (r < 0) {
goto end;
Expand Down Expand Up @@ -578,8 +573,7 @@ static int location_query_post(struct coap_resource *resource,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, type, tkl,
(uint8_t *)token,
COAP_VERSION_1, type, tkl, token,
COAP_RESPONSE_CODE_CREATED, id);
if (r < 0) {
goto end;
Expand Down Expand Up @@ -635,8 +629,7 @@ static int separate_get(struct coap_resource *resource,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, COAP_TYPE_ACK, tkl,
(uint8_t *)token, 0, id);
COAP_VERSION_1, COAP_TYPE_ACK, tkl, token, 0, id);
if (r < 0) {
goto end;
}
Expand All @@ -654,8 +647,7 @@ static int separate_get(struct coap_resource *resource,

/* Do not free and allocate "data" again, re-use the buffer */
r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, type, tkl,
(uint8_t *)token,
COAP_VERSION_1, type, tkl, token,
COAP_RESPONSE_CODE_CONTENT, id);
if (r < 0) {
goto end;
Expand Down Expand Up @@ -735,8 +727,7 @@ static int large_get(struct coap_resource *resource,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, COAP_TYPE_ACK, tkl,
(uint8_t *) token,
COAP_VERSION_1, COAP_TYPE_ACK, tkl, token,
COAP_RESPONSE_CODE_CONTENT, id);
if (r < 0) {
return -EINVAL;
Expand Down Expand Up @@ -852,8 +843,8 @@ static int large_update_put(struct coap_resource *resource,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, COAP_TYPE_ACK, tkl,
(uint8_t *) token, code, id);
COAP_VERSION_1, COAP_TYPE_ACK, tkl, token,
code, id);
if (r < 0) {
goto end;
}
Expand Down Expand Up @@ -934,8 +925,8 @@ static int large_create_post(struct coap_resource *resource,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, COAP_TYPE_ACK, tkl,
(uint8_t *)token, code, id);
COAP_VERSION_1, COAP_TYPE_ACK, tkl, token,
code, id);
if (r < 0) {
goto end;
}
Expand Down Expand Up @@ -1046,8 +1037,7 @@ static int send_notification_packet(const struct sockaddr *addr,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, type, tkl,
(uint8_t *)token,
COAP_VERSION_1, type, tkl, token,
COAP_RESPONSE_CODE_CONTENT, id);
if (r < 0) {
goto end;
Expand Down Expand Up @@ -1179,8 +1169,7 @@ static int core_get(struct coap_resource *resource,
}

r = coap_packet_init(&response, data, MAX_COAP_MSG_LEN,
COAP_VERSION_1, COAP_TYPE_ACK, tkl,
(uint8_t *)token,
COAP_VERSION_1, COAP_TYPE_ACK, tkl, token,
COAP_RESPONSE_CODE_CONTENT, id);
if (r < 0) {
r = -EINVAL;
Expand Down
2 changes: 1 addition & 1 deletion subsys/mgmt/updatehub/updatehub.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ static int send_request(enum coap_msgtype msgtype, enum coap_method method,

ret = coap_packet_init(&request_packet, data, MAX_PAYLOAD_SIZE,
COAP_VERSION_1, COAP_TYPE_CON,
8, coap_next_token(), method,
COAP_TOKEN_MAX_LEN, coap_next_token(), method,
coap_next_id());
if (ret < 0) {
LOG_ERR("Could not init packet");
Expand Down
4 changes: 2 additions & 2 deletions subsys/net/lib/coap/coap_link_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ int coap_well_known_core_get(struct coap_resource *resource,
struct coap_option query;
unsigned int num_queries;
size_t offset;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint16_t remaining;
uint16_t id;
uint8_t tkl;
Expand Down Expand Up @@ -638,7 +638,7 @@ int coap_well_known_core_get(struct coap_resource *resource,
uint8_t *data, uint16_t len)
{
struct coap_option query;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint16_t id;
uint8_t tkl;
uint8_t num_queries;
Expand Down
8 changes: 4 additions & 4 deletions tests/net/lib/coap/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ static int test_build_simple_pdu(void)

r = coap_packet_init(&cpkt, data, COAP_BUF_SIZE,
COAP_VERSION_1, COAP_TYPE_NON_CON,
strlen(token), (uint8_t *)token,
strlen(token), token,
COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED,
0x1234);
if (r < 0) {
Expand Down Expand Up @@ -617,7 +617,7 @@ static int prepare_block1_request(struct coap_packet *req,

r = coap_packet_init(req, data, COAP_BUF_SIZE, COAP_VERSION_1,
COAP_TYPE_CON, strlen(token),
(uint8_t *) token, COAP_METHOD_POST,
token, COAP_METHOD_POST,
coap_next_id());
if (r < 0) {
TC_PRINT("Unable to initialize request\n");
Expand Down Expand Up @@ -852,7 +852,7 @@ static int prepare_block2_request(struct coap_packet *req,

r = coap_packet_init(req, data, COAP_BUF_SIZE, COAP_VERSION_1,
COAP_TYPE_CON, strlen(token),
(uint8_t *) token, COAP_METHOD_GET,
token, COAP_METHOD_GET,
coap_next_id());
if (r < 0) {
TC_PRINT("Unable to initialize request\n");
Expand Down Expand Up @@ -1378,7 +1378,7 @@ static int test_observer_client(void)

r = coap_packet_init(&req, data, COAP_BUF_SIZE,
COAP_VERSION_1, COAP_TYPE_CON,
strlen(token), (uint8_t *)token,
strlen(token), token,
COAP_METHOD_GET, coap_next_id());
if (r < 0) {
TC_PRINT("Unable to initialize request\n");
Expand Down

0 comments on commit 77c68aa

Please sign in to comment.