net: coap: define max token length

This patch introduces COAP_TOKEN_MAX_LEN definition in coap.h file.
This definition replaces magic number across CoAP protocol
implementation and CoAP samples.

Signed-off-by: Hubert Miś <hubert.mis@nordicsemi.no>
This commit is contained in:
Hubert Miś 2021-01-23 08:20:58 +01:00 committed by Anas Nashif
commit 759f7454d8
4 changed files with 29 additions and 23 deletions

View file

@ -148,6 +148,8 @@ enum coap_response_code {
#define COAP_CODE_EMPTY (0)
#define COAP_TOKEN_MAX_LEN 8UL
/**
* @brief Set of Content-Format option values for CoAP.
*
@ -300,9 +302,10 @@ uint8_t coap_header_get_type(const struct coap_packet *cpkt);
* @brief Returns the token (if any) in the CoAP packet.
*
* @param cpkt CoAP packet representation
* @param token Where to store the token
* @param token Where to store the token, must point to a buffer containing
* at least COAP_TOKEN_MAX_LEN bytes
*
* @return Token length in the CoAP packet.
* @return Token length in the CoAP packet (0 - COAP_TOKEN_MAX_LEN).
*/
uint8_t coap_header_get_token(const struct coap_packet *cpkt, uint8_t *token);

View file

@ -417,7 +417,7 @@ static int process_obs_coap_reply(void)
{
struct coap_packet reply;
uint16_t id;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint8_t *data;
uint8_t type;
uint8_t tkl;
@ -455,7 +455,7 @@ static int process_obs_coap_reply(void)
goto end;
}
tkl = coap_header_get_token(&reply, (uint8_t *)token);
tkl = coap_header_get_token(&reply, token);
id = coap_header_get_id(&reply);
type = coap_header_get_type(&reply);

View file

@ -205,7 +205,7 @@ static int piggyback_get(struct coap_resource *resource,
{
struct coap_packet response;
uint8_t payload[40];
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint8_t *data;
uint16_t id;
uint8_t code;
@ -277,7 +277,7 @@ static int test_del(struct coap_resource *resource,
struct sockaddr *addr, socklen_t addr_len)
{
struct coap_packet response;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint8_t *data;
uint8_t tkl;
uint8_t code;
@ -325,7 +325,7 @@ static int test_put(struct coap_resource *resource,
struct sockaddr *addr, socklen_t addr_len)
{
struct coap_packet response;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
const uint8_t *payload;
uint8_t *data;
uint16_t payload_len;
@ -385,7 +385,7 @@ static int test_post(struct coap_resource *resource,
NULL };
const char * const *p;
struct coap_packet response;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
const uint8_t *payload;
uint8_t *data;
uint16_t payload_len;
@ -451,7 +451,7 @@ static int query_get(struct coap_resource *resource,
struct coap_option options[4];
struct coap_packet response;
uint8_t payload[40];
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint8_t *data;
uint16_t id;
uint8_t code;
@ -545,7 +545,7 @@ static int location_query_post(struct coap_resource *resource,
const char * const *p;
struct coap_packet response;
uint8_t *data;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint16_t id;
uint8_t code;
uint8_t type;
@ -602,7 +602,7 @@ static int separate_get(struct coap_resource *resource,
{
struct coap_packet response;
uint8_t payload[40];
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint8_t *data;
uint16_t id;
uint8_t code;
@ -693,7 +693,7 @@ static int large_get(struct coap_resource *resource,
static struct coap_block_context ctx;
struct coap_packet response;
uint8_t payload[64];
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint8_t *data;
uint16_t size;
uint16_t id;
@ -780,7 +780,7 @@ static int large_update_put(struct coap_resource *resource,
static struct coap_block_context ctx;
struct coap_packet response;
const uint8_t *payload;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint8_t *data;
uint16_t id;
uint16_t len;
@ -869,7 +869,7 @@ static int large_create_post(struct coap_resource *resource,
static struct coap_block_context ctx;
struct coap_packet response;
const uint8_t *payload;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint8_t *data;
uint16_t len;
uint16_t id;
@ -1099,7 +1099,7 @@ static int obs_get(struct coap_resource *resource,
struct sockaddr *addr, socklen_t addr_len)
{
struct coap_observer *observer;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint16_t id;
uint8_t code;
uint8_t type;
@ -1152,7 +1152,7 @@ static int core_get(struct coap_resource *resource,
{
static const char dummy_str[] = "Just a test\n";
struct coap_packet response;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint8_t *data;
uint16_t id;
uint8_t tkl;

View file

@ -14,6 +14,7 @@ LOG_MODULE_REGISTER(net_coap, CONFIG_COAP_LOG_LEVEL);
#include <errno.h>
#include <random/rand32.h>
#include <sys/atomic.h>
#include <sys/util.h>
#include <zephyr/types.h>
#include <sys/byteorder.h>
@ -333,10 +334,12 @@ int coap_packet_append_payload(struct coap_packet *cpkt, uint8_t *payload,
uint8_t *coap_next_token(void)
{
static uint32_t rand[2];
static uint32_t rand[
ceiling_fraction(COAP_TOKEN_MAX_LEN, sizeof(uint32_t))];
rand[0] = sys_rand32_get();
rand[1] = sys_rand32_get();
for (size_t i = 0; i < ARRAY_SIZE(rand); ++i) {
rand[i] = sys_rand32_get();
}
return (uint8_t *) rand;
}
@ -1255,13 +1258,13 @@ struct coap_reply *coap_response_received(
struct coap_reply *replies, size_t len)
{
struct coap_reply *r;
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint16_t id;
uint8_t tkl;
size_t i;
id = coap_header_get_id(response);
tkl = coap_header_get_token(response, (uint8_t *)token);
tkl = coap_header_get_token(response, token);
for (i = 0, r = replies; i < len; i++, r++) {
int age;
@ -1301,12 +1304,12 @@ struct coap_reply *coap_response_received(
void coap_reply_init(struct coap_reply *reply,
const struct coap_packet *request)
{
uint8_t token[8];
uint8_t token[COAP_TOKEN_MAX_LEN];
uint8_t tkl;
int age;
reply->id = coap_header_get_id(request);
tkl = coap_header_get_token(request, (uint8_t *)&token);
tkl = coap_header_get_token(request, token);
if (tkl > 0) {
memcpy(reply->token, token, tkl);