Skip to content

Commit

Permalink
fixup! Fix issues reported by clang-tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Jun 23, 2023
1 parent 73b0216 commit 8ebcc6a
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 99 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cmake-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ jobs:
- args: "-DOC_IPV4_ENABLED=ON -DOC_TCP_ENABLED=ON -DOC_PKI_ENABLED=OFF"
# cloud on (ipv4+tcp on), collections create on
- args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON"
# cloud on (ipv4+tcp on), collections create on, custom message buffer size
- args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_INOUT_BUFFER_SIZE=1024"
# cloud on (ipv4+tcp on), collections create on, custom message buffer size, custom message buffer pool size, custom app data buffer size, custom app data buffer pool size
- args: "-DOC_CLOUD_ENABLED=ON -DOC_COLLECTIONS_IF_CREATE_ENABLED=ON -DOC_INOUT_BUFFER_SIZE=2048 -DOC_INOUT_BUFFER_POOL=4 -DOC_APP_DATA_BUFFER_SIZE=2048 -DOC_APP_DATA_BUFFER_POOL=4"
# debug on
- args: "-DOC_DEBUG_ENABLED=ON"
# debug on, cloud on (ipv4+tcp on)
Expand Down
4 changes: 2 additions & 2 deletions api/oc_blockwise.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ oc_blockwise_find_response_buffer(const char *href, size_t href_len,
endpoint, method, query, query_len, role);
}

const void *
void *
oc_blockwise_dispatch_block(oc_blockwise_state_t *buffer, uint32_t block_offset,
uint32_t requested_block_size,
uint32_t *payload_size)
Expand All @@ -404,7 +404,7 @@ oc_blockwise_dispatch_block(oc_blockwise_state_t *buffer, uint32_t block_offset,
(uint32_t)(buffer->payload_size - block_offset));
}
buffer->next_block_offset = block_offset + *payload_size;
return (const void *)&buffer->buffer[block_offset];
return (void *)&buffer->buffer[block_offset];
}
return NULL;
}
Expand Down
7 changes: 4 additions & 3 deletions api/oc_client_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ dispatch_coap_request(void)
#else /* OC_TCP */
if ((long)payload_size > OC_BLOCK_SIZE) {
#endif /* !OC_TCP */
const void *payload = oc_blockwise_dispatch_block(
void *payload = oc_blockwise_dispatch_block(
g_request_buffer, 0, (uint32_t)OC_BLOCK_SIZE, &block_size);
if (payload) {
coap_set_payload(g_request, payload, block_size);
Expand Down Expand Up @@ -219,7 +219,8 @@ prepare_coap_request(oc_client_cb_t *cb)
}

if (oc_string_len(cb->query) > 0) {
coap_set_header_uri_query(g_request, oc_string(cb->query));
coap_set_header_uri_query(g_request, oc_string(cb->query),
oc_string_len(cb->query));
}

g_dispatch.client_cb = cb;
Expand Down Expand Up @@ -314,7 +315,7 @@ oc_init_multicast_update(const char *uri, const char *query)
coap_set_header_uri_path(g_request, uri, strlen(uri));

if (query) {
coap_set_header_uri_query(g_request, query);
coap_set_header_uri_query(g_request, query, strlen(query));
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion api/oc_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ handle_separate_response_request(coap_separate_t *request,
response_state->payload_size = (uint32_t)response_buffer->response_length;

uint32_t payload_size = 0;
const void *payload = oc_blockwise_dispatch_block(
void *payload = oc_blockwise_dispatch_block(
response_state, 0, request->block2_size, &payload_size);
if (payload != NULL) {
coap_set_payload(&response, payload, payload_size);
Expand Down
2 changes: 1 addition & 1 deletion apps/client_block_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ stop_observe(void *data)

if (oc_init_post(array_1, array_server, NULL, &post_array, LOW_QOS, NULL)) {
for (int i = 0; i < 100; i++) {
large_array[i] = oc_random_value();
large_array[i] = (int)oc_random_value();
OC_PRINTF("(%d %d) ", i, large_array[i]);
}
OC_PRINTF("\n");
Expand Down
3 changes: 2 additions & 1 deletion apps/cloud_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,8 @@ simulate_tpm_mbedtls_pk_parse_key(size_t device, mbedtls_pk_context *pk,
return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
}
uint8_t identity_private_key[4096];
int ret = fread(identity_private_key, 1, sizeof(identity_private_key), f);
size_t ret =
fread(identity_private_key, 1, sizeof(identity_private_key), f);
fclose(f);
return mbedtls_pk_parse_key(pk, identity_private_key, ret, NULL, 0, f_rng,
p_rng);
Expand Down
10 changes: 5 additions & 5 deletions include/oc_blockwise.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,12 @@ void oc_blockwise_free_response_buffer(oc_blockwise_state_t *buffer);
* @param block_offset the block offset
* @param requested_block_size blocksize to be send
* @param payload_size the send payload size
* @return const void*
* @return void*
*/
const void *oc_blockwise_dispatch_block(oc_blockwise_state_t *buffer,
uint32_t block_offset,
uint32_t requested_block_size,
uint32_t *payload_size);
void *oc_blockwise_dispatch_block(oc_blockwise_state_t *buffer,
uint32_t block_offset,
uint32_t requested_block_size,
uint32_t *payload_size);

/**
* @brief handle the incomming block (partial message)
Expand Down
150 changes: 82 additions & 68 deletions messaging/coap/coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,64 +68,77 @@
#include "security/oc_tls_internal.h"
#endif /* OC_SECURITY */

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

/* option format serialization */
#define COAP_SERIALIZE_INT_OPTION(packet, number, field, text) \
if (IS_OPTION(packet, number)) { \
option_length += coap_serialize_int_option(number, current_number, option, \
(packet)->field); \
if (option) { \
OC_DBG(text " [%u]", (unsigned int)(packet)->field); \
option = option_array + option_length; \
do { \
if (IS_OPTION(packet, number)) { \
option_length += coap_serialize_int_option(number, current_number, \
option, (packet)->field); \
if (option) { \
OC_DBG(text " [%u]", (unsigned int)(packet)->field); \
option = option_array + option_length; \
} \
current_number = number; \
} \
current_number = number; \
}
} while (0)

#define COAP_SERIALIZE_BYTE_OPTION(packet, number, field, text) \
if (IS_OPTION(packet, number)) { \
option_length += coap_serialize_array_option(number, current_number, \
option, (packet)->field, \
(packet)->field##_len, '\0'); \
if (option) { \
OC_DBG(text " %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]", \
(unsigned int)(packet)->field##_len, (packet)->field[0], \
(packet)->field[1], (packet)->field[2], (packet)->field[3], \
(packet)->field[4], (packet)->field[5], (packet)->field[6], \
(packet)->field[7]); /* FIXME always prints 8 bytes */ \
option = option_array + option_length; \
do { \
if (IS_OPTION(packet, number)) { \
option_length += coap_serialize_array_option( \
number, current_number, option, (packet)->field, \
(packet)->field##_len, '\0'); \
if (option) { \
OC_DBG(text " %u [0x%02X%02X%02X%02X%02X%02X%02X%02X]", \
(unsigned int)(packet)->field##_len, (packet)->field[0], \
(packet)->field[1], (packet)->field[2], (packet)->field[3], \
(packet)->field[4], (packet)->field[5], (packet)->field[6], \
(packet)->field[7]); /* FIXME always prints 8 bytes */ \
option = option_array + option_length; \
} \
current_number = number; \
} \
current_number = number; \
}
} while (0)

#define COAP_SERIALIZE_STRING_OPTION(packet, number, field, splitter, text) \
if (IS_OPTION(packet, number)) { \
option_length += coap_serialize_array_option( \
number, current_number, option, (uint8_t *)(packet)->field, \
(packet)->field##_len, splitter); \
if (option) { \
OC_DBG(text " [%.*s]", (int)(packet)->field##_len, (packet)->field); \
option = option_array + option_length; \
do { \
if (IS_OPTION(packet, number)) { \
option_length += coap_serialize_array_option( \
number, current_number, option, (uint8_t *)(packet)->field, \
(packet)->field##_len, splitter); \
if (option) { \
OC_DBG(text " [%.*s]", (int)(packet)->field##_len, (packet)->field); \
option = option_array + option_length; \
} \
current_number = number; \
} \
current_number = number; \
}
} while (0)

#define COAP_SERIALIZE_BLOCK_OPTION(packet, number, field, text) \
if (IS_OPTION(packet, number)) { \
uint32_t block = (packet)->field##_num << 4; \
if ((packet)->field##_more) { \
block |= 0x8; \
} \
block |= 0xF & coap_log_2((packet)->field##_size / 16); \
option_length += \
coap_serialize_int_option(number, current_number, option, block); \
if (option) { \
OC_DBG(text " [%lu%s (%u B/blk)]", (unsigned long)(packet)->field##_num, \
(packet)->field##_more ? "+" : "", (packet)->field##_size); \
OC_DBG(text " encoded: 0x%lX", (unsigned long)block); \
option = option_array + option_length; \
do { \
if (IS_OPTION(packet, number)) { \
uint32_t block = (packet)->field##_num << 4; \
if ((packet)->field##_more) { \
block |= 0x8; \
} \
block |= 0xF & coap_log_2((packet)->field##_size / 16); \
option_length += \
coap_serialize_int_option(number, current_number, option, block); \
if (option) { \
OC_DBG(text " [%lu%s (%u B/blk)]", \
(unsigned long)(packet)->field##_num, \
(packet)->field##_more ? "+" : "", (packet)->field##_size); \
OC_DBG(text " encoded: 0x%lX", (unsigned long)block); \
option = option_array + option_length; \
} \
current_number = number; \
} \
current_number = number; \
}
} while (0)

/*---------------------------------------------------------------------------*/
/*- Variables ---------------------------------------------------------------*/
Expand Down Expand Up @@ -402,7 +415,8 @@ coap_get_variable(const char *buffer, size_t length, const char *name,

#ifdef OC_TCP
static size_t
coap_serialize_signal_options(coap_packet_t *packet, uint8_t *option_array)
coap_serialize_signal_options(const coap_packet_t *packet,
uint8_t *option_array)
{
uint8_t *option = option_array;
unsigned int current_number = 0;
Expand Down Expand Up @@ -855,7 +869,7 @@ coap_oscore_parse_option(coap_packet_t *packet, uint8_t *current_option,
}
packet->block2_num = coap_parse_int_option(current_option, option_length);
packet->block2_more = (packet->block2_num & 0x08) >> 3;
packet->block2_size = 16 << (packet->block2_num & 0x07);
packet->block2_size = (uint16_t)(16 << (packet->block2_num & 0x07));
packet->block2_offset = (packet->block2_num & ~0x0000000F)
<< (packet->block2_num & 0x07);
packet->block2_num >>= 4;
Expand All @@ -868,7 +882,7 @@ coap_oscore_parse_option(coap_packet_t *packet, uint8_t *current_option,
}
packet->block1_num = coap_parse_int_option(current_option, option_length);
packet->block1_more = (packet->block1_num & 0x08) >> 3;
packet->block1_size = 16 << (packet->block1_num & 0x07);
packet->block1_size = (uint16_t)(16 << (packet->block1_num & 0x07));
packet->block1_offset = (packet->block1_num & ~0x0000000F)
<< (packet->block1_num & 0x07);
packet->block1_num >>= 4;
Expand Down Expand Up @@ -941,7 +955,8 @@ coap_oscore_parse_options(coap_packet_t *packet, const uint8_t *data,
/* payload marker 0xFF, currently only checking for 0xF* because rest is
* reserved */
if ((current_option[0] & 0xF0) == 0xF0) {
packet->payload = ++current_option;
uint8_t *payload = ++current_option;
packet->payload = payload;
packet->payload_len = (uint32_t)(data_len - (packet->payload - data));

if (packet->transport_type == COAP_TRANSPORT_UDP &&
Expand All @@ -950,7 +965,7 @@ coap_oscore_parse_options(coap_packet_t *packet, const uint8_t *data,
/* null-terminate payload */
}
if (!validate) {
packet->payload[packet->payload_len] =
payload[packet->payload_len] =
'\0'; // TODO: this writes after the payload, if the message was
// shrank so the allocation matches the message length this
// causes a memory corruption
Expand Down Expand Up @@ -1026,19 +1041,20 @@ coap_tcp_set_header_fields(coap_packet_t *packet,
{
packet->buffer[0] = 0x00;
packet->buffer[0] |=
COAP_TCP_HEADER_LEN_MASK & (len) << COAP_TCP_HEADER_LEN_POSITION;
COAP_TCP_HEADER_LEN_MASK & len << COAP_TCP_HEADER_LEN_POSITION;
packet->buffer[0] |= COAP_HEADER_TOKEN_LEN_MASK &
(packet->token_len) << COAP_HEADER_TOKEN_LEN_POSITION;

for (int i = 1; i <= num_extended_length_bytes; i++) {
packet->buffer[i] =
(uint8_t)((extended_len) >> (8 * (num_extended_length_bytes - i)));
(uint8_t)(extended_len >> (8 * (num_extended_length_bytes - i)));
}
packet->buffer[1 + num_extended_length_bytes] = packet->code;
}

static void
coap_tcp_compute_message_length(coap_packet_t *packet, size_t option_length,
coap_tcp_compute_message_length(const coap_packet_t *packet,
size_t option_length,
uint8_t *num_extended_length_bytes,
uint8_t *len, size_t *extended_len)
{
Expand Down Expand Up @@ -1268,13 +1284,8 @@ coap_oscore_serialize_message(coap_packet_t *packet, uint8_t *buffer,
OC_DBG("Token (len %u)", packet->token_len);
OC_LOGbytes(packet->token, packet->token_len);
option = packet->buffer + token_location;
for (uint8_t current_number = 0; current_number < packet->token_len;
++current_number) {
*option = packet->token[current_number];
++option;
}
// memcpy(option, packet->token, packet->token_len);
// option += packet->token_len;
memcpy(option, packet->token, packet->token_len);
option += packet->token_len;
} else {
OC_DBG("Inner CoAP code: %d", packet->code);
++header_length_calculation;
Expand Down Expand Up @@ -1371,7 +1382,7 @@ coap_udp_parse_message(coap_packet_t *packet, uint8_t *data, size_t data_len,
packet->token_len = (COAP_HEADER_TOKEN_LEN_MASK & packet->buffer[0]) >>
COAP_HEADER_TOKEN_LEN_POSITION;
packet->code = packet->buffer[1];
packet->mid = packet->buffer[2] << 8 | packet->buffer[3];
packet->mid = (uint16_t)(packet->buffer[2] << 8 | packet->buffer[3]);

if (packet->version != 1) {
OC_WRN("CoAP version must be 1");
Expand Down Expand Up @@ -1527,7 +1538,8 @@ int
coap_set_header_content_format(coap_packet_t *packet,
oc_content_format_t format)
{
packet->content_format = format;
assert(format > 0 && format <= UINT16_MAX);
packet->content_format = (uint16_t)format;
SET_OPTION(packet, COAP_OPTION_CONTENT_FORMAT);
return 1;
}
Expand Down Expand Up @@ -1600,10 +1612,11 @@ coap_get_header_proxy_uri(const coap_packet_t *packet, const char **uri)
}

size_t
coap_set_header_proxy_uri(coap_packet_t *packet, const char *uri)
coap_set_header_proxy_uri(coap_packet_t *packet, const char *uri,
size_t uri_len)
{
packet->proxy_uri = uri;
packet->proxy_uri_len = strlen(uri);
packet->proxy_uri_len = uri_len;

SET_OPTION(packet, COAP_OPTION_PROXY_URI);
return packet->proxy_uri_len;
Expand Down Expand Up @@ -1705,14 +1718,15 @@ coap_get_header_uri_query(const coap_packet_t *packet, const char **query)

#ifdef OC_CLIENT
size_t
coap_set_header_uri_query(coap_packet_t *packet, const char *query)
coap_set_header_uri_query(coap_packet_t *packet, const char *query,
size_t query_len)
{
while (query[0] == '?') {
++query;
}

packet->uri_query = query;
packet->uri_query_len = strlen(query);
packet->uri_query_len = query_len;

SET_OPTION(packet, COAP_OPTION_URI_QUERY);
return packet->uri_query_len;
Expand Down Expand Up @@ -1941,9 +1955,9 @@ coap_get_payload(const coap_packet_t *packet, const uint8_t **payload)
}

uint32_t
coap_set_payload(coap_packet_t *packet, const void *payload, uint32_t length)
coap_set_payload(coap_packet_t *packet, uint8_t *payload, uint32_t length)
{
packet->payload = (uint8_t *)payload;
packet->payload = payload;
#ifdef OC_TCP
if (packet->transport_type == COAP_TRANSPORT_TCP) {
packet->payload_len = length;
Expand Down
Loading

0 comments on commit 8ebcc6a

Please sign in to comment.