1.0

Dependencies:   EthernetInterface mbed-rtos

Committer:
HMFK03LST1
Date:
Wed Jul 20 14:14:11 2016 +0000
Revision:
11:f34b079ae15c
Parent:
10:ad8346bafe5e
first

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HMFK03LST1 0:c0179f2ad295 1
HMFK03LST1 0:c0179f2ad295 2 #ifndef TELEMETRY
HMFK03LST1 0:c0179f2ad295 3 #define TELEMETRY
HMFK03LST1 0:c0179f2ad295 4
HMFK03LST1 1:2df3958877f2 5 //#define DEBUG
HMFK03LST1 0:c0179f2ad295 6
HMFK03LST1 0:c0179f2ad295 7 #include "mbed.h"
HMFK03LST1 0:c0179f2ad295 8 #include "EthernetInterface.h"
HMFK03LST1 3:94a735c744ff 9
HMFK03LST1 9:4e882da5a890 10 /* Ethernet Interface for send/receive Datastructs over udp
HMFK03LST1 9:4e882da5a890 11
HMFK03LST1 9:4e882da5a890 12 By Sebastian Donner
HMFK03LST1 9:4e882da5a890 13
HMFK03LST1 9:4e882da5a890 14 Permission is hereby granted, free of charge, to any person
HMFK03LST1 9:4e882da5a890 15 obtaining a copy of this software and associated documentation
HMFK03LST1 9:4e882da5a890 16 files (the "Software"), to deal in the Software without
HMFK03LST1 9:4e882da5a890 17 restriction, including without limitation the rights to use,
HMFK03LST1 9:4e882da5a890 18 copy, modify, merge, publish, distribute, sublicense, and/or sell
HMFK03LST1 9:4e882da5a890 19 copies of the Software, and to permit persons to whom the
HMFK03LST1 9:4e882da5a890 20 Software is furnished to do so, subject to the following
HMFK03LST1 9:4e882da5a890 21 conditions:
HMFK03LST1 9:4e882da5a890 22
HMFK03LST1 9:4e882da5a890 23 The above copyright notice and this permission notice shall be
HMFK03LST1 9:4e882da5a890 24 included in all copies or substantial portions of the Software.
HMFK03LST1 9:4e882da5a890 25
HMFK03LST1 9:4e882da5a890 26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
HMFK03LST1 9:4e882da5a890 27 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
HMFK03LST1 9:4e882da5a890 28 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
HMFK03LST1 9:4e882da5a890 29 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HMFK03LST1 9:4e882da5a890 30 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
HMFK03LST1 9:4e882da5a890 31 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
HMFK03LST1 9:4e882da5a890 32 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
HMFK03LST1 9:4e882da5a890 33 OTHER DEALINGS IN THE SOFTWARE.
HMFK03LST1 9:4e882da5a890 34
HMFK03LST1 9:4e882da5a890 35 @code
HMFK03LST1 9:4e882da5a890 36 #include "mbed.h"
HMFK03LST1 9:4e882da5a890 37 #include "Telemetry.h"
HMFK03LST1 9:4e882da5a890 38
HMFK03LST1 9:4e882da5a890 39 Telemetry Network(p21,p22);
HMFK03LST1 8:2cd23c180dfe 40
HMFK03LST1 9:4e882da5a890 41 Serial debug(USBTX, USBRX);
HMFK03LST1 9:4e882da5a890 42 uint8_t buffer[255];
HMFK03LST1 9:4e882da5a890 43 int SELF_PORT = 8889;
HMFK03LST1 8:2cd23c180dfe 44
HMFK03LST1 9:4e882da5a890 45 typedef struct struct_test
HMFK03LST1 9:4e882da5a890 46 {
HMFK03LST1 9:4e882da5a890 47 struct_test()
HMFK03LST1 9:4e882da5a890 48 :length(9), ID(0)
HMFK03LST1 9:4e882da5a890 49 {}
HMFK03LST1 9:4e882da5a890 50 uint8_t length ;//1
HMFK03LST1 9:4e882da5a890 51 uint8_t ID ;//2
HMFK03LST1 9:4e882da5a890 52 uint8_t flag_bit0 : 1 ;//3.0
HMFK03LST1 9:4e882da5a890 53 uint8_t flag_bit1 : 1 ;//3.1
HMFK03LST1 9:4e882da5a890 54 uint8_t flagbyte_2 ;//4
HMFK03LST1 9:4e882da5a890 55 uint16_t word ;//6
HMFK03LST1 9:4e882da5a890 56 int16_t counter ;//8
HMFK03LST1 9:4e882da5a890 57 uint8_t cs ;//9
HMFK03LST1 9:4e882da5a890 58 } Tstruct_test;
HMFK03LST1 9:4e882da5a890 59
HMFK03LST1 9:4e882da5a890 60
HMFK03LST1 9:4e882da5a890 61 struct_test test;
HMFK03LST1 9:4e882da5a890 62
HMFK03LST1 9:4e882da5a890 63 uint8_t* struct_id[5];
HMFK03LST1 9:4e882da5a890 64
HMFK03LST1 9:4e882da5a890 65 main()
HMFK03LST1 9:4e882da5a890 66 {
HMFK03LST1 9:4e882da5a890 67 debug.baud(115200);
HMFK03LST1 9:4e882da5a890 68 Network.InitEthernetConnection();
HMFK03LST1 9:4e882da5a890 69 Network.ConnectSocket_UDP_Client();
HMFK03LST1 9:4e882da5a890 70 Network.ConnectSocket_UDP_Server(SELF_PORT);
HMFK03LST1 9:4e882da5a890 71
HMFK03LST1 9:4e882da5a890 72 struct_id[0]=&test.length;
HMFK03LST1 9:4e882da5a890 73
HMFK03LST1 9:4e882da5a890 74 debug.printf("%s",Network.ip_self);
HMFK03LST1 9:4e882da5a890 75 while(1)
HMFK03LST1 9:4e882da5a890 76 {
HMFK03LST1 9:4e882da5a890 77 Network.Rec_Data_UDP_Server((char*)buffer, 255);
HMFK03LST1 9:4e882da5a890 78 if (Network.Rec_Struct(buffer)) // receive and CS check
HMFK03LST1 9:4e882da5a890 79 {
HMFK03LST1 9:4e882da5a890 80 uint8_t* b_pointer;
HMFK03LST1 9:4e882da5a890 81
HMFK03LST1 9:4e882da5a890 82 b_pointer = struct_id[buffer[1]]; // Pointer to received Struct
HMFK03LST1 9:4e882da5a890 83
HMFK03LST1 9:4e882da5a890 84 for(int i = 0; i < (buffer[0]); i++) // Copy buffer to Struct
HMFK03LST1 9:4e882da5a890 85 {
HMFK03LST1 9:4e882da5a890 86 b_pointer = buffer[i];
HMFK03LST1 9:4e882da5a890 87 b_pointer++;
HMFK03LST1 9:4e882da5a890 88 }
HMFK03LST1 9:4e882da5a890 89
HMFK03LST1 9:4e882da5a890 90 b_pointer = struct_id[buffer[1]]; // Pointer to received Struct
HMFK03LST1 9:4e882da5a890 91
HMFK03LST1 9:4e882da5a890 92 Network.Send_Struct_UDP_Client(Network.input_Server, b_pointer); // Send received Struct back to sender
HMFK03LST1 9:4e882da5a890 93 }
HMFK03LST1 9:4e882da5a890 94
HMFK03LST1 9:4e882da5a890 95 wait(1);
HMFK03LST1 9:4e882da5a890 96 }
HMFK03LST1 9:4e882da5a890 97 }
HMFK03LST1 9:4e882da5a890 98 @endcode
HMFK03LST1 6:abadad863420 99 */
HMFK03LST1 3:94a735c744ff 100
HMFK03LST1 3:94a735c744ff 101
HMFK03LST1 0:c0179f2ad295 102
HMFK03LST1 0:c0179f2ad295 103 class Telemetry
HMFK03LST1 0:c0179f2ad295 104 {
HMFK03LST1 0:c0179f2ad295 105 public:
HMFK03LST1 3:94a735c744ff 106 //! Create Ethernet Interface
HMFK03LST1 10:ad8346bafe5e 107 Telemetry(PinName ora, PinName gre);
HMFK03LST1 7:a269ac8c3259 108
HMFK03LST1 7:a269ac8c3259 109 DigitalOut orange;
HMFK03LST1 7:a269ac8c3259 110 DigitalOut green;
HMFK03LST1 7:a269ac8c3259 111
HMFK03LST1 3:94a735c744ff 112 //! IP Address of mbed Interface
HMFK03LST1 2:e19b937a29c1 113 char* ip_self;
HMFK03LST1 3:94a735c744ff 114
HMFK03LST1 3:94a735c744ff 115 //! IP Addess and Port of databank Server
HMFK03LST1 2:e19b937a29c1 116 Endpoint db_server;
HMFK03LST1 3:94a735c744ff 117
HMFK03LST1 5:a96e4e59c710 118 //! IP Addess and Port of control Server
HMFK03LST1 2:e19b937a29c1 119 Endpoint controller;
HMFK03LST1 3:94a735c744ff 120
HMFK03LST1 7:a269ac8c3259 121 //! IP Addess and Port of MSG on Server Port
HMFK03LST1 7:a269ac8c3259 122 Endpoint input_Server;
HMFK03LST1 7:a269ac8c3259 123
HMFK03LST1 7:a269ac8c3259 124 //! IP Addess and Port of MSG on Client Port
HMFK03LST1 7:a269ac8c3259 125 Endpoint input_Client;
HMFK03LST1 3:94a735c744ff 126
HMFK03LST1 2:e19b937a29c1 127
HMFK03LST1 0:c0179f2ad295 128 #ifdef DEBUG
HMFK03LST1 0:c0179f2ad295 129 void InitUSBSerialConnection();
HMFK03LST1 0:c0179f2ad295 130 #endif
HMFK03LST1 3:94a735c744ff 131
HMFK03LST1 3:94a735c744ff 132 //! init with DHCP
HMFK03LST1 0:c0179f2ad295 133 bool InitEthernetConnection();
HMFK03LST1 3:94a735c744ff 134
HMFK03LST1 3:94a735c744ff 135 //! init with Static IP
HMFK03LST1 0:c0179f2ad295 136 bool InitEthernetConnection(const char* IPAdress, const char* SubNetMask, const char* GateWay);
HMFK03LST1 3:94a735c744ff 137
HMFK03LST1 3:94a735c744ff 138
HMFK03LST1 3:94a735c744ff 139 //! Close Connection
HMFK03LST1 0:c0179f2ad295 140 void CloseEthernetConnection();
HMFK03LST1 3:94a735c744ff 141
HMFK03LST1 3:94a735c744ff 142
HMFK03LST1 3:94a735c744ff 143 //! Connect Port TCP
HMFK03LST1 7:a269ac8c3259 144 void ConnectSocket_TCP(Endpoint Host);
HMFK03LST1 3:94a735c744ff 145
HMFK03LST1 7:a269ac8c3259 146 //! Connect Port for UDP Client
HMFK03LST1 7:a269ac8c3259 147 void ConnectSocket_UDP_Client();
HMFK03LST1 3:94a735c744ff 148
HMFK03LST1 7:a269ac8c3259 149 //! Connect Port for UDP Server
HMFK03LST1 7:a269ac8c3259 150 void ConnectSocket_UDP_Server(int Port);
HMFK03LST1 3:94a735c744ff 151
HMFK03LST1 3:94a735c744ff 152
HMFK03LST1 3:94a735c744ff 153 //! Close Port TCP
HMFK03LST1 7:a269ac8c3259 154 void CloseSocket_TCP();
HMFK03LST1 0:c0179f2ad295 155
HMFK03LST1 7:a269ac8c3259 156 //! Close Port UDP Client
HMFK03LST1 7:a269ac8c3259 157 void CloseSocket_UDP_Client();
HMFK03LST1 3:94a735c744ff 158
HMFK03LST1 3:94a735c744ff 159 //! Close Port UDP send
HMFK03LST1 7:a269ac8c3259 160 void CloseSocket_UDP_Server();
HMFK03LST1 3:94a735c744ff 161
HMFK03LST1 3:94a735c744ff 162
HMFK03LST1 3:94a735c744ff 163 //! Struct Check Sum calc
HMFK03LST1 6:abadad863420 164 uint8_t do_cs(uint8_t *buffer);
HMFK03LST1 7:a269ac8c3259 165
HMFK03LST1 3:94a735c744ff 166
HMFK03LST1 7:a269ac8c3259 167 //! Read UDP Packet on Client Port
HMFK03LST1 7:a269ac8c3259 168 int Rec_Data_UDP_Client(char *buffer, int size);
HMFK03LST1 7:a269ac8c3259 169
HMFK03LST1 7:a269ac8c3259 170 //! Read UDP Packet on Server Port
HMFK03LST1 7:a269ac8c3259 171 int Rec_Data_UDP_Server(char *buffer, int size);
HMFK03LST1 2:e19b937a29c1 172
HMFK03LST1 7:a269ac8c3259 173 //! Check Packet of containing Struct
HMFK03LST1 7:a269ac8c3259 174 bool Rec_Struct(uint8_t *buffer);
HMFK03LST1 3:94a735c744ff 175
HMFK03LST1 3:94a735c744ff 176 //! Read TCP Packet
HMFK03LST1 3:94a735c744ff 177 int Rec_Data_TCP(char *buffer, int size);
HMFK03LST1 3:94a735c744ff 178
HMFK03LST1 3:94a735c744ff 179
HMFK03LST1 7:a269ac8c3259 180 //! Send UDP Packet as Client
HMFK03LST1 7:a269ac8c3259 181 void Send_Data_UDP_Client(Endpoint Server, char* Daten, int size);
HMFK03LST1 7:a269ac8c3259 182
HMFK03LST1 7:a269ac8c3259 183 //! Send UDP Packet as Server
HMFK03LST1 7:a269ac8c3259 184 void Send_Data_UDP_Server(Endpoint Client, char* Daten, int size);
HMFK03LST1 7:a269ac8c3259 185
HMFK03LST1 7:a269ac8c3259 186 //! Send Struct as UDP Client
HMFK03LST1 7:a269ac8c3259 187 void Send_Struct_UDP_Client(Endpoint Server, uint8_t* Daten);
HMFK03LST1 3:94a735c744ff 188
HMFK03LST1 7:a269ac8c3259 189 //! Send Struct as UDP Server
HMFK03LST1 7:a269ac8c3259 190 void Send_Struct_UDP_Server(Endpoint Client, uint8_t* Daten);
HMFK03LST1 7:a269ac8c3259 191
HMFK03LST1 7:a269ac8c3259 192
HMFK03LST1 3:94a735c744ff 193 //! Send TCP Packet
HMFK03LST1 3:94a735c744ff 194 void Send_Data_TCP(char* Host, char* Daten);
HMFK03LST1 2:e19b937a29c1 195
HMFK03LST1 0:c0179f2ad295 196
HMFK03LST1 3:94a735c744ff 197 private:
HMFK03LST1 3:94a735c744ff 198 bool InitSucceed;
HMFK03LST1 2:e19b937a29c1 199
HMFK03LST1 0:c0179f2ad295 200 };
HMFK03LST1 0:c0179f2ad295 201
HMFK03LST1 0:c0179f2ad295 202 #endif