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

Fork of XBee by Suga koubou

Committer:
okini3939
Date:
Fri Jul 29 16:22:15 2011 +0000
Revision:
2:6efb3541af61
Child:
3:8573b122fa84

        

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 2:6efb3541af61 8 * @brief Weather Station
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 2:6efb3541af61 19 #undef USE_WIFICLASS
okini3939 2:6efb3541af61 20 #undef 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 2:6efb3541af61 56 #define SSID_NOT_CONFIGURED 0x23
okini3939 2:6efb3541af61 57 #define JOINING_AP 0xff
okini3939 2:6efb3541af61 58
okini3939 2:6efb3541af61 59 /// dns
okini3939 2:6efb3541af61 60 #define DNS_QUERY_A 1
okini3939 2:6efb3541af61 61 #define DNS_QUERY_NS 2
okini3939 2:6efb3541af61 62 #define DNS_QUERY_CNAME 5
okini3939 2:6efb3541af61 63 #define DNS_QUERY_PTR 12
okini3939 2:6efb3541af61 64 #define DNS_QUERY_MX 15
okini3939 2:6efb3541af61 65 #define DNS_QUERY_AAAA 28
okini3939 2:6efb3541af61 66 #define DNS_QUERY_ANY 255
okini3939 2:6efb3541af61 67 #define DNS_CLASS_IN 1
okini3939 2:6efb3541af61 68
okini3939 2:6efb3541af61 69 struct DNSHeader {
okini3939 2:6efb3541af61 70 uint16_t id;
okini3939 2:6efb3541af61 71 uint16_t flags;
okini3939 2:6efb3541af61 72 uint16_t questions;
okini3939 2:6efb3541af61 73 uint16_t answers;
okini3939 2:6efb3541af61 74 uint16_t authorities;
okini3939 2:6efb3541af61 75 uint16_t additional;
okini3939 2:6efb3541af61 76 };
okini3939 2:6efb3541af61 77
okini3939 2:6efb3541af61 78 struct DnsQuestionEnd {
okini3939 2:6efb3541af61 79 uint16_t type;
okini3939 2:6efb3541af61 80 uint16_t clas;
okini3939 2:6efb3541af61 81 };
okini3939 2:6efb3541af61 82
okini3939 2:6efb3541af61 83 struct DnsAnswer {
okini3939 2:6efb3541af61 84 uint16_t name;
okini3939 2:6efb3541af61 85 uint16_t type;
okini3939 2:6efb3541af61 86 uint16_t clas;
okini3939 2:6efb3541af61 87 uint32_t ttl;
okini3939 2:6efb3541af61 88 uint16_t length;
okini3939 2:6efb3541af61 89 } __attribute__((packed));
okini3939 2:6efb3541af61 90
okini3939 2:6efb3541af61 91
okini3939 2:6efb3541af61 92 #ifdef USE_WIFICLASS
okini3939 2:6efb3541af61 93 /**
okini3939 2:6efb3541af61 94 * Primary interface for communicating with an XBee Wi-Fi.
okini3939 2:6efb3541af61 95 */
okini3939 2:6efb3541af61 96 class XBeeWiFi : public XBee {
okini3939 2:6efb3541af61 97 public:
okini3939 2:6efb3541af61 98 XBeeWiFi (PinName p_tx, PinName p_rx);
okini3939 2:6efb3541af61 99
okini3939 2:6efb3541af61 100 int setup (int security, const char *ssid, const char *pin);
okini3939 2:6efb3541af61 101 int setup (const char *ssid);
okini3939 2:6efb3541af61 102 int reset ();
okini3939 2:6efb3541af61 103 int setAddress ();
okini3939 2:6efb3541af61 104 int setAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver);
okini3939 2:6efb3541af61 105 int setTimeout (int timeout);
okini3939 2:6efb3541af61 106 int getStatus ();
okini3939 2:6efb3541af61 107 #ifdef USE_WIFIDNS
okini3939 2:6efb3541af61 108 int getHostByName (const char* name, IpAddr &addr);
okini3939 2:6efb3541af61 109 #endif
okini3939 2:6efb3541af61 110
okini3939 2:6efb3541af61 111 protected:
okini3939 2:6efb3541af61 112 int getWiResponse (int apiId, int timeout = 1500);
okini3939 2:6efb3541af61 113 #ifdef USE_WIFIDNS
okini3939 2:6efb3541af61 114 int createDnsRequest (const char* name, char *buf);
okini3939 2:6efb3541af61 115 int getDnsResponse (const uint8_t *buf, int len, IpAddr &addr);
okini3939 2:6efb3541af61 116 #endif
okini3939 2:6efb3541af61 117
okini3939 2:6efb3541af61 118 private:
okini3939 2:6efb3541af61 119 IpAddr _nameserver;
okini3939 2:6efb3541af61 120 };
okini3939 2:6efb3541af61 121 #endif
okini3939 2:6efb3541af61 122
okini3939 2:6efb3541af61 123 /**
okini3939 2:6efb3541af61 124 * Represents a Wi-Fi TX packet that corresponds to Api Id: IPv4_TRANSMIT_REQUEST
okini3939 2:6efb3541af61 125 */
okini3939 2:6efb3541af61 126 class IPv4TransmitRequest : public PayloadRequest {
okini3939 2:6efb3541af61 127 public:
okini3939 2:6efb3541af61 128 /**
okini3939 2:6efb3541af61 129 * Creates a unicast IPv4TransmitRequest with the DEFAULT_FRAME_ID
okini3939 2:6efb3541af61 130 */
okini3939 2:6efb3541af61 131 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 132 IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint8_t *data, uint8_t dataLength);
okini3939 2:6efb3541af61 133 /**
okini3939 2:6efb3541af61 134 * Creates a default instance of this class. At a minimum you must specify
okini3939 2:6efb3541af61 135 * a payload, payload length and a destination address before sending this request.
okini3939 2:6efb3541af61 136 */
okini3939 2:6efb3541af61 137 IPv4TransmitRequest();
okini3939 2:6efb3541af61 138 IpAddr& getAddress();
okini3939 2:6efb3541af61 139 uint16_t getDstPort();
okini3939 2:6efb3541af61 140 uint16_t getSrcPort();
okini3939 2:6efb3541af61 141 uint8_t getProtocol();
okini3939 2:6efb3541af61 142 uint8_t getOption();
okini3939 2:6efb3541af61 143 void setAddress(IpAddr& dstAddr);
okini3939 2:6efb3541af61 144 void setDstPort(uint16_t dstPort);
okini3939 2:6efb3541af61 145 void setSrcPort(uint16_t srcPort);
okini3939 2:6efb3541af61 146 void setProtocol(uint8_t protocol);
okini3939 2:6efb3541af61 147 void setOption(uint8_t option);
okini3939 2:6efb3541af61 148 protected:
okini3939 2:6efb3541af61 149 // declare virtual functions
okini3939 2:6efb3541af61 150 virtual uint8_t getFrameData(uint8_t pos);
okini3939 2:6efb3541af61 151 virtual uint8_t getFrameDataLength();
okini3939 2:6efb3541af61 152 private:
okini3939 2:6efb3541af61 153 IpAddr _dstAddr;
okini3939 2:6efb3541af61 154 uint16_t _dstPort;
okini3939 2:6efb3541af61 155 uint16_t _srcPort;
okini3939 2:6efb3541af61 156 uint8_t _protocol;
okini3939 2:6efb3541af61 157 uint8_t _option;
okini3939 2:6efb3541af61 158 };
okini3939 2:6efb3541af61 159
okini3939 2:6efb3541af61 160 /**
okini3939 2:6efb3541af61 161 * Represents a Wi-Fi TX status packet
okini3939 2:6efb3541af61 162 */
okini3939 2:6efb3541af61 163 class Transmit_Status : public FrameIdResponse {
okini3939 2:6efb3541af61 164 public:
okini3939 2:6efb3541af61 165 Transmit_Status();
okini3939 2:6efb3541af61 166 uint8_t getStatus();
okini3939 2:6efb3541af61 167 bool isSuccess();
okini3939 2:6efb3541af61 168 };
okini3939 2:6efb3541af61 169
okini3939 2:6efb3541af61 170 /**
okini3939 2:6efb3541af61 171 * Represents a Wi-Fi RX packet
okini3939 2:6efb3541af61 172 */
okini3939 2:6efb3541af61 173 class IPV4RxFrame : public RxDataResponse {
okini3939 2:6efb3541af61 174 public:
okini3939 2:6efb3541af61 175 IPV4RxFrame();
okini3939 2:6efb3541af61 176 IpAddr& getSrcAddress();
okini3939 2:6efb3541af61 177 uint16_t getDstPort();
okini3939 2:6efb3541af61 178 uint16_t getSrcPort();
okini3939 2:6efb3541af61 179 uint8_t getProtocol();
okini3939 2:6efb3541af61 180 uint8_t getStatus();
okini3939 2:6efb3541af61 181 virtual uint8_t getDataLength();
okini3939 2:6efb3541af61 182 // frame position where data starts
okini3939 2:6efb3541af61 183 virtual uint8_t getDataOffset();
okini3939 2:6efb3541af61 184 private:
okini3939 2:6efb3541af61 185 IpAddr _srcAddr;
okini3939 2:6efb3541af61 186 };
okini3939 2:6efb3541af61 187
okini3939 2:6efb3541af61 188 #endif //XBeeWiFi_h