From 135c7694f8c54cfa7c937fa3944ac51c6f2d608f Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Tue, 2 Jul 2019 14:07:11 +0100
Subject: [PATCH] Default to a frame counter of 0

---
 iot/main/gps.cpp          |  9 ++-------
 iot/main/main.ino         |  9 +++++----
 iot/main/power.cpp        |  2 --
 iot/main/transmission.cpp | 23 +++++++++++++++++------
 iot/main/transmission.h   |  5 +++++
 5 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/iot/main/gps.cpp b/iot/main/gps.cpp
index 1189565..a4e5ad1 100644
--- a/iot/main/gps.cpp
+++ b/iot/main/gps.cpp
@@ -19,7 +19,7 @@ GPSLocation gps_fetch() {
 	// The struct that will hold the result
 	GPSLocation result;
 	
-	Serial.print(F("[gps] Working: "));
+	Serial.print(F("[gps] Working "));
 	uint32_t start = millis(), ms_last_update = millis();
 	uint8_t ticks = 0;
 	unsigned long fix_age = TinyGPS::GPS_INVALID_AGE;
@@ -33,7 +33,7 @@ GPSLocation gps_fetch() {
 		gps.stats(&chars, &sentences, &failed_checksum);
 		
 		if(millis() - start > 5000 && chars < 10) {
-			Serial.println(F("\n[error] GPS device init failed"));
+			Serial.println(F("\n[error/gps] Init failed"));
 			while(true) { delay(1); }
 		}
 		
@@ -57,11 +57,6 @@ GPSLocation gps_fetch() {
 					Serial.println();
 					ticks = 0;
 				}
-				// Serial.println(gps.location.lat());
-				// Serial.println(gps.location.lng());
-				// Serial.println(gps.date.year());
-				// Serial.println(gps.time.hour());
-				// Serial.println(gps.time.minute());
 			}
 			ms_last_update = millis();
 			continue;
diff --git a/iot/main/main.ino b/iot/main/main.ino
index 9648ded..feb5fee 100644
--- a/iot/main/main.ino
+++ b/iot/main/main.ino
@@ -60,16 +60,17 @@ void setup() {
 	const uint8_t* bytes_lat = reinterpret_cast<uint8_t const *>(&(gps_data.lat));
 	const uint8_t* bytes_lng = reinterpret_cast<uint8_t const *>(&(gps_data.lat));
 	
-	for(int i = 0; i < sizeof(uint32_t); i++) {
-		message[i] = bytes_id[i];
-	}
+	// for(int i = 0; i < sizeof(uint32_t); i++) {
+	// }
+	// The size of a float == the size of a uint32_t
 	for(int i = 0; i < sizeof(float); i++) {
+		message[i] = bytes_id[i];
 		message[sizeof(uint32_t) + i] = bytes_lat[i];
 		message[sizeof(uint32_t) + sizeof(float) + i] = bytes_lng[i];
 	}
 	
 	#ifdef ENABLE_RADIO
-	Serial.println(F("[main] Transmitting"));
+	// Serial.println(F("[main] Transmitting"));
 	transmit_init();
 	transmit_send(message, message_size);
 	#endif
diff --git a/iot/main/power.cpp b/iot/main/power.cpp
index d5a281d..a78e243 100644
--- a/iot/main/power.cpp
+++ b/iot/main/power.cpp
@@ -6,8 +6,6 @@ void power_off() {
 	Serial.println(F("[power] Shutting down"));
 	// Serial.println(F("[power] Switching GPS module to standby"));
 	// power_gps_standby();
-	
-	Serial.println(F("[power] Signalling TPL5111"));
 	pinMode(PIN_TPL_DONE, OUTPUT);
 	digitalWrite(PIN_TPL_DONE, HIGH);
 	
diff --git a/iot/main/transmission.cpp b/iot/main/transmission.cpp
index 9a6e437..8a74596 100644
--- a/iot/main/transmission.cpp
+++ b/iot/main/transmission.cpp
@@ -8,6 +8,13 @@
 // Global static variable that's used to detect when LMIC has finished doing it's thing
 bool is_sending_complete = false;
 
+
+void transmit_counter_save() {
+	store_eeprom_uint32_save(0, LMIC.seqnoUp);
+	store_eeprom_uint32_save(sizeof(uint32_t), LMIC.seqnoDn);
+}
+
+
 void transmit_init() {
 	Serial.println(F("[LMIC] Init"));
 	// LMIC init
@@ -62,7 +69,11 @@ void transmit_init() {
 	// Restore the frame counters
 	LMIC.seqnoUp = store_eeprom_uint32_retrieve(0);
 	LMIC.seqnoDn = store_eeprom_uint32_retrieve(sizeof(uint32_t));
-	Serial.println(LMIC.seqnoUp);
+	// Serial.println(LMIC.seqnoUp);
+	if(LMIC.seqnoUp >= 4294967295) { // The default value
+		LMIC.seqnoUp = LMIC.seqnoDn = 0; // Reset to 0
+		transmit_counter_save();
+	}
 }
 
 /**
@@ -78,7 +89,7 @@ bool transmit_send(uint8_t* data, int length) {
 	}
 	// Prepare upstream data transmission at the next possible time.
 	LMIC_setTxData2(1, data, length, 0);
-	Serial.println(F("Packet queued"));
+	Serial.println(F("done"));
 	
 	// Run the LMIC loop, but only until it's finished sending the packet
 	while (!is_sending_complete) {
@@ -86,8 +97,7 @@ bool transmit_send(uint8_t* data, int length) {
 	}
 	
 	// Persist the frame counters
-	store_eeprom_uint32_save(0, LMIC.seqnoUp);
-	store_eeprom_uint32_save(sizeof(uint32_t), LMIC.seqnoDn);
+	transmit_counter_save();
 	
 	// Reset it for next time (just in case)
 	is_sending_complete = false;
@@ -127,7 +137,7 @@ void onEvent (ev_t ev) {
 			Serial.println(F("EV_REJOIN_FAILED"));
 			break;*/
 		case EV_TXCOMPLETE:
-			Serial.println(F("EV_TXCOMPLETE (incl. RX window wait)"));
+			Serial.println(F("EV_TXCOMPLETE incl. RX"));
 			if(LMIC.dataLen) {
 				// data received in rx slot after tx
 				Serial.print(F("Received: "));
@@ -154,7 +164,8 @@ void onEvent (ev_t ev) {
 			Serial.println(F("EV_LINK_ALIVE"));
 			break;*/
 		default:
-			Serial.println(F("Unknown event"));
+			Serial.print(F("Unknown event "));
+			Serial.println(ev);
 			break;
 	}
 }
diff --git a/iot/main/transmission.h b/iot/main/transmission.h
index 38a9bc4..d99ef6e 100644
--- a/iot/main/transmission.h
+++ b/iot/main/transmission.h
@@ -1,5 +1,10 @@
 #pragma once
 
+/**
+ * Internal use only. Persists the LMIC frame counters to disk.
+ */
+void transmit_counter_save();
+
 /**
  * Initialises the RFM95 LoRa radio.
  */
