Used with eeprom_flash to write network configuration to STM32F103 flash

Dependents:   F103-Web-Server

Fork of my_eeprom_funcs by Chau Vo

Revision:
19:5671f3c25342
Parent:
18:d88314ae7979
Child:
20:858384cac44a
--- a/device_configuration.cpp	Sun Aug 21 17:46:55 2016 +0000
+++ b/device_configuration.cpp	Tue Aug 23 11:05:38 2016 +0000
@@ -42,14 +42,23 @@
 char strRemoteUdpServerIpAddr[16]; // RPC variable, converted from 16-bits u16server_ip_addr
 uint16_t u16RemoteUdpServerPort; // 16-bit variable to be compatible with eeprom functions
 // enable modes
-uint16_t u16EnableTcpServer = 0, u16EnableTcpClient = 0; // flags for enabling TCP server/client and UDP (UDP is always on for configuration)
-uint16_t u16EnableUdpServer = DEFAULT_ENABLE_FLAG_VALUE, u16EnableUdpClient = 0;
+uint16_t u16EnableTcpServer = DEFAULT_ENABLE_FLAG_VALUE, u16EnableTcpClient = DEFAULT_DISABLE_FLAG_VALUE; // flags for enabling TCP server/client and UDP (UDP is always on for configuration)
+uint16_t u16EnableUdpServer = DEFAULT_DISABLE_FLAG_VALUE, u16EnableUdpClient = DEFAULT_DISABLE_FLAG_VALUE;
 // extra
 uint8_t u8IpAddr[4]; // keep device ip address in 8-bits
 uint8_t u8MacAddr[6]; // keep mac in 8-bits
 uint8_t u8RemoteTcpServerIpAddr[4]; // remote TCP server ip address in 8-bits
 uint8_t u8RemoteUdpServerIpAddr[4]; // remote UDP server ip address in 8-bits
 
+
+// erase page
+void erase_device_configuration() {
+    // erase first_run flag
+    enableEEPROMWriting();
+    disableEEPROMWriting();
+}
+
+
 /*!
  * Function to write module network configuration
  * @param <char *buf> configuration buffer
@@ -213,7 +222,7 @@
         u8RemoteUdpServerIpAddr[2] = (uint8_t)(u16RemoteUdpServerIpAddr[2] & 0x00FF);
         u8RemoteUdpServerIpAddr[3] = (uint8_t)(u16RemoteUdpServerIpAddr[3] & 0x00FF);
         
-        // Remote port
+        // Remote UDP server port
         u16RemoteUdpServerPort = readEEPROMHalfWord(REMOTE_UDP_SERVER_PORT_POS);
         
         // Enable modes
@@ -237,9 +246,25 @@
         sprintf(strIpAddr, DEFAULT_IP_ADDRESS);
         sprintf(strIpSubnet, DEFAULT_IP_SUBNET);
         sprintf(strIpGateway, DEFAULT_IP_GATEWAY);
+        sprintf(strMacAddr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", u8MacAddr[0], u8MacAddr[1], u8MacAddr[2], u8MacAddr[3], u8MacAddr[4], u8MacAddr[5]);
+        
+        u16LocalTcpServerPort = DEFAULT_LOCAL_TCP_SERVER_PORT;
+        u16LocalUdpServerPort = DEFAULT_LOCAL_UDP_SERVER_PORT;
+        
         sprintf(strRemoteTcpServerIpAddr, DEFAULT_REMOTE_TCP_SERVER_IP);
+        u16RemoteTcpServerPort = DEFAULT_REMOTE_TCP_SERVER_PORT;
+        
         sprintf(strRemoteUdpServerIpAddr, DEFAULT_REMOTE_UDP_SERVER_IP);
-        sprintf(strMacAddr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", u8MacAddr[0], u8MacAddr[1], u8MacAddr[2], u8MacAddr[3], u8MacAddr[4], u8MacAddr[5]);
+        u16RemoteUdpServerPort = DEFAULT_REMOTE_UDP_SERVER_PORT;
+        
+        u16AutoTransmitFlag = DEFAULT_DISABLE_FLAG_VALUE;
+        u16TransmitPeriod = DEFAULT_TRANSMIT_PERIOD;
+        
+        u16EnableTcpServer = DEFAULT_ENABLE_FLAG_VALUE;
+        u16EnableTcpClient = DEFAULT_DISABLE_FLAG_VALUE;
+        u16EnableUdpServer = DEFAULT_DISABLE_FLAG_VALUE;
+        u16EnableUdpClient = DEFAULT_DISABLE_FLAG_VALUE;
+        
     }
     
     INFO("Successful");
@@ -264,8 +289,36 @@
     INFO("UDP client: %s", (u16EnableUdpClient == DEFAULT_ENABLE_FLAG_VALUE) ? "enabled" : "disable");
 }
 
-void reset_device_configuration() {
-    // erase first_run flag
-    enableEEPROMWriting();
-    disableEEPROMWriting();
+void reset_default_device_configuration() {
+    uint16_t ip[4] = {192,168,0,120};
+    uint16_t subnet[4] = {255,255,255,0};
+    uint16_t gateway[4] = {192,168,0,1};
+    uint16_t mac[3] = {u8MacAddr[3],u8MacAddr[4],u8MacAddr[5]};
+    
+    uint16_t tcp_port = DEFAULT_LOCAL_TCP_SERVER_PORT;
+    uint16_t udp_port = DEFAULT_LOCAL_UDP_SERVER_PORT;
+    
+    uint16_t remote_tcp_ip[4] = {192,168,0,2};
+    uint16_t remote_tcp_port = DEFAULT_REMOTE_TCP_SERVER_PORT;
+    
+    uint16_t remote_udp_ip[4] = {192,168,0,2};
+    uint16_t remote_udp_port = DEFAULT_REMOTE_UDP_SERVER_PORT;
+    
+    uint16_t auto_transmit = DEFAULT_DISABLE_FLAG_VALUE;
+    uint16_t transmit_period = DEFAULT_TRANSMIT_PERIOD;
+    
+    uint16_t enable_tcp_server = DEFAULT_ENABLE_FLAG_VALUE;
+    uint16_t enable_tcp_client = DEFAULT_DISABLE_FLAG_VALUE;
+    uint16_t enable_udp_server = DEFAULT_DISABLE_FLAG_VALUE;
+    uint16_t enable_udp_client = DEFAULT_DISABLE_FLAG_VALUE;
+    
+    // erase the device configured flag
+    erase_device_configuration();
+    
+    // write new one
+    write_device_configuration(ip, subnet, gateway, mac,
+            tcp_port, udp_port,
+            remote_tcp_ip, remote_tcp_port, auto_transmit, transmit_period,
+            remote_udp_ip, remote_udp_port,
+            enable_tcp_server, enable_tcp_client, enable_udp_server, enable_udp_client);
 }