diff --git a/api/include/awa/error.h b/api/include/awa/error.h index dbd1fd9..492df70 100644 --- a/api/include/awa/error.h +++ b/api/include/awa/error.h @@ -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 */ diff --git a/api/src/error.c b/api/src/error.c index d89f229..31cdb00 100644 --- a/api/src/error.c +++ b/api/src/error.c @@ -77,6 +77,7 @@ static const char * ErrorStrings[] = // 31 "AwaError_StaticClientInvalid", "AwaError_StaticClientNotConfigured", + "AwaError_StaticClientNotInitialized", "AwaError_LogLevelInvalid", }; diff --git a/api/tests-static/test_static_api_handlers.cc b/api/tests-static/test_static_api_handlers.cc index e785d98..616d6f1 100644 --- a/api/tests-static/test_static_api_handlers.cc +++ b/api/tests-static/test_static_api_handlers.cc @@ -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)); diff --git a/core/src/client/lwm2m_static.c b/core/src/client/lwm2m_static.c index 827d7df..da79992 100644 --- a/core/src/client/lwm2m_static.c +++ b/core/src/client/lwm2m_static.c @@ -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 @@ -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