Skip to content

Commit

Permalink
Modify Static Client API to separate Object definition from setting a…
Browse files Browse the repository at this point in the history
…n Object Operation handler.

Add AwaStaticClient_SetObjectOperationHandler().
Deprecate AwaStaticClient_DefineObjectWithHandler().

Signed-off-by: David Antliff <[email protected]>
  • Loading branch information
David Antliff committed May 2, 2016
1 parent 2e28498 commit d1513c5
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 15 deletions.
17 changes: 16 additions & 1 deletion api/include/awa/static.h
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ void AwaStaticClient_Free(AwaStaticClient ** client);
************************************************************************************************************/

/**
* @deprecated Use ::AwaStaticClient_DefineObject followed by ::AwaStaticClient_SetObjectOperationHandler instead.
*
* @brief Define a new custom LWM2M object with a user-specified callback handler
* that will be called whenever a LWM2M operation on any instance of the
* defined object is performed.
Expand All @@ -404,7 +406,8 @@ AwaError AwaStaticClient_DefineObjectWithHandler(AwaStaticClient * client, const
uint16_t minimumInstances, uint16_t maximumInstances, AwaStaticClientHandler handler);

/**
* @brief Define a new custom LWM2M object, leaving handling of any instances of the object to the LWM2M Client.
* @brief Define a new custom LWM2M object. By default, the LWM2M Client will handle instance operations such as delete and create.
* Use ::AwaStaticClient_SetObjectOperationHandler to override this default behaviour with a specified handler.
*
* In order for an LWM2M server to perform operations on the defined object,
* a matching object must be defined on the LWM2M server.
Expand All @@ -422,6 +425,18 @@ AwaError AwaStaticClient_DefineObjectWithHandler(AwaStaticClient * client, const
AwaError AwaStaticClient_DefineObject(AwaStaticClient * client, const char * objectName, AwaObjectID objectID,
uint16_t minimumInstances, uint16_t maximumInstances);

/**
* @brief Set the Object Operation handler function, to be called by the LWM2M Client when object instances are created or deleted.
*
* @param[in] client A pointer to a valid Awa Static Client.
* @param[in] objectID An ID that uniquely identifies the object for which the handler will be associated.
* @param[in] handler A user-specified callback handler.
* @return AwaError_Success on success.
* @return AwaError_DefinitionInvalid if @e objectID is invalid.
* @return AwaError_StaticClientInvalid if @e client is NULL.
*/
AwaError AwaStaticClient_SetObjectOperationHandler(AwaStaticClient * client, AwaObjectID objectID, AwaStaticClientHandler handler);

/**
* @brief Define a new resource of an existing object with a user-specified callback handler
* that will be called whenever a LWM2M operation on the resource is performed.
Expand Down
90 changes: 86 additions & 4 deletions core/src/client/lwm2m_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,45 @@ static AwaResult DefaultHandler(AwaStaticClient * client, AwaOperation operation
AwaError AwaStaticClient_DefineObject(AwaStaticClient * client, const char * objectName, AwaObjectID objectID,
uint16_t minimumInstances, uint16_t maximumInstances)
{
return AwaStaticClient_DefineObjectWithHandler(client, objectName, objectID, minimumInstances, maximumInstances, DefaultHandler);
AwaError result = AwaError_Unspecified;

if (client != NULL)
{
if ((objectName != NULL) && (minimumInstances <= maximumInstances))
{
ObjectDefinition * definition = Definition_NewObjectTypeWithHandler(objectName, objectID, minimumInstances, maximumInstances, (LWM2MHandler)DefaultHandler);

if (definition != NULL)
{
if (Definition_AddObjectType(Lwm2mCore_GetDefinitions(client->Context), definition) == 0)
{
Lwm2mCore_ObjectCreated(client->Context, objectID);
result = AwaError_Success;
}
else
{
result = AwaError_Internal;
}
}
else
{
result = AwaError_OutOfMemory;
}
}
else
{
result = AwaError_DefinitionInvalid;
}
}
else
{
result = AwaError_StaticClientInvalid;
}

return result;
}

/** @deprecated */
AwaError AwaStaticClient_DefineObjectWithHandler(AwaStaticClient * client, const char * objectName, AwaObjectID objectID,
uint16_t minimumInstances, uint16_t maximumInstances,
AwaStaticClientHandler handler)
Expand All @@ -556,11 +592,11 @@ AwaError AwaStaticClient_DefineObjectWithHandler(AwaStaticClient * client, const
{
if ((objectName != NULL) && (handler != NULL) && (minimumInstances <= maximumInstances))
{
ObjectDefinition * defintion = Definition_NewObjectTypeWithHandler(objectName, objectID, minimumInstances, maximumInstances, (LWM2MHandler)handler);
ObjectDefinition * definition = Definition_NewObjectTypeWithHandler(objectName, objectID, minimumInstances, maximumInstances, (LWM2MHandler)handler);

if (defintion != NULL)
if (definition != NULL)
{
if (Definition_AddObjectType(Lwm2mCore_GetDefinitions(client->Context), defintion) == 0)
if (Definition_AddObjectType(Lwm2mCore_GetDefinitions(client->Context), definition) == 0)
{
Lwm2mCore_ObjectCreated(client->Context, objectID);
result = AwaError_Success;
Expand Down Expand Up @@ -588,6 +624,52 @@ AwaError AwaStaticClient_DefineObjectWithHandler(AwaStaticClient * client, const
return result;
}

AwaError AwaStaticClient_SetObjectOperationHandler(AwaStaticClient * client, AwaObjectID objectID, AwaStaticClientHandler handler)
{
AwaError result = AwaError_Unspecified;

if (client != NULL)
{
if (handler != NULL)
{
DefinitionRegistry * registry = Lwm2mCore_GetDefinitions(client->Context);
if (registry != NULL)
{
ObjectDefinition * definition = Definition_LookupObjectDefinition(registry, objectID);
if (definition != NULL)
{
if (Definition_SetObjectHandler(definition, (LWM2MHandler)handler) == 0)
{
result = AwaError_Success;
}
else
{
result = AwaError_Internal;
}
}
else
{
result = AwaError_NotDefined;
}
}
else
{
result = AwaError_Internal;
}
}
else
{
result = AwaError_DefinitionInvalid;
}
}
else
{
result = AwaError_StaticClientInvalid;
}

return result;
}

const void * AwaStaticClient_GetResourceInstancePointer(AwaStaticClient * client, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID, AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, size_t * resourceSize)
{
const void * result = NULL;
Expand Down
36 changes: 27 additions & 9 deletions core/src/common/lwm2m_definition.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ ResourceDefinition * Definition_LookupResourceDefinition(const DefinitionRegistr
return Definition_LookupResourceDefinitionFromObjectDefinition(objFormat, resourceID);
}

ObjectDefinition * NewObjectType(const char * objName, ObjectIDType objectID, uint16_t maximumInstances,
uint16_t minimumInstances, const ObjectOperationHandlers * handlers, LWM2MHandler handler)
static ObjectDefinition * NewObjectType(const char * objName, ObjectIDType objectID, uint16_t maximumInstances,
uint16_t minimumInstances, const ObjectOperationHandlers * handlers, LWM2MHandler handler)
{
ObjectDefinition * objFormat = NULL;
objFormat = (ObjectDefinition *)malloc(sizeof(ObjectDefinition));
Expand Down Expand Up @@ -118,28 +118,46 @@ ObjectDefinition * Definition_NewObjectType(const char * objName, ObjectIDType o
}

ObjectDefinition * Definition_NewObjectTypeWithHandler(const char * objName, ObjectIDType objectID, uint16_t minimumInstances,
uint16_t maximumInstances, LWM2MHandler handler)
uint16_t maximumInstances, LWM2MHandler handler)
{
return NewObjectType(objName, objectID, maximumInstances, minimumInstances, NULL, handler);
}

int Definition_SetObjectHandler(ObjectDefinition * objectDefinition, LWM2MHandler handler)
{
int result = -1;

if (objectDefinition != NULL)
{
objectDefinition->Handler = handler;
result = 0;
}
else
{
Lwm2m_Error("objectDefinition is NULL\n");
result = -1;
}

return result;
}

int Definition_AddObjectType(DefinitionRegistry * registry, ObjectDefinition * objFormat)
{
int result = -1;
ObjectDefinition * ExistingObjFormat = NULL;
ObjectDefinition * existingObjFormat = NULL;

if ((ExistingObjFormat = Definition_LookupObjectDefinition(registry, objFormat->ObjectID)))
if ((existingObjFormat = Definition_LookupObjectDefinition(registry, objFormat->ObjectID)))
{
if (objFormat->MaximumInstances != ExistingObjFormat->MaximumInstances)
if (objFormat->MaximumInstances != existingObjFormat->MaximumInstances)
{
AwaResult_SetResult(AwaResult_MismatchedDefinition);
}
else if (strlen(ExistingObjFormat->ObjectName) != strlen(objFormat->ObjectName) ||
memcmp(ExistingObjFormat->ObjectName, objFormat->ObjectName, strlen(ExistingObjFormat->ObjectName)))
else if (strlen(existingObjFormat->ObjectName) != strlen(objFormat->ObjectName) ||
memcmp(existingObjFormat->ObjectName, objFormat->ObjectName, strlen(existingObjFormat->ObjectName)))
{
AwaResult_SetResult(AwaResult_MismatchedDefinition);
}
else if (objFormat->MinimumInstances != ExistingObjFormat->MinimumInstances)
else if (objFormat->MinimumInstances != existingObjFormat->MinimumInstances)
{
AwaResult_SetResult(AwaResult_MismatchedDefinition);
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/common/lwm2m_definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ ResourceDefinition * Definition_LookupResourceDefinitionFromObjectDefinition(con
ObjectDefinition * Definition_NewObjectType(const char * objName, ObjectIDType objectID, uint16_t maximumInstances,
uint16_t minimumInstances, const ObjectOperationHandlers * handlers);
ObjectDefinition * Definition_NewObjectTypeWithHandler(const char * objName, ObjectIDType objectID, uint16_t minimumInstances,
uint16_t maximumInstances, LWM2MHandler handler);
uint16_t maximumInstances, LWM2MHandler handler);
int Definition_SetObjectHandler(ObjectDefinition * objFormat, LWM2MHandler handler);
void Definition_FreeObjectType(ObjectDefinition * definition);
int Definition_AddObjectType(DefinitionRegistry * registry, ObjectDefinition * objFormat);
ObjectDefinition * Definition_CopyObjectDefinition(const ObjectDefinition * definition);
Expand Down

0 comments on commit d1513c5

Please sign in to comment.