XBee-mbed library Forked from http://mbed.org/users/okini3939/notebook/xbee-mbed/
Fork of XBee by
XBeeWiFi.h@4:f6d73acc1f75, 2012-03-13 (annotated)
- Committer:
- okini3939
- Date:
- Tue Mar 13 09:01:52 2012 +0000
- Revision:
- 4:f6d73acc1f75
- Parent:
- 3:8573b122fa84
- Child:
- 5:547cfff7adf7
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 2:6efb3541af61 | 1 | /** |
okini3939 | 2:6efb3541af61 | 2 | * XBee Wi-Fi library for mbed |
okini3939 | 2:6efb3541af61 | 3 | * Copyright (c) 2011 Hiroshi Suga |
okini3939 | 2:6efb3541af61 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
okini3939 | 2:6efb3541af61 | 5 | */ |
okini3939 | 2:6efb3541af61 | 6 | |
okini3939 | 2:6efb3541af61 | 7 | /** @file |
okini3939 | 3:8573b122fa84 | 8 | * @brief XBee Wi-Fi library for mbed |
okini3939 | 2:6efb3541af61 | 9 | */ |
okini3939 | 2:6efb3541af61 | 10 | |
okini3939 | 2:6efb3541af61 | 11 | #ifndef XBeeWiFi_h |
okini3939 | 2:6efb3541af61 | 12 | #define XBeeWiFi_h |
okini3939 | 2:6efb3541af61 | 13 | |
okini3939 | 2:6efb3541af61 | 14 | #include "mbed.h" |
okini3939 | 2:6efb3541af61 | 15 | #include "EthernetNetIf.h" |
okini3939 | 2:6efb3541af61 | 16 | #include "XBee.h" |
okini3939 | 2:6efb3541af61 | 17 | #include <inttypes.h> |
okini3939 | 2:6efb3541af61 | 18 | |
okini3939 | 3:8573b122fa84 | 19 | #define USE_WIFICLASS |
okini3939 | 3:8573b122fa84 | 20 | #define USE_WIFIDNS |
okini3939 | 2:6efb3541af61 | 21 | |
okini3939 | 2:6efb3541af61 | 22 | // the non-variable length of the frame data (not including frame id or api id or variable data size (e.g. payload, at command set value) |
okini3939 | 2:6efb3541af61 | 23 | #define IPv4_TRANSMIT_REQUEST_API_LENGTH 10 |
okini3939 | 2:6efb3541af61 | 24 | |
okini3939 | 2:6efb3541af61 | 25 | /** |
okini3939 | 2:6efb3541af61 | 26 | * Api Id constants |
okini3939 | 2:6efb3541af61 | 27 | */ |
okini3939 | 2:6efb3541af61 | 28 | #define IPv4_TRANSMIT_REQUEST 0x20 |
okini3939 | 2:6efb3541af61 | 29 | #define IPv4_RX_FRAME 0xb0 |
okini3939 | 2:6efb3541af61 | 30 | #define IPv4_TRANSMIT_STATUS 0x89 |
okini3939 | 2:6efb3541af61 | 31 | |
okini3939 | 2:6efb3541af61 | 32 | /// status |
okini3939 | 2:6efb3541af61 | 33 | #define MAC_FAILUE 0x01 |
okini3939 | 2:6efb3541af61 | 34 | #define PHYSICAL_ERROR 0x04 |
okini3939 | 2:6efb3541af61 | 35 | #define NETWORK_ACK_FAILUE 0x21 |
okini3939 | 2:6efb3541af61 | 36 | #define NOT_ASSOCIATED 0x22 |
okini3939 | 2:6efb3541af61 | 37 | #define NO_RESOURCES 0x32 |
okini3939 | 2:6efb3541af61 | 38 | #define CONNECTIONS_FAILUE 0x76 |
okini3939 | 2:6efb3541af61 | 39 | |
okini3939 | 2:6efb3541af61 | 40 | /// protocol |
okini3939 | 2:6efb3541af61 | 41 | #define PROTOCOL_UDP 0 |
okini3939 | 2:6efb3541af61 | 42 | #define PROTOCOL_TCP 1 |
okini3939 | 2:6efb3541af61 | 43 | |
okini3939 | 2:6efb3541af61 | 44 | /// option |
okini3939 | 2:6efb3541af61 | 45 | #define OPTION_LEAVEOPEN 1 |
okini3939 | 2:6efb3541af61 | 46 | |
okini3939 | 2:6efb3541af61 | 47 | /// security |
okini3939 | 2:6efb3541af61 | 48 | #define SECURITY_OPEN 0 |
okini3939 | 2:6efb3541af61 | 49 | #define SECURITY_WPA 1 |
okini3939 | 2:6efb3541af61 | 50 | #define SECURITY_WPA2 2 |
okini3939 | 2:6efb3541af61 | 51 | #define SECURITY_WEP40 3 |
okini3939 | 2:6efb3541af61 | 52 | #define SECURITY_WEP104 4 |
okini3939 | 2:6efb3541af61 | 53 | |
okini3939 | 2:6efb3541af61 | 54 | /// modem status |
okini3939 | 2:6efb3541af61 | 55 | #define JOINED_AP 0 |
okini3939 | 3:8573b122fa84 | 56 | #define INITIALIZATION 0x01 |
okini3939 | 3:8573b122fa84 | 57 | #define SSID_NOT_FOUND 0x22 |
okini3939 | 2:6efb3541af61 | 58 | #define SSID_NOT_CONFIGURED 0x23 |
okini3939 | 3:8573b122fa84 | 59 | #define JOIN_FAILED 0x27 |
okini3939 | 3:8573b122fa84 | 60 | #define WAITING_IPADDRESS 0x41 |
okini3939 | 3:8573b122fa84 | 61 | #define WAITING_SOCKETS 0x42 |
okini3939 | 3:8573b122fa84 | 62 | #define SCANNING_SSID 0xff |
okini3939 | 2:6efb3541af61 | 63 | |
okini3939 | 2:6efb3541af61 | 64 | /// dns |
okini3939 | 2:6efb3541af61 | 65 | #define DNS_QUERY_A 1 |
okini3939 | 2:6efb3541af61 | 66 | #define DNS_QUERY_NS 2 |
okini3939 | 2:6efb3541af61 | 67 | #define DNS_QUERY_CNAME 5 |
okini3939 | 2:6efb3541af61 | 68 | #define DNS_QUERY_PTR 12 |
okini3939 | 2:6efb3541af61 | 69 | #define DNS_QUERY_MX 15 |
okini3939 | 2:6efb3541af61 | 70 | #define DNS_QUERY_AAAA 28 |
okini3939 | 2:6efb3541af61 | 71 | #define DNS_QUERY_ANY 255 |
okini3939 | 2:6efb3541af61 | 72 | #define DNS_CLASS_IN 1 |
okini3939 | 2:6efb3541af61 | 73 | |
okini3939 | 3:8573b122fa84 | 74 | #define DNS_PORT 53 |
okini3939 | 4:f6d73acc1f75 | 75 | #define DNS_SRC_PORT 10053 |
okini3939 | 4:f6d73acc1f75 | 76 | #define DNS_TIMEOUT 15000 // ms |
okini3939 | 3:8573b122fa84 | 77 | |
okini3939 | 2:6efb3541af61 | 78 | struct DNSHeader { |
okini3939 | 2:6efb3541af61 | 79 | uint16_t id; |
okini3939 | 2:6efb3541af61 | 80 | uint16_t flags; |
okini3939 | 2:6efb3541af61 | 81 | uint16_t questions; |
okini3939 | 2:6efb3541af61 | 82 | uint16_t answers; |
okini3939 | 2:6efb3541af61 | 83 | uint16_t authorities; |
okini3939 | 2:6efb3541af61 | 84 | uint16_t additional; |
okini3939 | 2:6efb3541af61 | 85 | }; |
okini3939 | 2:6efb3541af61 | 86 | |
okini3939 | 2:6efb3541af61 | 87 | struct DnsQuestionEnd { |
okini3939 | 2:6efb3541af61 | 88 | uint16_t type; |
okini3939 | 2:6efb3541af61 | 89 | uint16_t clas; |
okini3939 | 2:6efb3541af61 | 90 | }; |
okini3939 | 2:6efb3541af61 | 91 | |
okini3939 | 2:6efb3541af61 | 92 | struct DnsAnswer { |
okini3939 | 2:6efb3541af61 | 93 | uint16_t name; |
okini3939 | 2:6efb3541af61 | 94 | uint16_t type; |
okini3939 | 2:6efb3541af61 | 95 | uint16_t clas; |
okini3939 | 2:6efb3541af61 | 96 | uint32_t ttl; |
okini3939 | 2:6efb3541af61 | 97 | uint16_t length; |
okini3939 | 2:6efb3541af61 | 98 | } __attribute__((packed)); |
okini3939 | 2:6efb3541af61 | 99 | |
okini3939 | 2:6efb3541af61 | 100 | |
okini3939 | 2:6efb3541af61 | 101 | #ifdef USE_WIFICLASS |
okini3939 | 2:6efb3541af61 | 102 | /** |
okini3939 | 2:6efb3541af61 | 103 | * Primary interface for communicating with an XBee Wi-Fi. |
okini3939 | 2:6efb3541af61 | 104 | */ |
okini3939 | 2:6efb3541af61 | 105 | class XBeeWiFi : public XBee { |
okini3939 | 2:6efb3541af61 | 106 | public: |
okini3939 | 4:f6d73acc1f75 | 107 | XBeeWiFi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts); |
okini3939 | 2:6efb3541af61 | 108 | |
okini3939 | 2:6efb3541af61 | 109 | int setup (int security, const char *ssid, const char *pin); |
okini3939 | 2:6efb3541af61 | 110 | int setup (const char *ssid); |
okini3939 | 2:6efb3541af61 | 111 | int reset (); |
okini3939 | 4:f6d73acc1f75 | 112 | int baud (int b); |
okini3939 | 2:6efb3541af61 | 113 | int setAddress (); |
okini3939 | 2:6efb3541af61 | 114 | int setAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver); |
okini3939 | 3:8573b122fa84 | 115 | int getAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver); |
okini3939 | 2:6efb3541af61 | 116 | int setTimeout (int timeout); |
okini3939 | 2:6efb3541af61 | 117 | int getStatus (); |
okini3939 | 4:f6d73acc1f75 | 118 | int getWiResponse (int apiId, int frameid = 0, int timeout = 3000); |
okini3939 | 2:6efb3541af61 | 119 | #ifdef USE_WIFIDNS |
okini3939 | 3:8573b122fa84 | 120 | int setNameserver (IpAddr &nameserver); |
okini3939 | 2:6efb3541af61 | 121 | int getHostByName (const char* name, IpAddr &addr); |
okini3939 | 2:6efb3541af61 | 122 | #endif |
okini3939 | 4:f6d73acc1f75 | 123 | /** |
okini3939 | 4:f6d73acc1f75 | 124 | * Call with instance of AtCommandResponse only if getApiId() == AT_COMMAND_RESPONSE |
okini3939 | 4:f6d73acc1f75 | 125 | */ |
okini3939 | 4:f6d73acc1f75 | 126 | // void getIPV4RxFrame (XBeeResponse &responses); |
okini3939 | 2:6efb3541af61 | 127 | |
okini3939 | 2:6efb3541af61 | 128 | protected: |
okini3939 | 3:8573b122fa84 | 129 | int getWiAddr (IpAddr &ipaddr); |
okini3939 | 2:6efb3541af61 | 130 | #ifdef USE_WIFIDNS |
okini3939 | 2:6efb3541af61 | 131 | int createDnsRequest (const char* name, char *buf); |
okini3939 | 2:6efb3541af61 | 132 | int getDnsResponse (const uint8_t *buf, int len, IpAddr &addr); |
okini3939 | 2:6efb3541af61 | 133 | #endif |
okini3939 | 2:6efb3541af61 | 134 | |
okini3939 | 2:6efb3541af61 | 135 | private: |
okini3939 | 2:6efb3541af61 | 136 | IpAddr _nameserver; |
okini3939 | 2:6efb3541af61 | 137 | }; |
okini3939 | 2:6efb3541af61 | 138 | #endif |
okini3939 | 2:6efb3541af61 | 139 | |
okini3939 | 2:6efb3541af61 | 140 | /** |
okini3939 | 2:6efb3541af61 | 141 | * Represents a Wi-Fi TX packet that corresponds to Api Id: IPv4_TRANSMIT_REQUEST |
okini3939 | 2:6efb3541af61 | 142 | */ |
okini3939 | 2:6efb3541af61 | 143 | class IPv4TransmitRequest : public PayloadRequest { |
okini3939 | 2:6efb3541af61 | 144 | public: |
okini3939 | 2:6efb3541af61 | 145 | /** |
okini3939 | 2:6efb3541af61 | 146 | * Creates a unicast IPv4TransmitRequest with the DEFAULT_FRAME_ID |
okini3939 | 2:6efb3541af61 | 147 | */ |
okini3939 | 4:f6d73acc1f75 | 148 | IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint16_t srcPort, uint8_t protocol, uint8_t option, uint8_t *data, uint16_t dataLength, uint8_t frameId); |
okini3939 | 4:f6d73acc1f75 | 149 | IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint8_t *data, uint16_t dataLength); |
okini3939 | 2:6efb3541af61 | 150 | /** |
okini3939 | 2:6efb3541af61 | 151 | * Creates a default instance of this class. At a minimum you must specify |
okini3939 | 2:6efb3541af61 | 152 | * a payload, payload length and a destination address before sending this request. |
okini3939 | 2:6efb3541af61 | 153 | */ |
okini3939 | 2:6efb3541af61 | 154 | IPv4TransmitRequest(); |
okini3939 | 2:6efb3541af61 | 155 | IpAddr& getAddress(); |
okini3939 | 2:6efb3541af61 | 156 | uint16_t getDstPort(); |
okini3939 | 2:6efb3541af61 | 157 | uint16_t getSrcPort(); |
okini3939 | 2:6efb3541af61 | 158 | uint8_t getProtocol(); |
okini3939 | 2:6efb3541af61 | 159 | uint8_t getOption(); |
okini3939 | 2:6efb3541af61 | 160 | void setAddress(IpAddr& dstAddr); |
okini3939 | 2:6efb3541af61 | 161 | void setDstPort(uint16_t dstPort); |
okini3939 | 2:6efb3541af61 | 162 | void setSrcPort(uint16_t srcPort); |
okini3939 | 2:6efb3541af61 | 163 | void setProtocol(uint8_t protocol); |
okini3939 | 2:6efb3541af61 | 164 | void setOption(uint8_t option); |
okini3939 | 2:6efb3541af61 | 165 | protected: |
okini3939 | 2:6efb3541af61 | 166 | // declare virtual functions |
okini3939 | 4:f6d73acc1f75 | 167 | virtual uint8_t getFrameData(uint16_t pos); |
okini3939 | 4:f6d73acc1f75 | 168 | virtual uint16_t getFrameDataLength(); |
okini3939 | 2:6efb3541af61 | 169 | private: |
okini3939 | 2:6efb3541af61 | 170 | IpAddr _dstAddr; |
okini3939 | 2:6efb3541af61 | 171 | uint16_t _dstPort; |
okini3939 | 2:6efb3541af61 | 172 | uint16_t _srcPort; |
okini3939 | 2:6efb3541af61 | 173 | uint8_t _protocol; |
okini3939 | 2:6efb3541af61 | 174 | uint8_t _option; |
okini3939 | 2:6efb3541af61 | 175 | }; |
okini3939 | 2:6efb3541af61 | 176 | |
okini3939 | 2:6efb3541af61 | 177 | /** |
okini3939 | 2:6efb3541af61 | 178 | * Represents a Wi-Fi TX status packet |
okini3939 | 2:6efb3541af61 | 179 | */ |
okini3939 | 2:6efb3541af61 | 180 | class Transmit_Status : public FrameIdResponse { |
okini3939 | 2:6efb3541af61 | 181 | public: |
okini3939 | 2:6efb3541af61 | 182 | Transmit_Status(); |
okini3939 | 2:6efb3541af61 | 183 | uint8_t getStatus(); |
okini3939 | 2:6efb3541af61 | 184 | bool isSuccess(); |
okini3939 | 2:6efb3541af61 | 185 | }; |
okini3939 | 2:6efb3541af61 | 186 | |
okini3939 | 2:6efb3541af61 | 187 | /** |
okini3939 | 2:6efb3541af61 | 188 | * Represents a Wi-Fi RX packet |
okini3939 | 2:6efb3541af61 | 189 | */ |
okini3939 | 2:6efb3541af61 | 190 | class IPV4RxFrame : public RxDataResponse { |
okini3939 | 2:6efb3541af61 | 191 | public: |
okini3939 | 2:6efb3541af61 | 192 | IPV4RxFrame(); |
okini3939 | 2:6efb3541af61 | 193 | IpAddr& getSrcAddress(); |
okini3939 | 2:6efb3541af61 | 194 | uint16_t getDstPort(); |
okini3939 | 2:6efb3541af61 | 195 | uint16_t getSrcPort(); |
okini3939 | 2:6efb3541af61 | 196 | uint8_t getProtocol(); |
okini3939 | 2:6efb3541af61 | 197 | uint8_t getStatus(); |
okini3939 | 4:f6d73acc1f75 | 198 | virtual uint16_t getDataLength(); |
okini3939 | 2:6efb3541af61 | 199 | // frame position where data starts |
okini3939 | 2:6efb3541af61 | 200 | virtual uint8_t getDataOffset(); |
okini3939 | 2:6efb3541af61 | 201 | private: |
okini3939 | 2:6efb3541af61 | 202 | IpAddr _srcAddr; |
okini3939 | 2:6efb3541af61 | 203 | }; |
okini3939 | 2:6efb3541af61 | 204 | |
okini3939 | 2:6efb3541af61 | 205 | #endif //XBeeWiFi_h |