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

Dependents:   XBee_WiFi_EA_LPC4088

Fork of XBee by Suga koubou

Committer:
marin8703
Date:
Tue Feb 10 21:16:07 2015 +0000
Revision:
8:042c457e3282
Parent:
6:6f84e2840408
Adapting to LPC4088

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
marin8703 8:042c457e3282 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);
marin8703 8:042c457e3282 240 DBG("**in getWiAddr after getResponse()**\r\n");
marin8703 8:042c457e3282 241
marin8703 8:042c457e3282 242 DBG("**isOK: %d **\r\n", atResponse.isOk() );
marin8703 8:042c457e3282 243 DBG("**Response length: %d **\r\n", atResponse.getValueLength() );
marin8703 8:042c457e3282 244 DBG("**Response: %s **\r\n", atResponse.getValue() );
marin8703 8:042c457e3282 245
okini3939 3:8573b122fa84 246 if (atResponse.isOk() && atResponse.getValueLength() >= 7) {
marin8703 8:042c457e3282 247 DBG("**Inside getWiAddr if**\r\n");
okini3939 3:8573b122fa84 248 sscanf((char*)atResponse.getValue(), "%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
marin8703 8:042c457e3282 249 DBG("**ip(getWiAddr): %d.%d.%d.%d **\r\n", ip1,ip2,ip3,ip4);
okini3939 3:8573b122fa84 250 ipaddr = IpAddr(ip1, ip2, ip3, ip4);
okini3939 3:8573b122fa84 251 return 0;
okini3939 3:8573b122fa84 252 }
okini3939 3:8573b122fa84 253 return -1;
okini3939 3:8573b122fa84 254 }
okini3939 3:8573b122fa84 255
okini3939 2:6efb3541af61 256 int XBeeWiFi::setTimeout (int timeout) {
okini3939 2:6efb3541af61 257 // timeout
okini3939 2:6efb3541af61 258 uint8_t cmd[2] = {'T', 'P'};
okini3939 2:6efb3541af61 259 char val[1];
okini3939 2:6efb3541af61 260 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 261
okini3939 2:6efb3541af61 262 val[0] = timeout;
okini3939 2:6efb3541af61 263 atRequest.setCommand(cmd);
okini3939 2:6efb3541af61 264 atRequest.setCommandValue((uint8_t*)val);
okini3939 2:6efb3541af61 265 atRequest.setCommandValueLength(1);
okini3939 3:8573b122fa84 266 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 267 send(atRequest);
okini3939 2:6efb3541af61 268
okini3939 2:6efb3541af61 269 return getWiResponse(AT_COMMAND_RESPONSE);
okini3939 2:6efb3541af61 270 }
okini3939 2:6efb3541af61 271
okini3939 2:6efb3541af61 272 int XBeeWiFi::getStatus () {
okini3939 2:6efb3541af61 273 // AI
okini3939 2:6efb3541af61 274 uint8_t cmd[2] = {'A', 'I'};
okini3939 2:6efb3541af61 275 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 276
okini3939 2:6efb3541af61 277 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 278 atRequest.setFrameId(getNextFrameId());
okini3939 2:6efb3541af61 279 send(atRequest);
okini3939 2:6efb3541af61 280
okini3939 3:8573b122fa84 281 return getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId());
okini3939 2:6efb3541af61 282 }
okini3939 2:6efb3541af61 283
okini3939 3:8573b122fa84 284 int XBeeWiFi::getWiResponse (int apiId, int frameid, int timeout) {
okini3939 2:6efb3541af61 285 AtCommandResponse atResponse;
okini3939 2:6efb3541af61 286
okini3939 2:6efb3541af61 287 if (readPacket(timeout)) {
okini3939 3:8573b122fa84 288 if (frameid && frameid != getResponse().getFrameData()[0]) {
okini3939 3:8573b122fa84 289 DBG("Expected AT response but got %x (frame %d)\r\n", getResponse().getApiId(), getResponse().getFrameData()[0]);
okini3939 3:8573b122fa84 290 if (! readPacket(timeout)) return -1;
okini3939 3:8573b122fa84 291 DBG("Retry ok\r\n");
okini3939 3:8573b122fa84 292 }
okini3939 3:8573b122fa84 293
okini3939 2:6efb3541af61 294 if (getResponse().getApiId() == apiId) {
okini3939 2:6efb3541af61 295 getResponse().getAtCommandResponse(atResponse);
okini3939 2:6efb3541af61 296
okini3939 3:8573b122fa84 297 if (getResponse().getApiId() == IPv4_RX_FRAME) {
okini3939 3:8573b122fa84 298 return 0;
okini3939 3:8573b122fa84 299 } else
okini3939 2:6efb3541af61 300 if (atResponse.isOk()) {
okini3939 2:6efb3541af61 301
okini3939 3:8573b122fa84 302 // if (getResponse().getFrameDataLength() > 4) {
okini3939 2:6efb3541af61 303 return atResponse.getValue()[0];
okini3939 2:6efb3541af61 304
okini3939 2:6efb3541af61 305 } else {
okini3939 3:8573b122fa84 306 DBG("Command return error code: %x\r\n", atResponse.getStatus());
okini3939 2:6efb3541af61 307 }
okini3939 2:6efb3541af61 308 } else {
okini3939 3:8573b122fa84 309 DBG("Expected AT response but got %x\r\n", getResponse().getApiId());
okini3939 2:6efb3541af61 310 }
okini3939 2:6efb3541af61 311 } else {
okini3939 2:6efb3541af61 312 if (getResponse().isError()) {
okini3939 3:8573b122fa84 313 DBG("Error reading packet. Error code: %x\r\n", getResponse().getErrorCode());
okini3939 2:6efb3541af61 314 } else {
okini3939 3:8573b122fa84 315 DBG("No response from radio\r\n");
okini3939 2:6efb3541af61 316 }
okini3939 2:6efb3541af61 317 }
okini3939 2:6efb3541af61 318
okini3939 2:6efb3541af61 319 return -1;
okini3939 2:6efb3541af61 320 }
okini3939 2:6efb3541af61 321
okini3939 2:6efb3541af61 322 #ifdef USE_WIFIDNS
okini3939 5:547cfff7adf7 323 int XBeeWiFi::setNameserver (IpAddr &nameserver, int port = DNS_PORT) {
okini3939 3:8573b122fa84 324 _nameserver = nameserver;
okini3939 5:547cfff7adf7 325 _nameport = port;
okini3939 3:8573b122fa84 326 return 0;
okini3939 3:8573b122fa84 327 }
okini3939 3:8573b122fa84 328
okini3939 4:f6d73acc1f75 329 // *** Note: wifi is turned off when XBee send the port 53 udp packet.
okini3939 2:6efb3541af61 330 int XBeeWiFi::getHostByName (const char* name, IpAddr &addr) {
okini3939 4:f6d73acc1f75 331 Timer timeout;
okini3939 4:f6d73acc1f75 332 char buf[600];
okini3939 4:f6d73acc1f75 333 int r, len;
okini3939 3:8573b122fa84 334 uint8_t cmd[2] = {'C', '0'};
okini3939 3:8573b122fa84 335 char val[2];
okini3939 2:6efb3541af61 336 IPv4TransmitRequest dnsRequest;
okini3939 2:6efb3541af61 337 AtCommandRequest atRequest;
okini3939 2:6efb3541af61 338 AtCommandResponse atResponse;
okini3939 2:6efb3541af61 339
okini3939 2:6efb3541af61 340 if (!strcmp(name, "localhost")) {
okini3939 2:6efb3541af61 341 addr = IpAddr(127, 0, 0, 1);
okini3939 2:6efb3541af61 342 return 0;
okini3939 2:6efb3541af61 343 }
okini3939 5:547cfff7adf7 344
okini3939 3:8573b122fa84 345 // bind src port
okini3939 3:8573b122fa84 346 val[0] = (DNS_SRC_PORT >> 8) & 0xff;
okini3939 3:8573b122fa84 347 val[1] = DNS_SRC_PORT & 0xff;
okini3939 3:8573b122fa84 348 atRequest.setCommand(cmd);
okini3939 3:8573b122fa84 349 atRequest.setCommandValue((uint8_t*)val);
okini3939 3:8573b122fa84 350 atRequest.setCommandValueLength(2);
okini3939 3:8573b122fa84 351 atRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 352 send(atRequest);
okini3939 3:8573b122fa84 353 r = getWiResponse(AT_COMMAND_RESPONSE);
okini3939 3:8573b122fa84 354 DBG("wifi C0: %d\r\n", r);
okini3939 5:547cfff7adf7 355
okini3939 2:6efb3541af61 356 // send DNS request
okini3939 3:8573b122fa84 357 len = createDnsRequest(name, buf);
okini3939 2:6efb3541af61 358 dnsRequest.setAddress(_nameserver);
okini3939 5:547cfff7adf7 359 dnsRequest.setDstPort(_nameport);
okini3939 3:8573b122fa84 360 dnsRequest.setSrcPort(DNS_SRC_PORT);
okini3939 2:6efb3541af61 361 dnsRequest.setProtocol(PROTOCOL_UDP);
okini3939 2:6efb3541af61 362 dnsRequest.setPayload((uint8_t*)buf);
okini3939 2:6efb3541af61 363 dnsRequest.setPayloadLength(len);
okini3939 4:f6d73acc1f75 364
okini3939 4:f6d73acc1f75 365 // wait responce
okini3939 4:f6d73acc1f75 366 timeout.reset();
okini3939 4:f6d73acc1f75 367 timeout.start();
okini3939 4:f6d73acc1f75 368 while (timeout.read_ms() < DNS_TIMEOUT) {
okini3939 3:8573b122fa84 369 dnsRequest.setFrameId(getNextFrameId());
okini3939 3:8573b122fa84 370 send(dnsRequest);
okini3939 3:8573b122fa84 371
okini3939 3:8573b122fa84 372 r = getWiResponse(TX_STATUS_RESPONSE, dnsRequest.getFrameId());
okini3939 3:8573b122fa84 373 DBG("wifi TX: %d\r\n", r);
okini3939 2:6efb3541af61 374
okini3939 4:f6d73acc1f75 375 if (r >= 0) {
okini3939 3:8573b122fa84 376 // recv DNS request
okini3939 3:8573b122fa84 377 r = getWiResponse(IPv4_RX_FRAME, 0, 3000);
okini3939 3:8573b122fa84 378 DBG("wifi RX: %d\r\n", r);
okini3939 3:8573b122fa84 379 if (r >= 0) {
okini3939 4:f6d73acc1f75 380 timeout.stop();
okini3939 3:8573b122fa84 381 getResponse().getAtCommandResponse(atResponse);
okini3939 3:8573b122fa84 382 return getDnsResponse(atResponse.getValue() + 6, atResponse.getValueLength() - 6, addr);
okini3939 2:6efb3541af61 383 }
okini3939 3:8573b122fa84 384 } else {
okini3939 3:8573b122fa84 385 break;
okini3939 2:6efb3541af61 386 }
okini3939 2:6efb3541af61 387 }
okini3939 4:f6d73acc1f75 388 timeout.stop();
okini3939 2:6efb3541af61 389
okini3939 2:6efb3541af61 390 return -1;
okini3939 2:6efb3541af61 391 }
okini3939 2:6efb3541af61 392
okini3939 2:6efb3541af61 393 int XBeeWiFi::createDnsRequest (const char* name, char *buf) {
okini3939 2:6efb3541af61 394 struct DNSHeader *dnsHeader;
okini3939 2:6efb3541af61 395 struct DnsQuestionEnd *dnsEnd;
okini3939 2:6efb3541af61 396 int len, num;
okini3939 2:6efb3541af61 397
okini3939 2:6efb3541af61 398 // DNS header
okini3939 2:6efb3541af61 399 dnsHeader = (DNSHeader*)buf;
okini3939 2:6efb3541af61 400 dnsHeader->id = REVERSE_ENDIAN(0xdead);
okini3939 2:6efb3541af61 401 dnsHeader->flags = REVERSE_ENDIAN(0x100);
okini3939 2:6efb3541af61 402 dnsHeader->questions = REVERSE_ENDIAN(1);
okini3939 2:6efb3541af61 403 dnsHeader->answers = 0;
okini3939 2:6efb3541af61 404 dnsHeader->authorities = 0;
okini3939 2:6efb3541af61 405 dnsHeader->additional = 0;
okini3939 2:6efb3541af61 406
okini3939 2:6efb3541af61 407 // DNS question
okini3939 3:8573b122fa84 408 len = sizeof(DNSHeader);
okini3939 3:8573b122fa84 409 while ((num = (int)strchr(name, '.')) != NULL) {
okini3939 2:6efb3541af61 410 num = num - (int)name;
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 name = name + num + 1;
okini3939 2:6efb3541af61 415 len = len + num;
okini3939 2:6efb3541af61 416 }
okini3939 2:6efb3541af61 417
okini3939 3:8573b122fa84 418 if ((num = strlen(name)) != NULL) {
okini3939 2:6efb3541af61 419 buf[len] = num;
okini3939 2:6efb3541af61 420 len ++;
okini3939 2:6efb3541af61 421 strncpy(&buf[len], name, num);
okini3939 3:8573b122fa84 422 len = len + num;
okini3939 2:6efb3541af61 423 }
okini3939 3:8573b122fa84 424 buf[len] = 0;
okini3939 3:8573b122fa84 425 len ++;
okini3939 2:6efb3541af61 426
okini3939 2:6efb3541af61 427 dnsEnd = (DnsQuestionEnd*)&buf[len];
okini3939 2:6efb3541af61 428 dnsEnd->type = REVERSE_ENDIAN(DNS_QUERY_A);
okini3939 2:6efb3541af61 429 dnsEnd->clas = REVERSE_ENDIAN(DNS_CLASS_IN);
okini3939 2:6efb3541af61 430
okini3939 3:8573b122fa84 431 return len + sizeof(DnsQuestionEnd);
okini3939 2:6efb3541af61 432 }
okini3939 2:6efb3541af61 433
okini3939 2:6efb3541af61 434 int XBeeWiFi::getDnsResponse (const uint8_t *buf, int len, IpAddr &addr) {
okini3939 2:6efb3541af61 435 int i;
okini3939 2:6efb3541af61 436 struct DNSHeader *dnsHeader;
okini3939 2:6efb3541af61 437 struct DnsAnswer *dnsAnswer;
okini3939 2:6efb3541af61 438
okini3939 2:6efb3541af61 439 // DNS header
okini3939 2:6efb3541af61 440 dnsHeader = (DNSHeader*)buf;
okini3939 2:6efb3541af61 441 if (REVERSE_ENDIAN(dnsHeader->id) != 0xdead || (REVERSE_ENDIAN(dnsHeader->flags) & 0x800f) != 0x8000) {
okini3939 2:6efb3541af61 442 return -1;
okini3939 2:6efb3541af61 443 }
okini3939 2:6efb3541af61 444
okini3939 2:6efb3541af61 445 // skip question
okini3939 3:8573b122fa84 446 for (i = sizeof(DNSHeader); buf[i] && i < len; i ++);
okini3939 2:6efb3541af61 447 i = i + 1 + sizeof(DnsQuestionEnd);
okini3939 2:6efb3541af61 448
okini3939 2:6efb3541af61 449 // DNS answer
okini3939 2:6efb3541af61 450 while (i < len) {
okini3939 2:6efb3541af61 451 dnsAnswer = (DnsAnswer*)&buf[i];
okini3939 2:6efb3541af61 452 if (dnsAnswer->clas != REVERSE_ENDIAN(DNS_CLASS_IN)) {
okini3939 2:6efb3541af61 453 return -1;
okini3939 2:6efb3541af61 454 }
okini3939 2:6efb3541af61 455
okini3939 3:8573b122fa84 456 i = i + sizeof(DnsAnswer);
okini3939 2:6efb3541af61 457 if (dnsAnswer->type == REVERSE_ENDIAN(DNS_QUERY_A)) {
okini3939 2:6efb3541af61 458 addr = IpAddr(buf[i], buf[i + 1], buf[i + 2], buf[i + 3]);
okini3939 2:6efb3541af61 459 return 0;
okini3939 2:6efb3541af61 460 }
okini3939 2:6efb3541af61 461 i = i + dnsAnswer->length;
okini3939 2:6efb3541af61 462 }
okini3939 2:6efb3541af61 463
okini3939 2:6efb3541af61 464 return -1;
okini3939 2:6efb3541af61 465 }
okini3939 2:6efb3541af61 466 #endif
okini3939 2:6efb3541af61 467
okini3939 2:6efb3541af61 468
okini3939 2:6efb3541af61 469 IPv4TransmitRequest::IPv4TransmitRequest() : PayloadRequest(IPv4_TRANSMIT_REQUEST, DEFAULT_FRAME_ID, NULL, 0) {
okini3939 2:6efb3541af61 470 }
okini3939 2:6efb3541af61 471
okini3939 4:f6d73acc1f75 472 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 473 _dstAddr = dstAddr;
okini3939 2:6efb3541af61 474 _dstPort = dstPort;
okini3939 2:6efb3541af61 475 _srcPort = srcPort;
okini3939 2:6efb3541af61 476 _protocol = protocol;
okini3939 2:6efb3541af61 477 _option = option;
okini3939 2:6efb3541af61 478 }
okini3939 2:6efb3541af61 479
okini3939 4:f6d73acc1f75 480 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 481 _dstAddr = dstAddr;
okini3939 2:6efb3541af61 482 _dstPort = dstPort;
okini3939 2:6efb3541af61 483 _srcPort = 0x270f;
okini3939 2:6efb3541af61 484 _protocol = PROTOCOL_UDP;
okini3939 2:6efb3541af61 485 _option = 0;
okini3939 2:6efb3541af61 486 }
okini3939 2:6efb3541af61 487
okini3939 4:f6d73acc1f75 488 uint8_t IPv4TransmitRequest::getFrameData(uint16_t pos) {
okini3939 2:6efb3541af61 489 if (pos == 0) {
okini3939 2:6efb3541af61 490 return _dstAddr[0];
okini3939 2:6efb3541af61 491 } else if (pos == 1) {
okini3939 2:6efb3541af61 492 return _dstAddr[1];
okini3939 2:6efb3541af61 493 } else if (pos == 2) {
okini3939 2:6efb3541af61 494 return _dstAddr[2];
okini3939 2:6efb3541af61 495 } else if (pos == 3) {
okini3939 2:6efb3541af61 496 return _dstAddr[3];
okini3939 2:6efb3541af61 497 } else if (pos == 4) {
okini3939 2:6efb3541af61 498 return (_dstPort >> 8) & 0xff;
okini3939 2:6efb3541af61 499 } else if (pos == 5) {
okini3939 2:6efb3541af61 500 return _dstPort & 0xff;
okini3939 2:6efb3541af61 501 } else if (pos == 6) {
okini3939 2:6efb3541af61 502 return (_srcPort >> 8) & 0xff;
okini3939 2:6efb3541af61 503 } else if (pos == 7) {
okini3939 2:6efb3541af61 504 return _srcPort & 0xff;
okini3939 2:6efb3541af61 505 } else if (pos == 8) {
okini3939 2:6efb3541af61 506 return _protocol;
okini3939 2:6efb3541af61 507 } else if (pos == 9) {
okini3939 2:6efb3541af61 508 return _option;
okini3939 2:6efb3541af61 509 } else {
okini3939 2:6efb3541af61 510 return getPayload()[pos - IPv4_TRANSMIT_REQUEST_API_LENGTH];
okini3939 2:6efb3541af61 511 }
okini3939 2:6efb3541af61 512 }
okini3939 2:6efb3541af61 513
okini3939 4:f6d73acc1f75 514 uint16_t IPv4TransmitRequest::getFrameDataLength() {
okini3939 2:6efb3541af61 515 return IPv4_TRANSMIT_REQUEST_API_LENGTH + getPayloadLength();
okini3939 2:6efb3541af61 516 }
okini3939 2:6efb3541af61 517
okini3939 2:6efb3541af61 518 IpAddr& IPv4TransmitRequest::getAddress() {
okini3939 2:6efb3541af61 519 return _dstAddr;
okini3939 2:6efb3541af61 520 }
okini3939 2:6efb3541af61 521
okini3939 2:6efb3541af61 522 uint16_t IPv4TransmitRequest::getDstPort() {
okini3939 2:6efb3541af61 523 return _dstPort;
okini3939 2:6efb3541af61 524 }
okini3939 2:6efb3541af61 525
okini3939 2:6efb3541af61 526 uint16_t IPv4TransmitRequest::getSrcPort() {
okini3939 2:6efb3541af61 527 return _srcPort;
okini3939 2:6efb3541af61 528 }
okini3939 2:6efb3541af61 529
okini3939 2:6efb3541af61 530 uint8_t IPv4TransmitRequest::getProtocol() {
okini3939 2:6efb3541af61 531 return _protocol;
okini3939 2:6efb3541af61 532 }
okini3939 2:6efb3541af61 533
okini3939 2:6efb3541af61 534 uint8_t IPv4TransmitRequest::getOption() {
okini3939 2:6efb3541af61 535 return _option;
okini3939 2:6efb3541af61 536 }
okini3939 2:6efb3541af61 537
okini3939 2:6efb3541af61 538 void IPv4TransmitRequest::setAddress(IpAddr &dstAddr) {
okini3939 2:6efb3541af61 539 _dstAddr = dstAddr;
okini3939 2:6efb3541af61 540 }
okini3939 2:6efb3541af61 541
okini3939 2:6efb3541af61 542 void IPv4TransmitRequest::setDstPort(uint16_t dstPort) {
okini3939 2:6efb3541af61 543 _dstPort = dstPort;
okini3939 2:6efb3541af61 544 }
okini3939 2:6efb3541af61 545
okini3939 2:6efb3541af61 546 void IPv4TransmitRequest::setSrcPort(uint16_t srcPort) {
okini3939 2:6efb3541af61 547 _srcPort = srcPort;
okini3939 2:6efb3541af61 548 }
okini3939 2:6efb3541af61 549
okini3939 2:6efb3541af61 550 void IPv4TransmitRequest::setProtocol(uint8_t protocol) {
okini3939 2:6efb3541af61 551 _protocol = protocol;
okini3939 2:6efb3541af61 552 }
okini3939 2:6efb3541af61 553
okini3939 2:6efb3541af61 554 void IPv4TransmitRequest::setOption(uint8_t option) {
okini3939 2:6efb3541af61 555 _option = option;
okini3939 2:6efb3541af61 556 }
okini3939 2:6efb3541af61 557
okini3939 2:6efb3541af61 558
okini3939 2:6efb3541af61 559 Transmit_Status::Transmit_Status() : FrameIdResponse() {
okini3939 2:6efb3541af61 560
okini3939 2:6efb3541af61 561 }
okini3939 2:6efb3541af61 562
okini3939 2:6efb3541af61 563 uint8_t Transmit_Status::getStatus() {
okini3939 2:6efb3541af61 564 return getFrameData()[1];
okini3939 2:6efb3541af61 565 }
okini3939 2:6efb3541af61 566
okini3939 2:6efb3541af61 567 bool Transmit_Status::isSuccess() {
okini3939 2:6efb3541af61 568 return getStatus() == SUCCESS;
okini3939 2:6efb3541af61 569 }
okini3939 2:6efb3541af61 570
okini3939 2:6efb3541af61 571
okini3939 4:f6d73acc1f75 572 IPV4RxFrame::IPV4RxFrame () {
okini3939 4:f6d73acc1f75 573 }
okini3939 4:f6d73acc1f75 574
okini3939 2:6efb3541af61 575 IpAddr& IPV4RxFrame::getSrcAddress() {
okini3939 4:f6d73acc1f75 576 _srcAddr = IpAddr(getFrameData()[0], getFrameData()[1], getFrameData()[2], getFrameData()[3]);
okini3939 2:6efb3541af61 577 return _srcAddr;
okini3939 2:6efb3541af61 578 }
okini3939 2:6efb3541af61 579
okini3939 2:6efb3541af61 580 uint16_t IPV4RxFrame::getDstPort() {
okini3939 4:f6d73acc1f75 581 return (getFrameData()[4] << 8) + getFrameData()[5];
okini3939 2:6efb3541af61 582 }
okini3939 2:6efb3541af61 583
okini3939 2:6efb3541af61 584 uint16_t IPV4RxFrame::getSrcPort() {
okini3939 4:f6d73acc1f75 585 return (getFrameData()[6] << 8) + getFrameData()[7];
okini3939 2:6efb3541af61 586 }
okini3939 2:6efb3541af61 587
okini3939 2:6efb3541af61 588 uint8_t IPV4RxFrame::getProtocol() {
okini3939 4:f6d73acc1f75 589 return getFrameData()[8];
okini3939 2:6efb3541af61 590 }
okini3939 2:6efb3541af61 591
okini3939 2:6efb3541af61 592 uint8_t IPV4RxFrame::getStatus() {
okini3939 4:f6d73acc1f75 593 return getFrameData()[9];
okini3939 2:6efb3541af61 594 }
okini3939 2:6efb3541af61 595
okini3939 2:6efb3541af61 596 // markers to read data from packet array. this is the index, so the 12th item in the array
okini3939 2:6efb3541af61 597 uint8_t IPV4RxFrame::getDataOffset() {
okini3939 4:f6d73acc1f75 598 return 10;
okini3939 2:6efb3541af61 599 }
okini3939 2:6efb3541af61 600
okini3939 4:f6d73acc1f75 601 uint16_t IPV4RxFrame::getDataLength() {
okini3939 2:6efb3541af61 602 return getPacketLength() - getDataOffset() - 1;
okini3939 2:6efb3541af61 603 }
okini3939 6:6f84e2840408 604
okini3939 6:6f84e2840408 605 #endif
okini3939 6:6f84e2840408 606