Poerting the Xbee library to use teh wifi functionality of the LPC4088.

Dependents:   XBee_WiFi_EA_LPC4088

Fork of XBee by Suga koubou

Committer:
okini3939
Date:
Tue Mar 13 09:11:35 2012 +0000
Revision:
5:547cfff7adf7
Parent:
4:f6d73acc1f75
Child:
6:6f84e2840408

        

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 3:8573b122fa84 11 //#define DEBUG
okini3939 3:8573b122fa84 12 #include "dbg.h"
okini3939 3:8573b122fa84 13
okini3939 2:6efb3541af61 14 #include "mbed.h"
okini3939 2:6efb3541af61 15 #include "XBee.h"
okini3939 2:6efb3541af61 16 #include "XBeeWiFi.h"
okini3939 2:6efb3541af61 17 #include "EthernetNetIf.h"
okini3939 2:6efb3541af61 18
okini3939 2:6efb3541af61 19 #define REVERSE_ENDIAN(x) (uint16_t)(((uint16_t)x >> 8) | ((uint16_t)x << 8))
okini3939 2:6efb3541af61 20
okini3939 2:6efb3541af61 21 #ifdef USE_WIFICLASS
okini3939 4:f6d73acc1f75 22 XBeeWiFi::XBeeWiFi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts) : XBee(p_tx, p_rx, p_cts, p_rts) {
okini3939 2:6efb3541af61 23 }
okini3939 2:6efb3541af61 24
okini3939 2:6efb3541af61 25 int XBeeWiFi::setup (int security, const char *ssid, const char *pin) {
okini3939 3:8573b122fa84 26 int len, r;
okini3939 2:6efb3541af61 27 uint8_t cmd[2], val[32];
okini3939 2:6efb3541af61 28 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 29
okini3939 3:8573b122fa84 30 // SSID
okini3939 3:8573b122fa84 31 memcpy(cmd, "ID", 2);
okini3939 3:8573b122fa84 32 len = strlen(ssid);
okini3939 3:8573b122fa84 33 memcpy(val, ssid, len);
okini3939 3:8573b122fa84 34 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 35 atRequest.setCommandValue(val);
okini3939 3:8573b122fa84 36 atRequest.setCommandValueLength(len);
okini3939 3:8573b122fa84 37 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 38 send(atRequest);
okini3939 3:8573b122fa84 39 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 40 DBG("wifi ID: %d\r\n", r);
okini3939 3:8573b122fa84 41 if (r < 0) return -1;
okini3939 3:8573b122fa84 42
okini3939 3:8573b122fa84 43 if (security != SECURITY_OPEN) {
okini3939 3:8573b122fa84 44 // PIN
okini3939 3:8573b122fa84 45 memcpy(cmd, "PK", 2);
okini3939 3:8573b122fa84 46 len = strlen(pin);
okini3939 3:8573b122fa84 47 memcpy(val, pin, len);
okini3939 3:8573b122fa84 48 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 49 atRequest.setCommandValue(val);
okini3939 3:8573b122fa84 50 atRequest.setCommandValueLength(len);
okini3939 3:8573b122fa84 51 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 52 send(atRequest);
okini3939 3:8573b122fa84 53 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 54 DBG("wifi PK: %d\r\n", r);
okini3939 3:8573b122fa84 55 if (r < 0) return -1;
okini3939 3:8573b122fa84 56 }
okini3939 3:8573b122fa84 57
okini3939 2:6efb3541af61 58 // security type
okini3939 2:6efb3541af61 59 memcpy(cmd, "EE", 2);
okini3939 2:6efb3541af61 60 val[0] = security;
okini3939 2:6efb3541af61 61 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 62 atRequest.setCommandValue(val);
okini3939 2:6efb3541af61 63 atRequest.setCommandValueLength(1);
okini3939 3:8573b122fa84 64 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 65 send(atRequest);
okini3939 3:8573b122fa84 66 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 67 DBG("wifi EE: %d\r\n", r);
okini3939 3:8573b122fa84 68 if (r < 0) return -1;
okini3939 2:6efb3541af61 69
okini3939 2:6efb3541af61 70 return 0;
okini3939 2:6efb3541af61 71 }
okini3939 2:6efb3541af61 72
okini3939 2:6efb3541af61 73 int XBeeWiFi::setup (const char *ssid) {
okini3939 2:6efb3541af61 74 return setup(SECURITY_OPEN, ssid, NULL);
okini3939 2:6efb3541af61 75 }
okini3939 2:6efb3541af61 76
okini3939 2:6efb3541af61 77 int XBeeWiFi::reset () {
okini3939 2:6efb3541af61 78 // RESET
okini3939 3:8573b122fa84 79 uint8_t cmd[2] = {'N', 'R'}; // Network reset
okini3939 3:8573b122fa84 80 // uint8_t cmd[2] = {'F', 'R'}; // Software reset
okini3939 2:6efb3541af61 81 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 82
okini3939 2:6efb3541af61 83 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 84 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 85 send(atRequest);
okini3939 3:8573b122fa84 86 return getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 87 }
okini3939 2:6efb3541af61 88
okini3939 4:f6d73acc1f75 89 int XBeeWiFi::baud (int b) {
okini3939 4:f6d73acc1f75 90 // RESET
okini3939 4:f6d73acc1f75 91 uint8_t cmd[2] = {'B', 'D'};
okini3939 4:f6d73acc1f75 92 char val[4];
okini3939 4:f6d73acc1f75 93 AtCommandRequest atRequest;
okini3939 4:f6d73acc1f75 94 int r, len;
okini3939 4:f6d73acc1f75 95
okini3939 4:f6d73acc1f75 96 if (b < 0x100) {
okini3939 4:f6d73acc1f75 97 val[0] = b;
okini3939 4:f6d73acc1f75 98 len = 1;
okini3939 4:f6d73acc1f75 99 } else {
okini3939 4:f6d73acc1f75 100 val[0] = (b >> 24) & 0xff;
okini3939 4:f6d73acc1f75 101 val[1] = (b >> 16) & 0xff;
okini3939 4:f6d73acc1f75 102 val[2] = (b >> 8) & 0xff;
okini3939 4:f6d73acc1f75 103 val[3] = b & 0xff;
okini3939 4:f6d73acc1f75 104 len = 4;
okini3939 4:f6d73acc1f75 105 }
okini3939 4:f6d73acc1f75 106 atRequest.setCommand(cmd);
okini3939 4:f6d73acc1f75 107 atRequest.setCommandValue((uint8_t*)val);
okini3939 4:f6d73acc1f75 108 atRequest.setCommandValueLength(len);
okini3939 4:f6d73acc1f75 109 atRequest.setFrameId(getNextFrameId());
okini3939 4:f6d73acc1f75 110 send(atRequest);
okini3939 4:f6d73acc1f75 111 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 4:f6d73acc1f75 112
okini3939 4:f6d73acc1f75 113 if (r == 0) {
okini3939 4:f6d73acc1f75 114 begin(b);
okini3939 4:f6d73acc1f75 115 }
okini3939 4:f6d73acc1f75 116 return r;
okini3939 4:f6d73acc1f75 117 }
okini3939 4:f6d73acc1f75 118
okini3939 2:6efb3541af61 119 int XBeeWiFi::setAddress () {
okini3939 2:6efb3541af61 120 // DHCP
okini3939 2:6efb3541af61 121 uint8_t cmd[2] = {'M', 'A'};
okini3939 2:6efb3541af61 122 char val[1] = {0};
okini3939 2:6efb3541af61 123 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 124
okini3939 5:547cfff7adf7 125 _nameserver = IpAddr(0,0,0,0);
okini3939 5:547cfff7adf7 126 _nameport = DNS_PORT;
okini3939 5:547cfff7adf7 127
okini3939 2:6efb3541af61 128 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 129 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 130 atRequest.setCommandValueLength(1);
okini3939 3:8573b122fa84 131 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 132 send(atRequest);
okini3939 2:6efb3541af61 133
okini3939 3:8573b122fa84 134 return getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 135 }
okini3939 2:6efb3541af61 136
okini3939 2:6efb3541af61 137 int XBeeWiFi::setAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver) {
okini3939 2:6efb3541af61 138 uint8_t cmd[2];
okini3939 2:6efb3541af61 139 char val[32];
okini3939 2:6efb3541af61 140 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 141
okini3939 2:6efb3541af61 142 // Static
okini3939 2:6efb3541af61 143 memcpy(cmd, "MA", 2);
okini3939 2:6efb3541af61 144 val[0] = 1;
okini3939 2:6efb3541af61 145 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 146 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 147 atRequest.setCommandValueLength(1);
okini3939 3:8573b122fa84 148 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 149 send(atRequest);
okini3939 3:8573b122fa84 150 getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 151
okini3939 2:6efb3541af61 152 // IP address
okini3939 2:6efb3541af61 153 memcpy(cmd, "MY", 2);
okini3939 2:6efb3541af61 154 sprintf(val, "%d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
okini3939 2:6efb3541af61 155 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 156 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 157 atRequest.setCommandValueLength(strlen(val));
okini3939 3:8573b122fa84 158 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 159 send(atRequest);
okini3939 3:8573b122fa84 160 getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 161
okini3939 2:6efb3541af61 162 // sub netmask
okini3939 2:6efb3541af61 163 memcpy(cmd, "NK", 2);
okini3939 2:6efb3541af61 164 sprintf(val, "%d.%d.%d.%d", netmask[0], netmask[1], netmask[2], netmask[3]);
okini3939 2:6efb3541af61 165 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 166 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 167 atRequest.setCommandValueLength(strlen(val));
okini3939 3:8573b122fa84 168 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 169 send(atRequest);
okini3939 3:8573b122fa84 170 getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 171
okini3939 2:6efb3541af61 172 // default gateway
okini3939 2:6efb3541af61 173 memcpy(cmd, "GW", 2);
okini3939 2:6efb3541af61 174 sprintf(val, "%d.%d.%d.%d", gateway[0], gateway[1], gateway[2], gateway[3]);
okini3939 2:6efb3541af61 175 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 176 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 177 atRequest.setCommandValueLength(strlen(val));
okini3939 3:8573b122fa84 178 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 179 send(atRequest);
okini3939 3:8573b122fa84 180 getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 181
okini3939 2:6efb3541af61 182 // name server
okini3939 2:6efb3541af61 183 _nameserver = nameserver;
okini3939 5:547cfff7adf7 184 _nameport = DNS_PORT;
okini3939 2:6efb3541af61 185
okini3939 2:6efb3541af61 186 return 0;
okini3939 2:6efb3541af61 187 }
okini3939 2:6efb3541af61 188
okini3939 3:8573b122fa84 189 int XBeeWiFi::getAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver) {
okini3939 3:8573b122fa84 190 int r;
okini3939 3:8573b122fa84 191 uint8_t cmd[2];
okini3939 3:8573b122fa84 192 AtCommandRequest atRequest;
okini3939 3:8573b122fa84 193 AtCommandResponse atResponse;
okini3939 3:8573b122fa84 194
okini3939 3:8573b122fa84 195 memcpy(cmd, "MY", 2);
okini3939 3:8573b122fa84 196 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 197 atRequest.clearCommandValue();
okini3939 3:8573b122fa84 198 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 199 send(atRequest);
okini3939 3:8573b122fa84 200 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 201 DBG("wifi MY: %d\r\n", r);
okini3939 3:8573b122fa84 202 if (r >= 0) {
okini3939 3:8573b122fa84 203 r = getWiAddr(ipaddr);
okini3939 3:8573b122fa84 204 }
okini3939 3:8573b122fa84 205
okini3939 3:8573b122fa84 206 memcpy(cmd, "MK", 2);
okini3939 3:8573b122fa84 207 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 208 atRequest.clearCommandValue();
okini3939 3:8573b122fa84 209 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 210 send(atRequest);
okini3939 3:8573b122fa84 211 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 212 DBG("wifi MK: %d\r\n", r);
okini3939 3:8573b122fa84 213 if (r >= 0) {
okini3939 3:8573b122fa84 214 r = getWiAddr(netmask);
okini3939 3:8573b122fa84 215 }
okini3939 3:8573b122fa84 216
okini3939 3:8573b122fa84 217 memcpy(cmd, "GW", 2);
okini3939 3:8573b122fa84 218 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 219 atRequest.clearCommandValue();
okini3939 3:8573b122fa84 220 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 221 send(atRequest);
okini3939 3:8573b122fa84 222 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 223 DBG("wifi GW: %d\r\n", r);
okini3939 3:8573b122fa84 224 if (r >= 0) {
okini3939 3:8573b122fa84 225 r = getWiAddr(gateway);
okini3939 3:8573b122fa84 226 }
okini3939 3:8573b122fa84 227
okini3939 3:8573b122fa84 228 nameserver = _nameserver;
okini3939 3:8573b122fa84 229
okini3939 3:8573b122fa84 230 return 0;
okini3939 3:8573b122fa84 231 }
okini3939 3:8573b122fa84 232
okini3939 3:8573b122fa84 233 int XBeeWiFi::getWiAddr (IpAddr &ipaddr) {
okini3939 3:8573b122fa84 234 int ip1, ip2, ip3, ip4;
okini3939 3:8573b122fa84 235 AtCommandResponse atResponse;
okini3939 3:8573b122fa84 236
okini3939 3:8573b122fa84 237 getResponse().getAtCommandResponse(atResponse);
okini3939 3:8573b122fa84 238 if (atResponse.isOk() && atResponse.getValueLength() >= 7) {
okini3939 3:8573b122fa84 239 sscanf((char*)atResponse.getValue(), "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
okini3939 3:8573b122fa84 240 ipaddr = IpAddr(ip1, ip2, ip3, ip4);
okini3939 3:8573b122fa84 241 return 0;
okini3939 3:8573b122fa84 242 }
okini3939 3:8573b122fa84 243 return -1;
okini3939 3:8573b122fa84 244 }
okini3939 3:8573b122fa84 245
okini3939 2:6efb3541af61 246 int XBeeWiFi::setTimeout (int timeout) {
okini3939 2:6efb3541af61 247 // timeout
okini3939 2:6efb3541af61 248 uint8_t cmd[2] = {'T', 'P'};
okini3939 2:6efb3541af61 249 char val[1];
okini3939 2:6efb3541af61 250 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 251
okini3939 2:6efb3541af61 252 val[0] = timeout;
okini3939 2:6efb3541af61 253 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 254 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 255 atRequest.setCommandValueLength(1);
okini3939 3:8573b122fa84 256 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 257 send(atRequest);
okini3939 2:6efb3541af61 258
okini3939 2:6efb3541af61 259 return getWiResponse(AT_COMMAND_RESPONSE);
okini3939 2:6efb3541af61 260 }
okini3939 2:6efb3541af61 261
okini3939 2:6efb3541af61 262 int XBeeWiFi::getStatus () {
okini3939 2:6efb3541af61 263 // AI
okini3939 2:6efb3541af61 264 uint8_t cmd[2] = {'A', 'I'};
okini3939 2:6efb3541af61 265 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 266
okini3939 2:6efb3541af61 267 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 268 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 269 send(atRequest);
okini3939 2:6efb3541af61 270
okini3939 3:8573b122fa84 271 return getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 272 }
okini3939 2:6efb3541af61 273
okini3939 3:8573b122fa84 274 int XBeeWiFi::getWiResponse (int apiId, int frameid, int timeout) {
okini3939 2:6efb3541af61 275 AtCommandResponse atResponse;
okini3939 2:6efb3541af61 276
okini3939 2:6efb3541af61 277 if (readPacket(timeout)) {
okini3939 3:8573b122fa84 278 if (frameid && frameid != getResponse().getFrameData()[0]) {
okini3939 3:8573b122fa84 279 DBG("Expected AT response but got %x (frame %d)\r\n", getResponse().getApiId(), getResponse().getFrameData()[0]);
okini3939 3:8573b122fa84 280 if (! readPacket(timeout)) return -1;
okini3939 3:8573b122fa84 281 DBG("Retry ok\r\n");
okini3939 3:8573b122fa84 282 }
okini3939 3:8573b122fa84 283
okini3939 2:6efb3541af61 284 if (getResponse().getApiId() == apiId) {
okini3939 2:6efb3541af61 285 getResponse().getAtCommandResponse(atResponse);
okini3939 2:6efb3541af61 286
okini3939 3:8573b122fa84 287 if (getResponse().getApiId() == IPv4_RX_FRAME) {
okini3939 3:8573b122fa84 288 return 0;
okini3939 3:8573b122fa84 289 } else
okini3939 2:6efb3541af61 290 if (atResponse.isOk()) {
okini3939 2:6efb3541af61 291
okini3939 3:8573b122fa84 292 // if (getResponse().getFrameDataLength() > 4) {
okini3939 2:6efb3541af61 293 return atResponse.getValue()[0];
okini3939 2:6efb3541af61 294
okini3939 2:6efb3541af61 295 } else {
okini3939 3:8573b122fa84 296 DBG("Command return error code: %x\r\n", atResponse.getStatus());
okini3939 2:6efb3541af61 297 }
okini3939 2:6efb3541af61 298 } else {
okini3939 3:8573b122fa84 299 DBG("Expected AT response but got %x\r\n", getResponse().getApiId());
okini3939 2:6efb3541af61 300 }
okini3939 2:6efb3541af61 301 } else {
okini3939 2:6efb3541af61 302 if (getResponse().isError()) {
okini3939 3:8573b122fa84 303 DBG("Error reading packet. Error code: %x\r\n", getResponse().getErrorCode());
okini3939 2:6efb3541af61 304 } else {
okini3939 3:8573b122fa84 305 DBG("No response from radio\r\n");
okini3939 2:6efb3541af61 306 }
okini3939 2:6efb3541af61 307 }
okini3939 2:6efb3541af61 308
okini3939 2:6efb3541af61 309 return -1;
okini3939 2:6efb3541af61 310 }
okini3939 2:6efb3541af61 311
okini3939 2:6efb3541af61 312 #ifdef USE_WIFIDNS
okini3939 5:547cfff7adf7 313 int XBeeWiFi::setNameserver (IpAddr &nameserver, int port = DNS_PORT) {
okini3939 3:8573b122fa84 314 _nameserver = nameserver;
okini3939 5:547cfff7adf7 315 _nameport = port;
okini3939 3:8573b122fa84 316 return 0;
okini3939 3:8573b122fa84 317 }
okini3939 3:8573b122fa84 318
okini3939 4:f6d73acc1f75 319 // *** Note: wifi is turned off when XBee send the port 53 udp packet.
okini3939 2:6efb3541af61 320 int XBeeWiFi::getHostByName (const char* name, IpAddr &addr) {
okini3939 4:f6d73acc1f75 321 Timer timeout;
okini3939 4:f6d73acc1f75 322 char buf[600];
okini3939 4:f6d73acc1f75 323 int r, len;
okini3939 3:8573b122fa84 324 uint8_t cmd[2] = {'C', '0'};
okini3939 3:8573b122fa84 325 char val[2];
okini3939 2:6efb3541af61 326 IPv4TransmitRequest dnsRequest;
okini3939 2:6efb3541af61 327 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 328 AtCommandResponse atResponse;
okini3939 2:6efb3541af61 329
okini3939 2:6efb3541af61 330 if (!strcmp(name, "localhost")) {
okini3939 2:6efb3541af61 331 addr = IpAddr(127, 0, 0, 1);
okini3939 2:6efb3541af61 332 return 0;
okini3939 2:6efb3541af61 333 }
okini3939 5:547cfff7adf7 334
okini3939 3:8573b122fa84 335 // bind src port
okini3939 3:8573b122fa84 336 val[0] = (DNS_SRC_PORT >> 8) & 0xff;
okini3939 3:8573b122fa84 337 val[1] = DNS_SRC_PORT & 0xff;
okini3939 3:8573b122fa84 338 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 339 atRequest.setCommandValue((uint8_t*)val);
okini3939 3:8573b122fa84 340 atRequest.setCommandValueLength(2);
okini3939 3:8573b122fa84 341 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 342 send(atRequest);
okini3939 3:8573b122fa84 343 r = getWiResponse(AT_COMMAND_RESPONSE);
okini3939 3:8573b122fa84 344 DBG("wifi C0: %d\r\n", r);
okini3939 5:547cfff7adf7 345
okini3939 2:6efb3541af61 346 // send DNS request
okini3939 3:8573b122fa84 347 len = createDnsRequest(name, buf);
okini3939 2:6efb3541af61 348 dnsRequest.setAddress(_nameserver);
okini3939 5:547cfff7adf7 349 dnsRequest.setDstPort(_nameport);
okini3939 3:8573b122fa84 350 dnsRequest.setSrcPort(DNS_SRC_PORT);
okini3939 2:6efb3541af61 351 dnsRequest.setProtocol(PROTOCOL_UDP);
okini3939 2:6efb3541af61 352 dnsRequest.setPayload((uint8_t*)buf);
okini3939 2:6efb3541af61 353 dnsRequest.setPayloadLength(len);
okini3939 4:f6d73acc1f75 354
okini3939 4:f6d73acc1f75 355 // wait responce
okini3939 4:f6d73acc1f75 356 timeout.reset();
okini3939 4:f6d73acc1f75 357 timeout.start();
okini3939 4:f6d73acc1f75 358 while (timeout.read_ms() < DNS_TIMEOUT) {
okini3939 3:8573b122fa84 359 dnsRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 360 send(dnsRequest);
okini3939 3:8573b122fa84 361
okini3939 3:8573b122fa84 362 r = getWiResponse(TX_STATUS_RESPONSE, dnsRequest.getFrameId());
okini3939 3:8573b122fa84 363 DBG("wifi TX: %d\r\n", r);
okini3939 2:6efb3541af61 364
okini3939 4:f6d73acc1f75 365 if (r >= 0) {
okini3939 3:8573b122fa84 366 // recv DNS request
okini3939 3:8573b122fa84 367 r = getWiResponse(IPv4_RX_FRAME, 0, 3000);
okini3939 3:8573b122fa84 368 DBG("wifi RX: %d\r\n", r);
okini3939 3:8573b122fa84 369 if (r >= 0) {
okini3939 4:f6d73acc1f75 370 timeout.stop();
okini3939 3:8573b122fa84 371 getResponse().getAtCommandResponse(atResponse);
okini3939 3:8573b122fa84 372 return getDnsResponse(atResponse.getValue() + 6, atResponse.getValueLength() - 6, addr);
okini3939 2:6efb3541af61 373 }
okini3939 3:8573b122fa84 374 } else {
okini3939 3:8573b122fa84 375 break;
okini3939 2:6efb3541af61 376 }
okini3939 2:6efb3541af61 377 }
okini3939 4:f6d73acc1f75 378 timeout.stop();
okini3939 2:6efb3541af61 379
okini3939 2:6efb3541af61 380 return -1;
okini3939 2:6efb3541af61 381 }
okini3939 2:6efb3541af61 382
okini3939 2:6efb3541af61 383 int XBeeWiFi::createDnsRequest (const char* name, char *buf) {
okini3939 2:6efb3541af61 384 struct DNSHeader *dnsHeader;
okini3939 2:6efb3541af61 385 struct DnsQuestionEnd *dnsEnd;
okini3939 2:6efb3541af61 386 int len, num;
okini3939 2:6efb3541af61 387
okini3939 2:6efb3541af61 388 // DNS header
okini3939 2:6efb3541af61 389 dnsHeader = (DNSHeader*)buf;
okini3939 2:6efb3541af61 390 dnsHeader->id = REVERSE_ENDIAN(0xdead);
okini3939 2:6efb3541af61 391 dnsHeader->flags = REVERSE_ENDIAN(0x100);
okini3939 2:6efb3541af61 392 dnsHeader->questions = REVERSE_ENDIAN(1);
okini3939 2:6efb3541af61 393 dnsHeader->answers = 0;
okini3939 2:6efb3541af61 394 dnsHeader->authorities = 0;
okini3939 2:6efb3541af61 395 dnsHeader->additional = 0;
okini3939 2:6efb3541af61 396
okini3939 2:6efb3541af61 397 // DNS question
okini3939 3:8573b122fa84 398 len = sizeof(DNSHeader);
okini3939 3:8573b122fa84 399 while ((num = (int)strchr(name, '.')) != NULL) {
okini3939 2:6efb3541af61 400 num = num - (int)name;
okini3939 2:6efb3541af61 401 buf[len] = num;
okini3939 2:6efb3541af61 402 len ++;
okini3939 2:6efb3541af61 403 strncpy(&buf[len], name, num);
okini3939 3:8573b122fa84 404 name = name + num + 1;
okini3939 2:6efb3541af61 405 len = len + num;
okini3939 2:6efb3541af61 406 }
okini3939 2:6efb3541af61 407
okini3939 3:8573b122fa84 408 if ((num = strlen(name)) != NULL) {
okini3939 2:6efb3541af61 409 buf[len] = num;
okini3939 2:6efb3541af61 410 len ++;
okini3939 2:6efb3541af61 411 strncpy(&buf[len], name, num);
okini3939 3:8573b122fa84 412 len = len + num;
okini3939 2:6efb3541af61 413 }
okini3939 3:8573b122fa84 414 buf[len] = 0;
okini3939 3:8573b122fa84 415 len ++;
okini3939 2:6efb3541af61 416
okini3939 2:6efb3541af61 417 dnsEnd = (DnsQuestionEnd*)&buf[len];
okini3939 2:6efb3541af61 418 dnsEnd->type = REVERSE_ENDIAN(DNS_QUERY_A);
okini3939 2:6efb3541af61 419 dnsEnd->clas = REVERSE_ENDIAN(DNS_CLASS_IN);
okini3939 2:6efb3541af61 420
okini3939 3:8573b122fa84 421 return len + sizeof(DnsQuestionEnd);
okini3939 2:6efb3541af61 422 }
okini3939 2:6efb3541af61 423
okini3939 2:6efb3541af61 424 int XBeeWiFi::getDnsResponse (const uint8_t *buf, int len, IpAddr &addr) {
okini3939 2:6efb3541af61 425 int i;
okini3939 2:6efb3541af61 426 struct DNSHeader *dnsHeader;
okini3939 2:6efb3541af61 427 struct DnsAnswer *dnsAnswer;
okini3939 2:6efb3541af61 428
okini3939 2:6efb3541af61 429 // DNS header
okini3939 2:6efb3541af61 430 dnsHeader = (DNSHeader*)buf;
okini3939 2:6efb3541af61 431 if (REVERSE_ENDIAN(dnsHeader->id) != 0xdead || (REVERSE_ENDIAN(dnsHeader->flags) & 0x800f) != 0x8000) {
okini3939 2:6efb3541af61 432 return -1;
okini3939 2:6efb3541af61 433 }
okini3939 2:6efb3541af61 434
okini3939 2:6efb3541af61 435 // skip question
okini3939 3:8573b122fa84 436 for (i = sizeof(DNSHeader); buf[i] && i < len; i ++);
okini3939 2:6efb3541af61 437 i = i + 1 + sizeof(DnsQuestionEnd);
okini3939 2:6efb3541af61 438
okini3939 2:6efb3541af61 439 // DNS answer
okini3939 2:6efb3541af61 440 while (i < len) {
okini3939 2:6efb3541af61 441 dnsAnswer = (DnsAnswer*)&buf[i];
okini3939 2:6efb3541af61 442 if (dnsAnswer->clas != REVERSE_ENDIAN(DNS_CLASS_IN)) {
okini3939 2:6efb3541af61 443 return -1;
okini3939 2:6efb3541af61 444 }
okini3939 2:6efb3541af61 445
okini3939 3:8573b122fa84 446 i = i + sizeof(DnsAnswer);
okini3939 2:6efb3541af61 447 if (dnsAnswer->type == REVERSE_ENDIAN(DNS_QUERY_A)) {
okini3939 2:6efb3541af61 448 addr = IpAddr(buf[i], buf[i + 1], buf[i + 2], buf[i + 3]);
okini3939 2:6efb3541af61 449 return 0;
okini3939 2:6efb3541af61 450 }
okini3939 2:6efb3541af61 451 i = i + dnsAnswer->length;
okini3939 2:6efb3541af61 452 }
okini3939 2:6efb3541af61 453
okini3939 2:6efb3541af61 454 return -1;
okini3939 2:6efb3541af61 455 }
okini3939 2:6efb3541af61 456 #endif
okini3939 2:6efb3541af61 457
okini3939 2:6efb3541af61 458 #endif
okini3939 2:6efb3541af61 459
okini3939 2:6efb3541af61 460
okini3939 2:6efb3541af61 461 IPv4TransmitRequest::IPv4TransmitRequest() : PayloadRequest(IPv4_TRANSMIT_REQUEST, DEFAULT_FRAME_ID, NULL, 0) {
okini3939 2:6efb3541af61 462 }
okini3939 2:6efb3541af61 463
okini3939 4:f6d73acc1f75 464 IPv4TransmitRequest::IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint16_t srcPort, uint8_t protocol, uint8_t option, uint8_t *data, uint16_t dataLength, uint8_t frameId): PayloadRequest(IPv4_TRANSMIT_REQUEST, frameId, data, dataLength) {
okini3939 2:6efb3541af61 465 _dstAddr = dstAddr;
okini3939 2:6efb3541af61 466 _dstPort = dstPort;
okini3939 2:6efb3541af61 467 _srcPort = srcPort;
okini3939 2:6efb3541af61 468 _protocol = protocol;
okini3939 2:6efb3541af61 469 _option = option;
okini3939 2:6efb3541af61 470 }
okini3939 2:6efb3541af61 471
okini3939 4:f6d73acc1f75 472 IPv4TransmitRequest::IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint8_t *data, uint16_t dataLength): PayloadRequest(IPv4_TRANSMIT_REQUEST, DEFAULT_FRAME_ID, data, dataLength) {
okini3939 2:6efb3541af61 473 _dstAddr = dstAddr;
okini3939 2:6efb3541af61 474 _dstPort = dstPort;
okini3939 2:6efb3541af61 475 _srcPort = 0x270f;
okini3939 2:6efb3541af61 476 _protocol = PROTOCOL_UDP;
okini3939 2:6efb3541af61 477 _option = 0;
okini3939 2:6efb3541af61 478 }
okini3939 2:6efb3541af61 479
okini3939 4:f6d73acc1f75 480 uint8_t IPv4TransmitRequest::getFrameData(uint16_t pos) {
okini3939 2:6efb3541af61 481 if (pos == 0) {
okini3939 2:6efb3541af61 482 return _dstAddr[0];
okini3939 2:6efb3541af61 483 } else if (pos == 1) {
okini3939 2:6efb3541af61 484 return _dstAddr[1];
okini3939 2:6efb3541af61 485 } else if (pos == 2) {
okini3939 2:6efb3541af61 486 return _dstAddr[2];
okini3939 2:6efb3541af61 487 } else if (pos == 3) {
okini3939 2:6efb3541af61 488 return _dstAddr[3];
okini3939 2:6efb3541af61 489 } else if (pos == 4) {
okini3939 2:6efb3541af61 490 return (_dstPort >> 8) & 0xff;
okini3939 2:6efb3541af61 491 } else if (pos == 5) {
okini3939 2:6efb3541af61 492 return _dstPort & 0xff;
okini3939 2:6efb3541af61 493 } else if (pos == 6) {
okini3939 2:6efb3541af61 494 return (_srcPort >> 8) & 0xff;
okini3939 2:6efb3541af61 495 } else if (pos == 7) {
okini3939 2:6efb3541af61 496 return _srcPort & 0xff;
okini3939 2:6efb3541af61 497 } else if (pos == 8) {
okini3939 2:6efb3541af61 498 return _protocol;
okini3939 2:6efb3541af61 499 } else if (pos == 9) {
okini3939 2:6efb3541af61 500 return _option;
okini3939 2:6efb3541af61 501 } else {
okini3939 2:6efb3541af61 502 return getPayload()[pos - IPv4_TRANSMIT_REQUEST_API_LENGTH];
okini3939 2:6efb3541af61 503 }
okini3939 2:6efb3541af61 504 }
okini3939 2:6efb3541af61 505
okini3939 4:f6d73acc1f75 506 uint16_t IPv4TransmitRequest::getFrameDataLength() {
okini3939 2:6efb3541af61 507 return IPv4_TRANSMIT_REQUEST_API_LENGTH + getPayloadLength();
okini3939 2:6efb3541af61 508 }
okini3939 2:6efb3541af61 509
okini3939 2:6efb3541af61 510 IpAddr& IPv4TransmitRequest::getAddress() {
okini3939 2:6efb3541af61 511 return _dstAddr;
okini3939 2:6efb3541af61 512 }
okini3939 2:6efb3541af61 513
okini3939 2:6efb3541af61 514 uint16_t IPv4TransmitRequest::getDstPort() {
okini3939 2:6efb3541af61 515 return _dstPort;
okini3939 2:6efb3541af61 516 }
okini3939 2:6efb3541af61 517
okini3939 2:6efb3541af61 518 uint16_t IPv4TransmitRequest::getSrcPort() {
okini3939 2:6efb3541af61 519 return _srcPort;
okini3939 2:6efb3541af61 520 }
okini3939 2:6efb3541af61 521
okini3939 2:6efb3541af61 522 uint8_t IPv4TransmitRequest::getProtocol() {
okini3939 2:6efb3541af61 523 return _protocol;
okini3939 2:6efb3541af61 524 }
okini3939 2:6efb3541af61 525
okini3939 2:6efb3541af61 526 uint8_t IPv4TransmitRequest::getOption() {
okini3939 2:6efb3541af61 527 return _option;
okini3939 2:6efb3541af61 528 }
okini3939 2:6efb3541af61 529
okini3939 2:6efb3541af61 530 void IPv4TransmitRequest::setAddress(IpAddr &dstAddr) {
okini3939 2:6efb3541af61 531 _dstAddr = dstAddr;
okini3939 2:6efb3541af61 532 }
okini3939 2:6efb3541af61 533
okini3939 2:6efb3541af61 534 void IPv4TransmitRequest::setDstPort(uint16_t dstPort) {
okini3939 2:6efb3541af61 535 _dstPort = dstPort;
okini3939 2:6efb3541af61 536 }
okini3939 2:6efb3541af61 537
okini3939 2:6efb3541af61 538 void IPv4TransmitRequest::setSrcPort(uint16_t srcPort) {
okini3939 2:6efb3541af61 539 _srcPort = srcPort;
okini3939 2:6efb3541af61 540 }
okini3939 2:6efb3541af61 541
okini3939 2:6efb3541af61 542 void IPv4TransmitRequest::setProtocol(uint8_t protocol) {
okini3939 2:6efb3541af61 543 _protocol = protocol;
okini3939 2:6efb3541af61 544 }
okini3939 2:6efb3541af61 545
okini3939 2:6efb3541af61 546 void IPv4TransmitRequest::setOption(uint8_t option) {
okini3939 2:6efb3541af61 547 _option = option;
okini3939 2:6efb3541af61 548 }
okini3939 2:6efb3541af61 549
okini3939 2:6efb3541af61 550
okini3939 2:6efb3541af61 551 Transmit_Status::Transmit_Status() : FrameIdResponse() {
okini3939 2:6efb3541af61 552
okini3939 2:6efb3541af61 553 }
okini3939 2:6efb3541af61 554
okini3939 2:6efb3541af61 555 uint8_t Transmit_Status::getStatus() {
okini3939 2:6efb3541af61 556 return getFrameData()[1];
okini3939 2:6efb3541af61 557 }
okini3939 2:6efb3541af61 558
okini3939 2:6efb3541af61 559 bool Transmit_Status::isSuccess() {
okini3939 2:6efb3541af61 560 return getStatus() == SUCCESS;
okini3939 2:6efb3541af61 561 }
okini3939 2:6efb3541af61 562
okini3939 2:6efb3541af61 563
okini3939 4:f6d73acc1f75 564 IPV4RxFrame::IPV4RxFrame () {
okini3939 4:f6d73acc1f75 565 }
okini3939 4:f6d73acc1f75 566
okini3939 2:6efb3541af61 567 IpAddr& IPV4RxFrame::getSrcAddress() {
okini3939 4:f6d73acc1f75 568 _srcAddr = IpAddr(getFrameData()[0], getFrameData()[1], getFrameData()[2], getFrameData()[3]);
okini3939 2:6efb3541af61 569 return _srcAddr;
okini3939 2:6efb3541af61 570 }
okini3939 2:6efb3541af61 571
okini3939 2:6efb3541af61 572 uint16_t IPV4RxFrame::getDstPort() {
okini3939 4:f6d73acc1f75 573 return (getFrameData()[4] << 8) + getFrameData()[5];
okini3939 2:6efb3541af61 574 }
okini3939 2:6efb3541af61 575
okini3939 2:6efb3541af61 576 uint16_t IPV4RxFrame::getSrcPort() {
okini3939 4:f6d73acc1f75 577 return (getFrameData()[6] << 8) + getFrameData()[7];
okini3939 2:6efb3541af61 578 }
okini3939 2:6efb3541af61 579
okini3939 2:6efb3541af61 580 uint8_t IPV4RxFrame::getProtocol() {
okini3939 4:f6d73acc1f75 581 return getFrameData()[8];
okini3939 2:6efb3541af61 582 }
okini3939 2:6efb3541af61 583
okini3939 2:6efb3541af61 584 uint8_t IPV4RxFrame::getStatus() {
okini3939 4:f6d73acc1f75 585 return getFrameData()[9];
okini3939 2:6efb3541af61 586 }
okini3939 2:6efb3541af61 587
okini3939 2:6efb3541af61 588 // markers to read data from packet array. this is the index, so the 12th item in the array
okini3939 2:6efb3541af61 589 uint8_t IPV4RxFrame::getDataOffset() {
okini3939 4:f6d73acc1f75 590 return 10;
okini3939 2:6efb3541af61 591 }
okini3939 2:6efb3541af61 592
okini3939 4:f6d73acc1f75 593 uint16_t IPV4RxFrame::getDataLength() {
okini3939 2:6efb3541af61 594 return getPacketLength() - getDataOffset() - 1;
okini3939 2:6efb3541af61 595 }