Xbee API library with support SPI connection. (use only SPI supported model. see also http://www.digi.com/support/kbase/kbaseresultdetl?id=3362 )

Dependents:   XBeeWiFi_SPI_example

Fork of XBee by Suga koubou

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 #ifndef USE_XBEE_SPI
00109     XBeeWiFi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts);
00110 #else
00111     XBeeWiFi (PinName p_mosi, PinName p_miso, PinName p_sclk, PinName p_ssel, PinName p_attn, PinName p_reset);
00112 #endif
00113 
00114     int setup (int security, const char *ssid, const char *pin);
00115     int setup (const char *ssid);
00116     int reset ();
00117     int baud (int b);
00118     int setAddress ();
00119     int setAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver);
00120     int getAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver);
00121     int setTimeout (int timeout);
00122     int getStatus ();
00123     int getWiResponse (int apiId, int frameid = 0, int timeout = 3000);
00124 #ifdef USE_WIFIDNS
00125     int setNameserver (IpAddr &nameserver, int port = DNS_PORT);
00126     int getHostByName (const char* name, IpAddr &addr);
00127 #endif
00128     /**
00129      * Call with instance of AtCommandResponse only if getApiId() == AT_COMMAND_RESPONSE
00130      */
00131 //    void getIPV4RxFrame (XBeeResponse &responses);
00132 
00133 protected:
00134     int getWiAddr (IpAddr &ipaddr);
00135 #ifdef USE_WIFIDNS
00136     int createDnsRequest (const char* name, char *buf);
00137     int getDnsResponse (const uint8_t *buf, int len, IpAddr &addr);
00138 #endif
00139 
00140 private:
00141     IpAddr _nameserver;
00142     int _nameport;
00143 };
00144 
00145 /**
00146  * Represents a Wi-Fi TX packet that corresponds to Api Id: IPv4_TRANSMIT_REQUEST
00147  */
00148 class IPv4TransmitRequest : public PayloadRequest {
00149 public:
00150     /**
00151      * Creates a unicast IPv4TransmitRequest with the DEFAULT_FRAME_ID
00152      */
00153     IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint16_t srcPort, uint8_t protocol, uint8_t option, uint8_t *data, uint16_t dataLength, uint8_t frameId);
00154     IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint8_t *data, uint16_t dataLength);
00155     /**
00156      * Creates a default instance of this class.  At a minimum you must specify
00157      * a payload, payload length and a destination address before sending this request.
00158      */
00159     IPv4TransmitRequest();
00160     IpAddr& getAddress();
00161     uint16_t getDstPort();
00162     uint16_t getSrcPort();
00163     uint8_t getProtocol();
00164     uint8_t getOption();
00165     void setAddress(IpAddr& dstAddr);
00166     void setDstPort(uint16_t dstPort);
00167     void setSrcPort(uint16_t srcPort);
00168     void setProtocol(uint8_t protocol);
00169     void setOption(uint8_t option);
00170 protected:
00171     // declare virtual functions
00172     virtual uint8_t getFrameData(uint16_t pos);
00173     virtual uint16_t getFrameDataLength();
00174 private:
00175     IpAddr _dstAddr;
00176     uint16_t _dstPort;
00177     uint16_t _srcPort;
00178     uint8_t _protocol;
00179     uint8_t _option;
00180 };
00181 
00182 /**
00183  * Represents a Wi-Fi TX status packet
00184  */
00185 class Transmit_Status : public FrameIdResponse {
00186 public:
00187     Transmit_Status();
00188     uint8_t getStatus();
00189     bool isSuccess();
00190 };
00191 
00192 /**
00193  * Represents a Wi-Fi RX packet
00194  */
00195 class IPV4RxFrame : public RxDataResponse {
00196 public:
00197     IPV4RxFrame();
00198     IpAddr& getSrcAddress();
00199     uint16_t getDstPort();
00200     uint16_t getSrcPort();
00201     uint8_t getProtocol();
00202     uint8_t getStatus();
00203     virtual uint16_t getDataLength();
00204     // frame position where data starts
00205     virtual uint8_t getDataOffset();
00206 private:
00207     IpAddr _srcAddr;
00208 };
00209 
00210 #endif //XBeeWiFi_h
00211 #endif