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
maygup01 0:11cc2b7889af 19 // Note: this macro is needed on armcc to get the the PRI*32 macros
maygup01 0:11cc2b7889af 20 // from inttypes.h in a C++ code.
maygup01 0:11cc2b7889af 21 #ifndef __STDC_FORMAT_MACROS
maygup01 0:11cc2b7889af 22 #define __STDC_FORMAT_MACROS
maygup01 0:11cc2b7889af 23 #endif
maygup01 0:11cc2b7889af 24
maygup01 0:11cc2b7889af 25 #include "update-client-lwm2m/FirmwareUpdateResource.h"
maygup01 0:11cc2b7889af 26 #include "update-client-common/arm_uc_config.h"
maygup01 0:11cc2b7889af 27
maygup01 0:11cc2b7889af 28 #include "update-client-common/arm_uc_common.h"
maygup01 0:11cc2b7889af 29 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 30 #include "mbed-client/source/include/m2mcallbackstorage.h"
maygup01 0:11cc2b7889af 31 #include "mbed-client/m2mexecuteparams.h"
maygup01 0:11cc2b7889af 32 #include "mbed-client/m2mbase.h"
maygup01 0:11cc2b7889af 33 #endif
maygup01 0:11cc2b7889af 34 #include "source/update_client_hub_state_machine.h"
maygup01 0:11cc2b7889af 35 #include "source/update_client_hub_error_handler.h"
maygup01 0:11cc2b7889af 36
maygup01 0:11cc2b7889af 37 #include <stdio.h>
maygup01 0:11cc2b7889af 38 #include <inttypes.h>
maygup01 0:11cc2b7889af 39
maygup01 0:11cc2b7889af 40 #define ARM_UCS_LWM2M_INTERNAL_ERROR (-1)
maygup01 0:11cc2b7889af 41 #define ARM_UCS_LWM2M_INTERNAL_SUCCESS (0)
maygup01 0:11cc2b7889af 42
maygup01 0:11cc2b7889af 43 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 44 #define RESOURCE_VALUE(arg) arg
maygup01 0:11cc2b7889af 45 #else
maygup01 0:11cc2b7889af 46 #define RESOURCE_VALUE(arg) #arg
maygup01 0:11cc2b7889af 47 #endif
maygup01 0:11cc2b7889af 48
maygup01 0:11cc2b7889af 49 namespace FirmwareUpdateResource {
maygup01 0:11cc2b7889af 50
maygup01 0:11cc2b7889af 51 /* send delayed response */
maygup01 0:11cc2b7889af 52 enum {
maygup01 0:11cc2b7889af 53 ResourcePackage,
maygup01 0:11cc2b7889af 54 ResourcePackageURI,
maygup01 0:11cc2b7889af 55 ResourceUpdate
maygup01 0:11cc2b7889af 56 };
maygup01 0:11cc2b7889af 57
maygup01 0:11cc2b7889af 58 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 59 static void packageCallback(void *_parameters, const M2MExecuteParameter &params);
maygup01 0:11cc2b7889af 60 static void packageURICallback(void *_parameters, const M2MExecuteParameter &params);
maygup01 0:11cc2b7889af 61 static void updateCallback(void *, const M2MExecuteParameter &params);
maygup01 0:11cc2b7889af 62 static void notificationCallback(void *client_args, const M2MBase &object, NotificationDeliveryStatus delivery_status);
maygup01 0:11cc2b7889af 63 /* Default values are non-standard, but the standard has no
maygup01 0:11cc2b7889af 64 values for indicating that the device is initializing.
maygup01 0:11cc2b7889af 65 To address this, Service ignores -1 and/or 255 values coming through,
maygup01 0:11cc2b7889af 66 so for our purposes this is the correct form of initialization.
maygup01 0:11cc2b7889af 67 */
maygup01 0:11cc2b7889af 68 const uint8_t defaultValue = -1;
maygup01 0:11cc2b7889af 69 #else
maygup01 0:11cc2b7889af 70 static void packageCallback(void *_parameters);
maygup01 0:11cc2b7889af 71 static void updateCallback(void *);
maygup01 0:11cc2b7889af 72 static void notificationCallback(const M2MBase &object, NotificationDeliveryStatus delivery_status, void *client_args);
maygup01 0:11cc2b7889af 73 uint8_t defaultValue[] = {"-1"};
maygup01 0:11cc2b7889af 74 #endif
maygup01 0:11cc2b7889af 75 static void sendDelayedResponseTask(uintptr_t parameter);
maygup01 0:11cc2b7889af 76
maygup01 0:11cc2b7889af 77 /* LWM2M Firmware Update Object */
maygup01 0:11cc2b7889af 78 static M2MObject *updateObject;
maygup01 0:11cc2b7889af 79
maygup01 0:11cc2b7889af 80 /* LWM2M Firmware Update Object resources */
maygup01 0:11cc2b7889af 81 static M2MResource *resourcePackage = NULL;
maygup01 0:11cc2b7889af 82 static M2MResource *resourcePackageURI = NULL;
maygup01 0:11cc2b7889af 83 static M2MResource *resourceUpdate = NULL;
maygup01 0:11cc2b7889af 84 static M2MResource *resourceState = NULL;
maygup01 0:11cc2b7889af 85 static M2MResource *resourceResult = NULL;
maygup01 0:11cc2b7889af 86 static M2MResource *resourceName = NULL;
maygup01 0:11cc2b7889af 87 static M2MResource *resourceVersion = NULL;
maygup01 0:11cc2b7889af 88
maygup01 0:11cc2b7889af 89 /* function pointers to callback functions */
maygup01 0:11cc2b7889af 90 static void (*externalPackageCallback)(const uint8_t *buffer, uint16_t length) = NULL;
maygup01 0:11cc2b7889af 91 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 92 static void (*externalPackageURICallback)(const uint8_t *buffer, uint16_t length) = NULL;
maygup01 0:11cc2b7889af 93 #endif
maygup01 0:11cc2b7889af 94 static void (*externalUpdateCallback)(void) = NULL;
maygup01 0:11cc2b7889af 95 static void (*externalNotificationCallback)(void) = NULL;
maygup01 0:11cc2b7889af 96
maygup01 0:11cc2b7889af 97 /* Callback structs for delayed response.
maygup01 0:11cc2b7889af 98 *
maygup01 0:11cc2b7889af 99 * There needs to be one per callback type to avoid collisions between different operations.
maygup01 0:11cc2b7889af 100 */
maygup01 0:11cc2b7889af 101 static arm_uc_callback_t callbackNodePackage = { NULL, 0, NULL, 0 };
maygup01 0:11cc2b7889af 102 static arm_uc_callback_t callbackNodePackageURI = { NULL, 0, NULL, 0 };
maygup01 0:11cc2b7889af 103 static arm_uc_callback_t callbackNodeResourceUpdate = { NULL, 0, NULL, 0 };
maygup01 0:11cc2b7889af 104
maygup01 0:11cc2b7889af 105 #if defined(ARM_UC_FEATURE_FW_SOURCE_COAP) && (ARM_UC_FEATURE_FW_SOURCE_COAP == 1)
maygup01 0:11cc2b7889af 106 /* M2MInterface */
maygup01 0:11cc2b7889af 107 static M2MInterface *_m2m_interface;
maygup01 0:11cc2b7889af 108 #endif
maygup01 0:11cc2b7889af 109 }
maygup01 0:11cc2b7889af 110
maygup01 0:11cc2b7889af 111 /**
maygup01 0:11cc2b7889af 112 * @brief Initialize LWM2M Firmware Update Object
maygup01 0:11cc2b7889af 113 * @details Sets up LWM2M object with accompanying resources.
maygup01 0:11cc2b7889af 114 */
maygup01 0:11cc2b7889af 115 void FirmwareUpdateResource::Initialize(void)
maygup01 0:11cc2b7889af 116 {
maygup01 0:11cc2b7889af 117 static bool initialized = false;
maygup01 0:11cc2b7889af 118
maygup01 0:11cc2b7889af 119 if (!initialized) {
maygup01 0:11cc2b7889af 120 /* The LWM2M Firmware Update Object for LITE client is at /10252 (Manifest) */
maygup01 0:11cc2b7889af 121 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 122 updateObject = M2MInterfaceFactory::create_object(10252, _m2m_interface);
maygup01 0:11cc2b7889af 123 #else
maygup01 0:11cc2b7889af 124 updateObject = M2MInterfaceFactory::create_object("10252");
maygup01 0:11cc2b7889af 125 #endif
maygup01 0:11cc2b7889af 126
maygup01 0:11cc2b7889af 127 if (updateObject) {
maygup01 0:11cc2b7889af 128 initialized = true;
maygup01 0:11cc2b7889af 129
maygup01 0:11cc2b7889af 130 #if defined(ARM_UC_PROFILE_MBED_CLOUD_CLIENT) && (ARM_UC_PROFILE_MBED_CLOUD_CLIENT == 1)
maygup01 0:11cc2b7889af 131 updateObject->set_register_uri(false);
maygup01 0:11cc2b7889af 132
maygup01 0:11cc2b7889af 133 #endif
maygup01 0:11cc2b7889af 134
maygup01 0:11cc2b7889af 135 /* Create first (and only) instance /10252/0 */
maygup01 0:11cc2b7889af 136 M2MObjectInstance *updateInstance = updateObject->create_object_instance();
maygup01 0:11cc2b7889af 137
maygup01 0:11cc2b7889af 138 if (updateInstance) {
maygup01 0:11cc2b7889af 139
maygup01 0:11cc2b7889af 140 #if defined(ARM_UC_PROFILE_MBED_CLOUD_CLIENT) && (ARM_UC_PROFILE_MBED_CLOUD_CLIENT == 1)
maygup01 0:11cc2b7889af 141 /* Set observable so the Portal can read it */
maygup01 0:11cc2b7889af 142 updateInstance->set_register_uri(false);
maygup01 0:11cc2b7889af 143 #endif
maygup01 0:11cc2b7889af 144 uint8_t defaultVersion[] = {"-1"};
maygup01 0:11cc2b7889af 145
maygup01 0:11cc2b7889af 146 /* Create Package resource /10252/0/1 */
maygup01 0:11cc2b7889af 147 resourcePackage = updateInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 148 RESOURCE_VALUE(1), "Package", M2MResourceInstance::OPAQUE, false);
maygup01 0:11cc2b7889af 149 if (resourcePackage) {
maygup01 0:11cc2b7889af 150 /* This should be PUT according to the standard but
maygup01 0:11cc2b7889af 151 Connector client doesn't support callbacks for PUT.
maygup01 0:11cc2b7889af 152 */
maygup01 0:11cc2b7889af 153 resourcePackage->set_operation(M2MBase::POST_ALLOWED);
maygup01 0:11cc2b7889af 154 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 155 resourcePackage->set_execute_function((M2MResourceBase::execute_callback)packageCallback, NULL);
maygup01 0:11cc2b7889af 156 #else
maygup01 0:11cc2b7889af 157 resourcePackage->set_execute_function(packageCallback);
maygup01 0:11cc2b7889af 158
maygup01 0:11cc2b7889af 159 /* The delayed response if for processing heavier loads */
maygup01 0:11cc2b7889af 160 resourcePackage->set_delayed_response(true);
maygup01 0:11cc2b7889af 161 #endif
maygup01 0:11cc2b7889af 162
maygup01 0:11cc2b7889af 163 }
maygup01 0:11cc2b7889af 164
maygup01 0:11cc2b7889af 165 /* Create State resource /10252/0/2 */
maygup01 0:11cc2b7889af 166 resourceState = updateInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 167 RESOURCE_VALUE(2), "State", M2MResourceInstance::INTEGER, true);
maygup01 0:11cc2b7889af 168 if (resourceState) {
maygup01 0:11cc2b7889af 169 resourceState->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 170 resourceState->set_notification_delivery_status_cb(notificationCallback, NULL);
maygup01 0:11cc2b7889af 171 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 172 resourceState->set_value(defaultValue);
maygup01 0:11cc2b7889af 173 #else
maygup01 0:11cc2b7889af 174 resourceState->set_value(defaultValue, sizeof(defaultValue) - 1);
maygup01 0:11cc2b7889af 175 #endif
maygup01 0:11cc2b7889af 176 resourceState->publish_value_in_registration_msg(true);
maygup01 0:11cc2b7889af 177 resourceState->set_auto_observable(true);
maygup01 0:11cc2b7889af 178 }
maygup01 0:11cc2b7889af 179
maygup01 0:11cc2b7889af 180 /* Create Update Result resource /10252/0/3 */
maygup01 0:11cc2b7889af 181 resourceResult = updateInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 182 RESOURCE_VALUE(3), "UpdateResult", M2MResourceInstance::INTEGER, true);
maygup01 0:11cc2b7889af 183 if (resourceResult) {
maygup01 0:11cc2b7889af 184 resourceResult->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 185 resourceResult->set_notification_delivery_status_cb(notificationCallback, NULL);
maygup01 0:11cc2b7889af 186 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 187 resourceResult->set_value(defaultValue);
maygup01 0:11cc2b7889af 188 #else
maygup01 0:11cc2b7889af 189 resourceResult->set_value(defaultValue, sizeof(defaultValue) - 1);
maygup01 0:11cc2b7889af 190 #endif
maygup01 0:11cc2b7889af 191 resourceResult->publish_value_in_registration_msg(true);
maygup01 0:11cc2b7889af 192 resourceResult->set_auto_observable(true);
maygup01 0:11cc2b7889af 193 }
maygup01 0:11cc2b7889af 194
maygup01 0:11cc2b7889af 195 /* Create PkgName resource /10252/0/5 */
maygup01 0:11cc2b7889af 196 resourceName = updateInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 197 RESOURCE_VALUE(5), "PkgName", M2MResourceInstance::STRING, true);
maygup01 0:11cc2b7889af 198 if (resourceName) {
maygup01 0:11cc2b7889af 199 resourceName->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 200 resourceName->set_value(defaultVersion, sizeof(defaultVersion) - 1);
maygup01 0:11cc2b7889af 201 resourceName->publish_value_in_registration_msg(true);
maygup01 0:11cc2b7889af 202 resourceName->set_auto_observable(true);
maygup01 0:11cc2b7889af 203 }
maygup01 0:11cc2b7889af 204
maygup01 0:11cc2b7889af 205 /* Create PkgVersion resource /10252/0/6 */
maygup01 0:11cc2b7889af 206 resourceVersion = updateInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 207 RESOURCE_VALUE(6), "PkgVersion", M2MResourceInstance::STRING, true);
maygup01 0:11cc2b7889af 208 if (resourceVersion) {
maygup01 0:11cc2b7889af 209 resourceVersion->set_operation(M2MBase::GET_ALLOWED);
maygup01 0:11cc2b7889af 210 resourceVersion->set_value(defaultVersion, sizeof(defaultVersion) - 1);
maygup01 0:11cc2b7889af 211 resourceVersion->publish_value_in_registration_msg(true);
maygup01 0:11cc2b7889af 212 resourceVersion->set_auto_observable(true);
maygup01 0:11cc2b7889af 213 }
maygup01 0:11cc2b7889af 214
maygup01 0:11cc2b7889af 215 #if !defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) || (ARM_UC_PROFILE_MBED_CLIENT_LITE == 0)
maygup01 0:11cc2b7889af 216 /* Create Update resource /10252/0/9 */
maygup01 0:11cc2b7889af 217 resourceUpdate = updateInstance->create_dynamic_resource(
maygup01 0:11cc2b7889af 218 "9", "Update", M2MResourceInstance::STRING, false);
maygup01 0:11cc2b7889af 219 if (resourceUpdate) {
maygup01 0:11cc2b7889af 220 resourceUpdate->set_operation(M2MBase::POST_ALLOWED);
maygup01 0:11cc2b7889af 221 resourceUpdate->set_execute_function(updateCallback);
maygup01 0:11cc2b7889af 222 resourceUpdate->set_delayed_response(true);
maygup01 0:11cc2b7889af 223 }
maygup01 0:11cc2b7889af 224 #endif
maygup01 0:11cc2b7889af 225 }
maygup01 0:11cc2b7889af 226 }
maygup01 0:11cc2b7889af 227 }
maygup01 0:11cc2b7889af 228 }
maygup01 0:11cc2b7889af 229
maygup01 0:11cc2b7889af 230 M2MObject *FirmwareUpdateResource::getObject()
maygup01 0:11cc2b7889af 231 {
maygup01 0:11cc2b7889af 232 Initialize();
maygup01 0:11cc2b7889af 233
maygup01 0:11cc2b7889af 234 return updateObject;
maygup01 0:11cc2b7889af 235 }
maygup01 0:11cc2b7889af 236
maygup01 0:11cc2b7889af 237 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 238 void FirmwareUpdateResource::packageCallback(void *_parameters, const M2MExecuteParameter &params)
maygup01 0:11cc2b7889af 239 {
maygup01 0:11cc2b7889af 240 UC_SRCE_TRACE("FirmwareUpdateResource::packageCallback");
maygup01 0:11cc2b7889af 241
maygup01 0:11cc2b7889af 242 // Reset the resource values for every new Campaign
maygup01 0:11cc2b7889af 243 // to make sure values of new Campaign get sent to service
maygup01 0:11cc2b7889af 244 resourceState->set_value(defaultValue);
maygup01 0:11cc2b7889af 245 resourceResult->set_value(defaultValue);
maygup01 0:11cc2b7889af 246
maygup01 0:11cc2b7889af 247 if (externalPackageCallback) {
maygup01 0:11cc2b7889af 248 /* read payload */
maygup01 0:11cc2b7889af 249 const uint8_t *buffer = params.get_argument_value();
maygup01 0:11cc2b7889af 250 uint16_t length = params.get_argument_value_length();
maygup01 0:11cc2b7889af 251
maygup01 0:11cc2b7889af 252 /* invoke external callback function */
maygup01 0:11cc2b7889af 253 externalPackageCallback(buffer, length);
maygup01 0:11cc2b7889af 254
maygup01 0:11cc2b7889af 255 params.get_resource().send_post_response(params.get_token(), params.get_token_length());
maygup01 0:11cc2b7889af 256
maygup01 0:11cc2b7889af 257 // TODO: Do we need to pass the token param to delayed callback
maygup01 0:11cc2b7889af 258 // Or is above send_post_response enough?
maygup01 0:11cc2b7889af 259 // Below callback is needed because otherwise UC Hub is not notified
maygup01 0:11cc2b7889af 260 #else
maygup01 0:11cc2b7889af 261 void FirmwareUpdateResource::packageCallback(void *_parameters)
maygup01 0:11cc2b7889af 262 {
maygup01 0:11cc2b7889af 263 UC_SRCE_TRACE("FirmwareUpdateResource::packageCallback");
maygup01 0:11cc2b7889af 264
maygup01 0:11cc2b7889af 265 // Reset the resource values for every new Campaign
maygup01 0:11cc2b7889af 266 // to make sure values of new Campaign get sent to service
maygup01 0:11cc2b7889af 267 resourceState->set_value(defaultValue, sizeof(defaultValue) - 1);
maygup01 0:11cc2b7889af 268 resourceResult->set_value(defaultValue, sizeof(defaultValue) - 1);
maygup01 0:11cc2b7889af 269
maygup01 0:11cc2b7889af 270 if (_parameters && externalPackageCallback) {
maygup01 0:11cc2b7889af 271 /* recast parameter */
maygup01 0:11cc2b7889af 272 M2MResource::M2MExecuteParameter *parameters =
maygup01 0:11cc2b7889af 273 static_cast<M2MResource::M2MExecuteParameter *>(_parameters);
maygup01 0:11cc2b7889af 274
maygup01 0:11cc2b7889af 275 /* read payload */
maygup01 0:11cc2b7889af 276 const uint8_t *buffer = parameters->get_argument_value();
maygup01 0:11cc2b7889af 277 uint16_t length = parameters->get_argument_value_length();
maygup01 0:11cc2b7889af 278
maygup01 0:11cc2b7889af 279 /* invoke external callback function */
maygup01 0:11cc2b7889af 280 externalPackageCallback(buffer, length);
maygup01 0:11cc2b7889af 281 #endif
maygup01 0:11cc2b7889af 282 /* schedule delayed response */
maygup01 0:11cc2b7889af 283 ARM_UC_PostCallback(&callbackNodePackage,
maygup01 0:11cc2b7889af 284 FirmwareUpdateResource::sendDelayedResponseTask,
maygup01 0:11cc2b7889af 285 FirmwareUpdateResource::ResourcePackage);
maygup01 0:11cc2b7889af 286 }
maygup01 0:11cc2b7889af 287 }
maygup01 0:11cc2b7889af 288
maygup01 0:11cc2b7889af 289 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 290 void FirmwareUpdateResource::packageURICallback(void *_parameters, const M2MExecuteParameter &params)
maygup01 0:11cc2b7889af 291 {
maygup01 0:11cc2b7889af 292 UC_SRCE_TRACE("FirmwareUpdateResource::packageURICallback");
maygup01 0:11cc2b7889af 293
maygup01 0:11cc2b7889af 294 if (_parameters && externalPackageURICallback) {
maygup01 0:11cc2b7889af 295 /* read payload */
maygup01 0:11cc2b7889af 296 const uint8_t *buffer = params.get_argument_value();
maygup01 0:11cc2b7889af 297 uint16_t length = params.get_argument_value_length();
maygup01 0:11cc2b7889af 298
maygup01 0:11cc2b7889af 299 /* invoke external callback function */
maygup01 0:11cc2b7889af 300 externalPackageURICallback(buffer, length);
maygup01 0:11cc2b7889af 301
maygup01 0:11cc2b7889af 302 params.get_resource().send_post_response(params.get_token(), params.get_token_length());
maygup01 0:11cc2b7889af 303
maygup01 0:11cc2b7889af 304 // TODO: Do we need to pass the token param to delayed callback
maygup01 0:11cc2b7889af 305 // Or is above send_post_response enough?
maygup01 0:11cc2b7889af 306 // Below callback is needed because otherwise UC Hub is not notified
maygup01 0:11cc2b7889af 307 /* schedule delayed response */
maygup01 0:11cc2b7889af 308 ARM_UC_PostCallback(&callbackNodePackageURI,
maygup01 0:11cc2b7889af 309 FirmwareUpdateResource::sendDelayedResponseTask,
maygup01 0:11cc2b7889af 310 FirmwareUpdateResource::ResourcePackageURI);
maygup01 0:11cc2b7889af 311 }
maygup01 0:11cc2b7889af 312 }
maygup01 0:11cc2b7889af 313 #endif
maygup01 0:11cc2b7889af 314
maygup01 0:11cc2b7889af 315 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 316 void FirmwareUpdateResource::updateCallback(void *_parameters, const M2MExecuteParameter &params)
maygup01 0:11cc2b7889af 317 {
maygup01 0:11cc2b7889af 318 UC_SRCE_TRACE("FirmwareUpdateResource::updateCallback");
maygup01 0:11cc2b7889af 319
maygup01 0:11cc2b7889af 320 (void) _parameters;
maygup01 0:11cc2b7889af 321
maygup01 0:11cc2b7889af 322 if (externalUpdateCallback) {
maygup01 0:11cc2b7889af 323 /* invoke external callback function */
maygup01 0:11cc2b7889af 324 externalUpdateCallback();
maygup01 0:11cc2b7889af 325
maygup01 0:11cc2b7889af 326 params.get_resource().send_post_response(params.get_token(), params.get_token_length());
maygup01 0:11cc2b7889af 327
maygup01 0:11cc2b7889af 328 // TODO: Do we need to pass the token param to delayed callback
maygup01 0:11cc2b7889af 329 // Or is above send_post_response enough?
maygup01 0:11cc2b7889af 330 #else
maygup01 0:11cc2b7889af 331 void FirmwareUpdateResource::updateCallback(void *_parameters)
maygup01 0:11cc2b7889af 332 {
maygup01 0:11cc2b7889af 333 UC_SRCE_TRACE("FirmwareUpdateResource::updateCallback");
maygup01 0:11cc2b7889af 334
maygup01 0:11cc2b7889af 335 (void) _parameters;
maygup01 0:11cc2b7889af 336
maygup01 0:11cc2b7889af 337 if (externalUpdateCallback) {
maygup01 0:11cc2b7889af 338 /* invoke external callback function */
maygup01 0:11cc2b7889af 339 externalUpdateCallback();
maygup01 0:11cc2b7889af 340
maygup01 0:11cc2b7889af 341 #endif
maygup01 0:11cc2b7889af 342 // Below callback is needed because otherwise UC Hub is not notified
maygup01 0:11cc2b7889af 343 /* schedule delayed response */
maygup01 0:11cc2b7889af 344 ARM_UC_PostCallback(&callbackNodeResourceUpdate,
maygup01 0:11cc2b7889af 345 FirmwareUpdateResource::sendDelayedResponseTask,
maygup01 0:11cc2b7889af 346 FirmwareUpdateResource::ResourceUpdate);
maygup01 0:11cc2b7889af 347 }
maygup01 0:11cc2b7889af 348 }
maygup01 0:11cc2b7889af 349
maygup01 0:11cc2b7889af 350 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 351 void FirmwareUpdateResource::notificationCallback(void *client_args,
maygup01 0:11cc2b7889af 352 const M2MBase &object,
maygup01 0:11cc2b7889af 353 const NotificationDeliveryStatus delivery_status)
maygup01 0:11cc2b7889af 354
maygup01 0:11cc2b7889af 355 #else
maygup01 0:11cc2b7889af 356 void FirmwareUpdateResource::notificationCallback(const M2MBase &base,
maygup01 0:11cc2b7889af 357 const NotificationDeliveryStatus delivery_status,
maygup01 0:11cc2b7889af 358 void *client_args)
maygup01 0:11cc2b7889af 359 #endif
maygup01 0:11cc2b7889af 360 {
maygup01 0:11cc2b7889af 361 UC_SRCE_TRACE("FirmwareUpdateResource::notificationCallback status: %d", delivery_status);
maygup01 0:11cc2b7889af 362 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
maygup01 0:11cc2b7889af 363 path_buffer buffer;
maygup01 0:11cc2b7889af 364 object.uri_path(buffer);
maygup01 0:11cc2b7889af 365 UC_SRCE_TRACE("Callback for resource: %s", buffer.c_str());
maygup01 0:11cc2b7889af 366 #endif
maygup01 0:11cc2b7889af 367
maygup01 0:11cc2b7889af 368 if (delivery_status == NOTIFICATION_STATUS_DELIVERED) {
maygup01 0:11cc2b7889af 369 // Notification has been ACKed by server, complete to callback
maygup01 0:11cc2b7889af 370 UC_SRCE_TRACE("FirmwareUpdateResource::notificationCallback DELIVERED");
maygup01 0:11cc2b7889af 371
maygup01 0:11cc2b7889af 372 if (externalNotificationCallback) {
maygup01 0:11cc2b7889af 373 externalNotificationCallback();
maygup01 0:11cc2b7889af 374 }
maygup01 0:11cc2b7889af 375 } else if (delivery_status == NOTIFICATION_STATUS_BUILD_ERROR ||
maygup01 0:11cc2b7889af 376 delivery_status == NOTIFICATION_STATUS_RESEND_QUEUE_FULL ||
maygup01 0:11cc2b7889af 377 delivery_status == NOTIFICATION_STATUS_SEND_FAILED ||
maygup01 0:11cc2b7889af 378 delivery_status == NOTIFICATION_STATUS_UNSUBSCRIBED) {
maygup01 0:11cc2b7889af 379 // Error case, notification not reaching service
maygup01 0:11cc2b7889af 380 // We are sending out error because we cannot rely connection is
maygup01 0:11cc2b7889af 381 // anymore up and the service and client are not in sync anymore.
maygup01 0:11cc2b7889af 382 // Also sending new notifications after this might lock event
maygup01 0:11cc2b7889af 383 // machine because comms cannot service us anymore.
maygup01 0:11cc2b7889af 384 UC_SRCE_ERR_MSG("Received Notification delivery status: %d - ERROR!", delivery_status);
maygup01 0:11cc2b7889af 385 ARM_UC_HUB_ErrorHandler(HUB_ERR_CONNECTION, ARM_UC_HUB_getState());
maygup01 0:11cc2b7889af 386 } else {
maygup01 0:11cc2b7889af 387 // NOTIFICATION_STATUS_INIT
maygup01 0:11cc2b7889af 388 // NOTIFICATION_STATUS_SENT
maygup01 0:11cc2b7889af 389 // NOTIFICATION_STATUS_SUBSCRIBED
maygup01 0:11cc2b7889af 390 UC_SRCE_TRACE("FirmwareUpdateResource::notificationCallback Status ignored, waiting delivery...");
maygup01 0:11cc2b7889af 391 }
maygup01 0:11cc2b7889af 392 }
maygup01 0:11cc2b7889af 393
maygup01 0:11cc2b7889af 394 void FirmwareUpdateResource::sendDelayedResponseTask(uintptr_t parameter)
maygup01 0:11cc2b7889af 395 {
maygup01 0:11cc2b7889af 396 UC_SRCE_TRACE("FirmwareUpdateResource::sendDelayedResponseTask");
maygup01 0:11cc2b7889af 397
maygup01 0:11cc2b7889af 398 switch (parameter) {
maygup01 0:11cc2b7889af 399 case FirmwareUpdateResource::ResourcePackage:
maygup01 0:11cc2b7889af 400 UC_SRCE_TRACE("resourcePackage->send_delayed_post_response");
maygup01 0:11cc2b7889af 401 #if !defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) || (ARM_UC_PROFILE_MBED_CLIENT_LITE == 0)
maygup01 0:11cc2b7889af 402 resourcePackage->send_delayed_post_response();
maygup01 0:11cc2b7889af 403 #else
maygup01 0:11cc2b7889af 404 //called already in callback: resourcePackage->send_delayed_post_response();
maygup01 0:11cc2b7889af 405 #endif
maygup01 0:11cc2b7889af 406 break;
maygup01 0:11cc2b7889af 407 case FirmwareUpdateResource::ResourcePackageURI:
maygup01 0:11cc2b7889af 408 UC_SRCE_TRACE("resourcePackageURI->send_delayed_post_response");
maygup01 0:11cc2b7889af 409 #if !defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) || (ARM_UC_PROFILE_MBED_CLIENT_LITE == 0)
maygup01 0:11cc2b7889af 410 resourcePackageURI->send_delayed_post_response();
maygup01 0:11cc2b7889af 411 #else
maygup01 0:11cc2b7889af 412 //called already in callback: resourcePackageURI->send_delayed_post_response();
maygup01 0:11cc2b7889af 413 #endif
maygup01 0:11cc2b7889af 414 break;
maygup01 0:11cc2b7889af 415 case FirmwareUpdateResource::ResourceUpdate:
maygup01 0:11cc2b7889af 416 UC_SRCE_TRACE("resourceUpdate->send_delayed_post_response");
maygup01 0:11cc2b7889af 417 #if !defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) || (ARM_UC_PROFILE_MBED_CLIENT_LITE == 0)
maygup01 0:11cc2b7889af 418 resourceUpdate->send_delayed_post_response();
maygup01 0:11cc2b7889af 419 #else
maygup01 0:11cc2b7889af 420 //called already in callback: resourceUpdate->send_delayed_post_response();
maygup01 0:11cc2b7889af 421 #endif
maygup01 0:11cc2b7889af 422 break;
maygup01 0:11cc2b7889af 423 default:
maygup01 0:11cc2b7889af 424 UC_SRCE_ERR_MSG("unsupported resource");
maygup01 0:11cc2b7889af 425 break;
maygup01 0:11cc2b7889af 426 }
maygup01 0:11cc2b7889af 427 }
maygup01 0:11cc2b7889af 428
maygup01 0:11cc2b7889af 429 /*****************************************************************************/
maygup01 0:11cc2b7889af 430 /* Update Client Source */
maygup01 0:11cc2b7889af 431 /*****************************************************************************/
maygup01 0:11cc2b7889af 432
maygup01 0:11cc2b7889af 433 /* Add callback for resource /10252/0/1, Package */
maygup01 0:11cc2b7889af 434 int32_t FirmwareUpdateResource::addPackageCallback(void (*cb)(const uint8_t *buffer, uint16_t length))
maygup01 0:11cc2b7889af 435 {
maygup01 0:11cc2b7889af 436 UC_SRCE_TRACE("FirmwareUpdateResource::addPackageCallback: %p", cb);
maygup01 0:11cc2b7889af 437
maygup01 0:11cc2b7889af 438 externalPackageCallback = cb;
maygup01 0:11cc2b7889af 439
maygup01 0:11cc2b7889af 440 return ARM_UCS_LWM2M_INTERNAL_SUCCESS;
maygup01 0:11cc2b7889af 441 }
maygup01 0:11cc2b7889af 442
maygup01 0:11cc2b7889af 443 #if !defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) || (ARM_UC_PROFILE_MBED_CLIENT_LITE == 0)
maygup01 0:11cc2b7889af 444 /* Add callback for resource /10252/0/9, Update */
maygup01 0:11cc2b7889af 445 int32_t FirmwareUpdateResource::addUpdateCallback(void (*cb)(void))
maygup01 0:11cc2b7889af 446 {
maygup01 0:11cc2b7889af 447 UC_SRCE_TRACE("FirmwareUpdateResource::addUpdateCallback: %p", cb);
maygup01 0:11cc2b7889af 448
maygup01 0:11cc2b7889af 449 externalUpdateCallback = cb;
maygup01 0:11cc2b7889af 450
maygup01 0:11cc2b7889af 451 return ARM_UCS_LWM2M_INTERNAL_SUCCESS;
maygup01 0:11cc2b7889af 452 }
maygup01 0:11cc2b7889af 453 #endif
maygup01 0:11cc2b7889af 454
maygup01 0:11cc2b7889af 455 /* Add callback for when send{State, UpdateResult} is done */
maygup01 0:11cc2b7889af 456 int32_t FirmwareUpdateResource::addNotificationCallback(void (*cb)(void))
maygup01 0:11cc2b7889af 457 {
maygup01 0:11cc2b7889af 458 UC_SRCE_TRACE("FirmwareUpdateResource::addNotificationCallback: %p", cb);
maygup01 0:11cc2b7889af 459
maygup01 0:11cc2b7889af 460 externalNotificationCallback = cb;
maygup01 0:11cc2b7889af 461
maygup01 0:11cc2b7889af 462 return ARM_UCS_LWM2M_INTERNAL_SUCCESS;
maygup01 0:11cc2b7889af 463 }
maygup01 0:11cc2b7889af 464
maygup01 0:11cc2b7889af 465 /*****************************************************************************/
maygup01 0:11cc2b7889af 466 /* Update Client Status */
maygup01 0:11cc2b7889af 467 /*****************************************************************************/
maygup01 0:11cc2b7889af 468
maygup01 0:11cc2b7889af 469 /* Send state for resource /10252/0/2, State */
maygup01 0:11cc2b7889af 470 int32_t FirmwareUpdateResource::sendState(arm_ucs_lwm2m_state_t state)
maygup01 0:11cc2b7889af 471 {
maygup01 0:11cc2b7889af 472 UC_SRCE_TRACE("FirmwareUpdateResource::sendState");
maygup01 0:11cc2b7889af 473
maygup01 0:11cc2b7889af 474 int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
maygup01 0:11cc2b7889af 475
maygup01 0:11cc2b7889af 476 if ((resourceState != NULL)
maygup01 0:11cc2b7889af 477 && ARM_UC_IsValidState(state)
maygup01 0:11cc2b7889af 478 && resourceState->set_value((int64_t)state)) {
maygup01 0:11cc2b7889af 479 result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
maygup01 0:11cc2b7889af 480 }
maygup01 0:11cc2b7889af 481 return result;
maygup01 0:11cc2b7889af 482 }
maygup01 0:11cc2b7889af 483
maygup01 0:11cc2b7889af 484 /* Send result for resource /10252/0/3, Update Result */
maygup01 0:11cc2b7889af 485 int32_t FirmwareUpdateResource::sendUpdateResult(arm_ucs_lwm2m_result_t updateResult)
maygup01 0:11cc2b7889af 486 {
maygup01 0:11cc2b7889af 487 UC_SRCE_TRACE("FirmwareUpdateResource::sendUpdateResult");
maygup01 0:11cc2b7889af 488
maygup01 0:11cc2b7889af 489 int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
maygup01 0:11cc2b7889af 490
maygup01 0:11cc2b7889af 491 if ((resourceResult != NULL)
maygup01 0:11cc2b7889af 492 && ARM_UC_IsValidResult(updateResult)
maygup01 0:11cc2b7889af 493 && resourceResult->set_value((int64_t)updateResult)) {
maygup01 0:11cc2b7889af 494 result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
maygup01 0:11cc2b7889af 495 }
maygup01 0:11cc2b7889af 496 return result;
maygup01 0:11cc2b7889af 497 }
maygup01 0:11cc2b7889af 498
maygup01 0:11cc2b7889af 499 /* Send name for resource /10252/0/5 PkgName */
maygup01 0:11cc2b7889af 500 #define MAX_PACKAGE_NAME_CHARS 32
maygup01 0:11cc2b7889af 501 int32_t FirmwareUpdateResource::sendPkgName(const uint8_t *name, uint16_t length)
maygup01 0:11cc2b7889af 502 {
maygup01 0:11cc2b7889af 503 UC_SRCE_TRACE("FirmwareUpdateResource::sendPkgName");
maygup01 0:11cc2b7889af 504
maygup01 0:11cc2b7889af 505 int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
maygup01 0:11cc2b7889af 506
maygup01 0:11cc2b7889af 507 if ((resourceName == NULL)
maygup01 0:11cc2b7889af 508 || (name == NULL)
maygup01 0:11cc2b7889af 509 || (length > MAX_PACKAGE_NAME_CHARS)) {
maygup01 0:11cc2b7889af 510 UC_SRCE_ERR_MSG("bad arguments - resourceName, package name or length.");
maygup01 0:11cc2b7889af 511 } else {
maygup01 0:11cc2b7889af 512 /* the maximum length is defined in the OMA LWM2M standard. */
maygup01 0:11cc2b7889af 513 uint8_t value[MAX_PACKAGE_NAME_CHARS * 2] = { 0 };
maygup01 0:11cc2b7889af 514 uint8_t index = 0;
maygup01 0:11cc2b7889af 515
maygup01 0:11cc2b7889af 516 /* convert to printable characters using lookup table */
maygup01 0:11cc2b7889af 517 for (; (index < 32) && (index < length); index++) {
maygup01 0:11cc2b7889af 518 value[2 * index ] = arm_uc_hex_table[name[index] >> 4];
maygup01 0:11cc2b7889af 519 value[2 * index + 1] = arm_uc_hex_table[name[index] & 0x0F];
maygup01 0:11cc2b7889af 520 }
maygup01 0:11cc2b7889af 521 if (resourceName->set_value(value, 2 * index)) {
maygup01 0:11cc2b7889af 522 result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
maygup01 0:11cc2b7889af 523 }
maygup01 0:11cc2b7889af 524 }
maygup01 0:11cc2b7889af 525 return result;
maygup01 0:11cc2b7889af 526 }
maygup01 0:11cc2b7889af 527
maygup01 0:11cc2b7889af 528 /* Send version for resource /10252/0/6, PkgVersion */
maygup01 0:11cc2b7889af 529 #define MAX_PACKAGE_VERSION_CHARS 21
maygup01 0:11cc2b7889af 530 int32_t FirmwareUpdateResource::sendPkgVersion(uint64_t version)
maygup01 0:11cc2b7889af 531 {
maygup01 0:11cc2b7889af 532 UC_SRCE_TRACE("FirmwareUpdateResource::sendPkgVersion");
maygup01 0:11cc2b7889af 533
maygup01 0:11cc2b7889af 534 int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
maygup01 0:11cc2b7889af 535 if (resourceVersion != NULL) {
maygup01 0:11cc2b7889af 536 uint8_t value[MAX_PACKAGE_VERSION_CHARS + 1] = { 0 };
maygup01 0:11cc2b7889af 537 uint8_t length = snprintf((char *)value, MAX_PACKAGE_VERSION_CHARS, "%" PRIu64, version);
maygup01 0:11cc2b7889af 538 if (resourceVersion->set_value(value, length)) {
maygup01 0:11cc2b7889af 539 result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
maygup01 0:11cc2b7889af 540 }
maygup01 0:11cc2b7889af 541 }
maygup01 0:11cc2b7889af 542 return result;
maygup01 0:11cc2b7889af 543 }
maygup01 0:11cc2b7889af 544
maygup01 0:11cc2b7889af 545 #if defined(ARM_UC_FEATURE_FW_SOURCE_COAP) && (ARM_UC_FEATURE_FW_SOURCE_COAP == 1)
maygup01 0:11cc2b7889af 546 int32_t FirmwareUpdateResource::setM2MInterface(M2MInterface *interface)
maygup01 0:11cc2b7889af 547 {
maygup01 0:11cc2b7889af 548 UC_SRCE_TRACE("FirmwareUpdateResource::setM2MInterface");
maygup01 0:11cc2b7889af 549
maygup01 0:11cc2b7889af 550 int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
maygup01 0:11cc2b7889af 551
maygup01 0:11cc2b7889af 552 if (interface != NULL) {
maygup01 0:11cc2b7889af 553 _m2m_interface = interface;
maygup01 0:11cc2b7889af 554 result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
maygup01 0:11cc2b7889af 555 }
maygup01 0:11cc2b7889af 556 return result;
maygup01 0:11cc2b7889af 557 }
maygup01 0:11cc2b7889af 558
maygup01 0:11cc2b7889af 559 M2MInterface *FirmwareUpdateResource::getM2MInterface(void)
maygup01 0:11cc2b7889af 560 {
maygup01 0:11cc2b7889af 561 return _m2m_interface;
maygup01 0:11cc2b7889af 562 }
maygup01 0:11cc2b7889af 563 #endif //ARM_UC_FEATURE_FW_SOURCE_COAP
maygup01 0:11cc2b7889af 564
maygup01 0:11cc2b7889af 565 void FirmwareUpdateResource::Uninitialize(void)
maygup01 0:11cc2b7889af 566 {
maygup01 0:11cc2b7889af 567 UC_SRCE_TRACE("FirmwareUpdateResource::Uninitialize");
maygup01 0:11cc2b7889af 568 delete updateObject;
maygup01 0:11cc2b7889af 569 updateObject = NULL;
maygup01 0:11cc2b7889af 570 resourcePackage = NULL;
maygup01 0:11cc2b7889af 571 resourcePackageURI = NULL;
maygup01 0:11cc2b7889af 572 resourceUpdate = NULL;
maygup01 0:11cc2b7889af 573 resourceState = NULL;
maygup01 0:11cc2b7889af 574 resourceResult = NULL;
maygup01 0:11cc2b7889af 575 resourceName = NULL;
maygup01 0:11cc2b7889af 576 resourceVersion = NULL;
maygup01 0:11cc2b7889af 577
maygup01 0:11cc2b7889af 578 }