init

Dependencies:   aconno_I2C Lis2dh12 WatchdogTimer

Committer:
pathfindr
Date:
Wed May 29 22:59:12 2019 +0000
Revision:
52:bd7678eade77
Parent:
51:9078e6928412
Child:
53:c6942af186d7
ble funcs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pathfindr 0:94b743e06998 1 #include "main.h"
pathfindr 51:9078e6928412 2 // MUST USE MBED 5.10.4 2fd0c5cfbd
pathfindr 51:9078e6928412 3
pathfindr 51:9078e6928412 4 /*
pathfindr 51:9078e6928412 5 PRE DEPLOYMENT CHECKS!!
pathfindr 51:9078e6928412 6 • Ensure APP DATA address is correct
pathfindr 51:9078e6928412 7 */
pathfindr 51:9078e6928412 8
pathfindr 51:9078e6928412 9 /*
pathfindr 51:9078e6928412 10 TO DO
pathfindr 51:9078e6928412 11 STOP FRASH WRITE FROM CRASHING IF THERE IS A PROBLEM
pathfindr 51:9078e6928412 12 */
pathfindr 51:9078e6928412 13
pathfindr 21:e0b866630c27 14 //------------------------------------------------------------------------------
pathfindr 37:505ef618f06c 15 //FUNCTION PROTOTYPES - NEED TO ADD ALL OF THE MISSING ONES
pathfindr 0:94b743e06998 16 //------------------------------------------------------------------------------
pathfindr 45:6c7f5b556d74 17 //should really add these - will just add as and when needed for now
pathfindr 48:64b1613941d5 18 void event_activity_tx();
pathfindr 51:9078e6928412 19 void event_location_tx(bool failsafe);
pathfindr 52:bd7678eade77 20 #if BLE_ENABLED
pathfindr 52:bd7678eade77 21 void bleShutdown();
pathfindr 52:bd7678eade77 22 #endif
pathfindr 0:94b743e06998 23
pathfindr 0:94b743e06998 24 //------------------------------------------------------------------------------
pathfindr 36:8e359069192b 25 // GLOBALS
pathfindr 51:9078e6928412 26 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 27 char* GLOBAL_defaultApi = "b:gps2";
pathfindr 52:bd7678eade77 28 bool GLOBAL_accel_healthy = false;
pathfindr 52:bd7678eade77 29 bool GLOBAL_motionStopFlagTriggered = false;
pathfindr 52:bd7678eade77 30 bool GLOBAL_needToConfigureLis3dh = false;
pathfindr 52:bd7678eade77 31 bool GLOBAL_registeredOnNetwork = false;
pathfindr 52:bd7678eade77 32 bool GLOBAL_modemOn = false;
pathfindr 52:bd7678eade77 33 bool GLOBAL_LEDSequenceinProgress = false;
pathfindr 52:bd7678eade77 34 time_t GLOBAL_wakeTime = 0;
pathfindr 44:061509d1b38c 35 char GLOBAL_exceptionString[30];
pathfindr 52:bd7678eade77 36 char GLOBAL_debug_buffer[DEBUG_BUFFERSIZE];
pathfindr 51:9078e6928412 37 char GLOBAL_failed_broadcasts[10][160];
pathfindr 51:9078e6928412 38 bool GLOBAL_failed_broadcast_slots[10];
pathfindr 52:bd7678eade77 39 char GLOBAL_GPSlocString_prev[70];
pathfindr 52:bd7678eade77 40 bool GLOBAL_have_GPSlocString_prev = false;
pathfindr 34:4493c9f6d707 41
pathfindr 48:64b1613941d5 42 //SETTINGS
pathfindr 52:bd7678eade77 43 int RET_setting_firmware = 0;
pathfindr 52:bd7678eade77 44 uint32_t RET_setting_minimumupdate_hrs = 0;
pathfindr 52:bd7678eade77 45 uint8_t RET_setting_location_mode = DEFAULT_LOCATION_MODE;
pathfindr 52:bd7678eade77 46 uint8_t RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;
pathfindr 52:bd7678eade77 47 uint32_t RET_setting_location_tx_interval_mins = DEFAULT_LOCATION_TX_INTERVAL_MINS;
pathfindr 52:bd7678eade77 48 uint32_t RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;
pathfindr 52:bd7678eade77 49 uint16_t RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;
pathfindr 52:bd7678eade77 50 uint32_t RET_setting_activity_tx_interval_hrs = 0;
pathfindr 52:bd7678eade77 51 uint8_t RET_setting_activity_mode = 1;
pathfindr 52:bd7678eade77 52 uint32_t RET_setting_environmental_tx_interval_mins = 0;
pathfindr 52:bd7678eade77 53 uint16_t RET_setting_motion_g = DEFAULT_MOTION_G;
pathfindr 52:bd7678eade77 54 time_t RET_setting_motion_start_seconds = DEFAULT_MOTION_START_SECONDS;
pathfindr 52:bd7678eade77 55 time_t RET_setting_motion_stop_seconds = DEFAULT_MOTION_STOP_SECONDS;
pathfindr 52:bd7678eade77 56 uint16_t RET_setting_impact_g = 0;
pathfindr 52:bd7678eade77 57 uint8_t RET_setting_impact_alert = 0;
pathfindr 52:bd7678eade77 58 uint16_t RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT;
pathfindr 52:bd7678eade77 59 uint16_t RET_setting_beacon_interval_seconds = 0;
pathfindr 52:bd7678eade77 60 uint8_t RET_setting_beacon_scan = 0;
pathfindr 13:29f67f256709 61 //STATE
pathfindr 51:9078e6928412 62 char RET_pf_identifier[7];
pathfindr 52:bd7678eade77 63 uint8_t RET_coldBoot = 1;
pathfindr 52:bd7678eade77 64 bool RET_asleep = false;
pathfindr 52:bd7678eade77 65 bool RET_busy = false;
pathfindr 52:bd7678eade77 66 time_t RET_RTCunixtime = 0;
pathfindr 52:bd7678eade77 67 bool RET_haveSettings = false;
pathfindr 52:bd7678eade77 68 uint8_t RET_state = STATE_SETUP;
pathfindr 52:bd7678eade77 69 uint8_t RET_state_prev = RET_state;
pathfindr 52:bd7678eade77 70 uint8_t RET_buttonPressCount = 0;
pathfindr 52:bd7678eade77 71 time_t RET_buttonPressTime = 0;
pathfindr 52:bd7678eade77 72 time_t RET_buttonReleaseTime = 0;
pathfindr 52:bd7678eade77 73 time_t RET_buttonReleaseTime_prev = 0;
pathfindr 52:bd7678eade77 74 time_t RET_buttonHoldTime = 0;
pathfindr 52:bd7678eade77 75 time_t RET_SetupRunAt = 0;
pathfindr 52:bd7678eade77 76 time_t RET_SettingsGotAt = 0;
pathfindr 52:bd7678eade77 77 bool RET_setupInProgress = false;
pathfindr 52:bd7678eade77 78 bool RET_force2G = DEFAULT_FORCE2G;
pathfindr 52:bd7678eade77 79 bool RET_watchdogfired = false;
pathfindr 52:bd7678eade77 80 bool RET_receivedNewSettings = false;
pathfindr 52:bd7678eade77 81 uint32_t RET_GPSFailCount = 0;
pathfindr 52:bd7678eade77 82 uint32_t RET_NetworkFailCount = 0;
pathfindr 52:bd7678eade77 83 bool RET_debug = true;
pathfindr 52:bd7678eade77 84 time_t RET_debug_offat = 0;
pathfindr 52:bd7678eade77 85 float RET_voltage = 0.0;
pathfindr 52:bd7678eade77 86 float RET_temperature = 0.0;
pathfindr 52:bd7678eade77 87 float RET_temperature_max = -999.0; //Within broadcast frame. Set inital value to low
pathfindr 52:bd7678eade77 88 float RET_temperature_min = 999.0; //Within broadcase frame. Set inital value to high
pathfindr 52:bd7678eade77 89 uint32_t RET_modemBrownOutCountInSession = 0;
pathfindr 16:3bf5f1a5f869 90 //MOTION STATE
pathfindr 52:bd7678eade77 91 bool RET_motionTriggeredinFrame = false;
pathfindr 52:bd7678eade77 92 bool RET_motionStateOnInLocTXInterval = false;
pathfindr 52:bd7678eade77 93 time_t RET_motionStartTime = 0;
pathfindr 52:bd7678eade77 94 time_t RET_motionStopTime = 0;
pathfindr 52:bd7678eade77 95 bool RET_motionPendingOnState = false;
pathfindr 52:bd7678eade77 96 bool RET_motionPendingOffState = false;
pathfindr 52:bd7678eade77 97 bool RET_motionState = false;
pathfindr 52:bd7678eade77 98 float RET_motionTotalActivityHours = 0.0f;
pathfindr 52:bd7678eade77 99 float RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 52:bd7678eade77 100 time_t RET_motionFrameStart = 0;
pathfindr 45:6c7f5b556d74 101 char RET_activityData[ACTIVITY_BUFFERSIZE];
pathfindr 16:3bf5f1a5f869 102 //IMPACT
pathfindr 52:bd7678eade77 103 bool RET_impactTriggered = 0;
pathfindr 19:22261767c87a 104 //EVENTS LOGGING
pathfindr 52:bd7678eade77 105 time_t RET_eventTime_location_log = 0;
pathfindr 52:bd7678eade77 106 time_t RET_eventTime_environmental_log = 0;
pathfindr 19:22261767c87a 107 //EVENTS TX
pathfindr 52:bd7678eade77 108 time_t RET_eventTime_location_tx = 0;
pathfindr 52:bd7678eade77 109 time_t RET_eventTime_location_failsafe_tx = 0;
pathfindr 52:bd7678eade77 110 time_t RET_eventTime_environmental_tx = 0;
pathfindr 52:bd7678eade77 111 time_t RET_eventTime_activity_tx = 0;
pathfindr 52:bd7678eade77 112 time_t RET_eventTime_wakeFromDormant = 0;
pathfindr 52:bd7678eade77 113 //BLE
pathfindr 52:bd7678eade77 114 #if BLE_ENABLED
pathfindr 52:bd7678eade77 115 bool RET_bleBroadcasting = false;
pathfindr 52:bd7678eade77 116 uint8_t RET_bleAdv_flags = 0;
pathfindr 52:bd7678eade77 117 char RET_closestLocationTag_id[17];
pathfindr 52:bd7678eade77 118 int RET_closestLocationTag_rssi = 999;
pathfindr 52:bd7678eade77 119 bool RET_locationTag_present = false;
pathfindr 52:bd7678eade77 120 bool RET_detector_present = false;
pathfindr 52:bd7678eade77 121 const uint8_t bleAdv_motionTriggeredinFrame_flag = 0b01000000;
pathfindr 52:bd7678eade77 122 const uint8_t bleAdv_motionState_flag = 0b00100000;
pathfindr 52:bd7678eade77 123 const uint8_t bleAdv_impact_flag = 0b00010000;
pathfindr 52:bd7678eade77 124 #pragma pack(1)
pathfindr 52:bd7678eade77 125 struct bleData_t {
pathfindr 52:bd7678eade77 126 uint16_t applicationSpecificId;
pathfindr 52:bd7678eade77 127 uint8_t firmware;
pathfindr 52:bd7678eade77 128 uint8_t flags;
pathfindr 52:bd7678eade77 129 uint16_t voltage;
pathfindr 52:bd7678eade77 130 uint8_t buttonpresses;
pathfindr 52:bd7678eade77 131 int16_t temperature;
pathfindr 52:bd7678eade77 132 uint8_t humidity;
pathfindr 52:bd7678eade77 133 uint8_t lux;
pathfindr 52:bd7678eade77 134 int8_t accel_x;
pathfindr 52:bd7678eade77 135 int8_t accel_y;
pathfindr 52:bd7678eade77 136 int8_t accel_z;
pathfindr 52:bd7678eade77 137 /*char id_1; //cant use this and local name as it pushes us over size limit
pathfindr 52:bd7678eade77 138 char id_2;
pathfindr 52:bd7678eade77 139 char id_3;
pathfindr 52:bd7678eade77 140 char id_4;
pathfindr 52:bd7678eade77 141 char id_5;
pathfindr 52:bd7678eade77 142 char id_6;*/
pathfindr 52:bd7678eade77 143 };
pathfindr 52:bd7678eade77 144 #pragma pack()
pathfindr 52:bd7678eade77 145 #endif
pathfindr 0:94b743e06998 146 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 147 //GPIO
pathfindr 9:b0a1535b8ef2 148 //------------------------------------------------------------------------------
pathfindr 34:4493c9f6d707 149 InterruptIn button(PN_IN_BUTTON);
pathfindr 9:b0a1535b8ef2 150
pathfindr 9:b0a1535b8ef2 151 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 152 //PERIPHERALS
pathfindr 0:94b743e06998 153 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 154 #if BLE_ENABLED
pathfindr 52:bd7678eade77 155 BLE myble;
pathfindr 52:bd7678eade77 156 #endif
pathfindr 52:bd7678eade77 157 WatchdogTimer watchdog;
pathfindr 34:4493c9f6d707 158 LIS3DH lis3dh(PN_SPI_MOSI, PN_SPI_MISO, PN_SPI_CS0, PN_SPI_CLK);
pathfindr 52:bd7678eade77 159 SI7060 si7060(PN_I2C_SDA, PN_I2C_SCL);
pathfindr 34:4493c9f6d707 160 Modem modem(PN_GSM_PWR_KEY, PN_VREG_EN, PN_GSM_WAKE_DISABLE);
pathfindr 37:505ef618f06c 161 LowPowerTicker RTCticker;
pathfindr 51:9078e6928412 162 LowPowerTimer LPtimer;
pathfindr 9:b0a1535b8ef2 163
pathfindr 9:b0a1535b8ef2 164 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 165 //SEMAPHORES
pathfindr 9:b0a1535b8ef2 166 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 167 Semaphore mainthread;
pathfindr 9:b0a1535b8ef2 168
pathfindr 9:b0a1535b8ef2 169 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 170 // LOW LEVEL FUNCS
pathfindr 34:4493c9f6d707 171 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 172 void watchdogKick() {
pathfindr 52:bd7678eade77 173 watchdog.kick();
pathfindr 52:bd7678eade77 174 }
pathfindr 51:9078e6928412 175 void set_8bit_flag(uint8_t& flags, uint8_t flag) {
pathfindr 51:9078e6928412 176 flags |= flag;
pathfindr 51:9078e6928412 177 }
pathfindr 51:9078e6928412 178 bool get_8bit_flag(uint8_t& flags, uint8_t flag) {
pathfindr 51:9078e6928412 179 bool result = (flags & flag);
pathfindr 51:9078e6928412 180 return result;
pathfindr 51:9078e6928412 181 }
pathfindr 51:9078e6928412 182 void clr_8bit_flag(uint8_t& flags, uint8_t flag) {
pathfindr 51:9078e6928412 183 flags &= ~flag;
pathfindr 51:9078e6928412 184 }
pathfindr 50:a94e70c00fff 185 void nrf_configureForSleep(){
pathfindr 50:a94e70c00fff 186 //Disable SPI pins to reduce power
pathfindr 50:a94e70c00fff 187 //nrf_gpio_cfg_input(PN_SPI_MOSI, NRF_GPIO_PIN_NOPULL); //Don't need this one
pathfindr 34:4493c9f6d707 188 nrf_gpio_cfg_input(PN_SPI_MISO, NRF_GPIO_PIN_NOPULL);
pathfindr 50:a94e70c00fff 189
pathfindr 50:a94e70c00fff 190 //disable modem control lines
pathfindr 50:a94e70c00fff 191 nrf_gpio_cfg_input(PN_GSM_PWR_KEY, NRF_GPIO_PIN_NOPULL);
pathfindr 50:a94e70c00fff 192 nrf_gpio_cfg_input(PN_GSM_WAKE_DISABLE, NRF_GPIO_PIN_NOPULL);
pathfindr 50:a94e70c00fff 193
pathfindr 50:a94e70c00fff 194 //TWI0
pathfindr 52:bd7678eade77 195 NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
pathfindr 50:a94e70c00fff 196 *(volatile uint32_t *)0x40003FFC = 0;
pathfindr 50:a94e70c00fff 197 *(volatile uint32_t *)0x40003FFC;
pathfindr 50:a94e70c00fff 198 *(volatile uint32_t *)0x40003FFC = 1;
pathfindr 50:a94e70c00fff 199 //TWI1
pathfindr 52:bd7678eade77 200 NRF_TWI1->ENABLE=TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
pathfindr 50:a94e70c00fff 201 *(volatile uint32_t *)0x40004FFC = 0;
pathfindr 50:a94e70c00fff 202 *(volatile uint32_t *)0x40004FFC;
pathfindr 50:a94e70c00fff 203 *(volatile uint32_t *)0x40004FFC = 1;
pathfindr 6:388d3c7efdd9 204 }
pathfindr 19:22261767c87a 205 void setState(uint8_t state) {
pathfindr 19:22261767c87a 206 RET_state_prev = RET_state;
pathfindr 19:22261767c87a 207 RET_state = state;
pathfindr 22:810425eb76e1 208 }
pathfindr 51:9078e6928412 209 void dumpSettings() {
pathfindr 51:9078e6928412 210 if(RET_debug){
pathfindr 34:4493c9f6d707 211 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_RTCunixtime:%u", RET_RTCunixtime);debug_exe();
pathfindr 34:4493c9f6d707 212 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_firmware:%d", RET_setting_firmware);debug_exe();
pathfindr 34:4493c9f6d707 213 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_minimumupdate_hrs:%d", RET_setting_minimumupdate_hrs);debug_exe();
pathfindr 34:4493c9f6d707 214 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_state:%d", RET_state);debug_exe();
pathfindr 34:4493c9f6d707 215 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_mode:%d", RET_setting_location_mode);debug_exe();
pathfindr 34:4493c9f6d707 216 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_accuracy:%d", RET_setting_location_accuracy);debug_exe();
pathfindr 34:4493c9f6d707 217 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_tx_interval_mins:%d", RET_setting_location_tx_interval_mins);debug_exe();
pathfindr 34:4493c9f6d707 218 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_tx_failsafe_hrs:%d", RET_setting_location_tx_failsafe_hrs);debug_exe();
pathfindr 34:4493c9f6d707 219 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_location_timeout:%d", RET_setting_location_timeout);debug_exe();
pathfindr 34:4493c9f6d707 220 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_activity_tx_interval_hrs:%d", RET_setting_activity_tx_interval_hrs);debug_exe();
pathfindr 45:6c7f5b556d74 221 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_activity_mode:%d", RET_setting_activity_mode);debug_exe();
pathfindr 34:4493c9f6d707 222 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_environmental_tx_interval_mins:%d", RET_setting_environmental_tx_interval_mins);debug_exe();
pathfindr 34:4493c9f6d707 223 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_motion_g:%d", RET_setting_motion_g);debug_exe();
pathfindr 34:4493c9f6d707 224 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_motion_start_seconds:%d", RET_setting_motion_start_seconds);debug_exe();
pathfindr 34:4493c9f6d707 225 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_motion_stop_seconds:%d", RET_setting_motion_stop_seconds);debug_exe();
pathfindr 34:4493c9f6d707 226 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_impact_g:%d", RET_setting_impact_g);debug_exe();
pathfindr 34:4493c9f6d707 227 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_impact_alert:%d", RET_setting_impact_alert);debug_exe();
pathfindr 34:4493c9f6d707 228 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_connection_timeout:%d", RET_setting_connection_timeout);debug_exe();
pathfindr 34:4493c9f6d707 229 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_beacon_interval_seconds:%d", RET_setting_beacon_interval_seconds);debug_exe();
pathfindr 34:4493c9f6d707 230 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_setting_beacon_scan:%d", RET_setting_beacon_scan);debug_exe();
pathfindr 51:9078e6928412 231 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RET_pf_identifier:%s", RET_pf_identifier);debug_exe();
pathfindr 34:4493c9f6d707 232 }
pathfindr 24:c161db07557f 233 }
pathfindr 45:6c7f5b556d74 234 void recordFirmwareAsValid() {
pathfindr 52:bd7678eade77 235 #if BLE_ENABLED
pathfindr 52:bd7678eade77 236 bleShutdown();
pathfindr 52:bd7678eade77 237 #endif
pathfindr 51:9078e6928412 238 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RECORD FW %d AS VALID...", FW_VERSION);debug_exe();
pathfindr 45:6c7f5b556d74 239 read_app_data_from_flash(&app_data);
pathfindr 45:6c7f5b556d74 240 bool write_app_data_to_flash_execute = false;
pathfindr 45:6c7f5b556d74 241 if(get_flag(&app_data, app_execution_flag) == true) {
pathfindr 45:6c7f5b556d74 242 clr_flag(&app_data, app_execution_flag);
pathfindr 45:6c7f5b556d74 243 clr_flag(&app_data, first_run_flag);
pathfindr 45:6c7f5b556d74 244 write_app_data_to_flash_execute = true;
pathfindr 45:6c7f5b556d74 245 }
pathfindr 45:6c7f5b556d74 246 if(app_data.current_firmware_version != FW_VERSION) {
pathfindr 45:6c7f5b556d74 247 app_data.current_firmware_version = FW_VERSION;
pathfindr 45:6c7f5b556d74 248 app_data.target_firmware_version = FW_VERSION;
pathfindr 45:6c7f5b556d74 249 write_app_data_to_flash_execute = true;
pathfindr 45:6c7f5b556d74 250 }
pathfindr 45:6c7f5b556d74 251 if (write_app_data_to_flash_execute) {
pathfindr 45:6c7f5b556d74 252 write_app_data_to_flash(&app_data);
pathfindr 51:9078e6928412 253 //read back to check
pathfindr 51:9078e6928412 254 ThisThread::sleep_for(200); //need this delay
pathfindr 51:9078e6928412 255 read_app_data_from_flash(&app_data);
pathfindr 51:9078e6928412 256 if (app_data.current_firmware_version == FW_VERSION) {
pathfindr 51:9078e6928412 257 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "OK");debug_exe();
pathfindr 51:9078e6928412 258 } else {
pathfindr 51:9078e6928412 259 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FAIL - IS BLE SHUTDOWN?");debug_exe();
pathfindr 51:9078e6928412 260 }
pathfindr 51:9078e6928412 261 }
pathfindr 45:6c7f5b556d74 262 }
pathfindr 51:9078e6928412 263 void updateBatteryV() {
pathfindr 19:22261767c87a 264 NRF52_SAADC batteryIn;
pathfindr 19:22261767c87a 265 batteryIn.addChannel(9); // vdd for battery
pathfindr 19:22261767c87a 266 batteryIn.calibrate();
pathfindr 49:15ddd84ec9fa 267 //get initial reading, first reading is always low
pathfindr 49:15ddd84ec9fa 268 batteryIn.updateData(); batteryIn.getData()[0];
pathfindr 49:15ddd84ec9fa 269 float voltage_accumulator = 0.0;
pathfindr 49:15ddd84ec9fa 270 uint8_t readings = 0;
pathfindr 49:15ddd84ec9fa 271 for (uint8_t i = 1; i <= 50; i++) { // need to get it 2 times to get accurate data, first one is always low for some reason
pathfindr 19:22261767c87a 272 batteryIn.updateData();
pathfindr 49:15ddd84ec9fa 273 voltage_accumulator += (batteryIn.getData()[0])*(1.0/1024.0)*3.60;
pathfindr 49:15ddd84ec9fa 274 readings ++;
pathfindr 19:22261767c87a 275 }
pathfindr 51:9078e6928412 276 RET_voltage = (voltage_accumulator / (float)readings);
pathfindr 22:810425eb76e1 277 }
pathfindr 52:bd7678eade77 278 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 279 // TEMPERATURE
pathfindr 52:bd7678eade77 280 //------------------------------------------------------------------------------
pathfindr 22:810425eb76e1 281 float getTemperature() {
pathfindr 52:bd7678eade77 282 return si7060.getTemperature();
pathfindr 19:22261767c87a 283 }
pathfindr 51:9078e6928412 284 void updateTemperatures() {
pathfindr 52:bd7678eade77 285 if (RET_temperature == 0.0) {RET_temperature = getTemperature();}
pathfindr 51:9078e6928412 286 RET_temperature = (RET_temperature + (getTemperature() - RET_temperature) * 0.25f); //smoothed
pathfindr 52:bd7678eade77 287 if (RET_temperature < RET_temperature_min) {RET_temperature_min = RET_temperature;}
pathfindr 52:bd7678eade77 288 if (RET_temperature > RET_temperature_max) {RET_temperature_max = RET_temperature;}
pathfindr 51:9078e6928412 289 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Temp:%.1f Min:%.1f Max:%.1f",RET_temperature,RET_temperature_min,RET_temperature_max);debug_exe();}
pathfindr 51:9078e6928412 290 }
pathfindr 27:fa76f5a08195 291 void addToExceptionString(char* value) {
pathfindr 45:6c7f5b556d74 292 if(strstr(GLOBAL_exceptionString, value) == false){
pathfindr 52:bd7678eade77 293 snprintf(GLOBAL_exceptionString+strlen(GLOBAL_exceptionString),sizeof(GLOBAL_exceptionString),".%s",value);
pathfindr 45:6c7f5b556d74 294 }
pathfindr 27:fa76f5a08195 295 }
pathfindr 51:9078e6928412 296 void resetTemperatures() {
pathfindr 51:9078e6928412 297 RET_temperature_max = -999.0; //Within broadcast frame. Set inital value to low
pathfindr 51:9078e6928412 298 RET_temperature_min = 999.0; //Within broadcase frame. Set inital value to high
pathfindr 51:9078e6928412 299 }
pathfindr 19:22261767c87a 300 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 301 // USER BUTTON HANDLING
pathfindr 19:22261767c87a 302 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 303 void buttonPress() {
pathfindr 51:9078e6928412 304 RET_buttonPressTime = LPtimer.read_ms();
pathfindr 19:22261767c87a 305 }
pathfindr 19:22261767c87a 306 void buttonRelease() {
pathfindr 51:9078e6928412 307 //debounce catch
pathfindr 51:9078e6928412 308 RET_buttonReleaseTime = LPtimer.read_ms();
pathfindr 51:9078e6928412 309 if ((RET_buttonReleaseTime - RET_buttonReleaseTime_prev) > 75) {
pathfindr 51:9078e6928412 310 RET_buttonPressCount ++;
pathfindr 51:9078e6928412 311 RET_buttonHoldTime = (RET_buttonReleaseTime - RET_buttonPressTime);
pathfindr 51:9078e6928412 312 //temporarily enable debugging and dump settings
pathfindr 51:9078e6928412 313 RET_debug = true;
pathfindr 52:bd7678eade77 314 RET_debug_offat = (RET_RTCunixtime + 3600); //debug on for 1hr
pathfindr 51:9078e6928412 315 }
pathfindr 51:9078e6928412 316 RET_buttonReleaseTime_prev = RET_buttonReleaseTime;
pathfindr 19:22261767c87a 317 }
pathfindr 19:22261767c87a 318 //------------------------------------------------------------------------------
pathfindr 19:22261767c87a 319 // RTC TICKER
pathfindr 51:9078e6928412 320 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 321 void RTCtick() {
pathfindr 51:9078e6928412 322 //YOU MUST NOT CALL ANY OTHER FUNCTIONS OR DEBUG FROM INSIDE HERE!!! OR IT LOCKS UP THE DEVICE, just change vars & comparisons etc
pathfindr 19:22261767c87a 323 RET_RTCunixtime += 1;
pathfindr 51:9078e6928412 324
pathfindr 51:9078e6928412 325 //button logic
pathfindr 51:9078e6928412 326 if (RET_buttonHoldTime >= 4000 && RET_buttonHoldTime <= 10000) {
pathfindr 49:15ddd84ec9fa 327 RET_buttonHoldTime = 0;
pathfindr 49:15ddd84ec9fa 328 RET_buttonPressCount = 0;
pathfindr 51:9078e6928412 329 if (RET_state != STATE_DORMANT) {
pathfindr 51:9078e6928412 330 RET_state_prev = RET_state;
pathfindr 51:9078e6928412 331 RET_state = STATE_TURNOFF;
pathfindr 51:9078e6928412 332 } else {
pathfindr 51:9078e6928412 333 RET_state = STATE_TURNON;
pathfindr 51:9078e6928412 334 }
pathfindr 49:15ddd84ec9fa 335 mainthread.release();
pathfindr 51:9078e6928412 336 } else if (RET_buttonHoldTime >= 14000 && RET_buttonHoldTime <= 30000) {
pathfindr 49:15ddd84ec9fa 337 RET_buttonHoldTime = 0;
pathfindr 49:15ddd84ec9fa 338 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 339 RET_state = STATE_SCORCHEDEARTH;
pathfindr 49:15ddd84ec9fa 340 mainthread.release();
pathfindr 49:15ddd84ec9fa 341 } else {
pathfindr 51:9078e6928412 342 if((LPtimer.read_ms() - RET_buttonReleaseTime) > 1500 && RET_buttonPressCount > 0) {
pathfindr 49:15ddd84ec9fa 343 if(RET_busy == true) {
pathfindr 49:15ddd84ec9fa 344 //RTCtick_ledflash_count = 4;// 2 flashes
pathfindr 49:15ddd84ec9fa 345 } else {
pathfindr 49:15ddd84ec9fa 346 RET_SetupRunAt = 0; //allow setup to run again
pathfindr 49:15ddd84ec9fa 347 switch (RET_buttonPressCount) { //double catches to help with debounce
pathfindr 49:15ddd84ec9fa 348 case 1 :
pathfindr 50:a94e70c00fff 349 if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
pathfindr 49:15ddd84ec9fa 350 RET_state = STATE_BUTTONPRESS1;
pathfindr 49:15ddd84ec9fa 351 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 352 mainthread.release();
pathfindr 49:15ddd84ec9fa 353 break;
pathfindr 49:15ddd84ec9fa 354 case 3 :
pathfindr 50:a94e70c00fff 355 if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
pathfindr 49:15ddd84ec9fa 356 RET_state = STATE_BUTTONPRESS3;
pathfindr 49:15ddd84ec9fa 357 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 358 mainthread.release();
pathfindr 49:15ddd84ec9fa 359 break;
pathfindr 49:15ddd84ec9fa 360 case 5 :
pathfindr 50:a94e70c00fff 361 if(RET_state_prev != RET_state) {RET_state_prev = RET_state;}
pathfindr 49:15ddd84ec9fa 362 RET_state = STATE_BUTTONPRESS5;
pathfindr 49:15ddd84ec9fa 363 RET_buttonPressCount = 0;
pathfindr 49:15ddd84ec9fa 364 mainthread.release();
pathfindr 49:15ddd84ec9fa 365 break;
pathfindr 49:15ddd84ec9fa 366 default :
pathfindr 49:15ddd84ec9fa 367 //do nothing
pathfindr 49:15ddd84ec9fa 368 break;
pathfindr 39:f767b8037475 369 }
pathfindr 9:b0a1535b8ef2 370 }
pathfindr 51:9078e6928412 371 RET_buttonPressCount = 0;
pathfindr 9:b0a1535b8ef2 372 }
pathfindr 49:15ddd84ec9fa 373 }
pathfindr 52:bd7678eade77 374
pathfindr 52:bd7678eade77 375 if(RET_debug && GLOBAL_LEDSequenceinProgress == false){
pathfindr 52:bd7678eade77 376 if (RET_motionState) {
pathfindr 52:bd7678eade77 377 led1 = 0;
pathfindr 52:bd7678eade77 378 } else {
pathfindr 52:bd7678eade77 379 led1 = 1;
pathfindr 52:bd7678eade77 380 }
pathfindr 52:bd7678eade77 381 }
pathfindr 9:b0a1535b8ef2 382 }
pathfindr 52:bd7678eade77 383 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 384 // GLOBALS
pathfindr 52:bd7678eade77 385 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 386 void resetSessionVars() {
pathfindr 34:4493c9f6d707 387 GLOBAL_accel_healthy = false;
pathfindr 52:bd7678eade77 388 RET_motionTriggeredinFrame = false;
pathfindr 52:bd7678eade77 389 RET_modemBrownOutCountInSession = 0;
pathfindr 34:4493c9f6d707 390 memset(GLOBAL_exceptionString,0x00,sizeof(GLOBAL_exceptionString));
pathfindr 34:4493c9f6d707 391 }
pathfindr 38:476a9b5629a1 392 void healthCheck() {
pathfindr 39:f767b8037475 393 //check for watchdog fire
pathfindr 39:f767b8037475 394 if (RET_watchdogfired == true) {
pathfindr 39:f767b8037475 395 addToExceptionString("WD");
pathfindr 39:f767b8037475 396 RET_watchdogfired = false;
pathfindr 39:f767b8037475 397 }
pathfindr 41:07e41dbd5304 398 //check clock
pathfindr 41:07e41dbd5304 399 if(RET_haveSettings == true) {
pathfindr 41:07e41dbd5304 400 if (RET_RTCunixtime < 1547678732) {
pathfindr 41:07e41dbd5304 401 //go dormant for 72hrs and then resetup
pathfindr 41:07e41dbd5304 402 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 403 setState(STATE_DORMANT);
pathfindr 41:07e41dbd5304 404 }
pathfindr 41:07e41dbd5304 405 }
pathfindr 41:07e41dbd5304 406 //check that we have a reasonable time to loc
pathfindr 41:07e41dbd5304 407 if(RET_haveSettings == true) {
pathfindr 41:07e41dbd5304 408 //check location tx delta is set
pathfindr 41:07e41dbd5304 409 if (RET_eventTime_location_tx == 0) {
pathfindr 51:9078e6928412 410 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERR:eventTime_location_tx is 0");debug_exe();}
pathfindr 41:07e41dbd5304 411 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 412 }
pathfindr 41:07e41dbd5304 413 //check location failsafe tx delta is less that 2 weeks
pathfindr 41:07e41dbd5304 414 long location_failsafe_tx_delta = (RET_eventTime_location_failsafe_tx - RET_RTCunixtime);
pathfindr 41:07e41dbd5304 415 if (location_failsafe_tx_delta > TENDAYSINSECONDS) {
pathfindr 51:9078e6928412 416 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ERR:location_failsafe_tx_delta too small");debug_exe();}
pathfindr 41:07e41dbd5304 417 RET_haveSettings = false;
pathfindr 41:07e41dbd5304 418 }
pathfindr 41:07e41dbd5304 419 }
pathfindr 41:07e41dbd5304 420 if (RET_haveSettings == false) {
pathfindr 41:07e41dbd5304 421 setState(STATE_SETUP);
pathfindr 41:07e41dbd5304 422 }
pathfindr 38:476a9b5629a1 423 }
pathfindr 24:c161db07557f 424
pathfindr 22:810425eb76e1 425 bool selfTest() {
pathfindr 22:810425eb76e1 426 int test_count = 0;
pathfindr 22:810425eb76e1 427 int test_pass = 0;
pathfindr 22:810425eb76e1 428
pathfindr 8:7351f8c4cd60 429 //Accelerometer
pathfindr 22:810425eb76e1 430 test_count ++;
pathfindr 8:7351f8c4cd60 431 uint8_t lis3dh_id;
pathfindr 8:7351f8c4cd60 432 lis3dh.LIS3DH_GetWHO_AM_I(&lis3dh_id);
pathfindr 27:fa76f5a08195 433 if (lis3dh_id == 51) {
pathfindr 43:7b232f03628f 434 GLOBAL_accel_healthy = true;
pathfindr 27:fa76f5a08195 435 test_pass ++;
pathfindr 27:fa76f5a08195 436 } else {
pathfindr 43:7b232f03628f 437 GLOBAL_accel_healthy = false;
pathfindr 52:bd7678eade77 438 addToExceptionString("AF");
pathfindr 27:fa76f5a08195 439 }
pathfindr 22:810425eb76e1 440
pathfindr 22:810425eb76e1 441 //Result
pathfindr 22:810425eb76e1 442 if (test_count == test_pass) {
pathfindr 22:810425eb76e1 443 return true;
pathfindr 22:810425eb76e1 444 } else {
pathfindr 52:bd7678eade77 445 //addToExceptionString("HF");
pathfindr 22:810425eb76e1 446 return false;
pathfindr 9:b0a1535b8ef2 447 }
pathfindr 9:b0a1535b8ef2 448 }
pathfindr 52:bd7678eade77 449 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 450 // BLE FUNCS
pathfindr 52:bd7678eade77 451 //------------------------------------------------------------------------------
pathfindr 52:bd7678eade77 452 #if BLE_ENABLED
pathfindr 52:bd7678eade77 453 void bleBuildData(bleData_t &bleData)
pathfindr 52:bd7678eade77 454 {
pathfindr 52:bd7678eade77 455 bleData.applicationSpecificId = BLE_SERVICEID_GPSPLUS;
pathfindr 52:bd7678eade77 456 bleData.firmware = FW_VERSION;
pathfindr 52:bd7678eade77 457 //set flags
pathfindr 52:bd7678eade77 458 if (RET_motionTriggeredinFrame) {
pathfindr 52:bd7678eade77 459 set_8bit_flag(RET_bleAdv_flags, bleAdv_motionTriggeredinFrame_flag);
pathfindr 52:bd7678eade77 460 } else {
pathfindr 52:bd7678eade77 461 clr_8bit_flag(RET_bleAdv_flags, bleAdv_motionTriggeredinFrame_flag);
pathfindr 52:bd7678eade77 462 }
pathfindr 52:bd7678eade77 463 if (RET_motionState) {
pathfindr 52:bd7678eade77 464 set_8bit_flag(RET_bleAdv_flags, bleAdv_motionState_flag);
pathfindr 52:bd7678eade77 465 } else {
pathfindr 52:bd7678eade77 466 clr_8bit_flag(RET_bleAdv_flags, bleAdv_motionState_flag);
pathfindr 52:bd7678eade77 467 }
pathfindr 52:bd7678eade77 468 clr_8bit_flag(RET_bleAdv_flags, bleAdv_impact_flag);
pathfindr 52:bd7678eade77 469 bleData.flags = RET_bleAdv_flags;
pathfindr 52:bd7678eade77 470 bleData.voltage = uint16_t(RET_voltage * 100);
pathfindr 52:bd7678eade77 471 bleData.temperature = uint16_t(getTemperature() * 10);
pathfindr 52:bd7678eade77 472 bleData.humidity = 0xFF;
pathfindr 52:bd7678eade77 473 bleData.lux = 0xFF;
pathfindr 52:bd7678eade77 474 bleData.accel_x = 0xFF;
pathfindr 52:bd7678eade77 475 bleData.accel_y = 0xFF;
pathfindr 52:bd7678eade77 476 bleData.accel_z = 0xFF;
pathfindr 52:bd7678eade77 477 /*
pathfindr 52:bd7678eade77 478 bleData.id_1 = RET_pf_identifier[0]; //dont use this just stick it in the local name
pathfindr 52:bd7678eade77 479 bleData.id_2 = RET_pf_identifier[1];
pathfindr 52:bd7678eade77 480 bleData.id_3 = RET_pf_identifier[2];
pathfindr 52:bd7678eade77 481 bleData.id_4 = RET_pf_identifier[3];
pathfindr 52:bd7678eade77 482 bleData.id_5 = RET_pf_identifier[4];
pathfindr 52:bd7678eade77 483 bleData.id_6 = RET_pf_identifier[5];
pathfindr 52:bd7678eade77 484 */
pathfindr 52:bd7678eade77 485 }
pathfindr 52:bd7678eade77 486 void bleUpdateAndAdvertise(void)
pathfindr 52:bd7678eade77 487 {
pathfindr 52:bd7678eade77 488 int AdvertisingInterval = (RET_setting_beacon_interval_seconds * 1000);
pathfindr 52:bd7678eade77 489 bleData_t bleData;
pathfindr 52:bd7678eade77 490 bleBuildData(bleData);
pathfindr 52:bd7678eade77 491 myble.gap().stopAdvertising();
pathfindr 52:bd7678eade77 492 myble.gap().setTxPower(DEFAULT_BEACON_POWER); // set tx power,valid values are -40, -20, -16, -12, -8, -4, 0, 4
pathfindr 52:bd7678eade77 493 myble.gap().accumulateAdvertisingPayloadTxPower(DEFAULT_BEACON_POWER); // (accepted values are -40, -30, -20, -16, -12, -8, -4, 0, and 4 dBm)
pathfindr 52:bd7678eade77 494 myble.gap().setAdvertisingInterval(AdvertisingInterval);
pathfindr 52:bd7678eade77 495 myble.gap().updateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)&bleData, sizeof(bleData_t));
pathfindr 52:bd7678eade77 496 //myble.gap().updateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&bleData, sizeof(bleData_t));
pathfindr 52:bd7678eade77 497 myble.gap().startAdvertising();
pathfindr 52:bd7678eade77 498 RET_bleBroadcasting = true;
pathfindr 52:bd7678eade77 499 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "BLE Adv Start");debug_exe();}
pathfindr 52:bd7678eade77 500 }
pathfindr 52:bd7678eade77 501 void bleSetupAdvertising(BLE::InitializationCompleteCallbackContext *params)
pathfindr 52:bd7678eade77 502 {
pathfindr 52:bd7678eade77 503 if (RET_pf_identifier[0] != 0x00 && RET_pf_identifier[1] != 0x00 && RET_pf_identifier[2] != 0x00) {
pathfindr 52:bd7678eade77 504 bleData_t bleData;
pathfindr 52:bd7678eade77 505 bleBuildData(bleData);
pathfindr 52:bd7678eade77 506 char localname[6];
pathfindr 52:bd7678eade77 507 memcpy(localname,RET_pf_identifier,6);
pathfindr 52:bd7678eade77 508 myble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE );
pathfindr 52:bd7678eade77 509 myble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)localname, sizeof(localname));
pathfindr 52:bd7678eade77 510 myble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)&bleData, sizeof(bleData_t));
pathfindr 52:bd7678eade77 511 //myble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&bleData, sizeof(bleData_t));
pathfindr 52:bd7678eade77 512 myble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED);
pathfindr 52:bd7678eade77 513 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "BLE Setup...");debug_exe();}
pathfindr 52:bd7678eade77 514 }
pathfindr 52:bd7678eade77 515 }
pathfindr 52:bd7678eade77 516 void bleStopAdvertising(void)
pathfindr 52:bd7678eade77 517 {
pathfindr 52:bd7678eade77 518 myble.gap().stopAdvertising();
pathfindr 52:bd7678eade77 519 RET_bleBroadcasting = false;
pathfindr 52:bd7678eade77 520 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "BLE Adv Stop");debug_exe();}
pathfindr 52:bd7678eade77 521 }
pathfindr 52:bd7678eade77 522 void bleAdvertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
pathfindr 52:bd7678eade77 523 struct AdvertisingData_t {
pathfindr 52:bd7678eade77 524 uint8_t length;
pathfindr 52:bd7678eade77 525 GapAdvertisingData::DataType_t dataType;
pathfindr 52:bd7678eade77 526 uint8_t data[0];
pathfindr 52:bd7678eade77 527 } PACKED;
pathfindr 52:bd7678eade77 528 struct bleData_t {
pathfindr 52:bd7678eade77 529 uint16_t applicationSpecificId;
pathfindr 52:bd7678eade77 530 } PACKED;
pathfindr 52:bd7678eade77 531
pathfindr 52:bd7678eade77 532 //LOOKING FOR (SIAEC BETA)
pathfindr 52:bd7678eade77 533 //14f64b977c250854
pathfindr 52:bd7678eade77 534 //442ce31996801546
pathfindr 52:bd7678eade77 535
pathfindr 52:bd7678eade77 536 char beta_location_tag_0[17] = {'4','1','1','7','6','9','0','5','6','4','6','8','3','f','6','8',0x00}; //bens
pathfindr 52:bd7678eade77 537 char beta_location_tag_1[17] = {'1','4','f','6','4','b','9','7','7','c','2','5','0','8','5','4',0x00}; //singapore
pathfindr 52:bd7678eade77 538 char beta_location_tag_2[17] = {'4','4','2','c','e','3','1','9','9','6','8','0','1','5','4','6',0x00}; //singapore
pathfindr 52:bd7678eade77 539
pathfindr 52:bd7678eade77 540 //Portal demos
pathfindr 52:bd7678eade77 541 char beta_location_tag_3[17] = {'1','5','f','c','6','d','d','5','c','3','7','f','1','f','4','a',0x00};
pathfindr 52:bd7678eade77 542 char beta_location_tag_4[17] = {'b','a','a','e','c','d','6','8','b','d','7','e','e','a','6','5',0x00};
pathfindr 52:bd7678eade77 543 char beta_location_tag_5[17] = {'7','e','4','b','7','8','e','0','f','a','3','3','0','8','a','0',0x00};
pathfindr 52:bd7678eade77 544 char beta_location_tag_6[17] = {'e','1','1','3','c','d','e','a','a','e','f','f','1','4','d','9',0x00};
pathfindr 52:bd7678eade77 545 char beta_location_tag_7[17] = {'8','c','9','2','4','5','f','f','0','c','f','a','b','9','2','f',0x00};
pathfindr 52:bd7678eade77 546 char beta_location_tag_8[17] = {'0','7','e','9','9','1','d','3','b','e','b','6','4','0','4','8',0x00};
pathfindr 52:bd7678eade77 547 char beta_location_tag_9[17] = {'9','5','6','3','a','0','8','6','2','1','e','8','e','c','1','4',0x00};
pathfindr 52:bd7678eade77 548
pathfindr 52:bd7678eade77 549 //Search for the manufacturer specific data with matching application-ID
pathfindr 52:bd7678eade77 550 AdvertisingData_t *pAdvData;
pathfindr 52:bd7678eade77 551 for (size_t index = 0; index < params->advertisingDataLen; index += (pAdvData->length + 1)) {
pathfindr 52:bd7678eade77 552 pAdvData = (AdvertisingData_t *)&params->advertisingData[index];
pathfindr 52:bd7678eade77 553 const bleData_t *pbleData = (const bleData_t *)pAdvData->data;
pathfindr 52:bd7678eade77 554
pathfindr 52:bd7678eade77 555 //ESTIMOTE BEACON
pathfindr 52:bd7678eade77 556 //example data 020104 0303 9afe1716 9afe 22 02c887042a33f9f0 00efebc60146f8ffffffff //spaces just to show identifier
pathfindr 52:bd7678eade77 557
pathfindr 52:bd7678eade77 558 //BLE TYPE SWITCH
pathfindr 52:bd7678eade77 559 switch(pbleData->applicationSpecificId) {
pathfindr 52:bd7678eade77 560 case BLE_SERVICEID_PFBEACON1 :
pathfindr 52:bd7678eade77 561 {
pathfindr 52:bd7678eade77 562 char beacon_identifier[17];
pathfindr 52:bd7678eade77 563 snprintf(beacon_identifier, sizeof(beacon_identifier), "%02x%02x%02x%02x%02x%02x%02x%02x",
pathfindr 52:bd7678eade77 564 params->advertisingData[12],
pathfindr 52:bd7678eade77 565 params->advertisingData[13],
pathfindr 52:bd7678eade77 566 params->advertisingData[14],
pathfindr 52:bd7678eade77 567 params->advertisingData[15],
pathfindr 52:bd7678eade77 568 params->advertisingData[16],
pathfindr 52:bd7678eade77 569 params->advertisingData[17],
pathfindr 52:bd7678eade77 570 params->advertisingData[18],
pathfindr 52:bd7678eade77 571 params->advertisingData[19]);
pathfindr 52:bd7678eade77 572 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), " Mac: [%02x%02x%02x%02x%02x%02x], rssi: %d, AType: %u, S: %04x, Id: %s",params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],params->rssi, params->type, pbleData->applicationSpecificId, beacon_identifier);debug_exe();}
pathfindr 52:bd7678eade77 573 //check to see if its one of the two location beta beacons we are looking for...
pathfindr 52:bd7678eade77 574 if (strcmp(beacon_identifier, beta_location_tag_0) == 0
pathfindr 52:bd7678eade77 575 || strcmp(beacon_identifier, beta_location_tag_1) == 0
pathfindr 52:bd7678eade77 576 || strcmp(beacon_identifier, beta_location_tag_2) == 0
pathfindr 52:bd7678eade77 577 || strcmp(beacon_identifier, beta_location_tag_3) == 0
pathfindr 52:bd7678eade77 578 || strcmp(beacon_identifier, beta_location_tag_4) == 0
pathfindr 52:bd7678eade77 579 || strcmp(beacon_identifier, beta_location_tag_5) == 0
pathfindr 52:bd7678eade77 580 || strcmp(beacon_identifier, beta_location_tag_6) == 0
pathfindr 52:bd7678eade77 581 || strcmp(beacon_identifier, beta_location_tag_7) == 0
pathfindr 52:bd7678eade77 582 || strcmp(beacon_identifier, beta_location_tag_8) == 0
pathfindr 52:bd7678eade77 583 || strcmp(beacon_identifier, beta_location_tag_9) == 0
pathfindr 52:bd7678eade77 584 ) {
pathfindr 52:bd7678eade77 585 //dump advertising data
pathfindr 52:bd7678eade77 586 //for (unsigned index = 0; index < params->advertisingDataLen; index++) { if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "%02x",params->advertisingData[index]);debug_exe(false);}}
pathfindr 52:bd7678eade77 587 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), " MATCH");debug_exe();}
pathfindr 52:bd7678eade77 588 int tag_rssi = params->rssi * -1; //to convert to positive number
pathfindr 52:bd7678eade77 589 if (tag_rssi < RET_closestLocationTag_rssi) {
pathfindr 52:bd7678eade77 590 RET_closestLocationTag_rssi = tag_rssi;
pathfindr 52:bd7678eade77 591 memcpy(RET_closestLocationTag_id, beacon_identifier, sizeof(RET_closestLocationTag_id));
pathfindr 52:bd7678eade77 592 RET_locationTag_present = true;
pathfindr 52:bd7678eade77 593 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Location Tag Present: %s, rssi: %d",closestLocationTag_id, closestLocationTag_rssi);debug_exe();}
pathfindr 52:bd7678eade77 594 }
pathfindr 52:bd7678eade77 595 }
pathfindr 52:bd7678eade77 596 break;
pathfindr 52:bd7678eade77 597 }
pathfindr 52:bd7678eade77 598 case BLE_SERVICEID_BT04 :
pathfindr 52:bd7678eade77 599 {
pathfindr 52:bd7678eade77 600 //nothing yet
pathfindr 52:bd7678eade77 601 break;
pathfindr 52:bd7678eade77 602 }
pathfindr 52:bd7678eade77 603 case BLE_SERVICEID_PFDETECTOR1 :
pathfindr 52:bd7678eade77 604 {
pathfindr 52:bd7678eade77 605 char ble_advdata[100];
pathfindr 52:bd7678eade77 606 snprintf(ble_advdata, sizeof(ble_advdata), "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
pathfindr 52:bd7678eade77 607 params->advertisingData[0],
pathfindr 52:bd7678eade77 608 params->advertisingData[1],
pathfindr 52:bd7678eade77 609 params->advertisingData[2],
pathfindr 52:bd7678eade77 610 params->advertisingData[3],
pathfindr 52:bd7678eade77 611 params->advertisingData[4],
pathfindr 52:bd7678eade77 612 params->advertisingData[5],
pathfindr 52:bd7678eade77 613 params->advertisingData[6],
pathfindr 52:bd7678eade77 614 params->advertisingData[7],
pathfindr 52:bd7678eade77 615 params->advertisingData[8],
pathfindr 52:bd7678eade77 616 params->advertisingData[9],
pathfindr 52:bd7678eade77 617 params->advertisingData[10],
pathfindr 52:bd7678eade77 618 params->advertisingData[11],
pathfindr 52:bd7678eade77 619 params->advertisingData[12],
pathfindr 52:bd7678eade77 620 params->advertisingData[13],
pathfindr 52:bd7678eade77 621 params->advertisingData[14],
pathfindr 52:bd7678eade77 622 params->advertisingData[15],
pathfindr 52:bd7678eade77 623 params->advertisingData[16],
pathfindr 52:bd7678eade77 624 params->advertisingData[17],
pathfindr 52:bd7678eade77 625 params->advertisingData[18],
pathfindr 52:bd7678eade77 626 params->advertisingData[19],
pathfindr 52:bd7678eade77 627 params->advertisingData[20]);
pathfindr 52:bd7678eade77 628 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), " Mac: [%02x%02x%02x%02x%02x%02x], rssi: %d, AType: %u, Id: %s",params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],params->rssi, pAdvData->dataType, ble_advdata);debug_exe();}
pathfindr 52:bd7678eade77 629 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Detector @rssi: %d",params->rssi);debug_exe();}
pathfindr 52:bd7678eade77 630 if (params->rssi >= -75) {
pathfindr 52:bd7678eade77 631 RET_detector_present = true;
pathfindr 52:bd7678eade77 632 }
pathfindr 52:bd7678eade77 633 break;
pathfindr 52:bd7678eade77 634 }
pathfindr 52:bd7678eade77 635 }
pathfindr 52:bd7678eade77 636 }
pathfindr 52:bd7678eade77 637 }
pathfindr 52:bd7678eade77 638 void bleConfigureScan(void)
pathfindr 52:bd7678eade77 639 {
pathfindr 52:bd7678eade77 640 //configure scan
pathfindr 52:bd7678eade77 641 //myble.gap().setScanParams(60,30); //scan interval //scan window
pathfindr 52:bd7678eade77 642 myble.gap().setScanParams(100,100); //scan interval //scan window
pathfindr 52:bd7678eade77 643 myble.gap().startScan(bleAdvertisementCallback);
pathfindr 52:bd7678eade77 644 }
pathfindr 52:bd7678eade77 645 void bleStopScan(void)
pathfindr 52:bd7678eade77 646 {
pathfindr 52:bd7678eade77 647 myble.gap().stopScan();
pathfindr 52:bd7678eade77 648 }
pathfindr 52:bd7678eade77 649 void bleShutdown(void)
pathfindr 52:bd7678eade77 650 {
pathfindr 52:bd7678eade77 651 bleStopScan();
pathfindr 52:bd7678eade77 652 bleStopAdvertising();
pathfindr 52:bd7678eade77 653 myble.shutdown();
pathfindr 52:bd7678eade77 654 }
pathfindr 52:bd7678eade77 655 #endif
pathfindr 9:b0a1535b8ef2 656
pathfindr 9:b0a1535b8ef2 657 //------------------------------------------------------------------------------
pathfindr 9:b0a1535b8ef2 658 // MOTION FUNCS
pathfindr 9:b0a1535b8ef2 659 //------------------------------------------------------------------------------
pathfindr 48:64b1613941d5 660 bool checkMotion() {
pathfindr 51:9078e6928412 661 if (RET_haveSettings) { //NEED THIS AS THINGS GO FUNKY WITHOUT RTC TIME SET CORRECTLY
pathfindr 51:9078e6928412 662 if (lis3dh_int2) {
pathfindr 51:9078e6928412 663 RET_motionTriggeredinFrame = true;
pathfindr 51:9078e6928412 664 GLOBAL_needToConfigureLis3dh = true; //interrupt has fire so need to clear it
pathfindr 52:bd7678eade77 665 if(RET_debug) {
pathfindr 52:bd7678eade77 666 LED1on(100);
pathfindr 52:bd7678eade77 667 }
pathfindr 51:9078e6928412 668 if (!RET_motionPendingOnState) {
pathfindr 51:9078e6928412 669 RET_motionPendingOnState = true;
pathfindr 51:9078e6928412 670 RET_motionPendingOffState = false;
pathfindr 51:9078e6928412 671 // Log start motion time
pathfindr 51:9078e6928412 672 RET_motionStartTime = RET_RTCunixtime;
pathfindr 51:9078e6928412 673 }
pathfindr 51:9078e6928412 674 } else {
pathfindr 51:9078e6928412 675 RET_motionTriggeredinFrame = false;
pathfindr 51:9078e6928412 676 RET_motionPendingOnState = false;
pathfindr 51:9078e6928412 677 if (!RET_motionPendingOffState) {
pathfindr 51:9078e6928412 678 RET_motionPendingOffState = true;
pathfindr 51:9078e6928412 679 //log stop motion time
pathfindr 51:9078e6928412 680 RET_motionStopTime = RET_RTCunixtime;
pathfindr 51:9078e6928412 681 }
pathfindr 9:b0a1535b8ef2 682 }
pathfindr 51:9078e6928412 683 //calculate motion state
pathfindr 51:9078e6928412 684 if (RET_motionFrameStart == 0) {RET_motionFrameStart = RET_RTCunixtime;}
pathfindr 51:9078e6928412 685 if (RET_motionPendingOnState) {
pathfindr 51:9078e6928412 686 //check if above threshold
pathfindr 51:9078e6928412 687 time_t inMotionForSeconds = ((RET_RTCunixtime - RET_motionStartTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
pathfindr 52:bd7678eade77 688 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "M:%u",inMotionForSeconds);debug_exe();}
pathfindr 52:bd7678eade77 689 if (inMotionForSeconds >= RET_setting_motion_start_seconds) {
pathfindr 52:bd7678eade77 690 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MotionOn");debug_exe();}
pathfindr 51:9078e6928412 691 RET_motionState = true;
pathfindr 52:bd7678eade77 692 RET_motionStateOnInLocTXInterval = true;
pathfindr 52:bd7678eade77 693 if (RET_motionState == false) { //If this is first after motionState 0
pathfindr 52:bd7678eade77 694 if (RET_setting_activity_mode == 2 && RET_setting_activity_tx_interval_hrs > 0) {
pathfindr 52:bd7678eade77 695 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
pathfindr 52:bd7678eade77 696 sprintf(RET_activityData+strlen(RET_activityData),"1.%u!",epochOffsetMins);
pathfindr 52:bd7678eade77 697 }
pathfindr 45:6c7f5b556d74 698 }
pathfindr 23:a3b0ccf75ca5 699 }
pathfindr 9:b0a1535b8ef2 700 }
pathfindr 51:9078e6928412 701 if (RET_motionPendingOffState) {
pathfindr 51:9078e6928412 702 time_t noMotionForSeconds = ((RET_RTCunixtime - RET_motionStopTime) + (DEFAULT_SLEEP_FRAME / 1000)); //Plus DEFAULT_SLEEP_FRAME as it should include frame time
pathfindr 52:bd7678eade77 703 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "NM:%u",noMotionForSeconds);debug_exe();}
pathfindr 52:bd7678eade77 704 if (noMotionForSeconds >= RET_setting_motion_stop_seconds) {
pathfindr 52:bd7678eade77 705 if (RET_motionState == true) {
pathfindr 52:bd7678eade77 706 RET_motionPendingOffState = false;
pathfindr 52:bd7678eade77 707 RET_motionState = false;
pathfindr 52:bd7678eade77 708 GLOBAL_motionStopFlagTriggered = true;
pathfindr 52:bd7678eade77 709 //if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "MotionStop");debug_exe();}
pathfindr 52:bd7678eade77 710 if (RET_setting_activity_mode > 0 && RET_setting_activity_tx_interval_hrs > 0) {
pathfindr 52:bd7678eade77 711 RET_motionTotalActivityHoursSincePost += ((float(RET_motionStopTime)-float(RET_motionStartTime)) / 3600.0f);
pathfindr 52:bd7678eade77 712 RET_motionTotalActivityHours += RET_motionTotalActivityHoursSincePost;
pathfindr 52:bd7678eade77 713 if (RET_setting_activity_mode == 2) {
pathfindr 52:bd7678eade77 714 time_t epochOffsetMins = ((RET_RTCunixtime - RET_motionFrameStart) / 60);
pathfindr 52:bd7678eade77 715 sprintf(RET_activityData+strlen(RET_activityData),"0.%u!",epochOffsetMins);
pathfindr 52:bd7678eade77 716 }
pathfindr 51:9078e6928412 717 }
pathfindr 45:6c7f5b556d74 718 }
pathfindr 23:a3b0ccf75ca5 719 }
pathfindr 9:b0a1535b8ef2 720 }
pathfindr 9:b0a1535b8ef2 721 }
pathfindr 48:64b1613941d5 722 return RET_motionState;
pathfindr 8:7351f8c4cd60 723 }
pathfindr 8:7351f8c4cd60 724
pathfindr 14:9a54b1b65bc8 725 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 726 // UPDATE OPERATING SETTINGS
pathfindr 14:9a54b1b65bc8 727 //------------------------------------------------------------------------------
pathfindr 14:9a54b1b65bc8 728 bool saveSettings(char* settingsBuffer) {
pathfindr 18:22edaa7e74b1 729 int matchCount = 0;
pathfindr 19:22261767c87a 730 int critical_fail_count = 0;
pathfindr 32:dff4858bdf37 731 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;
pathfindr 51:9078e6928412 732 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; int TEMP_t = -1; char TEMP_u[7];
pathfindr 50:a94e70c00fff 733
pathfindr 51:9078e6928412 734 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "RESP: %s",settingsBuffer);debug_exe();}
pathfindr 50:a94e70c00fff 735
pathfindr 51:9078e6928412 736 //exampple
pathfindr 51:9078e6928412 737 //+CUSD: 0,”1#a:12,b:1558020598,c:-1,d:-1,e:1,f:2,g:5,h:168,i:90,j:168,k:0,l:8,m:60,n:60,o:0,p:0,q:120,r:0,s:0,t:1,u:F859C1#”,15
pathfindr 51:9078e6928412 738
pathfindr 51:9078e6928412 739 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,t:%d,u:%s",
pathfindr 51:9078e6928412 740 &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,&TEMP_t,&TEMP_u) ) > 0 ) {
pathfindr 48:64b1613941d5 741
pathfindr 51:9078e6928412 742 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "PARAMS: 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,t:%d,u:%s\n",
pathfindr 51:9078e6928412 743 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,TEMP_t,TEMP_u);debug_exe();}
pathfindr 34:4493c9f6d707 744
pathfindr 41:07e41dbd5304 745 if(TEMP_a != -1) { RET_setting_minimumupdate_hrs = TEMP_a; }
pathfindr 50:a94e70c00fff 746 if(TEMP_b > 1552915630) {
pathfindr 50:a94e70c00fff 747 if (RET_RTCunixtime < 1552915630) {
pathfindr 50:a94e70c00fff 748 //assume this is first proper unixtime and set
pathfindr 50:a94e70c00fff 749 RET_RTCunixtime = TEMP_b;
pathfindr 50:a94e70c00fff 750 } else {
pathfindr 51:9078e6928412 751 //this is not a first unixtime, so check within safe range, convert to long long first to avoid -1 rollover issue
pathfindr 51:9078e6928412 752 long long unixtime_in = TEMP_b;
pathfindr 51:9078e6928412 753 long long unixtime_current = RET_RTCunixtime;
pathfindr 51:9078e6928412 754 if ((unixtime_in - unixtime_current) < 604800) { //allow 1 week difference
pathfindr 50:a94e70c00fff 755 RET_RTCunixtime = TEMP_b;
pathfindr 50:a94e70c00fff 756 } else {
pathfindr 50:a94e70c00fff 757 critical_fail_count++;
pathfindr 51:9078e6928412 758 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "unixtime too far ahead. C:%u N:%u",RET_RTCunixtime,TEMP_b);debug_exe();}
pathfindr 50:a94e70c00fff 759 }
pathfindr 50:a94e70c00fff 760 }
pathfindr 50:a94e70c00fff 761 } else {
pathfindr 50:a94e70c00fff 762 critical_fail_count++;
pathfindr 51:9078e6928412 763 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "unixtime too low");debug_exe();}
pathfindr 50:a94e70c00fff 764 }
pathfindr 41:07e41dbd5304 765 if(TEMP_c != -1) { RET_setting_firmware = TEMP_c; } else { RET_setting_firmware = -1;}
pathfindr 41:07e41dbd5304 766 if(TEMP_d != -1) { setState(TEMP_d); } else { setState(STATE_NORMAL);}
pathfindr 41:07e41dbd5304 767 if(TEMP_e != -1) { RET_setting_location_mode = TEMP_e; }
pathfindr 52:bd7678eade77 768 if(RET_setting_location_mode > 4) {RET_setting_location_mode = DEFAULT_LOCATION_MODE;}
pathfindr 41:07e41dbd5304 769 if(TEMP_f != -1) { RET_setting_location_accuracy = TEMP_f; }
pathfindr 34:4493c9f6d707 770 if(RET_setting_location_accuracy > 3) {RET_setting_location_accuracy = DEFAULT_LOCATION_ACCURACY;}
pathfindr 41:07e41dbd5304 771 if(TEMP_g != -1) { RET_setting_location_tx_interval_mins = TEMP_g; }
pathfindr 41:07e41dbd5304 772 if(TEMP_h != -1) { RET_setting_location_tx_failsafe_hrs = TEMP_h; }
pathfindr 34:4493c9f6d707 773 if(RET_setting_location_tx_failsafe_hrs > 504) {RET_setting_location_tx_failsafe_hrs = DEFAULT_LOCATION_TX_FAILSAFE_HRS;}
pathfindr 41:07e41dbd5304 774 if(TEMP_i != -1) { RET_setting_location_timeout = TEMP_i; }
pathfindr 27:fa76f5a08195 775 if(RET_setting_location_timeout < 60 || RET_setting_location_timeout > 300) {RET_setting_location_timeout = DEFAULT_LOCATION_TIMEOUT;}
pathfindr 41:07e41dbd5304 776 if(TEMP_j != -1) { RET_setting_activity_tx_interval_hrs = TEMP_j; }
pathfindr 41:07e41dbd5304 777 if(TEMP_k != -1) { RET_setting_environmental_tx_interval_mins = TEMP_k; }
pathfindr 41:07e41dbd5304 778 if(TEMP_l != -1) { RET_setting_motion_g = TEMP_l; }
pathfindr 43:7b232f03628f 779 if(RET_setting_motion_g < 4 || RET_setting_motion_g > 127) {RET_setting_motion_g = DEFAULT_MOTION_G;}
pathfindr 41:07e41dbd5304 780 if(TEMP_m != -1) { RET_setting_motion_start_seconds = TEMP_m; }
pathfindr 41:07e41dbd5304 781 if(TEMP_n != -1) { RET_setting_motion_stop_seconds = TEMP_n; }
pathfindr 41:07e41dbd5304 782 if(TEMP_o != -1) { RET_setting_impact_g = TEMP_o; }
pathfindr 41:07e41dbd5304 783 if(TEMP_p != -1) { RET_setting_impact_alert = TEMP_p; }
pathfindr 41:07e41dbd5304 784 if(TEMP_q != -1) { RET_setting_connection_timeout = TEMP_q; }
pathfindr 47:cc6d4d0bf897 785 if(RET_setting_connection_timeout < 60 || RET_setting_connection_timeout > 240) { RET_setting_connection_timeout = DEFAULT_CONNECTION_TIMEOUT; }
pathfindr 41:07e41dbd5304 786 if(TEMP_r != -1) { RET_setting_beacon_interval_seconds = TEMP_r; }
pathfindr 41:07e41dbd5304 787 if(TEMP_s != -1) { RET_setting_beacon_scan = TEMP_s; }
pathfindr 45:6c7f5b556d74 788 if(TEMP_t != -1) { RET_setting_activity_mode = TEMP_t; }
pathfindr 51:9078e6928412 789 if(TEMP_u[0] != 0x00 && TEMP_u[1] != 0x00 && TEMP_u[2] != 0x00) { strcpy(RET_pf_identifier,TEMP_u); }
pathfindr 45:6c7f5b556d74 790
pathfindr 19:22261767c87a 791 if (critical_fail_count == 0) {
pathfindr 45:6c7f5b556d74 792 RET_receivedNewSettings = true;
pathfindr 51:9078e6928412 793 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "NEW SETTINGS");debug_exe();}
pathfindr 40:4270e09e59f2 794 dumpSettings();
pathfindr 32:dff4858bdf37 795 RET_haveSettings = true;
pathfindr 34:4493c9f6d707 796 GLOBAL_needToConfigureLis3dh = true;
pathfindr 32:dff4858bdf37 797 RET_SettingsGotAt = RET_RTCunixtime;
pathfindr 37:505ef618f06c 798 //check for firmware update
pathfindr 38:476a9b5629a1 799 if (RET_setting_firmware > 0 && RET_setting_firmware != FW_VERSION) {
pathfindr 48:64b1613941d5 800 //Broadcast any activity data we have before fw update
pathfindr 48:64b1613941d5 801 event_activity_tx();
pathfindr 48:64b1613941d5 802 //prep for FW update
pathfindr 52:bd7678eade77 803 #if BLE_ENABLED
pathfindr 52:bd7678eade77 804 bleShutdown();
pathfindr 52:bd7678eade77 805 #endif
pathfindr 37:505ef618f06c 806 read_app_data_from_flash(&app_data);
pathfindr 45:6c7f5b556d74 807 clr_flag(&app_data, app_execution_flag);
pathfindr 45:6c7f5b556d74 808 clr_flag(&app_data, first_run_flag);
pathfindr 45:6c7f5b556d74 809 app_data.current_firmware_version = FW_VERSION;
pathfindr 37:505ef618f06c 810 app_data.target_firmware_version = RET_setting_firmware;
pathfindr 37:505ef618f06c 811 write_app_data_to_flash(&app_data);
pathfindr 51:9078e6928412 812 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "OTA\n");debug_exe();}
pathfindr 48:64b1613941d5 813 ThisThread::sleep_for(200);
pathfindr 37:505ef618f06c 814 system_reset();
pathfindr 37:505ef618f06c 815 }
pathfindr 19:22261767c87a 816 return true;
pathfindr 19:22261767c87a 817 } else {
pathfindr 44:061509d1b38c 818 addToExceptionString("SCF");
pathfindr 19:22261767c87a 819 return false;
pathfindr 19:22261767c87a 820 }
pathfindr 14:9a54b1b65bc8 821 } else {
pathfindr 44:061509d1b38c 822 addToExceptionString("SPE");
pathfindr 15:7aad9a7f970c 823 return false;
pathfindr 14:9a54b1b65bc8 824 }
pathfindr 14:9a54b1b65bc8 825 }
pathfindr 24:c161db07557f 826
pathfindr 41:07e41dbd5304 827 //------------------------------------------------------------------------------
pathfindr 41:07e41dbd5304 828 // SET EVENT TIMES
pathfindr 41:07e41dbd5304 829 //------------------------------------------------------------------------------
pathfindr 41:07e41dbd5304 830 void setEventTime_Location() {
pathfindr 20:5404841fdd2b 831 if(RET_setting_location_tx_interval_mins > 0) {
pathfindr 20:5404841fdd2b 832 RET_eventTime_location_tx = (RET_RTCunixtime + (RET_setting_location_tx_interval_mins * 60));
pathfindr 51:9078e6928412 833 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - LOC TX @ %u, MODE %d",RET_eventTime_location_tx, RET_setting_location_mode);debug_exe();}
pathfindr 20:5404841fdd2b 834 }
pathfindr 20:5404841fdd2b 835 if(RET_setting_location_tx_failsafe_hrs > 0) {
pathfindr 20:5404841fdd2b 836 RET_eventTime_location_failsafe_tx = (RET_RTCunixtime + (RET_setting_location_tx_failsafe_hrs * 3600));
pathfindr 51:9078e6928412 837 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - LOC FS TX @ %u",RET_eventTime_location_failsafe_tx);debug_exe();}
pathfindr 20:5404841fdd2b 838 }
pathfindr 41:07e41dbd5304 839 }
pathfindr 41:07e41dbd5304 840 void setEventTime_Activity() {
pathfindr 22:810425eb76e1 841 if(RET_setting_activity_tx_interval_hrs > 0) {
pathfindr 22:810425eb76e1 842 RET_eventTime_activity_tx = (RET_RTCunixtime + (RET_setting_activity_tx_interval_hrs * 3600));
pathfindr 51:9078e6928412 843 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - ACT TX @ %u",RET_eventTime_activity_tx);debug_exe();}
pathfindr 20:5404841fdd2b 844 }
pathfindr 41:07e41dbd5304 845 }
pathfindr 41:07e41dbd5304 846 void setEventTime_Environmental() {
pathfindr 20:5404841fdd2b 847 if(RET_eventTime_environmental_tx > 0) {
pathfindr 20:5404841fdd2b 848 RET_eventTime_environmental_tx = (RET_RTCunixtime + (RET_setting_environmental_tx_interval_mins * 60));
pathfindr 51:9078e6928412 849 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "EVTSET - ENV TX @ %u",RET_eventTime_environmental_tx);debug_exe();}
pathfindr 34:4493c9f6d707 850 }
pathfindr 20:5404841fdd2b 851 }
pathfindr 41:07e41dbd5304 852 void setEventTimes() {
pathfindr 43:7b232f03628f 853 //SET ALL EVENT TIMES
pathfindr 41:07e41dbd5304 854 setEventTime_Location();
pathfindr 41:07e41dbd5304 855 setEventTime_Activity();
pathfindr 41:07e41dbd5304 856 setEventTime_Environmental();
pathfindr 41:07e41dbd5304 857 }
pathfindr 13:29f67f256709 858
pathfindr 13:29f67f256709 859 //------------------------------------------------------------------------------
pathfindr 40:4270e09e59f2 860 // ERROR LOG
pathfindr 40:4270e09e59f2 861 //------------------------------------------------------------------------------
pathfindr 40:4270e09e59f2 862 void sendErrorLog(char* error) {
pathfindr 51:9078e6928412 863 updateBatteryV();
pathfindr 40:4270e09e59f2 864 int timetaken = (RET_RTCunixtime - GLOBAL_wakeTime);
pathfindr 40:4270e09e59f2 865 char bytestosend[100];
pathfindr 40:4270e09e59f2 866 memset(bytestosend,0x00,sizeof(bytestosend));
pathfindr 52:bd7678eade77 867 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,v:%.2f,e:%d,z:%s.%s,s:0)\0",GLOBAL_defaultApi,FW_VERSION,RET_voltage,timetaken,error,GLOBAL_exceptionString);
pathfindr 43:7b232f03628f 868 if (modem.on(RET_force2G)) {
pathfindr 48:64b1613941d5 869 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 51:9078e6928412 870 modem.USSDmessage(bytestosend, false, GLOBAL_defaultApi);
pathfindr 43:7b232f03628f 871 }
pathfindr 43:7b232f03628f 872 }
pathfindr 40:4270e09e59f2 873 ThisThread::sleep_for(250);
pathfindr 40:4270e09e59f2 874 }
pathfindr 40:4270e09e59f2 875
pathfindr 40:4270e09e59f2 876 //------------------------------------------------------------------------------
pathfindr 43:7b232f03628f 877 // EVENTS
pathfindr 13:29f67f256709 878 //------------------------------------------------------------------------------
pathfindr 51:9078e6928412 879 void failed_broadcast_log(char* eventstring) {
pathfindr 51:9078e6928412 880 //First available free slot
pathfindr 51:9078e6928412 881 bool saved = false;
pathfindr 51:9078e6928412 882 for(int i = 0; i < sizeof(GLOBAL_failed_broadcast_slots); i++) {
pathfindr 51:9078e6928412 883 if (GLOBAL_failed_broadcast_slots[i] == false) {
pathfindr 51:9078e6928412 884 memset(GLOBAL_failed_broadcasts[i],0x00,sizeof(GLOBAL_failed_broadcasts[i])); //clear row
pathfindr 51:9078e6928412 885 snprintf(GLOBAL_failed_broadcasts[i],sizeof(GLOBAL_failed_broadcasts[i]),"%s",eventstring); //save new row
pathfindr 51:9078e6928412 886 GLOBAL_failed_broadcast_slots[i] = true;
pathfindr 51:9078e6928412 887 saved = true;
pathfindr 51:9078e6928412 888 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Failed Broadcast saved: %s",eventstring);debug_exe();}
pathfindr 51:9078e6928412 889 break;
pathfindr 51:9078e6928412 890 }
pathfindr 51:9078e6928412 891 }
pathfindr 51:9078e6928412 892 if (!saved) {
pathfindr 51:9078e6928412 893 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Failed Broadcast save fail");debug_exe();}
pathfindr 51:9078e6928412 894 }
pathfindr 51:9078e6928412 895 return;
pathfindr 51:9078e6928412 896 }
pathfindr 51:9078e6928412 897 void failed_broadcasts_tx() {
pathfindr 52:bd7678eade77 898 #if BLE_ENABLED
pathfindr 52:bd7678eade77 899 bleStopAdvertising();
pathfindr 52:bd7678eade77 900 #endif
pathfindr 51:9078e6928412 901 //check we have something to send...
pathfindr 51:9078e6928412 902 int numbertosend = 0;
pathfindr 51:9078e6928412 903 for(int i = 0; i < sizeof(GLOBAL_failed_broadcast_slots); i++) {
pathfindr 51:9078e6928412 904 if (GLOBAL_failed_broadcast_slots[i] == true) {
pathfindr 51:9078e6928412 905 numbertosend ++;
pathfindr 51:9078e6928412 906 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Retry:%s",GLOBAL_failed_broadcasts[i]);debug_exe();}
pathfindr 51:9078e6928412 907 }
pathfindr 51:9078e6928412 908 }
pathfindr 51:9078e6928412 909 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Failed Broadcasts to send:%d",numbertosend);debug_exe();}
pathfindr 51:9078e6928412 910 for(int i = 0; i < sizeof(GLOBAL_failed_broadcast_slots); i++) {
pathfindr 51:9078e6928412 911 if (GLOBAL_failed_broadcast_slots[i] == true) {
pathfindr 51:9078e6928412 912 if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) {
pathfindr 51:9078e6928412 913 if (modem.on(RET_force2G)) {
pathfindr 51:9078e6928412 914 if (modem.registerOnNetwork(2,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 51:9078e6928412 915 char result[100];
pathfindr 51:9078e6928412 916 bool getResponse = false;
pathfindr 51:9078e6928412 917 snprintf(result,sizeof(result),"%s",modem.USSDmessage(GLOBAL_failed_broadcasts[i], getResponse, GLOBAL_defaultApi));
pathfindr 51:9078e6928412 918 if (strcmp(result, "sendok") == 0) {
pathfindr 51:9078e6928412 919 //clear row
pathfindr 51:9078e6928412 920 memset(GLOBAL_failed_broadcasts[i],0x00,sizeof(GLOBAL_failed_broadcasts[i]));
pathfindr 51:9078e6928412 921 GLOBAL_failed_broadcast_slots[i] = false;
pathfindr 51:9078e6928412 922 }
pathfindr 51:9078e6928412 923 }
pathfindr 51:9078e6928412 924 }
pathfindr 51:9078e6928412 925 }
pathfindr 51:9078e6928412 926 }
pathfindr 51:9078e6928412 927 }
pathfindr 51:9078e6928412 928 }
pathfindr 43:7b232f03628f 929 bool event_setup(bool manualrun) {
pathfindr 52:bd7678eade77 930 #if BLE_ENABLED
pathfindr 52:bd7678eade77 931 bleStopAdvertising();
pathfindr 52:bd7678eade77 932 #endif
pathfindr 51:9078e6928412 933 bool pass = false;
pathfindr 49:15ddd84ec9fa 934 LED1on(0);
pathfindr 48:64b1613941d5 935 if (RET_NetworkFailCount > DEFAULT_MAX_FAILED_CONNECTIONS) addToExceptionString("NF");
pathfindr 48:64b1613941d5 936 if (RET_GPSFailCount > DEFAULT_MAX_FAILED_GPS) addToExceptionString("GF");
pathfindr 41:07e41dbd5304 937 RET_setupInProgress = true;
pathfindr 48:64b1613941d5 938 RET_NetworkFailCount = 0;
pathfindr 48:64b1613941d5 939 RET_GPSFailCount = 0;
pathfindr 49:15ddd84ec9fa 940 RET_setting_connection_timeout = 180; //reset to longer value to setup run to help with connection
pathfindr 25:7adeb1a53360 941 bool selftestresult = selfTest();
pathfindr 45:6c7f5b556d74 942 int connectionStartTime = RET_RTCunixtime;
pathfindr 38:476a9b5629a1 943 if (modem.on(RET_force2G)) {
pathfindr 48:64b1613941d5 944 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 45:6c7f5b556d74 945 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for ussd response time.
pathfindr 45:6c7f5b556d74 946 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 43:7b232f03628f 947 if (manualrun) addToExceptionString("MAN");
pathfindr 17:ba55c026b1d6 948 char bytestosend[160];
pathfindr 52:bd7678eade77 949 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:setup,f:%d,v:%.2f,t:%.1f,e:%d,y:%d,z:SETUP-%s,k:%s,m:%s,s:1)\0",GLOBAL_defaultApi,FW_VERSION,RET_voltage,RET_temperature,timetaken_total,timetaken_connection,GLOBAL_exceptionString,SKU,HW_MAJORREVISION);
pathfindr 34:4493c9f6d707 950 char result[200];
pathfindr 51:9078e6928412 951 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, true, GLOBAL_defaultApi));
pathfindr 49:15ddd84ec9fa 952 RET_setupInProgress = false; //this turns off the flashing led
pathfindr 51:9078e6928412 953 if (strcmp(result, "sendokrecfail") == 0) {
pathfindr 51:9078e6928412 954 //send ok but receive fail
pathfindr 51:9078e6928412 955 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Ok but Rec fail");debug_exe();}
pathfindr 51:9078e6928412 956 sendErrorLog("ERR-SU-NR");
pathfindr 51:9078e6928412 957 } else if (strcmp(result, "sendfail") == 0) {
pathfindr 51:9078e6928412 958 //do nothing
pathfindr 51:9078e6928412 959 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Fail");debug_exe();}
pathfindr 51:9078e6928412 960 } else {
pathfindr 51:9078e6928412 961 if (saveSettings(result) == true) {
pathfindr 51:9078e6928412 962 pass = true;
pathfindr 51:9078e6928412 963 } else {
pathfindr 19:22261767c87a 964 //something went critically wrong getting settings
pathfindr 48:64b1613941d5 965 sendErrorLog("ERR-SU-IR");
pathfindr 51:9078e6928412 966 }
pathfindr 16:3bf5f1a5f869 967 }
pathfindr 13:29f67f256709 968 }
pathfindr 13:29f67f256709 969 } else {
pathfindr 19:22261767c87a 970 //FAILUREMODE Modem failed to turn on
pathfindr 51:9078e6928412 971 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"Modem Fail");debug_exe();}
pathfindr 13:29f67f256709 972 }
pathfindr 49:15ddd84ec9fa 973
pathfindr 49:15ddd84ec9fa 974 //IF WE PASSED THEN LETS DO OTHER POSTS
pathfindr 49:15ddd84ec9fa 975 if (pass == true) {
pathfindr 49:15ddd84ec9fa 976 GLOBAL_wakeTime = RET_RTCunixtime; //lets reset this here so that the following loc and act function have sensible values for total time
pathfindr 49:15ddd84ec9fa 977 //GET LOC DATA
pathfindr 52:bd7678eade77 978 //uint8_t previous_location_accuracy = RET_setting_location_accuracy;
pathfindr 52:bd7678eade77 979 //RET_setting_location_accuracy = 1; //set location mode to CL only
pathfindr 51:9078e6928412 980 event_location_tx(false);
pathfindr 52:bd7678eade77 981 //RET_setting_location_accuracy = previous_location_accuracy; //set location mode back to previous
pathfindr 49:15ddd84ec9fa 982 //SEND ANY ACTIVITY DATA
pathfindr 49:15ddd84ec9fa 983 event_activity_tx();
pathfindr 49:15ddd84ec9fa 984 LED1blink(4,500); //PASS AND END
pathfindr 49:15ddd84ec9fa 985 }
pathfindr 49:15ddd84ec9fa 986
pathfindr 49:15ddd84ec9fa 987 //TIDY UP = LOG RUN TIME - THIS MUST GO AT END AFTER WE HAVE GOT SERVER TIMESTAMP
pathfindr 21:e0b866630c27 988 RET_SetupRunAt = RET_RTCunixtime;
pathfindr 49:15ddd84ec9fa 989 RET_GPSFailCount = 0;
pathfindr 49:15ddd84ec9fa 990 RET_NetworkFailCount = 0;
pathfindr 52:bd7678eade77 991 setEventTimes();
pathfindr 13:29f67f256709 992 //RESULT
pathfindr 19:22261767c87a 993 return pass;
pathfindr 13:29f67f256709 994 }
pathfindr 39:f767b8037475 995 void event_turnonofflog_tx(bool turnon) {
pathfindr 52:bd7678eade77 996 #if BLE_ENABLED
pathfindr 52:bd7678eade77 997 bleStopAdvertising();
pathfindr 52:bd7678eade77 998 #endif
pathfindr 48:64b1613941d5 999 RET_NetworkFailCount = 0; //reset network blocker
pathfindr 48:64b1613941d5 1000 RET_GPSFailCount = 0;
pathfindr 51:9078e6928412 1001 updateBatteryV();
pathfindr 45:6c7f5b556d74 1002 int connectionStartTime = RET_RTCunixtime;
pathfindr 39:f767b8037475 1003 if (modem.on(RET_force2G)) {
pathfindr 39:f767b8037475 1004 char locString[70];
pathfindr 45:6c7f5b556d74 1005 int gpsStartTime = RET_RTCunixtime;
pathfindr 48:64b1613941d5 1006 memcpy(locString, modem.getLocation(1, RET_setting_location_timeout, RET_GPSFailCount, RET_NetworkFailCount), sizeof(locString));
pathfindr 45:6c7f5b556d74 1007 int timetaken_gps = (RET_RTCunixtime - gpsStartTime);
pathfindr 48:64b1613941d5 1008 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 45:6c7f5b556d74 1009 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime);
pathfindr 45:6c7f5b556d74 1010 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 39:f767b8037475 1011 char bytestosend[160];
pathfindr 39:f767b8037475 1012 if (turnon) {
pathfindr 52:bd7678eade77 1013 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,t:%.1f,v:%.2f,z:TURNON,e:%d,y:%d,x:%d,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_voltage,timetaken_total,timetaken_connection,timetaken_gps,locString);
pathfindr 39:f767b8037475 1014 } else {
pathfindr 52:bd7678eade77 1015 snprintf(bytestosend,sizeof(bytestosend),"(%s,a:log,f:%d,t:%.1f,v:%.2f,z:TURNOFF,e:%d,y:%d,x:%d,s:1%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_voltage,timetaken_total,timetaken_connection,timetaken_gps,locString);
pathfindr 39:f767b8037475 1016 }
pathfindr 39:f767b8037475 1017 char result[180];
pathfindr 51:9078e6928412 1018 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytestosend, false, GLOBAL_defaultApi));
pathfindr 39:f767b8037475 1019 }
pathfindr 39:f767b8037475 1020 }
pathfindr 39:f767b8037475 1021 }
pathfindr 51:9078e6928412 1022 void event_location_tx(bool failsafe) {
pathfindr 52:bd7678eade77 1023 #if BLE_ENABLED
pathfindr 52:bd7678eade77 1024 bleStopAdvertising();
pathfindr 52:bd7678eade77 1025 #endif
pathfindr 51:9078e6928412 1026 //check if we have any outstanding to send
pathfindr 51:9078e6928412 1027 failed_broadcasts_tx();
pathfindr 49:15ddd84ec9fa 1028 bool selfTestResult = selfTest();
pathfindr 52:bd7678eade77 1029 bool skipTX = false;
pathfindr 51:9078e6928412 1030 char bytesToSend[160];
pathfindr 52:bd7678eade77 1031
pathfindr 52:bd7678eade77 1032 //Check for Nearby Location Beacons
pathfindr 52:bd7678eade77 1033 #if BLE_ENABLED
pathfindr 52:bd7678eade77 1034 memset(RET_closestLocationTag_id, 0x00, sizeof(RET_closestLocationTag_id));
pathfindr 52:bd7678eade77 1035 RET_closestLocationTag_rssi = 999;
pathfindr 52:bd7678eade77 1036 RET_locationTag_present = false;
pathfindr 52:bd7678eade77 1037 RET_detector_present = false;
pathfindr 52:bd7678eade77 1038 if (RET_setting_beacon_scan == 1 && RET_setting_location_mode != LOCATION_MODE_REALTIME) {
pathfindr 52:bd7678eade77 1039 LowPowerTimer bleScan_t;
pathfindr 52:bd7678eade77 1040 bleScan_t.start();
pathfindr 52:bd7678eade77 1041 uint32_t bleScan_startmillis = bleScan_t.read_ms(); uint32_t bleScan_runtime = 0;
pathfindr 52:bd7678eade77 1042 if (!myble.hasInitialized()) {
pathfindr 52:bd7678eade77 1043 myble.init();
pathfindr 52:bd7678eade77 1044 while (!myble.hasInitialized() && bleScan_runtime < 1000) {
pathfindr 52:bd7678eade77 1045 bleScan_runtime = (bleScan_t.read_ms() - bleScan_startmillis);
pathfindr 52:bd7678eade77 1046 }
pathfindr 52:bd7678eade77 1047 }
pathfindr 52:bd7678eade77 1048 if (myble.hasInitialized()) {
pathfindr 52:bd7678eade77 1049 //Scan for 10 seconds
pathfindr 52:bd7678eade77 1050 bleConfigureScan();
pathfindr 52:bd7678eade77 1051 bleScan_startmillis = bleScan_t.read_ms();
pathfindr 52:bd7678eade77 1052 while (bleScan_runtime < 10000 && RET_detector_present == false) {
pathfindr 52:bd7678eade77 1053 bleScan_runtime = (bleScan_t.read_ms() - bleScan_startmillis);
pathfindr 52:bd7678eade77 1054 myble.waitForEvent(); // low power wait for event
pathfindr 52:bd7678eade77 1055 }
pathfindr 52:bd7678eade77 1056 //if (LocationTag_preset) if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "LocationTag: %s, rssi: %d",closestLocationTag_id, closestLocationTag_rssi);debug_exe();}
pathfindr 52:bd7678eade77 1057 }
pathfindr 52:bd7678eade77 1058 bleScan_t.stop();
pathfindr 52:bd7678eade77 1059 myble.gap().stopScan();
pathfindr 52:bd7678eade77 1060 }
pathfindr 52:bd7678eade77 1061 //see if we are in range of ble detector, if so, no need to get gps and broadcast
pathfindr 52:bd7678eade77 1062 if (RET_detector_present == true && RET_setting_beacon_interval_seconds > 0) {
pathfindr 52:bd7678eade77 1063 skipTX = true;
pathfindr 52:bd7678eade77 1064 }
pathfindr 52:bd7678eade77 1065 #endif
pathfindr 52:bd7678eade77 1066
pathfindr 52:bd7678eade77 1067
pathfindr 52:bd7678eade77 1068 if (skipTX == false || failsafe == true) {
pathfindr 52:bd7678eade77 1069 //Set any network or GPS fail flags
pathfindr 52:bd7678eade77 1070 if (RET_NetworkFailCount > DEFAULT_MAX_FAILED_CONNECTIONS) addToExceptionString("NF");
pathfindr 52:bd7678eade77 1071 if (RET_GPSFailCount > DEFAULT_MAX_FAILED_GPS) addToExceptionString("GF");
pathfindr 52:bd7678eade77 1072 //lets check to see if we've had repeated comms failings in this location, if so dont bother trying. reset by movment and timeout
pathfindr 52:bd7678eade77 1073 if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) {
pathfindr 52:bd7678eade77 1074 if (RET_receivedNewSettings) {RET_receivedNewSettings = false; addToExceptionString("SR"); }
pathfindr 52:bd7678eade77 1075 if (modem.on(RET_force2G)) {
pathfindr 52:bd7678eade77 1076 char locString[70];
pathfindr 52:bd7678eade77 1077 int gpsStartTime = RET_RTCunixtime;
pathfindr 52:bd7678eade77 1078
pathfindr 52:bd7678eade77 1079 //check if we have moved since last post, if not use same valid gps fix if exists
pathfindr 52:bd7678eade77 1080 if (RET_motionStateOnInLocTXInterval == false && GLOBAL_have_GPSlocString_prev == true) {
pathfindr 52:bd7678eade77 1081 memcpy(locString, GLOBAL_GPSlocString_prev, sizeof(locString));
pathfindr 52:bd7678eade77 1082 addToExceptionString("P");
pathfindr 51:9078e6928412 1083 } else {
pathfindr 52:bd7678eade77 1084 memcpy(locString, modem.getLocation(RET_setting_location_accuracy, RET_setting_location_timeout, RET_GPSFailCount, RET_NetworkFailCount), sizeof(locString));
pathfindr 52:bd7678eade77 1085 }
pathfindr 52:bd7678eade77 1086
pathfindr 52:bd7678eade77 1087 int timetaken_gps = (RET_RTCunixtime - gpsStartTime);
pathfindr 52:bd7678eade77 1088
pathfindr 52:bd7678eade77 1089 //SEND DATA
pathfindr 52:bd7678eade77 1090 int connectionStartTime = RET_RTCunixtime;
pathfindr 52:bd7678eade77 1091 if (modem.registerOnNetwork(DEFAULT_CONNECTION_ATTEMPTS,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 52:bd7678eade77 1092 int timetaken_total = (RET_RTCunixtime - GLOBAL_wakeTime) + 10; //add 10 for avg ussd response time.
pathfindr 52:bd7678eade77 1093 int timetaken_connection = (RET_RTCunixtime - connectionStartTime);
pathfindr 52:bd7678eade77 1094 //Check if we should wait for settings back
pathfindr 52:bd7678eade77 1095 bool getSettings = true;
pathfindr 52:bd7678eade77 1096 //work out if we need to get settings back
pathfindr 52:bd7678eade77 1097 if ( ((RET_RTCunixtime - RET_SettingsGotAt)/3600) < RET_setting_minimumupdate_hrs ) { getSettings = false; timetaken_total -= 10;} //remove the extra 10 seconds from times
pathfindr 52:bd7678eade77 1098 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.0f,n:%.0f,q:%.0f,v:%.1f,z:L%s,e:%d,y:%d,x:%d,j:%d,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_temperature_min,RET_temperature_max,RET_voltage,GLOBAL_exceptionString,timetaken_total,timetaken_connection,timetaken_gps,RET_motionStateOnInLocTXInterval,getSettings,locString);
pathfindr 52:bd7678eade77 1099 //snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:loc,f:%d,t:%.0f,v:%.1f,z:L-%s,e:%d,y:%d,x:%d,j:%d,s:%d%s)\0",GLOBAL_defaultApi,FW_VERSION,RET_temperature,RET_voltage,GLOBAL_exceptionString,timetaken_total,timetaken_connection,timetaken_gps,RET_motionStateOnInLocTXInterval,getSettings,locString);
pathfindr 52:bd7678eade77 1100 char result[180];
pathfindr 52:bd7678eade77 1101 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getSettings, GLOBAL_defaultApi));
pathfindr 52:bd7678eade77 1102 if (getSettings == true && strcmp(result, "sendokrecfail") == 0) {
pathfindr 52:bd7678eade77 1103 //send ok but receive fail
pathfindr 52:bd7678eade77 1104 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Ok but Rec fail");debug_exe();}
pathfindr 52:bd7678eade77 1105 //sendErrorLog("ERR-LOC-NR");
pathfindr 52:bd7678eade77 1106 } else if (strcmp(result, "sendok") == 0) {
pathfindr 52:bd7678eade77 1107 //do nothing
pathfindr 52:bd7678eade77 1108 } else if (strcmp(result, "sendfail") == 0) {
pathfindr 52:bd7678eade77 1109 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"USSD Send Fail");debug_exe();}
pathfindr 52:bd7678eade77 1110 //need to log for retry
pathfindr 52:bd7678eade77 1111 } else {
pathfindr 52:bd7678eade77 1112 if (getSettings) {
pathfindr 52:bd7678eade77 1113 if (saveSettings(result) == false){sendErrorLog("ERR-LOC-IR");}
pathfindr 52:bd7678eade77 1114 }
pathfindr 48:64b1613941d5 1115 }
pathfindr 22:810425eb76e1 1116 }
pathfindr 22:810425eb76e1 1117 }
pathfindr 22:810425eb76e1 1118 }
pathfindr 20:5404841fdd2b 1119 }
pathfindr 20:5404841fdd2b 1120 //RESETS
pathfindr 52:bd7678eade77 1121 RET_motionStateOnInLocTXInterval = false;
pathfindr 41:07e41dbd5304 1122 setEventTime_Location();
pathfindr 19:22261767c87a 1123 }
pathfindr 51:9078e6928412 1124 void event_activity_tx() {
pathfindr 52:bd7678eade77 1125 #if BLE_ENABLED
pathfindr 52:bd7678eade77 1126 bleStopAdvertising();
pathfindr 52:bd7678eade77 1127 #endif
pathfindr 51:9078e6928412 1128 //check if we have any outstanding to send
pathfindr 51:9078e6928412 1129 failed_broadcasts_tx();
pathfindr 51:9078e6928412 1130 //check we have something to send...
pathfindr 51:9078e6928412 1131 bool sendok = false;
pathfindr 51:9078e6928412 1132 if (RET_motionTotalActivityHoursSincePost > 0.0f && RET_setting_activity_mode > 0) {
pathfindr 51:9078e6928412 1133 //Build data to send
pathfindr 51:9078e6928412 1134 char bytesToSend[160];
pathfindr 51:9078e6928412 1135 if (RET_setting_activity_mode == 1) {
pathfindr 52:bd7678eade77 1136 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,t:%u,r:%.2f,h:%.2f)\0",GLOBAL_defaultApi,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost,RET_motionTotalActivityHours);
pathfindr 51:9078e6928412 1137 } else if (RET_setting_activity_mode == 2) {
pathfindr 52:bd7678eade77 1138 snprintf(bytesToSend,sizeof(bytesToSend),"(%s,a:act,e:%s,t:%u,r:%.2f)\0",GLOBAL_defaultApi,RET_activityData,RET_motionFrameStart,RET_motionTotalActivityHoursSincePost);
pathfindr 51:9078e6928412 1139 }
pathfindr 51:9078e6928412 1140 if (RET_NetworkFailCount <= DEFAULT_MAX_FAILED_CONNECTIONS) {
pathfindr 51:9078e6928412 1141 if (modem.on(RET_force2G)) {
pathfindr 51:9078e6928412 1142 //SEND DATA
pathfindr 51:9078e6928412 1143 if (modem.registerOnNetwork(2,RET_setting_connection_timeout,RET_NetworkFailCount)) {
pathfindr 51:9078e6928412 1144 char result[100];
pathfindr 51:9078e6928412 1145 bool getResponse = false;
pathfindr 51:9078e6928412 1146 snprintf(result,sizeof(result),"%s",modem.USSDmessage(bytesToSend, getResponse, GLOBAL_defaultApi));
pathfindr 51:9078e6928412 1147 if (strcmp(result, "sendok") == 0) {
pathfindr 51:9078e6928412 1148 sendok = true;
pathfindr 51:9078e6928412 1149 //if(!RET_debug) {sendok = true;} //FOR TESTING
pathfindr 51:9078e6928412 1150 }
pathfindr 51:9078e6928412 1151 }
pathfindr 51:9078e6928412 1152 }
pathfindr 51:9078e6928412 1153 }
pathfindr 51:9078e6928412 1154 //If send failed then log it for retry - only need to do this for mode 2, for mode the time just increments
pathfindr 51:9078e6928412 1155 if (sendok == true) {
pathfindr 51:9078e6928412 1156 if (RET_setting_activity_mode == 1) {
pathfindr 51:9078e6928412 1157 RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 51:9078e6928412 1158 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
pathfindr 51:9078e6928412 1159 } else if (RET_setting_activity_mode == 2) {
pathfindr 51:9078e6928412 1160 RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 51:9078e6928412 1161 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
pathfindr 51:9078e6928412 1162 }
pathfindr 51:9078e6928412 1163 } else if (sendok == false) {
pathfindr 51:9078e6928412 1164 if (RET_setting_activity_mode == 1) {
pathfindr 51:9078e6928412 1165 //do nothing
pathfindr 51:9078e6928412 1166 } else if (RET_setting_activity_mode == 2) {
pathfindr 51:9078e6928412 1167 failed_broadcast_log(bytesToSend);
pathfindr 51:9078e6928412 1168 RET_motionTotalActivityHoursSincePost = 0.0f;
pathfindr 51:9078e6928412 1169 if (RET_haveSettings) { RET_motionFrameStart = RET_RTCunixtime; }
pathfindr 51:9078e6928412 1170 }
pathfindr 51:9078e6928412 1171 }
pathfindr 51:9078e6928412 1172 }
pathfindr 51:9078e6928412 1173 //RESETS
pathfindr 51:9078e6928412 1174 memset(RET_activityData,0x00,sizeof(RET_activityData));
pathfindr 51:9078e6928412 1175 setEventTime_Activity();
pathfindr 48:64b1613941d5 1176 }
pathfindr 24:c161db07557f 1177
pathfindr 7:e9a19750700d 1178 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 1179 // STATE ENGINE
pathfindr 7:e9a19750700d 1180 //------------------------------------------------------------------------------
pathfindr 6:388d3c7efdd9 1181 void mainStateEngine() {
pathfindr 16:3bf5f1a5f869 1182 switch(RET_state) {
pathfindr 16:3bf5f1a5f869 1183 case STATE_SETUP :
pathfindr 34:4493c9f6d707 1184 { //need the curlies to avoid "transfer of control bypass init error warning"
pathfindr 48:64b1613941d5 1185 RET_busy = true;
pathfindr 41:07e41dbd5304 1186 //check that we havent run setup too recently
pathfindr 41:07e41dbd5304 1187 time_t setupRunAt_delta = (RET_RTCunixtime - RET_SetupRunAt);
pathfindr 49:15ddd84ec9fa 1188 if (RET_SetupRunAt == 0 || setupRunAt_delta >= ONEDAYINSECONDS) {
pathfindr 51:9078e6928412 1189 updateBatteryV();
pathfindr 43:7b232f03628f 1190 if (event_setup(false)) {
pathfindr 41:07e41dbd5304 1191 // All good
pathfindr 41:07e41dbd5304 1192 setState(STATE_NORMAL);
pathfindr 41:07e41dbd5304 1193 } else {
pathfindr 41:07e41dbd5304 1194 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + ONEDAYINSECONDS);
pathfindr 41:07e41dbd5304 1195 setState(STATE_DORMANT);
pathfindr 51:9078e6928412 1196 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SETUP FAILED:DORMANT until %u\n",RET_eventTime_wakeFromDormant);debug_exe();}
pathfindr 41:07e41dbd5304 1197 }
pathfindr 19:22261767c87a 1198 } else {
pathfindr 41:07e41dbd5304 1199 time_t setupCanRunAt = (RET_RTCunixtime + (ONEDAYINSECONDS - setupRunAt_delta));
pathfindr 51:9078e6928412 1200 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SETUP CAN RUN AGAIN @%u \n",setupCanRunAt);debug_exe();}
pathfindr 13:29f67f256709 1201 }
pathfindr 48:64b1613941d5 1202 RET_busy = false;
pathfindr 6:388d3c7efdd9 1203 break;
pathfindr 34:4493c9f6d707 1204 }
pathfindr 16:3bf5f1a5f869 1205 case STATE_NORMAL :
pathfindr 48:64b1613941d5 1206 {
pathfindr 48:64b1613941d5 1207 RET_busy = true;
pathfindr 24:c161db07557f 1208 //LOCATION EVENT
pathfindr 19:22261767c87a 1209 bool run_location_tx = false;
pathfindr 27:fa76f5a08195 1210 switch (RET_setting_location_mode) {
pathfindr 27:fa76f5a08195 1211 case 1: //INTERVAL POST
pathfindr 27:fa76f5a08195 1212 if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) { run_location_tx = true; }
pathfindr 27:fa76f5a08195 1213 break;
pathfindr 27:fa76f5a08195 1214 case 2: //INTERVAL POST WITH MOTION CHECK
pathfindr 40:4270e09e59f2 1215 if(RET_RTCunixtime >= RET_eventTime_location_tx && RET_eventTime_location_tx > 0) {
pathfindr 52:bd7678eade77 1216 if (RET_motionStateOnInLocTXInterval == true || RET_motionState == true) {
pathfindr 40:4270e09e59f2 1217 run_location_tx = true;
pathfindr 40:4270e09e59f2 1218 } else {
pathfindr 52:bd7678eade77 1219 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "Skip TX no mot\n");debug_exe();}
pathfindr 40:4270e09e59f2 1220 }
pathfindr 40:4270e09e59f2 1221 }
pathfindr 27:fa76f5a08195 1222 break;
pathfindr 27:fa76f5a08195 1223 case 3: //POST ON STOP MOTION
pathfindr 52:bd7678eade77 1224 if (GLOBAL_motionStopFlagTriggered) { GLOBAL_motionStopFlagTriggered = false; run_location_tx = true; }
pathfindr 27:fa76f5a08195 1225 break;
pathfindr 27:fa76f5a08195 1226 }
pathfindr 48:64b1613941d5 1227 if(RET_RTCunixtime >= RET_eventTime_location_failsafe_tx && RET_eventTime_location_failsafe_tx > 0) {
pathfindr 48:64b1613941d5 1228 RET_NetworkFailCount = 0; //reset to ensure connection
pathfindr 48:64b1613941d5 1229 RET_GPSFailCount = 0; // reset to ensure gps try
pathfindr 52:bd7678eade77 1230 addToExceptionString("FS");
pathfindr 51:9078e6928412 1231 updateBatteryV();
pathfindr 51:9078e6928412 1232 event_location_tx(true);
pathfindr 51:9078e6928412 1233 } else {
pathfindr 51:9078e6928412 1234 if (run_location_tx) { updateBatteryV(); event_location_tx(false); }
pathfindr 48:64b1613941d5 1235 }
pathfindr 19:22261767c87a 1236
pathfindr 24:c161db07557f 1237 //ACTIVITY EVENT
pathfindr 24:c161db07557f 1238 bool run_activity_tx = false;
pathfindr 45:6c7f5b556d74 1239 if(RET_RTCunixtime >= RET_eventTime_activity_tx && RET_eventTime_activity_tx > 0) {
pathfindr 48:64b1613941d5 1240 if (RET_setting_activity_mode == 1 && RET_motionTotalActivityHoursSincePost > 0.0f) {
pathfindr 45:6c7f5b556d74 1241 run_activity_tx = true;
pathfindr 45:6c7f5b556d74 1242 }
pathfindr 45:6c7f5b556d74 1243 if (RET_setting_activity_mode == 2 && strlen(RET_activityData) > 1) {
pathfindr 45:6c7f5b556d74 1244 run_activity_tx = true;
pathfindr 45:6c7f5b556d74 1245 }
pathfindr 9:b0a1535b8ef2 1246 }
pathfindr 45:6c7f5b556d74 1247 //If in sendevent mode, check if buffer is close to full, if so send
pathfindr 45:6c7f5b556d74 1248 if (RET_setting_activity_mode == 2 && strlen(RET_activityData) > (ACTIVITY_BUFFERSIZE-20)) { run_activity_tx = true; }
pathfindr 48:64b1613941d5 1249 if (run_activity_tx) { event_activity_tx(); }
pathfindr 48:64b1613941d5 1250
pathfindr 48:64b1613941d5 1251 RET_busy = false;
pathfindr 9:b0a1535b8ef2 1252 break;
pathfindr 34:4493c9f6d707 1253 }
pathfindr 16:3bf5f1a5f869 1254 case STATE_DORMANT :
pathfindr 34:4493c9f6d707 1255 {
pathfindr 50:a94e70c00fff 1256 RET_busy = true;
pathfindr 22:810425eb76e1 1257 if (RET_RTCunixtime >= RET_eventTime_wakeFromDormant) {
pathfindr 22:810425eb76e1 1258 if (RET_haveSettings) {
pathfindr 22:810425eb76e1 1259 setState(STATE_NORMAL);
pathfindr 22:810425eb76e1 1260 } else {
pathfindr 22:810425eb76e1 1261 setState(STATE_SETUP);
pathfindr 22:810425eb76e1 1262 }
pathfindr 7:e9a19750700d 1263 }
pathfindr 51:9078e6928412 1264 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"STATE:DORMANT until %u Prev S:%d\n",RET_eventTime_wakeFromDormant,RET_state_prev);debug_exe();}
pathfindr 50:a94e70c00fff 1265 RET_busy = false;
pathfindr 12:8345612bf867 1266 break;
pathfindr 34:4493c9f6d707 1267 }
pathfindr 16:3bf5f1a5f869 1268 case STATE_BUTTONPRESS1 :
pathfindr 34:4493c9f6d707 1269 {
pathfindr 50:a94e70c00fff 1270 RET_busy = true;
pathfindr 51:9078e6928412 1271 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 1\n");debug_exe();}
pathfindr 51:9078e6928412 1272 updateBatteryV();
pathfindr 51:9078e6928412 1273 if (RET_voltage < 2.5f) {
pathfindr 48:64b1613941d5 1274 LED1blink(3,500);
pathfindr 48:64b1613941d5 1275 } else if (selfTest() == false){
pathfindr 48:64b1613941d5 1276 LED1blink(4,500);
pathfindr 48:64b1613941d5 1277 } else {
pathfindr 48:64b1613941d5 1278 if(RET_state_prev == STATE_DORMANT) {
pathfindr 48:64b1613941d5 1279 LED1on(500);
pathfindr 48:64b1613941d5 1280 } else if (RET_state_prev == STATE_NORMAL) {
pathfindr 48:64b1613941d5 1281 LED1blink(2,500);
pathfindr 48:64b1613941d5 1282 }
pathfindr 48:64b1613941d5 1283 }
pathfindr 50:a94e70c00fff 1284 if (RET_state_prev != STATE_NORMAL || RET_state_prev != STATE_DORMANT) {
pathfindr 50:a94e70c00fff 1285 setState(STATE_NORMAL);
pathfindr 50:a94e70c00fff 1286 } else {
pathfindr 50:a94e70c00fff 1287 setState(RET_state_prev);
pathfindr 50:a94e70c00fff 1288 }
pathfindr 51:9078e6928412 1289 dumpSettings();
pathfindr 50:a94e70c00fff 1290 RET_busy = false;
pathfindr 28:24b02608fa5f 1291 break;
pathfindr 34:4493c9f6d707 1292 }
pathfindr 28:24b02608fa5f 1293 case STATE_BUTTONPRESS3 :
pathfindr 34:4493c9f6d707 1294 {
pathfindr 50:a94e70c00fff 1295 RET_busy = true;
pathfindr 48:64b1613941d5 1296 if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) {
pathfindr 51:9078e6928412 1297 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 3\n");debug_exe();}
pathfindr 48:64b1613941d5 1298 LED1blink(3,500);
pathfindr 48:64b1613941d5 1299 LED1on(0);
pathfindr 51:9078e6928412 1300 updateBatteryV();
pathfindr 48:64b1613941d5 1301 event_setup(true);
pathfindr 48:64b1613941d5 1302 LED1off();
pathfindr 48:64b1613941d5 1303 }
pathfindr 48:64b1613941d5 1304 setState(STATE_NORMAL); //turns device back on
pathfindr 50:a94e70c00fff 1305 RET_busy = false;
pathfindr 48:64b1613941d5 1306 break;
pathfindr 48:64b1613941d5 1307 }
pathfindr 48:64b1613941d5 1308 case STATE_BUTTONPRESS5 :
pathfindr 48:64b1613941d5 1309 {
pathfindr 50:a94e70c00fff 1310 RET_busy = true;
pathfindr 48:64b1613941d5 1311 if(RET_state_prev == STATE_DORMANT || RET_state_prev == STATE_NORMAL) {
pathfindr 51:9078e6928412 1312 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON PRESS 5\n");debug_exe();}
pathfindr 48:64b1613941d5 1313 LED1blink(5,500);
pathfindr 48:64b1613941d5 1314 LED1on(0);
pathfindr 51:9078e6928412 1315 updateBatteryV();
pathfindr 51:9078e6928412 1316 event_location_tx(false);
pathfindr 48:64b1613941d5 1317 LED1off();
pathfindr 48:64b1613941d5 1318 }
pathfindr 48:64b1613941d5 1319 setState(STATE_NORMAL); //turns device back on
pathfindr 50:a94e70c00fff 1320 RET_busy = false;
pathfindr 9:b0a1535b8ef2 1321 break;
pathfindr 34:4493c9f6d707 1322 }
pathfindr 51:9078e6928412 1323 case STATE_TURNOFF :
pathfindr 34:4493c9f6d707 1324 {
pathfindr 50:a94e70c00fff 1325 RET_busy = true;
pathfindr 51:9078e6928412 1326 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON HOLD \n");debug_exe();}
pathfindr 51:9078e6928412 1327 RET_eventTime_wakeFromDormant = (RET_RTCunixtime + THREEDAYSINSECONDS);
pathfindr 51:9078e6928412 1328 setState(STATE_DORMANT);
pathfindr 51:9078e6928412 1329 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"TURNING OFF\n");debug_exe();}
pathfindr 51:9078e6928412 1330 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"STATE:DORMANT until %u\n",RET_eventTime_wakeFromDormant);debug_exe();}
pathfindr 51:9078e6928412 1331 LED1on(5000);
pathfindr 51:9078e6928412 1332 //event_turnonofflog_tx(false);
pathfindr 51:9078e6928412 1333 RET_busy = false;
pathfindr 51:9078e6928412 1334 break;
pathfindr 51:9078e6928412 1335 }
pathfindr 51:9078e6928412 1336 case STATE_TURNON :
pathfindr 51:9078e6928412 1337 {
pathfindr 51:9078e6928412 1338 RET_busy = true;
pathfindr 51:9078e6928412 1339 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"BUTTON HOLD \n");debug_exe();}
pathfindr 51:9078e6928412 1340 if (RET_haveSettings) {
pathfindr 51:9078e6928412 1341 setState(STATE_NORMAL);
pathfindr 19:22261767c87a 1342 } else {
pathfindr 51:9078e6928412 1343 setState(STATE_SETUP);
pathfindr 19:22261767c87a 1344 }
pathfindr 51:9078e6928412 1345 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"TURNING ON\n");debug_exe();}
pathfindr 51:9078e6928412 1346 LED1blink(20,100);
pathfindr 50:a94e70c00fff 1347 RET_busy = false;
pathfindr 6:388d3c7efdd9 1348 break;
pathfindr 34:4493c9f6d707 1349 }
pathfindr 39:f767b8037475 1350 case STATE_SCORCHEDEARTH :
pathfindr 39:f767b8037475 1351 {
pathfindr 50:a94e70c00fff 1352 RET_busy = true;
pathfindr 51:9078e6928412 1353 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"SCORCHED EARTH - RESETTING");debug_exe();}
pathfindr 39:f767b8037475 1354 setState(STATE_SETUP); //this wont be used but just incase to kick it out of this state
pathfindr 39:f767b8037475 1355 read_app_data_from_flash(&app_data);
pathfindr 39:f767b8037475 1356 app_data.current_firmware_version = 0;
pathfindr 39:f767b8037475 1357 app_data.target_firmware_version = 0;
pathfindr 39:f767b8037475 1358 write_app_data_to_flash(&app_data);
pathfindr 39:f767b8037475 1359 system_reset();
pathfindr 50:a94e70c00fff 1360 RET_busy = false;
pathfindr 39:f767b8037475 1361 break;
pathfindr 39:f767b8037475 1362 }
pathfindr 6:388d3c7efdd9 1363 default :
pathfindr 34:4493c9f6d707 1364 {
pathfindr 19:22261767c87a 1365 setState(STATE_SETUP);
pathfindr 34:4493c9f6d707 1366 }
pathfindr 6:388d3c7efdd9 1367 }
pathfindr 6:388d3c7efdd9 1368 }
pathfindr 6:388d3c7efdd9 1369
pathfindr 7:e9a19750700d 1370 //------------------------------------------------------------------------------
pathfindr 7:e9a19750700d 1371 // MAIN
pathfindr 7:e9a19750700d 1372 //------------------------------------------------------------------------------
pathfindr 50:a94e70c00fff 1373 int main() {
pathfindr 24:c161db07557f 1374 //INIT
pathfindr 51:9078e6928412 1375 LED1off();
pathfindr 51:9078e6928412 1376 ThisThread::sleep_for(2000); //Initial pause, this is needed for softdevice to init, dont remove!! If we dont have this we get crashes
pathfindr 52:bd7678eade77 1377 watchdog.configure(300.0); //5 mins //Do not set to less than 4500ms or can cause issues with softdevice
pathfindr 43:7b232f03628f 1378 modem.off(false);
pathfindr 24:c161db07557f 1379 RTCticker.attach(&RTCtick, 1.0);
pathfindr 51:9078e6928412 1380 LPtimer.start();
pathfindr 34:4493c9f6d707 1381 button.fall(&buttonPress);
pathfindr 24:c161db07557f 1382 button.rise(&buttonRelease);
pathfindr 43:7b232f03628f 1383 read_app_data_from_flash(&app_data);
pathfindr 34:4493c9f6d707 1384 dumpSettings();
pathfindr 4:8d8e9bfa82e4 1385
pathfindr 4:8d8e9bfa82e4 1386 //CHECK IF THIS IS RESET
pathfindr 43:7b232f03628f 1387 //0x00000008 == lock up //0x00000004 == soft reset //0x00000002 == watchdog //0x00000001 == button/hardreset
pathfindr 4:8d8e9bfa82e4 1388 if (NRF_POWER->RESETREAS != 0xffffffff) {
pathfindr 43:7b232f03628f 1389 RET_coldBoot = 1;
pathfindr 4:8d8e9bfa82e4 1390 switch(NRF_POWER->RESETREAS) {
pathfindr 4:8d8e9bfa82e4 1391 case 0x00000001 :
pathfindr 52:bd7678eade77 1392 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:HW\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 1393 break;
pathfindr 4:8d8e9bfa82e4 1394 case 0x00000002 :
pathfindr 52:bd7678eade77 1395 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:WD\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 1396 break;
pathfindr 4:8d8e9bfa82e4 1397 case 0x00000004 :
pathfindr 52:bd7678eade77 1398 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:SW\n");debug_exe();}
pathfindr 4:8d8e9bfa82e4 1399 break;
pathfindr 43:7b232f03628f 1400 case 0x00000008 :
pathfindr 52:bd7678eade77 1401 if(RET_debug){debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"RR:LU\n");debug_exe();}
pathfindr 43:7b232f03628f 1402 break;
pathfindr 4:8d8e9bfa82e4 1403 }
pathfindr 36:8e359069192b 1404 NRF_POWER->RESETREAS = 0xffffffff;
pathfindr 4:8d8e9bfa82e4 1405 }
pathfindr 24:c161db07557f 1406
pathfindr 9:b0a1535b8ef2 1407 //CHECK FOR FIRST BOOT
pathfindr 52:bd7678eade77 1408 if (RET_coldBoot == 1) {
pathfindr 52:bd7678eade77 1409 memset(RET_activityData,0x00,sizeof(RET_activityData));
pathfindr 52:bd7678eade77 1410 lis3dh_configureForSleep(DEFAULT_MOTION_G,DEFAULT_IMPACT_G);
pathfindr 51:9078e6928412 1411 updateBatteryV();
pathfindr 52:bd7678eade77 1412 if (RET_voltage < 2.5f) { LED1errorCode(10,2);}
pathfindr 43:7b232f03628f 1413 addToExceptionString("FR");
pathfindr 24:c161db07557f 1414 }
pathfindr 13:29f67f256709 1415
pathfindr 34:4493c9f6d707 1416 //MAIN LOOP
pathfindr 52:bd7678eade77 1417 while(true) {
pathfindr 39:f767b8037475 1418 RET_asleep = false;
pathfindr 52:bd7678eade77 1419 watchdogKick();
pathfindr 51:9078e6928412 1420 updateTemperatures();
pathfindr 52:bd7678eade77 1421 resetSessionVars();
pathfindr 39:f767b8037475 1422 healthCheck(); //this must be after resetGlobals
pathfindr 13:29f67f256709 1423 GLOBAL_wakeTime = RET_RTCunixtime;
pathfindr 11:60eb0ff945f2 1424
pathfindr 40:4270e09e59f2 1425 //check and log motion
pathfindr 48:64b1613941d5 1426 if (RET_state == STATE_NORMAL || RET_state == STATE_SETUP) {
pathfindr 52:bd7678eade77 1427 if (checkMotion() == true) { //if motionstate is true, then reset network and gps fail counts, so that they are not skipped
pathfindr 48:64b1613941d5 1428 RET_NetworkFailCount = 0;
pathfindr 48:64b1613941d5 1429 RET_GPSFailCount = 0;
pathfindr 48:64b1613941d5 1430 }
pathfindr 48:64b1613941d5 1431 }
pathfindr 40:4270e09e59f2 1432
pathfindr 9:b0a1535b8ef2 1433 //MAIN LOGIC
pathfindr 51:9078e6928412 1434 if(RET_debug){
pathfindr 39:f767b8037475 1435 if (RET_state != 99) {
pathfindr 52:bd7678eade77 1436 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "F:%d, S:%d, SET:%d, MF:%d, MS:%d, MTX:%d, %u, L:%u, LFS:%u, A:%u", FW_VERSION, RET_state, RET_haveSettings, RET_motionTriggeredinFrame, RET_motionState, RET_motionStateOnInLocTXInterval, RET_RTCunixtime,RET_eventTime_location_tx,RET_eventTime_location_failsafe_tx,RET_eventTime_activity_tx);debug_exe();
pathfindr 51:9078e6928412 1437 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "ACT M:%d, HsP:%.2f, Ht:%.2f, D:%s",RET_setting_activity_mode,RET_motionTotalActivityHoursSincePost,RET_motionTotalActivityHours,RET_activityData);debug_exe();
pathfindr 39:f767b8037475 1438 } else {
pathfindr 43:7b232f03628f 1439 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "F:%d, S:%d, WAKE@:%u\n", FW_VERSION, RET_state, RET_eventTime_wakeFromDormant);debug_exe();
pathfindr 39:f767b8037475 1440 }
pathfindr 51:9078e6928412 1441 if (RET_coldBoot == false && RET_RTCunixtime > RET_debug_offat) {
pathfindr 51:9078e6928412 1442 debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer),"DEBUGGING OFF\n");debug_exe();
pathfindr 51:9078e6928412 1443 RET_debug = false;
pathfindr 51:9078e6928412 1444 }
pathfindr 38:476a9b5629a1 1445 }
pathfindr 37:505ef618f06c 1446
pathfindr 42:f482f4cdb319 1447 //STATE ENGINE
pathfindr 9:b0a1535b8ef2 1448 mainStateEngine();
pathfindr 51:9078e6928412 1449
pathfindr 34:4493c9f6d707 1450 //LOG FIRST RUN - BOOTLOADER COMMS
pathfindr 34:4493c9f6d707 1451 if (RET_coldBoot) {
pathfindr 45:6c7f5b556d74 1452 recordFirmwareAsValid();
pathfindr 34:4493c9f6d707 1453 RET_coldBoot = 0;
pathfindr 34:4493c9f6d707 1454 }
pathfindr 51:9078e6928412 1455
pathfindr 52:bd7678eade77 1456 //CONFIGURE BLE BEACON BROADCAST
pathfindr 52:bd7678eade77 1457 #if BLE_ENABLED
pathfindr 52:bd7678eade77 1458 //see if we are in range of ble detector, if so, no need to get gps and broadcast
pathfindr 52:bd7678eade77 1459 if (RET_setting_beacon_interval_seconds > 0 && RET_pf_identifier[0] != 0x00 && RET_pf_identifier[1] != 0x00 && RET_pf_identifier[2] != 0x00) {
pathfindr 52:bd7678eade77 1460 if (RET_bleBroadcasting == false) {
pathfindr 52:bd7678eade77 1461 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "BLE init...");debug_exe();}
pathfindr 52:bd7678eade77 1462 myble.init(bleSetupAdvertising);
pathfindr 52:bd7678eade77 1463 if (myble.hasInitialized()) {
pathfindr 52:bd7678eade77 1464 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "OK");debug_exe();}
pathfindr 52:bd7678eade77 1465 bleUpdateAndAdvertise();
pathfindr 52:bd7678eade77 1466 } else {
pathfindr 52:bd7678eade77 1467 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "FAIL");debug_exe();}
pathfindr 52:bd7678eade77 1468 }
pathfindr 52:bd7678eade77 1469 } else {
pathfindr 52:bd7678eade77 1470 if(RET_debug) {debug_prep();snprintf(GLOBAL_debug_buffer, sizeof(GLOBAL_debug_buffer), "BLE update");debug_exe();}
pathfindr 52:bd7678eade77 1471 bleUpdateAndAdvertise();
pathfindr 52:bd7678eade77 1472 }
pathfindr 52:bd7678eade77 1473 } else {
pathfindr 52:bd7678eade77 1474 if (RET_bleBroadcasting == true) bleStopAdvertising();
pathfindr 52:bd7678eade77 1475 }
pathfindr 52:bd7678eade77 1476 #endif
pathfindr 52:bd7678eade77 1477
pathfindr 9:b0a1535b8ef2 1478 //PRE-SLEEP ACTIONS
pathfindr 45:6c7f5b556d74 1479 LED1off();
pathfindr 52:bd7678eade77 1480 modem.off(GLOBAL_modemOn); //HARD OR SOFT SHUT DOWN - DEPENDING ON modemOn state
pathfindr 34:4493c9f6d707 1481 if (GLOBAL_needToConfigureLis3dh) { lis3dh_configureForSleep(RET_setting_motion_g,RET_setting_impact_g); }
pathfindr 50:a94e70c00fff 1482 nrf_configureForSleep();
pathfindr 34:4493c9f6d707 1483 NRFuart_uninit();
pathfindr 34:4493c9f6d707 1484 watchdogKick();
pathfindr 34:4493c9f6d707 1485 //SLEEP
pathfindr 39:f767b8037475 1486 RET_asleep = true;
pathfindr 26:fa3579737329 1487 mainthread.wait(DEFAULT_SLEEP_FRAME);
pathfindr 34:4493c9f6d707 1488 }
pathfindr 7:e9a19750700d 1489 }