,,

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-2018 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-helper/update-helper.h"
leothedragon 0:8f0bb79ddd48 20
leothedragon 0:8f0bb79ddd48 21 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
leothedragon 0:8f0bb79ddd48 22
leothedragon 0:8f0bb79ddd48 23 #include <stdio.h>
leothedragon 0:8f0bb79ddd48 24 #include <stdint.h>
leothedragon 0:8f0bb79ddd48 25
leothedragon 0:8f0bb79ddd48 26 static MbedCloudClient* _client;
leothedragon 0:8f0bb79ddd48 27
leothedragon 0:8f0bb79ddd48 28 #ifdef ARM_UPDATE_CLIENT_VERSION_VALUE
leothedragon 0:8f0bb79ddd48 29 #if ARM_UPDATE_CLIENT_VERSION_VALUE > 101000
leothedragon 0:8f0bb79ddd48 30 void update_helper_set_cloud_client(MbedCloudClient* client)
leothedragon 0:8f0bb79ddd48 31 {
leothedragon 0:8f0bb79ddd48 32 _client = client;
leothedragon 0:8f0bb79ddd48 33 }
leothedragon 0:8f0bb79ddd48 34
leothedragon 0:8f0bb79ddd48 35 void __attribute__((weak)) update_authorize(int32_t request)
leothedragon 0:8f0bb79ddd48 36 {
leothedragon 0:8f0bb79ddd48 37 switch (request)
leothedragon 0:8f0bb79ddd48 38 {
leothedragon 0:8f0bb79ddd48 39 /* Cloud Client wishes to download new firmware. This can have a negative
leothedragon 0:8f0bb79ddd48 40 impact on the performance of the rest of the system.
leothedragon 0:8f0bb79ddd48 41
leothedragon 0:8f0bb79ddd48 42 The user application is supposed to pause performance sensitive tasks
leothedragon 0:8f0bb79ddd48 43 before authorizing the download.
leothedragon 0:8f0bb79ddd48 44
leothedragon 0:8f0bb79ddd48 45 Note: the authorization call can be postponed and called later.
leothedragon 0:8f0bb79ddd48 46 This doesn't affect the performance of the Cloud Client.
leothedragon 0:8f0bb79ddd48 47 */
leothedragon 0:8f0bb79ddd48 48 case MbedCloudClient::UpdateRequestDownload:
leothedragon 0:8f0bb79ddd48 49 printf("Firmware download requested\r\n");
leothedragon 0:8f0bb79ddd48 50 printf("Authorization granted\r\n");
leothedragon 0:8f0bb79ddd48 51 _client->update_authorize(MbedCloudClient::UpdateRequestDownload);
leothedragon 0:8f0bb79ddd48 52 break;
leothedragon 0:8f0bb79ddd48 53
leothedragon 0:8f0bb79ddd48 54 /* Cloud Client wishes to reboot and apply the new firmware.
leothedragon 0:8f0bb79ddd48 55
leothedragon 0:8f0bb79ddd48 56 The user application is supposed to save all current work before rebooting.
leothedragon 0:8f0bb79ddd48 57
leothedragon 0:8f0bb79ddd48 58 Note: the authorization call can be postponed and called later.
leothedragon 0:8f0bb79ddd48 59 This doesn't affect the performance of the Cloud Client.
leothedragon 0:8f0bb79ddd48 60 */
leothedragon 0:8f0bb79ddd48 61 case MbedCloudClient::UpdateRequestInstall:
leothedragon 0:8f0bb79ddd48 62 printf("Firmware install requested\r\n");
leothedragon 0:8f0bb79ddd48 63 printf("Authorization granted\r\n");
leothedragon 0:8f0bb79ddd48 64 _client->update_authorize(MbedCloudClient::UpdateRequestInstall);
leothedragon 0:8f0bb79ddd48 65 break;
leothedragon 0:8f0bb79ddd48 66
leothedragon 0:8f0bb79ddd48 67 default:
leothedragon 0:8f0bb79ddd48 68 printf("Error - unknown request\r\n");
leothedragon 0:8f0bb79ddd48 69 break;
leothedragon 0:8f0bb79ddd48 70 }
leothedragon 0:8f0bb79ddd48 71 }
leothedragon 0:8f0bb79ddd48 72 #endif
leothedragon 0:8f0bb79ddd48 73 #endif
leothedragon 0:8f0bb79ddd48 74
leothedragon 0:8f0bb79ddd48 75 void __attribute__((weak)) update_progress(uint32_t progress, uint32_t total)
leothedragon 0:8f0bb79ddd48 76 {
leothedragon 0:8f0bb79ddd48 77 uint8_t percent = progress * 100 / total;
leothedragon 0:8f0bb79ddd48 78
leothedragon 0:8f0bb79ddd48 79 /* only show progress bar if debug trace is disabled */
leothedragon 0:8f0bb79ddd48 80 #if (!defined(MBED_CONF_MBED_TRACE_ENABLE) || MBED_CONF_MBED_TRACE_ENABLE == 0) \
leothedragon 0:8f0bb79ddd48 81 && !ARM_UC_ALL_TRACE_ENABLE \
leothedragon 0:8f0bb79ddd48 82 && !ARM_UC_HUB_TRACE_ENABLE
leothedragon 0:8f0bb79ddd48 83
leothedragon 0:8f0bb79ddd48 84 printf("\rDownloading: [");
leothedragon 0:8f0bb79ddd48 85 for (uint8_t index = 0; index < 50; index++)
leothedragon 0:8f0bb79ddd48 86 {
leothedragon 0:8f0bb79ddd48 87 if (index < percent / 2)
leothedragon 0:8f0bb79ddd48 88 {
leothedragon 0:8f0bb79ddd48 89 printf("+");
leothedragon 0:8f0bb79ddd48 90 }
leothedragon 0:8f0bb79ddd48 91 else if (index == percent / 2)
leothedragon 0:8f0bb79ddd48 92 {
leothedragon 0:8f0bb79ddd48 93 static uint8_t old_max = 0;
leothedragon 0:8f0bb79ddd48 94 static uint8_t counter = 0;
leothedragon 0:8f0bb79ddd48 95
leothedragon 0:8f0bb79ddd48 96 if (index == old_max)
leothedragon 0:8f0bb79ddd48 97 {
leothedragon 0:8f0bb79ddd48 98 counter++;
leothedragon 0:8f0bb79ddd48 99 }
leothedragon 0:8f0bb79ddd48 100 else
leothedragon 0:8f0bb79ddd48 101 {
leothedragon 0:8f0bb79ddd48 102 old_max = index;
leothedragon 0:8f0bb79ddd48 103 counter = 0;
leothedragon 0:8f0bb79ddd48 104 }
leothedragon 0:8f0bb79ddd48 105
leothedragon 0:8f0bb79ddd48 106 switch (counter % 4)
leothedragon 0:8f0bb79ddd48 107 {
leothedragon 0:8f0bb79ddd48 108 case 0:
leothedragon 0:8f0bb79ddd48 109 printf("/");
leothedragon 0:8f0bb79ddd48 110 break;
leothedragon 0:8f0bb79ddd48 111 case 1:
leothedragon 0:8f0bb79ddd48 112 printf("-");
leothedragon 0:8f0bb79ddd48 113 break;
leothedragon 0:8f0bb79ddd48 114 case 2:
leothedragon 0:8f0bb79ddd48 115 printf("\\");
leothedragon 0:8f0bb79ddd48 116 break;
leothedragon 0:8f0bb79ddd48 117 case 3:
leothedragon 0:8f0bb79ddd48 118 default:
leothedragon 0:8f0bb79ddd48 119 printf("|");
leothedragon 0:8f0bb79ddd48 120 break;
leothedragon 0:8f0bb79ddd48 121 }
leothedragon 0:8f0bb79ddd48 122 }
leothedragon 0:8f0bb79ddd48 123 else
leothedragon 0:8f0bb79ddd48 124 {
leothedragon 0:8f0bb79ddd48 125 printf(" ");
leothedragon 0:8f0bb79ddd48 126 }
leothedragon 0:8f0bb79ddd48 127 }
leothedragon 0:8f0bb79ddd48 128 printf("] %d %%", percent);
leothedragon 0:8f0bb79ddd48 129 fflush(stdout);
leothedragon 0:8f0bb79ddd48 130 #else
leothedragon 0:8f0bb79ddd48 131 printf("Downloading: %d %%\r\n", percent);
leothedragon 0:8f0bb79ddd48 132 #endif
leothedragon 0:8f0bb79ddd48 133
leothedragon 0:8f0bb79ddd48 134 if (progress == total)
leothedragon 0:8f0bb79ddd48 135 {
leothedragon 0:8f0bb79ddd48 136 printf("\r\nDownload completed\r\n");
leothedragon 0:8f0bb79ddd48 137 }
leothedragon 0:8f0bb79ddd48 138 }
leothedragon 0:8f0bb79ddd48 139
leothedragon 0:8f0bb79ddd48 140 #endif // MBED_CLOUD_CLIENT_SUPPORT_UPDATE