Skip to content

Commit

Permalink
Support the preferred content format for Bootstrap-Request
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Bertin <sbertin@telular.com>
  • Loading branch information
sbertin-telular committed Feb 16, 2021
1 parent 1b87c37 commit da23477
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 115 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ If DTLS feature enable:
To launch a bootstrap session:
``./lwm2mclient -b``

***Note:***
*If used with the examples/bootstrap_server the client needs to be built with LWM2M_SUPPORT_TLV*

### Simpler test client example

In the any directory, run the following commands:
Expand Down
70 changes: 69 additions & 1 deletion core/bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* Bosch Software Innovations GmbH - Please refer to git log
* David Navarro, Intel Corporation - Please refer to git log
* Tuve Nordius, Husqvarna Group - Please refer to git log
* Scott Bertin, AMETEK, Inc. - Please refer to git log
*
*******************************************************************************/

Expand Down Expand Up @@ -93,6 +94,34 @@ static void prv_requestBootstrap(lwm2m_context_t * context,
}
query_length += res;

#ifndef LWM2M_VERSION_1_0
// TODO Add SenML CBOR as a preferred content type when supported.
#if defined(LWM2M_SUPPORT_SENML_JSON) || defined(LWM2M_SUPPORT_TLV)
res = utils_stringCopy(query + query_length, PRV_QUERY_BUFFER_LENGTH - query_length, QUERY_DELIMITER QUERY_PCT);
if (res < 0)
{
bootstrapServer->status = STATE_BS_FAILING;
return;
}
query_length += res;
#if defined(LWM2M_SUPPORT_SENML_JSON)
res = utils_stringCopy(query + query_length, PRV_QUERY_BUFFER_LENGTH - query_length, REG_ATTR_CONTENT_SENML_JSON);
#elif defined(LWM2M_SUPPORT_TLV)
res = utils_stringCopy(query + query_length, PRV_QUERY_BUFFER_LENGTH - query_length, REG_ATTR_CONTENT_TLV);
#else
/* This shouldn't be possible. */
#warning No supported content type for bootstrap. Bootstrap will always fail.
res = -1;
#endif
if (res < 0)
{
bootstrapServer->status = STATE_BS_FAILING;
return;
}
query_length += res;
#endif
#endif

if (bootstrapServer->sessionH == NULL)
{
bootstrapServer->sessionH = lwm2m_connect_server(bootstrapServer->secObjInstID, context->userData);
Expand Down Expand Up @@ -624,6 +653,7 @@ uint8_t bootstrap_handleRequest(lwm2m_context_t * contextP,
{
uint8_t result;
char * name;
lwm2m_media_type_t format = 0;

LOG_URI(uriP);
if (contextP->bootstrapCallback == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
Expand All @@ -637,15 +667,51 @@ uint8_t bootstrap_handleRequest(lwm2m_context_t * contextP,
}

if (message->uri_query->len == QUERY_NAME_LEN) return COAP_400_BAD_REQUEST;
#ifdef LWM2M_VERSION_1_0
if (message->uri_query->next != NULL) return COAP_400_BAD_REQUEST;
#else
if (message->uri_query->next != NULL)
{
char formatString[6];
char *endP;
multi_option_t *uri_query = message->uri_query->next;
if (lwm2m_strncmp((char *)uri_query->data, QUERY_PCT, QUERY_PCT_LEN) != 0)
{
return COAP_400_BAD_REQUEST;
}

if (uri_query->len == QUERY_PCT_LEN) return COAP_400_BAD_REQUEST;
if (uri_query->len > QUERY_PCT_LEN + 5) return COAP_400_BAD_REQUEST;
if (uri_query->next != NULL) return COAP_400_BAD_REQUEST;

memcpy(formatString, uri_query->data + QUERY_PCT_LEN, uri_query->len - QUERY_PCT_LEN);
formatString[uri_query->len - QUERY_PCT_LEN] = 0;

format = (lwm2m_media_type_t)strtol(formatString, &endP, 10);
if('\0' != *endP) return COAP_400_BAD_REQUEST;
switch (format)
{
#ifdef LWM2M_SUPPORT_SENML_JSON
case LWM2M_CONTENT_SENML_JSON:
break;
#endif
default:
#ifdef LWM2M_SUPPORT_TLV
format = LWM2M_CONTENT_TLV;
#else
return COAP_400_BAD_REQUEST;
#endif
}
}
#endif

name = (char *)lwm2m_malloc(message->uri_query->len - QUERY_NAME_LEN + 1);
if (name == NULL) return COAP_500_INTERNAL_SERVER_ERROR;

memcpy(name, message->uri_query->data + QUERY_NAME_LEN, message->uri_query->len - QUERY_NAME_LEN);
name[message->uri_query->len - QUERY_NAME_LEN] = 0;

result = contextP->bootstrapCallback(fromSessionH, COAP_NO_ERROR, NULL, name, contextP->bootstrapUserData);
result = contextP->bootstrapCallback(fromSessionH, COAP_NO_ERROR, NULL, name, format, contextP->bootstrapUserData);

lwm2m_free(name);

Expand Down Expand Up @@ -685,6 +751,7 @@ static void prv_resultCallback(lwm2m_context_t * contextP,
COAP_503_SERVICE_UNAVAILABLE,
uriP,
NULL,
0,
dataP->userData);
}
else
Expand All @@ -695,6 +762,7 @@ static void prv_resultCallback(lwm2m_context_t * contextP,
packet->code,
uriP,
NULL,
0,
dataP->userData);
}
transaction_free_userData(contextP, transacP);
Expand Down
4 changes: 4 additions & 0 deletions core/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
#define QUERY_STARTER "?"
#define QUERY_NAME "ep="
#define QUERY_NAME_LEN 3 // strlen("ep=")
#define QUERY_PCT "pct="
#define QUERY_PCT_LEN 4
#define QUERY_SMS "sms="
#define QUERY_SMS_LEN 4
#define QUERY_LIFETIME "lt="
Expand Down Expand Up @@ -195,6 +197,8 @@
#define REG_ATTR_TYPE_VALUE_LEN 11
#define REG_ATTR_CONTENT_KEY "ct"
#define REG_ATTR_CONTENT_KEY_LEN 2
#define REG_ATTR_CONTENT_TLV "11542"
#define REG_ATTR_CONTENT_TLV_LEN 5
#define REG_ATTR_CONTENT_JSON "11543"
#define REG_ATTR_CONTENT_JSON_LEN 5
#define REG_ATTR_CONTENT_JSON_OLD "1543"
Expand Down
Loading

0 comments on commit da23477

Please sign in to comment.