The FirmwareUpdater is a mbed firmware update library with HTTP server on cloud.
Dependents: FirmwareUpdater_TestProgram geigercounter04 firm LPC1768_up_frim
Revision 3:851bd91fa0ae, committed 2010-11-05
- Comitter:
- shintamainjp
- Date:
- Fri Nov 05 12:16:17 2010 +0000
- Parent:
- 2:a9a32355af69
- Commit message:
- Specification changed. The maximum firmware name length changed to 8 from 4.
Changed in this revision
FirmwareUpdater.cpp | Show annotated file Show diff for this revision Revisions of this file |
FirmwareUpdater.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r a9a32355af69 -r 851bd91fa0ae FirmwareUpdater.cpp --- a/FirmwareUpdater.cpp Wed Nov 03 22:06:30 2010 +0000 +++ b/FirmwareUpdater.cpp Fri Nov 05 12:16:17 2010 +0000 @@ -50,10 +50,9 @@ /* * A file name on the mbed local file system should keep '8 + 3' types of name. */ - const int PREFIX_MAXLEN = 4; - if (name.length() > PREFIX_MAXLEN) { - LOG("ERR : Invalid firmware name '%s' found. The maximum length is PREFIX_MAXLEN.\n", name.c_str()); - error("ERR : Invalid firmware name '%s' found. The maximum length is PREFIX_MAXLEN.\n", name.c_str()); + if (MAXNAMELEN < name.length()) { + LOG("ERR : Invalid firmware name '%s' found. The maximum length is %d.\n", name.c_str(), MAXNAMELEN); + error("ERR : Invalid firmware name '%s' found. The maximum length is %d.\n", name.c_str(), MAXNAMELEN); } } @@ -172,9 +171,9 @@ /* * Copy it. */ - char nbuf[32]; - snprintf(nbuf, sizeof(nbuf) - 1, "-%d", ver_new); - std::string file_bin = "/local/" + name + std::string(nbuf) + EXT_BIN; + char nn[32]; + createNewBinName(ver_new, nn, sizeof(nn)); + std::string file_bin = "/local/" + std::string(nn) + EXT_BIN; if (copy(file_txttmp, file_txt) != 0) { return -6; } @@ -214,7 +213,7 @@ LOG("ERR : Fetch '%s' to '%s'.\n", src_url.c_str(), local_file.c_str()); return -1; } - LOG("INFO: Fetch '%s' to '%s'.\n", src_url.c_str(), local_file.c_str()); + LOG("INFO: Fetched '%s' to '%s'.\n", src_url.c_str(), local_file.c_str()); return 0; } @@ -297,6 +296,31 @@ } /** + * Create a new binary file name. + * + * @param ver Version. + * @param buf A pointer to a buffer. + * @param siz A size of the buffer. + * + * @return Return 0 if it succeed. + */ +int FirmwareUpdater::createNewBinName(const int ver, char *buf, size_t siz) { + if (siz <= name.length()) { + return -1; + } + snprintf(buf, siz - 1, "%s", name.c_str()); + char nb[32]; + snprintf(nb, sizeof(nb) - 1, "-%d", ver); + if (strlen(buf) + strlen(nb) <= MAXNAMELEN) { + strcat(buf, nb); + return 0; + } else { + strcpy(buf + (MAXNAMELEN - strlen(nb)), nb); + return 0; + } +} + +/** * Read a version from a file. * * @param filename file name.
diff -r a9a32355af69 -r 851bd91fa0ae FirmwareUpdater.h --- a/FirmwareUpdater.h Wed Nov 03 22:06:30 2010 +0000 +++ b/FirmwareUpdater.h Fri Nov 05 12:16:17 2010 +0000 @@ -39,11 +39,11 @@ * #include "EthernetNetIf.h" * * EthernetNetIf eth; - * FirmwareUpdater fwup("http://mbed.org/media/uploads/shintamainjp/", "firmware", true); + * FirmwareUpdater fwup("http://mbed.org/media/uploads/shintamainjp/", "firm", true); * * // There are 2 files for the firmware. - * // 1. firmware.txt : firmware version file. - * // 2. firmware.bin : firmware binary file. + * // 1. firm.txt : firmware version file. + * // 2. firm.bin : firmware binary file. * * int main() { * eth.setup(); @@ -109,7 +109,9 @@ * Reset system. */ void reset(); + private: + static const int MAXNAMELEN = 8; static const std::string EXT_BIN; static const std::string EXT_BINTMP; static const std::string EXT_TXT; @@ -151,6 +153,17 @@ * Cleanup all bin files. */ int cleanupAllBinFiles(void); + + /** + * Create a new binary file name. + * + * @param ver Version. + * @param buf A pointer to a buffer. + * @param siz A size of the buffer. + * + * @return Return 0 if it succeed. + */ + int createNewBinName(const int ver, char *buf, size_t siz); /** * Read a version from a file.