Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

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, SRCE_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, SRCE_ERR_NONE);
00070 
00071     return retval;
00072 }
00073 
00074 /**
00075  * @brief Send Update Client state.
00076  * @details From the OMA LWM2M Technical Specification:
00077  *
00078  *          Indicates current state with respect to this firmware update.
00079  *          This value is set by the LWM2M Client.
00080  *          0: Idle (before downloading or after successful updating)
00081  *          1: Downloading (The data sequence is on the way)
00082  *          2: Downloaded
00083  *          3: Updating
00084  *
00085  *          If writing the firmware package to Package Resource is done,
00086  *          or, if the device has downloaded the firmware package from the
00087  *          Package URI the state changes to Downloaded.
00088  *
00089  *          If writing an empty string to Package Resource is done or
00090  *          writing an empty string to Package URI is done, the state
00091  *          changes to Idle.
00092  *
00093  *          When in Downloaded state, and the executable Resource Update is
00094  *          triggered, the state changes to Updating.
00095  *          If the Update Resource failed, the state returns at Downloaded.
00096  *          If performing the Update Resource was successful, the state
00097  *          changes from Updating to Idle.
00098  *
00099  * @param state Valid states: ARM_UC_MONITOR_STATE_IDLE
00100  *                            ARM_UC_MONITOR_STATE_DOWNLOADING
00101  *                            ARM_UC_MONITOR_STATE_DOWNLOADED
00102  *                            ARM_UC_MONITOR_STATE_UPDATING
00103  *
00104  * @return Error code.
00105  */
00106 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SendState(arm_uc_monitor_state_t state)
00107 {
00108     ARM_UC_INIT_ERROR(result, ERR_INVALID_PARAMETER);
00109 
00110     int32_t retval = -1;
00111 
00112     switch (state)
00113     {
00114         case ARM_UC_MONITOR_STATE_IDLE:
00115             retval = FirmwareUpdateResource::sendState(
00116                 FirmwareUpdateResource::ARM_UCS_LWM2M_STATE_IDLE);
00117             break;
00118         case ARM_UC_MONITOR_STATE_DOWNLOADING:
00119             retval = FirmwareUpdateResource::sendState(
00120                 FirmwareUpdateResource::ARM_UCS_LWM2M_STATE_DOWNLOADING);
00121             break;
00122         case ARM_UC_MONITOR_STATE_DOWNLOADED:
00123             retval = FirmwareUpdateResource::sendState(
00124                 FirmwareUpdateResource::ARM_UCS_LWM2M_STATE_DOWNLOADED);
00125             break;
00126         case ARM_UC_MONITOR_STATE_UPDATING:
00127             retval = FirmwareUpdateResource::sendState(
00128                 FirmwareUpdateResource::ARM_UCS_LWM2M_STATE_UPDATING);
00129             break;
00130         default:
00131             break;
00132     }
00133 
00134     if (retval == 0)
00135     {
00136         result.code = ERR_NONE;
00137     }
00138 
00139     return result;
00140 }
00141 
00142 /**
00143  * @brief Send update result.
00144  * @details From the OMA LWM2M Technical Specification:
00145  *
00146  *          Contains the result of downloading or updating the firmware
00147  *          0: Initial value. Once the updating process is initiated
00148  *             (Download /Update), this Resource MUST be reset to Initial
00149  *             value.
00150  *          1: Firmware updated successfully,
00151  *          2: Not enough storage for the new firmware package.
00152  *          3. Out of memory during downloading process.
00153  *          4: Connection lost during downloading process.
00154  *          5: CRC check failure for new downloaded package.
00155  *          6: Unsupported package type.
00156  *          7: Invalid URI
00157  *          8: Firmware update failed
00158  *
00159  *          This Resource MAY be reported by sending Observe operation.
00160  *
00161  * @param result Valid results: ARM_UC_MONITOR_RESULT_INITIAL
00162  *                              ARM_UC_MONITOR_RESULT_SUCCESS
00163  *                              ARM_UC_MONITOR_RESULT_ERROR_STORAGE
00164  *                              ARM_UC_MONITOR_RESULT_ERROR_MEMORY
00165  *                              ARM_UC_MONITOR_RESULT_ERROR_CONNECTION
00166  *                              ARM_UC_MONITOR_RESULT_ERROR_CRC
00167  *                              ARM_UC_MONITOR_RESULT_ERROR_TYPE
00168  *                              ARM_UC_MONITOR_RESULT_ERROR_URI
00169  *                              ARM_UC_MONITOR_RESULT_ERROR_UPDATE
00170  *
00171  * @return Error code.
00172  */
00173 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SendUpdateResult(arm_uc_monitor_result_t updateResult)
00174 {
00175     ARM_UC_INIT_ERROR(result, ERR_INVALID_PARAMETER);
00176 
00177     int32_t retval = -1;
00178 
00179     switch (updateResult)
00180     {
00181         case ARM_UC_MONITOR_RESULT_INITIAL:
00182             retval = FirmwareUpdateResource::sendUpdateResult(
00183                 FirmwareUpdateResource::ARM_UCS_LWM2M_RESULT_INITIAL);
00184             break;
00185         case ARM_UC_MONITOR_RESULT_SUCCESS:
00186             retval = FirmwareUpdateResource::sendUpdateResult(
00187                 FirmwareUpdateResource::ARM_UCS_LWM2M_RESULT_SUCCESS);
00188             break;
00189         case ARM_UC_MONITOR_RESULT_ERROR_STORAGE:
00190             retval = FirmwareUpdateResource::sendUpdateResult(
00191                 FirmwareUpdateResource::ARM_UCS_LWM2M_RESULT_ERROR_STORAGE);
00192             break;
00193         case ARM_UC_MONITOR_RESULT_ERROR_MEMORY:
00194             retval = FirmwareUpdateResource::sendUpdateResult(
00195                 FirmwareUpdateResource::ARM_UCS_LWM2M_RESULT_ERROR_MEMORY);
00196             break;
00197         case ARM_UC_MONITOR_RESULT_ERROR_CONNECTION:
00198             retval = FirmwareUpdateResource::sendUpdateResult(
00199                 FirmwareUpdateResource::ARM_UCS_LWM2M_RESULT_ERROR_CONNECTION);
00200             break;
00201         case ARM_UC_MONITOR_RESULT_ERROR_CRC:
00202             retval = FirmwareUpdateResource::sendUpdateResult(
00203                 FirmwareUpdateResource::ARM_UCS_LWM2M_RESULT_ERROR_CRC);
00204             break;
00205         case ARM_UC_MONITOR_RESULT_ERROR_TYPE:
00206             retval = FirmwareUpdateResource::sendUpdateResult(
00207                 FirmwareUpdateResource::ARM_UCS_LWM2M_RESULT_ERROR_TYPE);
00208             break;
00209         case ARM_UC_MONITOR_RESULT_ERROR_URI:
00210             retval = FirmwareUpdateResource::sendUpdateResult(
00211                 FirmwareUpdateResource::ARM_UCS_LWM2M_RESULT_ERROR_URI);
00212             break;
00213         case ARM_UC_MONITOR_RESULT_ERROR_UPDATE:
00214             retval = FirmwareUpdateResource::sendUpdateResult(
00215                 FirmwareUpdateResource::ARM_UCS_LWM2M_RESULT_ERROR_UPDATE);
00216             break;
00217         case ARM_UC_MONITOR_RESULT_ERROR_HASH:
00218             retval = FirmwareUpdateResource::sendUpdateResult(
00219                 FirmwareUpdateResource::ARM_UCS_LWM2M_RESULT_ERROR_HASH);
00220             break;
00221         default:
00222             break;
00223     }
00224 
00225     if (retval == 0)
00226     {
00227         result.code = ERR_NONE;
00228     }
00229 
00230     return result;
00231 }
00232 
00233 /**
00234  * @brief Send current firmware name.
00235  * @details The firmware name is the SHA256 hash.
00236  *
00237  * @param name Pointer to buffer struct. Hash is stored as byte array.
00238  * @return Error code.
00239  */
00240 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SendName(arm_uc_buffer_t* name)
00241 {
00242     ARM_UC_INIT_ERROR(result, ERR_INVALID_PARAMETER);
00243 
00244     int32_t retval = -1;
00245 
00246     if (name && name->ptr)
00247     {
00248         retval = FirmwareUpdateResource::sendPkgName(name->ptr, name->size);
00249     }
00250 
00251     if (retval == 0)
00252     {
00253         result.code = ERR_NONE;
00254     }
00255 
00256     return result;
00257 }
00258 
00259 /**
00260  * @brief Send current firmware version.
00261  * @details The firmware version is the timestamp from the manifest that
00262  *          authorized the firmware.
00263  *
00264  * @param version Timestamp, 64 bit unsigned integer.
00265  * @return Error code.
00266  */
00267 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SendVersion(uint64_t version)
00268 {
00269     ARM_UC_INIT_ERROR(result, ERR_INVALID_PARAMETER);
00270 
00271     int32_t retval = FirmwareUpdateResource::sendPkgVersion(version);
00272 
00273     if (retval == 0)
00274     {
00275         result.code = ERR_NONE;
00276     }
00277 
00278     return result;
00279 }
00280 
00281 /**
00282  * @brief Set the bootloader hash.
00283  * @details The bootloader hash is a hash of the bootloader. This is
00284  *          used for tracking the version of the bootloader used.
00285  *
00286  * @param name Pointer to buffer struct. Hash is stored as byte array.
00287  * @return Error code.
00288  */
00289 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SetBootloaderHash(arm_uc_buffer_t* hash)
00290 {
00291     ARM_UC_INIT_ERROR(result, ERR_INVALID_PARAMETER);
00292 
00293     int32_t retval = DeviceMetadataResource::setBootloaderHash(hash);
00294 
00295     if (retval == 0)
00296     {
00297         result.code = ERR_NONE;
00298     }
00299 
00300     return result;
00301 }
00302 
00303 /**
00304  * @brief Set the OEM bootloader hash.
00305  * @details If the end-user has modified the bootloader the hash of the
00306  *          modified bootloader can be set here.
00307  *
00308  * @param name Pointer to buffer struct. Hash is stored as byte array.
00309  * @return Error code.
00310  */
00311 arm_uc_error_t ARM_UCS_LWM2M_MONITOR_SetOEMBootloaderHash(arm_uc_buffer_t* hash)
00312 {
00313     ARM_UC_INIT_ERROR(result, ERR_INVALID_PARAMETER);
00314 
00315     int32_t retval = DeviceMetadataResource::setOEMBootloaderHash(hash);
00316 
00317     if (retval == 0)
00318     {
00319         result.code = ERR_NONE;
00320     }
00321 
00322     return result;
00323 }
00324