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

Committer:
okini3939
Date:
Wed Dec 18 02:24:33 2013 +0000
Revision:
6:6f84e2840408
Parent:
5:547cfff7adf7
Child:
7:b36422ef864f
add XBee_conf.h

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 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 /**
okini3939 2:6efb3541af61 102 * Primary interface for communicating with an XBee Wi-Fi.
okini3939 2:6efb3541af61 103 */
okini3939 2:6efb3541af61 104 class XBeeWiFi : public XBee {
okini3939 2:6efb3541af61 105 public:
okini3939 4:f6d73acc1f75 106 XBeeWiFi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts);
okini3939 2:6efb3541af61 107
okini3939 2:6efb3541af61 108 int setup (int security, const char *ssid, const char *pin);
okini3939 2:6efb3541af61 109 int setup (const char *ssid);
okini3939 2:6efb3541af61 110 int reset ();
okini3939 4:f6d73acc1f75 111 int baud (int b);
okini3939 2:6efb3541af61 112 int setAddress ();
okini3939 2:6efb3541af61 113 int setAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver);
okini3939 3:8573b122fa84 114 int getAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver);
okini3939 2:6efb3541af61 115 int setTimeout (int timeout);
okini3939 2:6efb3541af61 116 int getStatus ();
okini3939 4:f6d73acc1f75 117 int getWiResponse (int apiId, int frameid = 0, int timeout = 3000);
okini3939 2:6efb3541af61 118 #ifdef USE_WIFIDNS
okini3939 5:547cfff7adf7 119 int setNameserver (IpAddr &nameserver, int port);
okini3939 2:6efb3541af61 120 int getHostByName (const char* name, IpAddr &addr);
okini3939 2:6efb3541af61 121 #endif
okini3939 4:f6d73acc1f75 122 /**
okini3939 4:f6d73acc1f75 123 * Call with instance of AtCommandResponse only if getApiId() == AT_COMMAND_RESPONSE
okini3939 4:f6d73acc1f75 124 */
okini3939 4:f6d73acc1f75 125 // void getIPV4RxFrame (XBeeResponse &responses);
okini3939 2:6efb3541af61 126
okini3939 2:6efb3541af61 127 protected:
okini3939 3:8573b122fa84 128 int getWiAddr (IpAddr &ipaddr);
okini3939 2:6efb3541af61 129 #ifdef USE_WIFIDNS
okini3939 2:6efb3541af61 130 int createDnsRequest (const char* name, char *buf);
okini3939 2:6efb3541af61 131 int getDnsResponse (const uint8_t *buf, int len, IpAddr &addr);
okini3939 2:6efb3541af61 132 #endif
okini3939 2:6efb3541af61 133
okini3939 2:6efb3541af61 134 private:
okini3939 2:6efb3541af61 135 IpAddr _nameserver;
okini3939 5:547cfff7adf7 136 int _nameport;
okini3939 2:6efb3541af61 137 };
okini3939 2:6efb3541af61 138
okini3939 2:6efb3541af61 139 /**
okini3939 2:6efb3541af61 140 * Represents a Wi-Fi TX packet that corresponds to Api Id: IPv4_TRANSMIT_REQUEST
okini3939 2:6efb3541af61 141 */
okini3939 2:6efb3541af61 142 class IPv4TransmitRequest : public PayloadRequest {
okini3939 2:6efb3541af61 143 public:
okini3939 2:6efb3541af61 144 /**
okini3939 2:6efb3541af61 145 * Creates a unicast IPv4TransmitRequest with the DEFAULT_FRAME_ID
okini3939 2:6efb3541af61 146 */
okini3939 4:f6d73acc1f75 147 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 148 IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint8_t *data, uint16_t dataLength);
okini3939 2:6efb3541af61 149 /**
okini3939 2:6efb3541af61 150 * Creates a default instance of this class. At a minimum you must specify
okini3939 2:6efb3541af61 151 * a payload, payload length and a destination address before sending this request.
okini3939 2:6efb3541af61 152 */
okini3939 2:6efb3541af61 153 IPv4TransmitRequest();
okini3939 2:6efb3541af61 154 IpAddr& getAddress();
okini3939 2:6efb3541af61 155 uint16_t getDstPort();
okini3939 2:6efb3541af61 156 uint16_t getSrcPort();
okini3939 2:6efb3541af61 157 uint8_t getProtocol();
okini3939 2:6efb3541af61 158 uint8_t getOption();
okini3939 2:6efb3541af61 159 void setAddress(IpAddr& dstAddr);
okini3939 2:6efb3541af61 160 void setDstPort(uint16_t dstPort);
okini3939 2:6efb3541af61 161 void setSrcPort(uint16_t srcPort);
okini3939 2:6efb3541af61 162 void setProtocol(uint8_t protocol);
okini3939 2:6efb3541af61 163 void setOption(uint8_t option);
okini3939 2:6efb3541af61 164 protected:
okini3939 2:6efb3541af61 165 // declare virtual functions
okini3939 4:f6d73acc1f75 166 virtual uint8_t getFrameData(uint16_t pos);
okini3939 4:f6d73acc1f75 167 virtual uint16_t getFrameDataLength();
okini3939 2:6efb3541af61 168 private:
okini3939 2:6efb3541af61 169 IpAddr _dstAddr;
okini3939 2:6efb3541af61 170 uint16_t _dstPort;
okini3939 2:6efb3541af61 171 uint16_t _srcPort;
okini3939 2:6efb3541af61 172 uint8_t _protocol;
okini3939 2:6efb3541af61 173 uint8_t _option;
okini3939 2:6efb3541af61 174 };
okini3939 2:6efb3541af61 175
okini3939 2:6efb3541af61 176 /**
okini3939 2:6efb3541af61 177 * Represents a Wi-Fi TX status packet
okini3939 2:6efb3541af61 178 */
okini3939 2:6efb3541af61 179 class Transmit_Status : public FrameIdResponse {
okini3939 2:6efb3541af61 180 public:
okini3939 2:6efb3541af61 181 Transmit_Status();
okini3939 2:6efb3541af61 182 uint8_t getStatus();
okini3939 2:6efb3541af61 183 bool isSuccess();
okini3939 2:6efb3541af61 184 };
okini3939 2:6efb3541af61 185
okini3939 2:6efb3541af61 186 /**
okini3939 2:6efb3541af61 187 * Represents a Wi-Fi RX packet
okini3939 2:6efb3541af61 188 */
okini3939 2:6efb3541af61 189 class IPV4RxFrame : public RxDataResponse {
okini3939 2:6efb3541af61 190 public:
okini3939 2:6efb3541af61 191 IPV4RxFrame();
okini3939 2:6efb3541af61 192 IpAddr& getSrcAddress();
okini3939 2:6efb3541af61 193 uint16_t getDstPort();
okini3939 2:6efb3541af61 194 uint16_t getSrcPort();
okini3939 2:6efb3541af61 195 uint8_t getProtocol();
okini3939 2:6efb3541af61 196 uint8_t getStatus();
okini3939 4:f6d73acc1f75 197 virtual uint16_t getDataLength();
okini3939 2:6efb3541af61 198 // frame position where data starts
okini3939 2:6efb3541af61 199 virtual uint8_t getDataOffset();
okini3939 2:6efb3541af61 200 private:
okini3939 2:6efb3541af61 201 IpAddr _srcAddr;
okini3939 2:6efb3541af61 202 };
okini3939 2:6efb3541af61 203
okini3939 2:6efb3541af61 204 #endif //XBeeWiFi_h
okini3939 6:6f84e2840408 205 #endif