Software Update via Ethernet - the mbed application can pull down an updated application binary from a web server and activate that binary. This library works only with the LPC1768, as it relies on the magic-chip boot-loader mechanism.

Dependents:   WattEye X10Svr PUB_SWUpdate

Success!! With this library, a network connection, and a web server hosting a new binary image, you can update the mbed firmware over the air (FOTA) - well, at least via Ethernet so far.

As of March 2015, it has been tested with the following mbed official libraries:

And a custom derivation:

  • HTTPClient v33, v32, which includes a custom HTTPFile.

Part of the update process involves checking the integrity of the downloaded binary file, for both a checksum and the program (file) size. To create this additional information, a small perl script is used (the important part is only 20 lines of code). See the documentation in the header file.

After the new binary is successfully downloaded, the checksum and the size are evaluated and if correct, then the old binary file is removed (this is the only way to cause the new binary to activate).

The mbed can then be automatically reset to activate the new image, or this may be deferred in case there is some other process necessary for an orderly restart.

Details are in the SWUpdate header file, and PUB_SWUpdate is a publicly accessible demonstration program for this library.

Committer:
WiredHome
Date:
Mon Apr 21 00:20:05 2014 +0000
Revision:
0:e221363f7942
Child:
1:208de08b1a19
Software Update - not yet reliable

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:e221363f7942 1
WiredHome 0:e221363f7942 2 // Software Update via Ethernet from forum -
WiredHome 0:e221363f7942 3 // http://mbed.org/forum/mbed/topic/1183/
WiredHome 0:e221363f7942 4 #include "mbed.h"
WiredHome 0:e221363f7942 5 #include "SWUpdate.h"
WiredHome 0:e221363f7942 6 #include "HTTPClient.h"
WiredHome 0:e221363f7942 7 #include "HTTPText.h"
WiredHome 0:e221363f7942 8 #include "HTTPFile.h"
WiredHome 0:e221363f7942 9 #include <stdio.h>
WiredHome 0:e221363f7942 10
WiredHome 0:e221363f7942 11 extern "C" void mbed_reset();
WiredHome 0:e221363f7942 12
WiredHome 0:e221363f7942 13 #define DEBUG "SWup"
WiredHome 0:e221363f7942 14 #include <cstdio>
WiredHome 0:e221363f7942 15 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
WiredHome 0:e221363f7942 16 #define DBG(x, ...) std::printf("[DBG %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 0:e221363f7942 17 #define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 0:e221363f7942 18 #define ERR(x, ...) std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 0:e221363f7942 19 #define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
WiredHome 0:e221363f7942 20 #else
WiredHome 0:e221363f7942 21 #define DBG(x, ...)
WiredHome 0:e221363f7942 22 #define WARN(x, ...)
WiredHome 0:e221363f7942 23 #define ERR(x, ...)
WiredHome 0:e221363f7942 24 #define INFO(x, ...)
WiredHome 0:e221363f7942 25 #endif
WiredHome 0:e221363f7942 26
WiredHome 0:e221363f7942 27 bool SoftwareUpdate(const char *url, const char * name, Reboot_T reboot) {
WiredHome 0:e221363f7942 28 HTTPClient http;
WiredHome 0:e221363f7942 29 //http.setTimeout( 15000 );
WiredHome 0:e221363f7942 30 char fqurl[150]; // fully qualified url
WiredHome 0:e221363f7942 31 char verfn[32]; // local version file
WiredHome 0:e221363f7942 32 char fwfn[32];
WiredHome 0:e221363f7942 33 bool result = false; // many things can go wrong, assume failure
WiredHome 0:e221363f7942 34 //char *verfn = "/local/VERSION.TXT";
WiredHome 0:e221363f7942 35 char buf[50];
WiredHome 0:e221363f7942 36
WiredHome 0:e221363f7942 37 INFO("SoftwareUpdate(%s,%s)", url, name);
WiredHome 0:e221363f7942 38 snprintf(verfn, 32, "/local/%s.ver", name);
WiredHome 0:e221363f7942 39
WiredHome 0:e221363f7942 40 /* Read installed version string */
WiredHome 0:e221363f7942 41 int inst_ver = -1;
WiredHome 0:e221363f7942 42 FILE *fv = fopen(verfn, "r");
WiredHome 0:e221363f7942 43 if (fv) {
WiredHome 0:e221363f7942 44 fscanf(fv, "%d", &inst_ver);
WiredHome 0:e221363f7942 45 fclose(fv);
WiredHome 0:e221363f7942 46 }
WiredHome 0:e221363f7942 47 INFO(" Installed version: %d", inst_ver);
WiredHome 0:e221363f7942 48
WiredHome 0:e221363f7942 49 /* Download latest version string */
WiredHome 0:e221363f7942 50 HTTPText server_ver("test message");
WiredHome 0:e221363f7942 51 snprintf(fqurl, 150, "%s/%s.txt", url, name);
WiredHome 0:e221363f7942 52 HTTPResult r = http.get(fqurl, buf, sizeof(buf));
WiredHome 0:e221363f7942 53 if (r == HTTP_OK) {
WiredHome 0:e221363f7942 54 int latest_ver = -1;
WiredHome 0:e221363f7942 55 INFO(" read {%s}", buf);
WiredHome 0:e221363f7942 56 sscanf(buf, "%d", &latest_ver);
WiredHome 0:e221363f7942 57 INFO(" web version: %d", latest_ver);
WiredHome 0:e221363f7942 58 if (inst_ver != latest_ver) {
WiredHome 0:e221363f7942 59 INFO(" Downloading new firmware...");
WiredHome 0:e221363f7942 60 sprintf(fwfn, "/local/%s%d.BIN", name, latest_ver);
WiredHome 0:e221363f7942 61 snprintf(fqurl, 150, "%s/%s.bin", url, name);
WiredHome 0:e221363f7942 62
WiredHome 0:e221363f7942 63 HTTPFile latest(fwfn);
WiredHome 0:e221363f7942 64 r = http.get(fqurl, &latest);
WiredHome 0:e221363f7942 65 if (r == HTTP_OK) {
WiredHome 0:e221363f7942 66 sprintf(fwfn, "/local/%s%d.BIN", name, inst_ver);
WiredHome 0:e221363f7942 67 INFO(" Firmware downloaded, removing old version (%s).", fwfn);
WiredHome 0:e221363f7942 68 // if (remove(fwfn)) {
WiredHome 0:e221363f7942 69 ERR(" *** Failed to remove old version. ***");
WiredHome 0:e221363f7942 70 // }
WiredHome 0:e221363f7942 71 INFO("Updating stored version number.");
WiredHome 0:e221363f7942 72 fv = fopen(verfn, "w");
WiredHome 0:e221363f7942 73 if (fv) {
WiredHome 0:e221363f7942 74 int fr = fputs(buf, fv);
WiredHome 0:e221363f7942 75 if (fr < 0) {
WiredHome 0:e221363f7942 76 ERR("Failed (%d) to update stored version number.", fr);
WiredHome 0:e221363f7942 77 fclose( fv );
WiredHome 0:e221363f7942 78 } else {
WiredHome 0:e221363f7942 79 fclose( fv );
WiredHome 0:e221363f7942 80 if (reboot == AUTO_REBOOT) {
WiredHome 0:e221363f7942 81 WARN("Resetting...\n");
WiredHome 0:e221363f7942 82 wait_ms(200);
WiredHome 0:e221363f7942 83 mbed_reset();
WiredHome 0:e221363f7942 84 }
WiredHome 0:e221363f7942 85 result = true;
WiredHome 0:e221363f7942 86 }
WiredHome 0:e221363f7942 87 }
WiredHome 0:e221363f7942 88 } else {
WiredHome 0:e221363f7942 89 WARN("Couldn't download lastest firmware.\n");
WiredHome 0:e221363f7942 90 }
WiredHome 0:e221363f7942 91 }
WiredHome 0:e221363f7942 92 } else {
WiredHome 0:e221363f7942 93 WARN("Couldn't check latest firmware version.\n");
WiredHome 0:e221363f7942 94 }
WiredHome 0:e221363f7942 95 return result;
WiredHome 0:e221363f7942 96 }