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:
AndyA
Date:
Thu Jan 27 12:56:13 2022 +0000
Parent:
67:8e9ac893e366
Child:
69:47f800793d00
Commit message:
Added TCP socket for uploading FIZ box settings

Changed in this revision

LTCApp.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/LTCApp.h	Wed Nov 17 10:58:58 2021 +0000
+++ b/LTCApp.h	Thu Jan 27 12:56:13 2022 +0000
@@ -33,6 +33,7 @@
     int SerialOutMode;
     int FreeDPort;
     int VipsUDPPort;
+    int SettingsPort;
     char IPAddress[32];
     char Gateway[32];
     char Subnet[32];
--- a/main.cpp	Wed Nov 17 10:58:58 2021 +0000
+++ b/main.cpp	Thu Jan 27 12:56:13 2022 +0000
@@ -1,4 +1,4 @@
-#define APP_VERSION 0.21
+#define APP_VERSION 0.22
 
 /*
 Settings file options
@@ -87,6 +87,7 @@
 VIPSSerial VIPS(p28, p27);
 BufferedSerial COM1(p13, p14);
 FIZReader *FIZPort;
+EthernetInterface eth;
 
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
@@ -103,6 +104,8 @@
 DigitalOut GreenLED(p19);
 DigitalOut BlueLED(p20);
 
+LocalFileSystem localFS("local");
+
 #define LED_OFF 1
 #define LED_ON 0
 
@@ -542,7 +545,9 @@
         }
 
     }
-    if (UserSettings.HalfRate) {TXFrame = !TXFrame;}
+    if (UserSettings.HalfRate) {
+        TXFrame = !TXFrame;
+    }
 }
 
 
@@ -641,6 +646,7 @@
     UserSettings.SerialOutMode = mode_VIPS;
     UserSettings.FreeDPort = 0;
     UserSettings.VipsUDPPort = 0;
+    UserSettings.SettingsPort = 0;
     UserSettings.IPAddress[0] = 0;
     UserSettings.Gateway[0] = 0;
     UserSettings.Subnet[0] = 0;
@@ -668,7 +674,7 @@
     UserSettings.ForcePPF = 0;
     UserSettings.HalfRate = 0;
 
-    LocalFileSystem localFS("local");
+//    LocalFileSystem localFS("local");
     FILE *LSFile= fopen("/local/settings.txt","r");
     char lineBuffer[64];
     int valueIn;
@@ -697,6 +703,10 @@
                     pc.printf("Got VIPS_Port value from file of %d\r\n",valueIn);
                     UserSettings.VipsUDPPort = valueIn;
                 }
+                if (sscanf(lineBuffer,"Settings_Port=%d",&valueIn) == 1) {
+                    pc.printf("Got Settings update port value from file of %d\r\n",valueIn);
+                    UserSettings.SettingsPort = valueIn;
+                }
                 if (sscanf(lineBuffer,"IP_addr=%s",UserSettings.IPAddress) == 1) {
                     pc.printf("Got IP_addr value from file of %s\r\n",UserSettings.IPAddress);
                 }
@@ -803,10 +813,68 @@
     }
 }
 
+void settingsNetUpdate(void const* netInterface)
+{
+    FILE *LSFile;
+    const int rxBufferSize = 512; // making this 1024 causes a crash. Probably out of memory somewhere.
+    char settingsInBuffer[rxBufferSize];
+
+    const char filename[] = "/local/NewSet.txt";
+
+    Thread::wait(1000);
+    pc.puts("Starting network listen server\r\n");
+    Thread::wait(50);
+
+
+    int fileSize = 0;
+    TCPSocketServer server;
+    server.bind(UserSettings.SettingsPort);
+    server.listen();
+
+    while (true) {
+        TCPSocketConnection connection;
+        pc.puts("Waiting for connections\r\n");
+        server.accept(connection);
+        pc.puts("Connected\r\n");
+        fileSize = 0;
+        LSFile = fopen(filename,"w");
+        //  connection.set_blocking(true,0);
+        int bytesIn = 0;
+        do {
+            bytesIn = connection.receive(settingsInBuffer,rxBufferSize);
+            fwrite(settingsInBuffer,bytesIn,1,LSFile);
+            printf("For %d bytes\r\n",bytesIn);
+            fileSize+=bytesIn;
+            if (fileSize > 1024*20) // 20k settings file? I think not.
+                bytesIn=0; // force close.
+        } while (bytesIn > 0);
+
+        pc.puts("Closing new settings file\r\n");
+        fclose(LSFile);
+        connection.close();
+
+        // sanitycheck file.
+        if ((fileSize > 512) && (fileSize < 1024*10)) {
+            pc.puts("Settings valid. Changing file new settings file\r\n");
+            if (rename("/local/settings.txt","/local/oldSet.txt") == 0)
+                pc.puts("Old file moved\r\n");
+            else
+                pc.puts("Old file move failed\r\n");
+
+            if (rename(filename,"/local/settings.txt") == 0)
+                pc.puts("new file moved\r\n");
+            else
+                pc.puts("new file move failed\r\n");
+
+        }
+    }
+}
+
+
 void ethernetTask(void const* settings)
 {
 
-    EthernetInterface eth;
+    Thread* ListenTask=NULL;
     UDPSocket* FreeDSocket = NULL;
     Endpoint FreeDTarget;
 
@@ -816,8 +884,8 @@
     UserSettings_t *settingsPtr = (UserSettings_t *) settings;
 
     pc.puts("Ethernet task startup\r\n");
-    if (!settingsPtr->FreeDPort && !settingsPtr->VipsUDPPort) {
-        pc.puts("No UDP port set. Ethernet task exiting\r\n");
+    if (!settingsPtr->FreeDPort && !settingsPtr->VipsUDPPort && !settingsPtr->SettingsPort) {
+        pc.puts("No ports set. Ethernet task exiting\r\n");
         return;
     }
     int Result = -1;
@@ -848,10 +916,22 @@
             }
         }
         pc.puts("Ethernet connect complete\r\n");
+        Thread::wait(100);
 
+        if (ListenTask)
+            delete ListenTask;
+        if (settingsPtr->SettingsPort) {
+            pc.puts("Starting network update task\r\n");
+            Thread::wait(100);
+            ListenTask = new Thread(settingsNetUpdate, NULL, osPriorityHigh, 256 * 4);
+            pc.puts("Done\r\n");
+            Thread::wait(100);
+        }
         if (FreeDSocket)
             delete FreeDSocket;
         if (UserSettings.FreeDPort) {
+            pc.puts("Starting FreeD socket\r\n");
+            Thread::wait(50);
             FreeDSocket = new UDPSocket();
             FreeDSocket->init();
             FreeDSocket->set_broadcasting(true);
@@ -861,6 +941,8 @@
         if (VIPSSocket)
             delete VIPSSocket;
         if (UserSettings.VipsUDPPort) {
+            pc.puts("Starting VIPS socket\r\n");
+            Thread::wait(50);
             VIPSSocket = new UDPSocket();
             VIPSSocket->init();
             VIPSSocket->set_broadcasting(true);
@@ -868,6 +950,8 @@
         }
         EthernetTimer.reset();
         EthernetTimer.start();
+        pc.puts("Network ready for TX\r\n");
+        Thread::wait(50);
         while (true) {
             if (EthTxNow) {
 //                NewData.lock();
@@ -902,6 +986,7 @@
 //                NewData.unlock();
                 Thread::wait(1);
         }
+        ListenTask->terminate();
         eth.disconnect();
         pc.puts("Attempting to restart network\r\n");
     }
@@ -992,7 +1077,7 @@
             FIZPort = new FIZCanon(p9, p10);
             pc.printf("Set Canon\r\n");
             break;
-case formatArri:
+        case formatArri:
             FIZPort = new FIZ_ArriCmotion(p9, p10);
             pc.printf("Set Arri\r\n");
             break;
@@ -1004,7 +1089,7 @@
 //    FIZPort = new FIZDigiPowerActive(p9, p10);
     COM1.baud(115200); // radio port
 
-    Thread serialTask(ethernetTask, &UserSettings, osPriorityHigh, 256 * 4);
+    Thread ethernetThread(ethernetTask, &UserSettings, osPriorityHigh, 256 * 4);
     led1 = 0;
     inputTimer.reset();
     inputTimer.start();