leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arm_uc_device_identity_raw.c Source File

arm_uc_device_identity_raw.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_RAW_CONFIG) && (ARM_UC_FEATURE_IDENTITY_RAW_CONFIG == 1)
00024 
00025 #define SIZE_OF_GUID (sizeof(arm_uc_guid_t))
00026 // Hex encoded GUIDs with up to 4 hyphens.
00027 #define SIZE_OF_TEXT_GUID ((SIZE_OF_GUID) * 2 + 4)
00028 
00029 static arm_uc_guid_t arm_uc_class_id_raw = {0};
00030 static arm_uc_guid_t arm_uc_vendor_id_raw = {0};
00031 static arm_uc_guid_t arm_uc_device_id_raw = {0};
00032 
00033 /**
00034  * @brief Function for setting the vendor GUID.
00035  * @details The GUID is copied.
00036  * @param guid Pointer to a arm_uc_guid_t GUID.
00037  * @return Error code.
00038  */
00039 arm_uc_error_t pal_raw_setVendorGuid(const arm_uc_guid_t *guid)
00040 {
00041     arm_uc_error_t err = {ERR_NONE};
00042     if (guid == NULL) {
00043         err.code = ARM_UC_DI_ERR_INVALID_PARAMETER;
00044     } else {
00045         memcpy(arm_uc_vendor_id_raw, guid, sizeof(arm_uc_guid_t));
00046     }
00047     return err;
00048 }
00049 
00050 /**
00051  * @brief Function for getting a pointer to the vendor GUID.
00052  * @param guid Pointer to a arm_uc_guid_t pointer.
00053  * @return Error code.
00054  */
00055 arm_uc_error_t pal_raw_getVendorGuid(arm_uc_guid_t *guid)
00056 {
00057     arm_uc_error_t err = {ERR_NONE};
00058     if (guid == NULL) {
00059         err.code = ARM_UC_DI_ERR_INVALID_PARAMETER;
00060     } else {
00061         memcpy(guid, arm_uc_vendor_id_raw, sizeof(arm_uc_guid_t));
00062     }
00063     return err;
00064 }
00065 
00066 /**
00067  * @brief Function for setting the device class GUID.
00068  * @details The GUID is copied.
00069  * @param guid Pointer to a arm_uc_guid_t GUID.
00070  * @param copy Boolean value indicating whether the value should be copied or
00071  *             referenced.
00072  * @return Error code.
00073  */
00074 arm_uc_error_t pal_raw_setClassGuid(const arm_uc_guid_t *guid)
00075 {
00076     arm_uc_error_t err = {ERR_NONE};
00077     if (guid == NULL) {
00078         err.code = ARM_UC_DI_ERR_INVALID_PARAMETER;
00079     } else {
00080         memcpy(arm_uc_class_id_raw, guid, sizeof(arm_uc_guid_t));
00081     }
00082     return err;
00083 }
00084 
00085 /**
00086  * @brief Function for getting a pointer to the device class GUID.
00087  * @param guid Pointer to a arm_uc_guid_t pointer.
00088  * @return Error code.
00089  */
00090 arm_uc_error_t pal_raw_getClassGuid(arm_uc_guid_t *guid)
00091 {
00092     arm_uc_error_t err = {ERR_NONE};
00093     if (guid == NULL) {
00094         err.code = ARM_UC_DI_ERR_INVALID_PARAMETER;
00095     } else {
00096         memcpy(guid, arm_uc_class_id_raw, sizeof(arm_uc_guid_t));
00097     }
00098     return err;
00099 }
00100 
00101 /**
00102  * @brief Function for setting the device GUID.
00103  * @details The GUID is copied.
00104  * @param guid Pointer to a arm_uc_guid_t GUID.
00105  * @param copy Boolean value indicating whether the value should be copied or
00106  *             referenced.
00107  * @return Error code.
00108  */
00109 arm_uc_error_t pal_raw_setDeviceGuid(const arm_uc_guid_t *guid)
00110 {
00111     arm_uc_error_t err = {ERR_NONE};
00112     if (guid == NULL) {
00113         err.code = ARM_UC_DI_ERR_INVALID_PARAMETER;
00114     }
00115     if (err.error == ERR_NONE) {
00116         memcpy(arm_uc_device_id_raw, guid, sizeof(arm_uc_guid_t));
00117     }
00118     return err;
00119 }
00120 
00121 /**
00122  * @brief Function for getting a pointer to the device GUID.
00123  * @param guid Pointer to a arm_uc_guid_t pointer.
00124  * @return Error code.
00125  */
00126 arm_uc_error_t pal_raw_getDeviceGuid(arm_uc_guid_t *guid)
00127 {
00128     arm_uc_error_t err = {ERR_NONE};
00129     if (guid == NULL) {
00130         err.code = ARM_UC_DI_ERR_INVALID_PARAMETER;
00131     }
00132     if (err.error == ERR_NONE) {
00133         memcpy(guid, arm_uc_device_id_raw, sizeof(arm_uc_guid_t));
00134     }
00135     return err;
00136 }
00137 
00138 
00139 /**
00140  * @brief Check whether the three GUIDs provided are valid on the device.
00141  * @details
00142  * @param vendor_buffer Buffer pointer to the Vendor GUID.
00143  * @param class_buffer  Buffer pointer to the device class GUID.
00144  * @param device_buffer Buffer pointer to the device GUID.
00145  * @return Error code.
00146  */
00147 arm_uc_error_t pal_raw_deviceIdentityCheck(const arm_uc_buffer_t *vendor_buffer,
00148                                            const arm_uc_buffer_t *class_buffer,
00149                                            const arm_uc_buffer_t *device_buffer)
00150 {
00151     // TODO is it correct to use Manifest Manager error codes
00152     arm_uc_error_t result = { .code = MFST_ERR_NULL_PTR };
00153 
00154     uint8_t parameters_set = 0;
00155     uint8_t parameters_ok = 0;
00156     arm_uc_guid_t guid = { 0 };
00157     arm_uc_buffer_t guid_buffer = {
00158         .size_max = sizeof(arm_uc_guid_t),
00159         .size = sizeof(arm_uc_guid_t),
00160         .ptr = (uint8_t *) &guid
00161     };
00162 
00163     /* check device - device is optional */
00164     /* TODO: The meaning of Device ID is undefined. */
00165 #if 0
00166     if (device_buffer &&
00167             device_buffer->ptr &&
00168             (device_buffer->size > 0)) {
00169         parameters_set++;
00170 
00171         arm_uc_error_t retval = pal_raw_getDeviceGuid(&guid);
00172 
00173         if (retval.code == ERR_NONE) {
00174 
00175             uint32_t rc = ARM_UC_BinCompareCT(&guid_buffer, device_buffer);
00176             bool is_same = !rc;
00177 
00178             if (is_same) {
00179                 parameters_ok++;
00180             } else {
00181                 result.code = MFST_ERR_GUID_DEVICE;
00182             }
00183         }
00184     }
00185 #endif
00186 
00187     /* check class - class is optional */
00188     if (class_buffer &&
00189             class_buffer->ptr &&
00190             (class_buffer->size > 0)) {
00191         parameters_set++;
00192 
00193         arm_uc_error_t retval = pal_raw_getClassGuid(&guid);
00194 
00195         if (retval.code == ERR_NONE) {
00196             uint32_t rc = ARM_UC_BinCompareCT(&guid_buffer, class_buffer);
00197             bool is_same = !rc;
00198 
00199             if (is_same) {
00200                 parameters_ok++;
00201             } else {
00202                 result.code = MFST_ERR_GUID_DEVCLASS;
00203             }
00204         }
00205     }
00206 
00207     /* check vendor - vendor is mandatory and has mask 0x10. */
00208 
00209     if (vendor_buffer &&
00210             vendor_buffer->ptr &&
00211             (vendor_buffer->size > 0)) {
00212         parameters_set += 0x10;
00213 
00214         arm_uc_error_t retval = pal_raw_getVendorGuid(&guid);
00215 
00216         if (retval.code == ERR_NONE) {
00217             uint32_t rc = ARM_UC_BinCompareCT(&guid_buffer, vendor_buffer);
00218             bool is_same = !rc;
00219 
00220             if (is_same) {
00221                 parameters_ok += 0x10;
00222             } else {
00223                 result.code = MFST_ERR_GUID_VENDOR;
00224             }
00225         }
00226     }
00227 
00228     /* Device ID checks out when:
00229         - vendor match and neither class nor device is passed
00230         - vendor and class match and no device is passed
00231         - vendor and device match and no class is passed
00232         - vendor and class and device match
00233     */
00234     if ((parameters_set >= 0x10) && (parameters_set == parameters_ok)) {
00235         result.code = ERR_NONE;
00236     }
00237 
00238     return result;
00239 }
00240 
00241 const ARM_PAL_DEVICE_IDENTITY arm_uc_device_identity_raw = {
00242     .SetVendorGuid          = pal_raw_setVendorGuid,
00243     .GetVendorGuid          = pal_raw_getVendorGuid,
00244     .SetClassGuid           = pal_raw_setClassGuid,
00245     .GetClassGuid           = pal_raw_getClassGuid,
00246     .SetDeviceGuid          = pal_raw_setDeviceGuid,
00247     .GetDeviceGuid          = pal_raw_getDeviceGuid,
00248     .DeviceIdentityCheck    = pal_raw_deviceIdentityCheck
00249 };
00250 
00251 #endif // ARM_UC_FEATURE_IDENTITY_RAW_CONFIG