XBee-mbed library http://mbed.org/users/okini3939/notebook/xbee-mbed/

Dependents:   device_server_udp led_sender_post XBee_API_ex1 XBee_API_ex2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers XBeeWiFi.h Source File

XBeeWiFi.h

Go to the documentation of this file.
00001 /**
00002  * XBee Wi-Fi library for mbed
00003  * Copyright (c) 2011 Hiroshi Suga
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  */
00006 
00007 /** @file
00008  * @brief XBee Wi-Fi library for mbed
00009  */
00010 
00011 #include "XBee_conf.h"
00012 #ifdef ENABLE_XBEE_WIFI
00013 
00014 #ifndef XBeeWiFi_h
00015 #define XBeeWiFi_h
00016 
00017 #include "mbed.h"
00018 #include "EthernetNetIf.h"
00019 #include "XBee.h"
00020 #include <inttypes.h>
00021 
00022 #undef USE_WIFIDNS
00023 
00024 // 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)
00025 #define IPv4_TRANSMIT_REQUEST_API_LENGTH 10
00026 
00027 /**
00028  * Api Id constants
00029  */
00030 #define IPv4_TRANSMIT_REQUEST 0x20
00031 #define IPv4_RX_FRAME 0xb0
00032 #define IPv4_TRANSMIT_STATUS 0x89
00033 
00034 /// status
00035 #define MAC_FAILUE 0x01
00036 #define PHYSICAL_ERROR 0x04
00037 #define NETWORK_ACK_FAILUE 0x21
00038 #define NOT_ASSOCIATED 0x22
00039 #define NO_RESOURCES 0x32
00040 #define CONNECTIONS_FAILUE 0x76
00041 
00042 /// protocol
00043 #define PROTOCOL_UDP 0
00044 #define PROTOCOL_TCP 1
00045 
00046 /// option
00047 #define OPTION_LEAVEOPEN 1
00048 
00049 /// security
00050 #define SECURITY_OPEN 0
00051 #define SECURITY_WPA 1
00052 #define SECURITY_WPA2 2
00053 #define SECURITY_WEP40 3
00054 #define SECURITY_WEP104 4
00055 
00056 /// modem status
00057 #define JOINED_AP 0
00058 #define INITIALIZATION 0x01
00059 #define SSID_NOT_FOUND 0x22
00060 #define SSID_NOT_CONFIGURED 0x23
00061 #define JOIN_FAILED 0x27
00062 #define WAITING_IPADDRESS 0x41
00063 #define WAITING_SOCKETS 0x42
00064 #define SCANNING_SSID 0xff
00065 
00066 /// dns
00067 #define DNS_QUERY_A 1
00068 #define DNS_QUERY_NS 2
00069 #define DNS_QUERY_CNAME 5
00070 #define DNS_QUERY_PTR 12
00071 #define DNS_QUERY_MX 15
00072 #define DNS_QUERY_AAAA 28
00073 #define DNS_QUERY_ANY 255
00074 #define DNS_CLASS_IN 1
00075 
00076 #define DNS_PORT 53
00077 #define DNS_SRC_PORT 10053
00078 #define DNS_TIMEOUT 15000 // ms
00079 
00080 struct DNSHeader {
00081         uint16_t id; 
00082         uint16_t flags; 
00083         uint16_t questions; 
00084         uint16_t answers; 
00085         uint16_t authorities; 
00086         uint16_t additional; 
00087 };
00088 
00089 struct DnsQuestionEnd { 
00090         uint16_t type; 
00091         uint16_t clas; 
00092 };
00093 
00094 struct DnsAnswer {
00095         uint16_t name; 
00096         uint16_t type; 
00097         uint16_t clas; 
00098         uint32_t ttl; 
00099         uint16_t length; 
00100 } __attribute__((packed)); 
00101 
00102 
00103 /**
00104  * Primary interface for communicating with an XBee Wi-Fi.
00105  */
00106 class XBeeWiFi : public XBee {
00107 public:
00108     XBeeWiFi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts);
00109 
00110     int setup (int security, const char *ssid, const char *pin);
00111     int setup (const char *ssid);
00112     int reset ();
00113     int baud (int b);
00114     int setAddress ();
00115     int setAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver);
00116     int getAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver);
00117     int setTimeout (int timeout);
00118     int getStatus ();
00119     int getWiResponse (int apiId, int frameid = 0, int timeout = 3000);
00120 #ifdef USE_WIFIDNS
00121     int setNameserver (IpAddr &nameserver, int port);
00122     int getHostByName (const char* name, IpAddr &addr);
00123 #endif
00124     /**
00125      * Call with instance of AtCommandResponse only if getApiId() == AT_COMMAND_RESPONSE
00126      */
00127 //    void getIPV4RxFrame (XBeeResponse &responses);
00128 
00129 protected:
00130     int getWiAddr (IpAddr &ipaddr);
00131 #ifdef USE_WIFIDNS
00132     int createDnsRequest (const char* name, char *buf);
00133     int getDnsResponse (const uint8_t *buf, int len, IpAddr &addr);
00134 #endif
00135 
00136 private:
00137     IpAddr _nameserver;
00138     int _nameport;
00139 };
00140 
00141 /**
00142  * Represents a Wi-Fi TX packet that corresponds to Api Id: IPv4_TRANSMIT_REQUEST
00143  */
00144 class IPv4TransmitRequest : public PayloadRequest {
00145 public:
00146     /**
00147      * Creates a unicast IPv4TransmitRequest with the DEFAULT_FRAME_ID
00148      */
00149     IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint16_t srcPort, uint8_t protocol, uint8_t option, uint8_t *data, uint16_t dataLength, uint8_t frameId);
00150     IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint8_t *data, uint16_t dataLength);
00151     /**
00152      * Creates a default instance of this class.  At a minimum you must specify
00153      * a payload, payload length and a destination address before sending this request.
00154      */
00155     IPv4TransmitRequest();
00156     IpAddr& getAddress();
00157     uint16_t getDstPort();
00158     uint16_t getSrcPort();
00159     uint8_t getProtocol();
00160     uint8_t getOption();
00161     void setAddress(IpAddr& dstAddr);
00162     void setDstPort(uint16_t dstPort);
00163     void setSrcPort(uint16_t srcPort);
00164     void setProtocol(uint8_t protocol);
00165     void setOption(uint8_t option);
00166 protected:
00167     // declare virtual functions
00168     virtual uint8_t getFrameData(uint16_t pos);
00169     virtual uint16_t getFrameDataLength();
00170 private:
00171     IpAddr _dstAddr;
00172     uint16_t _dstPort;
00173     uint16_t _srcPort;
00174     uint8_t _protocol;
00175     uint8_t _option;
00176 };
00177 
00178 /**
00179  * Represents a Wi-Fi TX status packet
00180  */
00181 class Transmit_Status : public FrameIdResponse {
00182 public:
00183     Transmit_Status();
00184     uint8_t getStatus();
00185     bool isSuccess();
00186 };
00187 
00188 /**
00189  * Represents a Wi-Fi RX packet
00190  */
00191 class IPV4RxFrame : public RxDataResponse {
00192 public:
00193     IPV4RxFrame();
00194     IpAddr& getSrcAddress();
00195     uint16_t getDstPort();
00196     uint16_t getSrcPort();
00197     uint8_t getProtocol();
00198     uint8_t getStatus();
00199     virtual uint16_t getDataLength();
00200     // frame position where data starts
00201     virtual uint8_t getDataOffset();
00202 private:
00203     IpAddr _srcAddr;
00204 };
00205 
00206 #endif //XBeeWiFi_h
00207 #endif