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

Fork of XBee by Suga koubou

Committer:
okini3939
Date:
Thu Mar 08 17:41:29 2012 +0000
Revision:
3:8573b122fa84
Parent:
2:6efb3541af61
Child:
4:f6d73acc1f75

        

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 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 3:8573b122fa84 75 #define DNS_SRC_PORT 1234
okini3939 3:8573b122fa84 76 #define DNS_TIMEOUT 5 // x 3s
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 3:8573b122fa84 107 XBeeWiFi (PinName p_tx, PinName p_rx, PinName p_cts);
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 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 3:8573b122fa84 117 int getWiResponse (int apiId, int frameid = 0, int timeout = 1500);
okini3939 2:6efb3541af61 118 #ifdef USE_WIFIDNS
okini3939 3:8573b122fa84 119 int setNameserver (IpAddr &nameserver);
okini3939 2:6efb3541af61 120 int getHostByName (const char* name, IpAddr &addr);
okini3939 2:6efb3541af61 121 #endif
okini3939 2:6efb3541af61 122
okini3939 2:6efb3541af61 123 protected:
okini3939 3:8573b122fa84 124 int getWiAddr (IpAddr &ipaddr);
okini3939 2:6efb3541af61 125 #ifdef USE_WIFIDNS
okini3939 2:6efb3541af61 126 int createDnsRequest (const char* name, char *buf);
okini3939 2:6efb3541af61 127 int getDnsResponse (const uint8_t *buf, int len, IpAddr &addr);
okini3939 2:6efb3541af61 128 #endif
okini3939 2:6efb3541af61 129
okini3939 2:6efb3541af61 130 private:
okini3939 2:6efb3541af61 131 IpAddr _nameserver;
okini3939 2:6efb3541af61 132 };
okini3939 2:6efb3541af61 133 #endif
okini3939 2:6efb3541af61 134
okini3939 2:6efb3541af61 135 /**
okini3939 2:6efb3541af61 136 * Represents a Wi-Fi TX packet that corresponds to Api Id: IPv4_TRANSMIT_REQUEST
okini3939 2:6efb3541af61 137 */
okini3939 2:6efb3541af61 138 class IPv4TransmitRequest : public PayloadRequest {
okini3939 2:6efb3541af61 139 public:
okini3939 2:6efb3541af61 140 /**
okini3939 2:6efb3541af61 141 * Creates a unicast IPv4TransmitRequest with the DEFAULT_FRAME_ID
okini3939 2:6efb3541af61 142 */
okini3939 2:6efb3541af61 143 IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint16_t srcPort, uint8_t protocol, uint8_t option, uint8_t *data, uint8_t dataLength, uint8_t frameId);
okini3939 2:6efb3541af61 144 IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint8_t *data, uint8_t dataLength);
okini3939 2:6efb3541af61 145 /**
okini3939 2:6efb3541af61 146 * Creates a default instance of this class. At a minimum you must specify
okini3939 2:6efb3541af61 147 * a payload, payload length and a destination address before sending this request.
okini3939 2:6efb3541af61 148 */
okini3939 2:6efb3541af61 149 IPv4TransmitRequest();
okini3939 2:6efb3541af61 150 IpAddr& getAddress();
okini3939 2:6efb3541af61 151 uint16_t getDstPort();
okini3939 2:6efb3541af61 152 uint16_t getSrcPort();
okini3939 2:6efb3541af61 153 uint8_t getProtocol();
okini3939 2:6efb3541af61 154 uint8_t getOption();
okini3939 2:6efb3541af61 155 void setAddress(IpAddr& dstAddr);
okini3939 2:6efb3541af61 156 void setDstPort(uint16_t dstPort);
okini3939 2:6efb3541af61 157 void setSrcPort(uint16_t srcPort);
okini3939 2:6efb3541af61 158 void setProtocol(uint8_t protocol);
okini3939 2:6efb3541af61 159 void setOption(uint8_t option);
okini3939 2:6efb3541af61 160 protected:
okini3939 2:6efb3541af61 161 // declare virtual functions
okini3939 2:6efb3541af61 162 virtual uint8_t getFrameData(uint8_t pos);
okini3939 2:6efb3541af61 163 virtual uint8_t getFrameDataLength();
okini3939 2:6efb3541af61 164 private:
okini3939 2:6efb3541af61 165 IpAddr _dstAddr;
okini3939 2:6efb3541af61 166 uint16_t _dstPort;
okini3939 2:6efb3541af61 167 uint16_t _srcPort;
okini3939 2:6efb3541af61 168 uint8_t _protocol;
okini3939 2:6efb3541af61 169 uint8_t _option;
okini3939 2:6efb3541af61 170 };
okini3939 2:6efb3541af61 171
okini3939 2:6efb3541af61 172 /**
okini3939 2:6efb3541af61 173 * Represents a Wi-Fi TX status packet
okini3939 2:6efb3541af61 174 */
okini3939 2:6efb3541af61 175 class Transmit_Status : public FrameIdResponse {
okini3939 2:6efb3541af61 176 public:
okini3939 2:6efb3541af61 177 Transmit_Status();
okini3939 2:6efb3541af61 178 uint8_t getStatus();
okini3939 2:6efb3541af61 179 bool isSuccess();
okini3939 2:6efb3541af61 180 };
okini3939 2:6efb3541af61 181
okini3939 2:6efb3541af61 182 /**
okini3939 2:6efb3541af61 183 * Represents a Wi-Fi RX packet
okini3939 2:6efb3541af61 184 */
okini3939 2:6efb3541af61 185 class IPV4RxFrame : public RxDataResponse {
okini3939 2:6efb3541af61 186 public:
okini3939 2:6efb3541af61 187 IPV4RxFrame();
okini3939 2:6efb3541af61 188 IpAddr& getSrcAddress();
okini3939 2:6efb3541af61 189 uint16_t getDstPort();
okini3939 2:6efb3541af61 190 uint16_t getSrcPort();
okini3939 2:6efb3541af61 191 uint8_t getProtocol();
okini3939 2:6efb3541af61 192 uint8_t getStatus();
okini3939 2:6efb3541af61 193 virtual uint8_t getDataLength();
okini3939 2:6efb3541af61 194 // frame position where data starts
okini3939 2:6efb3541af61 195 virtual uint8_t getDataOffset();
okini3939 2:6efb3541af61 196 private:
okini3939 2:6efb3541af61 197 IpAddr _srcAddr;
okini3939 2:6efb3541af61 198 };
okini3939 2:6efb3541af61 199
okini3939 2:6efb3541af61 200 #endif //XBeeWiFi_h