The FirmwareUpdater is a mbed firmware update library with HTTP server on cloud.
Dependents: FirmwareUpdater_TestProgram geigercounter04 firm LPC1768_up_frim
Diff: FirmwareUpdater.cpp
- Revision:
- 3:851bd91fa0ae
- Parent:
- 2:a9a32355af69
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.