init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Revision:
21:e0b866630c27
Parent:
20:5404841fdd2b
Child:
22:810425eb76e1
--- a/main.cpp	Wed Dec 19 09:02:20 2018 +0000
+++ b/main.cpp	Thu Dec 20 22:04:45 2018 +0000
@@ -1,5 +1,9 @@
 #include "main.h"
-//include "nrf_soc.h"
+
+//------------------------------------------------------------------------------
+//FUNCTION PROTOTYPES
+//------------------------------------------------------------------------------ 
+#define FW_VERSION 1
 
 //------------------------------------------------------------------------------
 //FUNCTION PROTOTYPES
@@ -60,6 +64,7 @@
 static time_t           RET_buttonReleaseTime                           __attribute__((section("noinit"),zero_init));
 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));
 //MOTION STATE
 static bool             RET_motionTriggered                             __attribute__((section("noinit"),zero_init));
 static bool             RET_motionTriggeredInTXInterval                 __attribute__((section("noinit"),zero_init));
@@ -202,7 +207,7 @@
 }
 void factoryReset() {
     DEBUG("Factory Reset \n");
-    Thread::wait(500); //wait to ensure that devices are turned off
+    Thread::wait(200); //wait to ensure that devices are turned off
     //IDENTITY 
     RET_imei = 0;
     memset(RET_pf_identifier,0,sizeof(RET_pf_identifier));
@@ -212,13 +217,14 @@
     RET_state = STATE_SETUP;
     RET_state_prev = RET_state;
     RET_RTCunixtime = 0;
+    RET_SetupRunAt = 0;
     //SETTINGS
     RET_setting_firmware = 0;
     RET_setting_location_mode = 2;
     RET_setting_location_accuracy = 1;
     RET_setting_location_tx_interval_mins = 1440;
     RET_setting_location_tx_failsafe_hrs = (7*24); //7 days
-    RET_setting_location_timeout = 180;
+    RET_setting_location_timeout = 240;
     RET_setting_activity_tx_interval_mins = 0;
     RET_setting_environmental_tx_interval_mins = 0;
     RET_setting_motion_g = 11;
@@ -335,23 +341,23 @@
         //FAILUREMODE need to verify the identifier against a reg exp or similar
         if(1==1) { memcpy(RET_pf_identifier, TEMP_a, sizeof(TEMP_a));              changed = 'Y'; } else { changed = 'N'; critical_fail_count++; }; DEBUG("RET_pf_identifier:%s..%c\n",RET_pf_identifier,changed);
         if(TEMP_b != 0) { RET_RTCunixtime = 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'; }; DEBUG("RET_setting_firmware:%d..%c\n",RET_setting_firmware,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);
         if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e;                     changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_accuracy:%d..%c\n",RET_setting_location_accuracy,changed);
         if(TEMP_f != -1) { RET_setting_location_accuracy = TEMP_f;                 changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_accuracy:%d..%c\n",RET_setting_location_accuracy,changed);
         if(TEMP_g != -1) { RET_setting_location_tx_interval_mins = TEMP_g;         changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_tx_interval_mins:%d..%c\n",RET_setting_location_tx_interval_mins,changed);
-        if(TEMP_h != -1) { RET_setting_location_tx_failsafe_hrs = TEMP_h;          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_tx_failsafe_hrs:%d..%c\n",RET_setting_location_tx_failsafe_hrs,changed)
-        if(TEMP_i != -1) { RET_setting_location_timeout = TEMP_i;                  changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_timeout:%d..%c\n",RET_setting_location_timeout,changed)
-        if(TEMP_j != -1) { RET_setting_activity_tx_interval_mins = TEMP_j;         changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_activity_tx_interval_mins:%d..%c\n",RET_setting_activity_tx_interval_mins,changed)
-        if(TEMP_k != -1) { RET_setting_environmental_tx_interval_mins = TEMP_k;    changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_environmental_tx_interval_mins:%d..%c\n",RET_setting_environmental_tx_interval_mins,changed)
-        if(TEMP_l != -1) { RET_setting_motion_g = TEMP_l;                          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_g:%d..%c\n",RET_setting_motion_g,changed)
-        if(TEMP_m != -1) { RET_setting_motion_start_seconds = TEMP_m;              changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_start_seconds:%d..%c\n",RET_setting_motion_start_seconds,changed)
-        if(TEMP_n != -1) { RET_setting_motion_stop_seconds = TEMP_n;               changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_stop_seconds:%d..%c\n",RET_setting_motion_stop_seconds,changed)
-        if(TEMP_o != -1) { RET_setting_impact_g = TEMP_o;                          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_g:%d..%c\n",RET_setting_impact_g,changed)
-        if(TEMP_p != -1) { RET_setting_impact_alert = TEMP_p;                      changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_alert:%d..%c\n",RET_setting_impact_alert,changed)
-        if(TEMP_q != -1) { RET_setting_connection_timeout = TEMP_q;                changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_connection_timeout:%d..%c\n",RET_setting_connection_timeout,changed)
-        if(TEMP_r != -1) { RET_setting_beacon_interval_seconds = TEMP_r;           changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_interval_seconds:%d..%c\n",RET_setting_beacon_interval_seconds,changed)
-        if(TEMP_s != -1) { RET_setting_beacon_scan = TEMP_s;                       changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_scan:%d..%c\n",RET_setting_beacon_scan,changed)
+        if(TEMP_h != -1) { RET_setting_location_tx_failsafe_hrs = TEMP_h;          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_tx_failsafe_hrs:%d..%c\n",RET_setting_location_tx_failsafe_hrs,changed);
+        if(TEMP_i != -1) { RET_setting_location_timeout = TEMP_i;                  changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_location_timeout:%d..%c\n",RET_setting_location_timeout,changed);
+        if(TEMP_j != -1) { RET_setting_activity_tx_interval_mins = TEMP_j;         changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_activity_tx_interval_mins:%d..%c\n",RET_setting_activity_tx_interval_mins,changed);
+        if(TEMP_k != -1) { RET_setting_environmental_tx_interval_mins = TEMP_k;    changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_environmental_tx_interval_mins:%d..%c\n",RET_setting_environmental_tx_interval_mins,changed);
+        if(TEMP_l != -1) { RET_setting_motion_g = TEMP_l;                          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_g:%d..%c\n",RET_setting_motion_g,changed);
+        if(TEMP_m != -1) { RET_setting_motion_start_seconds = TEMP_m;              changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_start_seconds:%d..%c\n",RET_setting_motion_start_seconds,changed);
+        if(TEMP_n != -1) { RET_setting_motion_stop_seconds = TEMP_n;               changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_motion_stop_seconds:%d..%c\n",RET_setting_motion_stop_seconds,changed);
+        if(TEMP_o != -1) { RET_setting_impact_g = TEMP_o;                          changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_g:%d..%c\n",RET_setting_impact_g,changed);
+        if(TEMP_p != -1) { RET_setting_impact_alert = TEMP_p;                      changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_impact_alert:%d..%c\n",RET_setting_impact_alert,changed);
+        if(TEMP_q != -1) { RET_setting_connection_timeout = TEMP_q;                changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_connection_timeout:%d..%c\n",RET_setting_connection_timeout,changed);
+        if(TEMP_r != -1) { RET_setting_beacon_interval_seconds = TEMP_r;           changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_interval_seconds:%d..%c\n",RET_setting_beacon_interval_seconds,changed);
+        if(TEMP_s != -1) { RET_setting_beacon_scan = TEMP_s;                       changed = 'Y'; } else { changed = 'N'; }; DEBUG("RET_setting_beacon_scan:%d..%c\n",RET_setting_beacon_scan,changed);
         
         if (critical_fail_count == 0) { 
             DEBUG("GOT SETTINGS OK\n"); 
@@ -368,6 +374,27 @@
         return false;
     }
 }
+bool dumpSettings() {
+    DEBUG("RET_pf_identifier:%s \n",RET_pf_identifier);
+    DEBUG("RET_RTCunixtime:%u \n",RET_RTCunixtime);
+    DEBUG("RET_setting_firmware:%d \n",RET_setting_firmware);
+    DEBUG("RET_state:%d \n",RET_state);
+    DEBUG("RET_setting_location_accuracy:%d \n",RET_setting_location_accuracy);
+    DEBUG("RET_setting_location_accuracy:%d \n",RET_setting_location_accuracy);
+    DEBUG("RET_setting_location_tx_interval_mins:%d \n",RET_setting_location_tx_interval_mins);
+    DEBUG("RET_setting_location_tx_failsafe_hrs:%d \n",RET_setting_location_tx_failsafe_hrs);
+    DEBUG("RET_setting_location_timeout:%d \n",RET_setting_location_timeout);
+    DEBUG("RET_setting_activity_tx_interval_mins:%d \n",RET_setting_activity_tx_interval_mins);
+    DEBUG("RET_setting_environmental_tx_interval_mins:%d \n",RET_setting_environmental_tx_interval_mins);
+    DEBUG("RET_setting_motion_g:%d \n",RET_setting_motion_g);
+    DEBUG("RET_setting_motion_start_seconds:%d \n",RET_setting_motion_start_seconds);
+    DEBUG("RET_setting_motion_stop_seconds:%d \n",RET_setting_motion_stop_seconds);
+    DEBUG("RET_setting_impact_g:%d \n",RET_setting_impact_g);
+    DEBUG("RET_setting_impact_alert:%d \n",RET_setting_impact_alert);
+    DEBUG("RET_setting_connection_timeout:%d \n",RET_setting_connection_timeout);
+    DEBUG("RET_setting_beacon_interval_seconds:%d \n",RET_setting_beacon_interval_seconds);
+    DEBUG("RET_setting_beacon_scan:%d \n",RET_setting_beacon_scan);
+}
 void saveEventTimes() {
     //SET EVENT TIMES
     if(RET_setting_location_tx_interval_mins > 0) { 
@@ -393,7 +420,7 @@
 //------------------------------------------------------------------------------ 
 bool setup() {
     bool pass = true;
-    Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
+    Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
     if (modem.on()) {
         RET_imei = modem.getIMEI();
         GLOBAL_imei = RET_imei;
@@ -404,7 +431,7 @@
             char result[180];
             snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, 2, GLOBAL_defaultApi));
             if (result != "err") {
-                DEBUG("fromserver: %s \n",result);
+                DEBUG("\nfromserver: %s \n",result);
                 if (!saveSettings(result)) { 
                     //something went critically wrong getting settings 
                     pass = false;
@@ -419,6 +446,9 @@
         pass = false;
     }
     
+    //LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP
+    RET_SetupRunAt = RET_RTCunixtime;
+    
     //RESULT
     return pass;
 }
@@ -429,16 +459,11 @@
 void event_location_tx() {
     DEBUG("PERFORM LOCATION TX\n");
     getBatteryV();  
-    
-    Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN);
+    Modem modem(PN_UART_TX, PN_UART_RX, PN_UART_CTS, PN_UART_RTS, PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
     if (modem.on()) {
-        
         //RET_setting_location_accuracy
         modem.getLocation(true, 3, true, RET_setting_location_timeout);
-        
     }
-    
-    
     //RESETS
     RET_motionTriggeredInTXInterval = 0;
     saveEventTimes();
@@ -468,7 +493,10 @@
             
             //LOCATION
             //Location Log
-            //if(RET_RTCunixtime > RET_eventTime_location_log && RET_eventTime_location_log > 0) { }
+            /*
+            if(RET_RTCunixtime > RET_eventTime_location_log && RET_eventTime_location_log > 0)  { 
+            }
+            */
             bool run_location_tx = false;
             switch (RET_setting_location_mode) {
                 case 1: //INTERVAL POST
@@ -492,14 +520,8 @@
                     break;   
             }
             //Failsafe timer catchall
-            if(RET_RTCunixtime > RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { 
-                run_location_tx = true; 
-            }
-            if (run_location_tx) {
-                event_location_tx();
-            }
-            
-            
+            if(RET_RTCunixtime > RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) { run_location_tx = true; }
+            if (run_location_tx) {event_location_tx();}
             
             //Environmental Log
             if(RET_RTCunixtime > RET_eventTime_environmental_log && RET_eventTime_environmental_log > 0) {
@@ -587,6 +609,9 @@
         }
         NRF_POWER->RESETREAS = 0xffffffff;
     }
+    //DEBUG
+    dumpSettings();
+    
     //CHECK FOR FIRST BOOT
     if (RET_coldBoot == 1) { factoryReset(); }
     
@@ -605,9 +630,7 @@
         GLOBAL_wakeTime = RET_RTCunixtime;
         
         //MAIN LOGIC
-        //DEBUG("STATE: %d,  RTC_MILLIS: %lld,  RTC_SECONDS:%lld,  BUTT_T:%d,  BUTT_C,%d \n", RET_state, RET_RTCmillis, RET_RTCunixtime, RET_buttonHoldTime, RET_buttonPressCount);
-        DEBUG("STATE: %d,  RTC_SECONDS:%u\n", RET_state, RET_RTCunixtime);
-
+        DEBUG("STATE:%d, RTC_SECONDS:%u, SETUPAT:%u\n", RET_state, RET_RTCunixtime,RET_SetupRunAt);
         
         event_location_tx();
         while(1) {