eichi kowata / Mbed 2 deprecated geiger

Dependencies:   EthernetNetIf NTPClient_NetServices mbed ConfigFile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers appconf.cpp Source File

appconf.cpp

00001 /*************************************************************
00002 
00003  History
00004  2011/08/23 - Add nsvlong0,cpmlong0.
00005  2011/08/03 - variable stream number.
00006 
00007 
00008 *************************************************************/
00009 /**
00010  * =============================================================================
00011  * Application configuration for 'Expansion Board One' example no.2
00012  * http://mbed.org/users/shintamainjp/notebook/starboard_expbrd-one_ex2_en/
00013  * =============================================================================
00014  * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
00015  *
00016  * Permission is hereby granted, free of charge, to any person obtaining a copy
00017  * of this software and associated documentation files (the "Software"), to deal
00018  * in the Software without restriction, including without limitation the rights
00019  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00020  * copies of the Software, and to permit persons to whom the Software is
00021  * furnished to do so, subject to the following conditions:
00022  *
00023  * The above copyright notice and this permission notice shall be included in
00024  * all copies or substantial portions of the Software.
00025  *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00029  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00030  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00031  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00032  * THE SOFTWARE.
00033  * =============================================================================
00034  */
00035 
00036 #include "appconf.h"
00037 #include "ConfigFile.h"
00038 
00039 #define KEY_PACHUBE_APIKEY   "PACHUBE_APIKEY"
00040 #define KEY_PACHUBE_FEEDID   "PACHUBE_FEEDID"
00041 #define KEY_STREAM_NSV0      "nsv0"      //  2011/08/03
00042 #define KEY_STREAM_CPM0      "cpm0"      //  2011/08/03
00043 #ifdef DUALGM
00044 #define KEY_STREAM_NSV1      "nsv1"      //  2011/08/03
00045 #define KEY_STREAM_CPM1      "cpm1"      //  2011/08/03
00046 #endif
00047 #define KEY_STREAM_NSVLONG0   "nsvlong0"   //  2011/08/23
00048 #define KEY_STREAM_CPMLONG0   "cpmlong0"   //  2011/08/23
00049 
00050 //#define KEY_PACHUBE_INTERVAL "PACHUBE_INTERVAL"
00051 
00052 /**
00053  * Initialize configuration.
00054  *
00055  * @param p A pointer to a application config.
00056  */
00057 void appconf_init(appconf_t *p) {
00058     memset(p->apikey, 0, sizeof(p->apikey));
00059     memset(p->feedid, 0, sizeof(p->feedid));
00060     p->stream_nsv0 = -1; //default value 2011/08/03
00061     p->stream_cpm0 = -1; //default value 2011/08/03
00062 #ifdef DUALGM
00063     p->stream_nsv1 = 2; //default value 2011/08/03
00064     p->stream_cpm1 = 3; //default value 2011/08/03
00065 #endif
00066     p->stream_nsvlong0 = 0; //default value 2011/08/23
00067     p->stream_cpmlong0 = 1; //default value 2011/08/23
00068 
00069 //    p->interval = 0;
00070 }
00071 
00072 /**
00073  * Read configuration.
00074  *
00075  * @param filename Filename.
00076  * @param p A pointer to a application config.
00077  * @return Return zero if it succeed.
00078  */
00079 int appconf_read(char *filename, appconf_t *p) {
00080     ConfigFile cf;
00081     if (!cf.read(filename)) {
00082         return -1;
00083     }
00084     if (!cf.getValue(KEY_PACHUBE_APIKEY, p->apikey, sizeof(p->apikey))) {
00085         return -2;
00086     }    
00087     if (!cf.getValue(KEY_PACHUBE_FEEDID, p->feedid, sizeof(p->feedid))) {
00088         return -3;
00089     }
00090 /*
00091     char buf[64];
00092     if (!cf.getValue(KEY_PACHUBE_INTERVAL, buf, sizeof(buf))) {
00093         return -4;
00094     } else {
00095         if (sscanf(buf, "%d", &(p->interval)) != 1) {
00096             return -5;
00097         }
00098     }
00099 */
00100 // 2011/08/03 begin
00101     char buf[64];
00102     if(cf.getValue(KEY_STREAM_NSV0, buf, sizeof(buf))) {
00103       if(sscanf(buf, "%d", &(p->stream_nsv0)) !=1) {
00104         return -5;
00105       }
00106    }
00107 
00108     if(cf.getValue(KEY_STREAM_CPM0, buf, sizeof(buf))) {
00109       if(sscanf(buf, "%d", &(p->stream_cpm0)) !=1) {
00110         return -6;
00111       }
00112    }
00113 #ifdef DUALGM
00114     if(cf.getValue(KEY_STREAM_NSV1, buf, sizeof(buf))) {
00115       if(sscanf(buf, "%d", &(p->stream_nsv1)) !=1) {
00116         return -7;
00117       }
00118    }
00119 
00120     if(cf.getValue(KEY_STREAM_CPM1, buf, sizeof(buf))) {
00121       if(sscanf(buf, "%d", &(p->stream_cpm1)) !=1) {
00122         return -8;
00123       }
00124    }
00125 #endif
00126 // 2011/08/03 end
00127     if(cf.getValue(KEY_STREAM_NSVLONG0, buf, sizeof(buf))) {     //  2011/08/23
00128       if(sscanf(buf, "%d", &(p->stream_nsvlong0)) !=1) {         //  2011/08/23
00129         return -9;                                              //  2011/08/23
00130       }                                                         //  2011/08/23
00131    }                                                            //  2011/08/23
00132     if(cf.getValue(KEY_STREAM_CPMLONG0, buf, sizeof(buf))) {     //  2011/08/23
00133       if(sscanf(buf, "%d", &(p->stream_cpmlong0)) !=1) {         //  2011/08/23
00134         return -10;                                              //  2011/08/23
00135       }                                                         //  2011/08/23
00136    }                                                            //  2011/08/23
00137     return 0;
00138 }
00139 
00140 /**
00141  * Write configuration.
00142  *
00143  * @param filename Filename.
00144  * @param p A pointer to a application config.
00145  * @return Return zero if it succeed.
00146  */
00147 int appconf_write(char *filename, appconf_t *p) {
00148     ConfigFile cf;
00149     if (!cf.setValue(KEY_PACHUBE_APIKEY, p->apikey)) {
00150         return -1;
00151     }
00152     if (!cf.setValue(KEY_PACHUBE_FEEDID, p->feedid)) {
00153         return -2;
00154     }
00155 /*
00156     char buf[64];
00157     snprintf(buf, sizeof(buf) - 1, "%d", p->interval);
00158     if (!cf.setValue(KEY_PACHUBE_INTERVAL, buf)) {
00159         return -3;
00160     }
00161 */
00162     if (!cf.write(filename)) {
00163         return -4;
00164     }
00165     return 0;
00166 }