
Dependencies: XBeeLib mbed HvZAlphaNumLib HvZServerLib
lib/udp.hpp@0:9cdba0589ba2, 2010-12-12 (annotated)
- Committer:
- etherealflaim
- Date:
- Sun Dec 12 19:35:00 2010 +0000
- Revision:
- 0:9cdba0589ba2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
etherealflaim | 0:9cdba0589ba2 | 1 | #ifndef _UDP |
etherealflaim | 0:9cdba0589ba2 | 2 | #define _UDP |
etherealflaim | 0:9cdba0589ba2 | 3 | |
etherealflaim | 0:9cdba0589ba2 | 4 | #include <string> |
etherealflaim | 0:9cdba0589ba2 | 5 | #include <vector> |
etherealflaim | 0:9cdba0589ba2 | 6 | #include <queue> |
etherealflaim | 0:9cdba0589ba2 | 7 | #include <cctype> |
etherealflaim | 0:9cdba0589ba2 | 8 | #include <iostream> |
etherealflaim | 0:9cdba0589ba2 | 9 | |
etherealflaim | 0:9cdba0589ba2 | 10 | #include "mbed.h" |
etherealflaim | 0:9cdba0589ba2 | 11 | #include "if/eth/EthernetNetIf.h" |
etherealflaim | 0:9cdba0589ba2 | 12 | #include "iHvZ.hpp" |
etherealflaim | 0:9cdba0589ba2 | 13 | |
etherealflaim | 0:9cdba0589ba2 | 14 | #include "util/types.h" |
etherealflaim | 0:9cdba0589ba2 | 15 | #include "net/net.h" |
etherealflaim | 0:9cdba0589ba2 | 16 | |
etherealflaim | 0:9cdba0589ba2 | 17 | #include "core/net.h" |
etherealflaim | 0:9cdba0589ba2 | 18 | #include "core/host.h" |
etherealflaim | 0:9cdba0589ba2 | 19 | |
etherealflaim | 0:9cdba0589ba2 | 20 | #define HVZ_PORT 4489 |
etherealflaim | 0:9cdba0589ba2 | 21 | #define HVZ_HOSTNAME "kylelemons.net" |
etherealflaim | 0:9cdba0589ba2 | 22 | |
etherealflaim | 0:9cdba0589ba2 | 23 | enum UDPErr |
etherealflaim | 0:9cdba0589ba2 | 24 | { |
etherealflaim | 0:9cdba0589ba2 | 25 | __UDPSOCKET_MIN = -0xFFFF, |
etherealflaim | 0:9cdba0589ba2 | 26 | UDPSOCKET_SETUP, ///<UDPSocket not properly configured |
etherealflaim | 0:9cdba0589ba2 | 27 | UDPSOCKET_IF, ///<Interface has problems, does not exist or is not initialized |
etherealflaim | 0:9cdba0589ba2 | 28 | UDPSOCKET_MEM, ///<Not enough mem |
etherealflaim | 0:9cdba0589ba2 | 29 | UDPSOCKET_INUSE, ///<Interface / Port is in use |
etherealflaim | 0:9cdba0589ba2 | 30 | //... |
etherealflaim | 0:9cdba0589ba2 | 31 | UDPSOCKET_OK = 0 ///<Success |
etherealflaim | 0:9cdba0589ba2 | 32 | }; |
etherealflaim | 0:9cdba0589ba2 | 33 | |
etherealflaim | 0:9cdba0589ba2 | 34 | ///UDP Socket Event(s) |
etherealflaim | 0:9cdba0589ba2 | 35 | enum UDPSocketEvent //Only one event here for now, but keeps that model in case we need to implement some others |
etherealflaim | 0:9cdba0589ba2 | 36 | { |
etherealflaim | 0:9cdba0589ba2 | 37 | UDPSOCKET_READABLE, ///<Data in buf |
etherealflaim | 0:9cdba0589ba2 | 38 | }; |
etherealflaim | 0:9cdba0589ba2 | 39 | |
etherealflaim | 0:9cdba0589ba2 | 40 | enum responseType |
etherealflaim | 0:9cdba0589ba2 | 41 | { |
etherealflaim | 0:9cdba0589ba2 | 42 | HVZ_REG = 0, |
etherealflaim | 0:9cdba0589ba2 | 43 | HVZ_TAG, |
etherealflaim | 0:9cdba0589ba2 | 44 | HVZ_PULL |
etherealflaim | 0:9cdba0589ba2 | 45 | }; |
etherealflaim | 0:9cdba0589ba2 | 46 | |
etherealflaim | 0:9cdba0589ba2 | 47 | enum pullType |
etherealflaim | 0:9cdba0589ba2 | 48 | { |
etherealflaim | 0:9cdba0589ba2 | 49 | PULL_DEV = 0, |
etherealflaim | 0:9cdba0589ba2 | 50 | PULL_MAC |
etherealflaim | 0:9cdba0589ba2 | 51 | }; |
etherealflaim | 0:9cdba0589ba2 | 52 | |
etherealflaim | 0:9cdba0589ba2 | 53 | class NetUdpSocket; |
etherealflaim | 0:9cdba0589ba2 | 54 | enum NetUdpSocketEvent; |
etherealflaim | 0:9cdba0589ba2 | 55 | |
etherealflaim | 0:9cdba0589ba2 | 56 | class UDP { |
etherealflaim | 0:9cdba0589ba2 | 57 | public: |
etherealflaim | 0:9cdba0589ba2 | 58 | |
etherealflaim | 0:9cdba0589ba2 | 59 | iHvZ *m_game; //< Maintain a connection to the active game |
etherealflaim | 0:9cdba0589ba2 | 60 | |
etherealflaim | 0:9cdba0589ba2 | 61 | Ethernet_MAC mac; |
etherealflaim | 0:9cdba0589ba2 | 62 | Ethernet eth; |
etherealflaim | 0:9cdba0589ba2 | 63 | EthernetNetIf conn; |
etherealflaim | 0:9cdba0589ba2 | 64 | char srvr_resp[128]; |
etherealflaim | 0:9cdba0589ba2 | 65 | int registered; |
etherealflaim | 0:9cdba0589ba2 | 66 | |
etherealflaim | 0:9cdba0589ba2 | 67 | DigitalIn m_eth; |
etherealflaim | 0:9cdba0589ba2 | 68 | InterruptIn m_eth_int; //< Get an interrupt whenever the ethernet button is hit |
etherealflaim | 0:9cdba0589ba2 | 69 | Timeout m_eth_chk; |
etherealflaim | 0:9cdba0589ba2 | 70 | |
etherealflaim | 0:9cdba0589ba2 | 71 | public: |
etherealflaim | 0:9cdba0589ba2 | 72 | |
etherealflaim | 0:9cdba0589ba2 | 73 | /* Creates a new socket */ |
etherealflaim | 0:9cdba0589ba2 | 74 | UDP(iHvZ *game, PinName eth_in); |
etherealflaim | 0:9cdba0589ba2 | 75 | |
etherealflaim | 0:9cdba0589ba2 | 76 | /* Closes and destroys socket */ |
etherealflaim | 0:9cdba0589ba2 | 77 | ~UDP(); //close() |
etherealflaim | 0:9cdba0589ba2 | 78 | |
etherealflaim | 0:9cdba0589ba2 | 79 | /* Check to see if theres an ethernet connection */ |
etherealflaim | 0:9cdba0589ba2 | 80 | void check(); |
etherealflaim | 0:9cdba0589ba2 | 81 | |
etherealflaim | 0:9cdba0589ba2 | 82 | /* Sends data to Server */ |
etherealflaim | 0:9cdba0589ba2 | 83 | int sendto(const char* buf, int len, Host* pHost); |
etherealflaim | 0:9cdba0589ba2 | 84 | |
etherealflaim | 0:9cdba0589ba2 | 85 | /* Receive from Server */ |
etherealflaim | 0:9cdba0589ba2 | 86 | int recvfrom(char* buf, int len, Host* pHost); |
etherealflaim | 0:9cdba0589ba2 | 87 | |
etherealflaim | 0:9cdba0589ba2 | 88 | /* Closes the socket */ |
etherealflaim | 0:9cdba0589ba2 | 89 | UDPErr close(); |
etherealflaim | 0:9cdba0589ba2 | 90 | |
etherealflaim | 0:9cdba0589ba2 | 91 | /* Checks to see if theres an ethernet link */ |
etherealflaim | 0:9cdba0589ba2 | 92 | void ethlink(); |
etherealflaim | 0:9cdba0589ba2 | 93 | |
etherealflaim | 0:9cdba0589ba2 | 94 | /* Read response from Server */ |
etherealflaim | 0:9cdba0589ba2 | 95 | void read(UDPSocketEvent e); |
etherealflaim | 0:9cdba0589ba2 | 96 | |
etherealflaim | 0:9cdba0589ba2 | 97 | /* Set the game device and tag IDs */ |
etherealflaim | 0:9cdba0589ba2 | 98 | void set_ids(char *response); |
etherealflaim | 0:9cdba0589ba2 | 99 | |
etherealflaim | 0:9cdba0589ba2 | 100 | /* Registers the new device */ |
etherealflaim | 0:9cdba0589ba2 | 101 | inline void register_device(Ethernet_MAC originMAC, Host *host) |
etherealflaim | 0:9cdba0589ba2 | 102 | { |
etherealflaim | 0:9cdba0589ba2 | 103 | char data[34]; |
etherealflaim | 0:9cdba0589ba2 | 104 | |
etherealflaim | 0:9cdba0589ba2 | 105 | sprintf(data, "iHvZ\001reg\001%02X%02X%02X%02X%02X%02X\001end", originMAC.octet[0], originMAC.octet[1], |
etherealflaim | 0:9cdba0589ba2 | 106 | originMAC.octet[2], originMAC.octet[3], originMAC.octet[4], originMAC.octet[5]); |
etherealflaim | 0:9cdba0589ba2 | 107 | |
etherealflaim | 0:9cdba0589ba2 | 108 | sendto(data, strlen(data), host); |
etherealflaim | 0:9cdba0589ba2 | 109 | } |
etherealflaim | 0:9cdba0589ba2 | 110 | |
etherealflaim | 0:9cdba0589ba2 | 111 | /* Sends a player tag to the server of the tagged device */ |
etherealflaim | 0:9cdba0589ba2 | 112 | inline void send_tag(Host *host, vector<string> taggedIDs); |
etherealflaim | 0:9cdba0589ba2 | 113 | |
etherealflaim | 0:9cdba0589ba2 | 114 | //Requests an update of all settings from Server |
etherealflaim | 0:9cdba0589ba2 | 115 | inline void send_pull(Host *host); |
etherealflaim | 0:9cdba0589ba2 | 116 | |
etherealflaim | 0:9cdba0589ba2 | 117 | inline int get_ack_err(char *response, responseType type) |
etherealflaim | 0:9cdba0589ba2 | 118 | { |
etherealflaim | 0:9cdba0589ba2 | 119 | char ack_err[3]; |
etherealflaim | 0:9cdba0589ba2 | 120 | int i; |
etherealflaim | 0:9cdba0589ba2 | 121 | |
etherealflaim | 0:9cdba0589ba2 | 122 | memset(ack_err,0,3); |
etherealflaim | 0:9cdba0589ba2 | 123 | |
etherealflaim | 0:9cdba0589ba2 | 124 | for( i = 5; i < 8; i++ ) |
etherealflaim | 0:9cdba0589ba2 | 125 | memcpy(&(ack_err[i-5]),&(response[i]),1); |
etherealflaim | 0:9cdba0589ba2 | 126 | |
etherealflaim | 0:9cdba0589ba2 | 127 | if( strcmp(ack_err,"ack") == 0 ) { |
etherealflaim | 0:9cdba0589ba2 | 128 | |
etherealflaim | 0:9cdba0589ba2 | 129 | switch( type ) { |
etherealflaim | 0:9cdba0589ba2 | 130 | case HVZ_REG: |
etherealflaim | 0:9cdba0589ba2 | 131 | return 1; |
etherealflaim | 0:9cdba0589ba2 | 132 | |
etherealflaim | 0:9cdba0589ba2 | 133 | case HVZ_PULL: |
etherealflaim | 0:9cdba0589ba2 | 134 | return 1; |
etherealflaim | 0:9cdba0589ba2 | 135 | |
etherealflaim | 0:9cdba0589ba2 | 136 | case HVZ_TAG: |
etherealflaim | 0:9cdba0589ba2 | 137 | // If sent invalid taggedIDs to server, the response will be ack |
etherealflaim | 0:9cdba0589ba2 | 138 | // but it will not have valid tagged IDs, this is the size |
etherealflaim | 0:9cdba0589ba2 | 139 | // of such a response |
etherealflaim | 0:9cdba0589ba2 | 140 | if( strlen(response) > 16 ) |
etherealflaim | 0:9cdba0589ba2 | 141 | return 1; |
etherealflaim | 0:9cdba0589ba2 | 142 | |
etherealflaim | 0:9cdba0589ba2 | 143 | else |
etherealflaim | 0:9cdba0589ba2 | 144 | return 0; |
etherealflaim | 0:9cdba0589ba2 | 145 | |
etherealflaim | 0:9cdba0589ba2 | 146 | } |
etherealflaim | 0:9cdba0589ba2 | 147 | } |
etherealflaim | 0:9cdba0589ba2 | 148 | |
etherealflaim | 0:9cdba0589ba2 | 149 | return 0; |
etherealflaim | 0:9cdba0589ba2 | 150 | } |
etherealflaim | 0:9cdba0589ba2 | 151 | |
etherealflaim | 0:9cdba0589ba2 | 152 | /* Update the Settings for the game */ |
etherealflaim | 0:9cdba0589ba2 | 153 | void update_settings(char *response); |
etherealflaim | 0:9cdba0589ba2 | 154 | |
etherealflaim | 0:9cdba0589ba2 | 155 | class CDummy; |
etherealflaim | 0:9cdba0589ba2 | 156 | |
etherealflaim | 0:9cdba0589ba2 | 157 | ///Setups callback |
etherealflaim | 0:9cdba0589ba2 | 158 | template<class T> |
etherealflaim | 0:9cdba0589ba2 | 159 | void setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) ) |
etherealflaim | 0:9cdba0589ba2 | 160 | { |
etherealflaim | 0:9cdba0589ba2 | 161 | m_pCbItem = (CDummy*) pItem; |
etherealflaim | 0:9cdba0589ba2 | 162 | m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod; |
etherealflaim | 0:9cdba0589ba2 | 163 | } |
etherealflaim | 0:9cdba0589ba2 | 164 | |
etherealflaim | 0:9cdba0589ba2 | 165 | ///Disables callback |
etherealflaim | 0:9cdba0589ba2 | 166 | void resetOnEvent(); |
etherealflaim | 0:9cdba0589ba2 | 167 | |
etherealflaim | 0:9cdba0589ba2 | 168 | protected: |
etherealflaim | 0:9cdba0589ba2 | 169 | void onNetUdpSocketEvent(NetUdpSocketEvent e); |
etherealflaim | 0:9cdba0589ba2 | 170 | UDPErr checkInst(); |
etherealflaim | 0:9cdba0589ba2 | 171 | |
etherealflaim | 0:9cdba0589ba2 | 172 | private: |
etherealflaim | 0:9cdba0589ba2 | 173 | NetUdpSocket* m_pNetUdpSocket; |
etherealflaim | 0:9cdba0589ba2 | 174 | |
etherealflaim | 0:9cdba0589ba2 | 175 | CDummy* m_pCbItem; |
etherealflaim | 0:9cdba0589ba2 | 176 | void (CDummy::*m_pCbMeth)(UDPSocketEvent); |
etherealflaim | 0:9cdba0589ba2 | 177 | |
etherealflaim | 0:9cdba0589ba2 | 178 | void (*m_pCb)(UDPSocketEvent); |
etherealflaim | 0:9cdba0589ba2 | 179 | |
etherealflaim | 0:9cdba0589ba2 | 180 | }; |
etherealflaim | 0:9cdba0589ba2 | 181 | |
etherealflaim | 0:9cdba0589ba2 | 182 | #endif |