1.0

Dependencies:   EthernetInterface mbed-rtos

Revision:
0:c0179f2ad295
Child:
2:e19b937a29c1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Telemetry.cpp	Sat May 02 11:02:37 2015 +0000
@@ -0,0 +1,307 @@
+/*------------------------------------------------*/
+/*Autor: Sebastian Hauzenberger                   */
+/*------------------------------------------------*/
+
+#include "Telemetry.h"
+
+//LEDs definieren
+#ifdef LED
+DigitalOut ledserial(LED1);
+DigitalOut ledeth(LED2);
+DigitalOut ledsock(LED3);
+DigitalOut ledwfa(LED4);
+#endif
+
+//Serielle Schnittstelle definieren
+#ifdef DEBUG
+Serial serial(USBTX, USBRX);
+#endif
+
+//Konstruktoren
+EthernetInterface eth;
+TCPSocketConnection sock_tcp;
+UDPSocket           sock_udp;
+    
+
+//Globale Variable
+bool InitSucceed = false;
+
+Telemetry::Telemetry()
+{
+    
+}
+
+#ifdef DEBUG
+void Telemetry::InitUSBSerialConnection()
+{
+    serial.baud(115200);
+    
+    #ifdef LED
+    ledserial = 1;
+    #endif
+}
+#endif
+
+//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
+    serial.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
+        
+        if (eth.connect(10000)==0) //CONNECT
+        {
+            #ifdef DEBUG
+            serial.printf("IP Adresse: %s\r\n\r\n", eth.getIPAddress());
+            #endif
+            
+            #ifdef LED
+            ledeth = 1;
+            #endif
+            
+            ReturnValue = true;
+        }
+        else
+        {
+            ReturnValue = false;
+        }
+    }
+    
+    #ifdef DEBUG
+    if (ReturnValue == false)
+    {
+        serial.printf("Fehlgeschlagen!");
+    }
+    #endif
+    
+    return ReturnValue;
+}
+
+bool Telemetry::InitEthernetConnection(const char* IPAdress, const char* SubNetMask, const char* GateWay)
+{   
+    bool ReturnValue = false;
+    #ifdef DEBUG
+    serial.printf("Initalisiere LAN Verbindung ohne DHCP\r\n\r\n");
+    serial.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)
+    {
+        #ifdef DEBUG
+        serial.printf("Verbinde\r\n\r\n");
+        #endif
+        
+        if (eth.connect(3000)==0) //CONNECT
+        {
+            #ifdef DEBUG
+            serial.printf("IP Adresse: %s\r\n\r\n", eth.getIPAddress());
+            #endif
+            
+            #ifdef LED
+            ledeth = 1;
+            #endif
+            
+            ReturnValue = true;
+        }
+        else
+        {
+            ReturnValue = false;
+        }
+    }
+    
+    #ifdef DEBUG
+    if (ReturnValue == false)
+    {
+        serial.printf("Fehlgeschlagen!");
+    }
+    #endif
+    
+    return ReturnValue;
+}
+
+
+void Telemetry::CloseEthernetConnection()
+{
+    eth.disconnect();
+    
+    #ifdef DEBUG
+    serial.printf("LAN Verbindung geschlossen\r\n\r\n");
+    #endif
+    
+    #ifdef LED
+    ledeth = 0;
+    #endif
+}
+
+void Telemetry::ConnectSocket_tcp(char* Host)
+{
+    sock_tcp.connect(Host, 80);
+    
+    #ifdef DEBUG
+    serial.printf("TCP Socket geoeffnet.\r\n\r\n");
+    #endif
+    
+    #ifdef LED
+    ledsock = 1;
+    #endif
+}
+
+void Telemetry::ConnectSocket_udp()
+{
+    sock_udp.init();
+    
+    #ifdef DEBUG
+    serial.printf("UDP Socket geoeffnet.\r\n\r\n");
+    #endif
+    
+}
+
+void Telemetry::CloseSocket()
+{
+    sock_tcp.close();
+    sock_udp.close();
+
+    #ifdef DEBUG
+    serial.printf("TCP/UDP Socket geschlossen.\r\n\r\n");
+    #endif
+    
+    #ifdef LED
+    ledsock = 0;
+    #endif
+}
+
+
+void Telemetry::TransmitDataoverUDP(char *Host, int Port, string Daten)
+{
+    Endpoint data_server;
+    data_server.set_address(Host, Port);
+//Umwandeln in char*
+    const char *DataBuf = Daten.c_str();
+    char DataPaket[Daten.length()];
+    strcpy(DataPaket,DataBuf);
+    
+    #ifdef DEBUG
+    serial.printf("----\r\n%s----\r\n\r\n",DataPaket);
+    serial.printf("Sende Paket UDP.\r\n\r\n");
+    #endif
+    
+    sock_udp.sendTo(data_server, DataPaket, sizeof(DataPaket));   
+
+    #ifdef DEBUG
+    serial.printf("UDP Paket gesendet.\r\n\r\n");
+    #endif
+}
+
+void Telemetry::TransmitDataoverTCP(char *Host, string Daten, string Username, string Passwort)
+{
+    #ifdef LED
+    ledwfa = 1;
+    #endif
+    
+    //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*
+    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);
+    
+    #ifdef DEBUG
+    serial.printf("Paket gesendet.\r\n\r\n");
+    #endif
+    
+    #ifdef LED
+    ledwfa = 0;
+    #endif
+}
+
+void Telemetry::ReadAnswerandTransmitoverSerial()
+{
+    char buffer[300];
+    int ret;
+    while (true)
+    {
+        ret = sock_tcp.receive(buffer, sizeof(buffer)-1);
+        if (ret <= 0)
+            break;
+        buffer[ret] = '\0';
+        
+        #ifdef DEBUG
+        serial.printf("Received %d chars from server:\n%s\n", ret, buffer);
+        #endif
+    }
+    #ifdef LED
+    ledwfa = 0;
+    #endif
+}
+
+string Telemetry::ReadAnswer()
+{
+    char buffer[300];
+    int ret;
+    while (true)
+    {
+        ret = sock_tcp.receive(buffer, sizeof(buffer)-1);
+        if (ret <= 0)
+            break;
+        buffer[ret] = '\0';
+    }
+    
+    #ifdef LED
+    ledwfa = 0;
+    #endif
+    
+    return buffer;
+}