init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
32:dff4858bdf37
Parent:
31:c84fc6d8eaa3
Child:
33:760005331b4c
--- a/main.cpp	Tue Jan 01 15:44:24 2019 +0000
+++ b/main.cpp	Tue Jan 01 20:46:39 2019 +0000
@@ -54,6 +54,7 @@
 static long long        RET_imei                                        __attribute__((section("noinit"),zero_init));
 //SETTINGS
 static int              RET_setting_firmware                            __attribute__((section("noinit"),zero_init)); //must be int to support negative value
+static uint32_t         RET_setting_minimumupdate_hrs                   __attribute__((section("noinit"),zero_init));
 static uint8_t          RET_setting_location_mode                       __attribute__((section("noinit"),zero_init));
 static uint8_t          RET_setting_location_accuracy                   __attribute__((section("noinit"),zero_init));
 static uint32_t         RET_setting_location_tx_interval_mins           __attribute__((section("noinit"),zero_init));
@@ -81,6 +82,7 @@
 static time_t           RET_buttonHoldTime                              __attribute__((section("noinit"),zero_init));
 static time_t           RET_RTCunixtime                                 __attribute__((section("noinit"),zero_init));
 static time_t           RET_SetupRunAt                                  __attribute__((section("noinit"),zero_init));
+static time_t           RET_SettingsGotAt                               __attribute__((section("noinit"),zero_init));
 //MOTION STATE
 static bool             RET_motionTriggered                             __attribute__((section("noinit"),zero_init));
 static bool             RET_motionTriggeredInTXInterval                 __attribute__((section("noinit"),zero_init));
@@ -174,6 +176,7 @@
     DEBUG("RET_RTCunixtime:%u \n",RET_RTCunixtime);
     DEBUG("RET_RTCunixtime_bu:%u \n",RET_RTCunixtime_bu);
     DEBUG("RET_setting_firmware:%d \n",RET_setting_firmware);
+    DEBUG("RET_setting_minimumupdate_hrs:%d \n",RET_setting_minimumupdate_hrs);
     DEBUG("RET_state:%d \n",RET_state);
     DEBUG("RET_setting_location_mode:%d \n",RET_setting_location_mode);
     DEBUG("RET_setting_location_accuracy:%d \n",RET_setting_location_accuracy);
@@ -285,8 +288,10 @@
     RET_RTCunixtime = 0;
     RET_RTCunixtime_bu = 0;
     RET_SetupRunAt = 0;
+    RET_SettingsGotAt = 0;
     //SETTINGS
     RET_setting_firmware = 0;
+    RET_setting_minimumupdate_hrs = 0;
     RET_setting_location_mode = DEFAULT_LOCATION_MODE;
     RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;
     RET_setting_location_tx_interval_mins = DEFAULT_LOCATION_TX_INTERVAL_MINS;
@@ -429,15 +434,17 @@
 bool saveSettings(char* settingsBuffer) {
     int matchCount = 0;
     int critical_fail_count = 0;
-    time_t TEMP_b = 0; int TEMP_c = -1; int TEMP_d = -1; int TEMP_e = -1; int TEMP_f = -1; int TEMP_g = -1; int TEMP_h = -1; int TEMP_i = -1; int TEMP_j = -1; 
+    int TEMP_a = -1; time_t TEMP_b = 0; int TEMP_c = -1; int TEMP_d = -1; int TEMP_e = -1; int TEMP_f = -1; int TEMP_g = -1; int TEMP_h = -1; int TEMP_i = -1; int TEMP_j = -1; 
     int TEMP_k = -1; int TEMP_l = -1; int TEMP_m = -1; int TEMP_n = -1; int TEMP_o = -1; int TEMP_p = -1; int TEMP_q = -1; int TEMP_r = -1; int TEMP_s = -1;
-    if ( (matchCount = sscanf(settingsBuffer,"b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d",
-    &TEMP_b,&TEMP_c,&TEMP_d,&TEMP_e,&TEMP_f,&TEMP_g,&TEMP_h,&TEMP_i,&TEMP_j,&TEMP_k,&TEMP_l,&TEMP_m,&TEMP_n,&TEMP_o,&TEMP_p,&TEMP_q,&TEMP_r,&TEMP_s) ) > 0 ) {
-        DEBUG("FROMSERVER: b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d\n",
-        TEMP_b,TEMP_c,TEMP_d,TEMP_e,TEMP_f,TEMP_g,TEMP_h,TEMP_i,TEMP_j,TEMP_k,TEMP_l,TEMP_m,TEMP_n,TEMP_o,TEMP_p,TEMP_q,TEMP_r,TEMP_s);
+    if ( (matchCount = sscanf(settingsBuffer,"a:%d,b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d",
+    &TEMP_a,&TEMP_b,&TEMP_c,&TEMP_d,&TEMP_e,&TEMP_f,&TEMP_g,&TEMP_h,&TEMP_i,&TEMP_j,&TEMP_k,&TEMP_l,&TEMP_m,&TEMP_n,&TEMP_o,&TEMP_p,&TEMP_q,&TEMP_r,&TEMP_s) ) > 0 ) {
+        DEBUG("FROMSERVER: a:%d,b:%u,c:%d,d:%d,e:%d,f:%d,g:%d,h:%d,i:%d,j:%d,k:%d,l:%d,m:%d,n:%d,o:%d,p:%d,q:%d,r:%d,s:%d\n",
+        TEMP_a,TEMP_b,TEMP_c,TEMP_d,TEMP_e,TEMP_f,TEMP_g,TEMP_h,TEMP_i,TEMP_j,TEMP_k,TEMP_l,TEMP_m,TEMP_n,TEMP_o,TEMP_p,TEMP_q,TEMP_r,TEMP_s);
         
         //FAILUREMODE need to be checking these against checksums
         char changed;
+        
+        if(TEMP_a != -1) { RET_setting_minimumupdate_hrs = TEMP_a;                 changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_minimumupdate_hrs:%u..%c\n",RET_setting_minimumupdate_hrs,changed);
         if(TEMP_b != 0) { RET_RTCunixtime = TEMP_b; RET_RTCunixtime_bu = TEMP_b;   changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_RTCunixtime:%u..%c\n",RET_RTCunixtime,changed);
         if(TEMP_c != -1) { RET_setting_firmware = TEMP_c;                          changed = 'Y'; } else { changed = 'N'; RET_setting_firmware = -1;}; DEBUG("RET_setting_firmware:%d..%c\n",RET_setting_firmware,changed);
         if(TEMP_d != -1) { setState(TEMP_d);                                       changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_state:%d..%c\n",RET_state,changed);
@@ -466,7 +473,8 @@
         if (critical_fail_count == 0) { 
             DEBUG("GOT SETTINGS OK\n"); 
             saveEventTimes();
-            RET_haveSettings = true;     
+            RET_haveSettings = true;
+            RET_SettingsGotAt = RET_RTCunixtime;
             return true;
         } else {
             DEBUG("CRITICAL FAILS:%d\n",critical_fail_count);
@@ -518,7 +526,7 @@
         if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytestosend[160];
-            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP-%s,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken,GLOBAL_exceptionString,locString);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,z:SETUP-%s,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,voltage,temperature,timetaken,GLOBAL_exceptionString,locString);
             char result[180];
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
             if (result != "err") {
@@ -562,7 +570,7 @@
         if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
             char bytestosend[160];
-            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:TEST-%s,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,locString);
+            snprintf(bytestosend,sizeof(bytestosend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:TEST-%s,e:%d,c:1,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,locString);
             char result[180];
             bool getSettings = true;
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, getSettings, 2, GLOBAL_defaultApi));
@@ -595,10 +603,14 @@
         //SEND DATA
         if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,(RET_setting_connection_timeout*1000))) {
             int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
+            
+            //Check if we should wait for settings back
+            bool getSettings = true;
+            if (  ((RET_RTCunixtime - RET_SettingsGotAt)/3600) < RET_setting_minimumupdate_hrs  ) { getSettings = false;}
+            
             char bytesToSend[160];
-            snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:LOC-%s,e:%d,c:1%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,locString);
+            snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.1f,v:%.2f,z:LOC-%s,e:%d,c:1,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,temperature,voltage,GLOBAL_exceptionString,timetaken,getSettings,locString);
             char result[180];
-            bool getSettings = true;
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, 2, GLOBAL_defaultApi));
             if (result != "err") {
                 //DEBUG("\nfromserver: %s \n",result);