Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lwm2m-monitor.cpp Source File

lwm2m-monitor.cpp

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2017 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include "update-lwm2m-mbed-apis.h"
00020 #include "update-client-lwm2m/lwm2m-monitor.h"
00021 #include "update-client-lwm2m/FirmwareUpdateResource.h"
00022 #include "update-client-lwm2m/DeviceMetadataResource.h"
00023 
00024 /**
00025  * @brief Get driver version.
00026  * @return Driver version.
00027  */
00028 uint32_t ARM_UCS_LWM2M_MONITOR_GetVersion(void)
00029 {
00030     return 0;
00031 }
00032 
00033 /**
00034  * @brief Get Source capabilities.
00035  * @return Struct containing capabilites. See definition above.
00036  */
00037 ARM_MONITOR_CAPABILITIES ARM_UCS_LWM2M_MONITOR_GetCapabilities(void)
00038 {
00039     ARM_MONITOR_CAPABILITIES result;
00040     result.state   = 1;
00041     result.result  = 1;
00042     result.version = 1;
00043 
00044     return result;
00045 }
00046 
00047 /**
00048  * @brief Initialize Monitor.
00049  * @return Error code.
00050  */
00051 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_Initialize(void (*notification_handler)(void))
00052 {
00053     ARM_UC_INIT_ERROR(retval, ERR_NONE);
00054 
00055     FirmwareUpdateResource::Initialize();
00056     FirmwareUpdateResource::addNotificationCallback(notification_handler);
00057 
00058     DeviceMetadataResource::Initialize();
00059 
00060     return retval;
00061 }
00062 
00063 /**
00064  * @brief Uninitialized Monitor.
00065  * @return Error code.
00066  */
00067 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_Uninitialize(void)
00068 {
00069     ARM_UC_INIT_ERROR(retval, ERR_NONE);
00070     return retval;
00071 }
00072 
00073 /**
00074  * @brief Send Update Client state.
00075  * @details From the OMA LWM2M Technical Specification:
00076  *
00077  *          Indicates current state with respect to this firmware update.
00078  *          This value is set by the LWM2M Client in accordance with state
00079  *          and arm_uc_monitor_state_t type.
00080  *
00081  * @return Error code.
00082  */
00083 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SendState(arm_uc_monitor_state_t an_update_state)
00084 {
00085     ARM_UC_INIT_ERROR(result, ERR_NONE);
00086 
00087     // If out of range of a legitimate update-state, return an "invalid-parameter" error to the caller,
00088     //   otherwise try send the new state to the monitor.
00089     if (!ARM_UC_IsValidState(an_update_state)) {
00090         ARM_UC_SET_ERROR(result, ERR_INVALID_PARAMETER);
00091     } else {
00092         FirmwareUpdateResource::arm_ucs_lwm2m_state_t state =
00093             (FirmwareUpdateResource::arm_ucs_lwm2m_state_t)an_update_state;
00094         if (FirmwareUpdateResource::sendState(state) != 0) {
00095             ARM_UC_SET_ERROR(result, ERR_UNSPECIFIED);
00096         }
00097     }
00098     return result;
00099 }
00100 
00101 /**
00102  * @brief Send update result.
00103  * @details From the OMA LWM2M Technical Specification:
00104  *          Contains the result of downloading or updating the firmware
00105  *          This Resource MAY be reported by sending Observe operation.
00106  *
00107  * @return Error code.
00108  */
00109 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SendUpdateResult(arm_uc_monitor_result_t an_update_result)
00110 {
00111     ARM_UC_INIT_ERROR(result, ERR_NONE);
00112 
00113     // If out of range of a legitimate update-result, send an "unspecified-error" result.
00114     if (!ARM_UC_IsValidResult(an_update_result)) {
00115         ARM_UC_SET_ERROR(result, ERR_INVALID_PARAMETER);
00116     } else {
00117         // Cast the arm_uc_monitor_result_t to a arm_ucs_lwm2m_result_t, and send it.
00118         FirmwareUpdateResource::arm_ucs_lwm2m_result_t code =
00119             (FirmwareUpdateResource::arm_ucs_lwm2m_result_t)an_update_result;
00120         if (FirmwareUpdateResource::sendUpdateResult(code) != 0) {
00121             ARM_UC_SET_ERROR(result, ERR_UNSPECIFIED);
00122         }
00123     }
00124     return result;
00125 }
00126 
00127 /**
00128  * @brief Send current firmware name.
00129  * @details The firmware name is the SHA256 hash.
00130  * @param name Pointer to buffer struct. Hash is stored as byte array.
00131  * @return Error code.
00132  */
00133 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SendName(arm_uc_buffer_t *name)
00134 {
00135     ARM_UC_INIT_ERROR(result, ERR_NONE);
00136     if (!name || !name->ptr) {
00137         ARM_UC_SET_ERROR(result, ERR_INVALID_PARAMETER);
00138     } else if (FirmwareUpdateResource::sendPkgName(name->ptr, name->size) != 0) {
00139         ARM_UC_SET_ERROR(result, ERR_UNSPECIFIED);
00140     }
00141     return result;
00142 }
00143 
00144 /**
00145  * @brief Send current firmware version.
00146  * @details The firmware version is the timestamp from the manifest that
00147  *          authorized the firmware.
00148  * @param version Timestamp, 64 bit unsigned integer.
00149  * @return Error code.
00150  */
00151 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SendVersion(uint64_t version)
00152 {
00153     ARM_UC_INIT_ERROR(result, ERR_NONE);
00154     if (FirmwareUpdateResource::sendPkgVersion(version) != 0) {
00155         ARM_UC_SET_ERROR(result, ERR_UNSPECIFIED);
00156     }
00157     return result;
00158 }
00159 
00160 /**
00161  * @brief Set the bootloader hash.
00162  * @details The bootloader hash is a hash of the bootloader. This is
00163  *          used for tracking the version of the bootloader used.
00164  * @param name Pointer to buffer struct. Hash is stored as byte array.
00165  * @return Error code.
00166  */
00167 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SetBootloaderHash(arm_uc_buffer_t *hash)
00168 {
00169     ARM_UC_INIT_ERROR(result, ERR_NONE);
00170     if (DeviceMetadataResource::setBootloaderHash(hash) != 0) {
00171         ARM_UC_SET_ERROR(result, ERR_UNSPECIFIED);
00172     }
00173     return result;
00174 }
00175 
00176 /**
00177  * @brief Set the OEM bootloader hash.
00178  * @details If the end-user has modified the bootloader the hash of the
00179  *          modified bootloader can be set here.
00180  * @param name Pointer to buffer struct. Hash is stored as byte array.
00181  * @return Error code.
00182  */
00183 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SetOEMBootloaderHash(arm_uc_buffer_t *hash)
00184 {
00185     ARM_UC_INIT_ERROR(result, ERR_NONE);
00186     if (DeviceMetadataResource::setOEMBootloaderHash(hash) != 0) {
00187         ARM_UC_SET_ERROR(result, ERR_UNSPECIFIED);
00188     }
00189     return result;
00190 }
00191