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

Fork of XBee by Suga koubou

Committer:
johnb
Date:
Wed Jan 08 23:43:40 2014 +0000
Revision:
9:9d8397c69167
Parent:
7:b36422ef864f
Merge David Dollar's changes - http://mbed.org/users/ddollar/code/XBee/rev/73d051b01344

Who changed what in which revision?

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