Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: XBeeLib mbed HvZAlphaNumLib HvZServerLib
udp.hpp
00001 #ifndef _UDP 00002 #define _UDP 00003 00004 #include <string> 00005 #include <vector> 00006 #include <queue> 00007 #include <cctype> 00008 #include <iostream> 00009 00010 #include "mbed.h" 00011 #include "if/eth/EthernetNetIf.h" 00012 #include "iHvZ.hpp" 00013 00014 #include "util/types.h" 00015 #include "net/net.h" 00016 00017 #include "core/net.h" 00018 #include "core/host.h" 00019 00020 #define HVZ_PORT 4489 00021 #define HVZ_HOSTNAME "kylelemons.net" 00022 00023 enum UDPErr 00024 { 00025 __UDPSOCKET_MIN = -0xFFFF, 00026 UDPSOCKET_SETUP, ///<UDPSocket not properly configured 00027 UDPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized 00028 UDPSOCKET_MEM, ///<Not enough mem 00029 UDPSOCKET_INUSE, ///<Interface / Port is in use 00030 //... 00031 UDPSOCKET_OK = 0 ///<Success 00032 }; 00033 00034 ///UDP Socket Event(s) 00035 enum UDPSocketEvent //Only one event here for now, but keeps that model in case we need to implement some others 00036 { 00037 UDPSOCKET_READABLE, ///<Data in buf 00038 }; 00039 00040 enum responseType 00041 { 00042 HVZ_REG = 0, 00043 HVZ_TAG, 00044 HVZ_PULL 00045 }; 00046 00047 enum pullType 00048 { 00049 PULL_DEV = 0, 00050 PULL_MAC 00051 }; 00052 00053 class NetUdpSocket; 00054 enum NetUdpSocketEvent; 00055 00056 class UDP { 00057 public: 00058 00059 iHvZ *m_game; //< Maintain a connection to the active game 00060 00061 Ethernet_MAC mac; 00062 Ethernet eth; 00063 EthernetNetIf conn; 00064 char srvr_resp[128]; 00065 int registered; 00066 00067 DigitalIn m_eth; 00068 InterruptIn m_eth_int; //< Get an interrupt whenever the ethernet button is hit 00069 Timeout m_eth_chk; 00070 00071 public: 00072 00073 /* Creates a new socket */ 00074 UDP(iHvZ *game, PinName eth_in); 00075 00076 /* Closes and destroys socket */ 00077 ~UDP(); //close() 00078 00079 /* Check to see if theres an ethernet connection */ 00080 void check(); 00081 00082 /* Sends data to Server */ 00083 int sendto(const char* buf, int len, Host* pHost); 00084 00085 /* Receive from Server */ 00086 int recvfrom(char* buf, int len, Host* pHost); 00087 00088 /* Closes the socket */ 00089 UDPErr close(); 00090 00091 /* Checks to see if theres an ethernet link */ 00092 void ethlink(); 00093 00094 /* Read response from Server */ 00095 void read(UDPSocketEvent e); 00096 00097 /* Set the game device and tag IDs */ 00098 void set_ids(char *response); 00099 00100 /* Registers the new device */ 00101 inline void register_device(Ethernet_MAC originMAC, Host *host) 00102 { 00103 char data[34]; 00104 00105 sprintf(data, "iHvZ\001reg\001%02X%02X%02X%02X%02X%02X\001end", originMAC.octet[0], originMAC.octet[1], 00106 originMAC.octet[2], originMAC.octet[3], originMAC.octet[4], originMAC.octet[5]); 00107 00108 sendto(data, strlen(data), host); 00109 } 00110 00111 /* Sends a player tag to the server of the tagged device */ 00112 inline void send_tag(Host *host, vector<string> taggedIDs); 00113 00114 //Requests an update of all settings from Server 00115 inline void send_pull(Host *host); 00116 00117 inline int get_ack_err(char *response, responseType type) 00118 { 00119 char ack_err[3]; 00120 int i; 00121 00122 memset(ack_err,0,3); 00123 00124 for( i = 5; i < 8; i++ ) 00125 memcpy(&(ack_err[i-5]),&(response[i]),1); 00126 00127 if( strcmp(ack_err,"ack") == 0 ) { 00128 00129 switch( type ) { 00130 case HVZ_REG: 00131 return 1; 00132 00133 case HVZ_PULL: 00134 return 1; 00135 00136 case HVZ_TAG: 00137 // If sent invalid taggedIDs to server, the response will be ack 00138 // but it will not have valid tagged IDs, this is the size 00139 // of such a response 00140 if( strlen(response) > 16 ) 00141 return 1; 00142 00143 else 00144 return 0; 00145 00146 } 00147 } 00148 00149 return 0; 00150 } 00151 00152 /* Update the Settings for the game */ 00153 void update_settings(char *response); 00154 00155 class CDummy; 00156 00157 ///Setups callback 00158 template<class T> 00159 void setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) ) 00160 { 00161 m_pCbItem = (CDummy*) pItem; 00162 m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod; 00163 } 00164 00165 ///Disables callback 00166 void resetOnEvent(); 00167 00168 protected: 00169 void onNetUdpSocketEvent(NetUdpSocketEvent e); 00170 UDPErr checkInst(); 00171 00172 private: 00173 NetUdpSocket* m_pNetUdpSocket; 00174 00175 CDummy* m_pCbItem; 00176 void (CDummy::*m_pCbMeth)(UDPSocketEvent); 00177 00178 void (*m_pCb)(UDPSocketEvent); 00179 00180 }; 00181 00182 #endif
Generated on Wed Jul 13 2022 16:17:46 by
1.7.2