From 4622d995c1a565c360f9cd47373965eba2c4394a Mon Sep 17 00:00:00 2001 From: Rahul Daga Date: Mon, 14 Mar 2016 14:43:04 +0530 Subject: [PATCH 01/14] add library for managing flow and custom ipso objects Signed-off-by: Rahul Daga --- Makefile.libobjects | 2 + common.h | 83 +++++ lwm2m-client-flow-access-object.c | 336 +++++++++++++++++ lwm2m-client-flow-access-object.h | 41 +++ lwm2m-client-flow-object.c | 578 ++++++++++++++++++++++++++++++ lwm2m-client-flow-object.h | 43 +++ lwm2m-client-ipso-digital-input.c | 445 +++++++++++++++++++++++ lwm2m-client-ipso-digital-input.h | 45 +++ lwm2m-client-ipso-light-control.c | 451 +++++++++++++++++++++++ lwm2m-client-ipso-light-control.h | 50 +++ 10 files changed, 2074 insertions(+) create mode 100644 Makefile.libobjects create mode 100644 common.h create mode 100644 lwm2m-client-flow-access-object.c create mode 100644 lwm2m-client-flow-access-object.h create mode 100644 lwm2m-client-flow-object.c create mode 100644 lwm2m-client-flow-object.h create mode 100644 lwm2m-client-ipso-digital-input.c create mode 100644 lwm2m-client-ipso-digital-input.h create mode 100644 lwm2m-client-ipso-light-control.c create mode 100644 lwm2m-client-ipso-light-control.h diff --git a/Makefile.libobjects b/Makefile.libobjects new file mode 100644 index 0000000..d3a130c --- /dev/null +++ b/Makefile.libobjects @@ -0,0 +1,2 @@ +libobjects_src = lwm2m-client-flow-object.c lwm2m-client-flow-access-object.c \ + lwm2m-client-ipso-digital-input.c lwm2m-client-ipso-light-control.c diff --git a/common.h b/common.h new file mode 100644 index 0000000..4177cc6 --- /dev/null +++ b/common.h @@ -0,0 +1,83 @@ +/** + * @file + * LightWeightM2M Common definitions for libobjects. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef COMMON_H_ +#define COMMON_H_ + +#define REGISTER_OBJECT(context, name, id, maxInstances, minInstances, handlers) \ + do \ + { \ + if (Lwm2mCore_RegisterObjectType(context, name, id, maxInstances, minInstances, handlers) \ + == -1) \ + { \ + Lwm2m_Error("Failed to register object type for "name" object with Lwm2m core\n"); \ + return -1; \ + } \ + }while(0) + +#define REGISTER_RESOURCE(context, name, objId, id, type, maxInstances, minInstances, operations, \ + handlers) \ + do \ + { \ + if (Lwm2mCore_RegisterResourceType(context, name, objId, id, type, maxInstances, \ + minInstances, operations, handlers) == -1) \ + { \ + Lwm2m_Error("Failed to register "name" resource with Lwm2m core\n"); \ + return -1; \ + } \ + }while(0) + +#define CREATE_OBJECT_INSTANCE(context, objectId, objectInstanceId) \ + do \ + { \ + if (Lwm2mCore_CreateObjectInstance(context, objectId, objectInstanceId) == -1) \ + { \ + Lwm2m_Error("Failed to create instance %d of "#objectId"\n", objectInstanceId); \ + return -1; \ + } \ + }while(0) + +#define CREATE_OPTIONAL_RESOURCE(context, objectId, objectInstanceId, id) \ + do \ + { \ + if (Lwm2mCore_CreateOptionalResource(context, objectId, objectInstanceId, id) == -1) \ + { \ + Lwm2m_Error("Failed to create optional "#id" for "#objectId" instance %d\n", \ + objectInstanceID); \ + return -1; \ + } \ + }while(0) + +#endif diff --git a/lwm2m-client-flow-access-object.c b/lwm2m-client-flow-access-object.c new file mode 100644 index 0000000..c567772 --- /dev/null +++ b/lwm2m-client-flow-access-object.c @@ -0,0 +1,336 @@ +/** + * @file + * LightWeightM2M LWM2M Flow Access object. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/*************************************************************************************************** + * Includes + **************************************************************************************************/ + +#include +#include +#include +#include +#include "lwm2m_core.h" +#include "coap_abstraction.h" +#include "common.h" + +/*************************************************************************************************** + * Definitions + **************************************************************************************************/ + +#define FLOWM2M_FLOW_ACCESS_OBJECT 20001 +#define FLOWM2M_FLOW_ACCESS_OBJECT_URL 0 +#define FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY 1 +#define FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET 2 +#define FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN 3 +#define FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY 4 + +#define REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, name, id, type) \ + REGISTER_RESOURCE(context, name, FLOWM2M_FLOW_ACCESS_OBJECT, id, type, \ + MultipleInstancesEnum_Single, MandatoryEnum_Mandatory, Operations_RW, \ + &flowAccessObjectResourceOperationHandlers) + +#define CREATE_FLOW_ACCESS_OBJECT_OPTIONAL_RESOURCE(context, objectInstanceId, resourcId) \ + CREATE_OPTIONAL_RESOURCE(context, FLOWM2M_FLOW_OBJECT, objectInstanceId, resourcId) + +/*************************************************************************************************** + * Typedefs + **************************************************************************************************/ + +typedef struct +{ + char * URL; + char * CustomerKey; + char * CustomerSecret; + char * RememberMeToken; + int64_t RememberMeTokenExpiry; +} FlowAccessObject; + +/*************************************************************************************************** + * Prototypes + **************************************************************************************************/ + +static int FlowAccessObject_ResourceReadHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen); + +static int FlowAccessObject_ResourceGetLengthHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID); + +static int FlowAccessObject_ResourceWriteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, + bool * changed); + +static int FlowAccessObject_ResourceCreateHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); + +static int FlowAccessObject_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID); + +static int FlowAccessObject_ObjectDeleteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); + +/*************************************************************************************************** + * Globals + **************************************************************************************************/ + +FlowAccessObject flowAccessObject; + +static ObjectOperationHandlers flowAccessObjectOperationHandlers = +{ + .CreateInstance = FlowAccessObject_ObjectCreateInstanceHandler, + .Delete = FlowAccessObject_ObjectDeleteHandler, +}; + +static ResourceOperationHandlers flowAccessObjectResourceOperationHandlers = +{ + .Read = FlowAccessObject_ResourceReadHandler, + .GetLength = FlowAccessObject_ResourceGetLengthHandler, + .Write = FlowAccessObject_ResourceWriteHandler, + .CreateOptionalResource = FlowAccessObject_ResourceCreateHandler, + .Execute = NULL, +}; + +/*************************************************************************************************** + * Implementation - Private + **************************************************************************************************/ + +static int FlowAccessObject_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID) +{ + return 0; +} + +static int FlowAccessObject_ResourceCreateHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) +{ + return 0; +} + +static int FlowAccessObject_ObjectDeleteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) +{ + + if (objectID != FLOWM2M_FLOW_ACCESS_OBJECT) + { + Lwm2m_Error("flowAccessObject_ObjectDeleteHandler Invalid OIR: %d/%d/%d\n", objectID, + objectInstanceID, resourceID); + return -1; + } + + if (objectInstanceID == 0) + { + memset(&flowAccessObject, 0, sizeof(FlowAccessObject)); + + if (flowAccessObject.URL) + free(flowAccessObject.URL); + if (flowAccessObject.CustomerKey) + free(flowAccessObject.CustomerKey); + if (flowAccessObject.CustomerSecret) + free(flowAccessObject.CustomerSecret); + if (flowAccessObject.RememberMeToken) + free(flowAccessObject.RememberMeToken); + } + else + { + Lwm2m_Error("flowAccessObject_ObjectDeleteHandler Invalid instance (not 0): %d", + objectInstanceID); + return -1; + } + + return 0; +} + +static int FlowAccessObject_ResourceReadHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen) +{ + int result = 0; + + switch (resourceID) + { + case FLOWM2M_FLOW_ACCESS_OBJECT_URL: + memcpy(destBuffer, flowAccessObject.URL, strlen(flowAccessObject.URL) + 1); + result = strlen(flowAccessObject.URL) + 1; + break; + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: + memcpy(destBuffer, flowAccessObject.CustomerKey, + strlen(flowAccessObject.CustomerKey) + 1); + result = strlen(flowAccessObject.CustomerKey) + 1; + break; + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: + memcpy(destBuffer, flowAccessObject.CustomerSecret, + strlen(flowAccessObject.CustomerSecret) + 1); + result = strlen(flowAccessObject.CustomerSecret) + 1; + break; + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: + memcpy(destBuffer, flowAccessObject.RememberMeToken, + strlen(flowAccessObject.RememberMeToken) + 1); + result = strlen(flowAccessObject.RememberMeToken) + 1; + break; + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: + memcpy(destBuffer, &flowAccessObject.RememberMeTokenExpiry, + sizeof(flowAccessObject.RememberMeTokenExpiry)); + result = sizeof(flowAccessObject.RememberMeTokenExpiry); + break; + default: + result = -1; + break; + } + + return result; +} + +static int FlowAccessObject_ResourceGetLengthHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID) +{ + int result = 0; + + switch (resourceID) + { + case FLOWM2M_FLOW_ACCESS_OBJECT_URL: + if(flowAccessObject.URL != NULL) + result = strlen(flowAccessObject.URL) + 1; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: + if(flowAccessObject.CustomerKey != NULL) + result = strlen(flowAccessObject.CustomerKey) + 1; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: + if(flowAccessObject.CustomerSecret != NULL) + result = strlen(flowAccessObject.CustomerSecret) + 1; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: + if(flowAccessObject.RememberMeToken != NULL) + result = strlen(flowAccessObject.RememberMeToken) + 1; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: + result = sizeof(flowAccessObject.RememberMeTokenExpiry); + break; + + default: + result = -1; + break; + } + + return result; +} + +static int FlowAccessObject_ResourceWriteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, + bool * changed) +{ + int result = 0; + + switch(resourceID) + { + case FLOWM2M_FLOW_ACCESS_OBJECT_URL: + if (flowAccessObject.URL) + free(flowAccessObject.URL); + flowAccessObject.URL = (char *)malloc(srcBufferLen + 1); + memset(flowAccessObject.URL, 0, srcBufferLen + 1); + memcpy(flowAccessObject.URL, srcBuffer, srcBufferLen); + result = srcBufferLen; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: + if (flowAccessObject.CustomerKey) + free(flowAccessObject.CustomerKey); + flowAccessObject.CustomerKey = (char *)malloc(srcBufferLen + 1); + memset(flowAccessObject.CustomerKey, 0, srcBufferLen + 1); + memcpy(flowAccessObject.CustomerKey, srcBuffer, srcBufferLen); + result = srcBufferLen; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: + if (flowAccessObject.CustomerSecret) + free(flowAccessObject.CustomerSecret); + flowAccessObject.CustomerSecret = (char *)malloc(srcBufferLen + 1); + memset(flowAccessObject.CustomerSecret, 0, srcBufferLen + 1); + memcpy(flowAccessObject.CustomerSecret, srcBuffer, srcBufferLen); + result = srcBufferLen; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: + if (flowAccessObject.RememberMeToken) + free(flowAccessObject.RememberMeToken); + flowAccessObject.RememberMeToken = (char *)malloc(srcBufferLen + 1); + memset(flowAccessObject.RememberMeToken, 0, srcBufferLen + 1); + memcpy(flowAccessObject.RememberMeToken, srcBuffer, srcBufferLen); + result = srcBufferLen; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: + memcpy(&flowAccessObject.RememberMeTokenExpiry, srcBuffer, srcBufferLen); + result = srcBufferLen; + break; + + default: + result = -1; + break; + } + + return result; +} + +/*************************************************************************************************** + * Implementation - Public + **************************************************************************************************/ + +int Lwm2m_RegisterFlowAccessObject(Lwm2mContextType * context) +{ + Lwm2mCore_RegisterObjectType(context, "FlowAccess" , FLOWM2M_FLOW_ACCESS_OBJECT, + MultipleInstancesEnum_Single, MandatoryEnum_Optional, &flowAccessObjectOperationHandlers); + + REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, "URL", FLOWM2M_FLOW_ACCESS_OBJECT_URL, \ + ResourceTypeEnum_TypeString); + REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, "CustomerKey", \ + FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY, ResourceTypeEnum_TypeString); + REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, "CustomerSecret", \ + FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET, ResourceTypeEnum_TypeString); + REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, "RememberMeToken", \ + FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN, ResourceTypeEnum_TypeString); + REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, "RememberMeTokenExpiry", \ + FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY, ResourceTypeEnum_TypeOpaque); + + return 0; +} diff --git a/lwm2m-client-flow-access-object.h b/lwm2m-client-flow-access-object.h new file mode 100644 index 0000000..cf67383 --- /dev/null +++ b/lwm2m-client-flow-access-object.h @@ -0,0 +1,41 @@ +/** + * @file + * LightWeightM2M LWM2M Flow Access object. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef LWM2M_CLIENT_FLOW_ACCESS_OBJECT_H_ +#define LWM2M_CLIENT_FLOW_ACCESS_OBJECT_H_ + +int Lwm2m_RegisterFlowAccessObject(Lwm2mContextType * context); + +#endif /* LWM2M_CLIENT_FLOW_ACCESS_OBJECT_H_ */ diff --git a/lwm2m-client-flow-object.c b/lwm2m-client-flow-object.c new file mode 100644 index 0000000..44c97c8 --- /dev/null +++ b/lwm2m-client-flow-object.c @@ -0,0 +1,578 @@ +/** + * @file + * LightWeightM2M LWM2M Flow object. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/*************************************************************************************************** + * Includes + **************************************************************************************************/ + +#include +#include +#include +#include +#include +#include "lwm2m_core.h" +#include "coap_abstraction.h" +#include "hmac.h" +#include "b64.h" +#include "common.h" + +/*************************************************************************************************** + * Definitions + **************************************************************************************************/ + +#define FLOWM2M_FLOW_OBJECT 20000 +#define FLOWM2M_FLOW_OBJECT_DEVICEID 0 +#define FLOWM2M_FLOW_OBJECT_PARENTID 1 +#define FLOWM2M_FLOW_OBJECT_DEVICETYPE 2 +#define FLOWM2M_FLOW_OBJECT_NAME 3 +#define FLOWM2M_FLOW_OBJECT_DESCRIPTION 4 +#define FLOWM2M_FLOW_OBJECT_FCAP 5 +#define FLOWM2M_FLOW_OBJECT_LICENSEEID 6 +#define FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE 7 +#define FLOWM2M_FLOW_OBJECT_HASHITERATIONS 8 +#define FLOWM2M_FLOW_OBJECT_LICENSEEHASH 9 +#define FLOWM2M_FLOW_OBJECT_STATUS 10 + +#define MAX_STRING_SIZE 64 +#define MAX_KEY_SIZE 64 + +#define REGISTER_FLOW_OBJECT_RESOURCE(context, name, id, type, minInstances) \ + REGISTER_RESOURCE(context, name, FLOWM2M_FLOW_OBJECT, id, type, \ + MultipleInstancesEnum_Single, minInstances, Operations_RW, \ + &flowObjectResourceOperationHandlers) + +#define CREATE_FLOW_OBJECT_OPTIONAL_RESOURCE(context, objectInstanceId, resourcId) \ + CREATE_OPTIONAL_RESOURCE(context, FLOWM2M_FLOW_OBJECT, objectInstanceId, resourcId) + +/*************************************************************************************************** + * Typedefs + **************************************************************************************************/ + +typedef struct +{ + void * DeviceID; + int DeviceIDSize; + void * ParentID; + int64_t ParentIDSize; + char * DeviceType; + char * Name; + char * Description; + char * FCAP; + int64_t LicenseeID; + void * LicenseeChallenge; + int64_t LicenseeChallengeSize; + int64_t HashIterations; + void * LicenseeHash; + int64_t LicenseeHashSize; + int64_t Status; +} FlowObject; + +/*************************************************************************************************** + * Prototypes + **************************************************************************************************/ + +static int FlowObject_ResourceReadHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen); + +static int FlowObject_ResourceGetLengthHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID); + +static int FlowObject_ResourceWriteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, + bool * changed); + +static int FlowObject_ResourceCreateHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); + +static int FlowObject_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID); + +static int FlowObject_ObjectDeleteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); + +/*************************************************************************************************** + * Globals + **************************************************************************************************/ + +static char licenseeSecret[MAX_STRING_SIZE] = "getATTtDsNBpBRnMsN7GoQ=="; +FlowObject flowObject; + +static ObjectOperationHandlers flowObjectOperationHandlers = +{ + .CreateInstance = FlowObject_ObjectCreateInstanceHandler, + .Delete = FlowObject_ObjectDeleteHandler, +}; + +static ResourceOperationHandlers flowObjectResourceOperationHandlers = +{ + .Read = FlowObject_ResourceReadHandler, + .GetLength = FlowObject_ResourceGetLengthHandler, + .Write = FlowObject_ResourceWriteHandler, + .CreateOptionalResource = FlowObject_ResourceCreateHandler, + .Execute = NULL, +}; + +/*************************************************************************************************** + * Implementation - Private + **************************************************************************************************/ + +static int FlowObject_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID) +{ + return 0; +} + +static int FlowObject_ResourceCreateHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) +{ + return 0; +} + +static int FlowObject_ObjectDeleteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) +{ + + if (objectID != FLOWM2M_FLOW_OBJECT) + { + Lwm2m_Error("FlowObject_ObjectDeleteHandler Invalid OIR: %d/%d/%d\n", objectID, + objectInstanceID, resourceID); + return -1; + } + + if (objectInstanceID == 0) + { + memset(&flowObject, 0, sizeof(FlowObject)); + + if(flowObject.DeviceID) + free(flowObject.DeviceID); + if(flowObject.ParentID) + free(flowObject.ParentID); + if(flowObject.DeviceType) + free(flowObject.DeviceType); + if(flowObject.Name) + free(flowObject.Name); + if(flowObject.Description) + free(flowObject.Description); + if(flowObject.FCAP) + free(flowObject.FCAP); + if(flowObject.LicenseeChallenge) + free(flowObject.LicenseeChallenge); + if(flowObject.LicenseeHash) + free(flowObject.LicenseeHash); + } + else + { + Lwm2m_Error("FlowObject_ObjectDeleteHandler Invalid instance (not 0): %d", objectInstanceID); + return -1; + } + + return 0; +} + +static int FlowObject_ResourceReadHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen) +{ + int result = 0; + + switch (resourceID) + { + case FLOWM2M_FLOW_OBJECT_DEVICEID: + if(flowObject.DeviceID != NULL) + { + memcpy(destBuffer, flowObject.DeviceID, flowObject.DeviceIDSize); + result = flowObject.DeviceIDSize; + } + break; + + case FLOWM2M_FLOW_OBJECT_PARENTID: + if(flowObject.ParentID != NULL) + { + memcpy(destBuffer, flowObject.ParentID, flowObject.ParentIDSize); + result = flowObject.ParentIDSize; + } + break; + + case FLOWM2M_FLOW_OBJECT_DEVICETYPE: + if(flowObject.DeviceType != NULL) + { + memcpy(destBuffer, flowObject.DeviceType, strlen(flowObject.DeviceType) + 1); + result = strlen(flowObject.DeviceType); + } + break; + + case FLOWM2M_FLOW_OBJECT_NAME: + if(flowObject.Name != NULL) + { + memcpy(destBuffer, flowObject.Name, strlen(flowObject.Name) + 1); + result = strlen(flowObject.Name) + 1; + } + break; + + case FLOWM2M_FLOW_OBJECT_DESCRIPTION: + if(flowObject.Description != NULL) + { + memcpy(destBuffer, flowObject.Description, strlen(flowObject.Description) + 1); + result = strlen(flowObject.Description) + 1; + } + break; + + case FLOWM2M_FLOW_OBJECT_FCAP: + if(flowObject.FCAP != NULL) + { + memcpy(destBuffer, flowObject.FCAP, strlen(flowObject.FCAP) + 1); + result = strlen(flowObject.FCAP) + 1; + } + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEEID: + memcpy(destBuffer, &flowObject.LicenseeID, sizeof(flowObject.LicenseeID)); + result = sizeof(flowObject.LicenseeID); + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: + if(flowObject.LicenseeChallenge != NULL) + { + memcpy(destBuffer, flowObject.LicenseeChallenge, flowObject.LicenseeChallengeSize); + result = flowObject.LicenseeChallengeSize; + } + break; + + case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: + memcpy(destBuffer, &flowObject.HashIterations, sizeof(flowObject.HashIterations)); + result = sizeof(flowObject.HashIterations); + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: + if(flowObject.LicenseeHash != NULL) + { + memcpy(destBuffer, flowObject.LicenseeHash, flowObject.LicenseeHashSize); + result = flowObject.LicenseeHashSize; + } + break; + + case FLOWM2M_FLOW_OBJECT_STATUS: + memcpy(destBuffer, &flowObject.Status, sizeof(flowObject.Status)); + result = sizeof(flowObject.Status); + break; + + default: + result = -1; + break; + } + + return result; +} + +static int FlowObject_ResourceGetLengthHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID) +{ + int result = 0; + + switch (resourceID) + { + case FLOWM2M_FLOW_OBJECT_DEVICEID: + result = flowObject.DeviceIDSize; + break; + + case FLOWM2M_FLOW_OBJECT_PARENTID: + result = flowObject.ParentIDSize; + break; + + case FLOWM2M_FLOW_OBJECT_DEVICETYPE: + if(flowObject.DeviceType != NULL) + result = strlen(flowObject.DeviceType) + 1; + break; + + case FLOWM2M_FLOW_OBJECT_NAME: + if(flowObject.Name != NULL) + result = strlen(flowObject.Name) + 1; + break; + + case FLOWM2M_FLOW_OBJECT_DESCRIPTION: + if(flowObject.Description != NULL) + result = strlen(flowObject.Description) + 1; + break; + + case FLOWM2M_FLOW_OBJECT_FCAP: + if(flowObject.FCAP != NULL) + result = strlen(flowObject.FCAP) + 1; + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEEID: + result = sizeof(flowObject.LicenseeID); + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: + result = flowObject.LicenseeChallengeSize; + break; + + case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: + result = sizeof(flowObject.HashIterations); + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: + result = flowObject.LicenseeHashSize; + break; + + case FLOWM2M_FLOW_OBJECT_STATUS: + result = sizeof(flowObject.Status); + break; + + default: + result = -1; + break; + } + + return result; +} + +static bool CalculateLicenseeHash(char * licenseeSecret, uint8_t hash[SHA256_HASH_LENGTH], + const char * challenge, int challengeLength, int iterations) +{ + int i; + uint8_t key[MAX_KEY_SIZE]; + int keyLen = b64Decode(key, sizeof(key), licenseeSecret, strlen(licenseeSecret)); + + if (keyLen == -1) + { + return false; + } + + HmacSha256_ComputeHash(hash, challenge, challengeLength, key, keyLen); + for (i = 1; i < iterations; i++) + { + HmacSha256_ComputeHash(hash, hash, SHA256_HASH_LENGTH, key, keyLen); + } + return true; +} + +static int FlowObject_ResourceWriteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, bool * changed) +{ + int result; + + switch(resourceID) + { + case FLOWM2M_FLOW_OBJECT_DEVICEID: + if(flowObject.DeviceID) + free(flowObject.DeviceID); + flowObject.DeviceID = malloc(srcBufferLen); + memcpy(flowObject.DeviceID, srcBuffer, srcBufferLen); + result = flowObject.DeviceIDSize = srcBufferLen; + break; + + case FLOWM2M_FLOW_OBJECT_PARENTID: + if(flowObject.ParentID) + free(flowObject.ParentID); + flowObject.ParentID = malloc(srcBufferLen); + memcpy(flowObject.ParentID, srcBuffer, srcBufferLen); + result = flowObject.ParentIDSize = srcBufferLen; + break; + + case FLOWM2M_FLOW_OBJECT_DEVICETYPE: + if(flowObject.DeviceType) + free(flowObject.DeviceType); + flowObject.DeviceType = malloc(srcBufferLen + 1); + memset(flowObject.DeviceType, 0, srcBufferLen + 1); + memcpy(flowObject.DeviceType, srcBuffer, srcBufferLen); + Lwm2m_Debug("Device type: %s\n", flowObject.DeviceType); + result = srcBufferLen; + break; + + case FLOWM2M_FLOW_OBJECT_NAME: + if(flowObject.Name) + free(flowObject.Name); + flowObject.Name = malloc(srcBufferLen + 1); + memset(flowObject.Name, 0, srcBufferLen + 1); + memcpy(flowObject.Name, srcBuffer, srcBufferLen); + result = srcBufferLen; + break; + + case FLOWM2M_FLOW_OBJECT_DESCRIPTION: + if(flowObject.Description) + free(flowObject.Description); + flowObject.Description = malloc(srcBufferLen + 1); + memset(flowObject.Description, 0, srcBufferLen + 1); + memcpy(flowObject.Description, srcBuffer, srcBufferLen); + result = srcBufferLen; + break; + + case FLOWM2M_FLOW_OBJECT_FCAP: + if(flowObject.FCAP) + free(flowObject.FCAP); + flowObject.FCAP = malloc(srcBufferLen + 1); + memset(flowObject.FCAP, 0, srcBufferLen + 1); + memcpy(flowObject.FCAP, srcBuffer, srcBufferLen); + Lwm2m_Error("FCAP: %s\n", flowObject.FCAP); + result = srcBufferLen; + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEEID: + memcpy(&flowObject.LicenseeID, srcBuffer, srcBufferLen); + result = srcBufferLen; + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: + if(flowObject.LicenseeChallenge) + free(flowObject.LicenseeChallenge); + flowObject.LicenseeChallenge = malloc(srcBufferLen); + memcpy(flowObject.LicenseeChallenge, srcBuffer, srcBufferLen); + result = flowObject.LicenseeChallengeSize = srcBufferLen; + break; + + case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: + memcpy(&flowObject.HashIterations, srcBuffer, srcBufferLen); + result = srcBufferLen; + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: + if(flowObject.LicenseeHash) + free(flowObject.LicenseeHash); + flowObject.LicenseeHash = malloc(srcBufferLen); + memcpy(flowObject.LicenseeHash, srcBuffer, srcBufferLen); + result = flowObject.LicenseeHashSize = srcBufferLen; + *changed = true; + break; + + case FLOWM2M_FLOW_OBJECT_STATUS: + memcpy(&flowObject.Status, srcBuffer, srcBufferLen); + Lwm2m_Debug("Status: %d\n", (int)flowObject.Status); + result = srcBufferLen; + break; + + default: + result = -1; + break; + } + + if(flowObject.HashIterations > 0 && + flowObject.LicenseeChallengeSize > 0 && + flowObject.LicenseeChallenge != NULL && + (resourceID == FLOWM2M_FLOW_OBJECT_HASHITERATIONS || + (resourceID == FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE))) + { + uint8_t licenseeHash[SHA256_HASH_LENGTH]; + + Lwm2m_Debug("Calculating licensee hash with %d iterations...\n", + (int)flowObject.HashIterations); + + if (CalculateLicenseeHash(licenseeSecret, licenseeHash, flowObject.LicenseeChallenge, + flowObject.LicenseeChallengeSize, flowObject.HashIterations)) + { + Lwm2m_Debug("Calculated hash, writing Licensee Hash resource...\n"); + if (Lwm2mCore_SetResourceInstanceValue(context, FLOWM2M_FLOW_OBJECT, 0, + FLOWM2M_FLOW_OBJECT_LICENSEEHASH, 0, licenseeHash, sizeof(licenseeHash)) == -1) + { + Lwm2m_Error("Failed to set Licensee Hash\n"); + return -1; + } + } + else + { + Lwm2m_Error("Licensee secret is invalid\n"); + return -1; + } + } + + return result; +} + +/*************************************************************************************************** + * Implementation - Public + **************************************************************************************************/ + +int Lwm2m_RegisterFlowObject(Lwm2mContextType * context) +{ + REGISTER_OBJECT(context, "FlowObject", FLOWM2M_FLOW_OBJECT, MultipleInstancesEnum_Single, \ + MandatoryEnum_Optional, &flowObjectOperationHandlers); + REGISTER_FLOW_OBJECT_RESOURCE(context, "DeviceID", FLOWM2M_FLOW_OBJECT_DEVICEID, \ + ResourceTypeEnum_TypeOpaque, MandatoryEnum_Mandatory); + REGISTER_FLOW_OBJECT_RESOURCE(context, "ParentID", FLOWM2M_FLOW_OBJECT_PARENTID, \ + ResourceTypeEnum_TypeOpaque, MandatoryEnum_Optional); + REGISTER_FLOW_OBJECT_RESOURCE(context, "DeviceType", FLOWM2M_FLOW_OBJECT_DEVICETYPE, \ + ResourceTypeEnum_TypeString, MandatoryEnum_Mandatory); + REGISTER_FLOW_OBJECT_RESOURCE(context, "Name", FLOWM2M_FLOW_OBJECT_NAME, \ + ResourceTypeEnum_TypeString, MandatoryEnum_Optional); + REGISTER_FLOW_OBJECT_RESOURCE(context, "Description", FLOWM2M_FLOW_OBJECT_DESCRIPTION, \ + ResourceTypeEnum_TypeString, MandatoryEnum_Optional); + REGISTER_FLOW_OBJECT_RESOURCE(context, "FCAP", FLOWM2M_FLOW_OBJECT_FCAP, \ + ResourceTypeEnum_TypeString, MandatoryEnum_Mandatory); + REGISTER_FLOW_OBJECT_RESOURCE(context, "LicenseeID", FLOWM2M_FLOW_OBJECT_LICENSEEID, \ + ResourceTypeEnum_TypeInteger, MandatoryEnum_Mandatory); + REGISTER_FLOW_OBJECT_RESOURCE(context, "LicenseeChallenge", FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE, \ + ResourceTypeEnum_TypeOpaque, MandatoryEnum_Optional); + REGISTER_FLOW_OBJECT_RESOURCE(context, "HashIterations", FLOWM2M_FLOW_OBJECT_HASHITERATIONS, \ + ResourceTypeEnum_TypeInteger, MandatoryEnum_Optional); + REGISTER_FLOW_OBJECT_RESOURCE(context, "LicenseeHash", FLOWM2M_FLOW_OBJECT_LICENSEEHASH, \ + ResourceTypeEnum_TypeOpaque, MandatoryEnum_Optional); + REGISTER_FLOW_OBJECT_RESOURCE(context, "Status", FLOWM2M_FLOW_OBJECT_STATUS, \ + ResourceTypeEnum_TypeInteger, MandatoryEnum_Optional); + + return 0; +} + +int Lwm2m_SetProvisioningInfo(Lwm2mContextType * context, const char * DeviceType, + const char * FCAP, int64_t LicenseeID) +{ + CREATE_OBJECT_INSTANCE(context, FLOWM2M_FLOW_OBJECT, 0); + + if (Lwm2mCore_SetResourceInstanceValue(context, FLOWM2M_FLOW_OBJECT, 0, + FLOWM2M_FLOW_OBJECT_FCAP, 0, FCAP, strlen(FCAP)) == -1) + { + Lwm2m_Error("Failed to set FCAP to %s\n", FCAP); + return -1; + } + + if (Lwm2mCore_SetResourceInstanceValue(context, FLOWM2M_FLOW_OBJECT, 0, + FLOWM2M_FLOW_OBJECT_DEVICETYPE, 0, DeviceType, strlen(DeviceType)) == -1) + { + Lwm2m_Error("Failed to set Device Type to %s\n", DeviceType); + return -1; + } + + if (Lwm2mCore_SetResourceInstanceValue(context, FLOWM2M_FLOW_OBJECT, 0, + FLOWM2M_FLOW_OBJECT_LICENSEEID, 0, &LicenseeID, sizeof(int64_t)) == -1) + { + Lwm2m_Error("Failed to set Licensee Id to %" PRId64 "\n", LicenseeID); + return -1; + } + return 0; +} diff --git a/lwm2m-client-flow-object.h b/lwm2m-client-flow-object.h new file mode 100644 index 0000000..2d6b024 --- /dev/null +++ b/lwm2m-client-flow-object.h @@ -0,0 +1,43 @@ +/** + * @file + * LightWeightM2M LWM2M Flow object. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef LWM2M_CLIENT_FLOW_OBJECT_H_ +#define LWM2M_CLIENT_FLOW_OBJECT_H_ + +int Lwm2m_RegisterFlowObject(Lwm2mContextType * context); +int Lwm2m_SetProvisioningInfo(Lwm2mContextType * context, const char * DeviceType, + const char * FCAP, int64_t LicenseeID); + +#endif /* LWM2M_CLIENT_FLOW_OBJECT_H_ */ diff --git a/lwm2m-client-ipso-digital-input.c b/lwm2m-client-ipso-digital-input.c new file mode 100644 index 0000000..1a70d97 --- /dev/null +++ b/lwm2m-client-ipso-digital-input.c @@ -0,0 +1,445 @@ +/** + * @file + * LightWeightM2M LWM2M Digital Input object. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/*************************************************************************************************** + * Includes + **************************************************************************************************/ + +#include +#include +#include +#include +#include "lwm2m_core.h" +#include "coap_abstraction.h" +#include "common.h" + +/*************************************************************************************************** + * Definitions + **************************************************************************************************/ + +#define IPSO_DIGITAL_INPUT_OBJECT 3200 +#define IPSO_DIGITAL_INPUT_STATE 5500 +#define IPSO_DIGITAL_INPUT_COUNTER 5501 +#define IPSO_DIGITAL_INPUT_POLARITY 5502 +#define IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD 5503 +#define IPSO_DIGITAL_INPUT_EDGE_SELECTION 5504 +#define IPSO_DIGITAL_INPUT_COUNTER_RESET 5505 + +#define IPSO_APPICATION_TYPE 5750 +#define IPSO_SENSOR_TYPE 5751 + +#define DIGITAL_INPUTS 2 + +#define MAX_STR_SIZE 128 + +#define REGISTER_DIGITAL_INPUT_RESOURCE(context, name, id, type, operations) \ + REGISTER_RESOURCE(context, name, IPSO_DIGITAL_INPUT_OBJECT, id, type, \ + MultipleInstancesEnum_Single, MandatoryEnum_Optional, operations, \ + &DigitalInputResourceOperationHandlers) + +#define CREATE_DIGITAL_INPUT_OPTIONAL_RESOURCE(context, objectInstanceId, resourcId) \ + CREATE_OPTIONAL_RESOURCE(context, IPSO_DIGITAL_INPUT_OBJECT, objectInstanceId, resourcId) + +/*************************************************************************************************** + * Typedefs + **************************************************************************************************/ + +typedef struct +{ + bool State; + int64_t Counter; + bool Polarity; + int64_t DebouncePeriod; + int64_t EdgeSelection; + char ApplicationType[MAX_STR_SIZE]; + char SensoryType[MAX_STR_SIZE]; +} IPSODigitalInput; + +/*************************************************************************************************** + * Prototypes + **************************************************************************************************/ + +static int DigitalInput_ResourceReadHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t *destBuffer, int destBufferLen); + +static int DigitalInput_ResourceGetLengthHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID); + +static int DigitalInput_ResourceWriteHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t *srcBuffer, int srcBufferLen, bool *changed); + +static int DigitalInput_ResourceExecuteHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, uint8_t *srcBuffer, + int srcBufferLen); + +static int DigitalInput_ResourceCreateHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); + +static int DigitalInput_ObjectCreateInstanceHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID); + +static int DigitalInput_ObjectDeleteHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); + +/*************************************************************************************************** + * Globals + **************************************************************************************************/ + +static ObjectOperationHandlers DigitalInputObjectOperationHandlers = +{ + .CreateInstance = DigitalInput_ObjectCreateInstanceHandler, + .Delete = DigitalInput_ObjectDeleteHandler, +}; + +static ResourceOperationHandlers DigitalInputResourceOperationHandlers = +{ + .Read = DigitalInput_ResourceReadHandler, + .GetLength = DigitalInput_ResourceGetLengthHandler, + .Write = DigitalInput_ResourceWriteHandler, + .CreateOptionalResource = DigitalInput_ResourceCreateHandler, + .Execute = DigitalInput_ResourceExecuteHandler, +}; + +static IPSODigitalInput digitalInputs[DIGITAL_INPUTS]; + +/*************************************************************************************************** + * Implementation + **************************************************************************************************/ + +static int DigitalInput_ObjectCreateInstanceHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID) +{ + if(objectInstanceID >= DIGITAL_INPUTS) + { + Lwm2m_Error("DigitalInput_ResourceCreateHandler instance number %d out of range (max %d)", + objectInstanceID, DIGITAL_INPUTS - 1); + return -1; + } + return objectInstanceID; +} + +static int DigitalInput_ResourceCreateHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) +{ + return 0; +} + +static int DigitalInput_ObjectDeleteHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) +{ + if (objectID != IPSO_DIGITAL_INPUT_OBJECT) + { + Lwm2m_Error("DigitalInput_ObjectDeleteHandler Invalid OIR: %d/%d/%d\n", objectID, + objectInstanceID, resourceID); + return -1; + } + + if(objectInstanceID >= DIGITAL_INPUTS) + { + Lwm2m_Error("DigitalInput_ObjectDeleteHandler instance number %d out of range (max %d)", + objectInstanceID, DIGITAL_INPUTS - 1); + return -1; + } + + if (resourceID == -1) + { + memset(&digitalInputs[objectInstanceID], 0, sizeof(IPSODigitalInput)); + } + else + { + //TODO + } + + return 0; +} + +static int DigitalInput_ResourceReadHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t *destBuffer, int destBufferLen) +{ + int result = 0; + + switch (resourceID) + { + case IPSO_DIGITAL_INPUT_STATE: + result = sizeof(digitalInputs[objectInstanceID].State); + memcpy(destBuffer, &digitalInputs[objectInstanceID].State, result); + break; + + case IPSO_DIGITAL_INPUT_COUNTER: + result = sizeof(digitalInputs[objectInstanceID].Counter); + memcpy(destBuffer, &digitalInputs[objectInstanceID].Counter, result); + break; + + case IPSO_DIGITAL_INPUT_POLARITY: + result = sizeof(digitalInputs[objectInstanceID].State); + memcpy(destBuffer, &digitalInputs[objectInstanceID].State, result); + break; + + case IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD: + result = sizeof(digitalInputs[objectInstanceID].Counter); + memcpy(destBuffer, &digitalInputs[objectInstanceID].Counter, result); + break; + + case IPSO_DIGITAL_INPUT_EDGE_SELECTION: + result = sizeof(digitalInputs[objectInstanceID].State); + memcpy(destBuffer, &digitalInputs[objectInstanceID].State, result); + break; + + case IPSO_APPICATION_TYPE: + result = strlen(digitalInputs[objectInstanceID].ApplicationType) + 1; + memcpy(destBuffer, digitalInputs[objectInstanceID].ApplicationType, result); + break; + + case IPSO_SENSOR_TYPE: + result = strlen(digitalInputs[objectInstanceID].SensoryType) + 1; + memcpy(destBuffer, digitalInputs[objectInstanceID].SensoryType, result); + break; + + default: + result = -1; + break; + } + + return result; +} + +static int DigitalInput_ResourceGetLengthHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID) +{ + int result = 0; + + switch (resourceID) + { + case IPSO_DIGITAL_INPUT_STATE: + result = sizeof(digitalInputs[objectInstanceID].State); + break; + + case IPSO_DIGITAL_INPUT_COUNTER: + result = sizeof(digitalInputs[objectInstanceID].Counter); + break; + + case IPSO_DIGITAL_INPUT_POLARITY: + result = sizeof(digitalInputs[objectInstanceID].State); + break; + + case IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD: + result = sizeof(digitalInputs[objectInstanceID].Counter); + break; + + case IPSO_DIGITAL_INPUT_EDGE_SELECTION: + result = sizeof(digitalInputs[objectInstanceID].State); + break; + + case IPSO_APPICATION_TYPE: + result = strlen(digitalInputs[objectInstanceID].ApplicationType) + 1; + break; + + case IPSO_SENSOR_TYPE: + result = strlen(digitalInputs[objectInstanceID].SensoryType) + 1; + break; + + default: + result = -1; + break; + } + + return result; +} + +static int DigitalInput_ResourceWriteHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t *srcBuffer, int srcBufferLen, bool *changed) +{ + int result; + + switch(resourceID) + { + case IPSO_DIGITAL_INPUT_STATE: + result = srcBufferLen; + memcpy(&digitalInputs[objectInstanceID].State, srcBuffer, result); + break; + + case IPSO_DIGITAL_INPUT_COUNTER: + result = srcBufferLen; + memcpy(&digitalInputs[objectInstanceID].Counter, srcBuffer, result); + Lwm2m_Debug("Button %d counter incremented to %d.\n", objectInstanceID + 1, + (int)digitalInputs[objectInstanceID].Counter); + break; + + case IPSO_DIGITAL_INPUT_POLARITY: + result = srcBufferLen; + memcpy(&digitalInputs[objectInstanceID].State, srcBuffer, result); + break; + + case IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD: + result = srcBufferLen; + memcpy(&digitalInputs[objectInstanceID].Counter, srcBuffer, result); + break; + + case IPSO_DIGITAL_INPUT_EDGE_SELECTION: + result = srcBufferLen; + memcpy(&digitalInputs[objectInstanceID].State, srcBuffer, result); + break; + + case IPSO_APPICATION_TYPE: + result = srcBufferLen; + if(result < sizeof(digitalInputs[objectInstanceID].ApplicationType)) + { + memcpy(digitalInputs[objectInstanceID].ApplicationType, srcBuffer, result); + } + else + { + Lwm2m_Error("DigitalInput_ResourceWriteHandler Application Type string too long: " + "%d", result); + result = -1; + } + break; + + case IPSO_SENSOR_TYPE: + result = srcBufferLen; + if(result < sizeof(digitalInputs[objectInstanceID].SensoryType)) + { + memcpy(digitalInputs[objectInstanceID].SensoryType, srcBuffer, result); + } + else + { + Lwm2m_Error("DigitalInput_ResourceWriteHandler Sensor Type string too long: %d", + result); + result = -1; + } + break; + + default: + result = -1; + break; + } + + if(result > 0) + *changed = true; + + return result; +} + +static int DigitalInput_ResourceExecuteHandler(void *context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, uint8_t *srcBuffer, + int srcBufferLen) +{ + if(resourceID == IPSO_DIGITAL_INPUT_COUNTER_RESET) + { + int64_t zero = 0; + if (Lwm2mCore_SetResourceInstanceValue(context, objectID, objectInstanceID, + IPSO_DIGITAL_INPUT_COUNTER, 0, &zero, sizeof(zero)) == -1) + { + Lwm2m_Error("Failed to set Counter to %" PRId64 "\n", zero); + return -1; + } + } + else + return -1; + + return 0; +} + +/*************************************************************************************************** + * Implementation - Public + **************************************************************************************************/ + +int DigitalInput_RegisterDigitalInputObject(Lwm2mContextType * context) +{ + + REGISTER_OBJECT(context, "DigitalInput", IPSO_DIGITAL_INPUT_OBJECT, \ + MultipleInstancesEnum_Multiple, MandatoryEnum_Optional, \ + &DigitalInputObjectOperationHandlers); + + REGISTER_DIGITAL_INPUT_RESOURCE(context, "State", IPSO_DIGITAL_INPUT_STATE, \ + ResourceTypeEnum_TypeBoolean, Operations_R); + REGISTER_DIGITAL_INPUT_RESOURCE(context, "Counter", IPSO_DIGITAL_INPUT_COUNTER, \ + ResourceTypeEnum_TypeInteger, Operations_R); + REGISTER_DIGITAL_INPUT_RESOURCE(context, "Polarity", IPSO_DIGITAL_INPUT_POLARITY, \ + ResourceTypeEnum_TypeBoolean, Operations_RW); + REGISTER_DIGITAL_INPUT_RESOURCE(context, "DebouncePeriod", IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD, \ + ResourceTypeEnum_TypeInteger, Operations_RW); + REGISTER_DIGITAL_INPUT_RESOURCE(context, "EdgeSelection", IPSO_DIGITAL_INPUT_EDGE_SELECTION, \ + ResourceTypeEnum_TypeInteger, Operations_RW); + REGISTER_DIGITAL_INPUT_RESOURCE(context, "CounterReset", IPSO_DIGITAL_INPUT_COUNTER_RESET, \ + ResourceTypeEnum_TypeNone, Operations_E); + REGISTER_DIGITAL_INPUT_RESOURCE(context, "ApplicationType", IPSO_APPICATION_TYPE, \ + ResourceTypeEnum_TypeString, Operations_R); + REGISTER_DIGITAL_INPUT_RESOURCE(context, "SensorType", IPSO_SENSOR_TYPE, \ + ResourceTypeEnum_TypeString, Operations_R); + + return 0; +} + +int DigitalInput_AddDigitialInput(Lwm2mContextType *context, ObjectInstanceIDType objectInstanceID) +{ + if(objectInstanceID < DIGITAL_INPUTS) + { + CREATE_OBJECT_INSTANCE(context, IPSO_DIGITAL_INPUT_OBJECT, objectInstanceID); + CREATE_DIGITAL_INPUT_OPTIONAL_RESOURCE(context, objectInstanceID, \ + IPSO_DIGITAL_INPUT_COUNTER); + CREATE_DIGITAL_INPUT_OPTIONAL_RESOURCE(context, objectInstanceID, \ + IPSO_DIGITAL_INPUT_COUNTER_RESET); + CREATE_DIGITAL_INPUT_OPTIONAL_RESOURCE(context, objectInstanceID, IPSO_SENSOR_TYPE); + + memset(&digitalInputs[objectInstanceID], 0, sizeof(IPSODigitalInput)); + snprintf(digitalInputs[objectInstanceID].SensoryType, MAX_STR_SIZE, "Button%d", + objectInstanceID + 1); + } + else + { + Lwm2m_Error("%d instance of Digital Input exceeds max instances %d\n", objectInstanceID, + DIGITAL_INPUTS); + return -1; + } + return 0; +} + +int DigitalInput_IncrementCounter(Lwm2mContextType *context, ObjectInstanceIDType objectInstanceID) +{ + int64_t counter = digitalInputs[objectInstanceID].Counter + 1; + if (Lwm2mCore_SetResourceInstanceValue(context, IPSO_DIGITAL_INPUT_OBJECT, objectInstanceID, + IPSO_DIGITAL_INPUT_COUNTER, 0, &counter, sizeof(counter)) == -1) + { + Lwm2m_Error("Failed to increment Counter resource\n"); + return -1; + } + return 0; +} diff --git a/lwm2m-client-ipso-digital-input.h b/lwm2m-client-ipso-digital-input.h new file mode 100644 index 0000000..b2f1676 --- /dev/null +++ b/lwm2m-client-ipso-digital-input.h @@ -0,0 +1,45 @@ +/** + * @file + * LightWeightM2M LWM2M Digital Input object. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef LWM2M_CLIENT_IPSO_DIGITAL_INPUT_H_ +#define LWM2M_CLIENT_IPSO_DIGITAL_INPUT_H_ + +#include "lwm2m_core.h" + +int DigitalInput_RegisterDigitalInputObject(Lwm2mContextType * context); +int DigitalInput_AddDigitialInput(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID); +int DigitalInput_IncrementCounter(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID); + +#endif /* LWM2M_CLIENT_IPSO_DIGITAL_INPUT_H_ */ diff --git a/lwm2m-client-ipso-light-control.c b/lwm2m-client-ipso-light-control.c new file mode 100644 index 0000000..ba7ebfb --- /dev/null +++ b/lwm2m-client-ipso-light-control.c @@ -0,0 +1,451 @@ +/** + * @file + * LightWeightM2M LWM2M Light Control object. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/*************************************************************************************************** + * Includes + **************************************************************************************************/ + +#include +#include +#include +#include +#include "lwm2m_core.h" +#include "coap_abstraction.h" +#include "lwm2m-client-ipso-light-control.h" +#include "common.h" + +/*************************************************************************************************** + * Definitions + **************************************************************************************************/ + +#define IPSO_LIGHT_CONTROL_OBJECT 3311 +#define IPSO_LIGHT_CONTROL_ON_OFF 5850 +#define IPSO_LIGHT_CONTROL_DIMMER 5851 +#define IPSO_LIGHT_CONTROL_COLOUR 5706 +#define IPSO_LIGHT_CONTROL_UNITS 5701 +#define IPSO_LIGHT_CONTROL_ON_TIME 5852 +#define IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER 5805 +#define IPSO_LIGHT_CONTROL_POWER_FACTOR 5820 + +#define LIGHT_CONTROLS 2 + +#define MAX_STR_SIZE 64 + +#define REGISTER_LIGHT_CONTROL_RESOURCE(context, name, id, type, minInstances, operations) \ + REGISTER_RESOURCE(context, name, IPSO_LIGHT_CONTROL_OBJECT, id, type, \ + MultipleInstancesEnum_Single, minInstances, operations, \ + &LightControlResourceOperationHandlers) + +#define CREATE_LIGHT_CONTROL_OPTIONAL_RESOURCE(context, objectInstanceId, resourcId) \ + CREATE_OPTIONAL_RESOURCE(context, IPSO_LIGHT_CONTROL_OBJECT, objectInstanceId, resourcId) + +/*************************************************************************************************** + * Typedefs + **************************************************************************************************/ + +typedef struct +{ + bool OnOff; + int64_t Dimmer; + char Colour[MAX_STR_SIZE]; + char Units[MAX_STR_SIZE]; + int64_t OnTime; + float CumulativeActivePower; + float PowerFactor; + LightControlCallBack callback; + void * context; +} IPSOLightControl; + +/*************************************************************************************************** + * Prototypes + **************************************************************************************************/ + +static int LightControl_ResourceReadHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen); + +static int LightControl_ResourceGetLengthHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID); + +static int LightControl_ResourceWriteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, + bool * changed); + + +static int LightControl_ResourceCreateHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); + +static int LightControl_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID); + +static int LightControl_ObjectDeleteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); + +/*************************************************************************************************** + * Globals + **************************************************************************************************/ + +static ObjectOperationHandlers LightControlObjectOperationHandlers = +{ + .CreateInstance = LightControl_ObjectCreateInstanceHandler, + .Delete = LightControl_ObjectDeleteHandler, +}; + +static ResourceOperationHandlers LightControlResourceOperationHandlers = +{ + .Read = LightControl_ResourceReadHandler, + .GetLength = LightControl_ResourceGetLengthHandler, + .Write = LightControl_ResourceWriteHandler, + .CreateOptionalResource = LightControl_ResourceCreateHandler, + .Execute = NULL, +}; + +static IPSOLightControl LightControls[LIGHT_CONTROLS]; + +/*************************************************************************************************** + * Implementation + **************************************************************************************************/ + +static int LightControl_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID) +{ + if(objectInstanceID >= LIGHT_CONTROLS) + { + Lwm2m_Error("LightControl_ResourceCreateHandler instance number %d out of range (max %d)", + objectInstanceID, LIGHT_CONTROLS - 1); + return -1; + } + + return objectInstanceID; +} + +static int LightControl_ResourceCreateHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) +{ + return 0; +} + +static int LightControl_ObjectDeleteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) +{ + if (objectID != IPSO_LIGHT_CONTROL_OBJECT) + { + Lwm2m_Error("LightControl_ObjectDeleteHandler Invalid OIR: %d/%d/%d\n", objectID, + objectInstanceID, resourceID); + return -1; + } + + if(objectInstanceID >= LIGHT_CONTROLS) + { + Lwm2m_Error("LightControl_ObjectDeleteHandler instance number %d out of range (max %d)", + objectInstanceID, LIGHT_CONTROLS - 1); + return -1; + } + + if (resourceID == -1) + { + memset(&LightControls[objectInstanceID], 0, sizeof(IPSOLightControl)); + } + else + { + //TODO + } + + return 0; +} + +static int LightControl_ResourceReadHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen) +{ + int result = 0; + + switch (resourceID) + { + case IPSO_LIGHT_CONTROL_ON_OFF: + result = sizeof(LightControls[objectInstanceID].OnOff); + memcpy(destBuffer, &LightControls[objectInstanceID].OnOff, result); + break; + + case IPSO_LIGHT_CONTROL_DIMMER: + result = sizeof(LightControls[objectInstanceID].Dimmer); + memcpy(destBuffer, &LightControls[objectInstanceID].Dimmer, result); + break; + + case IPSO_LIGHT_CONTROL_COLOUR: + result = strlen(LightControls[objectInstanceID].Colour) + 1; + memcpy(destBuffer, LightControls[objectInstanceID].Colour, result); + break; + + case IPSO_LIGHT_CONTROL_UNITS: + result = strlen(LightControls[objectInstanceID].Units) + 1; + memcpy(destBuffer, LightControls[objectInstanceID].Units, result); + break; + + case IPSO_LIGHT_CONTROL_ON_TIME: + result = sizeof(LightControls[objectInstanceID].OnTime); + memcpy(destBuffer, &LightControls[objectInstanceID].OnTime, result); + break; + + case IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER: + result = sizeof(LightControls[objectInstanceID].CumulativeActivePower); + memcpy(destBuffer, &LightControls[objectInstanceID].CumulativeActivePower, result); + break; + + case IPSO_LIGHT_CONTROL_POWER_FACTOR: + result = sizeof(LightControls[objectInstanceID].PowerFactor); + memcpy(destBuffer, &LightControls[objectInstanceID].PowerFactor, result); + break; + + default: + result = -1; + break; + } + + return result; +} + +static int LightControl_ResourceGetLengthHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID) +{ + int result = 0; + + switch (resourceID) + { + case IPSO_LIGHT_CONTROL_ON_OFF: + result = sizeof(LightControls[objectInstanceID].OnOff); + break; + + case IPSO_LIGHT_CONTROL_DIMMER: + result = sizeof(LightControls[objectInstanceID].Dimmer); + break; + + case IPSO_LIGHT_CONTROL_COLOUR: + result = strlen(LightControls[objectInstanceID].Colour) + 1; + break; + + case IPSO_LIGHT_CONTROL_UNITS: + result = strlen(LightControls[objectInstanceID].Units) + 1; + break; + + case IPSO_LIGHT_CONTROL_ON_TIME: + result = sizeof(LightControls[objectInstanceID].OnTime); + break; + + case IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER: + result = sizeof(LightControls[objectInstanceID].CumulativeActivePower); + break; + + case IPSO_LIGHT_CONTROL_POWER_FACTOR: + result = sizeof(LightControls[objectInstanceID].PowerFactor); + break; + + default: + result = -1; + break; + } + + return result; +} + +static int LightControl_ResourceWriteHandler(void * context, ObjectIDType objectID, + ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, + ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, bool * changed) +{ + int result; + bool CallCallback = false; + + switch(resourceID) + { + case IPSO_LIGHT_CONTROL_ON_OFF: + result = srcBufferLen; + memcpy(&LightControls[objectInstanceID].OnOff, srcBuffer, result); + CallCallback = true; + break; + + case IPSO_LIGHT_CONTROL_DIMMER: + result = srcBufferLen; + memcpy(&LightControls[objectInstanceID].Dimmer, srcBuffer, result); + CallCallback = true; + break; + + case IPSO_LIGHT_CONTROL_COLOUR: + result = srcBufferLen; + if(result < sizeof(LightControls[objectInstanceID].Colour)) + { + memcpy(LightControls[objectInstanceID].Colour, srcBuffer, result); + } + else + { + Lwm2m_Error("LightControl_ResourceWriteHandler Colour Type string too long: %d", + result); + result = -1; + } + CallCallback = true; + break; + + case IPSO_LIGHT_CONTROL_UNITS: + result = srcBufferLen; + if(result < sizeof(LightControls[objectInstanceID].Units)) + { + memcpy(LightControls[objectInstanceID].Units, srcBuffer, result); + } + else + { + Lwm2m_Error("LightControl_ResourceWriteHandler Units Type string too long: %d", + result); + result = -1; + } + break; + + case IPSO_LIGHT_CONTROL_ON_TIME: + result = srcBufferLen; + memcpy(&LightControls[objectInstanceID].OnTime, srcBuffer, result); + break; + + case IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER: + result = srcBufferLen; + memcpy(&LightControls[objectInstanceID].CumulativeActivePower, srcBuffer, result); + break; + + case IPSO_LIGHT_CONTROL_POWER_FACTOR: + result = srcBufferLen; + memcpy(&LightControls[objectInstanceID].PowerFactor, srcBuffer, result); + break; + default: + + result = -1; + break; + } + + if (LightControls[objectInstanceID].callback != NULL && CallCallback) + { + LightControls[objectInstanceID].callback(LightControls[objectInstanceID].context, + LightControls[objectInstanceID].OnOff, LightControls[objectInstanceID].Dimmer, + LightControls[objectInstanceID].Colour); + } + + + if(result > 0) + *changed = true; + + return result; +} + +/*************************************************************************************************** + * Implementation - Public + **************************************************************************************************/ + +int LightControl_RegisterLightControlObject(Lwm2mContextType * context) +{ + REGISTER_OBJECT(context, "LightControl", IPSO_LIGHT_CONTROL_OBJECT, \ + MultipleInstancesEnum_Multiple, MandatoryEnum_Optional, \ + &LightControlObjectOperationHandlers); + + REGISTER_LIGHT_CONTROL_RESOURCE(context, "On/Off", IPSO_LIGHT_CONTROL_ON_OFF, \ + ResourceTypeEnum_TypeBoolean, MandatoryEnum_Mandatory, Operations_RW); + REGISTER_LIGHT_CONTROL_RESOURCE(context, "Dimmer", IPSO_LIGHT_CONTROL_DIMMER, \ + ResourceTypeEnum_TypeInteger, MandatoryEnum_Optional, Operations_RW); + REGISTER_LIGHT_CONTROL_RESOURCE(context, "Colour", IPSO_LIGHT_CONTROL_COLOUR, \ + ResourceTypeEnum_TypeString, MandatoryEnum_Optional, Operations_RW); + REGISTER_LIGHT_CONTROL_RESOURCE(context, "Units", IPSO_LIGHT_CONTROL_UNITS, \ + ResourceTypeEnum_TypeString, MandatoryEnum_Mandatory, Operations_R); + REGISTER_LIGHT_CONTROL_RESOURCE(context, "OnTime", IPSO_LIGHT_CONTROL_ON_TIME, \ + ResourceTypeEnum_TypeInteger, MandatoryEnum_Optional, Operations_RW); + REGISTER_LIGHT_CONTROL_RESOURCE(context, "CumulativeActivePower", \ + IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER, ResourceTypeEnum_TypeFloat, \ + MandatoryEnum_Optional, Operations_R); + REGISTER_LIGHT_CONTROL_RESOURCE(context, "PowerFactor", IPSO_LIGHT_CONTROL_POWER_FACTOR, \ + ResourceTypeEnum_TypeFloat, MandatoryEnum_Optional, Operations_R); + + return 0; +} + +int LightControl_AddLightControl(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID, + LightControlCallBack callback, void * callbackContext) +{ + if(objectInstanceID <= LIGHT_CONTROLS) + { + CREATE_OBJECT_INSTANCE(context, IPSO_LIGHT_CONTROL_OBJECT, objectInstanceID); + CREATE_LIGHT_CONTROL_OPTIONAL_RESOURCE(context, objectInstanceID, IPSO_LIGHT_CONTROL_ON_OFF); + CREATE_LIGHT_CONTROL_OPTIONAL_RESOURCE(context, objectInstanceID, IPSO_LIGHT_CONTROL_COLOUR); + CREATE_LIGHT_CONTROL_OPTIONAL_RESOURCE(context, objectInstanceID, IPSO_LIGHT_CONTROL_ON_TIME); + + memset(&LightControls[objectInstanceID], 0, sizeof(IPSOLightControl)); + snprintf(LightControls[objectInstanceID].Colour, MAX_STR_SIZE, "Red%d", objectInstanceID+1); + + LightControls[objectInstanceID].callback = callback; + LightControls[objectInstanceID].context = callbackContext; + + bool state = false; + + if (Lwm2mCore_SetResourceInstanceValue(context, IPSO_LIGHT_CONTROL_OBJECT, objectInstanceID, + IPSO_LIGHT_CONTROL_ON_OFF, 0, &state, sizeof(state)) == -1) + { + Lwm2m_Error("Failed to set On/Off resource to %s", state ? "true" : "false"); + return -1; + } + } + else + { + Lwm2m_Error("%d instance of Light Control exceeds max instances %d\n", objectInstanceID, + LIGHT_CONTROLS); + return -1; + } + return 0; +} + +int LightControl_IncrementOnTime(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID, + int seconds) +{ + //only increment on time if it is on. + if(LightControls[objectInstanceID].OnOff == true) + { + int64_t OnTime = LightControls[objectInstanceID].OnTime + seconds; + if (Lwm2mCore_SetResourceInstanceValue(context, IPSO_LIGHT_CONTROL_OBJECT, objectInstanceID, + IPSO_LIGHT_CONTROL_ON_TIME, 0, &OnTime, sizeof(OnTime)) == -1) + { + Lwm2m_Error("Failed to increment ON Time resource\n"); + return -1; + } + } + else + return -1; + + return 0; +} diff --git a/lwm2m-client-ipso-light-control.h b/lwm2m-client-ipso-light-control.h new file mode 100644 index 0000000..10b8880 --- /dev/null +++ b/lwm2m-client-ipso-light-control.h @@ -0,0 +1,50 @@ +/** + * @file + * LightWeightM2M LWM2M Light Control object. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef LWM2M_CLIENT_IPSO_LIGHT_CONTROL_H_ +#define LWM2M_CLIENT_IPSO_LIGHT_CONTROL_H_ + +#include "lwm2m_core.h" + + +typedef void (*LightControlCallBack)(void * context, bool OnOff, unsigned char Dimmer, + const char * Colour); +int LightControl_RegisterLightControlObject(Lwm2mContextType * context); +int LightControl_AddLightControl(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID, + LightControlCallBack callback, void * callbackContext); +int LightControl_IncrementOnTime(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID, + int seconds); + +#endif /* LWM2M_CLIENT_IPSO_LIGHT_CONTROL_H_ */ From 76f9ba8e84ef1abaede1f72ac8cc7502c4af0828 Mon Sep 17 00:00:00 2001 From: manohar-narkhede-imgtec Date: Tue, 22 Mar 2016 12:25:40 +0530 Subject: [PATCH 02/14] update Readme.md --- README.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 52c43d7..4a7407f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,19 @@ -# libobjects -collection of lwm2m and custom ipso objects +# Libobjects + +Collection of IPSO and custom LWM2M objects. The list of LWM2M objects provided by OMNA can be found at +LWM2M object registry. + +The following custom objects are defined and used in Creator Kit applications. + +| Object | Object ID | +|---------------------|-----------| +| Flow Object | 20000 | +| Flow Access Object | 20001 | + +### Glossary + +| Name | Description | +|-------------- |----------- | +| LWM2M |LightweightM2M | +| IPSO |IPSO Alliance | +| OMNA |Open Mobile Naming Authority | From 51811b666909e7fd1ae2f16da7cf39ef2bd62221 Mon Sep 17 00:00:00 2001 From: manohar-narkhede-imgtec Date: Tue, 22 Mar 2016 15:08:51 +0530 Subject: [PATCH 03/14] Update Readme.md --- README.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4a7407f..c8517d3 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,16 @@ Collection of IPSO and custom LWM2M objects. The list of LWM2M objects provided by OMNA can be found at LWM2M object registry. -The following custom objects are defined and used in Creator Kit applications. +The following objects are defined and used in Creator Kit applications. -| Object | Object ID | -|---------------------|-----------| -| Flow Object | 20000 | -| Flow Access Object | 20001 | +### List of Objects + +| Object | Object ID | +|-----------------------|-----------| +| Flow Object | 20000 | +| Flow Access Object | 20001 | +| Digital Input Object | 3200 | +| Light Control Object | 3311 | ### Glossary From 5815d542614d8fc71d420dd58af5d74342c60324 Mon Sep 17 00:00:00 2001 From: Pratik Prajapati Date: Thu, 31 Mar 2016 09:44:51 +0530 Subject: [PATCH 04/14] move to static api Signed-off-by: Pratik Prajapati --- Makefile.libobjects | 4 +- lwm2m-client-flow-access-object.c | 417 ++++++++--------- lwm2m-client-flow-access-object.h | 3 +- lwm2m-client-flow-object.c | 747 +++++++++++++----------------- lwm2m-client-flow-object.h | 7 +- 5 files changed, 515 insertions(+), 663 deletions(-) diff --git a/Makefile.libobjects b/Makefile.libobjects index d3a130c..f89654e 100644 --- a/Makefile.libobjects +++ b/Makefile.libobjects @@ -1,2 +1,2 @@ -libobjects_src = lwm2m-client-flow-object.c lwm2m-client-flow-access-object.c \ - lwm2m-client-ipso-digital-input.c lwm2m-client-ipso-light-control.c +libobjects_src = lwm2m-client-flow-object.c lwm2m-client-flow-access-object.c +#lwm2m-client-ipso-digital-input.c lwm2m-client-ipso-light-control.c diff --git a/lwm2m-client-flow-access-object.c b/lwm2m-client-flow-access-object.c index c567772..c051e4a 100644 --- a/lwm2m-client-flow-access-object.c +++ b/lwm2m-client-flow-access-object.c @@ -41,10 +41,11 @@ #include #include #include -#include "lwm2m_core.h" #include "coap_abstraction.h" #include "common.h" +#include + /*************************************************************************************************** * Definitions **************************************************************************************************/ @@ -56,281 +57,229 @@ #define FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN 3 #define FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY 4 -#define REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, name, id, type) \ - REGISTER_RESOURCE(context, name, FLOWM2M_FLOW_ACCESS_OBJECT, id, type, \ - MultipleInstancesEnum_Single, MandatoryEnum_Mandatory, Operations_RW, \ - &flowAccessObjectResourceOperationHandlers) - -#define CREATE_FLOW_ACCESS_OBJECT_OPTIONAL_RESOURCE(context, objectInstanceId, resourcId) \ - CREATE_OPTIONAL_RESOURCE(context, FLOWM2M_FLOW_OBJECT, objectInstanceId, resourcId) +#define FLOW_ACCESS_INSTANCES 1 -/*************************************************************************************************** - * Typedefs - **************************************************************************************************/ typedef struct { - char * URL; - char * CustomerKey; - char * CustomerSecret; - char * RememberMeToken; - int64_t RememberMeTokenExpiry; + char URL[64]; + char CustomerKey[64]; + char CustomerSecret[64]; + char RememberMeToken[64]; + int8_t RememberMeTokenExpiry[16]; } FlowAccessObject; -/*************************************************************************************************** - * Prototypes - **************************************************************************************************/ - -static int FlowAccessObject_ResourceReadHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen); - -static int FlowAccessObject_ResourceGetLengthHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID); - -static int FlowAccessObject_ResourceWriteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, - bool * changed); - -static int FlowAccessObject_ResourceCreateHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); - -static int FlowAccessObject_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID); - -static int FlowAccessObject_ObjectDeleteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); +FlowAccessObject flowAccess[FLOW_ACCESS_INSTANCES]; /*************************************************************************************************** - * Globals - **************************************************************************************************/ - -FlowAccessObject flowAccessObject; - -static ObjectOperationHandlers flowAccessObjectOperationHandlers = -{ - .CreateInstance = FlowAccessObject_ObjectCreateInstanceHandler, - .Delete = FlowAccessObject_ObjectDeleteHandler, -}; - -static ResourceOperationHandlers flowAccessObjectResourceOperationHandlers = -{ - .Read = FlowAccessObject_ResourceReadHandler, - .GetLength = FlowAccessObject_ResourceGetLengthHandler, - .Write = FlowAccessObject_ResourceWriteHandler, - .CreateOptionalResource = FlowAccessObject_ResourceCreateHandler, - .Execute = NULL, -}; - -/*************************************************************************************************** - * Implementation - Private + * Implementation - Public **************************************************************************************************/ -static int FlowAccessObject_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID) +AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID, + AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, uint16_t *dataSize, bool *changed) { - return 0; -} + AwaResult result = AwaResult_InternalError; -static int FlowAccessObject_ResourceCreateHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) -{ - return 0; -} - -static int FlowAccessObject_ObjectDeleteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) -{ - - if (objectID != FLOWM2M_FLOW_ACCESS_OBJECT) - { - Lwm2m_Error("flowAccessObject_ObjectDeleteHandler Invalid OIR: %d/%d/%d\n", objectID, - objectInstanceID, resourceID); - return -1; - } - - if (objectInstanceID == 0) - { - memset(&flowAccessObject, 0, sizeof(FlowAccessObject)); - - if (flowAccessObject.URL) - free(flowAccessObject.URL); - if (flowAccessObject.CustomerKey) - free(flowAccessObject.CustomerKey); - if (flowAccessObject.CustomerSecret) - free(flowAccessObject.CustomerSecret); - if (flowAccessObject.RememberMeToken) - free(flowAccessObject.RememberMeToken); - } - else + if (!((objectID == FLOWM2M_FLOW_ACCESS_OBJECT) && (objectInstanceID >= 0) && (objectInstanceID < FLOW_ACCESS_INSTANCES))) { - Lwm2m_Error("flowAccessObject_ObjectDeleteHandler Invalid instance (not 0): %d", - objectInstanceID); - return -1; - } - - return 0; -} - -static int FlowAccessObject_ResourceReadHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen) -{ - int result = 0; - - switch (resourceID) - { - case FLOWM2M_FLOW_ACCESS_OBJECT_URL: - memcpy(destBuffer, flowAccessObject.URL, strlen(flowAccessObject.URL) + 1); - result = strlen(flowAccessObject.URL) + 1; - break; - case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: - memcpy(destBuffer, flowAccessObject.CustomerKey, - strlen(flowAccessObject.CustomerKey) + 1); - result = strlen(flowAccessObject.CustomerKey) + 1; - break; - case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: - memcpy(destBuffer, flowAccessObject.CustomerSecret, - strlen(flowAccessObject.CustomerSecret) + 1); - result = strlen(flowAccessObject.CustomerSecret) + 1; - break; - case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: - memcpy(destBuffer, flowAccessObject.RememberMeToken, - strlen(flowAccessObject.RememberMeToken) + 1); - result = strlen(flowAccessObject.RememberMeToken) + 1; - break; - case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: - memcpy(destBuffer, &flowAccessObject.RememberMeTokenExpiry, - sizeof(flowAccessObject.RememberMeTokenExpiry)); - result = sizeof(flowAccessObject.RememberMeTokenExpiry); - break; - default: - result = -1; - break; + printf("incorrect flow access object data\n"); + return result; } - return result; -} - -static int FlowAccessObject_ResourceGetLengthHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID) -{ - int result = 0; - - switch (resourceID) + switch (operation) { - case FLOWM2M_FLOW_ACCESS_OBJECT_URL: - if(flowAccessObject.URL != NULL) - result = strlen(flowAccessObject.URL) + 1; - break; - - case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: - if(flowAccessObject.CustomerKey != NULL) - result = strlen(flowAccessObject.CustomerKey) + 1; + case AwaOperation_CreateObjectInstance: + printf("flow access op - create object instance\n\n"); + result = AwaResult_SuccessCreated; + memset(&flowAccess[objectInstanceID], 0, sizeof(flowAccess[objectInstanceID])); break; - case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: - if(flowAccessObject.CustomerSecret != NULL) - result = strlen(flowAccessObject.CustomerSecret) + 1; + case AwaOperation_CreateResource: + printf("flow access op - create resource\n"); + result = AwaResult_SuccessCreated; break; - case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: - if(flowAccessObject.RememberMeToken != NULL) - result = strlen(flowAccessObject.RememberMeToken) + 1; + case AwaOperation_Read: + printf("flow access op - read\n"); + switch (resourceID) + { + case 0: + *dataPointer = flowAccess[objectInstanceID].URL; + *dataSize = strlen(flowAccess[objectInstanceID].URL) + 1; + result = AwaResult_SuccessCreated; + break; + + case 1: + *dataPointer = flowAccess[objectInstanceID].CustomerKey; + *dataSize = strlen(flowAccess[objectInstanceID].CustomerKey) + 1; + result = AwaResult_SuccessCreated; + break; + + case 2: + *dataPointer = flowAccess[objectInstanceID].CustomerSecret; + *dataSize = strlen(flowAccess[objectInstanceID].CustomerSecret) + 1; + result = AwaResult_SuccessCreated; + break; + + case 3: + *dataPointer = flowAccess[objectInstanceID].RememberMeToken; + *dataSize = strlen(flowAccess[objectInstanceID].RememberMeToken) + 1; + result = AwaResult_SuccessCreated; + break; + + case 4: + *dataPointer = flowAccess[objectInstanceID].RememberMeTokenExpiry; + *dataSize = sizeof(flowAccess[objectInstanceID].RememberMeTokenExpiry); + result = AwaResult_SuccessCreated; + break; + + default: + printf("\n invalid res id for flow access - read op"); + break; + } break; - case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: - result = sizeof(flowAccessObject.RememberMeTokenExpiry); - break; + case AwaOperation_Write: + printf("flow acccess op - write\n"); + switch (resourceID) + { + case 0: + strncpy(flowAccess[objectInstanceID].URL, *dataPointer, *dataSize + 1); + *changed = true; + result = AwaResult_SuccessCreated; + break; + + case 1: + strncpy(flowAccess[objectInstanceID].CustomerKey, *dataPointer, *dataSize + 1); + *changed = true; + result = AwaResult_SuccessCreated; + break; + + case 2: + strncpy(flowAccess[objectInstanceID].CustomerSecret, *dataPointer, *dataSize + 1); + *changed = true; + result = AwaResult_SuccessCreated; + break; + + case 3: + strncpy(flowAccess[objectInstanceID].RememberMeToken, *dataPointer, *dataSize + 1); + *changed = true; + result = AwaResult_SuccessCreated; + break; + + case 4: + memcpy(flowAccess[objectInstanceID].RememberMeTokenExpiry, *dataPointer, *dataSize); + *changed = true; + result = AwaResult_SuccessCreated; + break; + + default: + printf("invalid res id for flow access - write op\n"); + break; + } + break; default: - result = -1; + printf("flow access - unknown operation\n"); break; } - return result; } -static int FlowAccessObject_ResourceWriteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, - bool * changed) +int DefineFlowAccessObject(AwaStaticClient *awaClient) { - int result = 0; + AwaError error; - switch(resourceID) + error = AwaStaticClient_DefineObjectWithHandler(awaClient, "FlowAccess", 20001, 0, FLOW_ACCESS_INSTANCES, accessHandler); + if (error != AwaError_Success) { - case FLOWM2M_FLOW_ACCESS_OBJECT_URL: - if (flowAccessObject.URL) - free(flowAccessObject.URL); - flowAccessObject.URL = (char *)malloc(srcBufferLen + 1); - memset(flowAccessObject.URL, 0, srcBufferLen + 1); - memcpy(flowAccessObject.URL, srcBuffer, srcBufferLen); - result = srcBufferLen; - break; + printf("Failed to register flow access object\n"); + return 1; + } - case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: - if (flowAccessObject.CustomerKey) - free(flowAccessObject.CustomerKey); - flowAccessObject.CustomerKey = (char *)malloc(srcBufferLen + 1); - memset(flowAccessObject.CustomerKey, 0, srcBufferLen + 1); - memcpy(flowAccessObject.CustomerKey, srcBuffer, srcBufferLen); - result = srcBufferLen; - break; + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "URL", 20001, 0, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + accessHandler); + if (error != AwaError_Success) + { + printf("Failed to define URL resource\n"); + return 1; + } - case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: - if (flowAccessObject.CustomerSecret) - free(flowAccessObject.CustomerSecret); - flowAccessObject.CustomerSecret = (char *)malloc(srcBufferLen + 1); - memset(flowAccessObject.CustomerSecret, 0, srcBufferLen + 1); - memcpy(flowAccessObject.CustomerSecret, srcBuffer, srcBufferLen); - result = srcBufferLen; - break; + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CustomerKey", 20001, 1, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + accessHandler); + if (error != AwaError_Success) + { + printf("Failed to define CustomerKey resource\n"); + return 1; + } - case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: - if (flowAccessObject.RememberMeToken) - free(flowAccessObject.RememberMeToken); - flowAccessObject.RememberMeToken = (char *)malloc(srcBufferLen + 1); - memset(flowAccessObject.RememberMeToken, 0, srcBufferLen + 1); - memcpy(flowAccessObject.RememberMeToken, srcBuffer, srcBufferLen); - result = srcBufferLen; - break; + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CustomerSecret", 20001, 2, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + accessHandler); + if (error != AwaError_Success) + { + printf("Failed to define CustomerSecret resource\n"); + return 1; + } - case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: - memcpy(&flowAccessObject.RememberMeTokenExpiry, srcBuffer, srcBufferLen); - result = srcBufferLen; - break; + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeToken", 20001, 3, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + accessHandler); + if (error != AwaError_Success) + { + printf("Failed to define RememberMeToken resource\n"); + return 1; + } - default: - result = -1; - break; + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeTokenExpiry", 20001, 4, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, + accessHandler); + if (error != AwaError_Success) + { + printf("Failed to define RememberMeTokenExpiry resource\n"); + return 1; } - return result; + return 0; } -/*************************************************************************************************** - * Implementation - Public - **************************************************************************************************/ - -int Lwm2m_RegisterFlowAccessObject(Lwm2mContextType * context) +int CreateFlowAccessObject(AwaStaticClient *awaClient) { - Lwm2mCore_RegisterObjectType(context, "FlowAccess" , FLOWM2M_FLOW_ACCESS_OBJECT, - MultipleInstancesEnum_Single, MandatoryEnum_Optional, &flowAccessObjectOperationHandlers); - - REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, "URL", FLOWM2M_FLOW_ACCESS_OBJECT_URL, \ - ResourceTypeEnum_TypeString); - REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, "CustomerKey", \ - FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY, ResourceTypeEnum_TypeString); - REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, "CustomerSecret", \ - FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET, ResourceTypeEnum_TypeString); - REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, "RememberMeToken", \ - FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN, ResourceTypeEnum_TypeString); - REGISTER_FLOW_ACCESS_OBJECT_RESOURCE(context, "RememberMeTokenExpiry", \ - FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY, ResourceTypeEnum_TypeOpaque); + AwaError error; + error = AwaStaticClient_CreateObjectInstance(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0); + if (error != AwaError_Success) + { + printf("failed to create flow access object instance\n"); + return 1; + } + + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOWM2M_FLOW_ACCESS_OBJECT_URL); + if (error != AwaError_Success) + { + printf("failed to create URL resource\n"); + return 1; + } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY); + if (error != AwaError_Success) + { + printf("failed to create customer key resource\n"); + return 1; + } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET); + if (error != AwaError_Success) + { + printf("failed to create secret resource\n"); + return 1; + } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN); + if (error != AwaError_Success) + { + printf("failed to create token resource\n"); + return 1; + } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY); + if (error != AwaError_Success) + { + printf("failed to create token expiry resource\n"); + return 1; + } return 0; } diff --git a/lwm2m-client-flow-access-object.h b/lwm2m-client-flow-access-object.h index cf67383..cd9c9fd 100644 --- a/lwm2m-client-flow-access-object.h +++ b/lwm2m-client-flow-access-object.h @@ -36,6 +36,7 @@ #ifndef LWM2M_CLIENT_FLOW_ACCESS_OBJECT_H_ #define LWM2M_CLIENT_FLOW_ACCESS_OBJECT_H_ -int Lwm2m_RegisterFlowAccessObject(Lwm2mContextType * context); +int DefineFlowAccessObject(AwaStaticClient *awaClient); +int CreateFlowAccessObject(AwaStaticClient *awaClient); #endif /* LWM2M_CLIENT_FLOW_ACCESS_OBJECT_H_ */ diff --git a/lwm2m-client-flow-object.c b/lwm2m-client-flow-object.c index 44c97c8..7994f51 100644 --- a/lwm2m-client-flow-object.c +++ b/lwm2m-client-flow-object.c @@ -42,7 +42,7 @@ #include #include #include -#include "lwm2m_core.h" +#include #include "coap_abstraction.h" #include "hmac.h" #include "b64.h" @@ -68,511 +68,412 @@ #define MAX_STRING_SIZE 64 #define MAX_KEY_SIZE 64 -#define REGISTER_FLOW_OBJECT_RESOURCE(context, name, id, type, minInstances) \ - REGISTER_RESOURCE(context, name, FLOWM2M_FLOW_OBJECT, id, type, \ - MultipleInstancesEnum_Single, minInstances, Operations_RW, \ - &flowObjectResourceOperationHandlers) -#define CREATE_FLOW_OBJECT_OPTIONAL_RESOURCE(context, objectInstanceId, resourcId) \ - CREATE_OPTIONAL_RESOURCE(context, FLOWM2M_FLOW_OBJECT, objectInstanceId, resourcId) - -/*************************************************************************************************** - * Typedefs - **************************************************************************************************/ - -typedef struct -{ - void * DeviceID; - int DeviceIDSize; - void * ParentID; - int64_t ParentIDSize; - char * DeviceType; - char * Name; - char * Description; - char * FCAP; - int64_t LicenseeID; - void * LicenseeChallenge; - int64_t LicenseeChallengeSize; - int64_t HashIterations; - void * LicenseeHash; - int64_t LicenseeHashSize; - int64_t Status; -} FlowObject; - -/*************************************************************************************************** - * Prototypes - **************************************************************************************************/ - -static int FlowObject_ResourceReadHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen); - -static int FlowObject_ResourceGetLengthHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID); - -static int FlowObject_ResourceWriteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, - bool * changed); - -static int FlowObject_ResourceCreateHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); - -static int FlowObject_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID); - -static int FlowObject_ObjectDeleteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); /*************************************************************************************************** * Globals **************************************************************************************************/ -static char licenseeSecret[MAX_STRING_SIZE] = "getATTtDsNBpBRnMsN7GoQ=="; -FlowObject flowObject; +#define FLOW_INSTANCES 1 +static char licenseeSecret[64] = "getATTtDsNBpBRnMsN7GoQ=="; -static ObjectOperationHandlers flowObjectOperationHandlers = -{ - .CreateInstance = FlowObject_ObjectCreateInstanceHandler, - .Delete = FlowObject_ObjectDeleteHandler, -}; - -static ResourceOperationHandlers flowObjectResourceOperationHandlers = -{ - .Read = FlowObject_ResourceReadHandler, - .GetLength = FlowObject_ResourceGetLengthHandler, - .Write = FlowObject_ResourceWriteHandler, - .CreateOptionalResource = FlowObject_ResourceCreateHandler, - .Execute = NULL, -}; /*************************************************************************************************** - * Implementation - Private + * Implementation - Public **************************************************************************************************/ - -static int FlowObject_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID) +typedef struct { - return 0; -} + int8_t DeviceID[16]; + int8_t ParentID[16]; + char DeviceType[64]; + char Name[64]; + char Description[64]; + char FCAP[64]; + int8_t LicenseeID; + char LicenseeChallenge[64]; + int8_t HashIterations; + uint8_t LicenseeHash[SHA256_HASH_LENGTH]; + int8_t Status; +} FlowObject; -static int FlowObject_ResourceCreateHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) -{ - return 0; -} +static FlowObject flow[FLOW_INSTANCES]; -static int FlowObject_ObjectDeleteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) +static bool CalculateLicenseeHash(char *licenseeSecret, uint8_t hash[SHA256_HASH_LENGTH], const char *challenge, int challengeLength, int iterations) { + int i; + uint8_t key[MAX_KEY_SIZE]; + int keyLen = b64Decode(key, sizeof(key), licenseeSecret, strlen(licenseeSecret)); - if (objectID != FLOWM2M_FLOW_OBJECT) + if (keyLen == -1) { - Lwm2m_Error("FlowObject_ObjectDeleteHandler Invalid OIR: %d/%d/%d\n", objectID, - objectInstanceID, resourceID); - return -1; + return false; } - if (objectInstanceID == 0) - { - memset(&flowObject, 0, sizeof(FlowObject)); - - if(flowObject.DeviceID) - free(flowObject.DeviceID); - if(flowObject.ParentID) - free(flowObject.ParentID); - if(flowObject.DeviceType) - free(flowObject.DeviceType); - if(flowObject.Name) - free(flowObject.Name); - if(flowObject.Description) - free(flowObject.Description); - if(flowObject.FCAP) - free(flowObject.FCAP); - if(flowObject.LicenseeChallenge) - free(flowObject.LicenseeChallenge); - if(flowObject.LicenseeHash) - free(flowObject.LicenseeHash); - } - else + HmacSha256_ComputeHash(hash, challenge, challengeLength, key, keyLen); + for (i = 1; i < iterations; i++) { - Lwm2m_Error("FlowObject_ObjectDeleteHandler Invalid instance (not 0): %d", objectInstanceID); - return -1; + HmacSha256_ComputeHash(hash, hash, SHA256_HASH_LENGTH, key, keyLen); } - - return 0; + return true; } -static int FlowObject_ResourceReadHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen) +AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID, + AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, uint16_t *dataSize, bool *changed) { - int result = 0; + AwaResult result = AwaResult_InternalError; - switch (resourceID) + if (!((objectID == FLOWM2M_FLOW_OBJECT) && (objectInstanceID >= 0) && (objectInstanceID < FLOW_INSTANCES))) { - case FLOWM2M_FLOW_OBJECT_DEVICEID: - if(flowObject.DeviceID != NULL) - { - memcpy(destBuffer, flowObject.DeviceID, flowObject.DeviceIDSize); - result = flowObject.DeviceIDSize; - } - break; - - case FLOWM2M_FLOW_OBJECT_PARENTID: - if(flowObject.ParentID != NULL) - { - memcpy(destBuffer, flowObject.ParentID, flowObject.ParentIDSize); - result = flowObject.ParentIDSize; - } - break; - - case FLOWM2M_FLOW_OBJECT_DEVICETYPE: - if(flowObject.DeviceType != NULL) - { - memcpy(destBuffer, flowObject.DeviceType, strlen(flowObject.DeviceType) + 1); - result = strlen(flowObject.DeviceType); - } - break; - - case FLOWM2M_FLOW_OBJECT_NAME: - if(flowObject.Name != NULL) - { - memcpy(destBuffer, flowObject.Name, strlen(flowObject.Name) + 1); - result = strlen(flowObject.Name) + 1; - } - break; - - case FLOWM2M_FLOW_OBJECT_DESCRIPTION: - if(flowObject.Description != NULL) - { - memcpy(destBuffer, flowObject.Description, strlen(flowObject.Description) + 1); - result = strlen(flowObject.Description) + 1; - } - break; + printf("incorrect flow object data\n"); + return result; + } - case FLOWM2M_FLOW_OBJECT_FCAP: - if(flowObject.FCAP != NULL) - { - memcpy(destBuffer, flowObject.FCAP, strlen(flowObject.FCAP) + 1); - result = strlen(flowObject.FCAP) + 1; - } + switch (operation) + { + case AwaOperation_CreateObjectInstance: + printf("flow op - create object instance\n\n"); + result = AwaResult_SuccessCreated; + memset(&flow[objectInstanceID], 0, sizeof(flow[objectInstanceID])); break; - case FLOWM2M_FLOW_OBJECT_LICENSEEID: - memcpy(destBuffer, &flowObject.LicenseeID, sizeof(flowObject.LicenseeID)); - result = sizeof(flowObject.LicenseeID); + case AwaOperation_CreateResource: + printf("flow op - create resource\n"); + result = AwaResult_SuccessCreated; break; - case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: - if(flowObject.LicenseeChallenge != NULL) + case AwaOperation_Read: + printf("flow op - read\n"); + switch (resourceID) { - memcpy(destBuffer, flowObject.LicenseeChallenge, flowObject.LicenseeChallengeSize); - result = flowObject.LicenseeChallengeSize; + case 0: + *dataPointer = flow[objectInstanceID].DeviceID; + *dataSize = sizeof(flow[objectInstanceID].DeviceID); + break; + + case 1: + *dataPointer = flow[objectInstanceID].ParentID; + *dataSize = sizeof(flow[objectInstanceID].ParentID); + break; + + case 2: + *dataPointer = flow[objectInstanceID].DeviceType; + *dataSize = strlen(flow[objectInstanceID].DeviceType) + 1; + break; + + case 3: + *dataPointer = flow[objectInstanceID].Name; + *dataSize = strlen(flow[objectInstanceID].Name) + 1; + break; + + case 4: + *dataPointer = flow[objectInstanceID].Description; + *dataSize = strlen(flow[objectInstanceID].Description) + 1; + break; + + case 5: + *dataPointer = flow[objectInstanceID].FCAP; + *dataSize = strlen(flow[objectInstanceID].FCAP) + 1; + break; + + case 6: + *dataPointer = &flow[objectInstanceID].LicenseeID; + *dataSize = sizeof(flow[objectInstanceID].LicenseeID); + break; + + case 7: + *dataPointer = flow[objectInstanceID].LicenseeChallenge; + *dataSize = sizeof(flow[objectInstanceID].LicenseeChallenge); + break; + + case 8: + *dataPointer = &flow[objectInstanceID].HashIterations; + *dataSize = sizeof(flow[objectInstanceID].HashIterations); + break; + + case 9: + *dataPointer = flow[objectInstanceID].LicenseeHash; + *dataSize = sizeof(flow[objectInstanceID].LicenseeHash); + break; + + case 10: + *dataPointer = &flow[objectInstanceID].Status; + *dataSize = sizeof(flow[objectInstanceID].Status); + break; + + default: + printf("Invalid resource id\n"); } + result = AwaResult_SuccessContent; break; - case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: - memcpy(destBuffer, &flowObject.HashIterations, sizeof(flowObject.HashIterations)); - result = sizeof(flowObject.HashIterations); - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: - if(flowObject.LicenseeHash != NULL) + case AwaOperation_Write: + printf("flow op - write\n"); + switch (resourceID) { - memcpy(destBuffer, flowObject.LicenseeHash, flowObject.LicenseeHashSize); - result = flowObject.LicenseeHashSize; + case 0: + memcpy(flow[objectInstanceID].DeviceID, *dataPointer, *dataSize); + *changed = true; + break; + + case 1: + memcpy(flow[objectInstanceID].ParentID, *dataPointer, *dataSize); + *changed = true; + break; + + case 2: + strncpy(flow[objectInstanceID].DeviceType, *dataPointer, *dataSize + 1); + *changed = true; + break; + + case 3: + memcpy(flow[objectInstanceID].Name, *dataPointer, *dataSize); + *changed = true; + break; + + case 4: + strncpy(flow[objectInstanceID].Description, *dataPointer, *dataSize + 1); + *changed = true; + break; + + case 5: + strncpy(flow[objectInstanceID].FCAP, *dataPointer, *dataSize + 1); + *changed = true; + break; + + case 6: + flow[objectInstanceID].LicenseeID = *dataPointer; + *changed = true; + break; + + case 7: + memcpy(flow[objectInstanceID].LicenseeChallenge, *dataPointer, *dataSize); + *changed = true; + break; + + case 8: + flow[objectInstanceID].HashIterations = *dataPointer; + *changed = true; + break; + + case 9: + memcpy(flow[objectInstanceID].LicenseeHash, *dataPointer, *dataSize); + *changed = true; + break; + + case 10: + flow[objectInstanceID].Status = *dataPointer; + *changed = true; + break; + + default: + printf("invalid resource id for write operation\n"); + break; } - break; - - case FLOWM2M_FLOW_OBJECT_STATUS: - memcpy(destBuffer, &flowObject.Status, sizeof(flowObject.Status)); - result = sizeof(flowObject.Status); + result = AwaResult_SuccessContent; break; default: - result = -1; + printf("default case for handler, operation - %d\n", operation); break; } - return result; -} - -static int FlowObject_ResourceGetLengthHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID) -{ - int result = 0; - - switch (resourceID) + if(flow[objectInstanceID].HashIterations > 0 && flow[objectInstanceID].LicenseeChallenge != NULL + && (resourceID == FLOWM2M_FLOW_OBJECT_HASHITERATIONS || (resourceID == FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE))) { - case FLOWM2M_FLOW_OBJECT_DEVICEID: - result = flowObject.DeviceIDSize; - break; - - case FLOWM2M_FLOW_OBJECT_PARENTID: - result = flowObject.ParentIDSize; - break; - - case FLOWM2M_FLOW_OBJECT_DEVICETYPE: - if(flowObject.DeviceType != NULL) - result = strlen(flowObject.DeviceType) + 1; - break; - - case FLOWM2M_FLOW_OBJECT_NAME: - if(flowObject.Name != NULL) - result = strlen(flowObject.Name) + 1; - break; - - case FLOWM2M_FLOW_OBJECT_DESCRIPTION: - if(flowObject.Description != NULL) - result = strlen(flowObject.Description) + 1; - break; - - case FLOWM2M_FLOW_OBJECT_FCAP: - if(flowObject.FCAP != NULL) - result = strlen(flowObject.FCAP) + 1; - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEEID: - result = sizeof(flowObject.LicenseeID); - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: - result = flowObject.LicenseeChallengeSize; - break; - - case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: - result = sizeof(flowObject.HashIterations); - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: - result = flowObject.LicenseeHashSize; - break; + uint8_t licenseeHash[SHA256_HASH_LENGTH]; - case FLOWM2M_FLOW_OBJECT_STATUS: - result = sizeof(flowObject.Status); - break; + printf("Calculating licensee hash with %d iterations...\n", flow[objectInstanceID].HashIterations); - default: - result = -1; - break; + if (CalculateLicenseeHash(licenseeSecret, licenseeHash, flow[objectInstanceID].LicenseeChallenge, 64, + flow[objectInstanceID].HashIterations)) + { + printf("Calculated hash, writing Licensee Hash resource...\n"); + memcpy(flow[objectInstanceID].LicenseeHash, licenseeHash, SHA256_HASH_LENGTH); + result = AwaStaticClient_ResourceChanged(client, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_LICENSEEHASH); + } + else + { + printf("Licensee secret is invalid\n"); + return AwaResult_InternalError; + } } return result; } -static bool CalculateLicenseeHash(char * licenseeSecret, uint8_t hash[SHA256_HASH_LENGTH], - const char * challenge, int challengeLength, int iterations) +int DefineFlowObject(AwaStaticClient *awaClient) { - int i; - uint8_t key[MAX_KEY_SIZE]; - int keyLen = b64Decode(key, sizeof(key), licenseeSecret, strlen(licenseeSecret)); + AwaError error; - if (keyLen == -1) + error = AwaStaticClient_DefineObjectWithHandler(awaClient, "Flow", 20000, 0, FLOW_INSTANCES, handler); + if (error != AwaError_Success) { - return false; + printf("Failed to register flow bject\n"); + return 1; } - HmacSha256_ComputeHash(hash, challenge, challengeLength, key, keyLen); - for (i = 1; i < iterations; i++) + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceID", 20000, 0, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) { - HmacSha256_ComputeHash(hash, hash, SHA256_HASH_LENGTH, key, keyLen); + printf("Failed to define deviceID resource\n"); + return 1; } - return true; -} -static int FlowObject_ResourceWriteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, bool * changed) -{ - int result; - - switch(resourceID) + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "ParentID", 20000, 1, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) { - case FLOWM2M_FLOW_OBJECT_DEVICEID: - if(flowObject.DeviceID) - free(flowObject.DeviceID); - flowObject.DeviceID = malloc(srcBufferLen); - memcpy(flowObject.DeviceID, srcBuffer, srcBufferLen); - result = flowObject.DeviceIDSize = srcBufferLen; - break; - - case FLOWM2M_FLOW_OBJECT_PARENTID: - if(flowObject.ParentID) - free(flowObject.ParentID); - flowObject.ParentID = malloc(srcBufferLen); - memcpy(flowObject.ParentID, srcBuffer, srcBufferLen); - result = flowObject.ParentIDSize = srcBufferLen; - break; - - case FLOWM2M_FLOW_OBJECT_DEVICETYPE: - if(flowObject.DeviceType) - free(flowObject.DeviceType); - flowObject.DeviceType = malloc(srcBufferLen + 1); - memset(flowObject.DeviceType, 0, srcBufferLen + 1); - memcpy(flowObject.DeviceType, srcBuffer, srcBufferLen); - Lwm2m_Debug("Device type: %s\n", flowObject.DeviceType); - result = srcBufferLen; - break; - - case FLOWM2M_FLOW_OBJECT_NAME: - if(flowObject.Name) - free(flowObject.Name); - flowObject.Name = malloc(srcBufferLen + 1); - memset(flowObject.Name, 0, srcBufferLen + 1); - memcpy(flowObject.Name, srcBuffer, srcBufferLen); - result = srcBufferLen; - break; - - case FLOWM2M_FLOW_OBJECT_DESCRIPTION: - if(flowObject.Description) - free(flowObject.Description); - flowObject.Description = malloc(srcBufferLen + 1); - memset(flowObject.Description, 0, srcBufferLen + 1); - memcpy(flowObject.Description, srcBuffer, srcBufferLen); - result = srcBufferLen; - break; - - case FLOWM2M_FLOW_OBJECT_FCAP: - if(flowObject.FCAP) - free(flowObject.FCAP); - flowObject.FCAP = malloc(srcBufferLen + 1); - memset(flowObject.FCAP, 0, srcBufferLen + 1); - memcpy(flowObject.FCAP, srcBuffer, srcBufferLen); - Lwm2m_Error("FCAP: %s\n", flowObject.FCAP); - result = srcBufferLen; - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEEID: - memcpy(&flowObject.LicenseeID, srcBuffer, srcBufferLen); - result = srcBufferLen; - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: - if(flowObject.LicenseeChallenge) - free(flowObject.LicenseeChallenge); - flowObject.LicenseeChallenge = malloc(srcBufferLen); - memcpy(flowObject.LicenseeChallenge, srcBuffer, srcBufferLen); - result = flowObject.LicenseeChallengeSize = srcBufferLen; - break; - - case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: - memcpy(&flowObject.HashIterations, srcBuffer, srcBufferLen); - result = srcBufferLen; - break; + printf("Failed to define parentID resource\n"); + return 1; + } - case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: - if(flowObject.LicenseeHash) - free(flowObject.LicenseeHash); - flowObject.LicenseeHash = malloc(srcBufferLen); - memcpy(flowObject.LicenseeHash, srcBuffer, srcBufferLen); - result = flowObject.LicenseeHashSize = srcBufferLen; - *changed = true; - break; + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceType", 20000, 2, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define device type resource\n"); + return 1; + } - case FLOWM2M_FLOW_OBJECT_STATUS: - memcpy(&flowObject.Status, srcBuffer, srcBufferLen); - Lwm2m_Debug("Status: %d\n", (int)flowObject.Status); - result = srcBufferLen; - break; + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Name", 20000, 3, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define name resource\n"); + return 1; + } - default: - result = -1; - break; + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Description", 20000, 4, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define description resource\n"); + return 1; } - if(flowObject.HashIterations > 0 && - flowObject.LicenseeChallengeSize > 0 && - flowObject.LicenseeChallenge != NULL && - (resourceID == FLOWM2M_FLOW_OBJECT_HASHITERATIONS || - (resourceID == FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE))) + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "FCAP", 20000, 5, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) { - uint8_t licenseeHash[SHA256_HASH_LENGTH]; + printf("Failed to define FCAP resource\n"); + return 1; + } - Lwm2m_Debug("Calculating licensee hash with %d iterations...\n", - (int)flowObject.HashIterations); + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeID", 20000, 6, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define LicenseeID resource\n"); + return 1; + } - if (CalculateLicenseeHash(licenseeSecret, licenseeHash, flowObject.LicenseeChallenge, - flowObject.LicenseeChallengeSize, flowObject.HashIterations)) - { - Lwm2m_Debug("Calculated hash, writing Licensee Hash resource...\n"); - if (Lwm2mCore_SetResourceInstanceValue(context, FLOWM2M_FLOW_OBJECT, 0, - FLOWM2M_FLOW_OBJECT_LICENSEEHASH, 0, licenseeHash, sizeof(licenseeHash)) == -1) - { - Lwm2m_Error("Failed to set Licensee Hash\n"); - return -1; - } - } - else - { - Lwm2m_Error("Licensee secret is invalid\n"); - return -1; - } + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeChallenge", 20000, 7, AwaResourceType_String, 0, 1, AwaResourceOperations_WriteOnly, + handler); + if (error != AwaError_Success) + { + printf("Failed to define LicenseeChallenge resource\n"); + return 1; } - return result; -} + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "HashIterations", 20000, 8, AwaResourceType_String, 0, 1, AwaResourceOperations_WriteOnly, + handler); + if (error != AwaError_Success) + { + printf("Failed to define HashIterations resource\n"); + return 1; + } -/*************************************************************************************************** - * Implementation - Public - **************************************************************************************************/ + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeHash", 20000, 9, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadOnly, + handler); + if (error != AwaError_Success) + { + printf("Failed to define LicenseeHash resource\n"); + return 1; + } -int Lwm2m_RegisterFlowObject(Lwm2mContextType * context) -{ - REGISTER_OBJECT(context, "FlowObject", FLOWM2M_FLOW_OBJECT, MultipleInstancesEnum_Single, \ - MandatoryEnum_Optional, &flowObjectOperationHandlers); - REGISTER_FLOW_OBJECT_RESOURCE(context, "DeviceID", FLOWM2M_FLOW_OBJECT_DEVICEID, \ - ResourceTypeEnum_TypeOpaque, MandatoryEnum_Mandatory); - REGISTER_FLOW_OBJECT_RESOURCE(context, "ParentID", FLOWM2M_FLOW_OBJECT_PARENTID, \ - ResourceTypeEnum_TypeOpaque, MandatoryEnum_Optional); - REGISTER_FLOW_OBJECT_RESOURCE(context, "DeviceType", FLOWM2M_FLOW_OBJECT_DEVICETYPE, \ - ResourceTypeEnum_TypeString, MandatoryEnum_Mandatory); - REGISTER_FLOW_OBJECT_RESOURCE(context, "Name", FLOWM2M_FLOW_OBJECT_NAME, \ - ResourceTypeEnum_TypeString, MandatoryEnum_Optional); - REGISTER_FLOW_OBJECT_RESOURCE(context, "Description", FLOWM2M_FLOW_OBJECT_DESCRIPTION, \ - ResourceTypeEnum_TypeString, MandatoryEnum_Optional); - REGISTER_FLOW_OBJECT_RESOURCE(context, "FCAP", FLOWM2M_FLOW_OBJECT_FCAP, \ - ResourceTypeEnum_TypeString, MandatoryEnum_Mandatory); - REGISTER_FLOW_OBJECT_RESOURCE(context, "LicenseeID", FLOWM2M_FLOW_OBJECT_LICENSEEID, \ - ResourceTypeEnum_TypeInteger, MandatoryEnum_Mandatory); - REGISTER_FLOW_OBJECT_RESOURCE(context, "LicenseeChallenge", FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE, \ - ResourceTypeEnum_TypeOpaque, MandatoryEnum_Optional); - REGISTER_FLOW_OBJECT_RESOURCE(context, "HashIterations", FLOWM2M_FLOW_OBJECT_HASHITERATIONS, \ - ResourceTypeEnum_TypeInteger, MandatoryEnum_Optional); - REGISTER_FLOW_OBJECT_RESOURCE(context, "LicenseeHash", FLOWM2M_FLOW_OBJECT_LICENSEEHASH, \ - ResourceTypeEnum_TypeOpaque, MandatoryEnum_Optional); - REGISTER_FLOW_OBJECT_RESOURCE(context, "Status", FLOWM2M_FLOW_OBJECT_STATUS, \ - ResourceTypeEnum_TypeInteger, MandatoryEnum_Optional); + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Status", 20000, 10, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadOnly, + handler); + if (error != AwaError_Success) + { + printf("Failed to define Status resource\n"); + return 1; + } return 0; } -int Lwm2m_SetProvisioningInfo(Lwm2mContextType * context, const char * DeviceType, - const char * FCAP, int64_t LicenseeID) +int CreateFlowObject(AwaStaticClient *awaClient) { - CREATE_OBJECT_INSTANCE(context, FLOWM2M_FLOW_OBJECT, 0); + AwaError error; - if (Lwm2mCore_SetResourceInstanceValue(context, FLOWM2M_FLOW_OBJECT, 0, - FLOWM2M_FLOW_OBJECT_FCAP, 0, FCAP, strlen(FCAP)) == -1) + error = AwaStaticClient_CreateObjectInstance(awaClient, FLOWM2M_FLOW_OBJECT, 0); + if (error != AwaError_Success) { - Lwm2m_Error("Failed to set FCAP to %s\n", FCAP); - return -1; + printf("failed to create flow object instance\n"); + return 1; } - if (Lwm2mCore_SetResourceInstanceValue(context, FLOWM2M_FLOW_OBJECT, 0, - FLOWM2M_FLOW_OBJECT_DEVICETYPE, 0, DeviceType, strlen(DeviceType)) == -1) + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_DEVICEID); + if (error != AwaError_Success) { - Lwm2m_Error("Failed to set Device Type to %s\n", DeviceType); - return -1; + printf("failed to create device id resource\n"); + return 1; } - - if (Lwm2mCore_SetResourceInstanceValue(context, FLOWM2M_FLOW_OBJECT, 0, - FLOWM2M_FLOW_OBJECT_LICENSEEID, 0, &LicenseeID, sizeof(int64_t)) == -1) + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_PARENTID); + if (error != AwaError_Success) + { + printf("failed to create parent id resource\n"); + return 1; + } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_DEVICETYPE); + if (error != AwaError_Success) + { + printf("failed to create device type resource\n"); + return 1; + } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_NAME); + if (error != AwaError_Success) + { + printf("failed to create name resource\n"); + return 1; + } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_DESCRIPTION); + if (error != AwaError_Success) { - Lwm2m_Error("Failed to set Licensee Id to %" PRId64 "\n", LicenseeID); - return -1; + printf("failed to create description resource\n"); + return 1; } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_FCAP); + if (error != AwaError_Success) + { + printf("failed to create FCAP resource\n"); + return 1; + } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_LICENSEEID); + if (error != AwaError_Success) + { + printf("failed to create licensee ID resource\n"); + return 1; + } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE); + if (error != AwaError_Success) + { + printf("failed to create licensee challenge resource\n"); + return 1; + } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_LICENSEEHASH); + if (error != AwaError_Success) + { + printf("failed to create licensee hash resource\n"); + return 1; + } + error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_STATUS); + if (error != AwaError_Success) + { + printf("failed to create status resource\n"); + return 1; + } + return 0; } diff --git a/lwm2m-client-flow-object.h b/lwm2m-client-flow-object.h index 2d6b024..3d6850f 100644 --- a/lwm2m-client-flow-object.h +++ b/lwm2m-client-flow-object.h @@ -35,9 +35,10 @@ #ifndef LWM2M_CLIENT_FLOW_OBJECT_H_ #define LWM2M_CLIENT_FLOW_OBJECT_H_ +#include + +int DefineFlowObject(AwaStaticClient *awaClient); +int CreateFlowObject(AwaStaticClient *awaClient); -int Lwm2m_RegisterFlowObject(Lwm2mContextType * context); -int Lwm2m_SetProvisioningInfo(Lwm2mContextType * context, const char * DeviceType, - const char * FCAP, int64_t LicenseeID); #endif /* LWM2M_CLIENT_FLOW_OBJECT_H_ */ From 6548a3ab3f243b408bc09ac05d225ae0def76559 Mon Sep 17 00:00:00 2001 From: Pratik Prajapati Date: Sat, 2 Apr 2016 20:34:56 +0530 Subject: [PATCH 05/14] update copy operation Signed-off-by: Pratik Prajapati --- lwm2m-client-flow-access-object.c | 16 ++++++++-------- lwm2m-client-flow-object.c | 23 ++++++++++++----------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/lwm2m-client-flow-access-object.c b/lwm2m-client-flow-access-object.c index c051e4a..f34b6a6 100644 --- a/lwm2m-client-flow-access-object.c +++ b/lwm2m-client-flow-access-object.c @@ -95,12 +95,12 @@ AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObje break; case AwaOperation_CreateResource: - printf("flow access op - create resource\n"); + printf("flow access op - create resource %d\n", resourceID); result = AwaResult_SuccessCreated; break; case AwaOperation_Read: - printf("flow access op - read\n"); + printf("flow access op - read for res %d\n", resourceID); switch (resourceID) { case 0: @@ -140,29 +140,29 @@ AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObje break; case AwaOperation_Write: - printf("flow acccess op - write\n"); + printf("flow acccess op - write for res %d\n", resourcId); switch (resourceID) { case 0: - strncpy(flowAccess[objectInstanceID].URL, *dataPointer, *dataSize + 1); + snprintf(flowAccess[objectInstanceID].URL, *dataSize, "%s", (char *)*dataPointer); *changed = true; result = AwaResult_SuccessCreated; break; case 1: - strncpy(flowAccess[objectInstanceID].CustomerKey, *dataPointer, *dataSize + 1); + snprintf(flowAccess[objectInstanceID].CustomerKey, *dataSize, "%s", (char *)*dataPointer); *changed = true; result = AwaResult_SuccessCreated; break; case 2: - strncpy(flowAccess[objectInstanceID].CustomerSecret, *dataPointer, *dataSize + 1); + snprintf(flowAccess[objectInstanceID].CustomerSecret, *dataSize, "%s", (char *)*dataPointer); *changed = true; result = AwaResult_SuccessCreated; break; case 3: - strncpy(flowAccess[objectInstanceID].RememberMeToken, *dataPointer, *dataSize + 1); + snprintf(flowAccess[objectInstanceID].RememberMeToken, *dataSize, "%s", (char *)*dataPointer); *changed = true; result = AwaResult_SuccessCreated; break; @@ -174,7 +174,7 @@ AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObje break; default: - printf("invalid res id for flow access - write op\n"); + printf("invalid res id %d for flow access - write op\n", resourceID); break; } break; diff --git a/lwm2m-client-flow-object.c b/lwm2m-client-flow-object.c index 7994f51..ebf0cc2 100644 --- a/lwm2m-client-flow-object.c +++ b/lwm2m-client-flow-object.c @@ -137,12 +137,12 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o break; case AwaOperation_CreateResource: - printf("flow op - create resource\n"); + printf("flow op - create resource %d\n", resourceID); result = AwaResult_SuccessCreated; break; case AwaOperation_Read: - printf("flow op - read\n"); + printf("flow op - read for res %d\n", resourceID); switch (resourceID) { case 0: @@ -207,7 +207,7 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o break; case AwaOperation_Write: - printf("flow op - write\n"); + printf("flow op - write for res %d\n", resourceID); switch (resourceID) { case 0: @@ -221,22 +221,23 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o break; case 2: - strncpy(flow[objectInstanceID].DeviceType, *dataPointer, *dataSize + 1); + snprintf(flow[objectInstanceID].DeviceType, *dataSize, "%s", (char *)*dataPointer); *changed = true; break; case 3: - memcpy(flow[objectInstanceID].Name, *dataPointer, *dataSize); + snprintf(flow[objectInstanceID].Name, *dataSize, "%s", (char *)*dataPointer); *changed = true; break; case 4: - strncpy(flow[objectInstanceID].Description, *dataPointer, *dataSize + 1); + snprintf(flow[objectInstanceID].Description, *dataSize, "%s", (char *)*dataPointer); *changed = true; break; case 5: - strncpy(flow[objectInstanceID].FCAP, *dataPointer, *dataSize + 1); + snprintf(flow[objectInstanceID].FCAP, *dataSize, "%s", (char *)*dataPointer); + printf("FCAP = %s\n", flow[objectInstanceID].FCAP); *changed = true; break; @@ -368,7 +369,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeChallenge", 20000, 7, AwaResourceType_String, 0, 1, AwaResourceOperations_WriteOnly, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeChallenge", 20000, 7, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -376,7 +377,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "HashIterations", 20000, 8, AwaResourceType_String, 0, 1, AwaResourceOperations_WriteOnly, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "HashIterations", 20000, 8, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -384,7 +385,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeHash", 20000, 9, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadOnly, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeHash", 20000, 9, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -392,7 +393,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Status", 20000, 10, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadOnly, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Status", 20000, 10, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { From fc405037ae2472fb56d6d8485dcf331c3645820c Mon Sep 17 00:00:00 2001 From: Sean Kelly Date: Tue, 5 Apr 2016 12:26:48 +1200 Subject: [PATCH 06/14] Fix definitions and handlers for flow object and flow access object --- lwm2m-client-flow-access-object.c | 76 ++++------------ lwm2m-client-flow-access-object.h | 1 - lwm2m-client-flow-object.c | 145 +++++++++--------------------- lwm2m-client-flow-object.h | 2 - 4 files changed, 58 insertions(+), 166 deletions(-) diff --git a/lwm2m-client-flow-access-object.c b/lwm2m-client-flow-access-object.c index f34b6a6..09df94c 100644 --- a/lwm2m-client-flow-access-object.c +++ b/lwm2m-client-flow-access-object.c @@ -66,7 +66,7 @@ typedef struct char CustomerKey[64]; char CustomerSecret[64]; char RememberMeToken[64]; - int8_t RememberMeTokenExpiry[16]; + AwaTime RememberMeTokenExpiry; } FlowAccessObject; FlowAccessObject flowAccess[FLOW_ACCESS_INSTANCES]; @@ -76,7 +76,7 @@ FlowAccessObject flowAccess[FLOW_ACCESS_INSTANCES]; **************************************************************************************************/ AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID, - AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, uint16_t *dataSize, bool *changed) + AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, size_t *dataSize, bool *changed) { AwaResult result = AwaResult_InternalError; @@ -105,30 +105,30 @@ AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObje { case 0: *dataPointer = flowAccess[objectInstanceID].URL; - *dataSize = strlen(flowAccess[objectInstanceID].URL) + 1; + *dataSize = strlen(flowAccess[objectInstanceID].URL) ; result = AwaResult_SuccessCreated; break; case 1: *dataPointer = flowAccess[objectInstanceID].CustomerKey; - *dataSize = strlen(flowAccess[objectInstanceID].CustomerKey) + 1; + *dataSize = strlen(flowAccess[objectInstanceID].CustomerKey); result = AwaResult_SuccessCreated; break; case 2: *dataPointer = flowAccess[objectInstanceID].CustomerSecret; - *dataSize = strlen(flowAccess[objectInstanceID].CustomerSecret) + 1; + *dataSize = strlen(flowAccess[objectInstanceID].CustomerSecret); result = AwaResult_SuccessCreated; break; case 3: *dataPointer = flowAccess[objectInstanceID].RememberMeToken; - *dataSize = strlen(flowAccess[objectInstanceID].RememberMeToken) + 1; + *dataSize = strlen(flowAccess[objectInstanceID].RememberMeToken); result = AwaResult_SuccessCreated; break; case 4: - *dataPointer = flowAccess[objectInstanceID].RememberMeTokenExpiry; + *dataPointer = &flowAccess[objectInstanceID].RememberMeTokenExpiry; *dataSize = sizeof(flowAccess[objectInstanceID].RememberMeTokenExpiry); result = AwaResult_SuccessCreated; break; @@ -140,35 +140,39 @@ AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObje break; case AwaOperation_Write: - printf("flow acccess op - write for res %d\n", resourcId); + printf("flow acccess op - write for res %d\n", resourceID); switch (resourceID) { case 0: - snprintf(flowAccess[objectInstanceID].URL, *dataSize, "%s", (char *)*dataPointer); + memcpy(flowAccess[objectInstanceID].URL, *dataPointer, *dataSize); + flowAccess[objectInstanceID].URL[*dataSize] = '\0'; *changed = true; result = AwaResult_SuccessCreated; break; case 1: - snprintf(flowAccess[objectInstanceID].CustomerKey, *dataSize, "%s", (char *)*dataPointer); + memcpy(flowAccess[objectInstanceID].CustomerKey,*dataPointer, *dataSize); + flowAccess[objectInstanceID].CustomerKey[*dataSize] = '\0'; *changed = true; result = AwaResult_SuccessCreated; break; case 2: - snprintf(flowAccess[objectInstanceID].CustomerSecret, *dataSize, "%s", (char *)*dataPointer); + memcpy(flowAccess[objectInstanceID].CustomerSecret, *dataPointer, *dataSize); + flowAccess[objectInstanceID].CustomerSecret[*dataSize] = '\0'; *changed = true; result = AwaResult_SuccessCreated; break; case 3: - snprintf(flowAccess[objectInstanceID].RememberMeToken, *dataSize, "%s", (char *)*dataPointer); + memcpy(flowAccess[objectInstanceID].RememberMeToken, *dataPointer, *dataSize); + flowAccess[objectInstanceID].RememberMeToken[*dataSize] = '\0'; *changed = true; result = AwaResult_SuccessCreated; break; case 4: - memcpy(flowAccess[objectInstanceID].RememberMeTokenExpiry, *dataPointer, *dataSize); + flowAccess[objectInstanceID].RememberMeTokenExpiry = *((AwaTime *)*dataPointer); *changed = true; result = AwaResult_SuccessCreated; break; @@ -229,7 +233,7 @@ int DefineFlowAccessObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeTokenExpiry", 20001, 4, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeTokenExpiry", 20001, 4, AwaResourceType_Time, 0, 1, AwaResourceOperations_ReadWrite, accessHandler); if (error != AwaError_Success) { @@ -239,47 +243,3 @@ int DefineFlowAccessObject(AwaStaticClient *awaClient) return 0; } - -int CreateFlowAccessObject(AwaStaticClient *awaClient) -{ - AwaError error; - - error = AwaStaticClient_CreateObjectInstance(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0); - if (error != AwaError_Success) - { - printf("failed to create flow access object instance\n"); - return 1; - } - - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOWM2M_FLOW_ACCESS_OBJECT_URL); - if (error != AwaError_Success) - { - printf("failed to create URL resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY); - if (error != AwaError_Success) - { - printf("failed to create customer key resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET); - if (error != AwaError_Success) - { - printf("failed to create secret resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN); - if (error != AwaError_Success) - { - printf("failed to create token resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY); - if (error != AwaError_Success) - { - printf("failed to create token expiry resource\n"); - return 1; - } - return 0; -} diff --git a/lwm2m-client-flow-access-object.h b/lwm2m-client-flow-access-object.h index cd9c9fd..a834a66 100644 --- a/lwm2m-client-flow-access-object.h +++ b/lwm2m-client-flow-access-object.h @@ -37,6 +37,5 @@ #define LWM2M_CLIENT_FLOW_ACCESS_OBJECT_H_ int DefineFlowAccessObject(AwaStaticClient *awaClient); -int CreateFlowAccessObject(AwaStaticClient *awaClient); #endif /* LWM2M_CLIENT_FLOW_ACCESS_OBJECT_H_ */ diff --git a/lwm2m-client-flow-object.c b/lwm2m-client-flow-object.c index ebf0cc2..70f4273 100644 --- a/lwm2m-client-flow-object.c +++ b/lwm2m-client-flow-object.c @@ -84,19 +84,23 @@ static char licenseeSecret[64] = "getATTtDsNBpBRnMsN7GoQ=="; typedef struct { int8_t DeviceID[16]; + size_t DeviceIDSize; int8_t ParentID[16]; + size_t ParentIDSize; char DeviceType[64]; char Name[64]; char Description[64]; char FCAP[64]; - int8_t LicenseeID; - char LicenseeChallenge[64]; - int8_t HashIterations; + AwaInteger LicenseeID; + uint8_t LicenseeChallenge[64]; + size_t LicenseeChallengeSize; + AwaInteger HashIterations; uint8_t LicenseeHash[SHA256_HASH_LENGTH]; - int8_t Status; + size_t LicenseeHashSize; + AwaInteger Status; } FlowObject; -static FlowObject flow[FLOW_INSTANCES]; +static FlowObject flow[FLOW_INSTANCES] = {0}; static bool CalculateLicenseeHash(char *licenseeSecret, uint8_t hash[SHA256_HASH_LENGTH], const char *challenge, int challengeLength, int iterations) { @@ -118,7 +122,7 @@ static bool CalculateLicenseeHash(char *licenseeSecret, uint8_t hash[SHA256_HASH } AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID, - AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, uint16_t *dataSize, bool *changed) + AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, size_t *dataSize, bool *changed) { AwaResult result = AwaResult_InternalError; @@ -133,11 +137,12 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o case AwaOperation_CreateObjectInstance: printf("flow op - create object instance\n\n"); result = AwaResult_SuccessCreated; - memset(&flow[objectInstanceID], 0, sizeof(flow[objectInstanceID])); + memset(&flow[objectInstanceID], 0, sizeof(FlowObject)); break; case AwaOperation_CreateResource: printf("flow op - create resource %d\n", resourceID); + result = AwaResult_SuccessCreated; break; @@ -147,32 +152,32 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o { case 0: *dataPointer = flow[objectInstanceID].DeviceID; - *dataSize = sizeof(flow[objectInstanceID].DeviceID); + *dataSize = flow[objectInstanceID].DeviceIDSize; break; case 1: *dataPointer = flow[objectInstanceID].ParentID; - *dataSize = sizeof(flow[objectInstanceID].ParentID); + *dataSize = flow[objectInstanceID].ParentIDSize; break; case 2: *dataPointer = flow[objectInstanceID].DeviceType; - *dataSize = strlen(flow[objectInstanceID].DeviceType) + 1; + *dataSize = strlen(flow[objectInstanceID].DeviceType); break; case 3: *dataPointer = flow[objectInstanceID].Name; - *dataSize = strlen(flow[objectInstanceID].Name) + 1; + *dataSize = strlen(flow[objectInstanceID].Name); break; case 4: *dataPointer = flow[objectInstanceID].Description; - *dataSize = strlen(flow[objectInstanceID].Description) + 1; + *dataSize = strlen(flow[objectInstanceID].Description); break; case 5: *dataPointer = flow[objectInstanceID].FCAP; - *dataSize = strlen(flow[objectInstanceID].FCAP) + 1; + *dataSize = strlen(flow[objectInstanceID].FCAP); break; case 6: @@ -182,7 +187,7 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o case 7: *dataPointer = flow[objectInstanceID].LicenseeChallenge; - *dataSize = sizeof(flow[objectInstanceID].LicenseeChallenge); + *dataSize = flow[objectInstanceID].LicenseeChallengeSize; break; case 8: @@ -192,7 +197,7 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o case 9: *dataPointer = flow[objectInstanceID].LicenseeHash; - *dataSize = sizeof(flow[objectInstanceID].LicenseeHash); + *dataSize = flow[objectInstanceID].LicenseeHashSize; break; case 10: @@ -221,28 +226,33 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o break; case 2: - snprintf(flow[objectInstanceID].DeviceType, *dataSize, "%s", (char *)*dataPointer); + memcpy(flow[objectInstanceID].DeviceType, *dataPointer, *dataSize); + flow[objectInstanceID].DeviceType[*dataSize] = '\0'; *changed = true; break; case 3: - snprintf(flow[objectInstanceID].Name, *dataSize, "%s", (char *)*dataPointer); + memcpy(flow[objectInstanceID].Name, *dataPointer, *dataSize); + flow[objectInstanceID].Name[*dataSize] = '\0'; *changed = true; break; case 4: - snprintf(flow[objectInstanceID].Description, *dataSize, "%s", (char *)*dataPointer); + memcpy(flow[objectInstanceID].Description, *dataPointer, *dataSize); + flow[objectInstanceID].Description[*dataSize] = '\0'; *changed = true; break; case 5: - snprintf(flow[objectInstanceID].FCAP, *dataSize, "%s", (char *)*dataPointer); + memcpy(flow[objectInstanceID].FCAP, *dataPointer, *dataSize); + flow[objectInstanceID].FCAP[*dataSize] = '\0'; printf("FCAP = %s\n", flow[objectInstanceID].FCAP); *changed = true; break; case 6: - flow[objectInstanceID].LicenseeID = *dataPointer; + flow[objectInstanceID].LicenseeID = (*(AwaInteger*)(*dataPointer)); + printf("LicenseeID: %d %d\n", flow[objectInstanceID].LicenseeID, *dataSize); *changed = true; break; @@ -252,7 +262,7 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o break; case 8: - flow[objectInstanceID].HashIterations = *dataPointer; + flow[objectInstanceID].HashIterations = *((AwaInteger*)(*dataPointer)); *changed = true; break; @@ -262,7 +272,7 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o break; case 10: - flow[objectInstanceID].Status = *dataPointer; + flow[objectInstanceID].Status = *((AwaInteger*)(*dataPointer)); *changed = true; break; @@ -270,7 +280,7 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o printf("invalid resource id for write operation\n"); break; } - result = AwaResult_SuccessContent; + result = AwaResult_SuccessChanged; break; default: @@ -329,7 +339,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceType", 20000, 2, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceType", 20000, 2, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -353,7 +363,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "FCAP", 20000, 5, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "FCAP", 20000, 5, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -361,7 +371,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeID", 20000, 6, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeID", 20000, 6, AwaResourceType_Integer, 1, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -369,7 +379,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeChallenge", 20000, 7, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeChallenge", 20000, 7, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -377,7 +387,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "HashIterations", 20000, 8, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "HashIterations", 20000, 8, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -385,7 +395,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeHash", 20000, 9, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeHash", 20000, 9, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -393,7 +403,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Status", 20000, 10, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Status", 20000, 10, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -403,78 +413,3 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 0; } - -int CreateFlowObject(AwaStaticClient *awaClient) -{ - AwaError error; - - error = AwaStaticClient_CreateObjectInstance(awaClient, FLOWM2M_FLOW_OBJECT, 0); - if (error != AwaError_Success) - { - printf("failed to create flow object instance\n"); - return 1; - } - - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_DEVICEID); - if (error != AwaError_Success) - { - printf("failed to create device id resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_PARENTID); - if (error != AwaError_Success) - { - printf("failed to create parent id resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_DEVICETYPE); - if (error != AwaError_Success) - { - printf("failed to create device type resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_NAME); - if (error != AwaError_Success) - { - printf("failed to create name resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_DESCRIPTION); - if (error != AwaError_Success) - { - printf("failed to create description resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_FCAP); - if (error != AwaError_Success) - { - printf("failed to create FCAP resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_LICENSEEID); - if (error != AwaError_Success) - { - printf("failed to create licensee ID resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE); - if (error != AwaError_Success) - { - printf("failed to create licensee challenge resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_LICENSEEHASH); - if (error != AwaError_Success) - { - printf("failed to create licensee hash resource\n"); - return 1; - } - error = AwaStaticClient_CreateResource(awaClient, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_STATUS); - if (error != AwaError_Success) - { - printf("failed to create status resource\n"); - return 1; - } - - return 0; -} diff --git a/lwm2m-client-flow-object.h b/lwm2m-client-flow-object.h index 3d6850f..a473527 100644 --- a/lwm2m-client-flow-object.h +++ b/lwm2m-client-flow-object.h @@ -38,7 +38,5 @@ #include int DefineFlowObject(AwaStaticClient *awaClient); -int CreateFlowObject(AwaStaticClient *awaClient); - #endif /* LWM2M_CLIENT_FLOW_OBJECT_H_ */ From b37508383644d1f36b12ce4dc0bcc4cb0d2dd8cf Mon Sep 17 00:00:00 2001 From: Sean Kelly Date: Thu, 7 Apr 2016 13:04:04 +1200 Subject: [PATCH 07/14] Fix hashing process in Flow object, and improve Flow object and Flow Access object readability --- lwm2m-client-flow-access-object.c | 41 ++++----- lwm2m-client-flow-object.c | 136 ++++++++++++++---------------- 2 files changed, 86 insertions(+), 91 deletions(-) diff --git a/lwm2m-client-flow-access-object.c b/lwm2m-client-flow-access-object.c index 09df94c..5617f30 100644 --- a/lwm2m-client-flow-access-object.c +++ b/lwm2m-client-flow-access-object.c @@ -88,46 +88,48 @@ AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObje switch (operation) { + case AwaOperation_DeleteObjectInstance: + result = AwaResult_SuccessDeleted; + memset(&flowAccess[objectInstanceID], 0, sizeof(flowAccess[objectInstanceID])); + break; + case AwaOperation_CreateObjectInstance: - printf("flow access op - create object instance\n\n"); result = AwaResult_SuccessCreated; memset(&flowAccess[objectInstanceID], 0, sizeof(flowAccess[objectInstanceID])); break; case AwaOperation_CreateResource: - printf("flow access op - create resource %d\n", resourceID); result = AwaResult_SuccessCreated; break; case AwaOperation_Read: - printf("flow access op - read for res %d\n", resourceID); switch (resourceID) { - case 0: + case FLOWM2M_FLOW_ACCESS_OBJECT_URL: *dataPointer = flowAccess[objectInstanceID].URL; *dataSize = strlen(flowAccess[objectInstanceID].URL) ; result = AwaResult_SuccessCreated; break; - case 1: + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: *dataPointer = flowAccess[objectInstanceID].CustomerKey; *dataSize = strlen(flowAccess[objectInstanceID].CustomerKey); result = AwaResult_SuccessCreated; break; - case 2: + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: *dataPointer = flowAccess[objectInstanceID].CustomerSecret; *dataSize = strlen(flowAccess[objectInstanceID].CustomerSecret); result = AwaResult_SuccessCreated; break; - case 3: + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: *dataPointer = flowAccess[objectInstanceID].RememberMeToken; *dataSize = strlen(flowAccess[objectInstanceID].RememberMeToken); result = AwaResult_SuccessCreated; break; - case 4: + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: *dataPointer = &flowAccess[objectInstanceID].RememberMeTokenExpiry; *dataSize = sizeof(flowAccess[objectInstanceID].RememberMeTokenExpiry); result = AwaResult_SuccessCreated; @@ -140,38 +142,37 @@ AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObje break; case AwaOperation_Write: - printf("flow acccess op - write for res %d\n", resourceID); switch (resourceID) { - case 0: + case FLOWM2M_FLOW_ACCESS_OBJECT_URL: memcpy(flowAccess[objectInstanceID].URL, *dataPointer, *dataSize); flowAccess[objectInstanceID].URL[*dataSize] = '\0'; *changed = true; result = AwaResult_SuccessCreated; break; - case 1: + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: memcpy(flowAccess[objectInstanceID].CustomerKey,*dataPointer, *dataSize); flowAccess[objectInstanceID].CustomerKey[*dataSize] = '\0'; *changed = true; result = AwaResult_SuccessCreated; break; - case 2: + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: memcpy(flowAccess[objectInstanceID].CustomerSecret, *dataPointer, *dataSize); flowAccess[objectInstanceID].CustomerSecret[*dataSize] = '\0'; *changed = true; result = AwaResult_SuccessCreated; break; - case 3: + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: memcpy(flowAccess[objectInstanceID].RememberMeToken, *dataPointer, *dataSize); flowAccess[objectInstanceID].RememberMeToken[*dataSize] = '\0'; *changed = true; result = AwaResult_SuccessCreated; break; - case 4: + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: flowAccess[objectInstanceID].RememberMeTokenExpiry = *((AwaTime *)*dataPointer); *changed = true; result = AwaResult_SuccessCreated; @@ -194,14 +195,14 @@ int DefineFlowAccessObject(AwaStaticClient *awaClient) { AwaError error; - error = AwaStaticClient_DefineObjectWithHandler(awaClient, "FlowAccess", 20001, 0, FLOW_ACCESS_INSTANCES, accessHandler); + error = AwaStaticClient_DefineObjectWithHandler(awaClient, "FlowAccess", FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOW_ACCESS_INSTANCES, accessHandler); if (error != AwaError_Success) { printf("Failed to register flow access object\n"); return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "URL", 20001, 0, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "URL", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_URL, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, accessHandler); if (error != AwaError_Success) { @@ -209,7 +210,7 @@ int DefineFlowAccessObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CustomerKey", 20001, 1, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CustomerKey", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, accessHandler); if (error != AwaError_Success) { @@ -217,7 +218,7 @@ int DefineFlowAccessObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CustomerSecret", 20001, 2, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CustomerSecret", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, accessHandler); if (error != AwaError_Success) { @@ -225,7 +226,7 @@ int DefineFlowAccessObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeToken", 20001, 3, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeToken", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, accessHandler); if (error != AwaError_Success) { @@ -233,7 +234,7 @@ int DefineFlowAccessObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeTokenExpiry", 20001, 4, AwaResourceType_Time, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeTokenExpiry", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY, AwaResourceType_Time, 0, 1, AwaResourceOperations_ReadWrite, accessHandler); if (error != AwaError_Success) { diff --git a/lwm2m-client-flow-object.c b/lwm2m-client-flow-object.c index 70f4273..0de1d79 100644 --- a/lwm2m-client-flow-object.c +++ b/lwm2m-client-flow-object.c @@ -134,145 +134,139 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o switch (operation) { + case AwaOperation_DeleteObjectInstance: + result = AwaResult_SuccessDeleted; + memset(&flow[objectInstanceID], 0, sizeof(FlowObject)); + break; + case AwaOperation_CreateObjectInstance: - printf("flow op - create object instance\n\n"); result = AwaResult_SuccessCreated; memset(&flow[objectInstanceID], 0, sizeof(FlowObject)); break; case AwaOperation_CreateResource: - printf("flow op - create resource %d\n", resourceID); - result = AwaResult_SuccessCreated; break; case AwaOperation_Read: - printf("flow op - read for res %d\n", resourceID); switch (resourceID) { - case 0: + case FLOWM2M_FLOW_OBJECT_DEVICEID: *dataPointer = flow[objectInstanceID].DeviceID; *dataSize = flow[objectInstanceID].DeviceIDSize; break; - case 1: + case FLOWM2M_FLOW_OBJECT_PARENTID: *dataPointer = flow[objectInstanceID].ParentID; *dataSize = flow[objectInstanceID].ParentIDSize; break; - case 2: + case FLOWM2M_FLOW_OBJECT_DEVICETYPE: *dataPointer = flow[objectInstanceID].DeviceType; - *dataSize = strlen(flow[objectInstanceID].DeviceType); + *dataSize = strlen(flow[objectInstanceID].DeviceType) ; break; - case 3: + case FLOWM2M_FLOW_OBJECT_NAME: *dataPointer = flow[objectInstanceID].Name; *dataSize = strlen(flow[objectInstanceID].Name); break; - case 4: + case FLOWM2M_FLOW_OBJECT_DESCRIPTION: *dataPointer = flow[objectInstanceID].Description; *dataSize = strlen(flow[objectInstanceID].Description); break; - case 5: + case FLOWM2M_FLOW_OBJECT_FCAP: *dataPointer = flow[objectInstanceID].FCAP; *dataSize = strlen(flow[objectInstanceID].FCAP); break; - case 6: + case FLOWM2M_FLOW_OBJECT_LICENSEEID: *dataPointer = &flow[objectInstanceID].LicenseeID; *dataSize = sizeof(flow[objectInstanceID].LicenseeID); break; - case 7: + case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: *dataPointer = flow[objectInstanceID].LicenseeChallenge; *dataSize = flow[objectInstanceID].LicenseeChallengeSize; break; - case 8: + case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: *dataPointer = &flow[objectInstanceID].HashIterations; *dataSize = sizeof(flow[objectInstanceID].HashIterations); break; - case 9: + case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: *dataPointer = flow[objectInstanceID].LicenseeHash; *dataSize = flow[objectInstanceID].LicenseeHashSize; break; - case 10: + case FLOWM2M_FLOW_OBJECT_STATUS: *dataPointer = &flow[objectInstanceID].Status; *dataSize = sizeof(flow[objectInstanceID].Status); break; - default: - printf("Invalid resource id\n"); + printf("Invalid resource id %d\n", resourceID); + break; } result = AwaResult_SuccessContent; break; case AwaOperation_Write: - printf("flow op - write for res %d\n", resourceID); switch (resourceID) { - case 0: + case FLOWM2M_FLOW_OBJECT_DEVICEID: memcpy(flow[objectInstanceID].DeviceID, *dataPointer, *dataSize); - *changed = true; + flow[objectInstanceID].DeviceIDSize = *dataSize; break; - case 1: + case FLOWM2M_FLOW_OBJECT_PARENTID: memcpy(flow[objectInstanceID].ParentID, *dataPointer, *dataSize); - *changed = true; + flow[objectInstanceID].ParentIDSize = *dataSize; break; - case 2: + case FLOWM2M_FLOW_OBJECT_DEVICETYPE: memcpy(flow[objectInstanceID].DeviceType, *dataPointer, *dataSize); flow[objectInstanceID].DeviceType[*dataSize] = '\0'; - *changed = true; break; - case 3: + case FLOWM2M_FLOW_OBJECT_NAME: memcpy(flow[objectInstanceID].Name, *dataPointer, *dataSize); flow[objectInstanceID].Name[*dataSize] = '\0'; - *changed = true; break; - case 4: + case FLOWM2M_FLOW_OBJECT_DESCRIPTION: memcpy(flow[objectInstanceID].Description, *dataPointer, *dataSize); flow[objectInstanceID].Description[*dataSize] = '\0'; - *changed = true; break; - case 5: + case FLOWM2M_FLOW_OBJECT_FCAP: memcpy(flow[objectInstanceID].FCAP, *dataPointer, *dataSize); flow[objectInstanceID].FCAP[*dataSize] = '\0'; printf("FCAP = %s\n", flow[objectInstanceID].FCAP); - *changed = true; break; - case 6: + case FLOWM2M_FLOW_OBJECT_LICENSEEID: flow[objectInstanceID].LicenseeID = (*(AwaInteger*)(*dataPointer)); - printf("LicenseeID: %d %d\n", flow[objectInstanceID].LicenseeID, *dataSize); - *changed = true; break; - case 7: + case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: memcpy(flow[objectInstanceID].LicenseeChallenge, *dataPointer, *dataSize); - *changed = true; + flow[objectInstanceID].LicenseeChallengeSize = *dataSize; break; - case 8: + case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: flow[objectInstanceID].HashIterations = *((AwaInteger*)(*dataPointer)); - *changed = true; break; - case 9: + case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: memcpy(flow[objectInstanceID].LicenseeHash, *dataPointer, *dataSize); - *changed = true; + flow[objectInstanceID].LicenseeHashSize = *dataSize; break; - case 10: + case FLOWM2M_FLOW_OBJECT_STATUS: flow[objectInstanceID].Status = *((AwaInteger*)(*dataPointer)); + printf("Status: %li\n", flow[objectInstanceID].Status); *changed = true; break; @@ -288,25 +282,25 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o break; } - if(flow[objectInstanceID].HashIterations > 0 && flow[objectInstanceID].LicenseeChallenge != NULL - && (resourceID == FLOWM2M_FLOW_OBJECT_HASHITERATIONS || (resourceID == FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE))) + if(operation == AwaOperation_Write) { - uint8_t licenseeHash[SHA256_HASH_LENGTH]; - - printf("Calculating licensee hash with %d iterations...\n", flow[objectInstanceID].HashIterations); - - if (CalculateLicenseeHash(licenseeSecret, licenseeHash, flow[objectInstanceID].LicenseeChallenge, 64, - flow[objectInstanceID].HashIterations)) - { - printf("Calculated hash, writing Licensee Hash resource...\n"); - memcpy(flow[objectInstanceID].LicenseeHash, licenseeHash, SHA256_HASH_LENGTH); - result = AwaStaticClient_ResourceChanged(client, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_LICENSEEHASH); - } - else - { - printf("Licensee secret is invalid\n"); - return AwaResult_InternalError; - } + if(flow[objectInstanceID].HashIterations > 0 && flow[objectInstanceID].LicenseeChallengeSize > 0 + && (resourceID == FLOWM2M_FLOW_OBJECT_HASHITERATIONS || (resourceID == FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE))) + { + printf("Calculating licensee hash with %li iterations...\n", flow[objectInstanceID].HashIterations); + + if (CalculateLicenseeHash(licenseeSecret, flow[objectInstanceID].LicenseeHash, flow[objectInstanceID].LicenseeChallenge, flow[objectInstanceID].LicenseeChallengeSize, flow[objectInstanceID].HashIterations)) + { + printf("Calculated hash, writing Licensee Hash resource...\n"); + flow[objectInstanceID].LicenseeHashSize = SHA256_HASH_LENGTH; + AwaStaticClient_CreateResource(client, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_LICENSEEHASH); + AwaStaticClient_ResourceChanged(client, FLOWM2M_FLOW_OBJECT, 0, FLOWM2M_FLOW_OBJECT_LICENSEEHASH); + } + else + { + printf("Licensee secret is invalid\n"); + } + } } return result; @@ -316,14 +310,14 @@ int DefineFlowObject(AwaStaticClient *awaClient) { AwaError error; - error = AwaStaticClient_DefineObjectWithHandler(awaClient, "Flow", 20000, 0, FLOW_INSTANCES, handler); + error = AwaStaticClient_DefineObjectWithHandler(awaClient, "Flow", FLOWM2M_FLOW_OBJECT, 0, FLOW_INSTANCES, handler); if (error != AwaError_Success) { printf("Failed to register flow bject\n"); return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceID", 20000, 0, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceID", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_DEVICEID, AwaResourceType_Opaque, 1, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -331,7 +325,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "ParentID", 20000, 1, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "ParentID", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_PARENTID, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -339,7 +333,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceType", 20000, 2, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceType", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_DEVICETYPE, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -347,7 +341,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Name", 20000, 3, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Name", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_NAME, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -355,7 +349,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Description", 20000, 4, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Description", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_DESCRIPTION, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -363,7 +357,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "FCAP", 20000, 5, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "FCAP", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_FCAP, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -371,7 +365,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeID", 20000, 6, AwaResourceType_Integer, 1, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeID", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_LICENSEEID, AwaResourceType_Integer, 1, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -379,7 +373,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeChallenge", 20000, 7, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeChallenge", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -387,7 +381,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "HashIterations", 20000, 8, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "HashIterations", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_HASHITERATIONS, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -395,7 +389,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeHash", 20000, 9, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeHash", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_LICENSEEHASH, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { @@ -403,7 +397,7 @@ int DefineFlowObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Status", 20000, 10, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Status", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_STATUS, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, handler); if (error != AwaError_Success) { From ca4b1c7712cdae3ca9cb0060d831b249ab884816 Mon Sep 17 00:00:00 2001 From: Sean Kelly Date: Thu, 7 Apr 2016 13:20:26 +1200 Subject: [PATCH 08/14] Remove common.h --- common.h | 83 ------------------------------- lwm2m-client-flow-access-object.c | 1 - lwm2m-client-flow-object.c | 1 - lwm2m-client-ipso-digital-input.c | 1 - lwm2m-client-ipso-light-control.c | 1 - 5 files changed, 87 deletions(-) delete mode 100644 common.h diff --git a/common.h b/common.h deleted file mode 100644 index 4177cc6..0000000 --- a/common.h +++ /dev/null @@ -1,83 +0,0 @@ -/** - * @file - * LightWeightM2M Common definitions for libobjects. - * - * @author Imagination Technologies - * - * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group - * companies and/or licensors. - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are permitted - * provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions - * and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of - * conditions and the following disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to - * endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY - * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef COMMON_H_ -#define COMMON_H_ - -#define REGISTER_OBJECT(context, name, id, maxInstances, minInstances, handlers) \ - do \ - { \ - if (Lwm2mCore_RegisterObjectType(context, name, id, maxInstances, minInstances, handlers) \ - == -1) \ - { \ - Lwm2m_Error("Failed to register object type for "name" object with Lwm2m core\n"); \ - return -1; \ - } \ - }while(0) - -#define REGISTER_RESOURCE(context, name, objId, id, type, maxInstances, minInstances, operations, \ - handlers) \ - do \ - { \ - if (Lwm2mCore_RegisterResourceType(context, name, objId, id, type, maxInstances, \ - minInstances, operations, handlers) == -1) \ - { \ - Lwm2m_Error("Failed to register "name" resource with Lwm2m core\n"); \ - return -1; \ - } \ - }while(0) - -#define CREATE_OBJECT_INSTANCE(context, objectId, objectInstanceId) \ - do \ - { \ - if (Lwm2mCore_CreateObjectInstance(context, objectId, objectInstanceId) == -1) \ - { \ - Lwm2m_Error("Failed to create instance %d of "#objectId"\n", objectInstanceId); \ - return -1; \ - } \ - }while(0) - -#define CREATE_OPTIONAL_RESOURCE(context, objectId, objectInstanceId, id) \ - do \ - { \ - if (Lwm2mCore_CreateOptionalResource(context, objectId, objectInstanceId, id) == -1) \ - { \ - Lwm2m_Error("Failed to create optional "#id" for "#objectId" instance %d\n", \ - objectInstanceID); \ - return -1; \ - } \ - }while(0) - -#endif diff --git a/lwm2m-client-flow-access-object.c b/lwm2m-client-flow-access-object.c index 5617f30..596c886 100644 --- a/lwm2m-client-flow-access-object.c +++ b/lwm2m-client-flow-access-object.c @@ -42,7 +42,6 @@ #include #include #include "coap_abstraction.h" -#include "common.h" #include diff --git a/lwm2m-client-flow-object.c b/lwm2m-client-flow-object.c index 0de1d79..174ac2c 100644 --- a/lwm2m-client-flow-object.c +++ b/lwm2m-client-flow-object.c @@ -46,7 +46,6 @@ #include "coap_abstraction.h" #include "hmac.h" #include "b64.h" -#include "common.h" /*************************************************************************************************** * Definitions diff --git a/lwm2m-client-ipso-digital-input.c b/lwm2m-client-ipso-digital-input.c index 1a70d97..67c8149 100644 --- a/lwm2m-client-ipso-digital-input.c +++ b/lwm2m-client-ipso-digital-input.c @@ -43,7 +43,6 @@ #include #include "lwm2m_core.h" #include "coap_abstraction.h" -#include "common.h" /*************************************************************************************************** * Definitions diff --git a/lwm2m-client-ipso-light-control.c b/lwm2m-client-ipso-light-control.c index ba7ebfb..3d7e1e2 100644 --- a/lwm2m-client-ipso-light-control.c +++ b/lwm2m-client-ipso-light-control.c @@ -44,7 +44,6 @@ #include "lwm2m_core.h" #include "coap_abstraction.h" #include "lwm2m-client-ipso-light-control.h" -#include "common.h" /*************************************************************************************************** * Definitions From a79027c0eee72015ed8e54a71e7863a1e855d390 Mon Sep 17 00:00:00 2001 From: Sean Kelly Date: Thu, 7 Apr 2016 14:52:22 +1200 Subject: [PATCH 09/14] Initial add of device object - WIP --- Makefile.libobjects | 2 +- lwm2m-client-device-object.c | 160 +++++++++++++++++++++++++++++++++++ lwm2m-client-device-object.h | 41 +++++++++ 3 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 lwm2m-client-device-object.c create mode 100644 lwm2m-client-device-object.h diff --git a/Makefile.libobjects b/Makefile.libobjects index f89654e..0155e71 100644 --- a/Makefile.libobjects +++ b/Makefile.libobjects @@ -1,2 +1,2 @@ -libobjects_src = lwm2m-client-flow-object.c lwm2m-client-flow-access-object.c +libobjects_src = lwm2m-client-flow-object.c lwm2m-client-flow-access-object.c lwm2m-client-device-object.c #lwm2m-client-ipso-digital-input.c lwm2m-client-ipso-light-control.c diff --git a/lwm2m-client-device-object.c b/lwm2m-client-device-object.c new file mode 100644 index 0000000..8b7cafb --- /dev/null +++ b/lwm2m-client-device-object.c @@ -0,0 +1,160 @@ +/** + * @file + * LightWeightM2M LWM2M Flow Access object. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/*************************************************************************************************** + * Includes + **************************************************************************************************/ + +#include +#include +#include +#include +#include "coap_abstraction.h" + +#include + +/*************************************************************************************************** + * Definitions + **************************************************************************************************/ + +#define LWM2M_DEVICE_OBJECT 3 +#define LWM2M_DEVICE_OBJECT_MANUFACTURER 0 +#define LWM2M_DEVICE_OBJECT_MODEL_NUMBER 1 +#define LWM2M_DEVICE_OBJECT_SERIAL_NUMBER 2 +#define LWM2M_DEVICE_OBJECT_FIRMWARE_VERSION 3 +#define LWM2M_DEVICE_OBJECT_REBOOT 4 +#define LWM2M_DEVICE_OBJECT_FACTORY_RESET 5 +#define LWM2M_DEVICE_OBJECT_AVAILABLE_POWER_SOURCES 6 +#define LWM2M_DEVICE_OBJECT_POWER_SOURCE_VOLTAGE 7 +#define LWM2M_DEVICE_OBJECT_POWER_SOURCE_CURRENT 8 +#define LWM2M_DEVICE_OBJECT_BATTERY_LEVEL 9 +#define LWM2M_DEVICE_OBJECT_MEMORY_FREE 10 +#define LWM2M_DEVICE_OBJECT_ERROR_CODE 11 +#define LWM2M_DEVICE_OBJECT_RESET_ERROR_CODE 12 +#define LWM2M_DEVICE_OBJECT_CURRENT_TIME 13 +#define LWM2M_DEVICE_OBJECT_UTC_OFFSET 14 +#define LWM2M_DEVICE_OBJECT_TIME_ZONE 15 +#define LWM2M_DEVICE_OBJECT_SUPPORTED_BINDING_AND_MODES 16 +#define LWM2M_DEVICE_OBJECT_DEVICE_TYPE 17 +#define LWM2M_DEVICE_OBJECT_HARDWARE_VERSION 18 +#define LWM2M_DEVICE_OBJECT_SOFTWARE_VERSION 19 +#define LWM2M_DEVICE_OBJECT_BATTERY_STATUS 20 +#define LWM2M_DEVICE_OBJECT_MEMORY_TOTAL 21 + + +#define LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT 7 +#define LWM2M_DEVICE_OBJECT_ERROR_CODE_COUNT 2 + +typedef struct +{ + char Manufacturer[64]; + char ModelNumber[64]; + char SerialNumber[64]; + char FirmwareVersion[64]; + AwaInteger AvailablePowerSources[LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT]; + AwaInteger PowerSourceVoltage[LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT]; + AwaInteger PowerSourceCurrent[LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT]; + AwaInteger BatteryLevel; + AwaInteger MemoryFree; + AwaInteger ErrorCode[LWM2M_DEVICE_OBJECT_ERROR_CODE_COUNT]; + AwaTime CurrentTime; + char UTCOffset[16]; + char Timezone[16]; + char SupportedBindingandModes[4]; + char DeviceType[64]; + char HardwareVersion[64]; + char SoftwareVersion[64]; + AwaInteger BatteryStatus; + AwaInteger MemoryTotal; +} DeviceObject; + +DeviceObject DeviceObjectStorage = {0}; + +/*************************************************************************************************** + * Implementation - Public + **************************************************************************************************/ + +AwaResult executeHandler(AwaStaticClient *client, AwaOperation operation, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID, + AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, size_t *dataSize, bool *changed) +{ + AwaResult result = AwaResult_Unspecified; + + + printf("EXECUTE handler operation %d\n", operation); + + switch(operation) + { + case AwaOperation_CreateResource: + result = AwaResult_SuccessCreated; + break; + } + + return result; +} + + +int DefineDeviceObject(AwaStaticClient *awaClient) +{ + AwaStaticClient_DefineObject(awaClient, "Device" , LWM2M_DEVICE_OBJECT, 1, 1); + AwaStaticClient_DefineResourceWithPointer(awaClient, "Manufacturer", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MANUFACTURER, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadOnly, DeviceObjectStorage.Manufacturer, sizeof(DeviceObjectStorage.Manufacturer), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "ModelNumber", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MODEL_NUMBER, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadOnly, DeviceObjectStorage.ModelNumber, sizeof(DeviceObjectStorage.ModelNumber), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "SerialNumber", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_SERIAL_NUMBER, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadOnly, DeviceObjectStorage.SerialNumber, sizeof(DeviceObjectStorage.SerialNumber), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "FirmwareVersion", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_FIRMWARE_VERSION, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadOnly, DeviceObjectStorage.FirmwareVersion, sizeof(DeviceObjectStorage.FirmwareVersion), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "AvailablePowerSources", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_AVAILABLE_POWER_SOURCES, AwaResourceType_Integer, 0, LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT, AwaResourceOperations_ReadOnly, DeviceObjectStorage.AvailablePowerSources, sizeof(AwaInteger), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "PowerSourceVoltage", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_POWER_SOURCE_VOLTAGE, AwaResourceType_Integer, 0, LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT, AwaResourceOperations_ReadOnly, DeviceObjectStorage.PowerSourceVoltage, sizeof(AwaInteger), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "PowerSourceCurrent", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_POWER_SOURCE_CURRENT, AwaResourceType_Integer, 0, LWM2M_DEVICE_OBJECT_POWER_SOURCES_COUNT, AwaResourceOperations_ReadOnly, DeviceObjectStorage.PowerSourceCurrent, sizeof(AwaInteger), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "BatteryLevel", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_BATTERY_LEVEL, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadOnly, &DeviceObjectStorage.BatteryLevel, sizeof(AwaInteger), 0 ); + AwaStaticClient_DefineResourceWithPointer(awaClient, "MemoryFree", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MEMORY_FREE, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadOnly, &DeviceObjectStorage.BatteryStatus, sizeof(AwaInteger), 0 ); + AwaStaticClient_DefineResourceWithPointer(awaClient, "ErrorCode", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_ERROR_CODE, AwaResourceType_Integer, 1, LWM2M_DEVICE_OBJECT_ERROR_CODE_COUNT, AwaResourceOperations_ReadOnly, DeviceObjectStorage.ErrorCode, sizeof(AwaInteger), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "CurrentTime", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_CURRENT_TIME, AwaResourceType_Time, 0, 0, AwaResourceOperations_ReadWrite, &DeviceObjectStorage.CurrentTime, sizeof(AwaTime), 0 ); + AwaStaticClient_DefineResourceWithPointer(awaClient, "UTCOffset", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_UTC_OFFSET, AwaResourceType_String, 0, 0, AwaResourceOperations_ReadWrite, DeviceObjectStorage.UTCOffset, sizeof(DeviceObjectStorage.UTCOffset), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "Timezone", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_TIME_ZONE, AwaResourceType_String, 0, 0, AwaResourceOperations_ReadWrite, DeviceObjectStorage.Timezone, sizeof(DeviceObjectStorage.Timezone), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "SupportedBindingandModes", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_SUPPORTED_BINDING_AND_MODES, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadOnly, DeviceObjectStorage.SupportedBindingandModes, sizeof(DeviceObjectStorage.SupportedBindingandModes), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "DeviceType", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_DEVICE_TYPE, AwaResourceType_String, 0, 0, AwaResourceOperations_ReadOnly, DeviceObjectStorage.DeviceType, sizeof(DeviceObjectStorage.DeviceType), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "HardwareVersion", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_HARDWARE_VERSION, AwaResourceType_String, 0, 0, AwaResourceOperations_ReadOnly, DeviceObjectStorage.HardwareVersion, sizeof(DeviceObjectStorage.DeviceType), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "SoftwareVersion", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_SOFTWARE_VERSION, AwaResourceType_String, 0, 0, AwaResourceOperations_ReadOnly, DeviceObjectStorage.SoftwareVersion, sizeof(DeviceObjectStorage.SoftwareVersion), 0); + AwaStaticClient_DefineResourceWithPointer(awaClient, "BatteryStatus", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_BATTERY_STATUS, AwaResourceType_Integer, 0, 0, AwaResourceOperations_ReadOnly, &DeviceObjectStorage.BatteryStatus , sizeof(AwaInteger), 0 ); + AwaStaticClient_DefineResourceWithPointer(awaClient, "MemoryTotal", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_MEMORY_TOTAL, AwaResourceType_Integer, 0, 0, AwaResourceOperations_ReadOnly, &DeviceObjectStorage.MemoryTotal , sizeof(AwaInteger), 0 ); + + AwaStaticClient_DefineResourceWithHandler(awaClient, "Reboot", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_REBOOT, AwaResourceType_None, 1, 1, AwaResourceOperations_Execute, executeHandler); + AwaStaticClient_DefineResourceWithHandler(awaClient, "FactoryReset", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_FACTORY_RESET, AwaResourceType_None, 0, 0, AwaResourceOperations_Execute, executeHandler); + AwaStaticClient_DefineResourceWithHandler(awaClient, "ResetErrorCode", LWM2M_DEVICE_OBJECT, LWM2M_DEVICE_OBJECT_RESET_ERROR_CODE, AwaResourceType_None, 0, 0, AwaResourceOperations_Execute, executeHandler); + + AwaStaticClient_CreateObjectInstance(awaClient, LWM2M_DEVICE_OBJECT, 0); + + AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_SOFTWARE_VERSION); + + return 0; +} diff --git a/lwm2m-client-device-object.h b/lwm2m-client-device-object.h new file mode 100644 index 0000000..f1ac1a0 --- /dev/null +++ b/lwm2m-client-device-object.h @@ -0,0 +1,41 @@ +/** + * @file + * LightWeightM2M LWM2M Flow Access object. + * + * @author Imagination Technologies + * + * @copyright Copyright (c) 2016, Imagination Technologies Limited and/or its affiliated group + * companies and/or licensors. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are permitted + * provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of + * conditions and the following disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to + * endorse or promote products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef LIBOBJECTS_LWM2M_CLIENT_DEVICE_OBJECT_H_ +#define LIBOBJECTS_LWM2M_CLIENT_DEVICE_OBJECT_H_ + +int DefineDeviceObject(AwaStaticClient *awaClient); + +#endif /* LIBOBJECTS_LWM2M_CLIENT_DEVICE_OBJECT_H_ */ From a82e2ac19478388ca69b15411a2e22a5677507ab Mon Sep 17 00:00:00 2001 From: Sean Kelly Date: Fri, 8 Apr 2016 11:41:43 +1200 Subject: [PATCH 10/14] Add software version to device object for provisioning Improve licensee hashing conditions Fix flow access object --- lwm2m-client-device-object.c | 21 +++++++++++++++++++-- lwm2m-client-flow-access-object.c | 20 ++++++++++---------- lwm2m-client-flow-object.c | 2 +- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/lwm2m-client-device-object.c b/lwm2m-client-device-object.c index 8b7cafb..6129a33 100644 --- a/lwm2m-client-device-object.c +++ b/lwm2m-client-device-object.c @@ -100,7 +100,8 @@ typedef struct AwaInteger MemoryTotal; } DeviceObject; -DeviceObject DeviceObjectStorage = {0}; +DeviceObject DeviceObjectStorage = { .SupportedBindingandModes = "U", + .SoftwareVersion = VERSION}; /*************************************************************************************************** * Implementation - Public @@ -154,7 +155,23 @@ int DefineDeviceObject(AwaStaticClient *awaClient) AwaStaticClient_CreateObjectInstance(awaClient, LWM2M_DEVICE_OBJECT, 0); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_MANUFACTURER); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_MODEL_NUMBER); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_SERIAL_NUMBER); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_FIRMWARE_VERSION); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_AVAILABLE_POWER_SOURCES); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_POWER_SOURCE_VOLTAGE); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_POWER_SOURCE_CURRENT); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_BATTERY_LEVEL); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_MEMORY_FREE); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_CURRENT_TIME); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_UTC_OFFSET); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_TIME_ZONE); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_DEVICE_TYPE); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_HARDWARE_VERSION); AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_SOFTWARE_VERSION); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_BATTERY_STATUS); +// AwaStaticClient_CreateResource(awaClient, LWM2M_DEVICE_OBJECT, 0, LWM2M_DEVICE_OBJECT_MEMORY_TOTAL); - return 0; + return 0; } diff --git a/lwm2m-client-flow-access-object.c b/lwm2m-client-flow-access-object.c index 596c886..5748da2 100644 --- a/lwm2m-client-flow-access-object.c +++ b/lwm2m-client-flow-access-object.c @@ -107,31 +107,31 @@ AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObje case FLOWM2M_FLOW_ACCESS_OBJECT_URL: *dataPointer = flowAccess[objectInstanceID].URL; *dataSize = strlen(flowAccess[objectInstanceID].URL) ; - result = AwaResult_SuccessCreated; + result = AwaResult_SuccessContent; break; case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: *dataPointer = flowAccess[objectInstanceID].CustomerKey; *dataSize = strlen(flowAccess[objectInstanceID].CustomerKey); - result = AwaResult_SuccessCreated; + result = AwaResult_SuccessContent; break; case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: *dataPointer = flowAccess[objectInstanceID].CustomerSecret; *dataSize = strlen(flowAccess[objectInstanceID].CustomerSecret); - result = AwaResult_SuccessCreated; + result = AwaResult_SuccessContent; break; case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: *dataPointer = flowAccess[objectInstanceID].RememberMeToken; *dataSize = strlen(flowAccess[objectInstanceID].RememberMeToken); - result = AwaResult_SuccessCreated; + result = AwaResult_SuccessContent; break; case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: *dataPointer = &flowAccess[objectInstanceID].RememberMeTokenExpiry; *dataSize = sizeof(flowAccess[objectInstanceID].RememberMeTokenExpiry); - result = AwaResult_SuccessCreated; + result = AwaResult_SuccessContent; break; default: @@ -147,34 +147,34 @@ AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObje memcpy(flowAccess[objectInstanceID].URL, *dataPointer, *dataSize); flowAccess[objectInstanceID].URL[*dataSize] = '\0'; *changed = true; - result = AwaResult_SuccessCreated; + result = AwaResult_SuccessChanged; break; case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: memcpy(flowAccess[objectInstanceID].CustomerKey,*dataPointer, *dataSize); flowAccess[objectInstanceID].CustomerKey[*dataSize] = '\0'; *changed = true; - result = AwaResult_SuccessCreated; + result = AwaResult_SuccessChanged; break; case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: memcpy(flowAccess[objectInstanceID].CustomerSecret, *dataPointer, *dataSize); flowAccess[objectInstanceID].CustomerSecret[*dataSize] = '\0'; *changed = true; - result = AwaResult_SuccessCreated; + result = AwaResult_SuccessChanged; break; case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: memcpy(flowAccess[objectInstanceID].RememberMeToken, *dataPointer, *dataSize); flowAccess[objectInstanceID].RememberMeToken[*dataSize] = '\0'; *changed = true; - result = AwaResult_SuccessCreated; + result = AwaResult_SuccessChanged; break; case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: flowAccess[objectInstanceID].RememberMeTokenExpiry = *((AwaTime *)*dataPointer); *changed = true; - result = AwaResult_SuccessCreated; + result = AwaResult_SuccessChanged; break; default: diff --git a/lwm2m-client-flow-object.c b/lwm2m-client-flow-object.c index 174ac2c..8dbf071 100644 --- a/lwm2m-client-flow-object.c +++ b/lwm2m-client-flow-object.c @@ -283,7 +283,7 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o if(operation == AwaOperation_Write) { - if(flow[objectInstanceID].HashIterations > 0 && flow[objectInstanceID].LicenseeChallengeSize > 0 + if(flow[objectInstanceID].HashIterations > 0 && flow[objectInstanceID].LicenseeChallengeSize > 0 && flow[objectInstanceID].LicenseeHashSize == 0 && (resourceID == FLOWM2M_FLOW_OBJECT_HASHITERATIONS || (resourceID == FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE))) { printf("Calculating licensee hash with %li iterations...\n", flow[objectInstanceID].HashIterations); From 8d1892fe09b1198d6841d4f8a27c109d237480e6 Mon Sep 17 00:00:00 2001 From: Rahul Daga Date: Wed, 4 May 2016 13:02:14 +0530 Subject: [PATCH 11/14] update digital input methods to use static api Signed-off-by: Rahul Daga --- Makefile.libobjects | 5 +- lwm2m-client-ipso-digital-input.c | 604 ++++++++++++------------------ lwm2m-client-ipso-digital-input.h | 27 +- 3 files changed, 257 insertions(+), 379 deletions(-) diff --git a/Makefile.libobjects b/Makefile.libobjects index 0155e71..f2765c2 100644 --- a/Makefile.libobjects +++ b/Makefile.libobjects @@ -1,2 +1,3 @@ -libobjects_src = lwm2m-client-flow-object.c lwm2m-client-flow-access-object.c lwm2m-client-device-object.c -#lwm2m-client-ipso-digital-input.c lwm2m-client-ipso-light-control.c +libobjects_src = lwm2m-client-flow-object.c lwm2m-client-flow-access-object.c lwm2m-client-device-object.c \ + lwm2m-client-ipso-digital-input.c + #lwm2m-client-ipso-light-control.c diff --git a/lwm2m-client-ipso-digital-input.c b/lwm2m-client-ipso-digital-input.c index 67c8149..1c78264 100644 --- a/lwm2m-client-ipso-digital-input.c +++ b/lwm2m-client-ipso-digital-input.c @@ -41,35 +41,8 @@ #include #include #include -#include "lwm2m_core.h" -#include "coap_abstraction.h" - -/*************************************************************************************************** - * Definitions - **************************************************************************************************/ - -#define IPSO_DIGITAL_INPUT_OBJECT 3200 -#define IPSO_DIGITAL_INPUT_STATE 5500 -#define IPSO_DIGITAL_INPUT_COUNTER 5501 -#define IPSO_DIGITAL_INPUT_POLARITY 5502 -#define IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD 5503 -#define IPSO_DIGITAL_INPUT_EDGE_SELECTION 5504 -#define IPSO_DIGITAL_INPUT_COUNTER_RESET 5505 - -#define IPSO_APPICATION_TYPE 5750 -#define IPSO_SENSOR_TYPE 5751 - -#define DIGITAL_INPUTS 2 - -#define MAX_STR_SIZE 128 - -#define REGISTER_DIGITAL_INPUT_RESOURCE(context, name, id, type, operations) \ - REGISTER_RESOURCE(context, name, IPSO_DIGITAL_INPUT_OBJECT, id, type, \ - MultipleInstancesEnum_Single, MandatoryEnum_Optional, operations, \ - &DigitalInputResourceOperationHandlers) - -#define CREATE_DIGITAL_INPUT_OPTIONAL_RESOURCE(context, objectInstanceId, resourcId) \ - CREATE_OPTIONAL_RESOURCE(context, IPSO_DIGITAL_INPUT_OBJECT, objectInstanceId, resourcId) +#include +#include "lwm2m-client-ipso-digital-input.h" /*************************************************************************************************** * Typedefs @@ -77,368 +50,253 @@ typedef struct { - bool State; - int64_t Counter; - bool Polarity; - int64_t DebouncePeriod; - int64_t EdgeSelection; - char ApplicationType[MAX_STR_SIZE]; - char SensoryType[MAX_STR_SIZE]; + bool State; + AwaInteger Counter; + bool Polarity; + AwaTime DebouncePeriod; + AwaInteger EdgeSelection; + char ApplicationType[128]; + char SensorType[128]; } IPSODigitalInput; -/*************************************************************************************************** - * Prototypes - **************************************************************************************************/ - -static int DigitalInput_ResourceReadHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t *destBuffer, int destBufferLen); - -static int DigitalInput_ResourceGetLengthHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID); - -static int DigitalInput_ResourceWriteHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t *srcBuffer, int srcBufferLen, bool *changed); - -static int DigitalInput_ResourceExecuteHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, uint8_t *srcBuffer, - int srcBufferLen); - -static int DigitalInput_ResourceCreateHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); - -static int DigitalInput_ObjectCreateInstanceHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID); - -static int DigitalInput_ObjectDeleteHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); - /*************************************************************************************************** * Globals **************************************************************************************************/ -static ObjectOperationHandlers DigitalInputObjectOperationHandlers = -{ - .CreateInstance = DigitalInput_ObjectCreateInstanceHandler, - .Delete = DigitalInput_ObjectDeleteHandler, -}; - -static ResourceOperationHandlers DigitalInputResourceOperationHandlers = -{ - .Read = DigitalInput_ResourceReadHandler, - .GetLength = DigitalInput_ResourceGetLengthHandler, - .Write = DigitalInput_ResourceWriteHandler, - .CreateOptionalResource = DigitalInput_ResourceCreateHandler, - .Execute = DigitalInput_ResourceExecuteHandler, -}; - static IPSODigitalInput digitalInputs[DIGITAL_INPUTS]; /*************************************************************************************************** * Implementation **************************************************************************************************/ -static int DigitalInput_ObjectCreateInstanceHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID) -{ - if(objectInstanceID >= DIGITAL_INPUTS) - { - Lwm2m_Error("DigitalInput_ResourceCreateHandler instance number %d out of range (max %d)", - objectInstanceID, DIGITAL_INPUTS - 1); - return -1; - } - return objectInstanceID; -} - -static int DigitalInput_ResourceCreateHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) -{ - return 0; -} - -static int DigitalInput_ObjectDeleteHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) -{ - if (objectID != IPSO_DIGITAL_INPUT_OBJECT) - { - Lwm2m_Error("DigitalInput_ObjectDeleteHandler Invalid OIR: %d/%d/%d\n", objectID, - objectInstanceID, resourceID); - return -1; - } - - if(objectInstanceID >= DIGITAL_INPUTS) - { - Lwm2m_Error("DigitalInput_ObjectDeleteHandler instance number %d out of range (max %d)", - objectInstanceID, DIGITAL_INPUTS - 1); - return -1; - } - - if (resourceID == -1) - { - memset(&digitalInputs[objectInstanceID], 0, sizeof(IPSODigitalInput)); - } - else - { - //TODO - } - - return 0; -} - -static int DigitalInput_ResourceReadHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t *destBuffer, int destBufferLen) -{ - int result = 0; - - switch (resourceID) - { - case IPSO_DIGITAL_INPUT_STATE: - result = sizeof(digitalInputs[objectInstanceID].State); - memcpy(destBuffer, &digitalInputs[objectInstanceID].State, result); - break; - - case IPSO_DIGITAL_INPUT_COUNTER: - result = sizeof(digitalInputs[objectInstanceID].Counter); - memcpy(destBuffer, &digitalInputs[objectInstanceID].Counter, result); - break; - - case IPSO_DIGITAL_INPUT_POLARITY: - result = sizeof(digitalInputs[objectInstanceID].State); - memcpy(destBuffer, &digitalInputs[objectInstanceID].State, result); - break; - - case IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD: - result = sizeof(digitalInputs[objectInstanceID].Counter); - memcpy(destBuffer, &digitalInputs[objectInstanceID].Counter, result); - break; - - case IPSO_DIGITAL_INPUT_EDGE_SELECTION: - result = sizeof(digitalInputs[objectInstanceID].State); - memcpy(destBuffer, &digitalInputs[objectInstanceID].State, result); - break; - - case IPSO_APPICATION_TYPE: - result = strlen(digitalInputs[objectInstanceID].ApplicationType) + 1; - memcpy(destBuffer, digitalInputs[objectInstanceID].ApplicationType, result); - break; - - case IPSO_SENSOR_TYPE: - result = strlen(digitalInputs[objectInstanceID].SensoryType) + 1; - memcpy(destBuffer, digitalInputs[objectInstanceID].SensoryType, result); - break; - - default: - result = -1; - break; - } - - return result; -} - -static int DigitalInput_ResourceGetLengthHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID) -{ - int result = 0; - - switch (resourceID) - { - case IPSO_DIGITAL_INPUT_STATE: - result = sizeof(digitalInputs[objectInstanceID].State); - break; - - case IPSO_DIGITAL_INPUT_COUNTER: - result = sizeof(digitalInputs[objectInstanceID].Counter); - break; - - case IPSO_DIGITAL_INPUT_POLARITY: - result = sizeof(digitalInputs[objectInstanceID].State); - break; - - case IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD: - result = sizeof(digitalInputs[objectInstanceID].Counter); - break; - - case IPSO_DIGITAL_INPUT_EDGE_SELECTION: - result = sizeof(digitalInputs[objectInstanceID].State); - break; - - case IPSO_APPICATION_TYPE: - result = strlen(digitalInputs[objectInstanceID].ApplicationType) + 1; - break; - - case IPSO_SENSOR_TYPE: - result = strlen(digitalInputs[objectInstanceID].SensoryType) + 1; - break; - - default: - result = -1; - break; - } - - return result; -} - -static int DigitalInput_ResourceWriteHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t *srcBuffer, int srcBufferLen, bool *changed) -{ - int result; - - switch(resourceID) - { - case IPSO_DIGITAL_INPUT_STATE: - result = srcBufferLen; - memcpy(&digitalInputs[objectInstanceID].State, srcBuffer, result); - break; - - case IPSO_DIGITAL_INPUT_COUNTER: - result = srcBufferLen; - memcpy(&digitalInputs[objectInstanceID].Counter, srcBuffer, result); - Lwm2m_Debug("Button %d counter incremented to %d.\n", objectInstanceID + 1, - (int)digitalInputs[objectInstanceID].Counter); - break; - - case IPSO_DIGITAL_INPUT_POLARITY: - result = srcBufferLen; - memcpy(&digitalInputs[objectInstanceID].State, srcBuffer, result); - break; - - case IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD: - result = srcBufferLen; - memcpy(&digitalInputs[objectInstanceID].Counter, srcBuffer, result); - break; - - case IPSO_DIGITAL_INPUT_EDGE_SELECTION: - result = srcBufferLen; - memcpy(&digitalInputs[objectInstanceID].State, srcBuffer, result); - break; - - case IPSO_APPICATION_TYPE: - result = srcBufferLen; - if(result < sizeof(digitalInputs[objectInstanceID].ApplicationType)) - { - memcpy(digitalInputs[objectInstanceID].ApplicationType, srcBuffer, result); - } - else - { - Lwm2m_Error("DigitalInput_ResourceWriteHandler Application Type string too long: " - "%d", result); - result = -1; - } - break; - - case IPSO_SENSOR_TYPE: - result = srcBufferLen; - if(result < sizeof(digitalInputs[objectInstanceID].SensoryType)) - { - memcpy(digitalInputs[objectInstanceID].SensoryType, srcBuffer, result); - } - else - { - Lwm2m_Error("DigitalInput_ResourceWriteHandler Sensor Type string too long: %d", - result); - result = -1; - } - break; - - default: - result = -1; - break; - } - - if(result > 0) - *changed = true; - - return result; -} - -static int DigitalInput_ResourceExecuteHandler(void *context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, uint8_t *srcBuffer, - int srcBufferLen) +static AwaResult digitalInputHandler(AwaStaticClient *client, AwaOperation operation, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID, + AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, size_t *dataSize, bool *changed) { - if(resourceID == IPSO_DIGITAL_INPUT_COUNTER_RESET) - { - int64_t zero = 0; - if (Lwm2mCore_SetResourceInstanceValue(context, objectID, objectInstanceID, - IPSO_DIGITAL_INPUT_COUNTER, 0, &zero, sizeof(zero)) == -1) - { - Lwm2m_Error("Failed to set Counter to %" PRId64 "\n", zero); - return -1; - } - } - else - return -1; - - return 0; + AwaResult result = AwaResult_InternalError; + if (!((objectID == IPSO_DIGITAL_INPUT_OBJECT) && (objectInstanceID >= 0) && (objectInstanceID < DIGITAL_INPUTS))) + { + printf("Incorrect object data\n"); + return result; + } + + switch (operation) + { + case AwaOperation_DeleteObjectInstance: + result = AwaResult_SuccessDeleted; + memset(&digitalInputs[objectInstanceID], 0, sizeof(digitalInputs[objectInstanceID])); + break; + + case AwaOperation_CreateObjectInstance: + result = AwaResult_SuccessCreated; + memset(&digitalInputs[objectInstanceID], 0, sizeof(digitalInputs[objectInstanceID])); + break; + + case AwaOperation_CreateResource: + result = AwaResult_SuccessCreated; + break; + + case AwaOperation_Execute: + if (resourceID == IPSO_DIGITAL_INPUT_COUNTER_RESET) + { + digitalInputs[objectInstanceID].Counter = 0; + AwaStaticClient_ResourceChanged(client, IPSO_DIGITAL_INPUT_OBJECT, objectInstanceID, IPSO_DIGITAL_INPUT_COUNTER); + result = AwaResult_Success; + } + else + { + printf("\n Invalid resource ID for DigitalInput execute operation"); + result = AwaResult_InternalError; + } + break; + + case AwaOperation_Read: + result = AwaResult_SuccessContent; + switch (resourceID) + { + case IPSO_DIGITAL_INPUT_STATE: + *dataPointer = &digitalInputs[objectInstanceID].State; + *dataSize = sizeof(digitalInputs[objectInstanceID].State) ; + break; + + case IPSO_DIGITAL_INPUT_COUNTER: + *dataPointer = &digitalInputs[objectInstanceID].Counter; + *dataSize = sizeof(digitalInputs[objectInstanceID].Counter) ; + break; + + case IPSO_DIGITAL_INPUT_POLARITY: + *dataPointer = &digitalInputs[objectInstanceID].Polarity; + *dataSize = sizeof(digitalInputs[objectInstanceID].Polarity) ; + break; + + case IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD: + *dataPointer = &digitalInputs[objectInstanceID].DebouncePeriod; + *dataSize = sizeof(digitalInputs[objectInstanceID].DebouncePeriod) ; + break; + + case IPSO_DIGITAL_INPUT_EDGE_SELECTION: + *dataPointer = &digitalInputs[objectInstanceID].EdgeSelection; + *dataSize = sizeof(digitalInputs[objectInstanceID].EdgeSelection) ; + break; + + case IPSO_APPLICATION_TYPE: + *dataPointer = digitalInputs[objectInstanceID].ApplicationType; + *dataSize = sizeof(digitalInputs[objectInstanceID].ApplicationType) ; + break; + + case IPSO_SENSOR_TYPE: + *dataPointer = digitalInputs[objectInstanceID].SensorType; + *dataSize = sizeof(digitalInputs[objectInstanceID].SensorType) ; + break; + + default: + printf("\n Invalid resource ID for DigitalInput read operation"); + result = AwaResult_InternalError; + break; + } + break; + + case AwaOperation_Write: + *changed = true; + result = AwaResult_SuccessChanged; + switch (resourceID) + { + case IPSO_DIGITAL_INPUT_STATE: + digitalInputs[objectInstanceID].State = *((AwaBoolean *)*dataPointer); + break; + + case IPSO_DIGITAL_INPUT_COUNTER: + digitalInputs[objectInstanceID].Counter = *((AwaInteger *)*dataPointer); + break; + + case IPSO_DIGITAL_INPUT_POLARITY: + digitalInputs[objectInstanceID].Polarity = *((AwaBoolean *)*dataPointer); + break; + + case IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD: + digitalInputs[objectInstanceID].DebouncePeriod = *((AwaTime *)*dataPointer); + break; + + case IPSO_DIGITAL_INPUT_EDGE_SELECTION: + digitalInputs[objectInstanceID].EdgeSelection = *((AwaInteger *)*dataPointer); + break; + + case IPSO_APPLICATION_TYPE: + if(*dataSize < sizeof(digitalInputs[objectInstanceID].ApplicationType)) + { + memcpy(digitalInputs[objectInstanceID].ApplicationType, *dataPointer, *dataSize); + digitalInputs[objectInstanceID].ApplicationType[*dataSize] = '\0'; + } + else + { + result = AwaResult_BadRequest; + } + break; + + case IPSO_SENSOR_TYPE: + if(*dataSize < sizeof(digitalInputs[objectInstanceID].SensorType)) + { + memcpy(digitalInputs[objectInstanceID].SensorType, *dataPointer, *dataSize); + digitalInputs[objectInstanceID].SensorType[*dataSize] = '\0'; + } + else + { + result = AwaResult_BadRequest; + } + break; + + default: + printf("\n Invalid resource ID for DigitalInput write operation"); + result = AwaResult_InternalError; + break; + } + break; + default: + printf("DigitalInput - unknown operation\n"); + break; + } + return result; } -/*************************************************************************************************** - * Implementation - Public - **************************************************************************************************/ - -int DigitalInput_RegisterDigitalInputObject(Lwm2mContextType * context) +int DefineDigitalInputObject(AwaStaticClient *awaClient) { - - REGISTER_OBJECT(context, "DigitalInput", IPSO_DIGITAL_INPUT_OBJECT, \ - MultipleInstancesEnum_Multiple, MandatoryEnum_Optional, \ - &DigitalInputObjectOperationHandlers); - - REGISTER_DIGITAL_INPUT_RESOURCE(context, "State", IPSO_DIGITAL_INPUT_STATE, \ - ResourceTypeEnum_TypeBoolean, Operations_R); - REGISTER_DIGITAL_INPUT_RESOURCE(context, "Counter", IPSO_DIGITAL_INPUT_COUNTER, \ - ResourceTypeEnum_TypeInteger, Operations_R); - REGISTER_DIGITAL_INPUT_RESOURCE(context, "Polarity", IPSO_DIGITAL_INPUT_POLARITY, \ - ResourceTypeEnum_TypeBoolean, Operations_RW); - REGISTER_DIGITAL_INPUT_RESOURCE(context, "DebouncePeriod", IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD, \ - ResourceTypeEnum_TypeInteger, Operations_RW); - REGISTER_DIGITAL_INPUT_RESOURCE(context, "EdgeSelection", IPSO_DIGITAL_INPUT_EDGE_SELECTION, \ - ResourceTypeEnum_TypeInteger, Operations_RW); - REGISTER_DIGITAL_INPUT_RESOURCE(context, "CounterReset", IPSO_DIGITAL_INPUT_COUNTER_RESET, \ - ResourceTypeEnum_TypeNone, Operations_E); - REGISTER_DIGITAL_INPUT_RESOURCE(context, "ApplicationType", IPSO_APPICATION_TYPE, \ - ResourceTypeEnum_TypeString, Operations_R); - REGISTER_DIGITAL_INPUT_RESOURCE(context, "SensorType", IPSO_SENSOR_TYPE, \ - ResourceTypeEnum_TypeString, Operations_R); - - return 0; + AwaError error; + error = AwaStaticClient_DefineObjectWithHandler(awaClient, "DigitalInput", IPSO_DIGITAL_INPUT_OBJECT, 0, DIGITAL_INPUTS, digitalInputHandler); + if (error != AwaError_Success) + { + printf("Failed to register flow access object\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "State", IPSO_DIGITAL_INPUT_OBJECT, IPSO_DIGITAL_INPUT_STATE, AwaResourceType_Boolean, 0, 1, AwaResourceOperations_ReadOnly, digitalInputHandler); + if (error != AwaError_Success) + { + printf("Failed to define State resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Counter", IPSO_DIGITAL_INPUT_OBJECT, IPSO_DIGITAL_INPUT_COUNTER, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadOnly, digitalInputHandler); + if (error != AwaError_Success) + { + printf("Failed to define Counter resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Polarity", IPSO_DIGITAL_INPUT_OBJECT, IPSO_DIGITAL_INPUT_POLARITY, AwaResourceType_Boolean, 0, 1, AwaResourceOperations_ReadWrite, digitalInputHandler); + if (error != AwaError_Success) + { + printf("Failed to define Polarity resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DebouncePeriod", IPSO_DIGITAL_INPUT_OBJECT, IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD, AwaResourceType_Time, 0, 1, AwaResourceOperations_ReadWrite, digitalInputHandler); + if (error != AwaError_Success) + { + printf("Failed to define DebouncePeriod resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "EdgeSelection", IPSO_DIGITAL_INPUT_OBJECT, IPSO_DIGITAL_INPUT_EDGE_SELECTION, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, digitalInputHandler); + if (error != AwaError_Success) + { + printf("Failed to define EdgeSelection resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "ApplicationType", IPSO_DIGITAL_INPUT_OBJECT, IPSO_APPLICATION_TYPE, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadOnly, digitalInputHandler); + if (error != AwaError_Success) + { + printf("Failed to define ApplicationType resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "SensorType", IPSO_DIGITAL_INPUT_OBJECT , IPSO_SENSOR_TYPE, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadOnly, digitalInputHandler); + if (error != AwaError_Success) + { + printf("Failed to define SensoryType resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CounterReset", IPSO_DIGITAL_INPUT_OBJECT , IPSO_DIGITAL_INPUT_COUNTER_RESET, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_Execute, digitalInputHandler); + if (error != AwaError_Success) + { + printf("Failed to define CounterReset resource\n"); + return 1; + } + return 0; } -int DigitalInput_AddDigitialInput(Lwm2mContextType *context, ObjectInstanceIDType objectInstanceID) -{ - if(objectInstanceID < DIGITAL_INPUTS) - { - CREATE_OBJECT_INSTANCE(context, IPSO_DIGITAL_INPUT_OBJECT, objectInstanceID); - CREATE_DIGITAL_INPUT_OPTIONAL_RESOURCE(context, objectInstanceID, \ - IPSO_DIGITAL_INPUT_COUNTER); - CREATE_DIGITAL_INPUT_OPTIONAL_RESOURCE(context, objectInstanceID, \ - IPSO_DIGITAL_INPUT_COUNTER_RESET); - CREATE_DIGITAL_INPUT_OPTIONAL_RESOURCE(context, objectInstanceID, IPSO_SENSOR_TYPE); - - memset(&digitalInputs[objectInstanceID], 0, sizeof(IPSODigitalInput)); - snprintf(digitalInputs[objectInstanceID].SensoryType, MAX_STR_SIZE, "Button%d", - objectInstanceID + 1); - } - else - { - Lwm2m_Error("%d instance of Digital Input exceeds max instances %d\n", objectInstanceID, - DIGITAL_INPUTS); - return -1; - } - return 0; -} -int DigitalInput_IncrementCounter(Lwm2mContextType *context, ObjectInstanceIDType objectInstanceID) +int DigitalInput_IncrementCounter(AwaStaticClient *awaClient, AwaObjectInstanceID objectInstanceID) { - int64_t counter = digitalInputs[objectInstanceID].Counter + 1; - if (Lwm2mCore_SetResourceInstanceValue(context, IPSO_DIGITAL_INPUT_OBJECT, objectInstanceID, - IPSO_DIGITAL_INPUT_COUNTER, 0, &counter, sizeof(counter)) == -1) - { - Lwm2m_Error("Failed to increment Counter resource\n"); - return -1; - } - return 0; + if ((awaClient != NULL) && (objectInstanceID >= 0) && (objectInstanceID < DIGITAL_INPUTS)) + { + digitalInputs[objectInstanceID].Counter += 1; + AwaStaticClient_ResourceChanged(awaClient, IPSO_DIGITAL_INPUT_OBJECT, objectInstanceID, IPSO_DIGITAL_INPUT_COUNTER); + return 0; + } + else + { + printf("Invalid arguments passed to %s", __func__); + return 1; + } } diff --git a/lwm2m-client-ipso-digital-input.h b/lwm2m-client-ipso-digital-input.h index b2f1676..ff65581 100644 --- a/lwm2m-client-ipso-digital-input.h +++ b/lwm2m-client-ipso-digital-input.h @@ -36,10 +36,29 @@ #ifndef LWM2M_CLIENT_IPSO_DIGITAL_INPUT_H_ #define LWM2M_CLIENT_IPSO_DIGITAL_INPUT_H_ -#include "lwm2m_core.h" +#include "awa/static.h" -int DigitalInput_RegisterDigitalInputObject(Lwm2mContextType * context); -int DigitalInput_AddDigitialInput(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID); -int DigitalInput_IncrementCounter(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID); +/*************************************************************************************************** + * Macros + **************************************************************************************************/ + +#define IPSO_DIGITAL_INPUT_OBJECT 3200 +#define IPSO_DIGITAL_INPUT_STATE 5500 +#define IPSO_DIGITAL_INPUT_COUNTER 5501 +#define IPSO_DIGITAL_INPUT_POLARITY 5502 +#define IPSO_DIGITAL_INPUT_DEBOUNCE_PERIOD 5503 +#define IPSO_DIGITAL_INPUT_EDGE_SELECTION 5504 +#define IPSO_DIGITAL_INPUT_COUNTER_RESET 5505 +#define IPSO_APPLICATION_TYPE 5750 +#define IPSO_SENSOR_TYPE 5751 + +#define DIGITAL_INPUTS 2 + +/*************************************************************************************************** + * Functions + **************************************************************************************************/ + +int DefineDigitalInputObject(AwaStaticClient *awaClient); +int DigitalInput_IncrementCounter(AwaStaticClient *awaClient, AwaObjectInstanceID objectInstanceID); #endif /* LWM2M_CLIENT_IPSO_DIGITAL_INPUT_H_ */ From 9debace44036eaa03bc9de8f3d72de0b6af1ff95 Mon Sep 17 00:00:00 2001 From: Rahul Daga Date: Mon, 9 May 2016 19:01:52 +0530 Subject: [PATCH 12/14] minor fixes in digital input lib Signed-off-by: Rahul Daga --- lwm2m-client-ipso-digital-input.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lwm2m-client-ipso-digital-input.c b/lwm2m-client-ipso-digital-input.c index 1c78264..cb60979 100644 --- a/lwm2m-client-ipso-digital-input.c +++ b/lwm2m-client-ipso-digital-input.c @@ -50,9 +50,9 @@ typedef struct { - bool State; + AwaBoolean State; AwaInteger Counter; - bool Polarity; + AwaBoolean Polarity; AwaTime DebouncePeriod; AwaInteger EdgeSelection; char ApplicationType[128]; @@ -140,12 +140,12 @@ static AwaResult digitalInputHandler(AwaStaticClient *client, AwaOperation opera case IPSO_APPLICATION_TYPE: *dataPointer = digitalInputs[objectInstanceID].ApplicationType; - *dataSize = sizeof(digitalInputs[objectInstanceID].ApplicationType) ; + *dataSize = strlen(digitalInputs[objectInstanceID].ApplicationType) ; break; case IPSO_SENSOR_TYPE: *dataPointer = digitalInputs[objectInstanceID].SensorType; - *dataSize = sizeof(digitalInputs[objectInstanceID].SensorType) ; + *dataSize = strlen(digitalInputs[objectInstanceID].SensorType) ; break; default: @@ -190,7 +190,7 @@ static AwaResult digitalInputHandler(AwaStaticClient *client, AwaOperation opera { result = AwaResult_BadRequest; } - break; + break; case IPSO_SENSOR_TYPE: if(*dataSize < sizeof(digitalInputs[objectInstanceID].SensorType)) @@ -220,6 +220,8 @@ static AwaResult digitalInputHandler(AwaStaticClient *client, AwaOperation opera int DefineDigitalInputObject(AwaStaticClient *awaClient) { AwaError error; + int i; + error = AwaStaticClient_DefineObjectWithHandler(awaClient, "DigitalInput", IPSO_DIGITAL_INPUT_OBJECT, 0, DIGITAL_INPUTS, digitalInputHandler); if (error != AwaError_Success) { @@ -227,7 +229,7 @@ int DefineDigitalInputObject(AwaStaticClient *awaClient) return 1; } - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "State", IPSO_DIGITAL_INPUT_OBJECT, IPSO_DIGITAL_INPUT_STATE, AwaResourceType_Boolean, 0, 1, AwaResourceOperations_ReadOnly, digitalInputHandler); + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "State", IPSO_DIGITAL_INPUT_OBJECT, IPSO_DIGITAL_INPUT_STATE, AwaResourceType_Boolean, 1, 1, AwaResourceOperations_ReadOnly, digitalInputHandler); if (error != AwaError_Success) { printf("Failed to define State resource\n"); @@ -272,7 +274,7 @@ int DefineDigitalInputObject(AwaStaticClient *awaClient) error = AwaStaticClient_DefineResourceWithHandler(awaClient, "SensorType", IPSO_DIGITAL_INPUT_OBJECT , IPSO_SENSOR_TYPE, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadOnly, digitalInputHandler); if (error != AwaError_Success) { - printf("Failed to define SensoryType resource\n"); + printf("Failed to define SensorType resource\n"); return 1; } @@ -282,6 +284,12 @@ int DefineDigitalInputObject(AwaStaticClient *awaClient) printf("Failed to define CounterReset resource\n"); return 1; } + + for (i = 0; i < DIGITAL_INPUTS; i++) + { + AwaStaticClient_CreateObjectInstance(awaClient, IPSO_DIGITAL_INPUT_OBJECT, i); + AwaStaticClient_CreateResource(awaClient, IPSO_DIGITAL_INPUT_OBJECT, i, IPSO_DIGITAL_INPUT_COUNTER); + } return 0; } From d0641d761dfe799646af856f442936eec76777d4 Mon Sep 17 00:00:00 2001 From: Rahul Daga Date: Mon, 9 May 2016 19:04:28 +0530 Subject: [PATCH 13/14] update ipso light control object to use static api Signed-off-by: Rahul Daga --- Makefile.libobjects | 3 +- lwm2m-client-ipso-light-control.c | 628 ++++++++++++------------------ lwm2m-client-ipso-light-control.h | 12 +- 3 files changed, 263 insertions(+), 380 deletions(-) diff --git a/Makefile.libobjects b/Makefile.libobjects index f2765c2..5e88e27 100644 --- a/Makefile.libobjects +++ b/Makefile.libobjects @@ -1,3 +1,2 @@ libobjects_src = lwm2m-client-flow-object.c lwm2m-client-flow-access-object.c lwm2m-client-device-object.c \ - lwm2m-client-ipso-digital-input.c - #lwm2m-client-ipso-light-control.c + lwm2m-client-ipso-digital-input.c lwm2m-client-ipso-light-control.c diff --git a/lwm2m-client-ipso-light-control.c b/lwm2m-client-ipso-light-control.c index 3d7e1e2..21476de 100644 --- a/lwm2m-client-ipso-light-control.c +++ b/lwm2m-client-ipso-light-control.c @@ -41,34 +41,24 @@ #include #include #include -#include "lwm2m_core.h" -#include "coap_abstraction.h" + +#include "awa/static.h" #include "lwm2m-client-ipso-light-control.h" /*************************************************************************************************** * Definitions **************************************************************************************************/ -#define IPSO_LIGHT_CONTROL_OBJECT 3311 -#define IPSO_LIGHT_CONTROL_ON_OFF 5850 -#define IPSO_LIGHT_CONTROL_DIMMER 5851 -#define IPSO_LIGHT_CONTROL_COLOUR 5706 -#define IPSO_LIGHT_CONTROL_UNITS 5701 -#define IPSO_LIGHT_CONTROL_ON_TIME 5852 -#define IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER 5805 -#define IPSO_LIGHT_CONTROL_POWER_FACTOR 5820 - -#define LIGHT_CONTROLS 2 - -#define MAX_STR_SIZE 64 +#define IPSO_LIGHT_CONTROL_OBJECT 3311 +#define IPSO_LIGHT_CONTROL_ON_OFF 5850 +#define IPSO_LIGHT_CONTROL_DIMMER 5851 +#define IPSO_LIGHT_CONTROL_COLOUR 5706 +#define IPSO_LIGHT_CONTROL_UNITS 5701 +#define IPSO_LIGHT_CONTROL_ON_TIME 5852 +#define IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER 5805 +#define IPSO_LIGHT_CONTROL_POWER_FACTOR 5820 -#define REGISTER_LIGHT_CONTROL_RESOURCE(context, name, id, type, minInstances, operations) \ - REGISTER_RESOURCE(context, name, IPSO_LIGHT_CONTROL_OBJECT, id, type, \ - MultipleInstancesEnum_Single, minInstances, operations, \ - &LightControlResourceOperationHandlers) - -#define CREATE_LIGHT_CONTROL_OPTIONAL_RESOURCE(context, objectInstanceId, resourcId) \ - CREATE_OPTIONAL_RESOURCE(context, IPSO_LIGHT_CONTROL_OBJECT, objectInstanceId, resourcId) +#define LIGHT_CONTROLS 2 /*************************************************************************************************** * Typedefs @@ -76,375 +66,271 @@ typedef struct { - bool OnOff; - int64_t Dimmer; - char Colour[MAX_STR_SIZE]; - char Units[MAX_STR_SIZE]; - int64_t OnTime; - float CumulativeActivePower; - float PowerFactor; - LightControlCallBack callback; - void * context; + AwaBoolean OnOff; + AwaInteger Dimmer; + char Colour[64]; + char Units[64]; + AwaInteger OnTime; + AwaFloat CumulativeActivePower; + AwaFloat PowerFactor; + LightControlCallBack callback; + void *context; } IPSOLightControl; -/*************************************************************************************************** - * Prototypes - **************************************************************************************************/ - -static int LightControl_ResourceReadHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen); - -static int LightControl_ResourceGetLengthHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID); - -static int LightControl_ResourceWriteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, - bool * changed); - - -static int LightControl_ResourceCreateHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); - -static int LightControl_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID); - -static int LightControl_ObjectDeleteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID); - /*************************************************************************************************** * Globals **************************************************************************************************/ -static ObjectOperationHandlers LightControlObjectOperationHandlers = -{ - .CreateInstance = LightControl_ObjectCreateInstanceHandler, - .Delete = LightControl_ObjectDeleteHandler, -}; - -static ResourceOperationHandlers LightControlResourceOperationHandlers = -{ - .Read = LightControl_ResourceReadHandler, - .GetLength = LightControl_ResourceGetLengthHandler, - .Write = LightControl_ResourceWriteHandler, - .CreateOptionalResource = LightControl_ResourceCreateHandler, - .Execute = NULL, -}; - -static IPSOLightControl LightControls[LIGHT_CONTROLS]; +static IPSOLightControl lightControls[LIGHT_CONTROLS]; /*************************************************************************************************** * Implementation **************************************************************************************************/ -static int LightControl_ObjectCreateInstanceHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID) +static AwaResult lightControlHandler(AwaStaticClient *client, AwaOperation operation, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID, + AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, size_t *dataSize, bool *changed) { - if(objectInstanceID >= LIGHT_CONTROLS) - { - Lwm2m_Error("LightControl_ResourceCreateHandler instance number %d out of range (max %d)", - objectInstanceID, LIGHT_CONTROLS - 1); - return -1; - } - - return objectInstanceID; + AwaResult result = AwaResult_InternalError; + bool callCallback = false; + if (!((objectID == IPSO_LIGHT_CONTROL_OBJECT) && (objectInstanceID >= 0) && (objectInstanceID < LIGHT_CONTROLS))) + { + printf("Incorrect object data\n"); + return result; + } + + switch (operation) + { + case AwaOperation_DeleteObjectInstance: + result = AwaResult_SuccessDeleted; + memset(&lightControls[objectInstanceID], 0, sizeof(lightControls[objectInstanceID])); + break; + + case AwaOperation_CreateObjectInstance: + result = AwaResult_SuccessCreated; + memset(&lightControls[objectInstanceID], 0, sizeof(lightControls[objectInstanceID])); + break; + + case AwaOperation_CreateResource: + result = AwaResult_SuccessCreated; + break; + + case AwaOperation_Read: + result = AwaResult_SuccessContent; + switch (resourceID) + { + case IPSO_LIGHT_CONTROL_ON_OFF: + *dataPointer = &lightControls[objectInstanceID].OnOff; + *dataSize = sizeof(lightControls[objectInstanceID].OnOff) ; + break; + + case IPSO_LIGHT_CONTROL_DIMMER: + *dataPointer = &lightControls[objectInstanceID].Dimmer; + *dataSize = sizeof(lightControls[objectInstanceID].Dimmer) ; + break; + + case IPSO_LIGHT_CONTROL_COLOUR: + *dataPointer = lightControls[objectInstanceID].Colour; + *dataSize = sizeof(lightControls[objectInstanceID].Colour) ; + break; + + case IPSO_LIGHT_CONTROL_UNITS: + *dataPointer = lightControls[objectInstanceID].Units; + *dataSize = sizeof(lightControls[objectInstanceID].Units) ; + break; + + case IPSO_LIGHT_CONTROL_ON_TIME: + *dataPointer = &lightControls[objectInstanceID].OnTime; + *dataSize = sizeof(lightControls[objectInstanceID].OnTime) ; + break; + + case IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER: + *dataPointer = &lightControls[objectInstanceID].CumulativeActivePower; + *dataSize = sizeof(lightControls[objectInstanceID].CumulativeActivePower) ; + break; + + case IPSO_LIGHT_CONTROL_POWER_FACTOR: + *dataPointer = &lightControls[objectInstanceID].PowerFactor; + *dataSize = sizeof(lightControls[objectInstanceID].PowerFactor) ; + break; + + default: + printf("\n Invalid resource ID for LightControl read operation"); + result = AwaResult_InternalError; + break; + } + break; + + case AwaOperation_Write: + *changed = true; + result = AwaResult_SuccessChanged; + switch (resourceID) + { + case IPSO_LIGHT_CONTROL_ON_OFF: + lightControls[objectInstanceID].OnOff = *((AwaBoolean *)*dataPointer); + callCallback = true; + break; + + case IPSO_LIGHT_CONTROL_DIMMER: + lightControls[objectInstanceID].Dimmer = *((AwaInteger *)*dataPointer); + callCallback = true; + break; + + case IPSO_LIGHT_CONTROL_COLOUR: + if(*dataSize < sizeof(lightControls[objectInstanceID].Colour)) + { + memcpy(lightControls[objectInstanceID].Colour, *dataPointer, *dataSize); + lightControls[objectInstanceID].Colour[*dataSize] = '\0'; + callCallback = true; + } + else + { + result = AwaResult_BadRequest; + } + break; + + case IPSO_LIGHT_CONTROL_UNITS: + if(*dataSize < sizeof(lightControls[objectInstanceID].Units)) + { + memcpy(lightControls[objectInstanceID].Units, *dataPointer, *dataSize); + lightControls[objectInstanceID].Units[*dataSize] = '\0'; + } + else + { + result = AwaResult_BadRequest; + } + break; + + case IPSO_LIGHT_CONTROL_ON_TIME: + lightControls[objectInstanceID].OnTime = *((AwaInteger *)*dataPointer); + break; + + case IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER: + lightControls[objectInstanceID].CumulativeActivePower = *((AwaFloat *)*dataPointer); + break; + + case IPSO_LIGHT_CONTROL_POWER_FACTOR: + lightControls[objectInstanceID].PowerFactor = *((AwaFloat *)*dataPointer); + break; + + default: + printf("\n Invalid resource ID for LightControl write operation"); + result = AwaResult_InternalError; + break; + } + if (lightControls[objectInstanceID].callback != NULL && callCallback) + { + lightControls[objectInstanceID].callback(lightControls[objectInstanceID].context, + lightControls[objectInstanceID].OnOff, lightControls[objectInstanceID].Dimmer, + lightControls[objectInstanceID].Colour); + } + break; + default: + printf("LightControl - unknown operation\n"); + break; + } + return result; } -static int LightControl_ResourceCreateHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) -{ - return 0; -} - -static int LightControl_ObjectDeleteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID) -{ - if (objectID != IPSO_LIGHT_CONTROL_OBJECT) - { - Lwm2m_Error("LightControl_ObjectDeleteHandler Invalid OIR: %d/%d/%d\n", objectID, - objectInstanceID, resourceID); - return -1; - } - - if(objectInstanceID >= LIGHT_CONTROLS) - { - Lwm2m_Error("LightControl_ObjectDeleteHandler instance number %d out of range (max %d)", - objectInstanceID, LIGHT_CONTROLS - 1); - return -1; - } - - if (resourceID == -1) - { - memset(&LightControls[objectInstanceID], 0, sizeof(IPSOLightControl)); - } - else - { - //TODO - } - - return 0; -} - -static int LightControl_ResourceReadHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * destBuffer, int destBufferLen) -{ - int result = 0; - - switch (resourceID) - { - case IPSO_LIGHT_CONTROL_ON_OFF: - result = sizeof(LightControls[objectInstanceID].OnOff); - memcpy(destBuffer, &LightControls[objectInstanceID].OnOff, result); - break; - - case IPSO_LIGHT_CONTROL_DIMMER: - result = sizeof(LightControls[objectInstanceID].Dimmer); - memcpy(destBuffer, &LightControls[objectInstanceID].Dimmer, result); - break; - - case IPSO_LIGHT_CONTROL_COLOUR: - result = strlen(LightControls[objectInstanceID].Colour) + 1; - memcpy(destBuffer, LightControls[objectInstanceID].Colour, result); - break; - - case IPSO_LIGHT_CONTROL_UNITS: - result = strlen(LightControls[objectInstanceID].Units) + 1; - memcpy(destBuffer, LightControls[objectInstanceID].Units, result); - break; - - case IPSO_LIGHT_CONTROL_ON_TIME: - result = sizeof(LightControls[objectInstanceID].OnTime); - memcpy(destBuffer, &LightControls[objectInstanceID].OnTime, result); - break; - - case IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER: - result = sizeof(LightControls[objectInstanceID].CumulativeActivePower); - memcpy(destBuffer, &LightControls[objectInstanceID].CumulativeActivePower, result); - break; - - case IPSO_LIGHT_CONTROL_POWER_FACTOR: - result = sizeof(LightControls[objectInstanceID].PowerFactor); - memcpy(destBuffer, &LightControls[objectInstanceID].PowerFactor, result); - break; - - default: - result = -1; - break; - } - - return result; -} - -static int LightControl_ResourceGetLengthHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID) -{ - int result = 0; - - switch (resourceID) - { - case IPSO_LIGHT_CONTROL_ON_OFF: - result = sizeof(LightControls[objectInstanceID].OnOff); - break; - - case IPSO_LIGHT_CONTROL_DIMMER: - result = sizeof(LightControls[objectInstanceID].Dimmer); - break; - - case IPSO_LIGHT_CONTROL_COLOUR: - result = strlen(LightControls[objectInstanceID].Colour) + 1; - break; - - case IPSO_LIGHT_CONTROL_UNITS: - result = strlen(LightControls[objectInstanceID].Units) + 1; - break; - - case IPSO_LIGHT_CONTROL_ON_TIME: - result = sizeof(LightControls[objectInstanceID].OnTime); - break; - - case IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER: - result = sizeof(LightControls[objectInstanceID].CumulativeActivePower); - break; - - case IPSO_LIGHT_CONTROL_POWER_FACTOR: - result = sizeof(LightControls[objectInstanceID].PowerFactor); - break; - - default: - result = -1; - break; - } - - return result; -} - -static int LightControl_ResourceWriteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, bool * changed) -{ - int result; - bool CallCallback = false; - - switch(resourceID) - { - case IPSO_LIGHT_CONTROL_ON_OFF: - result = srcBufferLen; - memcpy(&LightControls[objectInstanceID].OnOff, srcBuffer, result); - CallCallback = true; - break; - - case IPSO_LIGHT_CONTROL_DIMMER: - result = srcBufferLen; - memcpy(&LightControls[objectInstanceID].Dimmer, srcBuffer, result); - CallCallback = true; - break; - - case IPSO_LIGHT_CONTROL_COLOUR: - result = srcBufferLen; - if(result < sizeof(LightControls[objectInstanceID].Colour)) - { - memcpy(LightControls[objectInstanceID].Colour, srcBuffer, result); - } - else - { - Lwm2m_Error("LightControl_ResourceWriteHandler Colour Type string too long: %d", - result); - result = -1; - } - CallCallback = true; - break; - - case IPSO_LIGHT_CONTROL_UNITS: - result = srcBufferLen; - if(result < sizeof(LightControls[objectInstanceID].Units)) - { - memcpy(LightControls[objectInstanceID].Units, srcBuffer, result); - } - else - { - Lwm2m_Error("LightControl_ResourceWriteHandler Units Type string too long: %d", - result); - result = -1; - } - break; - - case IPSO_LIGHT_CONTROL_ON_TIME: - result = srcBufferLen; - memcpy(&LightControls[objectInstanceID].OnTime, srcBuffer, result); - break; - - case IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER: - result = srcBufferLen; - memcpy(&LightControls[objectInstanceID].CumulativeActivePower, srcBuffer, result); - break; - - case IPSO_LIGHT_CONTROL_POWER_FACTOR: - result = srcBufferLen; - memcpy(&LightControls[objectInstanceID].PowerFactor, srcBuffer, result); - break; - default: - - result = -1; - break; - } - - if (LightControls[objectInstanceID].callback != NULL && CallCallback) - { - LightControls[objectInstanceID].callback(LightControls[objectInstanceID].context, - LightControls[objectInstanceID].OnOff, LightControls[objectInstanceID].Dimmer, - LightControls[objectInstanceID].Colour); - } - - - if(result > 0) - *changed = true; - - return result; -} - -/*************************************************************************************************** - * Implementation - Public - **************************************************************************************************/ - -int LightControl_RegisterLightControlObject(Lwm2mContextType * context) +int DefineLightControlObject(AwaStaticClient *awaClient) { - REGISTER_OBJECT(context, "LightControl", IPSO_LIGHT_CONTROL_OBJECT, \ - MultipleInstancesEnum_Multiple, MandatoryEnum_Optional, \ - &LightControlObjectOperationHandlers); - - REGISTER_LIGHT_CONTROL_RESOURCE(context, "On/Off", IPSO_LIGHT_CONTROL_ON_OFF, \ - ResourceTypeEnum_TypeBoolean, MandatoryEnum_Mandatory, Operations_RW); - REGISTER_LIGHT_CONTROL_RESOURCE(context, "Dimmer", IPSO_LIGHT_CONTROL_DIMMER, \ - ResourceTypeEnum_TypeInteger, MandatoryEnum_Optional, Operations_RW); - REGISTER_LIGHT_CONTROL_RESOURCE(context, "Colour", IPSO_LIGHT_CONTROL_COLOUR, \ - ResourceTypeEnum_TypeString, MandatoryEnum_Optional, Operations_RW); - REGISTER_LIGHT_CONTROL_RESOURCE(context, "Units", IPSO_LIGHT_CONTROL_UNITS, \ - ResourceTypeEnum_TypeString, MandatoryEnum_Mandatory, Operations_R); - REGISTER_LIGHT_CONTROL_RESOURCE(context, "OnTime", IPSO_LIGHT_CONTROL_ON_TIME, \ - ResourceTypeEnum_TypeInteger, MandatoryEnum_Optional, Operations_RW); - REGISTER_LIGHT_CONTROL_RESOURCE(context, "CumulativeActivePower", \ - IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER, ResourceTypeEnum_TypeFloat, \ - MandatoryEnum_Optional, Operations_R); - REGISTER_LIGHT_CONTROL_RESOURCE(context, "PowerFactor", IPSO_LIGHT_CONTROL_POWER_FACTOR, \ - ResourceTypeEnum_TypeFloat, MandatoryEnum_Optional, Operations_R); - - return 0; + AwaError error; + error = AwaStaticClient_DefineObjectWithHandler(awaClient, "LightControl", IPSO_LIGHT_CONTROL_OBJECT, 0, LIGHT_CONTROLS, lightControlHandler); + if (error != AwaError_Success) + { + printf("Failed to register light control object\n"); + return -1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "On/Off", IPSO_LIGHT_CONTROL_OBJECT, IPSO_LIGHT_CONTROL_ON_OFF, AwaResourceType_Boolean, 1, 1, AwaResourceOperations_ReadWrite, lightControlHandler); + if (error != AwaError_Success) + { + printf("Failed to define On/Off resource\n"); + return -1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Dimmer", IPSO_LIGHT_CONTROL_OBJECT, IPSO_LIGHT_CONTROL_DIMMER, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, lightControlHandler); + if (error != AwaError_Success) + { + printf("Failed to define Dimmer resource\n"); + return -1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Colour", IPSO_LIGHT_CONTROL_OBJECT, IPSO_LIGHT_CONTROL_COLOUR, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, lightControlHandler); + if (error != AwaError_Success) + { + printf("Failed to define Colour resource\n"); + return -1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Units", IPSO_LIGHT_CONTROL_OBJECT, IPSO_LIGHT_CONTROL_UNITS, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadOnly, lightControlHandler); + if (error != AwaError_Success) + { + printf("Failed to define Units resource\n"); + return -1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "OnTime", IPSO_LIGHT_CONTROL_OBJECT, IPSO_LIGHT_CONTROL_ON_TIME, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, lightControlHandler); + if (error != AwaError_Success) + { + printf("Failed to define OnTime resource\n"); + return -1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CumulativeActivePower", IPSO_LIGHT_CONTROL_OBJECT, IPSO_LIGHT_CONTROL_CUMULATIVE_ACTIVE_POWER, AwaResourceType_Float, 0, 1, AwaResourceOperations_ReadOnly, lightControlHandler); + if (error != AwaError_Success) + { + printf("Failed to define CumulativeActivePower resource\n"); + return -1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "PowerFactor", IPSO_LIGHT_CONTROL_OBJECT , IPSO_LIGHT_CONTROL_POWER_FACTOR, AwaResourceType_Float, 0, 1, AwaResourceOperations_ReadOnly, lightControlHandler); + if (error != AwaError_Success) + { + printf("Failed to define PowerFactor resource\n"); + return -1; + } + + return 0; } -int LightControl_AddLightControl(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID, - LightControlCallBack callback, void * callbackContext) +int LightControl_AddLightControl(AwaStaticClient *awaClient, ObjectInstanceIDType objectInstanceID, + LightControlCallBack callback, void *callbackContext) { - if(objectInstanceID <= LIGHT_CONTROLS) - { - CREATE_OBJECT_INSTANCE(context, IPSO_LIGHT_CONTROL_OBJECT, objectInstanceID); - CREATE_LIGHT_CONTROL_OPTIONAL_RESOURCE(context, objectInstanceID, IPSO_LIGHT_CONTROL_ON_OFF); - CREATE_LIGHT_CONTROL_OPTIONAL_RESOURCE(context, objectInstanceID, IPSO_LIGHT_CONTROL_COLOUR); - CREATE_LIGHT_CONTROL_OPTIONAL_RESOURCE(context, objectInstanceID, IPSO_LIGHT_CONTROL_ON_TIME); - - memset(&LightControls[objectInstanceID], 0, sizeof(IPSOLightControl)); - snprintf(LightControls[objectInstanceID].Colour, MAX_STR_SIZE, "Red%d", objectInstanceID+1); - - LightControls[objectInstanceID].callback = callback; - LightControls[objectInstanceID].context = callbackContext; - - bool state = false; - - if (Lwm2mCore_SetResourceInstanceValue(context, IPSO_LIGHT_CONTROL_OBJECT, objectInstanceID, - IPSO_LIGHT_CONTROL_ON_OFF, 0, &state, sizeof(state)) == -1) - { - Lwm2m_Error("Failed to set On/Off resource to %s", state ? "true" : "false"); - return -1; - } - } - else - { - Lwm2m_Error("%d instance of Light Control exceeds max instances %d\n", objectInstanceID, - LIGHT_CONTROLS); - return -1; - } - return 0; + if ((awaClient == NULL) || (objectInstanceID < 0) || (objectInstanceID >= LIGHT_CONTROLS)) + { + printf("Invalid arguments passed to %s", __func__); + return -1; + } + + AwaStaticClient_CreateObjectInstance(awaClient, IPSO_LIGHT_CONTROL_OBJECT, objectInstanceID); + AwaStaticClient_CreateResource(awaClient, IPSO_LIGHT_CONTROL_OBJECT, objectInstanceID, IPSO_LIGHT_CONTROL_COLOUR); + AwaStaticClient_CreateResource(awaClient, IPSO_LIGHT_CONTROL_OBJECT, objectInstanceID, IPSO_LIGHT_CONTROL_ON_TIME); + lightControls[objectInstanceID].OnOff = false; + snprintf(lightControls[objectInstanceID].Colour, sizeof(lightControls[objectInstanceID].Colour), "Red%d", objectInstanceID+1); + lightControls[objectInstanceID].callback = callback; + lightControls[objectInstanceID].context = callbackContext; + if (callback != NULL) + { + lightControls[objectInstanceID].callback(lightControls[objectInstanceID].context, + lightControls[objectInstanceID].OnOff, lightControls[objectInstanceID].Dimmer, + lightControls[objectInstanceID].Colour); + } + return 0; } -int LightControl_IncrementOnTime(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID, - int seconds) +int LightControl_IncrementOnTime(AwaStaticClient *awaClient, ObjectInstanceIDType objectInstanceID, AwaInteger seconds) { - //only increment on time if it is on. - if(LightControls[objectInstanceID].OnOff == true) - { - int64_t OnTime = LightControls[objectInstanceID].OnTime + seconds; - if (Lwm2mCore_SetResourceInstanceValue(context, IPSO_LIGHT_CONTROL_OBJECT, objectInstanceID, - IPSO_LIGHT_CONTROL_ON_TIME, 0, &OnTime, sizeof(OnTime)) == -1) - { - Lwm2m_Error("Failed to increment ON Time resource\n"); - return -1; - } - } - else - return -1; - - return 0; + if ((awaClient == NULL) || (objectInstanceID < 0) || (objectInstanceID >= LIGHT_CONTROLS)) + { + printf("Invalid arguments passed to %s", __func__); + return -1; + } + + if(lightControls[objectInstanceID].OnOff != true) + { + return -1; + } + + lightControls[objectInstanceID].OnTime += seconds; + return 0; } diff --git a/lwm2m-client-ipso-light-control.h b/lwm2m-client-ipso-light-control.h index 10b8880..dbeed14 100644 --- a/lwm2m-client-ipso-light-control.h +++ b/lwm2m-client-ipso-light-control.h @@ -39,12 +39,10 @@ #include "lwm2m_core.h" -typedef void (*LightControlCallBack)(void * context, bool OnOff, unsigned char Dimmer, - const char * Colour); -int LightControl_RegisterLightControlObject(Lwm2mContextType * context); -int LightControl_AddLightControl(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID, - LightControlCallBack callback, void * callbackContext); -int LightControl_IncrementOnTime(Lwm2mContextType * context, ObjectInstanceIDType objectInstanceID, - int seconds); +typedef void (*LightControlCallBack)(void *context, bool OnOff, unsigned char Dimmer, const char *Colour); +int DefineLightControlObject(AwaStaticClient *awaClient); +int LightControl_AddLightControl(AwaStaticClient *awaClient, ObjectInstanceIDType objectInstanceID, + LightControlCallBack callback, void *callbackContext); +int LightControl_IncrementOnTime(AwaStaticClient *awaClient, ObjectInstanceIDType objectInstanceID, AwaInteger seconds); #endif /* LWM2M_CLIENT_IPSO_LIGHT_CONTROL_H_ */ From b5668dad8219feeac50b7b24689fad01c2e123ed Mon Sep 17 00:00:00 2001 From: Rahul Daga Date: Wed, 11 May 2016 18:30:55 +0530 Subject: [PATCH 14/14] fix indentation in flow object and flow access object files Signed-off-by: Rahul Daga --- lwm2m-client-flow-access-object.c | 332 ++++++++--------- lwm2m-client-flow-object.c | 595 +++++++++++++++--------------- 2 files changed, 463 insertions(+), 464 deletions(-) diff --git a/lwm2m-client-flow-access-object.c b/lwm2m-client-flow-access-object.c index 5748da2..fd16b7a 100644 --- a/lwm2m-client-flow-access-object.c +++ b/lwm2m-client-flow-access-object.c @@ -49,23 +49,23 @@ * Definitions **************************************************************************************************/ -#define FLOWM2M_FLOW_ACCESS_OBJECT 20001 -#define FLOWM2M_FLOW_ACCESS_OBJECT_URL 0 -#define FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY 1 -#define FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET 2 -#define FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN 3 -#define FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY 4 +#define FLOWM2M_FLOW_ACCESS_OBJECT 20001 +#define FLOWM2M_FLOW_ACCESS_OBJECT_URL 0 +#define FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY 1 +#define FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET 2 +#define FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN 3 +#define FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY 4 #define FLOW_ACCESS_INSTANCES 1 typedef struct { - char URL[64]; - char CustomerKey[64]; - char CustomerSecret[64]; - char RememberMeToken[64]; - AwaTime RememberMeTokenExpiry; + char URL[64]; + char CustomerKey[64]; + char CustomerSecret[64]; + char RememberMeToken[64]; + AwaTime RememberMeTokenExpiry; } FlowAccessObject; FlowAccessObject flowAccess[FLOW_ACCESS_INSTANCES]; @@ -75,171 +75,171 @@ FlowAccessObject flowAccess[FLOW_ACCESS_INSTANCES]; **************************************************************************************************/ AwaResult accessHandler(AwaStaticClient *client, AwaOperation operation, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID, - AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, size_t *dataSize, bool *changed) + AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, size_t *dataSize, bool *changed) { - AwaResult result = AwaResult_InternalError; + AwaResult result = AwaResult_InternalError; - if (!((objectID == FLOWM2M_FLOW_ACCESS_OBJECT) && (objectInstanceID >= 0) && (objectInstanceID < FLOW_ACCESS_INSTANCES))) - { - printf("incorrect flow access object data\n"); - return result; - } + if (!((objectID == FLOWM2M_FLOW_ACCESS_OBJECT) && (objectInstanceID >= 0) && (objectInstanceID < FLOW_ACCESS_INSTANCES))) + { + printf("Incorrect flow access object data\n"); + return result; + } - switch (operation) - { + switch (operation) + { case AwaOperation_DeleteObjectInstance: result = AwaResult_SuccessDeleted; memset(&flowAccess[objectInstanceID], 0, sizeof(flowAccess[objectInstanceID])); break; - case AwaOperation_CreateObjectInstance: - result = AwaResult_SuccessCreated; - memset(&flowAccess[objectInstanceID], 0, sizeof(flowAccess[objectInstanceID])); - break; - - case AwaOperation_CreateResource: - result = AwaResult_SuccessCreated; - break; - - case AwaOperation_Read: - switch (resourceID) - { - case FLOWM2M_FLOW_ACCESS_OBJECT_URL: - *dataPointer = flowAccess[objectInstanceID].URL; - *dataSize = strlen(flowAccess[objectInstanceID].URL) ; - result = AwaResult_SuccessContent; - break; - - case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: - *dataPointer = flowAccess[objectInstanceID].CustomerKey; - *dataSize = strlen(flowAccess[objectInstanceID].CustomerKey); - result = AwaResult_SuccessContent; - break; - - case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: - *dataPointer = flowAccess[objectInstanceID].CustomerSecret; - *dataSize = strlen(flowAccess[objectInstanceID].CustomerSecret); - result = AwaResult_SuccessContent; - break; - - case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: - *dataPointer = flowAccess[objectInstanceID].RememberMeToken; - *dataSize = strlen(flowAccess[objectInstanceID].RememberMeToken); - result = AwaResult_SuccessContent; - break; - - case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: - *dataPointer = &flowAccess[objectInstanceID].RememberMeTokenExpiry; - *dataSize = sizeof(flowAccess[objectInstanceID].RememberMeTokenExpiry); - result = AwaResult_SuccessContent; - break; - - default: - printf("\n invalid res id for flow access - read op"); - break; - } - break; - - case AwaOperation_Write: - switch (resourceID) - { - case FLOWM2M_FLOW_ACCESS_OBJECT_URL: - memcpy(flowAccess[objectInstanceID].URL, *dataPointer, *dataSize); - flowAccess[objectInstanceID].URL[*dataSize] = '\0'; - *changed = true; - result = AwaResult_SuccessChanged; - break; - - case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: - memcpy(flowAccess[objectInstanceID].CustomerKey,*dataPointer, *dataSize); - flowAccess[objectInstanceID].CustomerKey[*dataSize] = '\0'; - *changed = true; - result = AwaResult_SuccessChanged; - break; - - case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: - memcpy(flowAccess[objectInstanceID].CustomerSecret, *dataPointer, *dataSize); - flowAccess[objectInstanceID].CustomerSecret[*dataSize] = '\0'; - *changed = true; - result = AwaResult_SuccessChanged; - break; - - case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: - memcpy(flowAccess[objectInstanceID].RememberMeToken, *dataPointer, *dataSize); - flowAccess[objectInstanceID].RememberMeToken[*dataSize] = '\0'; - *changed = true; - result = AwaResult_SuccessChanged; - break; - - case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: - flowAccess[objectInstanceID].RememberMeTokenExpiry = *((AwaTime *)*dataPointer); - *changed = true; - result = AwaResult_SuccessChanged; - break; - - default: - printf("invalid res id %d for flow access - write op\n", resourceID); - break; - } - break; - - default: - printf("flow access - unknown operation\n"); - break; - } - return result; + case AwaOperation_CreateObjectInstance: + result = AwaResult_SuccessCreated; + memset(&flowAccess[objectInstanceID], 0, sizeof(flowAccess[objectInstanceID])); + break; + + case AwaOperation_CreateResource: + result = AwaResult_SuccessCreated; + break; + + case AwaOperation_Read: + switch (resourceID) + { + case FLOWM2M_FLOW_ACCESS_OBJECT_URL: + *dataPointer = flowAccess[objectInstanceID].URL; + *dataSize = strlen(flowAccess[objectInstanceID].URL) ; + result = AwaResult_SuccessContent; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: + *dataPointer = flowAccess[objectInstanceID].CustomerKey; + *dataSize = strlen(flowAccess[objectInstanceID].CustomerKey); + result = AwaResult_SuccessContent; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: + *dataPointer = flowAccess[objectInstanceID].CustomerSecret; + *dataSize = strlen(flowAccess[objectInstanceID].CustomerSecret); + result = AwaResult_SuccessContent; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: + *dataPointer = flowAccess[objectInstanceID].RememberMeToken; + *dataSize = strlen(flowAccess[objectInstanceID].RememberMeToken); + result = AwaResult_SuccessContent; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: + *dataPointer = &flowAccess[objectInstanceID].RememberMeTokenExpiry; + *dataSize = sizeof(flowAccess[objectInstanceID].RememberMeTokenExpiry); + result = AwaResult_SuccessContent; + break; + + default: + printf("\n Invalid res id for flow access - read op"); + break; + } + break; + + case AwaOperation_Write: + switch (resourceID) + { + case FLOWM2M_FLOW_ACCESS_OBJECT_URL: + memcpy(flowAccess[objectInstanceID].URL, *dataPointer, *dataSize); + flowAccess[objectInstanceID].URL[*dataSize] = '\0'; + *changed = true; + result = AwaResult_SuccessChanged; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY: + memcpy(flowAccess[objectInstanceID].CustomerKey,*dataPointer, *dataSize); + flowAccess[objectInstanceID].CustomerKey[*dataSize] = '\0'; + *changed = true; + result = AwaResult_SuccessChanged; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET: + memcpy(flowAccess[objectInstanceID].CustomerSecret, *dataPointer, *dataSize); + flowAccess[objectInstanceID].CustomerSecret[*dataSize] = '\0'; + *changed = true; + result = AwaResult_SuccessChanged; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN: + memcpy(flowAccess[objectInstanceID].RememberMeToken, *dataPointer, *dataSize); + flowAccess[objectInstanceID].RememberMeToken[*dataSize] = '\0'; + *changed = true; + result = AwaResult_SuccessChanged; + break; + + case FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY: + flowAccess[objectInstanceID].RememberMeTokenExpiry = *((AwaTime *)*dataPointer); + *changed = true; + result = AwaResult_SuccessChanged; + break; + + default: + printf("Invalid res id %d for flow access - write op\n", resourceID); + break; + } + break; + + default: + printf("Flow access - unknown operation\n"); + break; + } + return result; } int DefineFlowAccessObject(AwaStaticClient *awaClient) { - AwaError error; - - error = AwaStaticClient_DefineObjectWithHandler(awaClient, "FlowAccess", FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOW_ACCESS_INSTANCES, accessHandler); - if (error != AwaError_Success) - { - printf("Failed to register flow access object\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "URL", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_URL, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, - accessHandler); - if (error != AwaError_Success) - { - printf("Failed to define URL resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CustomerKey", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, - accessHandler); - if (error != AwaError_Success) - { - printf("Failed to define CustomerKey resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CustomerSecret", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, - accessHandler); - if (error != AwaError_Success) - { - printf("Failed to define CustomerSecret resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeToken", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, - accessHandler); - if (error != AwaError_Success) - { - printf("Failed to define RememberMeToken resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeTokenExpiry", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY, AwaResourceType_Time, 0, 1, AwaResourceOperations_ReadWrite, - accessHandler); - if (error != AwaError_Success) - { - printf("Failed to define RememberMeTokenExpiry resource\n"); - return 1; - } - - return 0; + AwaError error; + + error = AwaStaticClient_DefineObjectWithHandler(awaClient, "FlowAccess", FLOWM2M_FLOW_ACCESS_OBJECT, 0, FLOW_ACCESS_INSTANCES, accessHandler); + if (error != AwaError_Success) + { + printf("Failed to register flow access object\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "URL", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_URL, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + accessHandler); + if (error != AwaError_Success) + { + printf("Failed to define URL resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CustomerKey", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERKEY, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + accessHandler); + if (error != AwaError_Success) + { + printf("Failed to define CustomerKey resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "CustomerSecret", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_CUSTOMERSECRET, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + accessHandler); + if (error != AwaError_Success) + { + printf("Failed to define CustomerSecret resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeToken", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKEN, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + accessHandler); + if (error != AwaError_Success) + { + printf("Failed to define RememberMeToken resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "RememberMeTokenExpiry", FLOWM2M_FLOW_ACCESS_OBJECT, FLOWM2M_FLOW_ACCESS_OBJECT_REMEMBERMETOKENEXPIRY, AwaResourceType_Time, 0, 1, AwaResourceOperations_ReadWrite, + accessHandler); + if (error != AwaError_Success) + { + printf("Failed to define RememberMeTokenExpiry resource\n"); + return 1; + } + + return 0; } diff --git a/lwm2m-client-flow-object.c b/lwm2m-client-flow-object.c index 8dbf071..fd66ba6 100644 --- a/lwm2m-client-flow-object.c +++ b/lwm2m-client-flow-object.c @@ -51,21 +51,21 @@ * Definitions **************************************************************************************************/ -#define FLOWM2M_FLOW_OBJECT 20000 -#define FLOWM2M_FLOW_OBJECT_DEVICEID 0 -#define FLOWM2M_FLOW_OBJECT_PARENTID 1 -#define FLOWM2M_FLOW_OBJECT_DEVICETYPE 2 -#define FLOWM2M_FLOW_OBJECT_NAME 3 -#define FLOWM2M_FLOW_OBJECT_DESCRIPTION 4 -#define FLOWM2M_FLOW_OBJECT_FCAP 5 -#define FLOWM2M_FLOW_OBJECT_LICENSEEID 6 -#define FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE 7 -#define FLOWM2M_FLOW_OBJECT_HASHITERATIONS 8 -#define FLOWM2M_FLOW_OBJECT_LICENSEEHASH 9 -#define FLOWM2M_FLOW_OBJECT_STATUS 10 +#define FLOWM2M_FLOW_OBJECT 20000 +#define FLOWM2M_FLOW_OBJECT_DEVICEID 0 +#define FLOWM2M_FLOW_OBJECT_PARENTID 1 +#define FLOWM2M_FLOW_OBJECT_DEVICETYPE 2 +#define FLOWM2M_FLOW_OBJECT_NAME 3 +#define FLOWM2M_FLOW_OBJECT_DESCRIPTION 4 +#define FLOWM2M_FLOW_OBJECT_FCAP 5 +#define FLOWM2M_FLOW_OBJECT_LICENSEEID 6 +#define FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE 7 +#define FLOWM2M_FLOW_OBJECT_HASHITERATIONS 8 +#define FLOWM2M_FLOW_OBJECT_LICENSEEHASH 9 +#define FLOWM2M_FLOW_OBJECT_STATUS 10 -#define MAX_STRING_SIZE 64 -#define MAX_KEY_SIZE 64 +#define MAX_STRING_SIZE 64 +#define MAX_KEY_SIZE 64 @@ -82,207 +82,206 @@ static char licenseeSecret[64] = "getATTtDsNBpBRnMsN7GoQ=="; **************************************************************************************************/ typedef struct { - int8_t DeviceID[16]; - size_t DeviceIDSize; - int8_t ParentID[16]; - size_t ParentIDSize; - char DeviceType[64]; - char Name[64]; - char Description[64]; - char FCAP[64]; - AwaInteger LicenseeID; - uint8_t LicenseeChallenge[64]; - size_t LicenseeChallengeSize; - AwaInteger HashIterations; - uint8_t LicenseeHash[SHA256_HASH_LENGTH]; - size_t LicenseeHashSize; - AwaInteger Status; + int8_t DeviceID[16]; + size_t DeviceIDSize; + int8_t ParentID[16]; + size_t ParentIDSize; + char DeviceType[64]; + char Name[64]; + char Description[64]; + char FCAP[64]; + AwaInteger LicenseeID; + uint8_t LicenseeChallenge[64]; + size_t LicenseeChallengeSize; + AwaInteger HashIterations; + uint8_t LicenseeHash[SHA256_HASH_LENGTH]; + size_t LicenseeHashSize; + AwaInteger Status; } FlowObject; static FlowObject flow[FLOW_INSTANCES] = {0}; static bool CalculateLicenseeHash(char *licenseeSecret, uint8_t hash[SHA256_HASH_LENGTH], const char *challenge, int challengeLength, int iterations) { - int i; - uint8_t key[MAX_KEY_SIZE]; - int keyLen = b64Decode(key, sizeof(key), licenseeSecret, strlen(licenseeSecret)); - - if (keyLen == -1) - { - return false; - } - - HmacSha256_ComputeHash(hash, challenge, challengeLength, key, keyLen); - for (i = 1; i < iterations; i++) - { - HmacSha256_ComputeHash(hash, hash, SHA256_HASH_LENGTH, key, keyLen); - } - return true; + int i; + uint8_t key[MAX_KEY_SIZE]; + int keyLen = b64Decode(key, sizeof(key), licenseeSecret, strlen(licenseeSecret)); + + if (keyLen == -1) + { + return false; + } + + HmacSha256_ComputeHash(hash, challenge, challengeLength, key, keyLen); + for (i = 1; i < iterations; i++) + { + HmacSha256_ComputeHash(hash, hash, SHA256_HASH_LENGTH, key, keyLen); + } + return true; } AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID, - AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, size_t *dataSize, bool *changed) + AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, size_t *dataSize, bool *changed) { - AwaResult result = AwaResult_InternalError; + AwaResult result = AwaResult_InternalError; - if (!((objectID == FLOWM2M_FLOW_OBJECT) && (objectInstanceID >= 0) && (objectInstanceID < FLOW_INSTANCES))) - { - printf("incorrect flow object data\n"); - return result; - } + if (!((objectID == FLOWM2M_FLOW_OBJECT) && (objectInstanceID >= 0) && (objectInstanceID < FLOW_INSTANCES))) + { + printf("Incorrect flow object data\n"); + return result; + } - switch (operation) - { + switch (operation) + { case AwaOperation_DeleteObjectInstance: result = AwaResult_SuccessDeleted; memset(&flow[objectInstanceID], 0, sizeof(FlowObject)); break; - case AwaOperation_CreateObjectInstance: - result = AwaResult_SuccessCreated; - memset(&flow[objectInstanceID], 0, sizeof(FlowObject)); - break; - - case AwaOperation_CreateResource: - result = AwaResult_SuccessCreated; - break; - - case AwaOperation_Read: - switch (resourceID) - { - case FLOWM2M_FLOW_OBJECT_DEVICEID: - *dataPointer = flow[objectInstanceID].DeviceID; - *dataSize = flow[objectInstanceID].DeviceIDSize; - break; - - case FLOWM2M_FLOW_OBJECT_PARENTID: - *dataPointer = flow[objectInstanceID].ParentID; - *dataSize = flow[objectInstanceID].ParentIDSize; - break; - - case FLOWM2M_FLOW_OBJECT_DEVICETYPE: - *dataPointer = flow[objectInstanceID].DeviceType; - *dataSize = strlen(flow[objectInstanceID].DeviceType) ; - break; - - case FLOWM2M_FLOW_OBJECT_NAME: - *dataPointer = flow[objectInstanceID].Name; - *dataSize = strlen(flow[objectInstanceID].Name); - break; - - case FLOWM2M_FLOW_OBJECT_DESCRIPTION: - *dataPointer = flow[objectInstanceID].Description; - *dataSize = strlen(flow[objectInstanceID].Description); - break; - - case FLOWM2M_FLOW_OBJECT_FCAP: - *dataPointer = flow[objectInstanceID].FCAP; - *dataSize = strlen(flow[objectInstanceID].FCAP); - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEEID: - *dataPointer = &flow[objectInstanceID].LicenseeID; - *dataSize = sizeof(flow[objectInstanceID].LicenseeID); - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: - *dataPointer = flow[objectInstanceID].LicenseeChallenge; - *dataSize = flow[objectInstanceID].LicenseeChallengeSize; - break; - - case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: - *dataPointer = &flow[objectInstanceID].HashIterations; - *dataSize = sizeof(flow[objectInstanceID].HashIterations); - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: - *dataPointer = flow[objectInstanceID].LicenseeHash; - *dataSize = flow[objectInstanceID].LicenseeHashSize; - break; - - case FLOWM2M_FLOW_OBJECT_STATUS: - *dataPointer = &flow[objectInstanceID].Status; - *dataSize = sizeof(flow[objectInstanceID].Status); - break; - default: - printf("Invalid resource id %d\n", resourceID); - break; - } - result = AwaResult_SuccessContent; - break; - - case AwaOperation_Write: - switch (resourceID) - { - case FLOWM2M_FLOW_OBJECT_DEVICEID: - memcpy(flow[objectInstanceID].DeviceID, *dataPointer, *dataSize); - flow[objectInstanceID].DeviceIDSize = *dataSize; - break; - - case FLOWM2M_FLOW_OBJECT_PARENTID: - memcpy(flow[objectInstanceID].ParentID, *dataPointer, *dataSize); - flow[objectInstanceID].ParentIDSize = *dataSize; - break; - - case FLOWM2M_FLOW_OBJECT_DEVICETYPE: - memcpy(flow[objectInstanceID].DeviceType, *dataPointer, *dataSize); - flow[objectInstanceID].DeviceType[*dataSize] = '\0'; - break; - - case FLOWM2M_FLOW_OBJECT_NAME: - memcpy(flow[objectInstanceID].Name, *dataPointer, *dataSize); - flow[objectInstanceID].Name[*dataSize] = '\0'; - break; - - case FLOWM2M_FLOW_OBJECT_DESCRIPTION: - memcpy(flow[objectInstanceID].Description, *dataPointer, *dataSize); - flow[objectInstanceID].Description[*dataSize] = '\0'; - break; - - case FLOWM2M_FLOW_OBJECT_FCAP: - memcpy(flow[objectInstanceID].FCAP, *dataPointer, *dataSize); - flow[objectInstanceID].FCAP[*dataSize] = '\0'; - printf("FCAP = %s\n", flow[objectInstanceID].FCAP); - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEEID: - flow[objectInstanceID].LicenseeID = (*(AwaInteger*)(*dataPointer)); - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: - memcpy(flow[objectInstanceID].LicenseeChallenge, *dataPointer, *dataSize); - flow[objectInstanceID].LicenseeChallengeSize = *dataSize; - break; - - case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: - flow[objectInstanceID].HashIterations = *((AwaInteger*)(*dataPointer)); - break; - - case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: - memcpy(flow[objectInstanceID].LicenseeHash, *dataPointer, *dataSize); - flow[objectInstanceID].LicenseeHashSize = *dataSize; - break; - - case FLOWM2M_FLOW_OBJECT_STATUS: - flow[objectInstanceID].Status = *((AwaInteger*)(*dataPointer)); - printf("Status: %li\n", flow[objectInstanceID].Status); - *changed = true; - break; - - default: - printf("invalid resource id for write operation\n"); - break; - } - result = AwaResult_SuccessChanged; - break; - - default: - printf("default case for handler, operation - %d\n", operation); - break; - } - - if(operation == AwaOperation_Write) - { + case AwaOperation_CreateObjectInstance: + result = AwaResult_SuccessCreated; + memset(&flow[objectInstanceID], 0, sizeof(FlowObject)); + break; + + case AwaOperation_CreateResource: + result = AwaResult_SuccessCreated; + break; + + case AwaOperation_Read: + switch (resourceID) + { + case FLOWM2M_FLOW_OBJECT_DEVICEID: + *dataPointer = flow[objectInstanceID].DeviceID; + *dataSize = flow[objectInstanceID].DeviceIDSize; + break; + + case FLOWM2M_FLOW_OBJECT_PARENTID: + *dataPointer = flow[objectInstanceID].ParentID; + *dataSize = flow[objectInstanceID].ParentIDSize; + break; + + case FLOWM2M_FLOW_OBJECT_DEVICETYPE: + *dataPointer = flow[objectInstanceID].DeviceType; + *dataSize = strlen(flow[objectInstanceID].DeviceType) ; + break; + + case FLOWM2M_FLOW_OBJECT_NAME: + *dataPointer = flow[objectInstanceID].Name; + *dataSize = strlen(flow[objectInstanceID].Name); + break; + + case FLOWM2M_FLOW_OBJECT_DESCRIPTION: + *dataPointer = flow[objectInstanceID].Description; + *dataSize = strlen(flow[objectInstanceID].Description); + break; + + case FLOWM2M_FLOW_OBJECT_FCAP: + *dataPointer = flow[objectInstanceID].FCAP; + *dataSize = strlen(flow[objectInstanceID].FCAP); + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEEID: + *dataPointer = &flow[objectInstanceID].LicenseeID; + *dataSize = sizeof(flow[objectInstanceID].LicenseeID); + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: + *dataPointer = flow[objectInstanceID].LicenseeChallenge; + *dataSize = flow[objectInstanceID].LicenseeChallengeSize; + break; + + case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: + *dataPointer = &flow[objectInstanceID].HashIterations; + *dataSize = sizeof(flow[objectInstanceID].HashIterations); + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: + *dataPointer = flow[objectInstanceID].LicenseeHash; + *dataSize = flow[objectInstanceID].LicenseeHashSize; + break; + + case FLOWM2M_FLOW_OBJECT_STATUS: + *dataPointer = &flow[objectInstanceID].Status; + *dataSize = sizeof(flow[objectInstanceID].Status); + break; + default: + printf("Invalid resource id %d\n", resourceID); + break; + } + result = AwaResult_SuccessContent; + break; + + case AwaOperation_Write: + switch (resourceID) + { + case FLOWM2M_FLOW_OBJECT_DEVICEID: + memcpy(flow[objectInstanceID].DeviceID, *dataPointer, *dataSize); + flow[objectInstanceID].DeviceIDSize = *dataSize; + break; + + case FLOWM2M_FLOW_OBJECT_PARENTID: + memcpy(flow[objectInstanceID].ParentID, *dataPointer, *dataSize); + flow[objectInstanceID].ParentIDSize = *dataSize; + break; + + case FLOWM2M_FLOW_OBJECT_DEVICETYPE: + memcpy(flow[objectInstanceID].DeviceType, *dataPointer, *dataSize); + flow[objectInstanceID].DeviceType[*dataSize] = '\0'; + break; + + case FLOWM2M_FLOW_OBJECT_NAME: + memcpy(flow[objectInstanceID].Name, *dataPointer, *dataSize); + flow[objectInstanceID].Name[*dataSize] = '\0'; + break; + + case FLOWM2M_FLOW_OBJECT_DESCRIPTION: + memcpy(flow[objectInstanceID].Description, *dataPointer, *dataSize); + flow[objectInstanceID].Description[*dataSize] = '\0'; + break; + + case FLOWM2M_FLOW_OBJECT_FCAP: + memcpy(flow[objectInstanceID].FCAP, *dataPointer, *dataSize); + flow[objectInstanceID].FCAP[*dataSize] = '\0'; + printf("FCAP = %s\n", flow[objectInstanceID].FCAP); + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEEID: + flow[objectInstanceID].LicenseeID = (*(AwaInteger*)(*dataPointer)); + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE: + memcpy(flow[objectInstanceID].LicenseeChallenge, *dataPointer, *dataSize); + flow[objectInstanceID].LicenseeChallengeSize = *dataSize; + break; + + case FLOWM2M_FLOW_OBJECT_HASHITERATIONS: + flow[objectInstanceID].HashIterations = *((AwaInteger*)(*dataPointer)); + break; + + case FLOWM2M_FLOW_OBJECT_LICENSEEHASH: + memcpy(flow[objectInstanceID].LicenseeHash, *dataPointer, *dataSize); + flow[objectInstanceID].LicenseeHashSize = *dataSize; + break; + + case FLOWM2M_FLOW_OBJECT_STATUS: + flow[objectInstanceID].Status = *((AwaInteger*)(*dataPointer)); + printf("Status: %li\n", flow[objectInstanceID].Status); + *changed = true; + break; + + default: + printf("Invalid resource id for write operation\n"); + break; + } + result = AwaResult_SuccessChanged; + break; + + default: + break; + } + + if(operation == AwaOperation_Write) + { if(flow[objectInstanceID].HashIterations > 0 && flow[objectInstanceID].LicenseeChallengeSize > 0 && flow[objectInstanceID].LicenseeHashSize == 0 && (resourceID == FLOWM2M_FLOW_OBJECT_HASHITERATIONS || (resourceID == FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE))) { @@ -300,109 +299,109 @@ AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID o printf("Licensee secret is invalid\n"); } } - } + } - return result; + return result; } int DefineFlowObject(AwaStaticClient *awaClient) { - AwaError error; - - error = AwaStaticClient_DefineObjectWithHandler(awaClient, "Flow", FLOWM2M_FLOW_OBJECT, 0, FLOW_INSTANCES, handler); - if (error != AwaError_Success) - { - printf("Failed to register flow bject\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceID", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_DEVICEID, AwaResourceType_Opaque, 1, 1, AwaResourceOperations_ReadWrite, - handler); - if (error != AwaError_Success) - { - printf("Failed to define deviceID resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "ParentID", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_PARENTID, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, - handler); - if (error != AwaError_Success) - { - printf("Failed to define parentID resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceType", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_DEVICETYPE, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadWrite, - handler); - if (error != AwaError_Success) - { - printf("Failed to define device type resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Name", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_NAME, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, - handler); - if (error != AwaError_Success) - { - printf("Failed to define name resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Description", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_DESCRIPTION, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, - handler); - if (error != AwaError_Success) - { - printf("Failed to define description resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "FCAP", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_FCAP, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadWrite, - handler); - if (error != AwaError_Success) - { - printf("Failed to define FCAP resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeID", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_LICENSEEID, AwaResourceType_Integer, 1, 1, AwaResourceOperations_ReadWrite, - handler); - if (error != AwaError_Success) - { - printf("Failed to define LicenseeID resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeChallenge", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, - handler); - if (error != AwaError_Success) - { - printf("Failed to define LicenseeChallenge resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "HashIterations", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_HASHITERATIONS, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, - handler); - if (error != AwaError_Success) - { - printf("Failed to define HashIterations resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeHash", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_LICENSEEHASH, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, - handler); - if (error != AwaError_Success) - { - printf("Failed to define LicenseeHash resource\n"); - return 1; - } - - error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Status", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_STATUS, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, - handler); - if (error != AwaError_Success) - { - printf("Failed to define Status resource\n"); - return 1; - } - - return 0; + AwaError error; + + error = AwaStaticClient_DefineObjectWithHandler(awaClient, "Flow", FLOWM2M_FLOW_OBJECT, 0, FLOW_INSTANCES, handler); + if (error != AwaError_Success) + { + printf("Failed to register flow bject\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceID", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_DEVICEID, AwaResourceType_Opaque, 1, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define deviceID resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "ParentID", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_PARENTID, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define parentID resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "DeviceType", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_DEVICETYPE, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define device type resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Name", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_NAME, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define name resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Description", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_DESCRIPTION, AwaResourceType_String, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define description resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "FCAP", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_FCAP, AwaResourceType_String, 1, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define FCAP resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeID", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_LICENSEEID, AwaResourceType_Integer, 1, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define LicenseeID resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeChallenge", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_LICENSEECHALLENGE, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define LicenseeChallenge resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "HashIterations", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_HASHITERATIONS, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define HashIterations resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "LicenseeHash", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_LICENSEEHASH, AwaResourceType_Opaque, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define LicenseeHash resource\n"); + return 1; + } + + error = AwaStaticClient_DefineResourceWithHandler(awaClient, "Status", FLOWM2M_FLOW_OBJECT, FLOWM2M_FLOW_OBJECT_STATUS, AwaResourceType_Integer, 0, 1, AwaResourceOperations_ReadWrite, + handler); + if (error != AwaError_Success) + { + printf("Failed to define Status resource\n"); + return 1; + } + + return 0; }