Simple interface for Mbed Cloud Client

Dependents:  

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers update_ui_example.cpp Source File

update_ui_example.cpp

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 "update_ui_example.h"
00020 
00021 #ifdef MBED_CLOUD_CLIENT_SUPPORT_UPDATE
00022 
00023 #include <stdio.h>
00024 #include <stdint.h>
00025 
00026 static MbedCloudClient* _client;
00027 
00028 #ifdef ARM_UPDATE_CLIENT_VERSION_VALUE
00029 #if ARM_UPDATE_CLIENT_VERSION_VALUE > 101000
00030 void update_ui_set_cloud_client(MbedCloudClient* client)
00031 {
00032     _client = client;
00033 }
00034 
00035 void update_authorize(int32_t request)
00036 {
00037     switch (request)
00038     {
00039         /* Cloud Client wishes to download new firmware. This can have a negative
00040            impact on the performance of the rest of the system.
00041 
00042            The user application is supposed to pause performance sensitive tasks
00043            before authorizing the download.
00044 
00045            Note: the authorization call can be postponed and called later.
00046            This doesn't affect the performance of the Cloud Client.
00047         */
00048         case MbedCloudClient::UpdateRequestDownload:
00049             printf("Firmware download requested\r\n");
00050             printf("Authorization granted\r\n");
00051             _client->update_authorize(MbedCloudClient::UpdateRequestDownload);
00052 
00053             break;
00054 
00055         /* Cloud Client wishes to reboot and apply the new firmware.
00056 
00057            The user application is supposed to save all current work before rebooting.
00058 
00059            Note: the authorization call can be postponed and called later.
00060            This doesn't affect the performance of the Cloud Client.
00061         */
00062         case MbedCloudClient::UpdateRequestInstall:
00063             printf("Firmware install requested\r\n");
00064             printf("Authorization granted\r\n");
00065             _client->update_authorize(MbedCloudClient::UpdateRequestInstall);
00066             break;
00067 
00068         default:
00069             printf("Error - unknown request\r\n");
00070             break;
00071     }
00072 }
00073 #endif
00074 #endif
00075 
00076 void update_progress(uint32_t progress, uint32_t total)
00077 {
00078     uint8_t percent = (uint8_t)((uint64_t)progress * 100 / total);
00079 
00080 /* only show progress bar if debug trace is disabled */
00081 #if !defined(MBED_CONF_MBED_TRACE_ENABLE) \
00082     && !ARM_UC_ALL_TRACE_ENABLE \
00083     && !ARM_UC_HUB_TRACE_ENABLE
00084 
00085     printf("\rDownloading: [");
00086     for (uint8_t index = 0; index < 50; index++)
00087     {
00088         if (index < percent / 2)
00089         {
00090             printf("+");
00091         }
00092         else if (index == percent / 2)
00093         {
00094             static uint8_t old_max = 0;
00095             static uint8_t counter = 0;
00096 
00097             if (index == old_max)
00098             {
00099                 counter++;
00100             }
00101             else
00102             {
00103                 old_max = index;
00104                 counter = 0;
00105             }
00106 
00107             switch (counter % 4)
00108             {
00109                 case 0:
00110                     printf("/");
00111                     break;
00112                 case 1:
00113                     printf("-");
00114                     break;
00115                 case 2:
00116                     printf("\\");
00117                     break;
00118                 case 3:
00119                 default:
00120                     printf("|");
00121                     break;
00122             }
00123         }
00124         else
00125         {
00126             printf(" ");
00127         }
00128     }
00129     printf("] %d %%", percent);
00130     fflush(stdout);
00131 #else
00132     printf("Downloading: %d %%\r\n", percent);
00133 #endif
00134 
00135     if (progress == total)
00136     {
00137         printf("\r\nDownload completed\r\n");
00138     }
00139 }
00140 
00141 #endif // MBED_CLOUD_CLIENT_SUPPORT_UPDATE