diff --git a/Makefile.libobjects b/Makefile.libobjects index d3a130c..5e88e27 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-device-object.c \ + lwm2m-client-ipso-digital-input.c lwm2m-client-ipso-light-control.c 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-device-object.c b/lwm2m-client-device-object.c new file mode 100644 index 0000000..6129a33 --- /dev/null +++ b/lwm2m-client-device-object.c @@ -0,0 +1,177 @@ +/** + * @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 = { .SupportedBindingandModes = "U", + .SoftwareVersion = VERSION}; + +/*************************************************************************************************** + * 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_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; +} 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_ */ diff --git a/lwm2m-client-flow-access-object.c b/lwm2m-client-flow-access-object.c index c567772..fd16b7a 100644 --- a/lwm2m-client-flow-access-object.c +++ b/lwm2m-client-flow-access-object.c @@ -41,296 +41,205 @@ #include #include #include -#include "lwm2m_core.h" #include "coap_abstraction.h" -#include "common.h" + +#include /*************************************************************************************************** * 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 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 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]; + AwaTime 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, -}; +FlowAccessObject flowAccess[FLOW_ACCESS_INSTANCES]; /*************************************************************************************************** - * 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, size_t *dataSize, bool *changed) { - return 0; + 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; + } + + 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; } -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) +int DefineFlowAccessObject(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); - - 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-access-object.h b/lwm2m-client-flow-access-object.h index cf67383..a834a66 100644 --- a/lwm2m-client-flow-access-object.h +++ b/lwm2m-client-flow-access-object.h @@ -36,6 +36,6 @@ #ifndef LWM2M_CLIENT_FLOW_ACCESS_OBJECT_H_ #define LWM2M_CLIENT_FLOW_ACCESS_OBJECT_H_ -int Lwm2m_RegisterFlowAccessObject(Lwm2mContextType * context); +int DefineFlowAccessObject(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..fd66ba6 100644 --- a/lwm2m-client-flow-object.c +++ b/lwm2m-client-flow-object.c @@ -42,537 +42,366 @@ #include #include #include -#include "lwm2m_core.h" +#include #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) +#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 -/*************************************************************************************************** - * 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); +#define MAX_STRING_SIZE 64 +#define MAX_KEY_SIZE 64 -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, -}; +#define FLOW_INSTANCES 1 +static char licenseeSecret[64] = "getATTtDsNBpBRnMsN7GoQ=="; -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) -{ - 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) +typedef struct { - 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; -} + 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 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 FlowObject flow[FLOW_INSTANCES] = {0}; -static int FlowObject_ResourceWriteHandler(void * context, ObjectIDType objectID, - ObjectInstanceIDType objectInstanceID, ResourceIDType resourceID, - ResourceInstanceIDType resourceInstanceID, uint8_t * srcBuffer, int srcBufferLen, bool * changed) +static bool CalculateLicenseeHash(char *licenseeSecret, uint8_t hash[SHA256_HASH_LENGTH], const char *challenge, int challengeLength, int iterations) { - 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; + 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; } -/*************************************************************************************************** - * Implementation - Public - **************************************************************************************************/ - -int Lwm2m_RegisterFlowObject(Lwm2mContextType * context) +AwaResult handler(AwaStaticClient *client, AwaOperation operation, AwaObjectID objectID, AwaObjectInstanceID objectInstanceID, + AwaResourceID resourceID, AwaResourceInstanceID resourceInstanceID, void **dataPointer, size_t *dataSize, bool *changed) { - 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; + AwaResult result = AwaResult_InternalError; + + if (!((objectID == FLOWM2M_FLOW_OBJECT) && (objectInstanceID >= 0) && (objectInstanceID < FLOW_INSTANCES))) + { + printf("Incorrect flow object data\n"); + return result; + } + + 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: + 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))) + { + 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; } -int Lwm2m_SetProvisioningInfo(Lwm2mContextType * context, const char * DeviceType, - const char * FCAP, int64_t LicenseeID) +int DefineFlowObject(AwaStaticClient *awaClient) { - 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; + 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; } diff --git a/lwm2m-client-flow-object.h b/lwm2m-client-flow-object.h index 2d6b024..a473527 100644 --- a/lwm2m-client-flow-object.h +++ b/lwm2m-client-flow-object.h @@ -35,9 +35,8 @@ #ifndef LWM2M_CLIENT_FLOW_OBJECT_H_ #define LWM2M_CLIENT_FLOW_OBJECT_H_ +#include -int Lwm2m_RegisterFlowObject(Lwm2mContextType * context); -int Lwm2m_SetProvisioningInfo(Lwm2mContextType * context, const char * DeviceType, - const char * FCAP, int64_t LicenseeID); +int DefineFlowObject(AwaStaticClient *awaClient); #endif /* LWM2M_CLIENT_FLOW_OBJECT_H_ */ diff --git a/lwm2m-client-ipso-digital-input.c b/lwm2m-client-ipso-digital-input.c index 1a70d97..cb60979 100644 --- a/lwm2m-client-ipso-digital-input.c +++ b/lwm2m-client-ipso-digital-input.c @@ -41,36 +41,8 @@ #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) +#include +#include "lwm2m-client-ipso-digital-input.h" /*************************************************************************************************** * Typedefs @@ -78,368 +50,261 @@ 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]; + AwaBoolean State; + AwaInteger Counter; + AwaBoolean 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 = strlen(digitalInputs[objectInstanceID].ApplicationType) ; + break; + + case IPSO_SENSOR_TYPE: + *dataPointer = digitalInputs[objectInstanceID].SensorType; + *dataSize = strlen(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; + int i; + + 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, 1, 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 SensorType 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; + } + + 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; } -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_ */ diff --git a/lwm2m-client-ipso-light-control.c b/lwm2m-client-ipso-light-control.c index ba7ebfb..21476de 100644 --- a/lwm2m-client-ipso-light-control.c +++ b/lwm2m-client-ipso-light-control.c @@ -41,35 +41,24 @@ #include #include #include -#include "lwm2m_core.h" -#include "coap_abstraction.h" + +#include "awa/static.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 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 @@ -77,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_ */