Example

Dependencies:   FXAS21002 FXOS8700Q

Committer:
maygup01
Date:
Tue Nov 19 09:49:38 2019 +0000
Revision:
0:11cc2b7889af
Example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maygup01 0:11cc2b7889af 1 // ----------------------------------------------------------------------------
maygup01 0:11cc2b7889af 2 // Copyright 2016-2017 ARM Ltd.
maygup01 0:11cc2b7889af 3 //
maygup01 0:11cc2b7889af 4 // SPDX-License-Identifier: Apache-2.0
maygup01 0:11cc2b7889af 5 //
maygup01 0:11cc2b7889af 6 // Licensed under the Apache License, Version 2.0 (the "License");
maygup01 0:11cc2b7889af 7 // you may not use this file except in compliance with the License.
maygup01 0:11cc2b7889af 8 // You may obtain a copy of the License at
maygup01 0:11cc2b7889af 9 //
maygup01 0:11cc2b7889af 10 // http://www.apache.org/licenses/LICENSE-2.0
maygup01 0:11cc2b7889af 11 //
maygup01 0:11cc2b7889af 12 // Unless required by applicable law or agreed to in writing, software
maygup01 0:11cc2b7889af 13 // distributed under the License is distributed on an "AS IS" BASIS,
maygup01 0:11cc2b7889af 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maygup01 0:11cc2b7889af 15 // See the License for the specific language governing permissions and
maygup01 0:11cc2b7889af 16 // limitations under the License.
maygup01 0:11cc2b7889af 17 // ----------------------------------------------------------------------------
maygup01 0:11cc2b7889af 18 // Note: this macro is needed on armcc to get the the PRI*32 macros
maygup01 0:11cc2b7889af 19 // from inttypes.h in a C++ code.
maygup01 0:11cc2b7889af 20 #ifndef __STDC_FORMAT_MACROS
maygup01 0:11cc2b7889af 21 #define __STDC_FORMAT_MACROS
maygup01 0:11cc2b7889af 22 #endif
maygup01 0:11cc2b7889af 23
maygup01 0:11cc2b7889af 24 #include "update-client-lwm2m/DeviceMetadataResource.h"
maygup01 0:11cc2b7889af 25 #include "update-client-lwm2m/FirmwareUpdateResource.h"
maygup01 0:11cc2b7889af 26 #include "update-client-common/arm_uc_types.h"
maygup01 0:11cc2b7889af 27 #include "pal4life-device-identity/pal_device_identity.h"
maygup01 0:11cc2b7889af 28 #include <inttypes.h>
maygup01 0:11cc2b7889af 29
maygup01 0:11cc2b7889af 30
maygup01 0:11cc2b7889af 31 #include <stdio.h>
maygup01 0:11cc2b7889af 32
maygup01 0:11cc2b7889af 33 #define ARM_UCS_LWM2M_INTERNAL_ERROR (-1)
maygup01 0:11cc2b7889af 34 #define ARM_UCS_LWM2M_INTERNAL_SUCCESS (0)
maygup01 0:11cc2b7889af 35
maygup01 0:11cc2b7889af 36 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 37 #define RESOURCE_VALUE(arg) arg
maygup01 0:11cc2b7889af 38 #define PROTOCOL_VERSION 2
maygup01 0:11cc2b7889af 39 #else
maygup01 0:11cc2b7889af 40 #define RESOURCE_VALUE(arg) #arg
maygup01 0:11cc2b7889af 41 #define PROTOCOL_VERSION 3
maygup01 0:11cc2b7889af 42 #endif
maygup01 0:11cc2b7889af 43
maygup01 0:11cc2b7889af 44 namespace DeviceMetadataResource {
maygup01 0:11cc2b7889af 45 /* LWM2M Firmware Update Object */
maygup01 0:11cc2b7889af 46 static M2MObject *deviceMetadataObject;
maygup01 0:11cc2b7889af 47
maygup01 0:11cc2b7889af 48 /* LWM2M Firmware Update Object resources */
maygup01 0:11cc2b7889af 49 static M2MResource *protocolSupportedResource = NULL; // /10255/0/0
maygup01 0:11cc2b7889af 50 static M2MResource *bootloaderHashResource = NULL; // /10255/0/1
maygup01 0:11cc2b7889af 51 static M2MResource *OEMBootloaderHashResource = NULL; // /10255/0/2
maygup01 0:11cc2b7889af 52 static M2MResource *vendorIdResource = NULL; // /10255/0/3
maygup01 0:11cc2b7889af 53 static M2MResource *classIdResource = NULL; // /10255/0/4
maygup01 0:11cc2b7889af 54 static M2MResource *deviceIdResource = NULL; // /10255/0/5
maygup01 0:11cc2b7889af 55 }
maygup01 0:11cc2b7889af 56
maygup01 0:11cc2b7889af 57
maygup01 0:11cc2b7889af 58 /**
maygup01 0:11cc2b7889af 59 * @brief Initialize LWM2M Device Metadata Object
maygup01 0:11cc2b7889af 60 * @details Sets up LWM2M object with accompanying resources.
maygup01 0:11cc2b7889af 61 */
maygup01 0:11cc2b7889af 62 void DeviceMetadataResource::Initialize(void)
maygup01 0:11cc2b7889af 63 {
maygup01 0:11cc2b7889af 64 static bool initialized = false;
maygup01 0:11cc2b7889af 65
maygup01 0:11cc2b7889af 66 if (!initialized) {
maygup01 0:11cc2b7889af 67
maygup01 0:11cc2b7889af 68 /* The LWM2M Firmware Update Object is at /10255 */
maygup01 0:11cc2b7889af 69 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 70 deviceMetadataObject = M2MInterfaceFactory::create_object(10255, FirmwareUpdateResource::getM2MInterface());
maygup01 0:11cc2b7889af 71 #else
maygup01 0:11cc2b7889af 72 deviceMetadataObject = M2MInterfaceFactory::create_object("10255");
maygup01 0:11cc2b7889af 73 #endif
maygup01 0:11cc2b7889af 74
maygup01 0:11cc2b7889af 75 if (deviceMetadataObject) {
maygup01 0:11cc2b7889af 76 initialized = true;
maygup01 0:11cc2b7889af 77
maygup01 0:11cc2b7889af 78 #if defined(ARM_UC_PROFILE_MBED_CLOUD_CLIENT) && (ARM_UC_PROFILE_MBED_CLOUD_CLIENT == 1)
maygup01 0:11cc2b7889af 79 deviceMetadataObject->set_register_uri(false);
maygup01 0:11cc2b7889af 80 #endif
maygup01 0:11cc2b7889af 81 /* Set object operating mode to GET_ALLOWED */
maygup01 0:11cc2b7889af 82 deviceMetadataObject->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 83 /* Create first (and only) instance /10255/0 */
maygup01 0:11cc2b7889af 84 M2MObjectInstance *deviceMetadataInstance = deviceMetadataObject->create_object_instance();
maygup01 0:11cc2b7889af 85
maygup01 0:11cc2b7889af 86 if (deviceMetadataInstance) {
maygup01 0:11cc2b7889af 87 #if defined(ARM_UC_PROFILE_MBED_CLOUD_CLIENT) && (ARM_UC_PROFILE_MBED_CLOUD_CLIENT == 1)
maygup01 0:11cc2b7889af 88 deviceMetadataInstance->set_register_uri(false);
maygup01 0:11cc2b7889af 89 #endif
maygup01 0:11cc2b7889af 90
maygup01 0:11cc2b7889af 91 /* Default values are non-standard, but the standard has no
maygup01 0:11cc2b7889af 92 values for indicating that the device is initializing.
maygup01 0:11cc2b7889af 93 */
maygup01 0:11cc2b7889af 94 const int64_t version = PROTOCOL_VERSION;
maygup01 0:11cc2b7889af 95 const uint8_t invalid_value[] = "INVALID";
maygup01 0:11cc2b7889af 96 const uint8_t invalid_value_size = sizeof(invalid_value) - 1;
maygup01 0:11cc2b7889af 97
maygup01 0:11cc2b7889af 98 ARM_UC_INIT_ERROR(err, ERR_INVALID_PARAMETER);
maygup01 0:11cc2b7889af 99 arm_uc_guid_t guid = { 0 };
maygup01 0:11cc2b7889af 100 uint8_t *value = NULL;
maygup01 0:11cc2b7889af 101 uint32_t value_length = 0;
maygup01 0:11cc2b7889af 102
maygup01 0:11cc2b7889af 103 /* Set instance operating mode to GET_ALLOWED */
maygup01 0:11cc2b7889af 104 deviceMetadataInstance->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 105
maygup01 0:11cc2b7889af 106 /* Create Update resource /10255/0/0 */
maygup01 0:11cc2b7889af 107 protocolSupportedResource = deviceMetadataInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 108 RESOURCE_VALUE(0),
maygup01 0:11cc2b7889af 109 "ProtocolSupported",
maygup01 0:11cc2b7889af 110 M2MResourceInstance::INTEGER,
maygup01 0:11cc2b7889af 111 true);
maygup01 0:11cc2b7889af 112 if (protocolSupportedResource) {
maygup01 0:11cc2b7889af 113 protocolSupportedResource->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 114 protocolSupportedResource->set_value(version);
maygup01 0:11cc2b7889af 115 protocolSupportedResource->publish_value_in_registration_msg(true);
maygup01 0:11cc2b7889af 116 protocolSupportedResource->set_auto_observable(true);
maygup01 0:11cc2b7889af 117 }
maygup01 0:11cc2b7889af 118
maygup01 0:11cc2b7889af 119 /* Create Update resource /10255/0/1 */
maygup01 0:11cc2b7889af 120 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 121 bootloaderHashResource = deviceMetadataInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 122 RESOURCE_VALUE(1),
maygup01 0:11cc2b7889af 123 "BootloaderHash",
maygup01 0:11cc2b7889af 124 M2MResourceInstance::OPAQUE,
maygup01 0:11cc2b7889af 125 true);
maygup01 0:11cc2b7889af 126 #else
maygup01 0:11cc2b7889af 127 bootloaderHashResource = deviceMetadataInstance->create_static_resource(
maygup01 0:11cc2b7889af 128 RESOURCE_VALUE(1),
maygup01 0:11cc2b7889af 129 "BootloaderHash",
maygup01 0:11cc2b7889af 130 M2MResourceInstance::OPAQUE,
maygup01 0:11cc2b7889af 131 (uint8_t *) invalid_value,
maygup01 0:11cc2b7889af 132 invalid_value_size);
maygup01 0:11cc2b7889af 133 #endif
maygup01 0:11cc2b7889af 134 if (bootloaderHashResource) {
maygup01 0:11cc2b7889af 135 bootloaderHashResource->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 136 bootloaderHashResource->publish_value_in_registration_msg(true);
maygup01 0:11cc2b7889af 137 bootloaderHashResource->set_auto_observable(true);
maygup01 0:11cc2b7889af 138 }
maygup01 0:11cc2b7889af 139
maygup01 0:11cc2b7889af 140 /* Create Update resource /10255/0/2 */
maygup01 0:11cc2b7889af 141 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 142 OEMBootloaderHashResource = deviceMetadataInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 143 RESOURCE_VALUE(2),
maygup01 0:11cc2b7889af 144 "OEMBootloaderHash",
maygup01 0:11cc2b7889af 145 M2MResourceInstance::OPAQUE,
maygup01 0:11cc2b7889af 146 true);
maygup01 0:11cc2b7889af 147 #else
maygup01 0:11cc2b7889af 148 OEMBootloaderHashResource = deviceMetadataInstance->create_static_resource(
maygup01 0:11cc2b7889af 149 RESOURCE_VALUE(2),
maygup01 0:11cc2b7889af 150 "OEMBootloaderHash",
maygup01 0:11cc2b7889af 151 M2MResourceInstance::OPAQUE,
maygup01 0:11cc2b7889af 152 (uint8_t *) invalid_value,
maygup01 0:11cc2b7889af 153 invalid_value_size);
maygup01 0:11cc2b7889af 154 #endif
maygup01 0:11cc2b7889af 155 if (OEMBootloaderHashResource) {
maygup01 0:11cc2b7889af 156 OEMBootloaderHashResource->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 157 OEMBootloaderHashResource->publish_value_in_registration_msg(true);
maygup01 0:11cc2b7889af 158 OEMBootloaderHashResource->set_auto_observable(true);
maygup01 0:11cc2b7889af 159 }
maygup01 0:11cc2b7889af 160
maygup01 0:11cc2b7889af 161 /* get vendor ID */
maygup01 0:11cc2b7889af 162 err = pal_getVendorGuid(&guid);
maygup01 0:11cc2b7889af 163 if (err.error == ERR_NONE) {
maygup01 0:11cc2b7889af 164 value = (uint8_t *) &guid;
maygup01 0:11cc2b7889af 165 value_length = sizeof(arm_uc_guid_t);
maygup01 0:11cc2b7889af 166 } else {
maygup01 0:11cc2b7889af 167 value = (uint8_t *) invalid_value;
maygup01 0:11cc2b7889af 168 value_length = invalid_value_size;
maygup01 0:11cc2b7889af 169 }
maygup01 0:11cc2b7889af 170
maygup01 0:11cc2b7889af 171 /* Create Update resource /10255/0/3 */
maygup01 0:11cc2b7889af 172 vendorIdResource = deviceMetadataInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 173 RESOURCE_VALUE(3),
maygup01 0:11cc2b7889af 174 "Vendor",
maygup01 0:11cc2b7889af 175 M2MResourceInstance::OPAQUE,
maygup01 0:11cc2b7889af 176 true);
maygup01 0:11cc2b7889af 177
maygup01 0:11cc2b7889af 178 if (vendorIdResource) {
maygup01 0:11cc2b7889af 179 vendorIdResource->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 180 vendorIdResource->set_value(value, value_length);
maygup01 0:11cc2b7889af 181 vendorIdResource->publish_value_in_registration_msg(true);
maygup01 0:11cc2b7889af 182 vendorIdResource->set_auto_observable(true);
maygup01 0:11cc2b7889af 183 }
maygup01 0:11cc2b7889af 184
maygup01 0:11cc2b7889af 185 /* get class ID */
maygup01 0:11cc2b7889af 186 err = pal_getClassGuid(&guid);
maygup01 0:11cc2b7889af 187 if (err.error == ERR_NONE) {
maygup01 0:11cc2b7889af 188 value = (uint8_t *) &guid;
maygup01 0:11cc2b7889af 189 value_length = sizeof(arm_uc_guid_t);
maygup01 0:11cc2b7889af 190 } else {
maygup01 0:11cc2b7889af 191 value = (uint8_t *) invalid_value;
maygup01 0:11cc2b7889af 192 value_length = invalid_value_size;
maygup01 0:11cc2b7889af 193 }
maygup01 0:11cc2b7889af 194
maygup01 0:11cc2b7889af 195 /* Create Update resource /10255/0/4 */
maygup01 0:11cc2b7889af 196 classIdResource = deviceMetadataInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 197 RESOURCE_VALUE(4),
maygup01 0:11cc2b7889af 198 "Class",
maygup01 0:11cc2b7889af 199 M2MResourceInstance::OPAQUE,
maygup01 0:11cc2b7889af 200 true);
maygup01 0:11cc2b7889af 201
maygup01 0:11cc2b7889af 202 if (classIdResource) {
maygup01 0:11cc2b7889af 203 classIdResource->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 204 classIdResource->set_value(value, value_length);
maygup01 0:11cc2b7889af 205 classIdResource->publish_value_in_registration_msg(true);
maygup01 0:11cc2b7889af 206 classIdResource->set_auto_observable(true);
maygup01 0:11cc2b7889af 207 }
maygup01 0:11cc2b7889af 208
maygup01 0:11cc2b7889af 209 /* get device ID */
maygup01 0:11cc2b7889af 210 err = pal_getDeviceGuid(&guid);
maygup01 0:11cc2b7889af 211 if (err.error == ERR_NONE) {
maygup01 0:11cc2b7889af 212 value = (uint8_t *) &guid;
maygup01 0:11cc2b7889af 213 value_length = sizeof(arm_uc_guid_t);
maygup01 0:11cc2b7889af 214 } else {
maygup01 0:11cc2b7889af 215 value = (uint8_t *) invalid_value;
maygup01 0:11cc2b7889af 216 value_length = invalid_value_size;
maygup01 0:11cc2b7889af 217 }
maygup01 0:11cc2b7889af 218
maygup01 0:11cc2b7889af 219 /* Create Update resource /10255/0/5 */
maygup01 0:11cc2b7889af 220 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 221 deviceIdResource = deviceMetadataInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 222 RESOURCE_VALUE(5),
maygup01 0:11cc2b7889af 223 "DeviceId",
maygup01 0:11cc2b7889af 224 M2MResourceInstance::OPAQUE,
maygup01 0:11cc2b7889af 225 true);
maygup01 0:11cc2b7889af 226 #else
maygup01 0:11cc2b7889af 227 deviceIdResource = deviceMetadataInstance->create_static_resource(
maygup01 0:11cc2b7889af 228 RESOURCE_VALUE(5),
maygup01 0:11cc2b7889af 229 "DeviceId",
maygup01 0:11cc2b7889af 230 M2MResourceInstance::OPAQUE,
maygup01 0:11cc2b7889af 231 value,
maygup01 0:11cc2b7889af 232 value_length);
maygup01 0:11cc2b7889af 233 #endif
maygup01 0:11cc2b7889af 234 if (deviceIdResource) {
maygup01 0:11cc2b7889af 235 deviceIdResource->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 236 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 237 deviceIdResource->set_value(value, value_length);
maygup01 0:11cc2b7889af 238 #endif
maygup01 0:11cc2b7889af 239 deviceIdResource->publish_value_in_registration_msg(true);
maygup01 0:11cc2b7889af 240 deviceIdResource->set_auto_observable(true);
maygup01 0:11cc2b7889af 241 }
maygup01 0:11cc2b7889af 242 }
maygup01 0:11cc2b7889af 243 }
maygup01 0:11cc2b7889af 244 }
maygup01 0:11cc2b7889af 245 }
maygup01 0:11cc2b7889af 246
maygup01 0:11cc2b7889af 247 int32_t DeviceMetadataResource::setBootloaderHash(arm_uc_buffer_t *hash)
maygup01 0:11cc2b7889af 248 {
maygup01 0:11cc2b7889af 249 UC_SRCE_TRACE("DeviceMetadataResource::setBootloaderHash ptr %p size %" PRIu32, hash, hash->size);
maygup01 0:11cc2b7889af 250
maygup01 0:11cc2b7889af 251 int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
maygup01 0:11cc2b7889af 252
maygup01 0:11cc2b7889af 253 if (bootloaderHashResource && hash && hash->size > 0) {
maygup01 0:11cc2b7889af 254 bool rt = bootloaderHashResource->set_value(hash->ptr, hash->size);
maygup01 0:11cc2b7889af 255 if (rt == true) {
maygup01 0:11cc2b7889af 256 result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
maygup01 0:11cc2b7889af 257 }
maygup01 0:11cc2b7889af 258 }
maygup01 0:11cc2b7889af 259
maygup01 0:11cc2b7889af 260 return result;
maygup01 0:11cc2b7889af 261 }
maygup01 0:11cc2b7889af 262
maygup01 0:11cc2b7889af 263 int32_t DeviceMetadataResource::setOEMBootloaderHash(arm_uc_buffer_t *hash)
maygup01 0:11cc2b7889af 264 {
maygup01 0:11cc2b7889af 265 UC_SRCE_TRACE("DeviceMetadataResource::setOEMBootloaderHash ptr %p size %" PRIu32, hash, hash->size);
maygup01 0:11cc2b7889af 266
maygup01 0:11cc2b7889af 267 int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
maygup01 0:11cc2b7889af 268
maygup01 0:11cc2b7889af 269 if (OEMBootloaderHashResource && hash && hash->size > 0) {
maygup01 0:11cc2b7889af 270 bool rt = OEMBootloaderHashResource->set_value(hash->ptr, hash->size);
maygup01 0:11cc2b7889af 271 if (rt == true) {
maygup01 0:11cc2b7889af 272 result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
maygup01 0:11cc2b7889af 273 }
maygup01 0:11cc2b7889af 274 }
maygup01 0:11cc2b7889af 275
maygup01 0:11cc2b7889af 276 return result;
maygup01 0:11cc2b7889af 277 }
maygup01 0:11cc2b7889af 278
maygup01 0:11cc2b7889af 279 M2MObject *DeviceMetadataResource::getObject()
maygup01 0:11cc2b7889af 280 {
maygup01 0:11cc2b7889af 281 Initialize();
maygup01 0:11cc2b7889af 282
maygup01 0:11cc2b7889af 283 return deviceMetadataObject;
maygup01 0:11cc2b7889af 284 }
maygup01 0:11cc2b7889af 285
maygup01 0:11cc2b7889af 286 void DeviceMetadataResource::Uninitialize()
maygup01 0:11cc2b7889af 287 {
maygup01 0:11cc2b7889af 288 UC_SRCE_TRACE("DeviceMetadataResource::Uninitialize");
maygup01 0:11cc2b7889af 289 delete deviceMetadataObject;
maygup01 0:11cc2b7889af 290 deviceMetadataObject = NULL;
maygup01 0:11cc2b7889af 291 }