net: coap: clean up token usage in samples and tests

This patch replaces magic numbers with COAP_TOKEN_MAX_LEN value and
removes unnecessary castings of token buffer type.

Signed-off-by: Hubert Miś <hubert.mis@nordicsemi.no>
This commit is contained in:
Hubert Miś 2021-01-27 19:28:00 +01:00 committed by Anas Nashif
parent 22687c34e3
commit 77c68aa070
5 changed files with 26 additions and 37 deletions

View file

@ -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");
@ -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");
@ -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");
@ -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");

View file

@ -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;
@ -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;
@ -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;
@ -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;
@ -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;
@ -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;
@ -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;
}
@ -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;
@ -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;
@ -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;
}
@ -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;
}
@ -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;
@ -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;

View file

@ -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");

View file

@ -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;
@ -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;

View file

@ -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) {
@ -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");
@ -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");
@ -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");