From cc5de7b5e21525fe70645480f962ed1bffcd0a3e Mon Sep 17 00:00:00 2001 From: Jin-Seong Kim Date: Wed, 19 Apr 2017 17:15:49 +0900 Subject: [PATCH] external/wakaama: update core APIs to support TCP/TLS as per RFC This commit is related to er-coap TCP/TLS support according to changes of wakaama er-coap API, updates wakaama core APIs to support TCP/TLS feature. - lwm2m_context_t : added new member, coap_protocol_t protocol - lwm2m_init2 function is newly added to intialize coap protocol member in lwm2m_context_t - transaction_new : added new argument, coap_protocol_t proto - lwm2m_buffer_send : added new argument, coap_protocol_t proto - core functions using coap_init_message, coap_parse_message are updated Change-Id: Icc7e3a7ac64e703255aafbab6c3f3d04dbb82cb4 Signed-off-by: Jin-Seong Kim --- external/wakaama/core/bootstrap.c | 8 ++++---- external/wakaama/core/internals.h | 2 +- external/wakaama/core/liblwm2m.c | 18 ++++++++++++++++++ external/wakaama/core/liblwm2m.h | 8 +++++++- external/wakaama/core/management.c | 6 +++--- external/wakaama/core/observe.c | 13 ++++++++----- external/wakaama/core/packet.c | 14 ++++++++------ external/wakaama/core/registration.c | 6 +++--- external/wakaama/core/transaction.c | 9 ++++++--- external/wakaama/examples/shared/connection.c | 8 +++++--- external/wakaama/examples/shared/dtlsconnection.c | 8 +++++--- external/wakaama/examples/shared/dtlsconnection.h | 2 +- 12 files changed, 69 insertions(+), 33 deletions(-) diff --git a/external/wakaama/core/bootstrap.c b/external/wakaama/core/bootstrap.c index f673a97..7829628 100644 --- a/external/wakaama/core/bootstrap.c +++ b/external/wakaama/core/bootstrap.c @@ -99,7 +99,7 @@ static void prv_requestBootstrap(lwm2m_context_t * context, LOG("Bootstrap server connection opened"); - transaction = transaction_new(bootstrapServer->sessionH, COAP_POST, NULL, NULL, context->nextMID++, 4, NULL); + transaction = transaction_new(bootstrapServer->sessionH, context->protocol, COAP_POST, NULL, NULL, context->nextMID++, 4, NULL); if (transaction == NULL) { bootstrapServer->status = STATE_BS_FAILING; @@ -646,7 +646,7 @@ int lwm2m_bootstrap_delete(lwm2m_context_t * contextP, bs_data_t * dataP; LOG_URI(uriP); - transaction = transaction_new(sessionH, COAP_DELETE, NULL, uriP, contextP->nextMID++, 4, NULL); + transaction = transaction_new(sessionH, contextP->protocol, COAP_DELETE, NULL, uriP, contextP->nextMID++, 4, NULL); if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; dataP = (bs_data_t *)lwm2m_malloc(sizeof(bs_data_t)); @@ -693,7 +693,7 @@ int lwm2m_bootstrap_write(lwm2m_context_t * contextP, return COAP_400_BAD_REQUEST; } - transaction = transaction_new(sessionH, COAP_PUT, NULL, uriP, contextP->nextMID++, 4, NULL); + transaction = transaction_new(sessionH, contextP->protocol, COAP_PUT, NULL, uriP, contextP->nextMID++, 4, NULL); if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; coap_set_header_content_type(transaction->message, format); @@ -725,7 +725,7 @@ int lwm2m_bootstrap_finish(lwm2m_context_t * contextP, bs_data_t * dataP; LOG("Entering"); - transaction = transaction_new(sessionH, COAP_POST, NULL, NULL, contextP->nextMID++, 4, NULL); + transaction = transaction_new(sessionH, contextP->protocol, COAP_POST, NULL, NULL, contextP->nextMID++, 4, NULL); if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; coap_set_header_uri_path(transaction->message, "/"URI_BOOTSTRAP_SEGMENT); diff --git a/external/wakaama/core/internals.h b/external/wakaama/core/internals.h index 8632357..e4f3517 100644 --- a/external/wakaama/core/internals.h +++ b/external/wakaama/core/internals.h @@ -261,7 +261,7 @@ coap_status_t object_createInstance(lwm2m_context_t * contextP, lwm2m_uri_t * ur coap_status_t object_writeInstance(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_data_t * dataP); // defined in transaction.c -lwm2m_transaction_t * transaction_new(void * sessionH, coap_method_t method, char * altPath, lwm2m_uri_t * uriP, uint16_t mID, uint8_t token_len, uint8_t* token); +lwm2m_transaction_t * transaction_new(void * sessionH, coap_protocol_t proto, coap_method_t method, char * altPath, lwm2m_uri_t * uriP, uint16_t mID, uint8_t token_len, uint8_t* token); int transaction_send(lwm2m_context_t * contextP, lwm2m_transaction_t * transacP); void transaction_free(lwm2m_transaction_t * transacP); void transaction_remove(lwm2m_context_t * contextP, lwm2m_transaction_t * transacP); diff --git a/external/wakaama/core/liblwm2m.c b/external/wakaama/core/liblwm2m.c index 599a9e2..e689e7d 100644 --- a/external/wakaama/core/liblwm2m.c +++ b/external/wakaama/core/liblwm2m.c @@ -69,6 +69,24 @@ lwm2m_context_t * lwm2m_init(void * userData) contextP->userData = userData; srand((int)lwm2m_gettime()); contextP->nextMID = rand(); + contextP->protocol = COAP_UDP; + } + + return contextP; +} + +lwm2m_context_t * lwm2m_init2(void * userData, coap_protocol_t proto) +{ + lwm2m_context_t * contextP; + + LOG("Entering"); + contextP = (lwm2m_context_t *)lwm2m_malloc(sizeof(lwm2m_context_t)); + if (NULL != contextP) { + memset(contextP, 0, sizeof(lwm2m_context_t)); + contextP->userData = userData; + srand((int)lwm2m_gettime()); + contextP->nextMID = rand(); + contextP->protocol = proto; } return contextP; diff --git a/external/wakaama/core/liblwm2m.h b/external/wakaama/core/liblwm2m.h index a1f91ed..b3b7cd9 100644 --- a/external/wakaama/core/liblwm2m.h +++ b/external/wakaama/core/liblwm2m.h @@ -62,6 +62,8 @@ extern "C" { #include #include +#include "er-coap-13/er-coap-13.h" + #ifdef LWM2M_SERVER_MODE #ifndef LWM2M_SUPPORT_JSON #define LWM2M_SUPPORT_JSON @@ -124,7 +126,8 @@ void lwm2m_close_connection(void * sessionH, void * userData); // sessionH: session handle identifying the peer (opaque to the core) // buffer, length: data to send // userData: parameter to lwm2m_init() -uint8_t lwm2m_buffer_send(void * sessionH, uint8_t * buffer, size_t length, void * userData); +// proto : parameter from lwm2m_init2(), to identifying type of protocol in er-coap +uint8_t lwm2m_buffer_send(void * sessionH, uint8_t * buffer, size_t length, void * userData, coap_protocol_t proto); // Compare two session handles // Returns true if the two sessions identify the same peer. false otherwise. // userData: parameter to lwm2m_init() @@ -667,11 +670,14 @@ typedef struct uint16_t nextMID; lwm2m_transaction_t * transactionList; void * userData; + coap_protocol_t protocol; /**/ } lwm2m_context_t; // initialize a liblwm2m context. lwm2m_context_t * lwm2m_init(void * userData); +// initialize a liblwm2m context with protocol +lwm2m_context_t * lwm2m_init2(void * userData, coap_protocol_t proto); // close a liblwm2m context. void lwm2m_close(lwm2m_context_t * contextP); diff --git a/external/wakaama/core/management.c b/external/wakaama/core/management.c index d864f6a..008521d 100644 --- a/external/wakaama/core/management.c +++ b/external/wakaama/core/management.c @@ -425,7 +425,7 @@ static int prv_makeOperation(lwm2m_context_t * contextP, clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); if (clientP == NULL) return COAP_404_NOT_FOUND; - transaction = transaction_new(clientP->sessionH, method, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); + transaction = transaction_new(clientP->sessionH, contextP->protocol, method, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; if (method == COAP_GET) @@ -617,7 +617,7 @@ int lwm2m_dm_write_attributes(lwm2m_context_t * contextP, clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); if (clientP == NULL) return COAP_404_NOT_FOUND; - transaction = transaction_new(clientP->sessionH, COAP_PUT, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); + transaction = transaction_new(clientP->sessionH, contextP->protocol, COAP_PUT, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; if (callback != NULL) @@ -747,7 +747,7 @@ int lwm2m_dm_discover(lwm2m_context_t * contextP, clientP = (lwm2m_client_t *)lwm2m_list_find((lwm2m_list_t *)contextP->clientList, clientID); if (clientP == NULL) return COAP_404_NOT_FOUND; - transaction = transaction_new(clientP->sessionH, COAP_GET, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); + transaction = transaction_new(clientP->sessionH, contextP->protocol, COAP_GET, clientP->altPath, uriP, contextP->nextMID++, 4, NULL); if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; coap_set_header_accept(transaction->message, LWM2M_CONTENT_LINK); diff --git a/external/wakaama/core/observe.c b/external/wakaama/core/observe.c index 595d0ec..19f985a 100644 --- a/external/wakaama/core/observe.c +++ b/external/wakaama/core/observe.c @@ -437,6 +437,7 @@ void observe_step(lwm2m_context_t * contextP, time_t * timeoutP) { lwm2m_observed_t * targetP; + coap_protocol_t proto = contextP->protocol; LOG("Entering"); for (targetP = contextP->observedList ; targetP != NULL ; targetP = targetP->next) @@ -652,7 +653,7 @@ void observe_step(lwm2m_context_t * contextP, break; } } - coap_init_message(message, COAP_TYPE_NON, COAP_205_CONTENT, 0); + coap_init_message(message, proto, COAP_TYPE_NON, COAP_205_CONTENT, 0); coap_set_header_content_type(message, format); coap_set_payload(message, buffer, length); } @@ -864,7 +865,7 @@ int lwm2m_observe(lwm2m_context_t * contextP, token[2] = observationP->id >> 8; token[3] = observationP->id & 0xFF; - transactionP = transaction_new(clientP->sessionH, COAP_GET, clientP->altPath, uriP, contextP->nextMID++, 4, token); + transactionP = transaction_new(clientP->sessionH, contextP->protocol, COAP_GET, clientP->altPath, uriP, contextP->nextMID++, 4, token); if (transactionP == NULL) { lwm2m_free(observationP); @@ -909,7 +910,7 @@ int lwm2m_observe_cancel(lwm2m_context_t * contextP, lwm2m_transaction_t * transactionP; cancellation_data_t * cancelP; - transactionP = transaction_new(clientP->sessionH, COAP_GET, clientP->altPath, uriP, contextP->nextMID++, 0, NULL); + transactionP = transaction_new(clientP->sessionH, contextP->protocol, COAP_GET, clientP->altPath, uriP, contextP->nextMID++, 0, NULL); if (transactionP == NULL) { return COAP_500_INTERNAL_SERVER_ERROR; @@ -960,6 +961,8 @@ bool observe_handleNotify(lwm2m_context_t * contextP, lwm2m_observation_t * observationP; uint32_t count; + coap_protocol_t proto = contextP->protocol; + LOG("Entering"); token_len = coap_get_header_token(message, (const uint8_t **)&tokenP); if (token_len != sizeof(uint32_t)) return false; @@ -975,13 +978,13 @@ bool observe_handleNotify(lwm2m_context_t * contextP, observationP = (lwm2m_observation_t *)lwm2m_list_find((lwm2m_list_t *)clientP->observationList, obsID); if (observationP == NULL) { - coap_init_message(response, COAP_TYPE_RST, 0, message->mid); + coap_init_message(response, proto, COAP_TYPE_RST, 0, message->mid); message_send(contextP, response, fromSessionH); } else { if (message->type == COAP_TYPE_CON ) { - coap_init_message(response, COAP_TYPE_ACK, 0, message->mid); + coap_init_message(response, proto, COAP_TYPE_ACK, 0, message->mid); message_send(contextP, response, fromSessionH); } observationP->callback(clientID, diff --git a/external/wakaama/core/packet.c b/external/wakaama/core/packet.c index 82de962..de0fa4d 100644 --- a/external/wakaama/core/packet.c +++ b/external/wakaama/core/packet.c @@ -203,11 +203,13 @@ void lwm2m_handle_packet(lwm2m_context_t * contextP, void * fromSessionH) { coap_status_t coap_error_code = NO_ERROR; + coap_protocol_t proto = contextP->protocol; + static coap_packet_t message[1]; static coap_packet_t response[1]; LOG("Entering"); - coap_error_code = coap_parse_message(message, buffer, (uint16_t)length); + coap_error_code = coap_parse_message(message, proto, buffer, (uint16_t)length); if (coap_error_code == NO_ERROR) { LOG_ARG("Parsed: ver %u, type %u, tkl %u, code %u.%.2u, mid %u, Content type: %d", @@ -224,12 +226,12 @@ void lwm2m_handle_packet(lwm2m_context_t * contextP, if (message->type == COAP_TYPE_CON) { /* Reliable CON requests are answered with an ACK. */ - coap_init_message(response, COAP_TYPE_ACK, COAP_205_CONTENT, message->mid); + coap_init_message(response, proto, COAP_TYPE_ACK, COAP_205_CONTENT, message->mid); } else { /* Unreliable NON requests are answered with a NON as well. */ - coap_init_message(response, COAP_TYPE_NON, COAP_205_CONTENT, contextP->nextMID++); + coap_init_message(response, proto, COAP_TYPE_NON, COAP_205_CONTENT, contextP->nextMID++); } /* mirror token */ @@ -368,7 +370,7 @@ void lwm2m_handle_packet(lwm2m_context_t * contextP, #endif if (!done && message->type == COAP_TYPE_CON ) { - coap_init_message(response, COAP_TYPE_ACK, 0, message->mid); + coap_init_message(response, proto, COAP_TYPE_ACK, 0, message->mid); coap_error_code = message_send(contextP, response, fromSessionH); } } @@ -405,7 +407,7 @@ void lwm2m_handle_packet(lwm2m_context_t * contextP, coap_error_code = COAP_500_INTERNAL_SERVER_ERROR; } /* Reuse input buffer for error message. */ - coap_init_message(message, COAP_TYPE_ACK, coap_error_code, message->mid); + coap_init_message(message, proto, COAP_TYPE_ACK, coap_error_code, message->mid); coap_set_payload(message, coap_error_message, strlen(coap_error_message)); message_send(contextP, message, fromSessionH); } @@ -433,7 +435,7 @@ coap_status_t message_send(lwm2m_context_t * contextP, LOG_ARG("coap_serialize_message() returned %d", pktBufferLen); if (0 != pktBufferLen) { - result = lwm2m_buffer_send(sessionH, pktBuffer, pktBufferLen, contextP->userData); + result = lwm2m_buffer_send(sessionH, pktBuffer, pktBufferLen, contextP->userData, contextP->protocol); } lwm2m_free(pktBuffer); } diff --git a/external/wakaama/core/registration.c b/external/wakaama/core/registration.c index a7422ce..6697941 100644 --- a/external/wakaama/core/registration.c +++ b/external/wakaama/core/registration.c @@ -185,7 +185,7 @@ static uint8_t prv_register(lwm2m_context_t * contextP, if (NULL == server->sessionH) return COAP_503_SERVICE_UNAVAILABLE; - transaction = transaction_new(server->sessionH, COAP_POST, NULL, NULL, contextP->nextMID++, 4, NULL); + transaction = transaction_new(server->sessionH, contextP->protocol, COAP_POST, NULL, NULL, contextP->nextMID++, 4, NULL); if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; coap_set_header_uri_path(transaction->message, "/"URI_REGISTRATION_SEGMENT); @@ -238,7 +238,7 @@ static int prv_updateRegistration(lwm2m_context_t * contextP, uint8_t payload[512]; int payload_length; - transaction = transaction_new(server->sessionH, COAP_POST, NULL, NULL, contextP->nextMID++, 4, NULL); + transaction = transaction_new(server->sessionH, contextP->protocol, COAP_POST, NULL, NULL, contextP->nextMID++, 4, NULL); if (transaction == NULL) return COAP_500_INTERNAL_SERVER_ERROR; coap_set_header_uri_path(transaction->message, server->location); @@ -455,7 +455,7 @@ void registration_deregister(lwm2m_context_t * contextP, return; } - transaction = transaction_new(serverP->sessionH, COAP_DELETE, NULL, NULL, contextP->nextMID++, 4, NULL); + transaction = transaction_new(serverP->sessionH, contextP->protocol, COAP_DELETE, NULL, NULL, contextP->nextMID++, 4, NULL); if (transaction == NULL) return; coap_set_header_uri_path(transaction->message, serverP->location); diff --git a/external/wakaama/core/transaction.c b/external/wakaama/core/transaction.c index 2f968e7..79f2941 100644 --- a/external/wakaama/core/transaction.c +++ b/external/wakaama/core/transaction.c @@ -142,6 +142,7 @@ static int prv_checkFinished(lwm2m_transaction_t * transacP, } lwm2m_transaction_t * transaction_new(void * sessionH, + coap_protocol_t proto, coap_method_t method, char * altPath, lwm2m_uri_t * uriP, @@ -167,7 +168,7 @@ lwm2m_transaction_t * transaction_new(void * sessionH, transacP->message = lwm2m_malloc(sizeof(coap_packet_t)); if (NULL == transacP->message) goto error; - coap_init_message(transacP->message, COAP_TYPE_CON, method, mID); + coap_init_message(transacP->message, proto, COAP_TYPE_CON, method, mID); transacP->peerH = sessionH; @@ -262,6 +263,8 @@ bool transaction_handleResponse(lwm2m_context_t * contextP, bool reset = false; lwm2m_transaction_t * transacP; + coap_protocol_t proto = contextP->protocol; + LOG("Entering"); transacP = contextP->transactionList; @@ -291,7 +294,7 @@ bool transaction_handleResponse(lwm2m_context_t * contextP, { if (COAP_TYPE_CON == message->type && NULL != response) { - coap_init_message(response, COAP_TYPE_ACK, 0, message->mid); + coap_init_message(response, proto, COAP_TYPE_ACK, 0, message->mid); message_send(contextP, response, fromSessionH); } @@ -383,7 +386,7 @@ int transaction_send(lwm2m_context_t * contextP, if (COAP_MAX_RETRANSMIT + 1 >= transacP->retrans_counter) { - (void)lwm2m_buffer_send(transacP->peerH, transacP->buffer, transacP->buffer_len, contextP->userData); + (void)lwm2m_buffer_send(transacP->peerH, transacP->buffer, transacP->buffer_len, contextP->userData, contextP->protocol); transacP->retrans_time += timeout; transacP->retrans_counter += 1; diff --git a/external/wakaama/examples/shared/connection.c b/external/wakaama/examples/shared/connection.c index b16a824..4312d67 100644 --- a/external/wakaama/examples/shared/connection.c +++ b/external/wakaama/examples/shared/connection.c @@ -229,7 +229,8 @@ void connection_free(connection_t * connList) int connection_send(connection_t *connP, uint8_t * buffer, - size_t length) + size_t length, + coap_protocol_t proto) { int nbSent; size_t offset; @@ -300,7 +301,8 @@ int connection_send(connection_t *connP, uint8_t lwm2m_buffer_send(void * sessionH, uint8_t * buffer, size_t length, - void * userdata) + void * userdata, + coap_protocol_t proto) { connection_t * connP = (connection_t*) sessionH; @@ -310,7 +312,7 @@ uint8_t lwm2m_buffer_send(void * sessionH, return COAP_500_INTERNAL_SERVER_ERROR ; } - if (-1 == connection_send(connP, buffer, length)) + if (-1 == connection_send(connP, buffer, length, proto)) { fprintf(stderr, "#> failed sending %lu bytes\r\n", length); return COAP_500_INTERNAL_SERVER_ERROR ; diff --git a/external/wakaama/examples/shared/dtlsconnection.c b/external/wakaama/examples/shared/dtlsconnection.c index c9ac973..e516180 100644 --- a/external/wakaama/examples/shared/dtlsconnection.c +++ b/external/wakaama/examples/shared/dtlsconnection.c @@ -542,7 +542,8 @@ void connection_free(dtls_connection_t * connList) dtlsContext = NULL; } -int connection_send(dtls_connection_t *connP, uint8_t * buffer, size_t length){ +int connection_send(dtls_connection_t *connP, uint8_t * buffer, size_t length, coap_protocol_t proto) +{ if (connP->dtlsSession == NULL) { // no security if ( 0 != send_data(connP, buffer, length)) { @@ -612,7 +613,8 @@ int connection_rehandshake(dtls_connection_t *connP, bool sendCloseNotify) { uint8_t lwm2m_buffer_send(void * sessionH, uint8_t * buffer, size_t length, - void * userdata) + void * userdata, + coap_protocol_t proto) { dtls_connection_t * connP = (dtls_connection_t*) sessionH; @@ -622,7 +624,7 @@ uint8_t lwm2m_buffer_send(void * sessionH, return COAP_500_INTERNAL_SERVER_ERROR ; } - if (-1 == connection_send(connP, buffer, length)) + if (-1 == connection_send(connP, buffer, length, proto)) { fprintf(stderr, "#> failed sending %lu bytes\r\n", length); return COAP_500_INTERNAL_SERVER_ERROR ; diff --git a/external/wakaama/examples/shared/dtlsconnection.h b/external/wakaama/examples/shared/dtlsconnection.h index aa0db20..9c28ce3 100644 --- a/external/wakaama/examples/shared/dtlsconnection.h +++ b/external/wakaama/examples/shared/dtlsconnection.h @@ -65,7 +65,7 @@ dtls_connection_t * connection_create(dtls_connection_t * connList, int sock, lw void connection_free(dtls_connection_t * connList); -int connection_send(dtls_connection_t *connP, uint8_t * buffer, size_t length); +int connection_send(dtls_connection_t *connP, uint8_t * buffer, size_t length, coap_protocol_t proto); int connection_handle_packet(dtls_connection_t *connP, uint8_t * buffer, size_t length); // rehandshake a connection, useful when your NAT timed out and your client has a new IP/PORT -- 2.7.4