Skip to content

Commit ae0579f

Browse files
author
Arto Kinnunen
authored
Update content_type to sn_coap_content_format_e (#27)
-Update content_type to be sn_coap_content_format_e instead of uint8_t to avoid type conversion (bugs). -Fix compiler warnings
1 parent 18102ed commit ae0579f

File tree

8 files changed

+32
-29
lines changed

8 files changed

+32
-29
lines changed

coap-service/coap_service_api.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ typedef int coap_service_security_done_cb(int8_t service_id, uint8_t address[sta
119119
* \param listen_port Port that Application wants to use for communicate with coap server.
120120
* \param service_options Options of the current service.
121121
* \param *start_ptr Callback to inform security handling is started and to fetch device password.
122-
* \param *security_done_cb Callback to inform security handling is done.
122+
* \param *coap_security_done_cb Callback to inform security handling is done.
123123
*
124124
* \return service_id / -1 for failure
125125
*/
126-
extern int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options, coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *security_done_cb);
126+
extern int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options, coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb);
127127

128128
/**
129129
* \brief Service delete
@@ -245,7 +245,7 @@ extern int8_t coap_service_unregister_uri(int8_t service_id, const char *uri);
245245
* \return msg_id Id number of the current message.
246246
*/
247247
extern uint16_t coap_service_request_send(int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16], uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri,
248-
uint8_t cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb);
248+
sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb);
249249

250250
/**
251251
* \brief Sends CoAP service response
@@ -260,7 +260,7 @@ extern uint16_t coap_service_request_send(int8_t service_id, uint8_t options, co
260260
* \return -1 For failure
261261
*- 0 For success
262262
*/
263-
extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, int32_t content_type, const uint8_t *payload_ptr,uint16_t payload_len);
263+
extern int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len);
264264

265265
extern int8_t coap_service_set_handshake_timeout(int8_t service_id, uint32_t min, uint32_t max);
266266
#ifdef __cplusplus

source/coap_connection_handler.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ static int send_to_socket(int8_t socket_id, uint8_t *address_ptr, uint16_t port,
317317

318318
static int receive_from_socket(int8_t socket_id, unsigned char *buf, size_t len)
319319
{
320+
(void)len;
320321
internal_socket_t *sock = int_socket_find_by_socket_id(socket_id);
321322
if( sock->data && sock->data_len > 0 ){
322323
memcpy( buf, sock->data, sock->data_len );

source/coap_message_handler.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
248248

249249
uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16],
250250
uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri,
251-
uint8_t cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb)
251+
sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb)
252252
{
253253
coap_transaction_t *transaction_ptr;
254254
sn_coap_hdr_s request;
@@ -280,7 +280,7 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
280280
request.msg_code = msg_code;
281281
request.uri_path_ptr = (uint8_t *)uri;
282282
request.uri_path_len = strlen(uri);
283-
coap_service_build_content_format(&request, (sn_coap_content_format_e)cont_type);
283+
coap_service_build_content_format(&request, cont_type);
284284

285285
do{
286286
randLIB_get_n_bytes_random(token,4);
@@ -313,14 +313,15 @@ uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t se
313313
}
314314

315315
//TODO: refactor this to use nsdl
316-
int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, int32_t content_type, const uint8_t *payload_ptr, uint16_t payload_len)
316+
int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len)
317317
{
318318
coap_transaction_t *transaction_ptr;
319319
sn_coap_hdr_s *response;
320320
sn_nsdl_addr_s dst_addr;
321321
uint16_t data_len;
322322
uint8_t *data_ptr;
323323
(void) options;
324+
(void)service_id;
324325

325326
tr_debug("Service %d, send CoAP response", service_id);
326327
if (!request_ptr || !handle) {
@@ -345,7 +346,7 @@ int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t ser
345346
}
346347
response->payload_len = payload_len;
347348
response->payload_ptr = (uint8_t *) payload_ptr; // Cast away const and trust that nsdl doesn't modify...
348-
coap_service_build_content_format(response, (sn_coap_content_format_e)content_type);
349+
coap_service_build_content_format(response, content_type);
349350

350351
data_len = sn_coap_builder_calc_needed_packet_data_size(response);
351352
data_ptr = own_alloc(data_len);

source/coap_security_handler.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const int ECJPAKE_SUITES[] = {
2626
};
2727
#endif
2828

29-
const static int PSK_SUITES[] = {
29+
static const int PSK_SUITES[] = {
3030
MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA256,
3131
MBEDTLS_TLS_PSK_WITH_AES_256_CCM_8,
3232
MBEDTLS_TLS_PSK_WITH_AES_128_CCM_8,
@@ -204,7 +204,7 @@ static int simple_cookie_check(void *ctx,
204204
}
205205

206206
/**** Key export function ****/
207-
207+
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
208208
static int export_key_block(void *ctx,
209209
const unsigned char *mk, const unsigned char *kb,
210210
size_t maclen, size_t keylen, size_t ivlen)
@@ -224,6 +224,7 @@ static int export_key_block(void *ctx,
224224
memcpy(p_key_block->value, kb /* + (2 * maclen)*/, (2 * keylen) + (2 * ivlen));
225225
return 0;
226226
}
227+
#endif
227228

228229
int coap_security_handler_configure_keys( coap_security_t *sec, coap_security_keys_t keys )
229230
{
@@ -559,6 +560,7 @@ int f_recv(void *ctx, unsigned char *buf, size_t len){
559560
int entropy_poll( void *ctx, unsigned char *output, size_t len,
560561
size_t *olen )
561562
{
563+
(void)ctx;
562564
//TODO: change to more secure random
563565
randLIB_seed_random();
564566
char *c = (char*)ns_dyn_mem_temporary_alloc(len);

source/coap_service_api.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ typedef struct uri_registration {
3636
typedef NS_LIST_HEAD(uri_registration_t, link) uri_registration_list_t;
3737

3838
typedef struct coap_service {
39-
coap_service_security_done_cb *security_done_cb;
39+
coap_service_security_done_cb *coap_security_done_cb;
4040
coap_service_security_start_cb *security_start_cb;
4141
coap_service_virtual_socket_send_cb *virtual_socket_send_cb;
4242
uri_registration_list_t uri_list;
@@ -244,8 +244,8 @@ static void sec_done_cb(int8_t socket_id, uint8_t address[static 16], uint16_t p
244244
{
245245
//TODO: this is not enough if shared socket. Inform all!
246246
coap_service_t *this = service_find_by_socket(socket_id);
247-
if (this && this->security_done_cb) { // secure done callback
248-
this->security_done_cb(this->service_id, address, keyblock);
247+
if (this && this->coap_security_done_cb) { // secure done callback
248+
this->coap_security_done_cb(this->service_id, address, keyblock);
249249
}
250250

251251
//TODO refactor this away. There should be no transaction_ptr(s) before done_cb has been called
@@ -276,7 +276,7 @@ static int get_passwd_cb(int8_t socket_id, uint8_t address[static 16], uint16_t
276276
}
277277

278278
int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options,
279-
coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *security_done_cb)
279+
coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb)
280280
{
281281
(void) interface_id;
282282

@@ -295,7 +295,7 @@ int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_
295295
this->service_options = service_options;
296296

297297
this->security_start_cb = start_ptr;
298-
this->security_done_cb = security_done_cb;
298+
this->coap_security_done_cb = coap_security_done_cb;
299299

300300
if (tasklet_id == -1) {
301301
tr_debug("service tasklet init");
@@ -447,13 +447,13 @@ int8_t coap_service_unregister_uri(int8_t service_id, const char *uri)
447447
}
448448

449449
uint16_t coap_service_request_send(int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16], uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri,
450-
uint8_t cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb){
450+
sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb){
451451
//TODO: coap_service_response_recv is an ugly cast, this should be refactored away + sn_coap_hdr_s MUST NOT be exposed to users of coap-service!
452452
//Callback would be still needed, but where to store callback?
453453
return coap_message_handler_request_send(coap_service_handle, service_id, options, destination_addr, destination_port, msg_type, msg_code, uri, cont_type, payload_ptr, payload_len, request_response_cb);
454454
}
455455

456-
int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, int32_t content_type, const uint8_t *payload_ptr,uint16_t payload_len){
456+
int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len){
457457
return coap_message_handler_response_send(coap_service_handle, service_id, options, request_ptr, message_code, content_type, payload_ptr, payload_len);
458458
}
459459

source/include/coap_message_handler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ extern int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle,
7373
uint8_t *data_ptr, uint16_t data_len, int16_t (cb)(int8_t, sn_coap_hdr_s *, coap_transaction_t *));
7474

7575
extern uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16],
76-
uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, uint8_t cont_type,
76+
uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, sn_coap_content_format_e cont_type,
7777
const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb);
7878

7979
extern int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code,
80-
int32_t content_type, const uint8_t *payload_ptr, uint16_t payload_len);
80+
sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len);
8181

8282
extern int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_time);
8383

test/coap-service/unittest/stub/coap_message_handler_stub.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@ int16_t coap_message_handler_coap_msg_process(coap_msg_handler_t *handle, int8_t
3939
return coap_message_handler_stub.int16_value;
4040
}
4141

42-
uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16],
43-
uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, uint8_t cont_type,
44-
const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb)
42+
uint16_t coap_message_handler_request_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options,
43+
const uint8_t destination_addr[static 16], uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code,
44+
const char *uri, sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb)
4545
{
4646
return coap_message_handler_stub.uint16_value;
4747
}
4848

49-
int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code,
50-
int32_t content_type, const uint8_t *payload_ptr, uint16_t payload_len)
49+
int8_t coap_message_handler_response_send(coap_msg_handler_t *handle, int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code,sn_coap_content_format_e content_type, const uint8_t *payload_ptr, uint16_t payload_len)
5150
{
5251
return coap_message_handler_stub.int8_value;
5352
}
@@ -57,4 +56,3 @@ int8_t coap_message_handler_exec(coap_msg_handler_t *handle, uint32_t current_ti
5756
return coap_message_handler_stub.int8_value;
5857
}
5958

60-

test/coap-service/unittest/stub/coap_service_api_stub.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "net_interface.h"
2020

2121
int8_t coap_service_initialize(int8_t interface_id, uint16_t listen_port, uint8_t service_options,
22-
coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *security_done_cb)
22+
coap_service_security_start_cb *start_ptr, coap_service_security_done_cb *coap_security_done_cb)
2323
{
2424
return 0;
2525
}
@@ -48,12 +48,13 @@ int8_t coap_service_unregister_uri(int8_t service_id, const char *uri)
4848
return 0;
4949
}
5050

51-
uint16_t coap_service_request_send(int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16], uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri,
52-
uint8_t cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_message_handler_response_recv *request_response_cb){
51+
uint16_t coap_service_request_send(int8_t service_id, uint8_t options, const uint8_t destination_addr[static 16], uint16_t destination_port, sn_coap_msg_type_e msg_type, sn_coap_msg_code_e msg_code, const char *uri, sn_coap_content_format_e cont_type, const uint8_t *payload_ptr, uint16_t payload_len, coap_service_response_recv *request_response_cb)
52+
{
5353
return 0;
5454
}
5555

56-
int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, int32_t content_type, const uint8_t *payload_ptr,uint16_t payload_len){
56+
int8_t coap_service_response_send(int8_t service_id, uint8_t options, sn_coap_hdr_s *request_ptr, sn_coap_msg_code_e message_code, sn_coap_content_format_e content_type, const uint8_t *payload_ptr,uint16_t payload_len)
57+
{
5758
return 0;
5859
}
5960

0 commit comments

Comments
 (0)