Simple interface for Mbed Cloud Client

Dependents:  

Committer:
MACRUM
Date:
Mon Jul 02 06:30:39 2018 +0000
Revision:
0:276e7a263c35
Initial commit

Who changed what in which revision?

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