Skip to content

Commit

Permalink
Merge pull request #129 from seank-img/static-api-fixes
Browse files Browse the repository at this point in the history
Fix Static API define behaviour before init and add test case
  • Loading branch information
David Antliff committed May 3, 2016
2 parents ef65d88 + b5b353b commit 14a0e2f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 27 deletions.
1 change: 1 addition & 0 deletions api/include/awa/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ typedef enum
// 31
AwaError_StaticClientInvalid, /**< Indicates an operation was attempted in the context of an invalid Static Client */
AwaError_StaticClientNotConfigured, /**< Indicates a Static Client was initialised without being configured */
AwaError_StaticClientNotInitialized, /**< Indicates a Static Client was not initialized */
AwaError_LogLevelInvalid, /**< Indicates an attempt to set an unknown log level */


Expand Down
1 change: 1 addition & 0 deletions api/src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static const char * ErrorStrings[] =
// 31
"AwaError_StaticClientInvalid",
"AwaError_StaticClientNotConfigured",
"AwaError_StaticClientNotInitialized",
"AwaError_LogLevelInvalid",
};

Expand Down
16 changes: 14 additions & 2 deletions api/tests-static/test_static_api_handlers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,20 @@ TEST_F(TestStaticClientHandler, AwaStaticClient_SetResourceOperationHandler_Inva
{
auto client = AwaStaticClient_New();
ASSERT_TRUE(client != NULL);
EXPECT_EQ(AwaError_Success, AwaStaticClient_DefineObject(client, 9999, "TestObject", 0, 1));
EXPECT_EQ(AwaError_Success, AwaStaticClient_DefineResource(client, 9999, 1, "TestResource", AwaResourceType_Integer, 1, 1, AwaResourceOperations_ReadWrite));

EXPECT_EQ(AwaError_DefinitionInvalid, AwaStaticClient_SetResourceOperationHandler(client, 9999, 1, handler));
EXPECT_EQ(AwaError_StaticClientNotInitialized, AwaStaticClient_DefineObject(client, 9999, "TestObject", 0, 1));
EXPECT_EQ(AwaError_DefinitionInvalid, AwaStaticClient_SetResourceOperationHandler(client, 9999, 1, handler));
EXPECT_EQ(AwaError_StaticClientNotInitialized, AwaStaticClient_DefineResource(client, 9999, 1, "TestResource", AwaResourceType_Integer, 1, 1, AwaResourceOperations_ReadWrite));

EXPECT_EQ(AwaError_Success, AwaStaticClient_SetBootstrapServerURI(client, "coap://127.0.0.1:15683/"));
EXPECT_EQ(AwaError_Success, AwaStaticClient_SetEndPointName(client, "imagination1"));
EXPECT_EQ(AwaError_Success, AwaStaticClient_SetCoAPListenAddressPort(client, "0.0.0.0", 5683));

EXPECT_EQ(AwaError_Success, AwaStaticClient_Init(client));

EXPECT_EQ(AwaError_Success , AwaStaticClient_DefineObject(client, 9999, "TestObject", 0, 1));
EXPECT_EQ(AwaError_Success, AwaStaticClient_DefineResource(client, 9999, 1, "TestResource", AwaResourceType_Integer, 1, 1, AwaResourceOperations_ReadWrite));

EXPECT_EQ(AwaError_StaticClientInvalid, AwaStaticClient_SetResourceOperationHandler(NULL, 9999, 1, handler));
EXPECT_EQ(AwaError_DefinitionInvalid, AwaStaticClient_SetResourceOperationHandler(client, 9999, 1, NULL));
Expand Down
64 changes: 39 additions & 25 deletions core/src/client/lwm2m_static.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,30 +547,37 @@ AwaError AwaStaticClient_DefineObject(AwaStaticClient * client, AwaObjectID obje

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

if (definition != NULL)
if ((objectName != NULL) && (minimumInstances <= maximumInstances))
{
if (Definition_AddObjectType(Lwm2mCore_GetDefinitions(client->Context), definition) == 0)
ObjectDefinition * definition = Definition_NewObjectTypeWithHandler(objectName, objectID, minimumInstances, maximumInstances, (LWM2MHandler)DefaultHandler);

if (definition != NULL)
{
Lwm2mCore_ObjectCreated(client->Context, objectID);
result = AwaError_Success;
if (Definition_AddObjectType(Lwm2mCore_GetDefinitions(client->Context), definition) == 0)
{
Lwm2mCore_ObjectCreated(client->Context, objectID);
result = AwaError_Success;
}
else
{
result = AwaError_Internal;
}
}
else
{
result = AwaError_Internal;
result = AwaError_OutOfMemory;
}
}
else
{
result = AwaError_OutOfMemory;
result = AwaError_DefinitionInvalid;
}
}
else
{
result = AwaError_DefinitionInvalid;
result = AwaError_StaticClientNotInitialized;
}
}
else
Expand Down Expand Up @@ -749,37 +756,44 @@ static AwaError DefineResource(AwaStaticClient * client, const char * resourceNa

if (client != NULL)
{
if ((resourceName != NULL) && (minimumInstances <= maximumInstances))
if (client->Initialised)
{
ObjectDefinition * objFormat = Definition_LookupObjectDefinition(Lwm2mCore_GetDefinitions(client->Context), objectID);
if (objFormat != NULL)
if ((resourceName != NULL) && (minimumInstances <= maximumInstances))
{
ResourceDefinition * resourceDefinition = Definition_NewResourceTypeWithHandler(objFormat, resourceName, resourceID, resourceType, minimumInstances, maximumInstances, operations, (LWM2MHandler)handler);
if (resourceDefinition != NULL)
ObjectDefinition * objFormat = Definition_LookupObjectDefinition(Lwm2mCore_GetDefinitions(client->Context), objectID);
if (objFormat != NULL)
{
resourceDefinition->Handler = (LWM2MHandler)handler;
resourceDefinition->DataPointers = dataPointers;
resourceDefinition->IsPointerArray = isPointerArray;
resourceDefinition->DataElementSize = dataElementSize;
resourceDefinition->DataStepSize = dataStepSize;
result = AwaError_Success;
ResourceDefinition * resourceDefinition = Definition_NewResourceTypeWithHandler(objFormat, resourceName, resourceID, resourceType, minimumInstances, maximumInstances, operations, (LWM2MHandler)handler);
if (resourceDefinition != NULL)
{
resourceDefinition->Handler = (LWM2MHandler)handler;
resourceDefinition->DataPointers = dataPointers;
resourceDefinition->IsPointerArray = isPointerArray;
resourceDefinition->DataElementSize = dataElementSize;
resourceDefinition->DataStepSize = dataStepSize;
result = AwaError_Success;
}
else
{
Lwm2m_Warning("resourceDefinition is NULL\n");
result = AwaError_DefinitionInvalid;
}
}
else
{
Lwm2m_Warning("resourceDefinition is NULL\n");
Lwm2m_Warning("objFormat is NULL\n");
result = AwaError_DefinitionInvalid;
}
}
else
{
Lwm2m_Warning("objFormat is NULL\n");
Lwm2m_Warning("One or more Define parameters are invalid\n");
result = AwaError_DefinitionInvalid;
}
}
else
{
Lwm2m_Warning("One or more Define parameters are invalid\n");
result = AwaError_DefinitionInvalid;
result = AwaError_StaticClientNotInitialized;
}
}
else
Expand Down

0 comments on commit 14a0e2f

Please sign in to comment.