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:01:52 2012 +0000
Revision:
4:f6d73acc1f75
Parent:
3:8573b122fa84
Child:
5:547cfff7adf7

        

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