leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 2 // Copyright 2016-2017 ARM Ltd.
leothedragon 0:8f0bb79ddd48 3 //
leothedragon 0:8f0bb79ddd48 4 // SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 5 //
leothedragon 0:8f0bb79ddd48 6 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:8f0bb79ddd48 7 // you may not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 8 // You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 9 //
leothedragon 0:8f0bb79ddd48 10 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 11 //
leothedragon 0:8f0bb79ddd48 12 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 13 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:8f0bb79ddd48 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 15 // See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 16 // limitations under the License.
leothedragon 0:8f0bb79ddd48 17 // ----------------------------------------------------------------------------
leothedragon 0:8f0bb79ddd48 18
leothedragon 0:8f0bb79ddd48 19 #include "update-client-hub/update_client_hub.h"
leothedragon 0:8f0bb79ddd48 20
leothedragon 0:8f0bb79ddd48 21 #include "update-client-common/arm_uc_common.h"
leothedragon 0:8f0bb79ddd48 22 #include "update-client-control-center/arm_uc_control_center.h"
leothedragon 0:8f0bb79ddd48 23 #include "update-client-control-center/arm_uc_pre_shared_key.h"
leothedragon 0:8f0bb79ddd48 24 #include "update-client-control-center/arm_uc_certificate.h"
leothedragon 0:8f0bb79ddd48 25 #include "update-client-source-manager/arm_uc_source_manager.h"
leothedragon 0:8f0bb79ddd48 26 #include "update-client-firmware-manager/arm_uc_firmware_manager.h"
leothedragon 0:8f0bb79ddd48 27 #include "update-client-manifest-manager/update-client-manifest-manager.h"
leothedragon 0:8f0bb79ddd48 28
leothedragon 0:8f0bb79ddd48 29 #include "update_client_hub_state_machine.h"
leothedragon 0:8f0bb79ddd48 30 #include "update_client_hub_event_handlers.h"
leothedragon 0:8f0bb79ddd48 31 #include "update_client_hub_error_handler.h"
leothedragon 0:8f0bb79ddd48 32
leothedragon 0:8f0bb79ddd48 33 #include "pal4life-device-identity/pal_device_identity.h"
leothedragon 0:8f0bb79ddd48 34
leothedragon 0:8f0bb79ddd48 35 #define HANDLE_INIT_ERROR(retval, msg, ...)\
leothedragon 0:8f0bb79ddd48 36 if (retval.error != ERR_NONE)\
leothedragon 0:8f0bb79ddd48 37 {\
leothedragon 0:8f0bb79ddd48 38 ARM_UC_HUB_setState(ARM_UC_HUB_STATE_UNINITIALIZED);\
leothedragon 0:8f0bb79ddd48 39 UC_HUB_ERR_MSG(msg " error code %s", ##__VA_ARGS__, ARM_UC_err2Str(retval));\
leothedragon 0:8f0bb79ddd48 40 return retval;\
leothedragon 0:8f0bb79ddd48 41 }
leothedragon 0:8f0bb79ddd48 42
leothedragon 0:8f0bb79ddd48 43 static const ARM_UPDATE_SOURCE **arm_uc_sources = NULL;
leothedragon 0:8f0bb79ddd48 44 static uint8_t arm_uc_sources_size = 0;
leothedragon 0:8f0bb79ddd48 45 extern arm_uc_mmContext_t *pManifestManagerContext;
leothedragon 0:8f0bb79ddd48 46
leothedragon 0:8f0bb79ddd48 47 /**
leothedragon 0:8f0bb79ddd48 48 * @brief Handle any errors posted by the scheduler.
leothedragon 0:8f0bb79ddd48 49 * @details This explicitly runs *not* in interrupt context, the scheduler has a dedicated
leothedragon 0:8f0bb79ddd48 50 * callback structure to ensure it can post at least this event.
leothedragon 0:8f0bb79ddd48 51 * ARM_UC_HUB_ErrorHandler() will invoke the HUB callback that was set up.
leothedragon 0:8f0bb79ddd48 52 * It is up to the external application to go about inducing a reset etc,
leothedragon 0:8f0bb79ddd48 53 * if that is what it decides. Note that the HUB is no longer operable
leothedragon 0:8f0bb79ddd48 54 * and the app should probably Uninitialize it and report an error.
leothedragon 0:8f0bb79ddd48 55 * However, the HUB will attempt some cleanup after it returns.
leothedragon 0:8f0bb79ddd48 56 * @param an_event the type of the event causing the error callback.
leothedragon 0:8f0bb79ddd48 57 * The only possible errors from the scheduler are currently:
leothedragon 0:8f0bb79ddd48 58 * ARM_UC_EQ_ERR_POOL_EXHAUSTED
leothedragon 0:8f0bb79ddd48 59 * ARM_UC_EQ_ERR_FAILED_TAKE
leothedragon 0:8f0bb79ddd48 60 * These are passed on to the Hub error handler as an internal error,
leothedragon 0:8f0bb79ddd48 61 * and the hub state is now considered unknown from this perspective.
leothedragon 0:8f0bb79ddd48 62 * (An internal error is considered fatal by the hub.)
leothedragon 0:8f0bb79ddd48 63 */
leothedragon 0:8f0bb79ddd48 64 void UC_HUB_scheduler_error_handler(uint32_t an_event)
leothedragon 0:8f0bb79ddd48 65 {
leothedragon 0:8f0bb79ddd48 66 UC_HUB_ERR_MSG("scheduler error: %" PRIu32, an_event);
leothedragon 0:8f0bb79ddd48 67 ARM_UC_HUB_ErrorHandler(HUB_ERR_INTERNAL_ERROR, ARM_UC_HUB_getState());
leothedragon 0:8f0bb79ddd48 68 }
leothedragon 0:8f0bb79ddd48 69
leothedragon 0:8f0bb79ddd48 70 /**
leothedragon 0:8f0bb79ddd48 71 * @brief Call initialiser of all components of the client.
leothedragon 0:8f0bb79ddd48 72 * finish asynchronously, will invoke callback when initialization is done.
leothedragon 0:8f0bb79ddd48 73 * @param init_cb the callback to be invoked at the end of initialization.
leothedragon 0:8f0bb79ddd48 74 */
leothedragon 0:8f0bb79ddd48 75 arm_uc_error_t ARM_UC_HUB_Initialize(void (*init_cb)(uintptr_t))
leothedragon 0:8f0bb79ddd48 76 {
leothedragon 0:8f0bb79ddd48 77 arm_uc_error_t retval;
leothedragon 0:8f0bb79ddd48 78
leothedragon 0:8f0bb79ddd48 79 if (ARM_UC_HUB_getState() != ARM_UC_HUB_STATE_UNINITIALIZED) {
leothedragon 0:8f0bb79ddd48 80 UC_HUB_ERR_MSG("Already Initialized/Initializing");
leothedragon 0:8f0bb79ddd48 81 return (arm_uc_error_t) { ERR_INVALID_STATE };
leothedragon 0:8f0bb79ddd48 82 }
leothedragon 0:8f0bb79ddd48 83 ARM_UC_HUB_setState(ARM_UC_HUB_STATE_INITIALIZING);
leothedragon 0:8f0bb79ddd48 84
leothedragon 0:8f0bb79ddd48 85 ARM_UC_SchedulerInit();
leothedragon 0:8f0bb79ddd48 86 ARM_UC_HUB_setInitializationCallback(init_cb);
leothedragon 0:8f0bb79ddd48 87 ARM_UC_SetSchedulerErrorHandler(UC_HUB_scheduler_error_handler);
leothedragon 0:8f0bb79ddd48 88
leothedragon 0:8f0bb79ddd48 89 /* Register event handler with Control Center. */
leothedragon 0:8f0bb79ddd48 90 retval = ARM_UC_ControlCenter_Initialize(ARM_UC_HUB_ControlCenterEventHandler);
leothedragon 0:8f0bb79ddd48 91 HANDLE_INIT_ERROR(retval, "Control Center init failed")
leothedragon 0:8f0bb79ddd48 92
leothedragon 0:8f0bb79ddd48 93 /* Register event handler with Firmware Manager */
leothedragon 0:8f0bb79ddd48 94 retval = ARM_UC_FirmwareManager.Initialize(ARM_UC_HUB_FirmwareManagerEventHandler);
leothedragon 0:8f0bb79ddd48 95 HANDLE_INIT_ERROR(retval, "Firmware Manager init failed")
leothedragon 0:8f0bb79ddd48 96
leothedragon 0:8f0bb79ddd48 97 /* Register event handler with Source Manager */
leothedragon 0:8f0bb79ddd48 98 retval = ARM_UC_SourceManager.Initialize(ARM_UC_HUB_SourceManagerEventHandler);
leothedragon 0:8f0bb79ddd48 99 HANDLE_INIT_ERROR(retval, "Source Manager init failed")
leothedragon 0:8f0bb79ddd48 100
leothedragon 0:8f0bb79ddd48 101 for (uint8_t index = 0; index < arm_uc_sources_size; index++) {
leothedragon 0:8f0bb79ddd48 102 ARM_UC_SourceManager.AddSource(arm_uc_sources[index]);
leothedragon 0:8f0bb79ddd48 103 }
leothedragon 0:8f0bb79ddd48 104
leothedragon 0:8f0bb79ddd48 105 /* Register event handler and add config store implementation to manifest
leothedragon 0:8f0bb79ddd48 106 manager.
leothedragon 0:8f0bb79ddd48 107 */
leothedragon 0:8f0bb79ddd48 108 retval = ARM_UC_mmInit(&pManifestManagerContext,
leothedragon 0:8f0bb79ddd48 109 ARM_UC_HUB_ManifestManagerEventHandler,
leothedragon 0:8f0bb79ddd48 110 NULL);
leothedragon 0:8f0bb79ddd48 111 HANDLE_INIT_ERROR(retval, "Manifest manager init failed")
leothedragon 0:8f0bb79ddd48 112
leothedragon 0:8f0bb79ddd48 113 /* add hard coded certificates to the manifest manager */
leothedragon 0:8f0bb79ddd48 114 // retval = ARM_UC_mmStoreCertificate(CA_PATH, cert, CERT_SIZE);
leothedragon 0:8f0bb79ddd48 115 // if ((retval.error != ERR_NONE) && (retval.code != MFST_ERR_PENDING))
leothedragon 0:8f0bb79ddd48 116 // {
leothedragon 0:8f0bb79ddd48 117 // HANDLE_INIT_ERROR(retval, "Manifest manager StoreCertificate failed")
leothedragon 0:8f0bb79ddd48 118 // }
leothedragon 0:8f0bb79ddd48 119
leothedragon 0:8f0bb79ddd48 120 return (arm_uc_error_t) { ERR_NONE };
leothedragon 0:8f0bb79ddd48 121 }
leothedragon 0:8f0bb79ddd48 122
leothedragon 0:8f0bb79ddd48 123 /**
leothedragon 0:8f0bb79ddd48 124 * @brief Process events in the event queue.
leothedragon 0:8f0bb79ddd48 125 */
leothedragon 0:8f0bb79ddd48 126 arm_uc_error_t ARM_UC_HUB_ProcessEvents()
leothedragon 0:8f0bb79ddd48 127 {
leothedragon 0:8f0bb79ddd48 128 ARM_UC_ProcessQueue();
leothedragon 0:8f0bb79ddd48 129
leothedragon 0:8f0bb79ddd48 130 return (arm_uc_error_t) { ERR_NONE };
leothedragon 0:8f0bb79ddd48 131 }
leothedragon 0:8f0bb79ddd48 132
leothedragon 0:8f0bb79ddd48 133 /**
leothedragon 0:8f0bb79ddd48 134 * @brief Register callback function for when callbacks are added to an empty queue.
leothedragon 0:8f0bb79ddd48 135 */
leothedragon 0:8f0bb79ddd48 136 arm_uc_error_t ARM_UC_HUB_AddNotificationHandler(void (*handler)(void))
leothedragon 0:8f0bb79ddd48 137 {
leothedragon 0:8f0bb79ddd48 138 ARM_UC_AddNotificationHandler(handler);
leothedragon 0:8f0bb79ddd48 139
leothedragon 0:8f0bb79ddd48 140 return (arm_uc_error_t) { ERR_NONE };
leothedragon 0:8f0bb79ddd48 141 }
leothedragon 0:8f0bb79ddd48 142
leothedragon 0:8f0bb79ddd48 143 /**
leothedragon 0:8f0bb79ddd48 144 * @brief Add source to the Update Client.
leothedragon 0:8f0bb79ddd48 145 */
leothedragon 0:8f0bb79ddd48 146 arm_uc_error_t ARM_UC_HUB_SetSources(const ARM_UPDATE_SOURCE *sources[],
leothedragon 0:8f0bb79ddd48 147 uint8_t size)
leothedragon 0:8f0bb79ddd48 148 {
leothedragon 0:8f0bb79ddd48 149 arm_uc_sources = sources;
leothedragon 0:8f0bb79ddd48 150 arm_uc_sources_size = size;
leothedragon 0:8f0bb79ddd48 151
leothedragon 0:8f0bb79ddd48 152 return (arm_uc_error_t) { ERR_NONE };
leothedragon 0:8f0bb79ddd48 153 }
leothedragon 0:8f0bb79ddd48 154
leothedragon 0:8f0bb79ddd48 155 /**
leothedragon 0:8f0bb79ddd48 156 * Set PAAL Update implementation
leothedragon 0:8f0bb79ddd48 157 */
leothedragon 0:8f0bb79ddd48 158 arm_uc_error_t ARM_UC_HUB_SetStorage(const ARM_UC_PAAL_UPDATE *implementation)
leothedragon 0:8f0bb79ddd48 159 {
leothedragon 0:8f0bb79ddd48 160 return ARM_UCP_SetPAALUpdate(implementation);
leothedragon 0:8f0bb79ddd48 161 }
leothedragon 0:8f0bb79ddd48 162
leothedragon 0:8f0bb79ddd48 163 /**
leothedragon 0:8f0bb79ddd48 164 * @brief Add monitor to the control center.
leothedragon 0:8f0bb79ddd48 165 */
leothedragon 0:8f0bb79ddd48 166 arm_uc_error_t ARM_UC_HUB_AddMonitor(const ARM_UPDATE_MONITOR *monitor)
leothedragon 0:8f0bb79ddd48 167 {
leothedragon 0:8f0bb79ddd48 168 return ARM_UC_ControlCenter_AddMonitor(monitor);
leothedragon 0:8f0bb79ddd48 169 }
leothedragon 0:8f0bb79ddd48 170
leothedragon 0:8f0bb79ddd48 171 /**
leothedragon 0:8f0bb79ddd48 172 * @brief Temporary error reporting function.
leothedragon 0:8f0bb79ddd48 173 */
leothedragon 0:8f0bb79ddd48 174 void ARM_UC_HUB_AddErrorCallback(void (*callback)(int32_t error))
leothedragon 0:8f0bb79ddd48 175 {
leothedragon 0:8f0bb79ddd48 176 ARM_UC_HUB_AddErrorCallbackInternal(callback);
leothedragon 0:8f0bb79ddd48 177 }
leothedragon 0:8f0bb79ddd48 178
leothedragon 0:8f0bb79ddd48 179 /**
leothedragon 0:8f0bb79ddd48 180 * @brief Authorize request.
leothedragon 0:8f0bb79ddd48 181 */
leothedragon 0:8f0bb79ddd48 182 arm_uc_error_t ARM_UC_Authorize(arm_uc_request_t request)
leothedragon 0:8f0bb79ddd48 183 {
leothedragon 0:8f0bb79ddd48 184 return ARM_UC_ControlCenter_Authorize(request);
leothedragon 0:8f0bb79ddd48 185 }
leothedragon 0:8f0bb79ddd48 186
leothedragon 0:8f0bb79ddd48 187 /**
leothedragon 0:8f0bb79ddd48 188 * @brief Set callback for receiving download progress.
leothedragon 0:8f0bb79ddd48 189 * @details User application call for setting callback handler.
leothedragon 0:8f0bb79ddd48 190 * The callback function takes the progreess in percent as argument.
leothedragon 0:8f0bb79ddd48 191 *
leothedragon 0:8f0bb79ddd48 192 * @param callback Function pointer to the progress function.
leothedragon 0:8f0bb79ddd48 193 * @return Error code.
leothedragon 0:8f0bb79ddd48 194 */
leothedragon 0:8f0bb79ddd48 195 arm_uc_error_t ARM_UC_SetProgressHandler(void (*callback)(uint32_t progress, uint32_t total))
leothedragon 0:8f0bb79ddd48 196 {
leothedragon 0:8f0bb79ddd48 197 return ARM_UC_ControlCenter_SetProgressHandler(callback);
leothedragon 0:8f0bb79ddd48 198 }
leothedragon 0:8f0bb79ddd48 199
leothedragon 0:8f0bb79ddd48 200 /**
leothedragon 0:8f0bb79ddd48 201 * @brief Set callback function for authorizing requests.
leothedragon 0:8f0bb79ddd48 202 * @details User application call for setting callback handler.
leothedragon 0:8f0bb79ddd48 203 * The callback function takes an enum request and an authorization
leothedragon 0:8f0bb79ddd48 204 * function pointer. To authorize the given request, the caller
leothedragon 0:8f0bb79ddd48 205 * invokes the authorization function.
leothedragon 0:8f0bb79ddd48 206 *
leothedragon 0:8f0bb79ddd48 207 * @param callback Function pointer to the authorization function.
leothedragon 0:8f0bb79ddd48 208 * @return Error code.
leothedragon 0:8f0bb79ddd48 209 */
leothedragon 0:8f0bb79ddd48 210 arm_uc_error_t ARM_UC_SetAuthorizeHandler(void (*callback)(int32_t))
leothedragon 0:8f0bb79ddd48 211 {
leothedragon 0:8f0bb79ddd48 212 return ARM_UC_ControlCenter_SetAuthorityHandler(callback);
leothedragon 0:8f0bb79ddd48 213 }
leothedragon 0:8f0bb79ddd48 214
leothedragon 0:8f0bb79ddd48 215 /**
leothedragon 0:8f0bb79ddd48 216 * @brief Override update authorization handler.
leothedragon 0:8f0bb79ddd48 217 * @details Force download and update to progress regardless of authorization
leothedragon 0:8f0bb79ddd48 218 * handler. This function is used for unblocking an update in a buggy
leothedragon 0:8f0bb79ddd48 219 * application.
leothedragon 0:8f0bb79ddd48 220 */
leothedragon 0:8f0bb79ddd48 221 void ARM_UC_OverrideAuthorization(void)
leothedragon 0:8f0bb79ddd48 222 {
leothedragon 0:8f0bb79ddd48 223 ARM_UC_ControlCenter_OverrideAuthorization();
leothedragon 0:8f0bb79ddd48 224 }
leothedragon 0:8f0bb79ddd48 225
leothedragon 0:8f0bb79ddd48 226 #if defined(ARM_UC_FEATURE_MANIFEST_PUBKEY) && (ARM_UC_FEATURE_MANIFEST_PUBKEY == 1)
leothedragon 0:8f0bb79ddd48 227 /**
leothedragon 0:8f0bb79ddd48 228 * @brief Add certificate.
leothedragon 0:8f0bb79ddd48 229 * @details [long description]
leothedragon 0:8f0bb79ddd48 230 *
leothedragon 0:8f0bb79ddd48 231 * @param certificate Pointer to certiface being added.
leothedragon 0:8f0bb79ddd48 232 * @param certificate_length Certificate length.
leothedragon 0:8f0bb79ddd48 233 * @param fingerprint Pointer to the fingerprint of the certificate being added.
leothedragon 0:8f0bb79ddd48 234 * @param fingerprint_length Fingerprint length.
leothedragon 0:8f0bb79ddd48 235 * @return Error code.
leothedragon 0:8f0bb79ddd48 236 */
leothedragon 0:8f0bb79ddd48 237 arm_uc_error_t ARM_UC_AddCertificate(const uint8_t *certificate,
leothedragon 0:8f0bb79ddd48 238 uint16_t certificate_length,
leothedragon 0:8f0bb79ddd48 239 const uint8_t *fingerprint,
leothedragon 0:8f0bb79ddd48 240 uint16_t fingerprint_length,
leothedragon 0:8f0bb79ddd48 241 void (*callback)(arm_uc_error_t, const arm_uc_buffer_t *))
leothedragon 0:8f0bb79ddd48 242 {
leothedragon 0:8f0bb79ddd48 243 return ARM_UC_Certificate_Add(certificate,
leothedragon 0:8f0bb79ddd48 244 certificate_length,
leothedragon 0:8f0bb79ddd48 245 fingerprint,
leothedragon 0:8f0bb79ddd48 246 fingerprint_length,
leothedragon 0:8f0bb79ddd48 247 callback);
leothedragon 0:8f0bb79ddd48 248 }
leothedragon 0:8f0bb79ddd48 249 #endif /* ARM_UC_FEATURE_MANIFEST_PUBKEY */
leothedragon 0:8f0bb79ddd48 250
leothedragon 0:8f0bb79ddd48 251 #if defined(ARM_UC_FEATURE_MANIFEST_PSK) && (ARM_UC_FEATURE_MANIFEST_PSK == 1)
leothedragon 0:8f0bb79ddd48 252 /**
leothedragon 0:8f0bb79ddd48 253 * @brief Set pointer to pre-shared-key with the given size.
leothedragon 0:8f0bb79ddd48 254 *
leothedragon 0:8f0bb79ddd48 255 * @param key Pointer to pre-shared-key.
leothedragon 0:8f0bb79ddd48 256 * @param bits Key size in bits.
leothedragon 0:8f0bb79ddd48 257 *
leothedragon 0:8f0bb79ddd48 258 * @return Error code.
leothedragon 0:8f0bb79ddd48 259 */
leothedragon 0:8f0bb79ddd48 260 arm_uc_error_t ARM_UC_AddPreSharedKey(const uint8_t *key, uint16_t bits)
leothedragon 0:8f0bb79ddd48 261 {
leothedragon 0:8f0bb79ddd48 262 return ARM_UC_PreSharedKey_SetSecret(key, bits);
leothedragon 0:8f0bb79ddd48 263 }
leothedragon 0:8f0bb79ddd48 264 #endif
leothedragon 0:8f0bb79ddd48 265
leothedragon 0:8f0bb79ddd48 266 /**
leothedragon 0:8f0bb79ddd48 267 * @brief Function for setting the vendor ID.
leothedragon 0:8f0bb79ddd48 268 * @details The ID is copied to a 16 byte struct. Any data after the first
leothedragon 0:8f0bb79ddd48 269 * 16 bytes will be ignored.
leothedragon 0:8f0bb79ddd48 270 * @param id Pointer to ID.
leothedragon 0:8f0bb79ddd48 271 * @param length Length of ID.
leothedragon 0:8f0bb79ddd48 272 * @return Error code.
leothedragon 0:8f0bb79ddd48 273 */
leothedragon 0:8f0bb79ddd48 274 arm_uc_error_t ARM_UC_SetVendorId(const uint8_t *id, uint8_t length)
leothedragon 0:8f0bb79ddd48 275 {
leothedragon 0:8f0bb79ddd48 276 arm_uc_guid_t uuid = { 0 };
leothedragon 0:8f0bb79ddd48 277
leothedragon 0:8f0bb79ddd48 278 if (id) {
leothedragon 0:8f0bb79ddd48 279 for (uint8_t index = 0;
leothedragon 0:8f0bb79ddd48 280 (index < sizeof(arm_uc_guid_t) && (index < length));
leothedragon 0:8f0bb79ddd48 281 index++) {
leothedragon 0:8f0bb79ddd48 282 ((uint8_t *) uuid)[index] = id[index];
leothedragon 0:8f0bb79ddd48 283 }
leothedragon 0:8f0bb79ddd48 284 }
leothedragon 0:8f0bb79ddd48 285
leothedragon 0:8f0bb79ddd48 286 return pal_setVendorGuid(&uuid);
leothedragon 0:8f0bb79ddd48 287 }
leothedragon 0:8f0bb79ddd48 288
leothedragon 0:8f0bb79ddd48 289 /**
leothedragon 0:8f0bb79ddd48 290 * @brief Function for setting the class ID.
leothedragon 0:8f0bb79ddd48 291 * @details The ID is copied to a 16 byte struct. Any data after the first
leothedragon 0:8f0bb79ddd48 292 * 16 bytes will be ignored.
leothedragon 0:8f0bb79ddd48 293 * @param id Pointer to ID.
leothedragon 0:8f0bb79ddd48 294 * @param length Length of ID.
leothedragon 0:8f0bb79ddd48 295 * @return Error code.
leothedragon 0:8f0bb79ddd48 296 */
leothedragon 0:8f0bb79ddd48 297 arm_uc_error_t ARM_UC_SetClassId(const uint8_t *id, uint8_t length)
leothedragon 0:8f0bb79ddd48 298 {
leothedragon 0:8f0bb79ddd48 299 arm_uc_guid_t uuid = { 0 };
leothedragon 0:8f0bb79ddd48 300
leothedragon 0:8f0bb79ddd48 301 if (id) {
leothedragon 0:8f0bb79ddd48 302 for (uint8_t index = 0;
leothedragon 0:8f0bb79ddd48 303 (index < sizeof(arm_uc_guid_t) && (index < length));
leothedragon 0:8f0bb79ddd48 304 index++) {
leothedragon 0:8f0bb79ddd48 305 ((uint8_t *) uuid)[index] = id[index];
leothedragon 0:8f0bb79ddd48 306 }
leothedragon 0:8f0bb79ddd48 307 }
leothedragon 0:8f0bb79ddd48 308
leothedragon 0:8f0bb79ddd48 309 return pal_setClassGuid(&uuid);
leothedragon 0:8f0bb79ddd48 310 }
leothedragon 0:8f0bb79ddd48 311
leothedragon 0:8f0bb79ddd48 312 /**
leothedragon 0:8f0bb79ddd48 313 * @brief Function for setting the device ID.
leothedragon 0:8f0bb79ddd48 314 * @details The ID is copied to a 16 byte struct. Any data after the first
leothedragon 0:8f0bb79ddd48 315 * 16 bytes will be ignored.
leothedragon 0:8f0bb79ddd48 316 * @param id Pointer to ID.
leothedragon 0:8f0bb79ddd48 317 * @param length Length of ID.
leothedragon 0:8f0bb79ddd48 318 * @return Error code.
leothedragon 0:8f0bb79ddd48 319 */
leothedragon 0:8f0bb79ddd48 320 arm_uc_error_t ARM_UC_SetDeviceId(const uint8_t *id, uint8_t length)
leothedragon 0:8f0bb79ddd48 321 {
leothedragon 0:8f0bb79ddd48 322 arm_uc_guid_t uuid = { 0 };
leothedragon 0:8f0bb79ddd48 323
leothedragon 0:8f0bb79ddd48 324 if (id) {
leothedragon 0:8f0bb79ddd48 325 for (uint8_t index = 0;
leothedragon 0:8f0bb79ddd48 326 (index < sizeof(arm_uc_guid_t) && (index < length));
leothedragon 0:8f0bb79ddd48 327 index++) {
leothedragon 0:8f0bb79ddd48 328 ((uint8_t *) uuid)[index] = id[index];
leothedragon 0:8f0bb79ddd48 329 }
leothedragon 0:8f0bb79ddd48 330 }
leothedragon 0:8f0bb79ddd48 331
leothedragon 0:8f0bb79ddd48 332 return pal_setDeviceGuid(&uuid);
leothedragon 0:8f0bb79ddd48 333 }
leothedragon 0:8f0bb79ddd48 334
leothedragon 0:8f0bb79ddd48 335 /**
leothedragon 0:8f0bb79ddd48 336 * @brief Function for reporting the vendor ID.
leothedragon 0:8f0bb79ddd48 337 * @details 16 bytes are copied into the supplied buffer.
leothedragon 0:8f0bb79ddd48 338 * @param id Pointer to storage for ID. MUST be at least 16 bytes long.
leothedragon 0:8f0bb79ddd48 339 * @param id_max the size of the ID buffer
leothedragon 0:8f0bb79ddd48 340 * @param id_size pointer to a variable to receive the size of the ID
leothedragon 0:8f0bb79ddd48 341 * written into the buffer (always 16).
leothedragon 0:8f0bb79ddd48 342 * @return Error code.
leothedragon 0:8f0bb79ddd48 343 */
leothedragon 0:8f0bb79ddd48 344 arm_uc_error_t ARM_UC_GetVendorId(uint8_t *id,
leothedragon 0:8f0bb79ddd48 345 const size_t id_max,
leothedragon 0:8f0bb79ddd48 346 size_t *id_size)
leothedragon 0:8f0bb79ddd48 347 {
leothedragon 0:8f0bb79ddd48 348 arm_uc_guid_t guid = {0};
leothedragon 0:8f0bb79ddd48 349 arm_uc_error_t err = {ERR_NONE};
leothedragon 0:8f0bb79ddd48 350 if (id_max < sizeof(arm_uc_guid_t)) {
leothedragon 0:8f0bb79ddd48 351 err.code = ARM_UC_DI_ERR_SIZE;
leothedragon 0:8f0bb79ddd48 352 }
leothedragon 0:8f0bb79ddd48 353 if (err.error == ERR_NONE) {
leothedragon 0:8f0bb79ddd48 354 err = pal_getVendorGuid(&guid);
leothedragon 0:8f0bb79ddd48 355 }
leothedragon 0:8f0bb79ddd48 356 if (err.error == ERR_NONE) {
leothedragon 0:8f0bb79ddd48 357 memcpy(id, guid, sizeof(arm_uc_guid_t));
leothedragon 0:8f0bb79ddd48 358 if (id_size != NULL) {
leothedragon 0:8f0bb79ddd48 359 *id_size = sizeof(arm_uc_guid_t);
leothedragon 0:8f0bb79ddd48 360 }
leothedragon 0:8f0bb79ddd48 361 }
leothedragon 0:8f0bb79ddd48 362 return err;
leothedragon 0:8f0bb79ddd48 363 }
leothedragon 0:8f0bb79ddd48 364
leothedragon 0:8f0bb79ddd48 365 /**
leothedragon 0:8f0bb79ddd48 366 * @brief Function for reporting the class ID.
leothedragon 0:8f0bb79ddd48 367 * @details 16 bytes are copied into the supplied buffer.
leothedragon 0:8f0bb79ddd48 368 * @param id Pointer to storage for ID. MUST be at least 16 bytes long.
leothedragon 0:8f0bb79ddd48 369 * @param id_max the size of the ID buffer
leothedragon 0:8f0bb79ddd48 370 * @param id_size pointer to a variable to receive the size of the ID
leothedragon 0:8f0bb79ddd48 371 * written into the buffer (always 16).
leothedragon 0:8f0bb79ddd48 372 * @return Error code.
leothedragon 0:8f0bb79ddd48 373 */
leothedragon 0:8f0bb79ddd48 374 arm_uc_error_t ARM_UC_GetClassId(uint8_t *id,
leothedragon 0:8f0bb79ddd48 375 const size_t id_max,
leothedragon 0:8f0bb79ddd48 376 size_t *id_size)
leothedragon 0:8f0bb79ddd48 377 {
leothedragon 0:8f0bb79ddd48 378 arm_uc_guid_t guid = {0};
leothedragon 0:8f0bb79ddd48 379 arm_uc_error_t err = {ERR_NONE};
leothedragon 0:8f0bb79ddd48 380 if (id_max < sizeof(arm_uc_guid_t)) {
leothedragon 0:8f0bb79ddd48 381 err.code = ARM_UC_DI_ERR_SIZE;
leothedragon 0:8f0bb79ddd48 382 }
leothedragon 0:8f0bb79ddd48 383 if (err.error == ERR_NONE) {
leothedragon 0:8f0bb79ddd48 384 err = pal_getClassGuid(&guid);
leothedragon 0:8f0bb79ddd48 385 }
leothedragon 0:8f0bb79ddd48 386 if (err.error == ERR_NONE) {
leothedragon 0:8f0bb79ddd48 387 memcpy(id, guid, sizeof(arm_uc_guid_t));
leothedragon 0:8f0bb79ddd48 388 if (id_size != NULL) {
leothedragon 0:8f0bb79ddd48 389 *id_size = sizeof(arm_uc_guid_t);
leothedragon 0:8f0bb79ddd48 390 }
leothedragon 0:8f0bb79ddd48 391 }
leothedragon 0:8f0bb79ddd48 392 return err;
leothedragon 0:8f0bb79ddd48 393 }
leothedragon 0:8f0bb79ddd48 394
leothedragon 0:8f0bb79ddd48 395 /**
leothedragon 0:8f0bb79ddd48 396 * @brief Function for reporting the device ID.
leothedragon 0:8f0bb79ddd48 397 * @details 16 bytes are copied into the supplied buffer.
leothedragon 0:8f0bb79ddd48 398 * @param id Pointer to storage for ID. MUST be at least 16 bytes long.
leothedragon 0:8f0bb79ddd48 399 * @param id_max the size of the ID buffer
leothedragon 0:8f0bb79ddd48 400 * @param id_size pointer to a variable to receive the size of the ID
leothedragon 0:8f0bb79ddd48 401 * written into the buffer (always 16).
leothedragon 0:8f0bb79ddd48 402 * @return Error code.
leothedragon 0:8f0bb79ddd48 403 */
leothedragon 0:8f0bb79ddd48 404 arm_uc_error_t ARM_UC_GetDeviceId(uint8_t *id,
leothedragon 0:8f0bb79ddd48 405 const size_t id_max,
leothedragon 0:8f0bb79ddd48 406 size_t *id_size)
leothedragon 0:8f0bb79ddd48 407 {
leothedragon 0:8f0bb79ddd48 408 arm_uc_guid_t guid = {0};
leothedragon 0:8f0bb79ddd48 409 arm_uc_error_t err = {ERR_NONE};
leothedragon 0:8f0bb79ddd48 410 if (id_max < sizeof(arm_uc_guid_t)) {
leothedragon 0:8f0bb79ddd48 411 err.code = ARM_UC_DI_ERR_SIZE;
leothedragon 0:8f0bb79ddd48 412 }
leothedragon 0:8f0bb79ddd48 413 if (err.error == ERR_NONE) {
leothedragon 0:8f0bb79ddd48 414 err = pal_getDeviceGuid(&guid);
leothedragon 0:8f0bb79ddd48 415 }
leothedragon 0:8f0bb79ddd48 416 if (err.error == ERR_NONE) {
leothedragon 0:8f0bb79ddd48 417 memcpy(id, guid, sizeof(arm_uc_guid_t));
leothedragon 0:8f0bb79ddd48 418 if (id_size != NULL) {
leothedragon 0:8f0bb79ddd48 419 *id_size = sizeof(arm_uc_guid_t);
leothedragon 0:8f0bb79ddd48 420 }
leothedragon 0:8f0bb79ddd48 421 }
leothedragon 0:8f0bb79ddd48 422 return err;
leothedragon 0:8f0bb79ddd48 423 }
leothedragon 0:8f0bb79ddd48 424
leothedragon 0:8f0bb79ddd48 425 arm_uc_error_t ARM_UC_HUB_Uninitialize(void)
leothedragon 0:8f0bb79ddd48 426 {
leothedragon 0:8f0bb79ddd48 427 if (ARM_UC_HUB_getState() <= ARM_UC_HUB_STATE_INITIALIZED) {
leothedragon 0:8f0bb79ddd48 428 UC_HUB_ERR_MSG("Update Client not initialized");
leothedragon 0:8f0bb79ddd48 429 return (arm_uc_error_t) { ERR_INVALID_STATE };
leothedragon 0:8f0bb79ddd48 430 }
leothedragon 0:8f0bb79ddd48 431
leothedragon 0:8f0bb79ddd48 432 arm_uc_error_t err = ARM_UC_SourceManager.Uninitialize();
leothedragon 0:8f0bb79ddd48 433 ARM_UC_HUB_setState(ARM_UC_HUB_STATE_UNINITIALIZED);
leothedragon 0:8f0bb79ddd48 434 return err;
leothedragon 0:8f0bb79ddd48 435 }
leothedragon 0:8f0bb79ddd48 436
leothedragon 0:8f0bb79ddd48 437 /**
leothedragon 0:8f0bb79ddd48 438 * @brief Return the details of the active firmware.
leothedragon 0:8f0bb79ddd48 439 * @param details Pointer to the firmware details structure.
leothedragon 0:8f0bb79ddd48 440 * @return ARM_UC_HUB_ERR_NOT_AVAILABLE if the active firmware details
leothedragon 0:8f0bb79ddd48 441 * are not yet available, ERR_INVALID_PARAMETER if "details" is
leothedragon 0:8f0bb79ddd48 442 * NULL or ERR_NONE for success.
leothedragon 0:8f0bb79ddd48 443 */
leothedragon 0:8f0bb79ddd48 444 arm_uc_error_t ARM_UC_API_GetActiveFirmwareDetails(arm_uc_firmware_details_t *details)
leothedragon 0:8f0bb79ddd48 445 {
leothedragon 0:8f0bb79ddd48 446 arm_uc_error_t err = {ARM_UC_HUB_ERR_NOT_AVAILABLE};
leothedragon 0:8f0bb79ddd48 447
leothedragon 0:8f0bb79ddd48 448 if (details == NULL) {
leothedragon 0:8f0bb79ddd48 449 err.code = ERR_INVALID_PARAMETER;
leothedragon 0:8f0bb79ddd48 450 } else {
leothedragon 0:8f0bb79ddd48 451 arm_uc_firmware_details_t *hub_details = ARM_UC_HUB_getActiveFirmwareDetails();
leothedragon 0:8f0bb79ddd48 452 if (hub_details) {
leothedragon 0:8f0bb79ddd48 453 memcpy(details, hub_details, sizeof(arm_uc_firmware_details_t));
leothedragon 0:8f0bb79ddd48 454 err.code = ERR_NONE;
leothedragon 0:8f0bb79ddd48 455 }
leothedragon 0:8f0bb79ddd48 456 }
leothedragon 0:8f0bb79ddd48 457 return err;
leothedragon 0:8f0bb79ddd48 458 }
leothedragon 0:8f0bb79ddd48 459
leothedragon 0:8f0bb79ddd48 460 /**
leothedragon 0:8f0bb79ddd48 461 * @brief Return whether or not the given state is a valid defined one.
leothedragon 0:8f0bb79ddd48 462 */
leothedragon 0:8f0bb79ddd48 463 bool ARM_UC_IsValidState(arm_uc_update_state_t an_update_state)
leothedragon 0:8f0bb79ddd48 464 {
leothedragon 0:8f0bb79ddd48 465 int val = (int)an_update_state;
leothedragon 0:8f0bb79ddd48 466 bool is_valid = ((val >= (int)ARM_UC_UPDATE_STATE_FIRST)
leothedragon 0:8f0bb79ddd48 467 && (val <= (int)ARM_UC_UPDATE_STATE_LAST));
leothedragon 0:8f0bb79ddd48 468 if (!is_valid) UC_ERROR_ERR_MSG("Invalid UC HUB reported state");
leothedragon 0:8f0bb79ddd48 469 return is_valid;
leothedragon 0:8f0bb79ddd48 470 }
leothedragon 0:8f0bb79ddd48 471
leothedragon 0:8f0bb79ddd48 472 /**
leothedragon 0:8f0bb79ddd48 473 * @brief Return whether or not the given result is a valid defined one.
leothedragon 0:8f0bb79ddd48 474 */
leothedragon 0:8f0bb79ddd48 475 bool ARM_UC_IsValidResult(arm_uc_update_result_t an_update_result)
leothedragon 0:8f0bb79ddd48 476 {
leothedragon 0:8f0bb79ddd48 477 bool is_valid = ((an_update_result >= ARM_UC_UPDATE_RESULT_UPDATE_FIRST)
leothedragon 0:8f0bb79ddd48 478 && (an_update_result <= ARM_UC_UPDATE_RESULT_UPDATE_LAST))
leothedragon 0:8f0bb79ddd48 479 || ((an_update_result >= ARM_UC_UPDATE_RESULT_FETCHER_FIRST)
leothedragon 0:8f0bb79ddd48 480 && (an_update_result <= ARM_UC_UPDATE_RESULT_FETCHER_LAST))
leothedragon 0:8f0bb79ddd48 481 || ((an_update_result >= ARM_UC_UPDATE_RESULT_WRITER_FIRST)
leothedragon 0:8f0bb79ddd48 482 && (an_update_result <= ARM_UC_UPDATE_RESULT_WRITER_LAST))
leothedragon 0:8f0bb79ddd48 483 || ((an_update_result >= ARM_UC_UPDATE_RESULT_PROCESSOR_FIRST)
leothedragon 0:8f0bb79ddd48 484 && (an_update_result <= ARM_UC_UPDATE_RESULT_PROCESSOR_LAST))
leothedragon 0:8f0bb79ddd48 485 || ((an_update_result >= ARM_UC_UPDATE_RESULT_MANIFEST_FIRST)
leothedragon 0:8f0bb79ddd48 486 && (an_update_result <= ARM_UC_UPDATE_RESULT_MANIFEST_LAST));
leothedragon 0:8f0bb79ddd48 487 if (!is_valid) UC_ERROR_ERR_MSG("Invalid UC HUB reported state");
leothedragon 0:8f0bb79ddd48 488 return is_valid;
leothedragon 0:8f0bb79ddd48 489 }