David Smart / Mbed 2 deprecated PUB_SWUpdate

Dependencies:   mbed HTTPClient SWUpdate mbed-rtos Watchdog EthernetInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //
00002 // Software Update Test Program
00003 // 
00004 // Mostly minimized to what is needed to demonstrate the SWUpdate library:
00005 // * EthernetInterface
00006 // * HTTPClient
00007 // * SWUpdate
00008 //
00009 // Additional items that I do not do without (but are not absolutely necessary):
00010 // * EthernetStatus - to know the state of the connection.
00011 // * Watchdog - because they are always a good idea.
00012 // * mbed-rtos - required by the EthernetInterface (I think)
00013 //
00014 // 
00015 // 
00016 //
00017 #include "mbed.h"               // mbed: ver 95, rtos: 64
00018 #include "EthernetInterface.h"  // ver 47
00019 #include "RawSerial.h"          // for console i/o
00020 
00021 // My custom libraries
00022 #include "HTTPClient.h"         // ver 33
00023 #include "SWUpdate.h"           // ver 20
00024 #include "Watchdog.h"           // ver 4
00025 #include "EthStatus.h"          // connection state and speed...
00026 
00027 // ===================================================
00028 // SWUpdate
00029 // ===================================================
00030 // Server url to where the Software will be found.
00031 const char *url = "http://192.168.1.201/mbed";
00032 
00033 // Base filename for the software (e.g. if you save it as "abcdefg.bin", use "abcdefg" here)
00034 // Remember: some servers are case-sensitive!
00035 const char *name = "SWUp";
00036 // ===================================================
00037 
00038 
00039 
00040 // Banner that is sent to the console on reboot - shows build date/time.
00041 const char * PROG_INFO = "SW Update: " __DATE__ ", " __TIME__;
00042 
00043 EthernetInterface eth;
00044 Watchdog wd;
00045 
00046 RawSerial pc(USBTX, USBRX);
00047 LocalFileSystem local("local");
00048 
00049 HTTPClient http;
00050 
00051 extern "C" void mbed_reset();
00052 
00053 
00054 void SoftwareUpdateCheck(void)
00055 {
00056     pc.printf("SoftwareUpdateCheck (%s) (%s)\r\n", url, name);
00057     SWUpdate_T su = SoftwareUpdate(url, name, DEFER_REBOOT);
00058     if (SWUP_OK == su) {
00059         pc.printf("  Update installed, rebooting ...\r\n");
00060         Thread::wait(3000);
00061         mbed_reset();
00062     } else if (SWUP_SAME_VER == su) {
00063         pc.printf("  no update available.\r\n");
00064     } else {
00065         pc.printf("  update failed %04X - %s\r\n", (int)su, SoftwareUpdateGetHTTPErrorMsg(su));
00066     }
00067 }
00068 
00069 
00070 // I like to see what address it gets, and when it loses the connection.
00071 void ShowIPAddress(bool show)
00072 {
00073     char buf[16];
00074     
00075     if (show)
00076         sprintf(buf, "%15s", eth.getIPAddress());
00077     else
00078         sprintf(buf, "%15s", "---.---.---.---");
00079     pc.printf("Ethernet connected as %s\r\n", buf);
00080 }
00081 
00082 
00083 void ShowMenu() {
00084     pc.printf("Menu:\r\n");
00085     pc.printf("  r - reset\r\n");
00086     pc.printf("  s - software update check\r\n");
00087     pc.printf("\r\n");
00088 }
00089 
00090 // Only 2 items in this menu - check for software updates and reset!
00091 void CheckConsoleInput(void)
00092 {
00093     if (pc.readable()) {
00094         int c = pc.getc();
00095         switch (c) {
00096             case 'r':
00097                 mbed_reset();
00098                 break;
00099             case 's':
00100                 SoftwareUpdateCheck();
00101                 break;
00102             default:
00103                 pc.printf("unknown command '%c'\r\n", c);
00104                 ShowMenu();
00105                 break;
00106         }
00107     }
00108 }
00109 
00110 // Friendlier interface to find out if it is currently connected.
00111 bool NetworkIsConnected(void)
00112 {
00113     return get_link_status();
00114 }
00115 
00116 
00117 int main()
00118 {
00119     pc.baud(460800);        // fast, because I like a snappy terminal!
00120     pc.printf("\r\n%s\r\n", PROG_INFO);
00121 
00122     if (wd.WatchdogCausedReset()) {
00123         pc.printf("**** Watchdog Event caused reset ****\r\n");
00124     }
00125     wd.Configure(30.0);   // nothing should take more than 30 s (we hope).
00126     
00127     pc.printf("***\r\n");
00128     pc.printf("Initializing network interface...\r\n");
00129     if (0 == eth.init()) {  //Use DHCP
00130         do {
00131             pc.printf("Connecting to network...\r\n");
00132             if (0 == eth.connect()) {
00133                 ShowIPAddress(true);
00134                 int speed = get_connection_speed();
00135                 pc.printf("Connected at %d Mb/s\r\n", speed);
00136                 ShowMenu();
00137                 while (NetworkIsConnected()) {
00138                     Thread::wait(500);
00139                     CheckConsoleInput();
00140                     wd.Service();
00141                 }
00142                 pc.printf("lost connection.\r\n");
00143                 ShowIPAddress(false);
00144                 eth.disconnect();
00145             }
00146             else {
00147                 pc.printf("  ... failed to connect.\r\n");
00148             }
00149             CheckConsoleInput();
00150         }
00151         while (1);
00152     }
00153     else {
00154         pc.printf("  ... failed to initialize, rebooting...\r\n");
00155         mbed_reset();
00156     }
00157 }