Skip to content

Commit

Permalink
Handle resource instances in writes.
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 Nov 16, 2020
1 parent 947288c commit c74f1f0
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 48 deletions.
16 changes: 8 additions & 8 deletions core/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,13 +656,13 @@ int lwm2m_data_parse(lwm2m_uri_t * uriP,
{
case LWM2M_CONTENT_TEXT:
if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) return 0;
#ifndef LWM2M_VERSION_1_0
// TODO: Support resource instance
if (LWM2M_URI_IS_SET_RESOURCE_INSTANCE(uriP)) return 0;
#endif
*dataP = lwm2m_data_new(1);
if (*dataP == NULL) return 0;
(*dataP)->id = uriP->resourceId;
#ifndef LWM2M_VERSION_1_0
if (LWM2M_URI_IS_SET_RESOURCE_INSTANCE(uriP))
(*dataP)->id = uriP->resourceInstanceId;
#endif
(*dataP)->type = LWM2M_TYPE_STRING;
res = prv_setBuffer(*dataP, buffer, bufferLen);
if (res == 0)
Expand All @@ -674,13 +674,13 @@ int lwm2m_data_parse(lwm2m_uri_t * uriP,

case LWM2M_CONTENT_OPAQUE:
if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) return 0;
#ifndef LWM2M_VERSION_1_0
// TODO: Support resource instance
if (LWM2M_URI_IS_SET_RESOURCE_INSTANCE(uriP)) return 0;
#endif
*dataP = lwm2m_data_new(1);
if (*dataP == NULL) return 0;
(*dataP)->id = uriP->resourceId;
#ifndef LWM2M_VERSION_1_0
if (LWM2M_URI_IS_SET_RESOURCE_INSTANCE(uriP))
(*dataP)->id = uriP->resourceInstanceId;
#endif
(*dataP)->type = LWM2M_TYPE_OPAQUE;
res = prv_setBuffer(*dataP, buffer, bufferLen);
if (res == 0)
Expand Down
2 changes: 1 addition & 1 deletion core/liblwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ void lwm2m_set_monitoring_callback(lwm2m_context_t * contextP, lwm2m_result_call
// Device Management APIs
int lwm2m_dm_read(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
int lwm2m_dm_discover(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_result_callback_t callback, void * userData);
int lwm2m_dm_write(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_media_type_t format, uint8_t * buffer, int length, lwm2m_result_callback_t callback, void * userData);
int lwm2m_dm_write(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_media_type_t format, uint8_t * buffer, int length, bool partialUpdate, lwm2m_result_callback_t callback, void * userData);
int lwm2m_dm_write_attributes(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_attributes_t * attrP, lwm2m_result_callback_t callback, void * userData);
int lwm2m_dm_execute(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, lwm2m_media_type_t format, uint8_t * buffer, int length, lwm2m_result_callback_t callback, void * userData);
int lwm2m_dm_create(lwm2m_context_t * contextP, uint16_t clientID, lwm2m_uri_t * uriP, int numData, lwm2m_data_t * dataP, lwm2m_result_callback_t callback, void * userData);
Expand Down
39 changes: 22 additions & 17 deletions core/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,25 @@ uint8_t dm_handleRequest(lwm2m_context_t * contextP,
lwm2m_update_registration(contextP, 0, true);
}
}
else if (!LWM2M_URI_IS_SET_RESOURCE(uriP))
else if (!IS_OPTION(message, COAP_OPTION_CONTENT_TYPE)
|| format == LWM2M_CONTENT_TEXT)
{
result = object_write(contextP, uriP, format, message->payload, message->payload_len, true);
if (!LWM2M_URI_IS_SET_RESOURCE(uriP)
#ifndef LWM2M_VERSION_1_0
|| LWM2M_URI_IS_SET_RESOURCE_INSTANCE(uriP)
#endif
)
{
result = COAP_400_BAD_REQUEST;
}
else
{
result = object_execute(contextP, uriP, message->payload, message->payload_len);
}
}
else
{
result = object_execute(contextP, uriP, message->payload, message->payload_len);
result = object_write(contextP, uriP, format, message->payload, message->payload_len, true);
}
}
break;
Expand Down Expand Up @@ -516,9 +528,12 @@ int lwm2m_dm_write(lwm2m_context_t * contextP,
lwm2m_media_type_t format,
uint8_t * buffer,
int length,
bool partialUpdate,
lwm2m_result_callback_t callback,
void * userData)
{
coap_method_t method = partialUpdate ? COAP_POST : COAP_PUT;

LOG_ARG("clientID: %d, format: %s, length: %d", clientID, STR_MEDIA_TYPE(format), length);
LOG_URI(uriP);
if (!LWM2M_URI_IS_SET_INSTANCE(uriP)
Expand All @@ -527,20 +542,10 @@ int lwm2m_dm_write(lwm2m_context_t * contextP,
return COAP_400_BAD_REQUEST;
}

if (LWM2M_URI_IS_SET_RESOURCE(uriP))
{
return prv_makeOperation(contextP, clientID, uriP,
COAP_PUT,
format, buffer, length,
callback, userData);
}
else
{
return prv_makeOperation(contextP, clientID, uriP,
COAP_POST,
format, buffer, length,
callback, userData);
}
return prv_makeOperation(contextP, clientID, uriP,
method,
format, buffer, length,
callback, userData);
}

int lwm2m_dm_execute(lwm2m_context_t * contextP,
Expand Down
15 changes: 12 additions & 3 deletions core/objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,23 @@ uint8_t object_write(lwm2m_context_t * contextP,
else
{
size = lwm2m_data_parse(uriP, buffer, length, format, &dataP);
if (size == 0)
if (size <= 0)
{
result = COAP_406_NOT_ACCEPTABLE;
}
}
#ifndef LWM2M_VERSION_1_0
// TODO: support resource instance
if (LWM2M_URI_IS_SET_RESOURCE_INSTANCE(uriP)) result = COAP_400_BAD_REQUEST;
if (result == NO_ERROR
&& LWM2M_URI_IS_SET_RESOURCE_INSTANCE(uriP))
{
lwm2m_data_t *subDataP = dataP;
dataP = lwm2m_data_new(1);
if (dataP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;
dataP->id = uriP->resourceId;
lwm2m_data_encode_instances(subDataP, size, dataP);
size = 1;
partial = true;
}
#endif
if (result == NO_ERROR)
{
Expand Down
21 changes: 20 additions & 1 deletion examples/client/lwm2mclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,26 @@ void handle_value_changed(lwm2m_context_t * lwm2mH,
return;
}
dataP->id = uri->resourceId;
lwm2m_data_encode_nstring(value, valueLength, dataP);

#ifndef LWM2M_VERSION_1_0
if (LWM2M_URI_IS_SET_RESOURCE_INSTANCE(uri))
{
lwm2m_data_t *subDataP = lwm2m_data_new(1);
if (subDataP == NULL)
{
fprintf(stderr, "Internal allocation failure !\n");
lwm2m_data_free(1, dataP);
return;
}
subDataP->id = uri->resourceInstanceId;
lwm2m_data_encode_nstring(value, valueLength, subDataP);
lwm2m_data_encode_instances(subDataP, 1, dataP);
}
else
#endif
{
lwm2m_data_encode_nstring(value, valueLength, dataP);
}

result = object->writeFunc(uri->instanceId, 1, dataP, object, LWM2M_WRITE_PARTIAL_UPDATE);
if (COAP_405_METHOD_NOT_ALLOWED == result)
Expand Down
33 changes: 24 additions & 9 deletions examples/client/object_access_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ static uint8_t prv_write_resources(uint16_t instanceId, int numData,
{
result = COAP_405_METHOD_NOT_ALLOWED;
}
else if (tlvArray[i].type == LWM2M_TYPE_MULTIPLE_RESOURCE)
{
result = COAP_404_NOT_FOUND;
}
else
{
if (1 != lwm2m_data_decode_int(&tlvArray[i], &value))
Expand All @@ -275,6 +279,10 @@ static uint8_t prv_write_resources(uint16_t instanceId, int numData,
{
result = COAP_405_METHOD_NOT_ALLOWED;
}
else if (tlvArray[i].type == LWM2M_TYPE_MULTIPLE_RESOURCE)
{
result = COAP_404_NOT_FOUND;
}
else
{
if (1 != lwm2m_data_decode_int(&tlvArray[i], &value))
Expand Down Expand Up @@ -431,22 +439,29 @@ static uint8_t prv_write_resources(uint16_t instanceId, int numData,
}
} break;
case RES_M_ACCESS_CONTROL_OWNER: {
if (1 == lwm2m_data_decode_int(tlvArray + i, &value))
if (tlvArray[i].type == LWM2M_TYPE_MULTIPLE_RESOURCE)
{
result = COAP_404_NOT_FOUND;
}
else
{
if (value >= 0 && value <= 0xFFFF)
if (1 == lwm2m_data_decode_int(tlvArray + i, &value))
{
accCtrlOiP->accCtrlOwner = value;
result = COAP_204_CHANGED;
if (value >= 0 && value <= 0xFFFF)
{
accCtrlOiP->accCtrlOwner = value;
result = COAP_204_CHANGED;
}
else
{
result = COAP_406_NOT_ACCEPTABLE;
}
}
else
{
result = COAP_406_NOT_ACCEPTABLE;
result = COAP_400_BAD_REQUEST;
}
}
else
{
result = COAP_400_BAD_REQUEST;
}
}
break;
default:
Expand Down
7 changes: 7 additions & 0 deletions examples/client/object_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,13 @@ static uint8_t prv_device_write(uint16_t instanceId,

do
{
/* No multiple instance resources */
if (dataArray[i].type == LWM2M_TYPE_MULTIPLE_RESOURCE)
{
result = COAP_404_NOT_FOUND;
continue;
}

switch (dataArray[i].id)
{
case RES_O_CURRENT_TIME:
Expand Down
7 changes: 7 additions & 0 deletions examples/client/object_firmware.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,13 @@ static uint8_t prv_firmware_write(uint16_t instanceId,

do
{
/* No multiple instance resources */
if (dataArray[i].type == LWM2M_TYPE_MULTIPLE_RESOURCE)
{
result = COAP_404_NOT_FOUND;
continue;
}

switch (dataArray[i].id)
{
case RES_M_PACKAGE:
Expand Down
7 changes: 7 additions & 0 deletions examples/client/object_security.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ static uint8_t prv_security_write(uint16_t instanceId,

i = 0;
do {
/* No multiple instance resources */
if (dataArray[i].type == LWM2M_TYPE_MULTIPLE_RESOURCE)
{
result = COAP_404_NOT_FOUND;
continue;
}

switch (dataArray[i].id)
{
case LWM2M_SECURITY_URI_ID:
Expand Down
12 changes: 11 additions & 1 deletion examples/client/object_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,13 @@ static uint8_t prv_server_write(uint16_t instanceId,
i = 0;
do
{
/* No multiple instance resources */
if (dataArray[i].type == LWM2M_TYPE_MULTIPLE_RESOURCE)
{
result = COAP_404_NOT_FOUND;
continue;
}

switch (dataArray[i].id)
{
case LWM2M_SERVER_SHORT_ID_ID:
Expand Down Expand Up @@ -726,12 +733,15 @@ static uint8_t prv_server_write(uint16_t instanceId,
case LWM2M_SERVER_BINDING_ID:
if ((dataArray[i].type == LWM2M_TYPE_STRING || dataArray[i].type == LWM2M_TYPE_OPAQUE)
&& dataArray[i].value.asBuffer.length > 0 && dataArray[i].value.asBuffer.length <= 3
#ifdef LWM2M_VERSION_1_0
&& (strncmp((char*)dataArray[i].value.asBuffer.buffer, "U", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "UQ", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "S", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "SQ", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "US", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "UQS", dataArray[i].value.asBuffer.length) == 0))
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "UQS", dataArray[i].value.asBuffer.length) == 0)
#endif
)
{
strncpy(targetP->binding, (char*)dataArray[i].value.asBuffer.buffer, dataArray[i].value.asBuffer.length);
result = COAP_204_CHANGED;
Expand Down
3 changes: 3 additions & 0 deletions examples/client/test_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ static uint8_t prv_write(uint16_t instanceId,

for (i = 0 ; i < numData ; i++)
{
/* No multiple instance resources */
if (dataArray[i].type == LWM2M_TYPE_MULTIPLE_RESOURCE) return COAP_404_NOT_FOUND;

switch (dataArray[i].id)
{
case 1:
Expand Down
12 changes: 11 additions & 1 deletion examples/lightclient/object_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ static uint8_t prv_server_write(uint16_t instanceId,
i = 0;
do
{
/* No multiple instance resources */
if (dataArray[i].type == LWM2M_TYPE_MULTIPLE_RESOURCE)
{
result = COAP_404_NOT_FOUND;
continue;
}

switch (dataArray[i].id)
{
case LWM2M_SERVER_SHORT_ID_ID:
Expand Down Expand Up @@ -304,12 +311,15 @@ static uint8_t prv_server_write(uint16_t instanceId,
case LWM2M_SERVER_BINDING_ID:
if ((dataArray[i].type == LWM2M_TYPE_STRING || dataArray[i].type == LWM2M_TYPE_OPAQUE)
&& dataArray[i].value.asBuffer.length > 0 && dataArray[i].value.asBuffer.length <= 3
#ifdef LWM2M_VERSION_1_0
&& (strncmp((char*)dataArray[i].value.asBuffer.buffer, "U", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "UQ", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "S", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "SQ", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "US", dataArray[i].value.asBuffer.length) == 0
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "UQS", dataArray[i].value.asBuffer.length) == 0))
|| strncmp((char*)dataArray[i].value.asBuffer.buffer, "UQS", dataArray[i].value.asBuffer.length) == 0)
#endif
)
{
strncpy(targetP->binding, (char*)dataArray[i].value.asBuffer.buffer, dataArray[i].value.asBuffer.length);
result = COAP_204_CHANGED;
Expand Down
3 changes: 3 additions & 0 deletions examples/lightclient/test_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ static uint8_t prv_write(uint16_t instanceId,

for (i = 0 ; i < numData ; i++)
{
/* No multiple instance resources */
if (dataArray[i].type == LWM2M_TYPE_MULTIPLE_RESOURCE) return COAP_404_NOT_FOUND;

switch (dataArray[i].id)
{
case 1:
Expand Down
Loading

0 comments on commit c74f1f0

Please sign in to comment.