eichi kowata / Mbed 2 deprecated geiger

Dependencies:   EthernetNetIf NTPClient_NetServices mbed ConfigFile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FirmwareUpdater.h Source File

FirmwareUpdater.h

00001 /*******************************************************************************
00002 modify 
00003  2011/08
00004         - Use commonClient.
00005  2011/07
00006         - Add MD5 checksum.
00007 
00008 *******************************************************************************/
00009 
00010 
00011 /**
00012  * =============================================================================
00013  * Firmware updater (Version 0.0.2)
00014  * =============================================================================
00015  * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
00016  *
00017  * Permission is hereby granted, free of charge, to any person obtaining a copy
00018  * of this software and associated documentation files (the "Software"), to deal
00019  * in the Software without restriction, including without limitation the rights
00020  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00021  * copies of the Software, and to permit persons to whom the Software is
00022  * furnished to do so, subject to the following conditions:
00023  *
00024  * The above copyright notice and this permission notice shall be included in
00025  * all copies or substantial portions of the Software.
00026  *
00027  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00028  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00030  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00031  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00032  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00033  * THE SOFTWARE.
00034  * =============================================================================
00035  */
00036 
00037 #ifndef FIRMWARE_UPDATER_H
00038 #define FIRMWARE_UPDATER_H
00039 
00040 #include "mbed.h"
00041 #include "HTTPClient.h"
00042 #include "LocalFileSystem.h"
00043 #include <string>
00044 
00045 /**
00046  * @code
00047  * #include "mbed.h"
00048  * #include "FirmwareUpdater.h"
00049  * #include "EthernetNetIf.h"
00050  *
00051  * EthernetNetIf eth;
00052  * FirmwareUpdater fwup("http://mbed.org/media/uploads/shintamainjp/", "firm", true);
00053  *
00054  * // There are 2 files for the firmware.
00055  * //  1. firm.txt : firmware version file.
00056  * //  2. firm.bin : firmware binary file.
00057  *
00058  * int main() {
00059  *     eth.setup();
00060  *     if (fwup.exist() == 0) {
00061  *         printf("Found a new firmware.\n");
00062  *         if (fwup.execute() == 0) {
00063  *             printf("Update succeed.\n");
00064  *             printf("Resetting this system...\n\n\n\n\n");
00065  *             fwup.reset();
00066  *         } else {
00067  *             printf("Update failed!\n");
00068  *         }
00069  *     }
00070  * }
00071  * @endcode
00072  */
00073 class FirmwareUpdater  {
00074 public:
00075     /**
00076      * Create.
00077      *
00078      * @param url URL for firmware. Do not include a target file name.
00079      * @param name An application name. Do not include a extention.
00080      * @param log True if logging.
00081      */
00082 //    FirmwareUpdater(std::string url, std::string name, bool log = false);
00083     FirmwareUpdater(std::string url, std::string name, HTTPClient *pclient, bool log = false);  //  2011/08
00084 
00085     /**
00086      * Dispose.
00087      */
00088     ~FirmwareUpdater();
00089 
00090     /**
00091      * Get a URL.
00092      *
00093      * @return URL.
00094      */
00095     const std::string getURL() const;
00096 
00097     /**
00098      * Get a name.
00099      *
00100      * @return name.
00101      */
00102     const std::string getName() const;
00103 
00104     /**
00105      * Checking a new firmware.
00106      * Compare versions of the software between local storage on mbed and on webserver.
00107      *
00108      * @return Return 0 if a new firmware exists.
00109      */
00110     int exist();
00111 
00112     /**
00113      * Execute update.
00114      *
00115      * @return Return 0 if it succeed.
00116      */
00117     int execute();
00118 
00119     /**
00120      * Reset system.
00121      */
00122     void reset();
00123 
00124 private:
00125     static const int MAXNAMELEN = 8;
00126     static const std::string EXT_BIN;
00127     static const std::string EXT_BINTMP;
00128     static const std::string EXT_TXT;
00129     static const std::string EXT_TXTTMP;
00130     static const std::string EXT_MD5;       //  2011/07
00131     static const std::string EXT_MD5TMP;    //  2011/07
00132     const std::string url;
00133     const std::string name;
00134     const bool log;
00135     LocalFileSystem local;
00136     HTTPClient *client;
00137 
00138     /**
00139      * Fetch a file.
00140      *
00141      * @param src_url URL of a source file.
00142      * @param local_file Local file name.
00143      *
00144      * @return Return 0 if it succeed.
00145      */
00146     int fetch(std::string src_url, std::string local_file);
00147 
00148     /**
00149      * Copy a file.
00150      *
00151      * @param local_file1 Source file.
00152      * @param local_file2 Destination file.
00153      *
00154      * @return Return 0 if it succeed.
00155      */
00156     int copy(std::string local_file1, std::string local_file2);
00157 
00158     /**
00159      * Output a message to a log file.
00160      *
00161      * @param format ...
00162      */
00163     void LOG(const char* format, ...);
00164 
00165     /**
00166      * Cleanup all bin files.
00167      */
00168     int cleanupAllBinFiles(void);
00169     
00170     /**
00171      * Cleanup all bbk(binary backup) files.
00172      *   2011/07
00173      */
00174     int cleanupAllBbkFiles(void);
00175 
00176     /**
00177      * Backup BIN files.
00178      *   2011/07
00179      */
00180     int BackupBinFiles(void);
00181 
00182     /**
00183      * Create a new binary file name.
00184      *
00185      * @param ver Version.
00186      * @param buf A pointer to a buffer.
00187      * @param siz A size of the buffer.
00188      *
00189      * @return Return 0 if it succeed.
00190      */
00191     int createNewBinName(const int ver, char *buf, size_t siz);
00192 
00193     /**
00194      * Read a version from a file.
00195      *
00196      * @param filename file name.
00197      * @return A version.
00198      */
00199     int readVersionFromFile(const char *filename);
00200 
00201     /**
00202      * Read a version from a URL.
00203      *
00204      * @param url URL.
00205      * @return A version.
00206      */
00207     int readVersionFromURL(const char *url);
00208     
00209     /**
00210      * Read a MD5 from a file.
00211      *  2011/07
00212      * @param filename file name.
00213      * @param md5str MD5 string.
00214      * @return Error code.
00215      */
00216     int readMd5FromFile(const char *filename, char *md5str);
00217 };
00218 
00219 #endif