gefixte version 03122015

Dependencies:   EthernetInterface mbed-rtos

Fork of Telemetrie_eth_h2m by Hochschule München

Revision:
3:94a735c744ff
Parent:
2:e19b937a29c1
Child:
4:5881695ba67a
--- a/Telemetry.cpp	Sat Oct 31 17:33:18 2015 +0000
+++ b/Telemetry.cpp	Sun Nov 22 13:48:21 2015 +0000
@@ -1,15 +1,41 @@
-/*------------------------------------------------*/
-/*Autor: Sebastian Donner                         */
-/*------------------------------------------------*/
+ /** Ethernet Interface for send/receive Datastructs over udp 
+ *
+ *
+ * By Sebastian Donner
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use,
+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following
+ * conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
 
 #include "Telemetry.h"
 
-//Debug Schnittstelle
+/**Debug Schnittstelle
+*/
 #ifdef DEBUG
 Serial debug(USBTX, USBRX);
+debug.baud(115200);
 #endif
 
-//Konstruktoren
+/**Konstruktoren
+*/
 EthernetInterface   eth;
 TCPSocketConnection sock_tcp;
 UDPSocket           sock_udp_send;  //send socket
@@ -18,14 +44,18 @@
 
     
 
-//Globale Variable
-bool InitSucceed = false;
-
+/** Create a new Ethernet interface
+  *
+  */
 Telemetry::Telemetry()
 {
-    
+    InitSucceed = false;
 }
 
+
+/** Init a Serial Debug interface
+  *
+  */
 #ifdef DEBUG
 void Telemetry::InitUSBSerialConnection()
 {
@@ -33,6 +63,179 @@
 }
 #endif
 
+
+
+
+/** Init Ethernet interface
+  * mit DHCP max. 10 Sekunden 
+*/
+bool Telemetry::InitEthernetConnection()
+{   
+    bool ReturnValue = false;
+    #ifdef DEBUG
+    debug.printf("Initalisiere LAN Verbindung mit DHCP\r\n\r\n");
+    #endif
+    
+    //Schnittstelle nur einmal initialisieren, sonst gibt es Fehler!
+    if (!InitSucceed)
+    {
+        if (eth.init()==0)  //Init Interface
+        {
+            InitSucceed = true;
+        }
+    }
+    
+    //Nur wenn Initialisierung erfolgreich war!
+    if (InitSucceed)
+    {
+        #ifdef DEBUG
+        serial.printf("Verbinde\r\n\r\n");
+        #endif
+        ip_self = eth.getIPAddress();
+        
+        if (eth.connect(10000)==0) //CONNECT
+        {
+            #ifdef DEBUG
+            printf("IP Adresse: %s\r\n\r\n", eth.getIPAddress());
+            #endif
+            ReturnValue = true;
+        }
+        else
+        {
+            #ifdef DEBUG
+            serial.printf("DHCP fail!\r\n\r\n");
+            #endif
+            ReturnValue = false;
+        }
+    }
+    
+    return ReturnValue;
+}
+
+
+/** Init Ethernet interface
+  * ohne DHCP max. 2.5 Sekunden 
+*/
+bool Telemetry::InitEthernetConnection(const char* IPAdress, const char* SubNetMask, const char* GateWay)
+{   
+    bool ReturnValue = false;
+    #ifdef DEBUG
+    printf("Initalisiere LAN Verbindung ohne DHCP\r\n\r\n");
+    printf("IP: %s - GateWay: %s - SubNetMask: %s\r\n\r\n",IPAdress, GateWay, SubNetMask);
+    #endif
+    
+    //Schnittstelle nur einmal initialisieren, sonst gibt es Fehler!
+    if (!InitSucceed)
+    {
+        if (eth.init(IPAdress, SubNetMask, GateWay)==0)  //Init Interface
+        {
+            InitSucceed = true;
+        }
+    }
+    
+    //Nur wenn Initialisierung erfolgreich war!
+    if (InitSucceed)
+    {   
+        if (eth.connect(2500)==0) //CONNECT
+        {
+            #ifdef DEBUG
+            serial.printf("Init success!");
+            #endif
+            
+            ReturnValue = true;
+        }
+        else
+        {
+            #ifdef DEBUG
+            serial.printf("Init fail!");
+            #endif
+            ReturnValue = false;
+        }
+    }    
+    
+    return ReturnValue;
+}
+
+
+//! Close Connection
+void Telemetry::CloseEthernetConnection()
+{
+    eth.disconnect();
+    InitSucceed = false;
+    
+    #ifdef DEBUG
+    serial.printf("LAN Stack close\r\n\r\n");
+    #endif
+}
+
+
+//! Connect Port TCP 
+void Telemetry::ConnectSocket_tcp(Endpoint Host)
+{
+    sock_tcp.connect(Host.get_address(), Host.get_port());
+    sock_tcp.set_blocking(false, 0);
+    #ifdef DEBUG
+    serial.printf("Open TCP Socket to IP: %s:%d.\r\n\r\n",Host.get_address(), Host.get_port());
+    #endif
+}
+
+
+//! Connect Port UDP receive 
+void Telemetry::ConnectSocket_udp_rec(int Port)
+{
+    sock_udp_rec.bind(Port);
+    sock_udp_rec.set_blocking(false, 0);  
+
+    #ifdef DEBUG
+    serial.printf("Open UDP_receive Socket on Port:%d.\r\n\r\n",Port);
+    #endif
+}
+
+
+//! Connect Port UDP send
+void Telemetry::ConnectSocket_udp_send()
+{
+    sock_udp_send.init();
+ 
+    #ifdef DEBUG
+    serial.printf("Open UDP_send Socket.\r\n\r\n");
+    #endif
+}
+
+
+//! Close Port TCP
+void Telemetry::CloseSocket_tcp()
+{
+    sock_tcp.close();
+
+    #ifdef DEBUG
+    serial.printf("TCP Socket closed.\r\n\r\n");
+    #endif
+}
+
+
+//! Close Port UDP receive
+void Telemetry::CloseSocket_udp_rec()
+{
+ sock_udp_rec.close();   
+  #ifdef DEBUG
+    serial.printf("UDP receive Socket closed.\r\n\r\n");
+  #endif
+}
+
+
+//! Close Port UDP send
+void Telemetry::CloseSocket_udp_send()
+{
+    sock_udp_send.close();
+
+    #ifdef DEBUG
+    serial.printf("UDP send Socket closed.\r\n\r\n");
+    #endif
+}
+
+
+//! Struct Check Sum calc
 char Telemetry::do_cs(char* buffer)
 {
     char ck_a=0;
@@ -48,172 +251,14 @@
 }
 
 
-//Funktion überladen. Ohne Parameter DHCP und 10 Sekunden Timeout. Mit Parameter kein DHCP und 3 Sekunden Timeout
-bool Telemetry::InitEthernetConnection()
-{   
-    bool ReturnValue = false;
-    #ifdef DEBUG
-    debug.printf("Initalisiere LAN Verbindung mit DHCP\r\n\r\n");
-    #endif
-    
-    //Schnittstelle nur einmal initialisieren, sonst gibt es Fehler!
-    if (!InitSucceed)
-    {
-        if (eth.init()==0)  //Init Interface
-        {
-            InitSucceed = true;
-            ReturnValue = true;
-        }
-    }
-    
-    //Nur wenn Initialisierung erfolgreich war!
-    if (InitSucceed)
-    {
-        #ifdef DEBUG
-        serial.printf("Verbinde\r\n\r\n");
-        #endif
-        ip_self = eth.getIPAddress();
-        
-        if (eth.connect(8000)==0) //CONNECT
-        {
-            printf("IP Adresse: %s\r\n\r\n", eth.getIPAddress());
-            ReturnValue = true;
-        }
-        else
-        {
-            ReturnValue = false;
-        }
-    }
-    
-    #ifdef DEBUG
-    if (ReturnValue == false)
-    {
-        serial.printf("DHCP fail!\r\n\r\n");
-    }
-    #endif
-    
-    return ReturnValue;
-}
-
-bool Telemetry::InitEthernetConnection(const char* IPAdress, const char* SubNetMask, const char* GateWay)
-{   
-    bool ReturnValue = false;
-    //#ifdef DEBUG
-    printf("Initalisiere LAN Verbindung ohne DHCP\r\n\r\n");
-    printf("IP: %s - GateWay: %s - SubNetMask: %s\r\n\r\n",IPAdress, GateWay, SubNetMask);
-    //#endif
-    
-    //Schnittstelle nur einmal initialisieren, sonst gibt es Fehler!
-    if (!InitSucceed)
-    {
-        if (eth.init(IPAdress, SubNetMask, GateWay)==0)  //Init Interface
-        {
-            InitSucceed = true;
-            ReturnValue = true;
-        }
-    }
-    
-    //Nur wenn Initialisierung erfolgreich war!
-    if (InitSucceed)
-    {   
-        if (eth.connect(2000)==0) //CONNECT
-        {
-            #ifdef DEBUG
-            serial.printf("Init success!");
-            #endif
-            
-            ReturnValue = true;
-        }
-        else
-        {
-            ReturnValue = false;
-        }
-    }
-    
-    #ifdef DEBUG
-    if (ReturnValue == false)
-    {
-        serial.printf("Init fail!");
-    }
-    #endif
-    
-    return ReturnValue;
-}
-
-
-void Telemetry::CloseEthernetConnection()
-{
-    eth.disconnect();
-    InitSucceed = false;
-    
-    #ifdef DEBUG
-    serial.printf("LAN Stack close\r\n\r\n");
-    #endif
-}
-
-void Telemetry::ConnectSocket_tcp(Endpoint Host)
-{
-    sock_tcp.connect(Host.get_address(), Host.get_port());
-    sock_tcp.set_blocking(false, 0);
-    #ifdef DEBUG
-    serial.printf("Open TCP Socket to IP: %s:%d.\r\n\r\n",Host.get_address(), Host.get_port());
-    #endif
-}
-
-void Telemetry::ConnectSocket_udp_rec(int Port)
-{
-    sock_udp_rec.bind(Port);
-    sock_udp_rec.set_blocking(false, 0);  
-
-    #ifdef DEBUG
-    serial.printf("Open UDP_receive Socket on Port:%d.\r\n\r\n",Port);
-    #endif
-}
-
-void Telemetry::ConnectSocket_udp_send()
-{
-    sock_udp_send.init();
- 
-    #ifdef DEBUG
-    serial.printf("Open UDP_send Socket.\r\n\r\n");
-    #endif
-}
-
-
-void Telemetry::CloseSocket_tcp()
-{
-    sock_tcp.close();
-
-    #ifdef DEBUG
-    serial.printf("TCP Socket closed.\r\n\r\n");
-    #endif
-}
-
-
-void Telemetry::CloseSocket_udp_send()
-{
-    sock_udp_send.close();
-
-    #ifdef DEBUG
-    serial.printf("UDP send Socket closed.\r\n\r\n");
-    #endif
-}
-
-
-void Telemetry::CloseSocket_udp_rec()
-{
- sock_udp_rec.close();   
-  #ifdef DEBUG
-    serial.printf("UDP receive Socket closed.\r\n\r\n");
-  #endif
-}
-
+//! Read UDP Packet
 int Telemetry::Rec_Data_UDP(char *buffer, int size)
 {
   return sock_udp_rec.receiveFrom(input_Host, buffer, size);    
 }
 
 
+//! Check UDP Packet of containing Struct
 bool Telemetry::Rec_Struct_UDP(char *buffer)
 {
   sock_udp_rec.receiveFrom(input_Host, buffer, 255);    
@@ -222,20 +267,16 @@
 }
 
 
+//! Read TCP Packet
 int Telemetry::Rec_Data_TCP(char *buffer,int size)
 { 
         return sock_tcp.receive(buffer, size);
 }
 
 
+//! Send UDP Packet
 void Telemetry::Send_Data_UDP(Endpoint Server, char* Daten, int size )
-{
-    // convert String to char*
-    //(string Daten)
-    //const char *DataBuf = Daten.c_str();
-    //char DataPaket[Daten.length()];
-    //strcpy(DataPaket,DataBuf);
-    
+{   
     sock_udp_send.sendTo(Server, Daten, size);   
 
     #ifdef DEBUG
@@ -244,6 +285,7 @@
 }
 
 
+//! Send Struct as UDP Packet
 void Telemetry::Send_Struct_UDP(Endpoint Server, char* Daten)
 {   
     Daten[(*Daten - 1)] = do_cs(Daten);
@@ -251,48 +293,25 @@
 }
 
 
-
-void Telemetry::Send_Data_TCP(char* Host, string Daten, string Username, string Passwort)
+//! Send TCP Packet
+void Telemetry::Send_Data_TCP(char* Host, char* Buffer)
 {
     
-    //Datenpaket schnüren
-    string DATEN = "Username=" + Username + "&Passwort=" + Passwort + "&Paket=" + Daten;
-    
-    string POST = "POST /H2MClient/h2m_client.php HTTP/1.1\r\n";
-    
-    string HostString = Host;
-    string HOST = "Host: " + HostString + "\r\n";
-    
-    string CONTENTTYPE = "Content-Type: application/x-www-form-urlencoded\r\n";
-    
-    string Length;
-    stringstream convert;
-    convert << DATEN.length();
-    Length = convert.str();
-    string CONTENTLENGTH = "Content-Length: " + Length + "\r\n\r\n";
-    
-    
-    string datenpaket = POST + HOST + CONTENTTYPE + CONTENTLENGTH + DATEN + "\r\n";
-    
-    //Umwandeln in char*
+    /* Umwandeln in char*
     const char *DataBuf = datenpaket.c_str();
     char DataPaket[datenpaket.length()];
     strcpy(DataPaket,DataBuf);
-
+    */
     #ifdef DEBUG
     serial.printf("----\r\n%s----\r\n\r\n",DataPaket);
     serial.printf("Sende Paket.\r\n\r\n");
     #endif
     
-    sock_tcp.send_all(DataPaket, sizeof(DataPaket)-1);
+    sock_tcp.send_all(Buffer, sizeof(Buffer)-1);
     
     #ifdef DEBUG
     serial.printf("Paket gesendet.\r\n\r\n");
     #endif
-    
-    #ifdef LED
-    ledwfa = 0;
-    #endif
 }