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 arm_uc_device_identity_cfstore.c Source File

arm_uc_device_identity_cfstore.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 
00022 #ifndef ARM_UC_USE_CFSTORE
00023 #define ARM_UC_USE_CFSTORE 0
00024 #endif
00025 
00026 #if ARM_UC_USE_CFSTORE
00027 
00028 #include <string.h>
00029 
00030 static arm_uc_guid_t arm_uc_vendor_guid = {0};
00031 static int arm_uc_vendor_guid_set = 0;
00032 static arm_uc_guid_t arm_uc_class_guid = {0};
00033 static int arm_uc_class_guid_set = 0;
00034 static arm_uc_guid_t arm_uc_device_guid = {0};
00035 static int arm_uc_device_guid_set = 0;
00036 
00037 /**
00038  * @brief Function for setting the vendor GUID.
00039  * @details The GUID is copied.
00040  * @param guid Pointer to a arm_uc_guid_t GUID.
00041  * @return Error code.
00042  */
00043 arm_uc_error_t pal_cfstore_setVendorGuid(const arm_uc_guid_t* vendor_guid)
00044 {
00045     arm_uc_error_t result = { .code = ERR_NONE };
00046 
00047     if (vendor_guid)
00048     {
00049         memcpy(&arm_uc_vendor_guid, vendor_guid, sizeof(arm_uc_guid_t));
00050         arm_uc_vendor_guid_set = 1;
00051     }
00052     else
00053     {
00054         memset(&arm_uc_vendor_guid, 0, sizeof(arm_uc_guid_t));
00055         arm_uc_vendor_guid_set = 0;
00056     }
00057     return result;
00058 }
00059 
00060 /**
00061  * @brief Function for getting a pointer to the vendor GUID.
00062  * @param guid Pointer to a arm_uc_guid_t.
00063  * @return Error code.
00064  */
00065 arm_uc_error_t pal_cfstore_getVendorGuid(arm_uc_guid_t* vendor_guid)
00066 {
00067     arm_uc_error_t result = { .code = ERR_INVALID_PARAMETER };
00068 
00069     if (vendor_guid && arm_uc_vendor_guid_set)
00070     {
00071         result.code = ERR_NONE;
00072         memcpy(vendor_guid, &arm_uc_vendor_guid, sizeof(arm_uc_guid_t));
00073     }
00074 
00075     return result;
00076 }
00077 
00078 /**
00079  * @brief Function for setting the device class GUID.
00080  * @details The GUID is copied/
00081  * @param guid Pointer to a arm_uc_guid_t GUID.
00082  * @return Error code.
00083  */
00084 arm_uc_error_t pal_cfstore_setClassGuid(const arm_uc_guid_t* class_guid)
00085 {
00086     arm_uc_error_t result = { .code = ERR_NONE };
00087 
00088     if (class_guid)
00089     {
00090         memcpy(&arm_uc_class_guid, class_guid, sizeof(arm_uc_guid_t));
00091         arm_uc_class_guid_set = 1;
00092     }
00093     else
00094     {
00095         memset(&arm_uc_class_guid, 0, sizeof(arm_uc_guid_t));
00096         arm_uc_class_guid_set = 0;
00097     }
00098     return result;
00099 }
00100 
00101 /**
00102  * @brief Function for getting a pointer to the device class GUID.
00103  * @param guid Pointer to a arm_uc_guid_t.
00104  * @return Error code.
00105  */
00106 arm_uc_error_t pal_cfstore_getClassGuid(arm_uc_guid_t* class_guid)
00107 {
00108     arm_uc_error_t result = { .code = ERR_INVALID_PARAMETER };
00109 
00110     if (class_guid && arm_uc_class_guid_set)
00111     {
00112         result.code = ERR_NONE;
00113         memcpy(class_guid, &arm_uc_class_guid, sizeof(arm_uc_guid_t));
00114     }
00115 
00116     return result;
00117 }
00118 
00119 /**
00120  * @brief Function for setting the device GUID.
00121  * @details The GUID is copied.
00122  * @param guid Pointer to a arm_uc_guid_t GUID.
00123  * @return Error code.
00124  */
00125 arm_uc_error_t pal_cfstore_setDeviceGuid(const arm_uc_guid_t* device_guid)
00126 {
00127     arm_uc_error_t result = { .code = ERR_NONE };
00128 
00129     if (device_guid)
00130     {
00131         memcpy(&arm_uc_device_guid, device_guid, sizeof(arm_uc_guid_t));
00132         arm_uc_device_guid_set = 1;
00133     }
00134     else
00135     {
00136         memset(&arm_uc_device_guid, 0, sizeof(arm_uc_guid_t));
00137         arm_uc_device_guid_set = 0;
00138     }
00139     return result;
00140 }
00141 
00142 /**
00143  * @brief Function for getting a pointer to the device GUID.
00144  * @param guid Pointer to a arm_uc_guid_t.
00145  * @return Error code.
00146  */
00147 arm_uc_error_t pal_cfstore_getDeviceGuid(arm_uc_guid_t* device_guid)
00148 {
00149     arm_uc_error_t result = { .code = ERR_INVALID_PARAMETER };
00150 
00151     if (device_guid && arm_uc_device_guid_set)
00152     {
00153         result.code = ERR_NONE;
00154         memcpy(device_guid, &arm_uc_device_guid, sizeof(arm_uc_guid_t));
00155     }
00156 
00157     return result;
00158 }
00159 
00160 static bool pal_cfstore_internal_compare(const arm_uc_guid_t* guid,
00161                                          const arm_uc_buffer_t* buffer)
00162 {
00163     // count how many bytes match
00164     uint8_t index = 0;
00165 
00166     if (guid && buffer)
00167     {
00168         for ( ; (index < sizeof(arm_uc_guid_t)) && (index < buffer->size); index++)
00169         {
00170             if (((const uint8_t*) guid)[index] != buffer->ptr[index])
00171             {
00172                 break;
00173             }
00174         }
00175     }
00176 
00177     // return true if all bytes matched
00178     return (index == sizeof(arm_uc_guid_t));
00179 }
00180 
00181 /**
00182  * @brief Check whether the three GUIDs provided are valid on the device.
00183  * @details
00184  * @param vendor_buffer Buffer pointer to the Vendor GUID.
00185  * @param class_buffer  Buffer pointer to the device class GUID.
00186  * @param device_buffer Buffer pointer to the device GUID.
00187  * @param isValid     Pointer to the boolean return value.
00188  * @return Error code.
00189  */
00190 arm_uc_error_t pal_cfstore_deviceIdentityCheck(const arm_uc_buffer_t* vendor_buffer,
00191                                                const arm_uc_buffer_t* class_buffer,
00192                                                const arm_uc_buffer_t* device_buffer)
00193 {
00194     arm_uc_error_t result = { .code = MFST_ERR_NULL_PTR };
00195 
00196     uint8_t parameters_set = 0;
00197     uint8_t parameters_ok = 0;
00198 
00199     /* check device - device is optional */
00200     if (arm_uc_device_guid_set)
00201     {
00202         if (device_buffer && device_buffer->ptr)
00203         {
00204             bool is_same = pal_cfstore_internal_compare(&arm_uc_device_guid,
00205                                                         device_buffer);
00206 
00207             if (is_same)
00208             {
00209                 parameters_ok++;
00210             }
00211             else
00212             {
00213                 result.code = MFST_ERR_GUID_DEVICE;
00214             }
00215 
00216             parameters_set++;
00217         }
00218     }
00219 
00220     /* check class - class is optional */
00221     if (arm_uc_class_guid_set)
00222     {
00223         if (class_buffer && class_buffer->ptr)
00224         {
00225             bool is_same = pal_cfstore_internal_compare(&arm_uc_class_guid,
00226                                                         class_buffer);
00227 
00228             if (is_same)
00229             {
00230                 parameters_ok++;
00231             }
00232             else
00233             {
00234                 result.code = MFST_ERR_GUID_DEVCLASS;
00235             }
00236 
00237             parameters_set++;
00238         }
00239     }
00240 
00241     /* check vendor - vendor is mandatory and has mask 0x10. */
00242     if (arm_uc_vendor_guid_set)
00243     {
00244         if (vendor_buffer && vendor_buffer->ptr)
00245         {
00246             bool is_same = pal_cfstore_internal_compare(&arm_uc_vendor_guid,
00247                                                         vendor_buffer);
00248 
00249             if (is_same)
00250             {
00251                 parameters_ok += 0x10;
00252             }
00253             else
00254             {
00255                 result.code = MFST_ERR_GUID_VENDOR;
00256             }
00257 
00258             parameters_set += 0x10;
00259         }
00260     }
00261 
00262     /* Device ID checks out when:
00263         - vendor match and neither class nor device is passed
00264         - vendor and class match and no device is passed
00265         - vendor and device match and no class is passed
00266         - vendor and class and device match
00267     */
00268     if ((parameters_set >= 0x10) && (parameters_set == parameters_ok))
00269     {
00270         result.code = MFST_ERR_NONE;
00271     }
00272 
00273     return result;
00274 }
00275 
00276 const ARM_PAL_DEVICE_IDENTITY arm_uc_device_identity_cfstore = {
00277     .SetVendorGuid          = pal_cfstore_setVendorGuid,
00278     .GetVendorGuid          = pal_cfstore_getVendorGuid,
00279     .SetClassGuid           = pal_cfstore_setClassGuid,
00280     .GetClassGuid           = pal_cfstore_getClassGuid,
00281     .SetDeviceGuid          = pal_cfstore_setDeviceGuid,
00282     .GetDeviceGuid          = pal_cfstore_getDeviceGuid,
00283     .DeviceIdentityCheck    = pal_cfstore_deviceIdentityCheck
00284 };
00285 
00286 #endif // ARM_UC_USE_CFSTORE