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:
Wed Dec 18 02:24:33 2013 +0000
Revision:
6:6f84e2840408
Parent:
5:547cfff7adf7
Child:
8:042c457e3282
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 3:8573b122fa84 14 //#define DEBUG
okini3939 3:8573b122fa84 15 #include "dbg.h"
okini3939 3:8573b122fa84 16
okini3939 2:6efb3541af61 17 #include "mbed.h"
okini3939 2:6efb3541af61 18 #include "XBee.h"
okini3939 2:6efb3541af61 19 #include "XBeeWiFi.h"
okini3939 2:6efb3541af61 20 #include "EthernetNetIf.h"
okini3939 2:6efb3541af61 21
okini3939 2:6efb3541af61 22 #define REVERSE_ENDIAN(x) (uint16_t)(((uint16_t)x >> 8) | ((uint16_t)x << 8))
okini3939 2:6efb3541af61 23
okini3939 4:f6d73acc1f75 24 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 25 }
okini3939 2:6efb3541af61 26
okini3939 2:6efb3541af61 27 int XBeeWiFi::setup (int security, const char *ssid, const char *pin) {
okini3939 3:8573b122fa84 28 int len, r;
okini3939 2:6efb3541af61 29 uint8_t cmd[2], val[32];
okini3939 2:6efb3541af61 30 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 31
okini3939 3:8573b122fa84 32 // SSID
okini3939 3:8573b122fa84 33 memcpy(cmd, "ID", 2);
okini3939 3:8573b122fa84 34 len = strlen(ssid);
okini3939 3:8573b122fa84 35 memcpy(val, ssid, len);
okini3939 3:8573b122fa84 36 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 37 atRequest.setCommandValue(val);
okini3939 3:8573b122fa84 38 atRequest.setCommandValueLength(len);
okini3939 3:8573b122fa84 39 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 40 send(atRequest);
okini3939 3:8573b122fa84 41 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 42 DBG("wifi ID: %d\r\n", r);
okini3939 3:8573b122fa84 43 if (r < 0) return -1;
okini3939 3:8573b122fa84 44
okini3939 3:8573b122fa84 45 if (security != SECURITY_OPEN) {
okini3939 3:8573b122fa84 46 // PIN
okini3939 3:8573b122fa84 47 memcpy(cmd, "PK", 2);
okini3939 3:8573b122fa84 48 len = strlen(pin);
okini3939 3:8573b122fa84 49 memcpy(val, pin, len);
okini3939 3:8573b122fa84 50 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 51 atRequest.setCommandValue(val);
okini3939 3:8573b122fa84 52 atRequest.setCommandValueLength(len);
okini3939 3:8573b122fa84 53 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 54 send(atRequest);
okini3939 3:8573b122fa84 55 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 56 DBG("wifi PK: %d\r\n", r);
okini3939 3:8573b122fa84 57 if (r < 0) return -1;
okini3939 3:8573b122fa84 58 }
okini3939 3:8573b122fa84 59
okini3939 2:6efb3541af61 60 // security type
okini3939 2:6efb3541af61 61 memcpy(cmd, "EE", 2);
okini3939 2:6efb3541af61 62 val[0] = security;
okini3939 2:6efb3541af61 63 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 64 atRequest.setCommandValue(val);
okini3939 2:6efb3541af61 65 atRequest.setCommandValueLength(1);
okini3939 3:8573b122fa84 66 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 67 send(atRequest);
okini3939 3:8573b122fa84 68 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 69 DBG("wifi EE: %d\r\n", r);
okini3939 3:8573b122fa84 70 if (r < 0) return -1;
okini3939 2:6efb3541af61 71
okini3939 2:6efb3541af61 72 return 0;
okini3939 2:6efb3541af61 73 }
okini3939 2:6efb3541af61 74
okini3939 2:6efb3541af61 75 int XBeeWiFi::setup (const char *ssid) {
okini3939 2:6efb3541af61 76 return setup(SECURITY_OPEN, ssid, NULL);
okini3939 2:6efb3541af61 77 }
okini3939 2:6efb3541af61 78
okini3939 2:6efb3541af61 79 int XBeeWiFi::reset () {
okini3939 2:6efb3541af61 80 // RESET
okini3939 3:8573b122fa84 81 uint8_t cmd[2] = {'N', 'R'}; // Network reset
okini3939 3:8573b122fa84 82 // uint8_t cmd[2] = {'F', 'R'}; // Software reset
okini3939 2:6efb3541af61 83 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 84
okini3939 2:6efb3541af61 85 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 86 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 87 send(atRequest);
okini3939 3:8573b122fa84 88 return getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 89 }
okini3939 2:6efb3541af61 90
okini3939 4:f6d73acc1f75 91 int XBeeWiFi::baud (int b) {
okini3939 4:f6d73acc1f75 92 // RESET
okini3939 4:f6d73acc1f75 93 uint8_t cmd[2] = {'B', 'D'};
okini3939 4:f6d73acc1f75 94 char val[4];
okini3939 4:f6d73acc1f75 95 AtCommandRequest atRequest;
okini3939 4:f6d73acc1f75 96 int r, len;
okini3939 4:f6d73acc1f75 97
okini3939 4:f6d73acc1f75 98 if (b < 0x100) {
okini3939 4:f6d73acc1f75 99 val[0] = b;
okini3939 4:f6d73acc1f75 100 len = 1;
okini3939 4:f6d73acc1f75 101 } else {
okini3939 4:f6d73acc1f75 102 val[0] = (b >> 24) & 0xff;
okini3939 4:f6d73acc1f75 103 val[1] = (b >> 16) & 0xff;
okini3939 4:f6d73acc1f75 104 val[2] = (b >> 8) & 0xff;
okini3939 4:f6d73acc1f75 105 val[3] = b & 0xff;
okini3939 4:f6d73acc1f75 106 len = 4;
okini3939 4:f6d73acc1f75 107 }
okini3939 4:f6d73acc1f75 108 atRequest.setCommand(cmd);
okini3939 4:f6d73acc1f75 109 atRequest.setCommandValue((uint8_t*)val);
okini3939 4:f6d73acc1f75 110 atRequest.setCommandValueLength(len);
okini3939 4:f6d73acc1f75 111 atRequest.setFrameId(getNextFrameId());
okini3939 4:f6d73acc1f75 112 send(atRequest);
okini3939 4:f6d73acc1f75 113 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 4:f6d73acc1f75 114
okini3939 4:f6d73acc1f75 115 if (r == 0) {
okini3939 4:f6d73acc1f75 116 begin(b);
okini3939 4:f6d73acc1f75 117 }
okini3939 4:f6d73acc1f75 118 return r;
okini3939 4:f6d73acc1f75 119 }
okini3939 4:f6d73acc1f75 120
okini3939 2:6efb3541af61 121 int XBeeWiFi::setAddress () {
okini3939 2:6efb3541af61 122 // DHCP
okini3939 2:6efb3541af61 123 uint8_t cmd[2] = {'M', 'A'};
okini3939 2:6efb3541af61 124 char val[1] = {0};
okini3939 2:6efb3541af61 125 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 126
okini3939 5:547cfff7adf7 127 _nameserver = IpAddr(0,0,0,0);
okini3939 5:547cfff7adf7 128 _nameport = DNS_PORT;
okini3939 5:547cfff7adf7 129
okini3939 2:6efb3541af61 130 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 131 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 132 atRequest.setCommandValueLength(1);
okini3939 3:8573b122fa84 133 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 134 send(atRequest);
okini3939 2:6efb3541af61 135
okini3939 3:8573b122fa84 136 return getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 137 }
okini3939 2:6efb3541af61 138
okini3939 2:6efb3541af61 139 int XBeeWiFi::setAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver) {
okini3939 2:6efb3541af61 140 uint8_t cmd[2];
okini3939 2:6efb3541af61 141 char val[32];
okini3939 2:6efb3541af61 142 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 143
okini3939 2:6efb3541af61 144 // Static
okini3939 2:6efb3541af61 145 memcpy(cmd, "MA", 2);
okini3939 2:6efb3541af61 146 val[0] = 1;
okini3939 2:6efb3541af61 147 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 148 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 149 atRequest.setCommandValueLength(1);
okini3939 3:8573b122fa84 150 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 151 send(atRequest);
okini3939 3:8573b122fa84 152 getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 153
okini3939 2:6efb3541af61 154 // IP address
okini3939 2:6efb3541af61 155 memcpy(cmd, "MY", 2);
okini3939 2:6efb3541af61 156 sprintf(val, "%d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
okini3939 2:6efb3541af61 157 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 158 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 159 atRequest.setCommandValueLength(strlen(val));
okini3939 3:8573b122fa84 160 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 161 send(atRequest);
okini3939 3:8573b122fa84 162 getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 163
okini3939 2:6efb3541af61 164 // sub netmask
okini3939 2:6efb3541af61 165 memcpy(cmd, "NK", 2);
okini3939 2:6efb3541af61 166 sprintf(val, "%d.%d.%d.%d", netmask[0], netmask[1], netmask[2], netmask[3]);
okini3939 2:6efb3541af61 167 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 168 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 169 atRequest.setCommandValueLength(strlen(val));
okini3939 3:8573b122fa84 170 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 171 send(atRequest);
okini3939 3:8573b122fa84 172 getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 173
okini3939 2:6efb3541af61 174 // default gateway
okini3939 2:6efb3541af61 175 memcpy(cmd, "GW", 2);
okini3939 2:6efb3541af61 176 sprintf(val, "%d.%d.%d.%d", gateway[0], gateway[1], gateway[2], gateway[3]);
okini3939 2:6efb3541af61 177 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 178 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 179 atRequest.setCommandValueLength(strlen(val));
okini3939 3:8573b122fa84 180 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 181 send(atRequest);
okini3939 3:8573b122fa84 182 getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 183
okini3939 2:6efb3541af61 184 // name server
okini3939 2:6efb3541af61 185 _nameserver = nameserver;
okini3939 5:547cfff7adf7 186 _nameport = DNS_PORT;
okini3939 2:6efb3541af61 187
okini3939 2:6efb3541af61 188 return 0;
okini3939 2:6efb3541af61 189 }
okini3939 2:6efb3541af61 190
okini3939 3:8573b122fa84 191 int XBeeWiFi::getAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver) {
okini3939 3:8573b122fa84 192 int r;
okini3939 3:8573b122fa84 193 uint8_t cmd[2];
okini3939 3:8573b122fa84 194 AtCommandRequest atRequest;
okini3939 3:8573b122fa84 195 AtCommandResponse atResponse;
okini3939 3:8573b122fa84 196
okini3939 3:8573b122fa84 197 memcpy(cmd, "MY", 2);
okini3939 3:8573b122fa84 198 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 199 atRequest.clearCommandValue();
okini3939 3:8573b122fa84 200 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 201 send(atRequest);
okini3939 3:8573b122fa84 202 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 203 DBG("wifi MY: %d\r\n", r);
okini3939 3:8573b122fa84 204 if (r >= 0) {
okini3939 3:8573b122fa84 205 r = getWiAddr(ipaddr);
okini3939 3:8573b122fa84 206 }
okini3939 3:8573b122fa84 207
okini3939 3:8573b122fa84 208 memcpy(cmd, "MK", 2);
okini3939 3:8573b122fa84 209 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 210 atRequest.clearCommandValue();
okini3939 3:8573b122fa84 211 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 212 send(atRequest);
okini3939 3:8573b122fa84 213 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 214 DBG("wifi MK: %d\r\n", r);
okini3939 3:8573b122fa84 215 if (r >= 0) {
okini3939 3:8573b122fa84 216 r = getWiAddr(netmask);
okini3939 3:8573b122fa84 217 }
okini3939 3:8573b122fa84 218
okini3939 3:8573b122fa84 219 memcpy(cmd, "GW", 2);
okini3939 3:8573b122fa84 220 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 221 atRequest.clearCommandValue();
okini3939 3:8573b122fa84 222 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 223 send(atRequest);
okini3939 3:8573b122fa84 224 r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 3:8573b122fa84 225 DBG("wifi GW: %d\r\n", r);
okini3939 3:8573b122fa84 226 if (r >= 0) {
okini3939 3:8573b122fa84 227 r = getWiAddr(gateway);
okini3939 3:8573b122fa84 228 }
okini3939 3:8573b122fa84 229
okini3939 3:8573b122fa84 230 nameserver = _nameserver;
okini3939 3:8573b122fa84 231
okini3939 3:8573b122fa84 232 return 0;
okini3939 3:8573b122fa84 233 }
okini3939 3:8573b122fa84 234
okini3939 3:8573b122fa84 235 int XBeeWiFi::getWiAddr (IpAddr &ipaddr) {
okini3939 3:8573b122fa84 236 int ip1, ip2, ip3, ip4;
okini3939 3:8573b122fa84 237 AtCommandResponse atResponse;
okini3939 3:8573b122fa84 238
okini3939 3:8573b122fa84 239 getResponse().getAtCommandResponse(atResponse);
okini3939 3:8573b122fa84 240 if (atResponse.isOk() && atResponse.getValueLength() >= 7) {
okini3939 3:8573b122fa84 241 sscanf((char*)atResponse.getValue(), "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
okini3939 3:8573b122fa84 242 ipaddr = IpAddr(ip1, ip2, ip3, ip4);
okini3939 3:8573b122fa84 243 return 0;
okini3939 3:8573b122fa84 244 }
okini3939 3:8573b122fa84 245 return -1;
okini3939 3:8573b122fa84 246 }
okini3939 3:8573b122fa84 247
okini3939 2:6efb3541af61 248 int XBeeWiFi::setTimeout (int timeout) {
okini3939 2:6efb3541af61 249 // timeout
okini3939 2:6efb3541af61 250 uint8_t cmd[2] = {'T', 'P'};
okini3939 2:6efb3541af61 251 char val[1];
okini3939 2:6efb3541af61 252 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 253
okini3939 2:6efb3541af61 254 val[0] = timeout;
okini3939 2:6efb3541af61 255 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 256 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 257 atRequest.setCommandValueLength(1);
okini3939 3:8573b122fa84 258 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 259 send(atRequest);
okini3939 2:6efb3541af61 260
okini3939 2:6efb3541af61 261 return getWiResponse(AT_COMMAND_RESPONSE);
okini3939 2:6efb3541af61 262 }
okini3939 2:6efb3541af61 263
okini3939 2:6efb3541af61 264 int XBeeWiFi::getStatus () {
okini3939 2:6efb3541af61 265 // AI
okini3939 2:6efb3541af61 266 uint8_t cmd[2] = {'A', 'I'};
okini3939 2:6efb3541af61 267 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 268
okini3939 2:6efb3541af61 269 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 270 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 271 send(atRequest);
okini3939 2:6efb3541af61 272
okini3939 3:8573b122fa84 273 return getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 274 }
okini3939 2:6efb3541af61 275
okini3939 3:8573b122fa84 276 int XBeeWiFi::getWiResponse (int apiId, int frameid, int timeout) {
okini3939 2:6efb3541af61 277 AtCommandResponse atResponse;
okini3939 2:6efb3541af61 278
okini3939 2:6efb3541af61 279 if (readPacket(timeout)) {
okini3939 3:8573b122fa84 280 if (frameid && frameid != getResponse().getFrameData()[0]) {
okini3939 3:8573b122fa84 281 DBG("Expected AT response but got %x (frame %d)\r\n", getResponse().getApiId(), getResponse().getFrameData()[0]);
okini3939 3:8573b122fa84 282 if (! readPacket(timeout)) return -1;
okini3939 3:8573b122fa84 283 DBG("Retry ok\r\n");
okini3939 3:8573b122fa84 284 }
okini3939 3:8573b122fa84 285
okini3939 2:6efb3541af61 286 if (getResponse().getApiId() == apiId) {
okini3939 2:6efb3541af61 287 getResponse().getAtCommandResponse(atResponse);
okini3939 2:6efb3541af61 288
okini3939 3:8573b122fa84 289 if (getResponse().getApiId() == IPv4_RX_FRAME) {
okini3939 3:8573b122fa84 290 return 0;
okini3939 3:8573b122fa84 291 } else
okini3939 2:6efb3541af61 292 if (atResponse.isOk()) {
okini3939 2:6efb3541af61 293
okini3939 3:8573b122fa84 294 // if (getResponse().getFrameDataLength() > 4) {
okini3939 2:6efb3541af61 295 return atResponse.getValue()[0];
okini3939 2:6efb3541af61 296
okini3939 2:6efb3541af61 297 } else {
okini3939 3:8573b122fa84 298 DBG("Command return error code: %x\r\n", atResponse.getStatus());
okini3939 2:6efb3541af61 299 }
okini3939 2:6efb3541af61 300 } else {
okini3939 3:8573b122fa84 301 DBG("Expected AT response but got %x\r\n", getResponse().getApiId());
okini3939 2:6efb3541af61 302 }
okini3939 2:6efb3541af61 303 } else {
okini3939 2:6efb3541af61 304 if (getResponse().isError()) {
okini3939 3:8573b122fa84 305 DBG("Error reading packet. Error code: %x\r\n", getResponse().getErrorCode());
okini3939 2:6efb3541af61 306 } else {
okini3939 3:8573b122fa84 307 DBG("No response from radio\r\n");
okini3939 2:6efb3541af61 308 }
okini3939 2:6efb3541af61 309 }
okini3939 2:6efb3541af61 310
okini3939 2:6efb3541af61 311 return -1;
okini3939 2:6efb3541af61 312 }
okini3939 2:6efb3541af61 313
okini3939 2:6efb3541af61 314 #ifdef USE_WIFIDNS
okini3939 5:547cfff7adf7 315 int XBeeWiFi::setNameserver (IpAddr &nameserver, int port = DNS_PORT) {
okini3939 3:8573b122fa84 316 _nameserver = nameserver;
okini3939 5:547cfff7adf7 317 _nameport = port;
okini3939 3:8573b122fa84 318 return 0;
okini3939 3:8573b122fa84 319 }
okini3939 3:8573b122fa84 320
okini3939 4:f6d73acc1f75 321 // *** Note: wifi is turned off when XBee send the port 53 udp packet.
okini3939 2:6efb3541af61 322 int XBeeWiFi::getHostByName (const char* name, IpAddr &addr) {
okini3939 4:f6d73acc1f75 323 Timer timeout;
okini3939 4:f6d73acc1f75 324 char buf[600];
okini3939 4:f6d73acc1f75 325 int r, len;
okini3939 3:8573b122fa84 326 uint8_t cmd[2] = {'C', '0'};
okini3939 3:8573b122fa84 327 char val[2];
okini3939 2:6efb3541af61 328 IPv4TransmitRequest dnsRequest;
okini3939 2:6efb3541af61 329 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 330 AtCommandResponse atResponse;
okini3939 2:6efb3541af61 331
okini3939 2:6efb3541af61 332 if (!strcmp(name, "localhost")) {
okini3939 2:6efb3541af61 333 addr = IpAddr(127, 0, 0, 1);
okini3939 2:6efb3541af61 334 return 0;
okini3939 2:6efb3541af61 335 }
okini3939 5:547cfff7adf7 336
okini3939 3:8573b122fa84 337 // bind src port
okini3939 3:8573b122fa84 338 val[0] = (DNS_SRC_PORT >> 8) & 0xff;
okini3939 3:8573b122fa84 339 val[1] = DNS_SRC_PORT & 0xff;
okini3939 3:8573b122fa84 340 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 341 atRequest.setCommandValue((uint8_t*)val);
okini3939 3:8573b122fa84 342 atRequest.setCommandValueLength(2);
okini3939 3:8573b122fa84 343 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 344 send(atRequest);
okini3939 3:8573b122fa84 345 r = getWiResponse(AT_COMMAND_RESPONSE);
okini3939 3:8573b122fa84 346 DBG("wifi C0: %d\r\n", r);
okini3939 5:547cfff7adf7 347
okini3939 2:6efb3541af61 348 // send DNS request
okini3939 3:8573b122fa84 349 len = createDnsRequest(name, buf);
okini3939 2:6efb3541af61 350 dnsRequest.setAddress(_nameserver);
okini3939 5:547cfff7adf7 351 dnsRequest.setDstPort(_nameport);
okini3939 3:8573b122fa84 352 dnsRequest.setSrcPort(DNS_SRC_PORT);
okini3939 2:6efb3541af61 353 dnsRequest.setProtocol(PROTOCOL_UDP);
okini3939 2:6efb3541af61 354 dnsRequest.setPayload((uint8_t*)buf);
okini3939 2:6efb3541af61 355 dnsRequest.setPayloadLength(len);
okini3939 4:f6d73acc1f75 356
okini3939 4:f6d73acc1f75 357 // wait responce
okini3939 4:f6d73acc1f75 358 timeout.reset();
okini3939 4:f6d73acc1f75 359 timeout.start();
okini3939 4:f6d73acc1f75 360 while (timeout.read_ms() < DNS_TIMEOUT) {
okini3939 3:8573b122fa84 361 dnsRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 362 send(dnsRequest);
okini3939 3:8573b122fa84 363
okini3939 3:8573b122fa84 364 r = getWiResponse(TX_STATUS_RESPONSE, dnsRequest.getFrameId());
okini3939 3:8573b122fa84 365 DBG("wifi TX: %d\r\n", r);
okini3939 2:6efb3541af61 366
okini3939 4:f6d73acc1f75 367 if (r >= 0) {
okini3939 3:8573b122fa84 368 // recv DNS request
okini3939 3:8573b122fa84 369 r = getWiResponse(IPv4_RX_FRAME, 0, 3000);
okini3939 3:8573b122fa84 370 DBG("wifi RX: %d\r\n", r);
okini3939 3:8573b122fa84 371 if (r >= 0) {
okini3939 4:f6d73acc1f75 372 timeout.stop();
okini3939 3:8573b122fa84 373 getResponse().getAtCommandResponse(atResponse);
okini3939 3:8573b122fa84 374 return getDnsResponse(atResponse.getValue() + 6, atResponse.getValueLength() - 6, addr);
okini3939 2:6efb3541af61 375 }
okini3939 3:8573b122fa84 376 } else {
okini3939 3:8573b122fa84 377 break;
okini3939 2:6efb3541af61 378 }
okini3939 2:6efb3541af61 379 }
okini3939 4:f6d73acc1f75 380 timeout.stop();
okini3939 2:6efb3541af61 381
okini3939 2:6efb3541af61 382 return -1;
okini3939 2:6efb3541af61 383 }
okini3939 2:6efb3541af61 384
okini3939 2:6efb3541af61 385 int XBeeWiFi::createDnsRequest (const char* name, char *buf) {
okini3939 2:6efb3541af61 386 struct DNSHeader *dnsHeader;
okini3939 2:6efb3541af61 387 struct DnsQuestionEnd *dnsEnd;
okini3939 2:6efb3541af61 388 int len, num;
okini3939 2:6efb3541af61 389
okini3939 2:6efb3541af61 390 // DNS header
okini3939 2:6efb3541af61 391 dnsHeader = (DNSHeader*)buf;
okini3939 2:6efb3541af61 392 dnsHeader->id = REVERSE_ENDIAN(0xdead);
okini3939 2:6efb3541af61 393 dnsHeader->flags = REVERSE_ENDIAN(0x100);
okini3939 2:6efb3541af61 394 dnsHeader->questions = REVERSE_ENDIAN(1);
okini3939 2:6efb3541af61 395 dnsHeader->answers = 0;
okini3939 2:6efb3541af61 396 dnsHeader->authorities = 0;
okini3939 2:6efb3541af61 397 dnsHeader->additional = 0;
okini3939 2:6efb3541af61 398
okini3939 2:6efb3541af61 399 // DNS question
okini3939 3:8573b122fa84 400 len = sizeof(DNSHeader);
okini3939 3:8573b122fa84 401 while ((num = (int)strchr(name, '.')) != NULL) {
okini3939 2:6efb3541af61 402 num = num - (int)name;
okini3939 2:6efb3541af61 403 buf[len] = num;
okini3939 2:6efb3541af61 404 len ++;
okini3939 2:6efb3541af61 405 strncpy(&buf[len], name, num);
okini3939 3:8573b122fa84 406 name = name + num + 1;
okini3939 2:6efb3541af61 407 len = len + num;
okini3939 2:6efb3541af61 408 }
okini3939 2:6efb3541af61 409
okini3939 3:8573b122fa84 410 if ((num = strlen(name)) != NULL) {
okini3939 2:6efb3541af61 411 buf[len] = num;
okini3939 2:6efb3541af61 412 len ++;
okini3939 2:6efb3541af61 413 strncpy(&buf[len], name, num);
okini3939 3:8573b122fa84 414 len = len + num;
okini3939 2:6efb3541af61 415 }
okini3939 3:8573b122fa84 416 buf[len] = 0;
okini3939 3:8573b122fa84 417 len ++;
okini3939 2:6efb3541af61 418
okini3939 2:6efb3541af61 419 dnsEnd = (DnsQuestionEnd*)&buf[len];
okini3939 2:6efb3541af61 420 dnsEnd->type = REVERSE_ENDIAN(DNS_QUERY_A);
okini3939 2:6efb3541af61 421 dnsEnd->clas = REVERSE_ENDIAN(DNS_CLASS_IN);
okini3939 2:6efb3541af61 422
okini3939 3:8573b122fa84 423 return len + sizeof(DnsQuestionEnd);
okini3939 2:6efb3541af61 424 }
okini3939 2:6efb3541af61 425
okini3939 2:6efb3541af61 426 int XBeeWiFi::getDnsResponse (const uint8_t *buf, int len, IpAddr &addr) {
okini3939 2:6efb3541af61 427 int i;
okini3939 2:6efb3541af61 428 struct DNSHeader *dnsHeader;
okini3939 2:6efb3541af61 429 struct DnsAnswer *dnsAnswer;
okini3939 2:6efb3541af61 430
okini3939 2:6efb3541af61 431 // DNS header
okini3939 2:6efb3541af61 432 dnsHeader = (DNSHeader*)buf;
okini3939 2:6efb3541af61 433 if (REVERSE_ENDIAN(dnsHeader->id) != 0xdead || (REVERSE_ENDIAN(dnsHeader->flags) & 0x800f) != 0x8000) {
okini3939 2:6efb3541af61 434 return -1;
okini3939 2:6efb3541af61 435 }
okini3939 2:6efb3541af61 436
okini3939 2:6efb3541af61 437 // skip question
okini3939 3:8573b122fa84 438 for (i = sizeof(DNSHeader); buf[i] && i < len; i ++);
okini3939 2:6efb3541af61 439 i = i + 1 + sizeof(DnsQuestionEnd);
okini3939 2:6efb3541af61 440
okini3939 2:6efb3541af61 441 // DNS answer
okini3939 2:6efb3541af61 442 while (i < len) {
okini3939 2:6efb3541af61 443 dnsAnswer = (DnsAnswer*)&buf[i];
okini3939 2:6efb3541af61 444 if (dnsAnswer->clas != REVERSE_ENDIAN(DNS_CLASS_IN)) {
okini3939 2:6efb3541af61 445 return -1;
okini3939 2:6efb3541af61 446 }
okini3939 2:6efb3541af61 447
okini3939 3:8573b122fa84 448 i = i + sizeof(DnsAnswer);
okini3939 2:6efb3541af61 449 if (dnsAnswer->type == REVERSE_ENDIAN(DNS_QUERY_A)) {
okini3939 2:6efb3541af61 450 addr = IpAddr(buf[i], buf[i + 1], buf[i + 2], buf[i + 3]);
okini3939 2:6efb3541af61 451 return 0;
okini3939 2:6efb3541af61 452 }
okini3939 2:6efb3541af61 453 i = i + dnsAnswer->length;
okini3939 2:6efb3541af61 454 }
okini3939 2:6efb3541af61 455
okini3939 2:6efb3541af61 456 return -1;
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 }
okini3939 6:6f84e2840408 596
okini3939 6:6f84e2840408 597 #endif
okini3939 6:6f84e2840408 598