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-source.cpp Source File

lwm2m-source.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-common/arm_uc_common.h"
00021 #include "update-client-lwm2m/lwm2m-source.h"
00022 #include "update-client-lwm2m/FirmwareUpdateResource.h"
00023 #include "update-client-lwm2m/DeviceMetadataResource.h"
00024 
00025 
00026 /* forward declaration */
00027 static void ARM_UCS_PackageCallback(const uint8_t* buffer, uint16_t length);
00028 
00029 /* local copy of the received manifest */
00030 static uint8_t* arm_ucs_manifest_buffer = NULL;
00031 static uint16_t arm_ucs_manifest_length = 0;
00032 
00033 /* callback function pointer and struct */
00034 static void (*ARM_UCS_EventHandler)(uint32_t event) = 0;
00035 static arm_uc_callback_t callbackNodeManifest = { NULL, 0, NULL, 0 };
00036 static arm_uc_callback_t callbackNodeNotification = { NULL, 0, NULL, 0 };
00037 
00038 /**
00039  * @brief Get driver version.
00040  * @return Driver version.
00041  */
00042 uint32_t ARM_UCS_LWM2M_SOURCE_GetVersion(void)
00043 {
00044     return 0;
00045 }
00046 
00047 /**
00048  * @brief Get Source capabilities.
00049  * @return Struct containing capabilites. See definition above.
00050  */
00051 ARM_SOURCE_CAPABILITIES ARM_UCS_LWM2M_SOURCE_GetCapabilities(void)
00052 {
00053     ARM_SOURCE_CAPABILITIES result;
00054     result.notify = 0;
00055     result.manifest_default = 0;
00056     result.manifest_url = 0;
00057     result.firmware = 0;
00058     result.keytable = 0;
00059 
00060     /* the event handler must be set before module can be used */
00061     if (ARM_UCS_EventHandler != 0)
00062     {
00063         result.notify = 1;
00064         result.manifest_default = 1;
00065     }
00066 
00067     return result;
00068 }
00069 
00070 /**
00071  * @brief Initialize Source.
00072  * @details Function pointer to event handler is passed as argument.
00073  *
00074  * @param cb_event Function pointer to event handler. See events above.
00075  * @return Error code.
00076  */
00077 arm_uc_error_t ARM_UCS_LWM2M_SOURCE_Initialize(ARM_SOURCE_SignalEvent_t cb_event)
00078 {
00079     UC_SRCE_TRACE("ARM_UCS_LWM2M_SOURCE_Initialize: %p", cb_event);
00080     ARM_UC_INIT_ERROR(result, SRCE_ERR_INVALID_PARAMETER);
00081 
00082     if (cb_event != 0)
00083     {
00084         /* store callback handler */
00085         ARM_UCS_EventHandler = cb_event;
00086 
00087         /* Initialize LWM2M Firmware Update Object */
00088         FirmwareUpdateResource::Initialize();
00089 
00090         /* Register callback handler */
00091         FirmwareUpdateResource::addPackageCallback(ARM_UCS_PackageCallback);
00092 
00093         DeviceMetadataResource::Initialize();
00094 
00095         ARM_UC_SET_ERROR(result, SRCE_ERR_NONE);
00096     }
00097 
00098     return result;
00099 }
00100 
00101 /**
00102  * @brief Uninitialized Source.
00103  * @return Error code.
00104  */
00105 arm_uc_error_t ARM_UCS_LWM2M_SOURCE_Uninitialize(void)
00106 {
00107     ARM_UC_INIT_ERROR(retval, SRCE_ERR_NONE);
00108     DeviceMetadataResource::Uninitialize();
00109     FirmwareUpdateResource::Uninitialize();
00110 
00111     return retval;
00112 }
00113 
00114 /**
00115  * @brief Cost estimation for retrieving manifest from the default location.
00116  * @details The estimation can vary over time and should not be cached too long.
00117  *          0x00000000 - The manifest is already downloaded.
00118  *          0xFFFFFFFF - Cannot retrieve manifest from this Source.
00119  *
00120  * @param cost Pointer to variable for the return value.
00121  * @return Error code.
00122  */
00123 arm_uc_error_t ARM_UCS_LWM2M_SOURCE_GetManifestDefaultCost(uint32_t* cost)
00124 {
00125     ARM_UC_INIT_ERROR(result, SRCE_ERR_INVALID_PARAMETER);
00126 
00127     if (cost != 0)
00128     {
00129         /* set cost to 0 when manifest is cached */
00130         if (arm_ucs_manifest_buffer && arm_ucs_manifest_length)
00131         {
00132             *cost = 0;
00133         }
00134         /* set cost to 0xFFFFFFFF when manifest has been read */
00135         else
00136         {
00137             *cost = 0xFFFFFFFF;
00138         }
00139 
00140         ARM_UC_SET_ERROR(result, SRCE_ERR_NONE);
00141     }
00142 
00143     return result;
00144 }
00145 
00146 /**
00147  * @brief Retrieve manifest from the default location.
00148  * @details Manifest is stored in supplied buffer.
00149  *          Event is generated once manifest is in buffer.
00150  *
00151  * @param buffer Struct containing byte array, maximum size, and actual size.
00152  * @return Error code.
00153  */
00154 arm_uc_error_t ARM_UCS_LWM2M_SOURCE_GetManifestDefault(arm_uc_buffer_t* buffer,
00155                                                        uint32_t offset)
00156 {
00157     ARM_UC_INIT_ERROR(result, SRCE_ERR_INVALID_PARAMETER);
00158 
00159     /* copy manifest from cache into buffer */
00160     if ((buffer != NULL) &&
00161         (buffer->ptr != NULL) &&
00162         (arm_ucs_manifest_buffer != NULL) &&
00163         (arm_ucs_manifest_length != 0) &&
00164         (offset < arm_ucs_manifest_length))
00165     {
00166         /* remaining length based on offset request */
00167         uint16_t length = arm_ucs_manifest_length - offset;
00168 
00169         /* set actual length based on buffer size */
00170         if (length > buffer->size_max)
00171         {
00172             length = buffer->size_max;
00173         }
00174 
00175         /* size check */
00176         if (length > 0)
00177         {
00178             /* copy manifest from local buffer to external buffer */
00179             memcpy(buffer->ptr, &arm_ucs_manifest_buffer[offset], length);
00180             buffer->size = length;
00181 
00182             /* delete local buffer once the entire manifest has been read */
00183             if (offset + length >= arm_ucs_manifest_length)
00184             {
00185                 delete[] arm_ucs_manifest_buffer;
00186                 arm_ucs_manifest_buffer = NULL;
00187                 arm_ucs_manifest_length = 0;
00188             }
00189 
00190             ARM_UC_SET_ERROR(result, SRCE_ERR_NONE);
00191 
00192             /* signal event handler that manifest has been copied to buffer */
00193             if (ARM_UCS_EventHandler)
00194             {
00195                 ARM_UC_PostCallback(&callbackNodeManifest,
00196                                     ARM_UCS_EventHandler,
00197                                     EVENT_MANIFEST);
00198             }
00199         }
00200     }
00201 
00202     return result;
00203 }
00204 
00205 static void ARM_UCS_PackageCallback(const uint8_t* buffer, uint16_t length)
00206 {
00207     uint32_t event_code = EVENT_ERROR;
00208 
00209     if (arm_ucs_manifest_buffer)
00210     {
00211         UC_SRCE_ERR_MSG("received new manifest before reading the old one");
00212 
00213         /* delete old buffer to make space for the new one */
00214         delete[] arm_ucs_manifest_buffer;
00215         arm_ucs_manifest_length = 0;
00216     }
00217 
00218     /* allocate a local buffer of the same size as the manifest */
00219     arm_ucs_manifest_buffer = new uint8_t[length];
00220 
00221     if (arm_ucs_manifest_buffer)
00222     {
00223         /* copy manifest from payload to local buffer */
00224         memcpy(arm_ucs_manifest_buffer, buffer, length);
00225         arm_ucs_manifest_length = length;
00226 
00227         event_code = EVENT_NOTIFICATION;
00228     }
00229 
00230     /* signal event handler with result */
00231     if (ARM_UCS_EventHandler)
00232     {
00233         ARM_UC_PostCallback(&callbackNodeNotification,
00234                             ARM_UCS_EventHandler,
00235                             event_code);
00236     }
00237 }
00238 
00239 /*****************************************************************************/
00240 /* Capabilities not supported by this source                                 */
00241 /*****************************************************************************/
00242 
00243 /**
00244  * @brief Cost estimation for retrieving manifest from URL.
00245  * @details The estimation can vary over time and should not be cached too long.
00246  *          0x00000000 - The manifest is already downloaded.
00247  *          0xFFFFFFFF - Cannot retrieve manifest from this Source.
00248  *
00249  * @param uri URI struct with manifest location.
00250  * @param cost Pointer to variable for the return value.
00251  * @return Error code.
00252  */
00253 arm_uc_error_t ARM_UCS_LWM2M_SOURCE_GetManifestURLCost(arm_uc_uri_t* uri,
00254                                                        uint32_t* cost)
00255 {
00256     (void) uri;
00257     (void) cost;
00258 
00259     ARM_UC_INIT_ERROR(result, SRCE_ERR_INVALID_PARAMETER);
00260 
00261     /* not supported - return default cost regardless of actual uri location */
00262     if (cost)
00263     {
00264         *cost = 0xFFFFFFFF;
00265         ARM_UC_SET_ERROR(result, SRCE_ERR_NONE);
00266     }
00267 
00268     return result;
00269 }
00270 
00271 /**
00272  * @brief Cost estimation for retrieving firmware from URL.
00273  * @details The estimation can vary over time and should not be cached too long.
00274  *          0x00000000 - The firmware is already downloaded.
00275  *          0xFFFFFFFF - Cannot retrieve firmware from this Source.
00276  *
00277  * @param uri URI struct with firmware location.
00278  * @param cost Pointer to variable for the return value.
00279  * @return Error code.
00280  */
00281 arm_uc_error_t ARM_UCS_LWM2M_SOURCE_GetFirmwareURLCost(arm_uc_uri_t* uri,
00282                                                        uint32_t* cost)
00283 {
00284     (void) uri;
00285     (void) cost;
00286 
00287     ARM_UC_INIT_ERROR(result, SRCE_ERR_INVALID_PARAMETER);
00288 
00289     /* not supported - return default cost regardless of actual uri location */
00290     if (cost != 0)
00291     {
00292         *cost = 0xFFFFFFFF;
00293         ARM_UC_SET_ERROR(result, SRCE_ERR_NONE);
00294     }
00295 
00296     return result;
00297 }
00298 
00299 /**
00300  * @brief Cost estimation for retrieving key table from URL.
00301  * @details The estimation can vary over time and should not be cached too long.
00302  *          0x00000000 - The firmware is already downloaded.
00303  *          0xFFFFFFFF - Cannot retrieve firmware from this Source.
00304  *
00305  * @param uri URI struct with keytable location.
00306  * @param cost Pointer to variable for the return value.
00307  * @return Error code.
00308  */
00309 arm_uc_error_t ARM_UCS_LWM2M_SOURCE_GetKeytableURLCost(arm_uc_uri_t* uri,
00310                                                        uint32_t* cost)
00311 {
00312     (void) uri;
00313     (void) cost;
00314 
00315     ARM_UC_INIT_ERROR(result, SRCE_ERR_INVALID_PARAMETER);
00316 
00317     /* not supported - return default cost regardless of actual uri location */
00318     if ((uri != 0) && (cost != 0))
00319     {
00320         *cost = 0xFFFFFFFF;
00321         ARM_UC_SET_ERROR(result, SRCE_ERR_NONE);
00322     }
00323 
00324     return result;
00325 }
00326 
00327 /**
00328  * @brief Retrieve manifest from URL.
00329  * @details Manifest is stored in supplied buffer.
00330  *          Event is generated once manifest is in buffer.
00331  *
00332  * @param uri URI struct with manifest location.
00333  * @param buffer Struct containing byte array, maximum size, and actual size.
00334  *
00335  * @return Error code.
00336  */
00337 arm_uc_error_t ARM_UCS_LWM2M_SOURCE_GetManifestURL(arm_uc_uri_t* uri,
00338                                                    arm_uc_buffer_t* buffer,
00339                                                    uint32_t offset)
00340 {
00341     (void) uri;
00342     (void) buffer;
00343     (void) offset;
00344 
00345     ARM_UC_INIT_ERROR(retval, SRCE_ERR_INVALID_PARAMETER);
00346 
00347     return retval;
00348 }
00349 
00350 /**
00351  * @brief Retrieve firmware fragment.
00352  * @details Firmware fragment is stored in supplied buffer.
00353  *          Event is generated once fragment is in buffer.
00354  *
00355  * @param uri URI struct with firmware location.
00356  * @param buffer Struct containing byte array, maximum size, and actual size.
00357  * @param offset Firmware offset to retrieve fragment from.
00358  * @return Error code.
00359  */
00360 arm_uc_error_t ARM_UCS_LWM2M_SOURCE_GetFirmwareFragment(arm_uc_uri_t* uri,
00361                                                         arm_uc_buffer_t* buffer,
00362                                                         uint32_t offset)
00363 {
00364     (void) uri;
00365     (void) buffer;
00366     (void) offset;
00367 
00368     ARM_UC_INIT_ERROR(retval, SRCE_ERR_INVALID_PARAMETER);
00369 
00370     return retval;
00371 }
00372 
00373 /**
00374  * @brief Retrieve a key table from a URL.
00375  * @details Key table is stored in supplied buffer.
00376  *          Event is generated once fragment is in buffer.
00377  *
00378  * @param uri URI struct with keytable location.
00379  * @param buffer Struct containing byte array, maximum size, and actual size.
00380  * @return Error code.
00381  */
00382 arm_uc_error_t ARM_UCS_LWM2M_SOURCE_GetKeytableURL(arm_uc_uri_t* uri,
00383                                                    arm_uc_buffer_t* buffer)
00384 {
00385     (void) uri;
00386     (void) buffer;
00387 
00388     ARM_UC_INIT_ERROR(retval, SRCE_ERR_INVALID_PARAMETER);
00389 
00390     return retval;
00391 }
00392