Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DeviceMetadataResource.cpp Source File

DeviceMetadataResource.cpp

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include "update-client-lwm2m/DeviceMetadataResource.h"
00020 
00021 #include "update-client-common/arm_uc_common.h"
00022 #include "pal4life-device-identity/pal_device_identity.h"
00023 
00024 #include <stdio.h>
00025 
00026 #define ARM_UCS_LWM2M_INTERNAL_ERROR (-1)
00027 #define ARM_UCS_LWM2M_INTERNAL_SUCCESS (0)
00028 
00029 namespace DeviceMetadataResource {
00030     /* LWM2M Firmware Update Object */
00031     static M2MObject* deviceMetadataObject;
00032 
00033     /* LWM2M Firmware Update Object resources */
00034     static M2MResource* protocolSupportedResource = NULL; // /10255/0/0
00035     static M2MResource* bootloaderHashResource    = NULL; // /10255/0/1
00036     static M2MResource* OEMBootloaderHashResource = NULL; // /10255/0/2
00037     static M2MResource* vendorIdResource          = NULL; // /10255/0/3
00038     static M2MResource* classIdResource           = NULL; // /10255/0/4
00039     static M2MResource* deviceIdResource          = NULL; // /10255/0/5
00040 }
00041 
00042 
00043 /**
00044  * @brief Initialize LWM2M Device Metadata Object
00045  * @details Sets up LWM2M object with accompanying resources.
00046  */
00047 void DeviceMetadataResource::Initialize(void)
00048 {
00049     static bool initialized = false;
00050 
00051     if (!initialized)
00052     {
00053         initialized = true;
00054 
00055         /* The LWM2M Firmware Update Object is at /10255 */
00056         deviceMetadataObject = M2MInterfaceFactory::create_object("10255");
00057 
00058         if (deviceMetadataObject)
00059         {
00060             /* Set object operating mode to GET_ALLOWED */
00061             deviceMetadataObject->set_operation(M2MBase::GET_ALLOWED);
00062             /* Create first (and only) instance /10255/0 */
00063             M2MObjectInstance* deviceMetadataInstance = deviceMetadataObject->create_object_instance();
00064 
00065             if (deviceMetadataInstance)
00066             {
00067                 /* Default values are non-standard, but the standard has no
00068                    values for indicating that the device is initializing.
00069                 */
00070                 int64_t defaultVersion   = 1;
00071                 const uint8_t invalid_value[]    = "INVALID";
00072                 const uint8_t invalid_value_size = sizeof(invalid_value) - 1;
00073 
00074                 ARM_UC_INIT_ERROR(err, ERR_INVALID_PARAMETER);
00075                 arm_uc_guid_t guid    = { 0 };
00076                 uint8_t* value        = NULL;
00077                 uint32_t value_length = 0;
00078 
00079                 /* Set instance operating mode to GET_ALLOWED */
00080                 deviceMetadataInstance->set_operation(M2MBase::GET_ALLOWED);
00081 
00082                 /* Create Update resource /10255/0/0 */
00083                 protocolSupportedResource = deviceMetadataInstance->create_dynamic_resource(
00084                                                     "0",
00085                                                     "ProtocolSupported",
00086                                                     M2MResourceInstance::INTEGER,
00087                                                     true);
00088                 if (protocolSupportedResource)
00089                 {
00090                     protocolSupportedResource->set_operation(M2MBase::GET_ALLOWED);
00091                     protocolSupportedResource->set_value(defaultVersion);
00092                 }
00093 
00094                 /* Create Update resource /10255/0/1 */
00095                 bootloaderHashResource = deviceMetadataInstance->create_static_resource(
00096                                                     "1",
00097                                                     "BootloaderHash",
00098                                                     M2MResourceInstance::OPAQUE,
00099                                                     (uint8_t *) invalid_value,
00100                                                     invalid_value_size);
00101                 if (bootloaderHashResource)
00102                 {
00103                     bootloaderHashResource->set_operation(M2MBase::GET_ALLOWED);
00104                 }
00105 
00106                 /* Create Update resource /10255/0/2 */
00107                 OEMBootloaderHashResource = deviceMetadataInstance->create_static_resource(
00108                                                     "2",
00109                                                     "OEMBootloaderHash",
00110                                                     M2MResourceInstance::OPAQUE,
00111                                                     (uint8_t *) invalid_value,
00112                                                     invalid_value_size);
00113                 if (OEMBootloaderHashResource)
00114                 {
00115                     OEMBootloaderHashResource->set_operation(M2MBase::GET_ALLOWED);
00116                 }
00117 
00118                 /* get vendor ID */
00119                 err = pal_getVendorGuid(&guid);
00120                 if (err.error == ERR_NONE)
00121                 {
00122                     value = (uint8_t *) &guid;
00123                     value_length = sizeof(arm_uc_guid_t);
00124                 }
00125                 else
00126                 {
00127                     value = (uint8_t *) invalid_value;
00128                     value_length = invalid_value_size;
00129                 }
00130 
00131                 /* Create Update resource /10255/0/3 */
00132                 vendorIdResource = deviceMetadataInstance->create_dynamic_resource(
00133                                                     "3",
00134                                                     "Vendor",
00135                                                     M2MResourceInstance::OPAQUE,
00136                                                     true);
00137 
00138                 if (vendorIdResource)
00139                 {
00140                     vendorIdResource->set_operation(M2MBase::GET_ALLOWED);
00141                     vendorIdResource->set_value(value, value_length);
00142                 }
00143 
00144                 /* get class ID */
00145                 err = pal_getClassGuid(&guid);
00146                 if (err.error == ERR_NONE)
00147                 {
00148                     value = (uint8_t *) &guid;
00149                     value_length = sizeof(arm_uc_guid_t);
00150                 }
00151                 else
00152                 {
00153                     value = (uint8_t *) invalid_value;
00154                     value_length = invalid_value_size;
00155                 }
00156 
00157                 /* Create Update resource /10255/0/4 */
00158                 classIdResource = deviceMetadataInstance->create_dynamic_resource(
00159                                                     "4",
00160                                                     "Class",
00161                                                     M2MResourceInstance::OPAQUE,
00162                                                     true);
00163 
00164                 if (classIdResource)
00165                 {
00166                     classIdResource->set_operation(M2MBase::GET_ALLOWED);
00167                     classIdResource->set_value(value, value_length);
00168                 }
00169 
00170                 /* get device ID */
00171                 err = pal_getDeviceGuid(&guid);
00172                 if (err.error == ERR_NONE)
00173                 {
00174                     value = (uint8_t *) &guid;
00175                     value_length = sizeof(arm_uc_guid_t);
00176                 }
00177                 else
00178                 {
00179                     value = (uint8_t *) invalid_value;
00180                     value_length = invalid_value_size;
00181                 }
00182 
00183                 /* Create Update resource /10255/0/5 */
00184                 deviceIdResource = deviceMetadataInstance->create_static_resource(
00185                                                     "5",
00186                                                     "DeviceId",
00187                                                     M2MResourceInstance::OPAQUE,
00188                                                     value,
00189                                                     value_length);
00190                 if (deviceIdResource)
00191                 {
00192                     deviceIdResource->set_operation(M2MBase::GET_ALLOWED);
00193                 }
00194             }
00195         }
00196     }
00197 }
00198 
00199 int32_t DeviceMetadataResource::setBootloaderHash(arm_uc_buffer_t* hash)
00200 {
00201     UC_SRCE_TRACE("DeviceMetadataResource::setBootloaderHash ptr %p size %u", hash, hash->size);
00202 
00203     int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
00204 
00205     if (bootloaderHashResource && hash && hash->size > 0)
00206     {
00207         bool rt = bootloaderHashResource->set_value(hash->ptr, hash->size);
00208         if (rt == true)
00209         {
00210             result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00211         }
00212     }
00213 
00214     return result;
00215 }
00216 
00217 int32_t DeviceMetadataResource::setOEMBootloaderHash(arm_uc_buffer_t* hash)
00218 {
00219     UC_SRCE_TRACE("DeviceMetadataResource::setOEMBootloaderHash ptr %p size %u", hash, hash->size);
00220 
00221     int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
00222 
00223     if (OEMBootloaderHashResource && hash && hash->size > 0)
00224     {
00225         bool rt = OEMBootloaderHashResource->set_value(hash->ptr, hash->size);
00226         if (rt == true)
00227         {
00228             result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00229         }
00230     }
00231 
00232     return result;
00233 }
00234 
00235 M2MObject* DeviceMetadataResource::getObject()
00236 {
00237     Initialize();
00238 
00239     return deviceMetadataObject;
00240 }
00241 
00242 void DeviceMetadataResource::Uninitialize()
00243 {
00244     UC_SRCE_TRACE("DeviceMetadataResource::Uninitialize");
00245     delete deviceMetadataObject;
00246     deviceMetadataObject = NULL;
00247 }