Committer:
leothedragon
Date:
Sun Apr 18 15:20:23 2021 +0000
Revision:
0:25fa8795676b
DS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:25fa8795676b 1 // ----------------------------------------------------------------------------
leothedragon 0:25fa8795676b 2 // Copyright 2016-2018 ARM Ltd.
leothedragon 0:25fa8795676b 3 //
leothedragon 0:25fa8795676b 4 // SPDX-License-Identifier: Apache-2.0
leothedragon 0:25fa8795676b 5 //
leothedragon 0:25fa8795676b 6 // Licensed under the Apache License, Version 2.0 (the "License");
leothedragon 0:25fa8795676b 7 // you may not use this file except in compliance with the License.
leothedragon 0:25fa8795676b 8 // You may obtain a copy of the License at
leothedragon 0:25fa8795676b 9 //
leothedragon 0:25fa8795676b 10 // http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:25fa8795676b 11 //
leothedragon 0:25fa8795676b 12 // Unless required by applicable law or agreed to in writing, software
leothedragon 0:25fa8795676b 13 // distributed under the License is distributed on an "AS IS" BASIS,
leothedragon 0:25fa8795676b 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:25fa8795676b 15 // See the License for the specific language governing permissions and
leothedragon 0:25fa8795676b 16 // limitations under the License.
leothedragon 0:25fa8795676b 17 // ----------------------------------------------------------------------------
leothedragon 0:25fa8795676b 18
leothedragon 0:25fa8795676b 19 #include "update-helper/update-helper.h"
leothedragon 0:25fa8795676b 20
leothedragon 0:25fa8795676b 21 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
leothedragon 0:25fa8795676b 22
leothedragon 0:25fa8795676b 23 #include <stdio.h>
leothedragon 0:25fa8795676b 24 #include <stdint.h>
leothedragon 0:25fa8795676b 25
leothedragon 0:25fa8795676b 26 static MbedCloudClient* _client;
leothedragon 0:25fa8795676b 27
leothedragon 0:25fa8795676b 28 #ifdef ARM_UPDATE_CLIENT_VERSION_VALUE
leothedragon 0:25fa8795676b 29 #if ARM_UPDATE_CLIENT_VERSION_VALUE > 101000
leothedragon 0:25fa8795676b 30 void update_helper_set_cloud_client(MbedCloudClient* client)
leothedragon 0:25fa8795676b 31 {
leothedragon 0:25fa8795676b 32 _client = client;
leothedragon 0:25fa8795676b 33 }
leothedragon 0:25fa8795676b 34
leothedragon 0:25fa8795676b 35 void __attribute__((weak)) update_authorize(int32_t request)
leothedragon 0:25fa8795676b 36 {
leothedragon 0:25fa8795676b 37 switch (request)
leothedragon 0:25fa8795676b 38 {
leothedragon 0:25fa8795676b 39 /* Cloud Client wishes to download new firmware. This can have a negative
leothedragon 0:25fa8795676b 40 impact on the performance of the rest of the system.
leothedragon 0:25fa8795676b 41
leothedragon 0:25fa8795676b 42 The user application is supposed to pause performance sensitive tasks
leothedragon 0:25fa8795676b 43 before authorizing the download.
leothedragon 0:25fa8795676b 44
leothedragon 0:25fa8795676b 45 Note: the authorization call can be postponed and called later.
leothedragon 0:25fa8795676b 46 This doesn't affect the performance of the Cloud Client.
leothedragon 0:25fa8795676b 47 */
leothedragon 0:25fa8795676b 48 case MbedCloudClient::UpdateRequestDownload:
leothedragon 0:25fa8795676b 49 printf("Firmware download requested\r\n");
leothedragon 0:25fa8795676b 50 printf("Authorization granted\r\n");
leothedragon 0:25fa8795676b 51 _client->update_authorize(MbedCloudClient::UpdateRequestDownload);
leothedragon 0:25fa8795676b 52 break;
leothedragon 0:25fa8795676b 53
leothedragon 0:25fa8795676b 54 /* Cloud Client wishes to reboot and apply the new firmware.
leothedragon 0:25fa8795676b 55
leothedragon 0:25fa8795676b 56 The user application is supposed to save all current work before rebooting.
leothedragon 0:25fa8795676b 57
leothedragon 0:25fa8795676b 58 Note: the authorization call can be postponed and called later.
leothedragon 0:25fa8795676b 59 This doesn't affect the performance of the Cloud Client.
leothedragon 0:25fa8795676b 60 */
leothedragon 0:25fa8795676b 61 case MbedCloudClient::UpdateRequestInstall:
leothedragon 0:25fa8795676b 62 printf("Firmware install requested\r\n");
leothedragon 0:25fa8795676b 63 printf("Authorization granted\r\n");
leothedragon 0:25fa8795676b 64 _client->update_authorize(MbedCloudClient::UpdateRequestInstall);
leothedragon 0:25fa8795676b 65 break;
leothedragon 0:25fa8795676b 66
leothedragon 0:25fa8795676b 67 default:
leothedragon 0:25fa8795676b 68 printf("Error - unknown request\r\n");
leothedragon 0:25fa8795676b 69 break;
leothedragon 0:25fa8795676b 70 }
leothedragon 0:25fa8795676b 71 }
leothedragon 0:25fa8795676b 72 #endif
leothedragon 0:25fa8795676b 73 #endif
leothedragon 0:25fa8795676b 74
leothedragon 0:25fa8795676b 75 void __attribute__((weak)) update_progress(uint32_t progress, uint32_t total)
leothedragon 0:25fa8795676b 76 {
leothedragon 0:25fa8795676b 77 uint8_t percent = progress * 100 / total;
leothedragon 0:25fa8795676b 78
leothedragon 0:25fa8795676b 79 /* only show progress bar if debug trace is disabled */
leothedragon 0:25fa8795676b 80 #if (!defined(MBED_CONF_MBED_TRACE_ENABLE) || MBED_CONF_MBED_TRACE_ENABLE == 0) \
leothedragon 0:25fa8795676b 81 && !ARM_UC_ALL_TRACE_ENABLE \
leothedragon 0:25fa8795676b 82 && !ARM_UC_HUB_TRACE_ENABLE
leothedragon 0:25fa8795676b 83
leothedragon 0:25fa8795676b 84 printf("\rDownloading: [");
leothedragon 0:25fa8795676b 85 for (uint8_t index = 0; index < 50; index++)
leothedragon 0:25fa8795676b 86 {
leothedragon 0:25fa8795676b 87 if (index < percent / 2)
leothedragon 0:25fa8795676b 88 {
leothedragon 0:25fa8795676b 89 printf("+");
leothedragon 0:25fa8795676b 90 }
leothedragon 0:25fa8795676b 91 else if (index == percent / 2)
leothedragon 0:25fa8795676b 92 {
leothedragon 0:25fa8795676b 93 static uint8_t old_max = 0;
leothedragon 0:25fa8795676b 94 static uint8_t counter = 0;
leothedragon 0:25fa8795676b 95
leothedragon 0:25fa8795676b 96 if (index == old_max)
leothedragon 0:25fa8795676b 97 {
leothedragon 0:25fa8795676b 98 counter++;
leothedragon 0:25fa8795676b 99 }
leothedragon 0:25fa8795676b 100 else
leothedragon 0:25fa8795676b 101 {
leothedragon 0:25fa8795676b 102 old_max = index;
leothedragon 0:25fa8795676b 103 counter = 0;
leothedragon 0:25fa8795676b 104 }
leothedragon 0:25fa8795676b 105
leothedragon 0:25fa8795676b 106 switch (counter % 4)
leothedragon 0:25fa8795676b 107 {
leothedragon 0:25fa8795676b 108 case 0:
leothedragon 0:25fa8795676b 109 printf("/");
leothedragon 0:25fa8795676b 110 break;
leothedragon 0:25fa8795676b 111 case 1:
leothedragon 0:25fa8795676b 112 printf("-");
leothedragon 0:25fa8795676b 113 break;
leothedragon 0:25fa8795676b 114 case 2:
leothedragon 0:25fa8795676b 115 printf("\\");
leothedragon 0:25fa8795676b 116 break;
leothedragon 0:25fa8795676b 117 case 3:
leothedragon 0:25fa8795676b 118 default:
leothedragon 0:25fa8795676b 119 printf("|");
leothedragon 0:25fa8795676b 120 break;
leothedragon 0:25fa8795676b 121 }
leothedragon 0:25fa8795676b 122 }
leothedragon 0:25fa8795676b 123 else
leothedragon 0:25fa8795676b 124 {
leothedragon 0:25fa8795676b 125 printf(" ");
leothedragon 0:25fa8795676b 126 }
leothedragon 0:25fa8795676b 127 }
leothedragon 0:25fa8795676b 128 printf("] %d %%", percent);
leothedragon 0:25fa8795676b 129 fflush(stdout);
leothedragon 0:25fa8795676b 130 #else
leothedragon 0:25fa8795676b 131 printf("Downloading: %d %%\r\n", percent);
leothedragon 0:25fa8795676b 132 #endif
leothedragon 0:25fa8795676b 133
leothedragon 0:25fa8795676b 134 if (progress == total)
leothedragon 0:25fa8795676b 135 {
leothedragon 0:25fa8795676b 136 printf("\r\nDownload completed\r\n");
leothedragon 0:25fa8795676b 137 }
leothedragon 0:25fa8795676b 138 }
leothedragon 0:25fa8795676b 139
leothedragon 0:25fa8795676b 140 #endif // MBED_CLOUD_CLIENT_SUPPORT_UPDATE