Vorlage für Projekt

Dependencies:   EthernetInterface mbed-rtos

Fork of Telemetrie_eth_h2m by Hochschule München

Committer:
HMFK03LST1
Date:
Mon Nov 30 14:27:16 2015 +0000
Revision:
7:a269ac8c3259
Parent:
6:abadad863420
Child:
8:2cd23c180dfe
Server Client UDP fix

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