Skip to content

Commit

Permalink
Registration version differences for LWM2M 1.1.
Browse files Browse the repository at this point in the history
Client reports 1.1 or 1.0 based on the build.
Server recognizes and accepts only versions it is built for.
Example server prints out client version.

Signed-off-by: Scott Bertin <sbertin@telular.com>
  • Loading branch information
sbertin-telular committed Jan 30, 2019
1 parent 84194c2 commit 5f2e52b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 20 deletions.
8 changes: 7 additions & 1 deletion core/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Toby Jaffey - Please refer to git log
* Bosch Software Innovations GmbH - Please refer to git log
* Pascal Rieux - Please refer to git log
* Scott Bertin - Please refer to git log
* Scott Bertin, AMETEK, Inc. - Please refer to git log
*
*******************************************************************************/
/*
Expand Down Expand Up @@ -153,8 +153,13 @@
#define QUERY_BINDING_LEN 2
#define QUERY_DELIMITER "&"

#ifdef LWM2M_VERSION_1_0
#define LWM2M_VERSION "1.0"
#define LWM2M_VERSION_LEN 3
#else
#define LWM2M_VERSION "1.1"
#define LWM2M_VERSION_LEN 3
#endif

#define QUERY_VERSION_FULL QUERY_VERSION LWM2M_VERSION
#define QUERY_VERSION_FULL_LEN QUERY_VERSION_LEN+LWM2M_VERSION_LEN
Expand Down Expand Up @@ -321,6 +326,7 @@ void free_block1_buffer(lwm2m_block1_data_t * block1Data);

// defined in utils.c
lwm2m_data_type_t utils_depthToDatatype(uri_depth_t depth);
lwm2m_version_t utils_stringToVersion(uint8_t *buffer, size_t length);
lwm2m_binding_t utils_stringToBinding(uint8_t *buffer, size_t length);
lwm2m_media_type_t utils_convertMediaType(coap_content_type_t type);
int utils_isAltPathValid(const char * altPath);
Expand Down
10 changes: 10 additions & 0 deletions core/liblwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* Bosch Software Innovations GmbH - Please refer to git log
* Pascal Rieux - Please refer to git log
* Ville Skyttä - Please refer to git log
* Scott Bertin, AMETEK, Inc. - Please refer to git log
*
*******************************************************************************/

Expand Down Expand Up @@ -432,6 +433,14 @@ typedef enum
STATE_BS_FAILED, // bootstrap failed
} lwm2m_status_t;

typedef enum
{
VERSION_MISSING = 0, // Version number not in registration.
VERSION_UNRECOGNIZED, // Version number in registration not recognized.
VERSION_1_0, // LWM2M version 1.0
VERSION_1_1, // LWM2M version 1.1
} lwm2m_version_t;

typedef enum
{
BINDING_UNKNOWN = 0,
Expand Down Expand Up @@ -546,6 +555,7 @@ typedef struct _lwm2m_client_
struct _lwm2m_client_ * next; // matches lwm2m_list_t::next
uint16_t internalID; // matches lwm2m_list_t::id
char * name;
lwm2m_version_t version;
lwm2m_binding_t binding;
char * msisdn;
char * altPath;
Expand Down
36 changes: 17 additions & 19 deletions core/registration.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* Julien Vermillard - Please refer to git log
* Bosch Software Innovations GmbH - Please refer to git log
* Pascal Rieux - Please refer to git log
* Scott Bertin - Please refer to git log
* Scott Bertin, AMETEK, Inc. - Please refer to git log
*
*******************************************************************************/

Expand Down Expand Up @@ -615,13 +615,13 @@ static int prv_getParameters(multi_option_t * query,
uint32_t * lifetimeP,
char ** msisdnP,
lwm2m_binding_t * bindingP,
char ** versionP)
lwm2m_version_t * versionP)
{
*nameP = NULL;
*lifetimeP = 0;
*msisdnP = NULL;
*bindingP = BINDING_UNKNOWN;
*versionP = NULL;
*versionP = VERSION_MISSING;

while (query != NULL)
{
Expand Down Expand Up @@ -664,15 +664,10 @@ static int prv_getParameters(multi_option_t * query,
}
else if (lwm2m_strncmp((char *)query->data, QUERY_VERSION, QUERY_VERSION_LEN) == 0)
{
if (*versionP != NULL) goto error;
if (*versionP != VERSION_MISSING) goto error;
if (query->len == QUERY_VERSION_LEN) goto error;

*versionP = (char *)lwm2m_malloc(query->len - QUERY_VERSION_LEN + 1);
if (*versionP != NULL)
{
memcpy(*versionP, query->data + QUERY_VERSION_LEN, query->len - QUERY_VERSION_LEN);
(*versionP)[query->len - QUERY_VERSION_LEN] = 0;
}
*versionP = utils_stringToVersion(query->data + QUERY_VERSION_LEN, query->len - QUERY_VERSION_LEN);
}
else if (lwm2m_strncmp((char *)query->data, QUERY_BINDING, QUERY_BINDING_LEN) == 0)
{
Expand All @@ -689,7 +684,6 @@ static int prv_getParameters(multi_option_t * query,
error:
if (*nameP != NULL) lwm2m_free(*nameP);
if (*msisdnP != NULL) lwm2m_free(*msisdnP);
if (*versionP != NULL) lwm2m_free(*versionP);

return -1;
}
Expand Down Expand Up @@ -1051,7 +1045,7 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP,
uint32_t lifetime;
char * msisdn;
char * altPath;
char * version;
lwm2m_version_t version;
lwm2m_binding_t binding;
lwm2m_client_object_t * objects;
bool supportJSON;
Expand All @@ -1075,7 +1069,7 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP,
case 0:
// Register operation
// Version is mandatory
if (version == NULL)
if (version == VERSION_MISSING)
{
if (name != NULL) lwm2m_free(name);
if (msisdn != NULL) lwm2m_free(msisdn);
Expand All @@ -1084,23 +1078,26 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP,
// Endpoint client name is mandatory
if (name == NULL)
{
lwm2m_free(version);
if (msisdn != NULL) lwm2m_free(msisdn);
return COAP_400_BAD_REQUEST;
}
// Object list is mandatory
if (objects == NULL)
{
lwm2m_free(version);
lwm2m_free(name);
if (msisdn != NULL) lwm2m_free(msisdn);
return COAP_400_BAD_REQUEST;
}
// version must be 1.0
if (strlen(version) != LWM2M_VERSION_LEN
|| lwm2m_strncmp(version, LWM2M_VERSION, LWM2M_VERSION_LEN))
// Check for a supported version
switch (version)
{
lwm2m_free(version);
case VERSION_1_0:
#ifndef LWM2M_VERSION_1_0
case VERSION_1_1:
#endif
/* Supported version */
break;
default:
lwm2m_free(name);
if (msisdn != NULL) lwm2m_free(msisdn);
return COAP_412_PRECONDITION_FAILED;
Expand Down Expand Up @@ -1137,6 +1134,7 @@ uint8_t registration_handleRequest(lwm2m_context_t * contextP,
contextP->clientList = (lwm2m_client_t *)LWM2M_LIST_ADD(contextP->clientList, clientP);
}
clientP->name = name;
clientP->version = version;
clientP->binding = binding;
clientP->msisdn = msisdn;
clientP->altPath = altPath;
Expand Down
28 changes: 28 additions & 0 deletions core/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Contributors:
* David Navarro, Intel Corporation - initial API and implementation
* Toby Jaffey - Please refer to git log
* Scott Bertin, AMETEK, Inc. - Please refer to git log
*
*******************************************************************************/

Expand Down Expand Up @@ -274,6 +275,33 @@ size_t utils_floatToText(double data,
return intLength + decLength;
}

lwm2m_version_t utils_stringToVersion(uint8_t * buffer,
size_t length)
{
if (length == 0) return VERSION_MISSING;
if (length != 3) return VERSION_UNRECOGNIZED;
if (buffer[1] != '.') return VERSION_UNRECOGNIZED;

switch (buffer[0])
{
case '1':
switch (buffer[2])
{
case '0':
return VERSION_1_0;
case '1':
return VERSION_1_1;
default:
break;
}
break;
default:
break;
}

return VERSION_UNRECOGNIZED;
}

lwm2m_binding_t utils_stringToBinding(uint8_t * buffer,
size_t length)
{
Expand Down
19 changes: 19 additions & 0 deletions examples/server/lwm2mserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Julien Vermillard - Please refer to git log
* Bosch Software Innovations GmbH - Please refer to git log
* Christian Renz - Please refer to git log
* Scott Bertin, AMETEK, Inc. - Please refer to git log
*
*******************************************************************************/

Expand Down Expand Up @@ -84,6 +85,23 @@ static void prv_print_error(uint8_t status)
fprintf(stdout, "\r\n");
}

static const char * prv_dump_version(lwm2m_version_t version)
{
switch(version)
{
case VERSION_MISSING:
return "Missing";
case VERSION_UNRECOGNIZED:
return "Unrecognized";
case VERSION_1_0:
return "1.0";
case VERSION_1_1:
return "1.1";
default:
return "";
}
}

static char * prv_dump_binding(lwm2m_binding_t binding)
{
switch (binding)
Expand Down Expand Up @@ -113,6 +131,7 @@ static void prv_dump_client(lwm2m_client_t * targetP)

fprintf(stdout, "Client #%d:\r\n", targetP->internalID);
fprintf(stdout, "\tname: \"%s\"\r\n", targetP->name);
fprintf(stdout, "\tversion: \"%s\"\r\n", prv_dump_version(targetP->version));
fprintf(stdout, "\tbinding: \"%s\"\r\n", prv_dump_binding(targetP->binding));
if (targetP->msisdn) fprintf(stdout, "\tmsisdn: \"%s\"\r\n", targetP->msisdn);
if (targetP->altPath) fprintf(stdout, "\talternative path: \"%s\"\r\n", targetP->altPath);
Expand Down

0 comments on commit 5f2e52b

Please sign in to comment.