I messed up the merge, so pushing it over to another repo so I don't lose it. Will tidy up and remove later

Dependencies:   BufferedSerial FatFileSystemCpp mbed

Files at this revision

API Documentation at this revision

Comitter:
JamieB
Date:
Thu Feb 10 14:00:32 2022 +0000
Parent:
75:9fd8f8b59180
Child:
77:6453dcc202d5
Commit message:
Fixed TCP Task not shutting down when Ethernet disconnected and re-added device reset on button hold (5-seconds)

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Feb 10 10:44:24 2022 +0000
+++ b/main.cpp	Thu Feb 10 14:00:32 2022 +0000
@@ -1,4 +1,4 @@
-#define APP_VERSION 0.27
+#define APP_VERSION 0.28
 
 /*
 Settings file options
@@ -163,6 +163,7 @@
 bool OKToCheckSync = false;
 volatile uint32_t VBOXTicks = 0; // time at the NEXT PPS edge
 uint32_t lastPPSSecondStart;
+volatile bool StopTCPListening = false;
 
 #define _longPPMTrackLen_ 20
 float PPMErrors[_longPPMTrackLen_];
@@ -635,6 +636,15 @@
 
 void OnResetTimeout()
 {
+    __disable_irq();
+    led1=1;
+    led2=1;
+    led3=1;
+    frameToggle=1;
+    RedLED=1; // red
+    GreenLED=1;
+    BlueLED=1;
+    wait(1);
     NVIC_SystemReset();
 }
 
@@ -881,51 +891,60 @@
 //    int fileSize = 0;
     TCPSocketServer server;
     server.bind(UserSettings.SettingsPort);
+    server.set_blocking(false, 1000);
     server.listen();
 
-    while (true) {
-        TCPSocketConnection connection;
-        pc.puts("Waiting for TCP connection\r\n");
+    TCPSocketConnection connection;
+    pc.puts("TCP thread waiting for connection\r\n");
+
+    while (!StopTCPListening) {
         server.accept(connection);
-        pc.puts("TCP Connected\r\n");
-        connection.set_blocking(false,50);
+        if (connection.is_connected()) {
+            pc.puts("TCP Connected\r\n");
+            connection.set_blocking(false,50);
 
-        int bytesIn = 0;
-        do {
-            bytesIn = connection.receive(settingsInBuffer,rxBufferSize);
-            if (bytesIn > 0) {
-                if (!validated_connection) {
-                    printf("Recieved %d bytes on unvalidated connection\r\n",bytesIn);
-                    if ((settingsInBuffer[0] == 0x07) &&
-                            (settingsInBuffer[1] == 0x05) &&
-                            (settingsInBuffer[2] == 0x02)) {
-                        validated_connection = true;
-                        printf("Validated Connection - Sending 'set quiet' command\r\n");
-                        VIPS.sendQuiet();
-                        Thread::wait(50);
-                        VIPS.EnableDirectTX(true);
+            int bytesIn = 0;
+            do {
+                bytesIn = connection.receive(settingsInBuffer,rxBufferSize);
+                if (bytesIn > 0) {
+                    if (!validated_connection) {
+                        printf("Recieved %d bytes on unvalidated connection\r\n",bytesIn);
+                        if ((settingsInBuffer[0] == 0x07) &&
+                                (settingsInBuffer[1] == 0x05) &&
+                                (settingsInBuffer[2] == 0x02)) {
+                            validated_connection = true;
+                            printf("Validated Connection - Sending 'set quiet' command\r\n");
+                            VIPS.sendQuiet();
+                            Thread::wait(50);
+                            VIPS.EnableDirectTX(true);
+                            GreenLED = LED_ON;
+                            VIPS.sendDirectTX((unsigned char *)settingsInBuffer, bytesIn);
+                            printf("Sent first %d bytes to VIPS\r\n",bytesIn); //also a set-quiet command but good to send twice
+                        } else {
+                            printf("Invalid: %X %X %X", settingsInBuffer[0], settingsInBuffer[1], settingsInBuffer[2]);
+                        }
+                    } else if (validated_connection) {
                         VIPS.sendDirectTX((unsigned char *)settingsInBuffer, bytesIn);
-                        printf("Sent first %d bytes to VIPS\r\n",bytesIn); //also a set-quiet command but good to send twice
-                    } else {
-                        printf("Invalid: %X %X %X", settingsInBuffer[0], settingsInBuffer[1], settingsInBuffer[2]);
+                        printf("Sent %d bytes to VIPS\r\n",bytesIn);
                     }
-                } else if (validated_connection) {
-                    VIPS.sendDirectTX((unsigned char *)settingsInBuffer, bytesIn);
-                    printf("Sent %d bytes to VIPS\r\n",bytesIn);
+                }
+                if (VIPS.getWaitingBuffer(&bufferToSend, &bytesToSend)) {
+                    connection.send((char *)bufferToSend, bytesToSend);
+                    printf("Recieved %d bytes from VIPS\r\n",bytesIn);
                 }
-            }
-            if (VIPS.getWaitingBuffer(&bufferToSend, &bytesToSend)) {
-                connection.send((char *)bufferToSend, bytesToSend);
-                printf("Recieved %d bytes from VIPS\r\n",bytesIn);
-            }
-        } while (connection.is_connected());
+            } while (connection.is_connected());
 
-        VIPS.EnableDirectTX(false);
-        validated_connection = false;
-        pc.puts("Disconnected TCP \r\n");
-        connection.close();
-
+            VIPS.EnableDirectTX(false);
+            GreenLED = LED_OFF;
+            validated_connection = false;
+            pc.puts("Disconnected TCP \r\n");
+            connection.close();
+            Thread::wait(50);
+        }
     }
+    VIPS.EnableDirectTX(false);
+    GreenLED = LED_OFF;
+    pc.puts("Ending TCP Task\r\n");
 }
 
 
@@ -981,9 +1000,8 @@
         if (settingsPtr->SettingsPort) {
             pc.puts("Starting network update task\r\n");
             Thread::wait(100);
+            StopTCPListening = false;
             ListenTask = new Thread(settingsNetUpdate, NULL, osPriorityHigh, 256 * 4);
-            pc.puts("Done\r\n");
-            Thread::wait(100);
         }
         if (PrimaryDataSocket)
             delete PrimaryDataSocket;
@@ -1048,7 +1066,15 @@
 //                NewData.unlock();
                 Thread::wait(1);
         }
-        ListenTask->terminate();
+        StopTCPListening = true;
+        Thread::wait(1);
+
+        while (ListenTask->get_state()) {
+            Thread::wait(50);
+        }
+//        int ListenState = ListenTask->get_state();
+//        pc.printf("Listen State: %d\r\n", ListenState);
+
         eth.disconnect();
         pc.puts("Attempting to restart network\r\n");
     }
@@ -1278,8 +1304,8 @@
             if (logButtonLastState) { // pressed
                 logButtonDownTime = inputTimer.read();
                 if ((logButtonDownTime - logButtonUpTime) > 0.2f) {
-                    //       pc.puts("Down\r\n");
-                    //        resetTimeout.attach(callback(&OnResetTimeout),10);
+//                    pc.puts("Down\r\n");
+                    resetTimeout.attach(callback(&OnResetTimeout),5);
                     if (logging) {
                         //           pc.puts("Logging off\r\n");
                         GreenLED = LED_OFF;
@@ -1297,7 +1323,7 @@
                 }
             } else { // released
                 logButtonUpTime = inputTimer.read();
-                //      resetTimeout.attach(NULL,0);
+                resetTimeout.attach(NULL,0);
                 if ((logButtonUpTime-logButtonDownTime) > 0.05f) {
                     //        pc.puts("Up\r\n");
                 }