Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FirmwareUpdateResource.cpp Source File

FirmwareUpdateResource.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 // Note: this macro is needed on armcc to get the the PRI*32 macros
00020 // from inttypes.h in a C++ code.
00021 #ifndef __STDC_FORMAT_MACROS
00022 #define __STDC_FORMAT_MACROS
00023 #endif
00024 
00025 #include "update-client-lwm2m/FirmwareUpdateResource.h"
00026 #include "update-client-common/arm_uc_config.h"
00027 
00028 #include "update-client-common/arm_uc_common.h"
00029 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00030 #include "mbed-client/source/include/m2mcallbackstorage.h"
00031 #include "mbed-client/m2mexecuteparams.h"
00032 #include "mbed-client/m2mbase.h"
00033 #endif
00034 #include "source/update_client_hub_state_machine.h"
00035 #include "source/update_client_hub_error_handler.h"
00036 
00037 #include <stdio.h>
00038 #include <inttypes.h>
00039 
00040 #define ARM_UCS_LWM2M_INTERNAL_ERROR (-1)
00041 #define ARM_UCS_LWM2M_INTERNAL_SUCCESS (0)
00042 
00043 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00044 #define RESOURCE_VALUE(arg) arg
00045 #else
00046 #define RESOURCE_VALUE(arg) #arg
00047 #endif
00048 
00049 namespace FirmwareUpdateResource {
00050 
00051 /* send delayed response */
00052 enum {
00053     ResourcePackage,
00054     ResourcePackageURI,
00055     ResourceUpdate
00056 };
00057 
00058 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00059 static void packageCallback(void *_parameters, const M2MExecuteParameter &params);
00060 static void packageURICallback(void *_parameters, const M2MExecuteParameter &params);
00061 static void updateCallback(void *, const M2MExecuteParameter &params);
00062 static void notificationCallback(void *client_args, const M2MBase &object, NotificationDeliveryStatus delivery_status);
00063 /* Default values are non-standard, but the standard has no
00064    values for indicating that the device is initializing.
00065    To address this, Service ignores -1 and/or 255 values coming through,
00066    so for our purposes this is the correct form of initialization.
00067 */
00068 const uint8_t defaultValue = -1;
00069 #else
00070 static void packageCallback(void *_parameters);
00071 static void updateCallback(void *);
00072 static void notificationCallback(const M2MBase &object, NotificationDeliveryStatus delivery_status, void *client_args);
00073 uint8_t defaultValue[] = {"-1"};
00074 #endif
00075 static void sendDelayedResponseTask(uintptr_t parameter);
00076 
00077 /* LWM2M Firmware Update Object */
00078 static M2MObject *updateObject;
00079 
00080 /* LWM2M Firmware Update Object resources */
00081 static M2MResource *resourcePackage = NULL;
00082 static M2MResource *resourcePackageURI = NULL;
00083 static M2MResource *resourceUpdate = NULL;
00084 static M2MResource *resourceState = NULL;
00085 static M2MResource *resourceResult = NULL;
00086 static M2MResource *resourceName = NULL;
00087 static M2MResource *resourceVersion = NULL;
00088 
00089 /* function pointers to callback functions */
00090 static void (*externalPackageCallback)(const uint8_t *buffer, uint16_t length) = NULL;
00091 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00092 static void (*externalPackageURICallback)(const uint8_t *buffer, uint16_t length) = NULL;
00093 #endif
00094 static void (*externalUpdateCallback)(void) = NULL;
00095 static void (*externalNotificationCallback)(void) = NULL;
00096 
00097 /* Callback structs for delayed response.
00098  *
00099  * There needs to be one per callback type to avoid collisions between different operations.
00100  */
00101 static arm_uc_callback_t callbackNodePackage = { NULL, 0, NULL, 0 };
00102 static arm_uc_callback_t callbackNodePackageURI = { NULL, 0, NULL, 0 };
00103 static arm_uc_callback_t callbackNodeResourceUpdate = { NULL, 0, NULL, 0 };
00104 
00105 #if defined(ARM_UC_FEATURE_FW_SOURCE_COAP) && (ARM_UC_FEATURE_FW_SOURCE_COAP == 1)
00106 /* M2MInterface */
00107 static M2MInterface *_m2m_interface;
00108 #endif
00109 }
00110 
00111 /**
00112  * @brief Initialize LWM2M Firmware Update Object
00113  * @details Sets up LWM2M object with accompanying resources.
00114  */
00115 void FirmwareUpdateResource::Initialize(void)
00116 {
00117     static bool initialized = false;
00118 
00119     if (!initialized) {
00120         /* The LWM2M Firmware Update Object for LITE client is at /10252 (Manifest) */
00121 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00122         updateObject = M2MInterfaceFactory::create_object(10252, _m2m_interface);
00123 #else
00124         updateObject = M2MInterfaceFactory::create_object("10252");
00125 #endif
00126 
00127         if (updateObject) {
00128             initialized = true;
00129 
00130 #if defined(ARM_UC_PROFILE_MBED_CLOUD_CLIENT) && (ARM_UC_PROFILE_MBED_CLOUD_CLIENT == 1)
00131             updateObject->set_register_uri(false);
00132 
00133 #endif
00134 
00135             /* Create first (and only) instance /10252/0 */
00136             M2MObjectInstance *updateInstance = updateObject->create_object_instance();
00137 
00138             if (updateInstance) {
00139 
00140 #if defined(ARM_UC_PROFILE_MBED_CLOUD_CLIENT) && (ARM_UC_PROFILE_MBED_CLOUD_CLIENT == 1)
00141                 /* Set observable so the Portal can read it */
00142                 updateInstance->set_register_uri(false);
00143 #endif
00144                 uint8_t defaultVersion[] = {"-1"};
00145 
00146                 /* Create Package resource /10252/0/1 */
00147                 resourcePackage = updateInstance->create_dynamic_resource(
00148                                       RESOURCE_VALUE(1), "Package", M2MResourceInstance::OPAQUE, false);
00149                 if (resourcePackage) {
00150                     /* This should be PUT according to the standard but
00151                        Connector client doesn't support callbacks for PUT.
00152                     */
00153                     resourcePackage->set_operation(M2MBase::POST_ALLOWED);
00154 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00155                     resourcePackage->set_execute_function((M2MResourceBase::execute_callback)packageCallback, NULL);
00156 #else
00157                     resourcePackage->set_execute_function(packageCallback);
00158 
00159                     /* The delayed response if for processing heavier loads */
00160                     resourcePackage->set_delayed_response(true);
00161 #endif
00162 
00163                 }
00164 
00165                 /* Create State resource /10252/0/2 */
00166                 resourceState = updateInstance->create_dynamic_resource(
00167                                     RESOURCE_VALUE(2), "State", M2MResourceInstance::INTEGER, true);
00168                 if (resourceState) {
00169                     resourceState->set_operation(M2MBase::GET_ALLOWED);
00170                     resourceState->set_notification_delivery_status_cb(notificationCallback, NULL);
00171 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00172                     resourceState->set_value(defaultValue);
00173 #else
00174                     resourceState->set_value(defaultValue, sizeof(defaultValue) - 1);
00175 #endif
00176                     resourceState->publish_value_in_registration_msg(true);
00177                     resourceState->set_auto_observable(true);
00178                 }
00179 
00180                 /* Create Update Result resource /10252/0/3 */
00181                 resourceResult = updateInstance->create_dynamic_resource(
00182                                      RESOURCE_VALUE(3), "UpdateResult", M2MResourceInstance::INTEGER, true);
00183                 if (resourceResult) {
00184                     resourceResult->set_operation(M2MBase::GET_ALLOWED);
00185                     resourceResult->set_notification_delivery_status_cb(notificationCallback, NULL);
00186 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00187                     resourceResult->set_value(defaultValue);
00188 #else
00189                     resourceResult->set_value(defaultValue, sizeof(defaultValue) - 1);
00190 #endif
00191                     resourceResult->publish_value_in_registration_msg(true);
00192                     resourceResult->set_auto_observable(true);
00193                 }
00194 
00195                 /* Create PkgName resource /10252/0/5 */
00196                 resourceName = updateInstance->create_dynamic_resource(
00197                                    RESOURCE_VALUE(5), "PkgName", M2MResourceInstance::STRING, true);
00198                 if (resourceName) {
00199                     resourceName->set_operation(M2MBase::GET_ALLOWED);
00200                     resourceName->set_value(defaultVersion, sizeof(defaultVersion) - 1);
00201                     resourceName->publish_value_in_registration_msg(true);
00202                     resourceName->set_auto_observable(true);
00203                 }
00204 
00205                 /* Create PkgVersion resource /10252/0/6 */
00206                 resourceVersion = updateInstance->create_dynamic_resource(
00207                                       RESOURCE_VALUE(6), "PkgVersion", M2MResourceInstance::STRING, true);
00208                 if (resourceVersion) {
00209                     resourceVersion->set_operation(M2MBase::GET_ALLOWED);
00210                     resourceVersion->set_value(defaultVersion, sizeof(defaultVersion) - 1);
00211                     resourceVersion->publish_value_in_registration_msg(true);
00212                     resourceVersion->set_auto_observable(true);
00213                 }
00214 
00215 #if !defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) || (ARM_UC_PROFILE_MBED_CLIENT_LITE == 0)
00216                 /* Create Update resource /10252/0/9 */
00217                 resourceUpdate = updateInstance->create_dynamic_resource(
00218                                      "9", "Update", M2MResourceInstance::STRING, false);
00219                 if (resourceUpdate) {
00220                     resourceUpdate->set_operation(M2MBase::POST_ALLOWED);
00221                     resourceUpdate->set_execute_function(updateCallback);
00222                     resourceUpdate->set_delayed_response(true);
00223                 }
00224 #endif
00225             }
00226         }
00227     }
00228 }
00229 
00230 M2MObject *FirmwareUpdateResource::getObject()
00231 {
00232     Initialize();
00233 
00234     return updateObject;
00235 }
00236 
00237 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00238 void FirmwareUpdateResource::packageCallback(void *_parameters, const M2MExecuteParameter &params)
00239 {
00240     UC_SRCE_TRACE("FirmwareUpdateResource::packageCallback");
00241 
00242     // Reset the resource values for every new Campaign
00243     // to make sure values of new Campaign get sent to service
00244     resourceState->set_value(defaultValue);
00245     resourceResult->set_value(defaultValue);
00246 
00247     if (externalPackageCallback) {
00248         /* read payload */
00249         const uint8_t *buffer = params.get_argument_value();
00250         uint16_t length = params.get_argument_value_length();
00251 
00252         /* invoke external callback function */
00253         externalPackageCallback(buffer, length);
00254 
00255         params.get_resource().send_post_response(params.get_token(), params.get_token_length());
00256 
00257         // TODO: Do we need to pass the token param to delayed callback
00258         // Or is above send_post_response enough?
00259         // Below callback is needed because otherwise UC Hub is not notified
00260 #else
00261 void FirmwareUpdateResource::packageCallback(void *_parameters)
00262 {
00263     UC_SRCE_TRACE("FirmwareUpdateResource::packageCallback");
00264 
00265     // Reset the resource values for every new Campaign
00266     // to make sure values of new Campaign get sent to service
00267     resourceState->set_value(defaultValue, sizeof(defaultValue) - 1);
00268     resourceResult->set_value(defaultValue, sizeof(defaultValue) - 1);
00269 
00270     if (_parameters && externalPackageCallback) {
00271         /* recast parameter */
00272         M2MResource::M2MExecuteParameter *parameters =
00273             static_cast<M2MResource::M2MExecuteParameter *>(_parameters);
00274 
00275         /* read payload */
00276         const uint8_t *buffer = parameters->get_argument_value();
00277         uint16_t length = parameters->get_argument_value_length();
00278 
00279         /* invoke external callback function */
00280         externalPackageCallback(buffer, length);
00281 #endif
00282         /* schedule delayed response */
00283         ARM_UC_PostCallback(&callbackNodePackage,
00284                             FirmwareUpdateResource::sendDelayedResponseTask,
00285                             FirmwareUpdateResource::ResourcePackage);
00286     }
00287 }
00288 
00289 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00290 void FirmwareUpdateResource::packageURICallback(void *_parameters, const M2MExecuteParameter &params)
00291 {
00292     UC_SRCE_TRACE("FirmwareUpdateResource::packageURICallback");
00293 
00294     if (_parameters && externalPackageURICallback) {
00295         /* read payload */
00296         const uint8_t *buffer = params.get_argument_value();
00297         uint16_t length = params.get_argument_value_length();
00298 
00299         /* invoke external callback function */
00300         externalPackageURICallback(buffer, length);
00301 
00302         params.get_resource().send_post_response(params.get_token(), params.get_token_length());
00303 
00304         // TODO: Do we need to pass the token param to delayed callback
00305         // Or is above send_post_response enough?
00306         // Below callback is needed because otherwise UC Hub is not notified
00307         /* schedule delayed response */
00308         ARM_UC_PostCallback(&callbackNodePackageURI,
00309                             FirmwareUpdateResource::sendDelayedResponseTask,
00310                             FirmwareUpdateResource::ResourcePackageURI);
00311     }
00312 }
00313 #endif
00314 
00315 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00316 void FirmwareUpdateResource::updateCallback(void *_parameters, const M2MExecuteParameter &params)
00317 {
00318     UC_SRCE_TRACE("FirmwareUpdateResource::updateCallback");
00319 
00320     (void) _parameters;
00321 
00322     if (externalUpdateCallback) {
00323         /* invoke external callback function */
00324         externalUpdateCallback();
00325 
00326         params.get_resource().send_post_response(params.get_token(), params.get_token_length());
00327 
00328         // TODO: Do we need to pass the token param to delayed callback
00329         // Or is above send_post_response enough?
00330 #else
00331 void FirmwareUpdateResource::updateCallback(void *_parameters)
00332 {
00333     UC_SRCE_TRACE("FirmwareUpdateResource::updateCallback");
00334 
00335     (void) _parameters;
00336 
00337     if (externalUpdateCallback) {
00338         /* invoke external callback function */
00339         externalUpdateCallback();
00340 
00341 #endif
00342         // Below callback is needed because otherwise UC Hub is not notified
00343         /* schedule delayed response */
00344         ARM_UC_PostCallback(&callbackNodeResourceUpdate,
00345                             FirmwareUpdateResource::sendDelayedResponseTask,
00346                             FirmwareUpdateResource::ResourceUpdate);
00347     }
00348 }
00349 
00350 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00351 void FirmwareUpdateResource::notificationCallback(void *client_args,
00352                                                   const M2MBase &object,
00353                                                   const NotificationDeliveryStatus delivery_status)
00354 
00355 #else
00356 void FirmwareUpdateResource::notificationCallback(const M2MBase &base,
00357                                                   const NotificationDeliveryStatus delivery_status,
00358                                                   void *client_args)
00359 #endif
00360 {
00361     UC_SRCE_TRACE("FirmwareUpdateResource::notificationCallback status: %d", delivery_status);
00362 #if defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) && (ARM_UC_PROFILE_MBED_CLIENT_LITE == 1)
00363     path_buffer buffer;
00364     object.uri_path(buffer);
00365     UC_SRCE_TRACE("Callback for resource: %s", buffer.c_str());
00366 #endif
00367 
00368     if (delivery_status == NOTIFICATION_STATUS_DELIVERED) {
00369         // Notification has been ACKed by server, complete to callback
00370         UC_SRCE_TRACE("FirmwareUpdateResource::notificationCallback DELIVERED");
00371 
00372         if (externalNotificationCallback) {
00373             externalNotificationCallback();
00374         }
00375     } else if (delivery_status == NOTIFICATION_STATUS_BUILD_ERROR ||
00376                delivery_status == NOTIFICATION_STATUS_RESEND_QUEUE_FULL ||
00377                delivery_status == NOTIFICATION_STATUS_SEND_FAILED ||
00378                delivery_status == NOTIFICATION_STATUS_UNSUBSCRIBED) {
00379         // Error case, notification not reaching service
00380         // We are sending out error because we cannot rely connection is
00381         // anymore up and the service and client are not in sync anymore.
00382         // Also sending new notifications after this might lock event
00383         // machine because comms cannot service us anymore.
00384         UC_SRCE_ERR_MSG("Received Notification delivery status: %d - ERROR!", delivery_status);
00385         ARM_UC_HUB_ErrorHandler(HUB_ERR_CONNECTION, ARM_UC_HUB_getState());
00386     } else {
00387         // NOTIFICATION_STATUS_INIT
00388         // NOTIFICATION_STATUS_SENT
00389         // NOTIFICATION_STATUS_SUBSCRIBED
00390         UC_SRCE_TRACE("FirmwareUpdateResource::notificationCallback Status ignored, waiting delivery...");
00391     }
00392 }
00393 
00394 void FirmwareUpdateResource::sendDelayedResponseTask(uintptr_t parameter)
00395 {
00396     UC_SRCE_TRACE("FirmwareUpdateResource::sendDelayedResponseTask");
00397 
00398     switch (parameter) {
00399         case FirmwareUpdateResource::ResourcePackage:
00400             UC_SRCE_TRACE("resourcePackage->send_delayed_post_response");
00401 #if !defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) || (ARM_UC_PROFILE_MBED_CLIENT_LITE == 0)
00402             resourcePackage->send_delayed_post_response();
00403 #else
00404             //called already in callback: resourcePackage->send_delayed_post_response();
00405 #endif
00406             break;
00407         case FirmwareUpdateResource::ResourcePackageURI:
00408             UC_SRCE_TRACE("resourcePackageURI->send_delayed_post_response");
00409 #if !defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) || (ARM_UC_PROFILE_MBED_CLIENT_LITE == 0)
00410             resourcePackageURI->send_delayed_post_response();
00411 #else
00412             //called already in callback: resourcePackageURI->send_delayed_post_response();
00413 #endif
00414             break;
00415         case FirmwareUpdateResource::ResourceUpdate:
00416             UC_SRCE_TRACE("resourceUpdate->send_delayed_post_response");
00417 #if !defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) || (ARM_UC_PROFILE_MBED_CLIENT_LITE == 0)
00418             resourceUpdate->send_delayed_post_response();
00419 #else
00420             //called already in callback: resourceUpdate->send_delayed_post_response();
00421 #endif
00422             break;
00423         default:
00424             UC_SRCE_ERR_MSG("unsupported resource");
00425             break;
00426     }
00427 }
00428 
00429 /*****************************************************************************/
00430 /* Update Client Source                                                      */
00431 /*****************************************************************************/
00432 
00433 /* Add callback for resource /10252/0/1, Package */
00434 int32_t FirmwareUpdateResource::addPackageCallback(void (*cb)(const uint8_t *buffer, uint16_t length))
00435 {
00436     UC_SRCE_TRACE("FirmwareUpdateResource::addPackageCallback: %p", cb);
00437 
00438     externalPackageCallback = cb;
00439 
00440     return ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00441 }
00442 
00443 #if !defined(ARM_UC_PROFILE_MBED_CLIENT_LITE) || (ARM_UC_PROFILE_MBED_CLIENT_LITE == 0)
00444 /* Add callback for resource /10252/0/9, Update */
00445 int32_t FirmwareUpdateResource::addUpdateCallback(void (*cb)(void))
00446 {
00447     UC_SRCE_TRACE("FirmwareUpdateResource::addUpdateCallback: %p", cb);
00448 
00449     externalUpdateCallback = cb;
00450 
00451     return ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00452 }
00453 #endif
00454 
00455 /* Add callback for when send{State, UpdateResult} is done */
00456 int32_t FirmwareUpdateResource::addNotificationCallback(void (*cb)(void))
00457 {
00458     UC_SRCE_TRACE("FirmwareUpdateResource::addNotificationCallback: %p", cb);
00459 
00460     externalNotificationCallback = cb;
00461 
00462     return ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00463 }
00464 
00465 /*****************************************************************************/
00466 /* Update Client Status                                                      */
00467 /*****************************************************************************/
00468 
00469 /* Send state for resource /10252/0/2, State */
00470 int32_t FirmwareUpdateResource::sendState(arm_ucs_lwm2m_state_t state)
00471 {
00472     UC_SRCE_TRACE("FirmwareUpdateResource::sendState");
00473 
00474     int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
00475 
00476     if ((resourceState != NULL)
00477             && ARM_UC_IsValidState(state)
00478             && resourceState->set_value((int64_t)state)) {
00479         result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00480     }
00481     return result;
00482 }
00483 
00484 /* Send result for resource /10252/0/3, Update Result */
00485 int32_t FirmwareUpdateResource::sendUpdateResult(arm_ucs_lwm2m_result_t updateResult)
00486 {
00487     UC_SRCE_TRACE("FirmwareUpdateResource::sendUpdateResult");
00488 
00489     int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
00490 
00491     if ((resourceResult != NULL)
00492             && ARM_UC_IsValidResult(updateResult)
00493             && resourceResult->set_value((int64_t)updateResult)) {
00494         result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00495     }
00496     return result;
00497 }
00498 
00499 /* Send name for resource /10252/0/5 PkgName */
00500 #define MAX_PACKAGE_NAME_CHARS 32
00501 int32_t FirmwareUpdateResource::sendPkgName(const uint8_t *name, uint16_t length)
00502 {
00503     UC_SRCE_TRACE("FirmwareUpdateResource::sendPkgName");
00504 
00505     int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
00506 
00507     if ((resourceName == NULL)
00508             || (name == NULL)
00509             || (length > MAX_PACKAGE_NAME_CHARS)) {
00510         UC_SRCE_ERR_MSG("bad arguments - resourceName, package name or length.");
00511     } else {
00512         /* the maximum length is defined in the OMA LWM2M standard. */
00513         uint8_t value[MAX_PACKAGE_NAME_CHARS * 2] = { 0 };
00514         uint8_t index = 0;
00515 
00516         /* convert to printable characters using lookup table */
00517         for (; (index < 32) && (index < length); index++) {
00518             value[2 * index    ] = arm_uc_hex_table[name[index] >> 4];
00519             value[2 * index + 1] = arm_uc_hex_table[name[index] & 0x0F];
00520         }
00521         if (resourceName->set_value(value, 2 * index)) {
00522             result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00523         }
00524     }
00525     return result;
00526 }
00527 
00528 /* Send version for resource /10252/0/6, PkgVersion */
00529 #define MAX_PACKAGE_VERSION_CHARS 21
00530 int32_t FirmwareUpdateResource::sendPkgVersion(uint64_t version)
00531 {
00532     UC_SRCE_TRACE("FirmwareUpdateResource::sendPkgVersion");
00533 
00534     int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
00535     if (resourceVersion != NULL) {
00536         uint8_t value[MAX_PACKAGE_VERSION_CHARS + 1] = { 0 };
00537         uint8_t length = snprintf((char *)value, MAX_PACKAGE_VERSION_CHARS, "%" PRIu64, version);
00538         if (resourceVersion->set_value(value, length)) {
00539             result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00540         }
00541     }
00542     return result;
00543 }
00544 
00545 #if defined(ARM_UC_FEATURE_FW_SOURCE_COAP) && (ARM_UC_FEATURE_FW_SOURCE_COAP == 1)
00546 int32_t FirmwareUpdateResource::setM2MInterface(M2MInterface *interface)
00547 {
00548     UC_SRCE_TRACE("FirmwareUpdateResource::setM2MInterface");
00549 
00550     int32_t result = ARM_UCS_LWM2M_INTERNAL_ERROR;
00551 
00552     if (interface != NULL) {
00553         _m2m_interface = interface;
00554         result = ARM_UCS_LWM2M_INTERNAL_SUCCESS;
00555     }
00556     return result;
00557 }
00558 
00559 M2MInterface *FirmwareUpdateResource::getM2MInterface(void)
00560 {
00561     return _m2m_interface;
00562 }
00563 #endif //ARM_UC_FEATURE_FW_SOURCE_COAP
00564 
00565 void FirmwareUpdateResource::Uninitialize(void)
00566 {
00567     UC_SRCE_TRACE("FirmwareUpdateResource::Uninitialize");
00568     delete updateObject;
00569     updateObject = NULL;
00570     resourcePackage = NULL;
00571     resourcePackageURI = NULL;
00572     resourceUpdate = NULL;
00573     resourceState = NULL;
00574     resourceResult = NULL;
00575     resourceName = NULL;
00576     resourceVersion = NULL;
00577 
00578 }