Skip to content

Commit

Permalink
Change object_checkReadable function.
Browse files Browse the repository at this point in the history
object_checkReadable and object_checkNumeric code are duplicated.
object_checkReadable could check resource is numeric or not.

Signed-off-by: Changkeun IM <asarabi8282@gmail.com>
  • Loading branch information
asarabi committed Dec 20, 2017
1 parent 7f475ca commit 309a1ec
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 49 deletions.
3 changes: 1 addition & 2 deletions core/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ uint8_t object_create(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_medi
uint8_t object_execute(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, uint8_t * buffer, size_t length);
uint8_t object_delete(lwm2m_context_t * contextP, lwm2m_uri_t * uriP);
uint8_t object_discover(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_server_t * serverP, uint8_t ** bufferP, size_t * lengthP);
uint8_t object_checkReadable(lwm2m_context_t * contextP, lwm2m_uri_t * uriP);
uint8_t object_checkNumeric(lwm2m_context_t * contextP, lwm2m_uri_t * uriP);
uint8_t object_checkReadable(lwm2m_context_t * contextP, lwm2m_uri_t * uriP, lwm2m_attributes_t * attrP);
bool object_isInstanceNew(lwm2m_context_t * contextP, uint16_t objectId, uint16_t instanceId);
int object_getRegisterPayloadBufferLength(lwm2m_context_t * contextP);
int object_getRegisterPayload(lwm2m_context_t * contextP, uint8_t * buffer, size_t length);
Expand Down
55 changes: 15 additions & 40 deletions core/objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* Bosch Software Innovations GmbH - Please refer to git log
* Pascal Rieux - Please refer to git log
* Scott Bertin - Please refer to git log
*
*
*******************************************************************************/

/*
Expand Down Expand Up @@ -61,7 +61,8 @@


uint8_t object_checkReadable(lwm2m_context_t * contextP,
lwm2m_uri_t * uriP)
lwm2m_uri_t * uriP,
lwm2m_attributes_t * attrP)
{
uint8_t result;
lwm2m_object_t * targetP;
Expand All @@ -75,7 +76,7 @@ uint8_t object_checkReadable(lwm2m_context_t * contextP,

if (!LWM2M_URI_IS_SET_INSTANCE(uriP)) return COAP_205_CONTENT;

if (NULL == lwm2m_list_find(targetP->instanceList, uriP->instanceId)) return COAP_404_NOT_FOUND;
if (NULL == LWM2M_LIST_FIND(targetP->instanceList, uriP->instanceId)) return COAP_404_NOT_FOUND;

if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) return COAP_205_CONTENT;

Expand All @@ -85,48 +86,22 @@ uint8_t object_checkReadable(lwm2m_context_t * contextP,

dataP->id = uriP->resourceId;

result = targetP->readFunc(uriP->instanceId, &size, &dataP, targetP);
lwm2m_data_free(1, dataP);

return result;
}

uint8_t object_checkNumeric(lwm2m_context_t * contextP,
lwm2m_uri_t * uriP)
{
uint8_t result;
lwm2m_object_t * targetP;
lwm2m_data_t * dataP = NULL;
int size;

LOG_URI(uriP);
if (!LWM2M_URI_IS_SET_RESOURCE(uriP)) return COAP_405_METHOD_NOT_ALLOWED;

targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
if (NULL == targetP) return COAP_404_NOT_FOUND;
if (NULL == targetP->readFunc) return COAP_405_METHOD_NOT_ALLOWED;

size = 1;
dataP = lwm2m_data_new(1);
if (dataP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;

dataP->id = uriP->resourceId;

result = targetP->readFunc(uriP->instanceId, &size, &dataP, targetP);
if (result == COAP_205_CONTENT)
{
switch (dataP->type)
if (attrP->toSet & ATTR_FLAG_NUMERIC)
{
case LWM2M_TYPE_INTEGER:
case LWM2M_TYPE_FLOAT:
break;
default:
result = COAP_405_METHOD_NOT_ALLOWED;
switch (dataP->type)
{
case LWM2M_TYPE_INTEGER:
case LWM2M_TYPE_FLOAT:
break;
default:
result = COAP_405_METHOD_NOT_ALLOWED;
}
}
}

lwm2m_data_free(1, dataP);

return result;
}

Expand Down Expand Up @@ -897,7 +872,7 @@ uint8_t object_createInstance(lwm2m_context_t * contextP,
targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
if (NULL == targetP) return COAP_404_NOT_FOUND;

if (NULL == targetP->createFunc)
if (NULL == targetP->createFunc)
{
return COAP_405_METHOD_NOT_ALLOWED;
}
Expand All @@ -915,7 +890,7 @@ uint8_t object_writeInstance(lwm2m_context_t * contextP,
targetP = (lwm2m_object_t *)LWM2M_LIST_FIND(contextP->objectList, uriP->objectId);
if (NULL == targetP) return COAP_404_NOT_FOUND;

if (NULL == targetP->writeFunc)
if (NULL == targetP->writeFunc)
{
return COAP_405_METHOD_NOT_ALLOWED;
}
Expand Down
8 changes: 1 addition & 7 deletions core/observe.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,9 @@ uint8_t observe_setParameters(lwm2m_context_t * contextP,

if (!LWM2M_URI_IS_SET_INSTANCE(uriP) && LWM2M_URI_IS_SET_RESOURCE(uriP)) return COAP_400_BAD_REQUEST;

result = object_checkReadable(contextP, uriP);
result = object_checkReadable(contextP, uriP, attrP);
if (COAP_205_CONTENT != result) return result;

if (0 != (attrP->toSet & ATTR_FLAG_NUMERIC))
{
result = object_checkNumeric(contextP, uriP);
if (COAP_205_CONTENT != result) return result;
}

watcherP = prv_getWatcher(contextP, uriP, serverP);
if (watcherP == NULL) return COAP_500_INTERNAL_SERVER_ERROR;

Expand Down

0 comments on commit 309a1ec

Please sign in to comment.