Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_uc_device_identity_nvstore.c Source File

arm_uc_device_identity_nvstore.c

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 "pal4life-device-identity/pal_device_identity.h"
00020 #include "update-client-common/arm_uc_config.h"
00021 #include <stdint.h>
00022 
00023 #if defined(ARM_UC_FEATURE_IDENTITY_NVSTORE) && (ARM_UC_FEATURE_IDENTITY_NVSTORE == 1)
00024 
00025 #include "CloudClientStorage.h"
00026 
00027 #define SIZE_OF_GUID (sizeof(arm_uc_guid_t))
00028 // Hex encoded GUIDs with up to 4 hyphens.
00029 #define SIZE_OF_TEXT_GUID ((SIZE_OF_GUID) * 2 + 4)
00030 
00031 /**
00032  * @brief Helper Function for setting a specific GUID Into NVStore.
00033  * @details .
00034  * @param guid Pointer to a arm_uc_guid_t GUID.
00035  * @param key The NVSTORE Key where to store the GUID
00036  * @return Error code.
00037  */
00038 static arm_uc_error_t pal_nvstore_internal_set_guid(const arm_uc_guid_t *guid,
00039                                                     cloud_client_param key)
00040 {
00041     arm_uc_error_t result = { .code = ERR_INVALID_PARAMETER };
00042 
00043     if (guid && key) {
00044         ccs_status_e ccs_status = set_config_parameter(key,
00045                                                        (const uint8_t *) guid,
00046                                                        SIZE_OF_GUID);
00047 
00048         if (ccs_status == CCS_STATUS_SUCCESS) {
00049             result.code = ERR_NONE;
00050         }
00051     }
00052 
00053     return result;
00054 }
00055 
00056 /**
00057  * @brief Helper Function for getting a specific GUID From NVStore.
00058  * @details .
00059  * @param guid Pointer to a arm_uc_guid_t GUID.
00060  * @param key The NVSTORE Key where to get the GUID
00061  * @return Error code.
00062  */
00063 static arm_uc_error_t pal_nvstore_internal_get_guid(arm_uc_guid_t *guid,
00064                                                     cloud_client_param key)
00065 {
00066     arm_uc_error_t result = { .error = ERR_INVALID_PARAMETER };
00067 
00068     if (guid && key) {
00069         uint8_t buffer[SIZE_OF_GUID] = { 0 };
00070         size_t value_length = 0;
00071         memset(guid, 0, SIZE_OF_GUID);
00072 
00073         ccs_status_e ccs_status = get_config_parameter(key,
00074                                                        buffer,
00075                                                        SIZE_OF_GUID,
00076                                                        &value_length);
00077         if (ccs_status == CCS_STATUS_KEY_DOESNT_EXIST) {
00078             result.code = ARM_UC_DI_ERR_NOT_FOUND;
00079         }
00080 
00081         if (ccs_status == CCS_STATUS_SUCCESS) {
00082             result.code = ERR_NONE;
00083             memcpy(guid, buffer, SIZE_OF_GUID);
00084         }
00085     }
00086 
00087     return result;
00088 }
00089 
00090 /**
00091  * @brief Helper Function for comparing the GUID from NVStore to
00092  *        given buffer.
00093  * @details .
00094  * @param guid Pointer to a arm_uc_guid_t GUID.
00095  * @param buffer buffer which to compare GUID value to
00096  * @return Error code.
00097  */
00098 static bool pal_nvstore_internal_compare(const arm_uc_guid_t *guid,
00099                                          const arm_uc_buffer_t *buffer)
00100 {
00101     // count how many bytes match
00102     size_t index = 0;
00103 
00104     if (guid && buffer) {
00105         for (; (index < sizeof(arm_uc_guid_t)) && (index < buffer->size); index++) {
00106             if (((uint8_t *) guid)[index] != buffer->ptr[index]) {
00107                 break;
00108             }
00109         }
00110     }
00111     return (index == sizeof(arm_uc_guid_t));
00112 }
00113 
00114 /**
00115  * @brief Function for setting the vendor GUID.
00116  * @details The GUID is copied.
00117  * @param guid Pointer to a arm_uc_guid_t GUID.
00118  * @param copy Boolean value indicating whether the value should be copied or
00119  *             referenced.
00120  * @return Error code.
00121  */
00122 arm_uc_error_t pal_nvstore_setVendorGuid(const arm_uc_guid_t *guid)
00123 {
00124     return pal_nvstore_internal_set_guid(guid,
00125                                          KEY_VENDOR_ID);
00126 }
00127 
00128 /**
00129  * @brief Function for getting a pointer to the vendor GUID.
00130  * @param guid Pointer to a arm_uc_guid_t pointer.
00131  * @return Error code.
00132  */
00133 arm_uc_error_t pal_nvstore_getVendorGuid(arm_uc_guid_t *guid)
00134 {
00135     return pal_nvstore_internal_get_guid(guid,
00136                                          KEY_VENDOR_ID);
00137 }
00138 
00139 /**
00140  * @brief Function for setting the device class GUID.
00141  * @details The GUID is copied.
00142  * @param guid Pointer to a arm_uc_guid_t GUID.
00143  * @param copy Boolean value indicating whether the value should be copied or
00144  *             referenced.
00145  * @return Error code.
00146  */
00147 arm_uc_error_t pal_nvstore_setClassGuid(const arm_uc_guid_t *guid)
00148 {
00149     return pal_nvstore_internal_set_guid(guid,
00150                                          KEY_CLASS_ID);
00151 }
00152 
00153 /**
00154  * @brief Function for getting a pointer to the device class GUID.
00155  * @param guid Pointer to a arm_uc_guid_t pointer.
00156  * @return Error code.
00157  */
00158 arm_uc_error_t pal_nvstore_getClassGuid(arm_uc_guid_t *guid)
00159 {
00160     return pal_nvstore_internal_get_guid(guid,
00161                                          KEY_CLASS_ID);
00162 
00163 }
00164 
00165 /**
00166  * @brief Function for setting the device GUID.
00167  * @details The GUID is copied.
00168  * @param guid Pointer to a arm_uc_guid_t GUID.
00169  * @param copy Boolean value indicating whether the value should be copied or
00170  *             referenced.
00171  * @return Error code.
00172  */
00173 arm_uc_error_t pal_nvstore_setDeviceGuid(const arm_uc_guid_t *guid)
00174 {
00175     return pal_nvstore_internal_set_guid(guid,
00176                                          ENDPOINT_NAME);
00177 }
00178 
00179 /**
00180  * @brief Function for getting a pointer to the device GUID.
00181  * @param guid Pointer to a arm_uc_guid_t pointer.
00182  * @return Error code.
00183  */
00184 arm_uc_error_t pal_nvstore_getDeviceGuid(arm_uc_guid_t *guid)
00185 {
00186     return pal_nvstore_internal_get_guid(guid,
00187                                          ENDPOINT_NAME);
00188 }
00189 
00190 
00191 /**
00192  * @brief Check whether the three GUIDs provided are valid on the device.
00193  * @details
00194  * @param vendor_buffer Buffer pointer to the Vendor GUID.
00195  * @param class_buffer  Buffer pointer to the device class GUID.
00196  * @param device_buffer Buffer pointer to the device GUID.
00197  * @param isValid     Pointer to the boolean return value.
00198  * @return Error code.
00199  */
00200 arm_uc_error_t pal_nvstore_deviceIdentityCheck(const arm_uc_buffer_t *vendor_buffer,
00201                                                const arm_uc_buffer_t *class_buffer,
00202                                                const arm_uc_buffer_t *device_buffer)
00203 {
00204     arm_uc_error_t result = { .code = MFST_ERR_NULL_PTR };
00205 
00206     uint8_t parameters_set = 0;
00207     uint8_t parameters_ok = 0;
00208 
00209     /* check device - device is optional */
00210     if (device_buffer &&
00211             device_buffer->ptr &&
00212             (device_buffer->size > 0)) {
00213         parameters_set++;
00214 
00215         arm_uc_guid_t guid = { 0 };
00216 
00217         arm_uc_error_t retval = pal_nvstore_getDeviceGuid(&guid);
00218 
00219         if (retval.code == ERR_NONE) {
00220             bool is_same = pal_nvstore_internal_compare(&guid, device_buffer);
00221 
00222             if (is_same) {
00223                 parameters_ok++;
00224             } else {
00225                 result.code = MFST_ERR_GUID_DEVICE;
00226             }
00227         }
00228     }
00229 
00230     /* check class - class is optional */
00231     if (class_buffer &&
00232             class_buffer->ptr &&
00233             (class_buffer->size > 0)) {
00234         parameters_set++;
00235 
00236         arm_uc_guid_t guid = { 0 };
00237 
00238         arm_uc_error_t retval = pal_nvstore_getClassGuid(&guid);
00239 
00240         if (retval.code == ERR_NONE) {
00241             bool is_same = pal_nvstore_internal_compare(&guid, class_buffer);
00242 
00243             if (is_same) {
00244                 parameters_ok++;
00245             } else {
00246                 result.code = MFST_ERR_GUID_DEVCLASS;
00247             }
00248         }
00249     }
00250 
00251     /* check vendor - vendor is mandatory and has mask 0x10. */
00252     if (vendor_buffer &&
00253             vendor_buffer->ptr &&
00254             (vendor_buffer->size > 0)) {
00255         parameters_set += 0x10;
00256 
00257         arm_uc_guid_t guid = { 0 };
00258 
00259         arm_uc_error_t retval = pal_nvstore_getVendorGuid(&guid);
00260 
00261         if (retval.code == ERR_NONE) {
00262             bool is_same = pal_nvstore_internal_compare(&guid, vendor_buffer);
00263 
00264             if (is_same) {
00265                 parameters_ok += 0x10;
00266             } else {
00267                 result.code = MFST_ERR_GUID_VENDOR;
00268             }
00269         }
00270     }
00271 
00272     /* Device ID checks out when:
00273         - vendor match and neither class nor device is passed
00274         - vendor and class match and no device is passed
00275         - vendor and device match and no class is passed
00276         - vendor and class and device match
00277     */
00278     if ((parameters_set >= 0x10) && (parameters_set == parameters_ok)) {
00279         result.code = ERR_NONE;
00280     }
00281 
00282     return result;
00283 }
00284 
00285 const ARM_PAL_DEVICE_IDENTITY arm_uc_device_identity_nvstore = {
00286     .SetVendorGuid          = pal_nvstore_setVendorGuid,
00287     .GetVendorGuid          = pal_nvstore_getVendorGuid,
00288     .SetClassGuid           = pal_nvstore_setClassGuid,
00289     .GetClassGuid           = pal_nvstore_getClassGuid,
00290     .SetDeviceGuid          = pal_nvstore_setDeviceGuid,
00291     .GetDeviceGuid          = pal_nvstore_getDeviceGuid,
00292     .DeviceIdentityCheck    = pal_nvstore_deviceIdentityCheck
00293 };
00294 
00295 #endif // ARM_UC_FEATURE_IDENTITY_NVSTORE