Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

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 // Note: this macro is needed on armcc to get the the PRI*32 macros
00019 // from inttypes.h in a C++ code.
00020 #ifndef __STDC_FORMAT_MACROS
00021 #define __STDC_FORMAT_MACROS
00022 #endif
00023 
00024 #include "update-client-lwm2m/DeviceMetadataResource.h"
00025 #include "update-client-lwm2m/FirmwareUpdateResource.h"
00026 #include "update-client-common/arm_uc_types.h"
00027 #include "pal4life-device-identity/pal_device_identity.h"
00028 #include <inttypes.h>
00029 
00030 
00031 #include <stdio.h>
00032 
00033 #define ARM_UCS_LWM2M_INTERNAL_ERROR (-1)
00034 #define ARM_UCS_LWM2M_INTERNAL_SUCCESS (0)
00035 
00036 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00037 #define RESOURCE_VALUE(arg) arg
00038 #define PROTOCOL_VERSION 2
00039 #else
00040 #define RESOURCE_VALUE(arg) #arg
00041 #define PROTOCOL_VERSION 3
00042 #endif
00043 
00044 namespace DeviceMetadataResource {
00045 /* LWM2M Firmware Update Object */
00046 static M2MObject *deviceMetadataObject;
00047 
00048 /* LWM2M Firmware Update Object resources */
00049 static M2MResource *protocolSupportedResource = NULL; // /10255/0/0
00050 static M2MResource *bootloaderHashResource    = NULL; // /10255/0/1
00051 static M2MResource *OEMBootloaderHashResource = NULL; // /10255/0/2
00052 static M2MResource *vendorIdResource          = NULL; // /10255/0/3
00053 static M2MResource *classIdResource           = NULL; // /10255/0/4
00054 static M2MResource *deviceIdResource          = NULL; // /10255/0/5
00055 }
00056 
00057 
00058 /**
00059  * @brief Initialize LWM2M Device Metadata Object
00060  * @details Sets up LWM2M object with accompanying resources.
00061  */
00062 void DeviceMetadataResource::Initialize(void)
00063 {
00064     static bool initialized = false;
00065 
00066     if (!initialized) {
00067 
00068         /* The LWM2M Firmware Update Object is at /10255 */
00069 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00070         deviceMetadataObject = M2MInterfaceFactory::create_object(10255, FirmwareUpdateResource::getM2MInterface());
00071 #else
00072         deviceMetadataObject = M2MInterfaceFactory::create_object("10255");
00073 #endif
00074 
00075         if (deviceMetadataObject) {
00076             initialized = true;
00077 
00078 #if defined(ARM_UC_PROFILE_MBED_CLOUD_CLIENT) && (ARM_UC_PROFILE_MBED_CLOUD_CLIENT == 1)
00079             deviceMetadataObject->set_register_uri(false);
00080 #endif
00081             /* Set object operating mode to GET_ALLOWED */
00082             deviceMetadataObject->set_operation(M2MBase::GET_ALLOWED);
00083             /* Create first (and only) instance /10255/0 */
00084             M2MObjectInstance *deviceMetadataInstance = deviceMetadataObject->create_object_instance();
00085 
00086             if (deviceMetadataInstance) {
00087 #if defined(ARM_UC_PROFILE_MBED_CLOUD_CLIENT) && (ARM_UC_PROFILE_MBED_CLOUD_CLIENT == 1)
00088                 deviceMetadataInstance->set_register_uri(false);
00089 #endif
00090 
00091                 /* Default values are non-standard, but the standard has no
00092                    values for indicating that the device is initializing.
00093                 */
00094                 const int64_t version   = PROTOCOL_VERSION;
00095                 const uint8_t invalid_value[]    = "INVALID";
00096                 const uint8_t invalid_value_size = sizeof(invalid_value) - 1;
00097 
00098                 ARM_UC_INIT_ERROR(err, ERR_INVALID_PARAMETER);
00099                 arm_uc_guid_t guid    = { 0 };
00100                 uint8_t *value        = NULL;
00101                 uint32_t value_length = 0;
00102 
00103                 /* Set instance operating mode to GET_ALLOWED */
00104                 deviceMetadataInstance->set_operation(M2MBase::GET_ALLOWED);
00105 
00106                 /* Create Update resource /10255/0/0 */
00107                 protocolSupportedResource = deviceMetadataInstance->create_dynamic_resource(
00108                                                 RESOURCE_VALUE(0),
00109                                                 "ProtocolSupported",
00110                                                 M2MResourceInstance::INTEGER,
00111                                                 true);
00112                 if (protocolSupportedResource) {
00113                     protocolSupportedResource->set_operation(M2MBase::GET_ALLOWED);
00114                     protocolSupportedResource->set_value(version);
00115                     protocolSupportedResource->publish_value_in_registration_msg(true);
00116                     protocolSupportedResource->set_auto_observable(true);
00117                 }
00118 
00119                 /* Create Update resource /10255/0/1 */
00120 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00121                 bootloaderHashResource = deviceMetadataInstance->create_dynamic_resource(
00122                                              RESOURCE_VALUE(1),
00123                                              "BootloaderHash",
00124                                              M2MResourceInstance::OPAQUE,
00125                                              true);
00126 #else
00127                 bootloaderHashResource = deviceMetadataInstance->create_static_resource(
00128                                              RESOURCE_VALUE(1),
00129                                              "BootloaderHash",
00130                                              M2MResourceInstance::OPAQUE,
00131                                              (uint8_t *) invalid_value,
00132                                              invalid_value_size);
00133 #endif
00134                 if (bootloaderHashResource) {
00135                     bootloaderHashResource->set_operation(M2MBase::GET_ALLOWED);
00136                     bootloaderHashResource->publish_value_in_registration_msg(true);
00137                     bootloaderHashResource->set_auto_observable(true);
00138                 }
00139 
00140                 /* Create Update resource /10255/0/2 */
00141 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00142                 OEMBootloaderHashResource = deviceMetadataInstance->create_dynamic_resource(
00143                                                 RESOURCE_VALUE(2),
00144                                                 "OEMBootloaderHash",
00145                                                 M2MResourceInstance::OPAQUE,
00146                                                 true);
00147 #else
00148                 OEMBootloaderHashResource = deviceMetadataInstance->create_static_resource(
00149                                                 RESOURCE_VALUE(2),
00150                                                 "OEMBootloaderHash",
00151                                                 M2MResourceInstance::OPAQUE,
00152                                                 (uint8_t *) invalid_value,
00153                                                 invalid_value_size);
00154 #endif
00155                 if (OEMBootloaderHashResource) {
00156                     OEMBootloaderHashResource->set_operation(M2MBase::GET_ALLOWED);
00157                     OEMBootloaderHashResource->publish_value_in_registration_msg(true);
00158                     OEMBootloaderHashResource->set_auto_observable(true);
00159                 }
00160 
00161                 /* get vendor ID */
00162                 err = pal_getVendorGuid(&guid);
00163                 if (err.error == ERR_NONE) {
00164                     value = (uint8_t *) &guid;
00165                     value_length = sizeof(arm_uc_guid_t);
00166                 } else {
00167                     value = (uint8_t *) invalid_value;
00168                     value_length = invalid_value_size;
00169                 }
00170 
00171                 /* Create Update resource /10255/0/3 */
00172                 vendorIdResource = deviceMetadataInstance->create_dynamic_resource(
00173                                        RESOURCE_VALUE(3),
00174                                        "Vendor",
00175                                        M2MResourceInstance::OPAQUE,
00176                                        true);
00177 
00178                 if (vendorIdResource) {
00179                     vendorIdResource->set_operation(M2MBase::GET_ALLOWED);
00180                     vendorIdResource->set_value(value, value_length);
00181                     vendorIdResource->publish_value_in_registration_msg(true);
00182                     vendorIdResource->set_auto_observable(true);
00183                 }
00184 
00185                 /* get class ID */
00186                 err = pal_getClassGuid(&guid);
00187                 if (err.error == ERR_NONE) {
00188                     value = (uint8_t *) &guid;
00189                     value_length = sizeof(arm_uc_guid_t);
00190                 } else {
00191                     value = (uint8_t *) invalid_value;
00192                     value_length = invalid_value_size;
00193                 }
00194 
00195                 /* Create Update resource /10255/0/4 */
00196                 classIdResource = deviceMetadataInstance->create_dynamic_resource(
00197                                       RESOURCE_VALUE(4),
00198                                       "Class",
00199                                       M2MResourceInstance::OPAQUE,
00200                                       true);
00201 
00202                 if (classIdResource) {
00203                     classIdResource->set_operation(M2MBase::GET_ALLOWED);
00204                     classIdResource->set_value(value, value_length);
00205                     classIdResource->publish_value_in_registration_msg(true);
00206                     classIdResource->set_auto_observable(true);
00207                 }
00208 
00209                 /* get device ID */
00210                 err = pal_getDeviceGuid(&guid);
00211                 if (err.error == ERR_NONE) {
00212                     value = (uint8_t *) &guid;
00213                     value_length = sizeof(arm_uc_guid_t);
00214                 } else {
00215                     value = (uint8_t *) invalid_value;
00216                     value_length = invalid_value_size;
00217                 }
00218 
00219                 /* Create Update resource /10255/0/5 */
00220 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00221                 deviceIdResource = deviceMetadataInstance->create_dynamic_resource(
00222                                        RESOURCE_VALUE(5),
00223                                        "DeviceId",
00224                                        M2MResourceInstance::OPAQUE,
00225                                        true);
00226 #else
00227                 deviceIdResource = deviceMetadataInstance->create_static_resource(
00228                                        RESOURCE_VALUE(5),
00229                                        "DeviceId",
00230                                        M2MResourceInstance::OPAQUE,
00231                                        value,
00232                                        value_length);
00233 #endif
00234                 if (deviceIdResource) {
00235                     deviceIdResource->set_operation(M2MBase::GET_ALLOWED);
00236 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00237                     deviceIdResource->set_value(value, value_length);
00238 #endif
00239                     deviceIdResource->publish_value_in_registration_msg(true);
00240                     deviceIdResource->set_auto_observable(true);
00241                 }
00242             }
00243         }
00244     }
00245 }
00246 
00247 int32_t DeviceMetadataResource::setBootloaderHash(arm_uc_buffer_t *hash)
00248 {
00249     UC_SRCE_TRACE("DeviceMetadataResource::setBootloaderHash ptr %p size %" PRIu32, hash, hash->size);
00250 
00251     int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
00252 
00253     if (bootloaderHashResource && hash && hash->size > 0) {
00254         bool rt = bootloaderHashResource->set_value(hash->ptr, hash->size);
00255         if (rt == true) {
00256             result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00257         }
00258     }
00259 
00260     return result;
00261 }
00262 
00263 int32_t DeviceMetadataResource::setOEMBootloaderHash(arm_uc_buffer_t *hash)
00264 {
00265     UC_SRCE_TRACE("DeviceMetadataResource::setOEMBootloaderHash ptr %p size %" PRIu32, hash, hash->size);
00266 
00267     int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
00268 
00269     if (OEMBootloaderHashResource && hash && hash->size > 0) {
00270         bool rt = OEMBootloaderHashResource->set_value(hash->ptr, hash->size);
00271         if (rt == true) {
00272             result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00273         }
00274     }
00275 
00276     return result;
00277 }
00278 
00279 M2MObject *DeviceMetadataResource::getObject()
00280 {
00281     Initialize();
00282 
00283     return deviceMetadataObject;
00284 }
00285 
00286 void DeviceMetadataResource::Uninitialize()
00287 {
00288     UC_SRCE_TRACE("DeviceMetadataResource::Uninitialize");
00289     delete deviceMetadataObject;
00290     deviceMetadataObject = NULL;
00291 }