Poerting the Xbee library to use teh wifi functionality of the LPC4088.
Dependents: XBee_WiFi_EA_LPC4088
Fork of XBee by
XBeeWiFi.cpp@2:6efb3541af61, 2011-07-29 (annotated)
- Committer:
- okini3939
- Date:
- Fri Jul 29 16:22:15 2011 +0000
- Revision:
- 2:6efb3541af61
- Child:
- 3:8573b122fa84
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 2:6efb3541af61 | 1 | /** |
okini3939 | 2:6efb3541af61 | 2 | * XBee Wi-Fi library for mbed |
okini3939 | 2:6efb3541af61 | 3 | * Copyright (c) 2011 Hiroshi Suga |
okini3939 | 2:6efb3541af61 | 4 | * Released under the MIT License: http://mbed.org/license/mit |
okini3939 | 2:6efb3541af61 | 5 | */ |
okini3939 | 2:6efb3541af61 | 6 | |
okini3939 | 2:6efb3541af61 | 7 | /** @file |
okini3939 | 2:6efb3541af61 | 8 | * @brief Weather Station |
okini3939 | 2:6efb3541af61 | 9 | */ |
okini3939 | 2:6efb3541af61 | 10 | |
okini3939 | 2:6efb3541af61 | 11 | #include "mbed.h" |
okini3939 | 2:6efb3541af61 | 12 | #include "XBee.h" |
okini3939 | 2:6efb3541af61 | 13 | #include "XBeeWiFi.h" |
okini3939 | 2:6efb3541af61 | 14 | #include "EthernetNetIf.h" |
okini3939 | 2:6efb3541af61 | 15 | |
okini3939 | 2:6efb3541af61 | 16 | #define REVERSE_ENDIAN(x) (uint16_t)(((uint16_t)x >> 8) | ((uint16_t)x << 8)) |
okini3939 | 2:6efb3541af61 | 17 | |
okini3939 | 2:6efb3541af61 | 18 | #ifdef USE_WIFICLASS |
okini3939 | 2:6efb3541af61 | 19 | XBeeWiFi::XBeeWiFi (PinName p_tx, PinName p_rx) : XBee(p_tx, p_rx) { |
okini3939 | 2:6efb3541af61 | 20 | } |
okini3939 | 2:6efb3541af61 | 21 | |
okini3939 | 2:6efb3541af61 | 22 | int XBeeWiFi::setup (int security, const char *ssid, const char *pin) { |
okini3939 | 2:6efb3541af61 | 23 | int len; |
okini3939 | 2:6efb3541af61 | 24 | uint8_t cmd[2], val[32]; |
okini3939 | 2:6efb3541af61 | 25 | AtCommandRequest atRequest; |
okini3939 | 2:6efb3541af61 | 26 | |
okini3939 | 2:6efb3541af61 | 27 | // security type |
okini3939 | 2:6efb3541af61 | 28 | memcpy(cmd, "EE", 2); |
okini3939 | 2:6efb3541af61 | 29 | val[0] = security; |
okini3939 | 2:6efb3541af61 | 30 | atRequest.setCommand(cmd); |
okini3939 | 2:6efb3541af61 | 31 | atRequest.setCommandValue(val); |
okini3939 | 2:6efb3541af61 | 32 | atRequest.setCommandValueLength(1); |
okini3939 | 2:6efb3541af61 | 33 | send(atRequest); |
okini3939 | 2:6efb3541af61 | 34 | getWiResponse(AT_COMMAND_RESPONSE); |
okini3939 | 2:6efb3541af61 | 35 | |
okini3939 | 2:6efb3541af61 | 36 | if (security != SECURITY_OPEN) { |
okini3939 | 2:6efb3541af61 | 37 | // SSID |
okini3939 | 2:6efb3541af61 | 38 | memcpy(cmd, "ID", 2); |
okini3939 | 2:6efb3541af61 | 39 | len = strlen(ssid); |
okini3939 | 2:6efb3541af61 | 40 | memcpy(val, ssid, len); |
okini3939 | 2:6efb3541af61 | 41 | atRequest.setCommand(cmd); |
okini3939 | 2:6efb3541af61 | 42 | atRequest.setCommandValue(val); |
okini3939 | 2:6efb3541af61 | 43 | atRequest.setCommandValueLength(len); |
okini3939 | 2:6efb3541af61 | 44 | send(atRequest); |
okini3939 | 2:6efb3541af61 | 45 | getWiResponse(AT_COMMAND_RESPONSE); |
okini3939 | 2:6efb3541af61 | 46 | |
okini3939 | 2:6efb3541af61 | 47 | // PIN |
okini3939 | 2:6efb3541af61 | 48 | memcpy(cmd, "PK", 2); |
okini3939 | 2:6efb3541af61 | 49 | len = strlen(pin); |
okini3939 | 2:6efb3541af61 | 50 | memcpy(val, pin, len); |
okini3939 | 2:6efb3541af61 | 51 | atRequest.setCommand(cmd); |
okini3939 | 2:6efb3541af61 | 52 | atRequest.setCommandValue(val); |
okini3939 | 2:6efb3541af61 | 53 | atRequest.setCommandValueLength(len); |
okini3939 | 2:6efb3541af61 | 54 | send(atRequest); |
okini3939 | 2:6efb3541af61 | 55 | getWiResponse(AT_COMMAND_RESPONSE); |
okini3939 | 2:6efb3541af61 | 56 | } |
okini3939 | 2:6efb3541af61 | 57 | |
okini3939 | 2:6efb3541af61 | 58 | return 0; |
okini3939 | 2:6efb3541af61 | 59 | } |
okini3939 | 2:6efb3541af61 | 60 | |
okini3939 | 2:6efb3541af61 | 61 | int XBeeWiFi::setup (const char *ssid) { |
okini3939 | 2:6efb3541af61 | 62 | return setup(SECURITY_OPEN, ssid, NULL); |
okini3939 | 2:6efb3541af61 | 63 | } |
okini3939 | 2:6efb3541af61 | 64 | |
okini3939 | 2:6efb3541af61 | 65 | int XBeeWiFi::reset () { |
okini3939 | 2:6efb3541af61 | 66 | // RESET |
okini3939 | 2:6efb3541af61 | 67 | uint8_t cmd[2] = {'N', 'R'}; |
okini3939 | 2:6efb3541af61 | 68 | AtCommandRequest atRequest; |
okini3939 | 2:6efb3541af61 | 69 | |
okini3939 | 2:6efb3541af61 | 70 | atRequest.setCommand(cmd); |
okini3939 | 2:6efb3541af61 | 71 | send(atRequest); |
okini3939 | 2:6efb3541af61 | 72 | |
okini3939 | 2:6efb3541af61 | 73 | return getWiResponse(AT_COMMAND_RESPONSE); |
okini3939 | 2:6efb3541af61 | 74 | } |
okini3939 | 2:6efb3541af61 | 75 | |
okini3939 | 2:6efb3541af61 | 76 | int XBeeWiFi::setAddress () { |
okini3939 | 2:6efb3541af61 | 77 | // DHCP |
okini3939 | 2:6efb3541af61 | 78 | uint8_t cmd[2] = {'M', 'A'}; |
okini3939 | 2:6efb3541af61 | 79 | char val[1] = {0}; |
okini3939 | 2:6efb3541af61 | 80 | AtCommandRequest atRequest; |
okini3939 | 2:6efb3541af61 | 81 | |
okini3939 | 2:6efb3541af61 | 82 | atRequest.setCommand(cmd); |
okini3939 | 2:6efb3541af61 | 83 | atRequest.setCommandValue((uint8_t*)val); |
okini3939 | 2:6efb3541af61 | 84 | atRequest.setCommandValueLength(1); |
okini3939 | 2:6efb3541af61 | 85 | send(atRequest); |
okini3939 | 2:6efb3541af61 | 86 | |
okini3939 | 2:6efb3541af61 | 87 | return getWiResponse(AT_COMMAND_RESPONSE); |
okini3939 | 2:6efb3541af61 | 88 | } |
okini3939 | 2:6efb3541af61 | 89 | |
okini3939 | 2:6efb3541af61 | 90 | int XBeeWiFi::setAddress (IpAddr &ipaddr, IpAddr &netmask, IpAddr &gateway, IpAddr &nameserver) { |
okini3939 | 2:6efb3541af61 | 91 | uint8_t cmd[2]; |
okini3939 | 2:6efb3541af61 | 92 | char val[32]; |
okini3939 | 2:6efb3541af61 | 93 | AtCommandRequest atRequest; |
okini3939 | 2:6efb3541af61 | 94 | |
okini3939 | 2:6efb3541af61 | 95 | // Static |
okini3939 | 2:6efb3541af61 | 96 | memcpy(cmd, "MA", 2); |
okini3939 | 2:6efb3541af61 | 97 | val[0] = 1; |
okini3939 | 2:6efb3541af61 | 98 | atRequest.setCommand(cmd); |
okini3939 | 2:6efb3541af61 | 99 | atRequest.setCommandValue((uint8_t*)val); |
okini3939 | 2:6efb3541af61 | 100 | atRequest.setCommandValueLength(1); |
okini3939 | 2:6efb3541af61 | 101 | send(atRequest); |
okini3939 | 2:6efb3541af61 | 102 | getWiResponse(AT_COMMAND_RESPONSE); |
okini3939 | 2:6efb3541af61 | 103 | |
okini3939 | 2:6efb3541af61 | 104 | // IP address |
okini3939 | 2:6efb3541af61 | 105 | memcpy(cmd, "MY", 2); |
okini3939 | 2:6efb3541af61 | 106 | sprintf(val, "%d.%d.%d.%d", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); |
okini3939 | 2:6efb3541af61 | 107 | atRequest.setCommand(cmd); |
okini3939 | 2:6efb3541af61 | 108 | atRequest.setCommandValue((uint8_t*)val); |
okini3939 | 2:6efb3541af61 | 109 | atRequest.setCommandValueLength(strlen(val)); |
okini3939 | 2:6efb3541af61 | 110 | send(atRequest); |
okini3939 | 2:6efb3541af61 | 111 | getWiResponse(AT_COMMAND_RESPONSE); |
okini3939 | 2:6efb3541af61 | 112 | |
okini3939 | 2:6efb3541af61 | 113 | // sub netmask |
okini3939 | 2:6efb3541af61 | 114 | memcpy(cmd, "NK", 2); |
okini3939 | 2:6efb3541af61 | 115 | sprintf(val, "%d.%d.%d.%d", netmask[0], netmask[1], netmask[2], netmask[3]); |
okini3939 | 2:6efb3541af61 | 116 | atRequest.setCommand(cmd); |
okini3939 | 2:6efb3541af61 | 117 | atRequest.setCommandValue((uint8_t*)val); |
okini3939 | 2:6efb3541af61 | 118 | atRequest.setCommandValueLength(strlen(val)); |
okini3939 | 2:6efb3541af61 | 119 | send(atRequest); |
okini3939 | 2:6efb3541af61 | 120 | getWiResponse(AT_COMMAND_RESPONSE); |
okini3939 | 2:6efb3541af61 | 121 | |
okini3939 | 2:6efb3541af61 | 122 | // default gateway |
okini3939 | 2:6efb3541af61 | 123 | memcpy(cmd, "GW", 2); |
okini3939 | 2:6efb3541af61 | 124 | sprintf(val, "%d.%d.%d.%d", gateway[0], gateway[1], gateway[2], gateway[3]); |
okini3939 | 2:6efb3541af61 | 125 | atRequest.setCommand(cmd); |
okini3939 | 2:6efb3541af61 | 126 | atRequest.setCommandValue((uint8_t*)val); |
okini3939 | 2:6efb3541af61 | 127 | atRequest.setCommandValueLength(strlen(val)); |
okini3939 | 2:6efb3541af61 | 128 | send(atRequest); |
okini3939 | 2:6efb3541af61 | 129 | getWiResponse(AT_COMMAND_RESPONSE); |
okini3939 | 2:6efb3541af61 | 130 | |
okini3939 | 2:6efb3541af61 | 131 | // name server |
okini3939 | 2:6efb3541af61 | 132 | _nameserver = nameserver; |
okini3939 | 2:6efb3541af61 | 133 | |
okini3939 | 2:6efb3541af61 | 134 | return 0; |
okini3939 | 2:6efb3541af61 | 135 | } |
okini3939 | 2:6efb3541af61 | 136 | |
okini3939 | 2:6efb3541af61 | 137 | int XBeeWiFi::setTimeout (int timeout) { |
okini3939 | 2:6efb3541af61 | 138 | // timeout |
okini3939 | 2:6efb3541af61 | 139 | uint8_t cmd[2] = {'T', 'P'}; |
okini3939 | 2:6efb3541af61 | 140 | char val[1]; |
okini3939 | 2:6efb3541af61 | 141 | AtCommandRequest atRequest; |
okini3939 | 2:6efb3541af61 | 142 | |
okini3939 | 2:6efb3541af61 | 143 | val[0] = timeout; |
okini3939 | 2:6efb3541af61 | 144 | atRequest.setCommand(cmd); |
okini3939 | 2:6efb3541af61 | 145 | atRequest.setCommandValue((uint8_t*)val); |
okini3939 | 2:6efb3541af61 | 146 | atRequest.setCommandValueLength(1); |
okini3939 | 2:6efb3541af61 | 147 | send(atRequest); |
okini3939 | 2:6efb3541af61 | 148 | |
okini3939 | 2:6efb3541af61 | 149 | return getWiResponse(AT_COMMAND_RESPONSE); |
okini3939 | 2:6efb3541af61 | 150 | } |
okini3939 | 2:6efb3541af61 | 151 | |
okini3939 | 2:6efb3541af61 | 152 | int XBeeWiFi::getStatus () { |
okini3939 | 2:6efb3541af61 | 153 | // AI |
okini3939 | 2:6efb3541af61 | 154 | uint8_t cmd[2] = {'A', 'I'}; |
okini3939 | 2:6efb3541af61 | 155 | AtCommandRequest atRequest; |
okini3939 | 2:6efb3541af61 | 156 | |
okini3939 | 2:6efb3541af61 | 157 | atRequest.setCommand(cmd); |
okini3939 | 2:6efb3541af61 | 158 | send(atRequest); |
okini3939 | 2:6efb3541af61 | 159 | |
okini3939 | 2:6efb3541af61 | 160 | return getWiResponse(AT_COMMAND_RESPONSE); |
okini3939 | 2:6efb3541af61 | 161 | } |
okini3939 | 2:6efb3541af61 | 162 | |
okini3939 | 2:6efb3541af61 | 163 | int XBeeWiFi::getWiResponse (int apiId, int timeout) { |
okini3939 | 2:6efb3541af61 | 164 | AtCommandResponse atResponse; |
okini3939 | 2:6efb3541af61 | 165 | |
okini3939 | 2:6efb3541af61 | 166 | if (readPacket(timeout)) { |
okini3939 | 2:6efb3541af61 | 167 | if (getResponse().getApiId() == apiId) { |
okini3939 | 2:6efb3541af61 | 168 | getResponse().getAtCommandResponse(atResponse); |
okini3939 | 2:6efb3541af61 | 169 | |
okini3939 | 2:6efb3541af61 | 170 | if (atResponse.isOk()) { |
okini3939 | 2:6efb3541af61 | 171 | |
okini3939 | 2:6efb3541af61 | 172 | return atResponse.getValue()[0]; |
okini3939 | 2:6efb3541af61 | 173 | |
okini3939 | 2:6efb3541af61 | 174 | } else { |
okini3939 | 2:6efb3541af61 | 175 | printf("Command return error code: %x\r\n", atResponse.getStatus()); |
okini3939 | 2:6efb3541af61 | 176 | } |
okini3939 | 2:6efb3541af61 | 177 | } else { |
okini3939 | 2:6efb3541af61 | 178 | printf("Expected AT response but got %x\r\n", getResponse().getApiId()); |
okini3939 | 2:6efb3541af61 | 179 | } |
okini3939 | 2:6efb3541af61 | 180 | } else { |
okini3939 | 2:6efb3541af61 | 181 | if (getResponse().isError()) { |
okini3939 | 2:6efb3541af61 | 182 | printf("Error reading packet. Error code: %x\r\n", getResponse().getErrorCode()); |
okini3939 | 2:6efb3541af61 | 183 | } else { |
okini3939 | 2:6efb3541af61 | 184 | printf("No response from radio"); |
okini3939 | 2:6efb3541af61 | 185 | } |
okini3939 | 2:6efb3541af61 | 186 | } |
okini3939 | 2:6efb3541af61 | 187 | |
okini3939 | 2:6efb3541af61 | 188 | return -1; |
okini3939 | 2:6efb3541af61 | 189 | } |
okini3939 | 2:6efb3541af61 | 190 | |
okini3939 | 2:6efb3541af61 | 191 | #ifdef USE_WIFIDNS |
okini3939 | 2:6efb3541af61 | 192 | int XBeeWiFi::getHostByName (const char* name, IpAddr &addr) { |
okini3939 | 2:6efb3541af61 | 193 | char buf[1024]; |
okini3939 | 2:6efb3541af61 | 194 | int len; |
okini3939 | 2:6efb3541af61 | 195 | IPv4TransmitRequest dnsRequest; |
okini3939 | 2:6efb3541af61 | 196 | AtCommandRequest atRequest; |
okini3939 | 2:6efb3541af61 | 197 | AtCommandResponse atResponse; |
okini3939 | 2:6efb3541af61 | 198 | |
okini3939 | 2:6efb3541af61 | 199 | if (!strcmp(name, "localhost")) { |
okini3939 | 2:6efb3541af61 | 200 | addr = IpAddr(127, 0, 0, 1); |
okini3939 | 2:6efb3541af61 | 201 | return 0; |
okini3939 | 2:6efb3541af61 | 202 | } |
okini3939 | 2:6efb3541af61 | 203 | |
okini3939 | 2:6efb3541af61 | 204 | len = createDnsRequest(name, buf); |
okini3939 | 2:6efb3541af61 | 205 | |
okini3939 | 2:6efb3541af61 | 206 | // send DNS request |
okini3939 | 2:6efb3541af61 | 207 | dnsRequest.setAddress(_nameserver); |
okini3939 | 2:6efb3541af61 | 208 | dnsRequest.setDstPort(53); |
okini3939 | 2:6efb3541af61 | 209 | dnsRequest.setSrcPort(1243); |
okini3939 | 2:6efb3541af61 | 210 | dnsRequest.setProtocol(PROTOCOL_UDP); |
okini3939 | 2:6efb3541af61 | 211 | dnsRequest.setPayload((uint8_t*)buf); |
okini3939 | 2:6efb3541af61 | 212 | dnsRequest.setPayloadLength(len); |
okini3939 | 2:6efb3541af61 | 213 | send(dnsRequest); |
okini3939 | 2:6efb3541af61 | 214 | |
okini3939 | 2:6efb3541af61 | 215 | if (getWiResponse(IPv4_TRANSMIT_STATUS) == 0) { |
okini3939 | 2:6efb3541af61 | 216 | // recv DNS request |
okini3939 | 2:6efb3541af61 | 217 | if (getWiResponse(IPv4_RX_FRAME, 30000) != -1) { |
okini3939 | 2:6efb3541af61 | 218 | getResponse().getAtCommandResponse(atResponse); |
okini3939 | 2:6efb3541af61 | 219 | if (atResponse.isOk()) { |
okini3939 | 2:6efb3541af61 | 220 | return getDnsResponse(atResponse.getValue(), atResponse.getValueLength(), addr); |
okini3939 | 2:6efb3541af61 | 221 | } |
okini3939 | 2:6efb3541af61 | 222 | } |
okini3939 | 2:6efb3541af61 | 223 | } |
okini3939 | 2:6efb3541af61 | 224 | |
okini3939 | 2:6efb3541af61 | 225 | return -1; |
okini3939 | 2:6efb3541af61 | 226 | } |
okini3939 | 2:6efb3541af61 | 227 | |
okini3939 | 2:6efb3541af61 | 228 | int XBeeWiFi::createDnsRequest (const char* name, char *buf) { |
okini3939 | 2:6efb3541af61 | 229 | struct DNSHeader *dnsHeader; |
okini3939 | 2:6efb3541af61 | 230 | struct DnsQuestionEnd *dnsEnd; |
okini3939 | 2:6efb3541af61 | 231 | int len, num; |
okini3939 | 2:6efb3541af61 | 232 | |
okini3939 | 2:6efb3541af61 | 233 | // DNS header |
okini3939 | 2:6efb3541af61 | 234 | dnsHeader = (DNSHeader*)buf; |
okini3939 | 2:6efb3541af61 | 235 | dnsHeader->id = REVERSE_ENDIAN(0xdead); |
okini3939 | 2:6efb3541af61 | 236 | dnsHeader->flags = REVERSE_ENDIAN(0x100); |
okini3939 | 2:6efb3541af61 | 237 | dnsHeader->questions = REVERSE_ENDIAN(1); |
okini3939 | 2:6efb3541af61 | 238 | dnsHeader->answers = 0; |
okini3939 | 2:6efb3541af61 | 239 | dnsHeader->authorities = 0; |
okini3939 | 2:6efb3541af61 | 240 | dnsHeader->additional = 0; |
okini3939 | 2:6efb3541af61 | 241 | len = sizeof(dnsHeader); |
okini3939 | 2:6efb3541af61 | 242 | |
okini3939 | 2:6efb3541af61 | 243 | // DNS question |
okini3939 | 2:6efb3541af61 | 244 | num = (int)strchr(name, '.'); |
okini3939 | 2:6efb3541af61 | 245 | while (num) { |
okini3939 | 2:6efb3541af61 | 246 | num = num - (int)name; |
okini3939 | 2:6efb3541af61 | 247 | buf[len] = num; |
okini3939 | 2:6efb3541af61 | 248 | len ++; |
okini3939 | 2:6efb3541af61 | 249 | strncpy(&buf[len], name, num); |
okini3939 | 2:6efb3541af61 | 250 | name = name + num; |
okini3939 | 2:6efb3541af61 | 251 | len = len + num; |
okini3939 | 2:6efb3541af61 | 252 | num = (int)strchr(name, '.'); |
okini3939 | 2:6efb3541af61 | 253 | } |
okini3939 | 2:6efb3541af61 | 254 | |
okini3939 | 2:6efb3541af61 | 255 | num = (int)strlen(name); |
okini3939 | 2:6efb3541af61 | 256 | if (num) { |
okini3939 | 2:6efb3541af61 | 257 | buf[len] = num; |
okini3939 | 2:6efb3541af61 | 258 | len ++; |
okini3939 | 2:6efb3541af61 | 259 | strncpy(&buf[len], name, num); |
okini3939 | 2:6efb3541af61 | 260 | len = len + num + 1; |
okini3939 | 2:6efb3541af61 | 261 | } |
okini3939 | 2:6efb3541af61 | 262 | |
okini3939 | 2:6efb3541af61 | 263 | dnsEnd = (DnsQuestionEnd*)&buf[len]; |
okini3939 | 2:6efb3541af61 | 264 | dnsEnd->type = REVERSE_ENDIAN(DNS_QUERY_A); |
okini3939 | 2:6efb3541af61 | 265 | dnsEnd->clas = REVERSE_ENDIAN(DNS_CLASS_IN); |
okini3939 | 2:6efb3541af61 | 266 | |
okini3939 | 2:6efb3541af61 | 267 | return len + sizeof(dnsEnd); |
okini3939 | 2:6efb3541af61 | 268 | } |
okini3939 | 2:6efb3541af61 | 269 | |
okini3939 | 2:6efb3541af61 | 270 | int XBeeWiFi::getDnsResponse (const uint8_t *buf, int len, IpAddr &addr) { |
okini3939 | 2:6efb3541af61 | 271 | int i; |
okini3939 | 2:6efb3541af61 | 272 | struct DNSHeader *dnsHeader; |
okini3939 | 2:6efb3541af61 | 273 | struct DnsAnswer *dnsAnswer; |
okini3939 | 2:6efb3541af61 | 274 | |
okini3939 | 2:6efb3541af61 | 275 | // DNS header |
okini3939 | 2:6efb3541af61 | 276 | dnsHeader = (DNSHeader*)buf; |
okini3939 | 2:6efb3541af61 | 277 | if (REVERSE_ENDIAN(dnsHeader->id) != 0xdead || (REVERSE_ENDIAN(dnsHeader->flags) & 0x800f) != 0x8000) { |
okini3939 | 2:6efb3541af61 | 278 | return -1; |
okini3939 | 2:6efb3541af61 | 279 | } |
okini3939 | 2:6efb3541af61 | 280 | |
okini3939 | 2:6efb3541af61 | 281 | // skip question |
okini3939 | 2:6efb3541af61 | 282 | for (i = sizeof(dnsHeader); buf[i] && i < len; i ++); |
okini3939 | 2:6efb3541af61 | 283 | i = i + 1 + sizeof(DnsQuestionEnd); |
okini3939 | 2:6efb3541af61 | 284 | |
okini3939 | 2:6efb3541af61 | 285 | // DNS answer |
okini3939 | 2:6efb3541af61 | 286 | while (i < len) { |
okini3939 | 2:6efb3541af61 | 287 | dnsAnswer = (DnsAnswer*)&buf[i]; |
okini3939 | 2:6efb3541af61 | 288 | if (dnsAnswer->clas != REVERSE_ENDIAN(DNS_CLASS_IN)) { |
okini3939 | 2:6efb3541af61 | 289 | return -1; |
okini3939 | 2:6efb3541af61 | 290 | } |
okini3939 | 2:6efb3541af61 | 291 | |
okini3939 | 2:6efb3541af61 | 292 | i = i + sizeof(dnsAnswer); |
okini3939 | 2:6efb3541af61 | 293 | if (dnsAnswer->type == REVERSE_ENDIAN(DNS_QUERY_A)) { |
okini3939 | 2:6efb3541af61 | 294 | addr = IpAddr(buf[i], buf[i + 1], buf[i + 2], buf[i + 3]); |
okini3939 | 2:6efb3541af61 | 295 | return 0; |
okini3939 | 2:6efb3541af61 | 296 | } |
okini3939 | 2:6efb3541af61 | 297 | i = i + dnsAnswer->length; |
okini3939 | 2:6efb3541af61 | 298 | } |
okini3939 | 2:6efb3541af61 | 299 | |
okini3939 | 2:6efb3541af61 | 300 | return -1; |
okini3939 | 2:6efb3541af61 | 301 | } |
okini3939 | 2:6efb3541af61 | 302 | #endif |
okini3939 | 2:6efb3541af61 | 303 | |
okini3939 | 2:6efb3541af61 | 304 | #endif |
okini3939 | 2:6efb3541af61 | 305 | |
okini3939 | 2:6efb3541af61 | 306 | |
okini3939 | 2:6efb3541af61 | 307 | IPv4TransmitRequest::IPv4TransmitRequest() : PayloadRequest(IPv4_TRANSMIT_REQUEST, DEFAULT_FRAME_ID, NULL, 0) { |
okini3939 | 2:6efb3541af61 | 308 | } |
okini3939 | 2:6efb3541af61 | 309 | |
okini3939 | 2:6efb3541af61 | 310 | IPv4TransmitRequest::IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint16_t srcPort, uint8_t protocol, uint8_t option, uint8_t *data, uint8_t dataLength, uint8_t frameId): PayloadRequest(IPv4_TRANSMIT_REQUEST, frameId, data, dataLength) { |
okini3939 | 2:6efb3541af61 | 311 | _dstAddr = dstAddr; |
okini3939 | 2:6efb3541af61 | 312 | _dstPort = dstPort; |
okini3939 | 2:6efb3541af61 | 313 | _srcPort = srcPort; |
okini3939 | 2:6efb3541af61 | 314 | _protocol = protocol; |
okini3939 | 2:6efb3541af61 | 315 | _option = option; |
okini3939 | 2:6efb3541af61 | 316 | } |
okini3939 | 2:6efb3541af61 | 317 | |
okini3939 | 2:6efb3541af61 | 318 | IPv4TransmitRequest::IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint8_t *data, uint8_t dataLength): PayloadRequest(IPv4_TRANSMIT_REQUEST, DEFAULT_FRAME_ID, data, dataLength) { |
okini3939 | 2:6efb3541af61 | 319 | _dstAddr = dstAddr; |
okini3939 | 2:6efb3541af61 | 320 | _dstPort = dstPort; |
okini3939 | 2:6efb3541af61 | 321 | _srcPort = 0x270f; |
okini3939 | 2:6efb3541af61 | 322 | _protocol = PROTOCOL_UDP; |
okini3939 | 2:6efb3541af61 | 323 | _option = 0; |
okini3939 | 2:6efb3541af61 | 324 | } |
okini3939 | 2:6efb3541af61 | 325 | |
okini3939 | 2:6efb3541af61 | 326 | uint8_t IPv4TransmitRequest::getFrameData(uint8_t pos) { |
okini3939 | 2:6efb3541af61 | 327 | if (pos == 0) { |
okini3939 | 2:6efb3541af61 | 328 | return _dstAddr[0]; |
okini3939 | 2:6efb3541af61 | 329 | } else if (pos == 1) { |
okini3939 | 2:6efb3541af61 | 330 | return _dstAddr[1]; |
okini3939 | 2:6efb3541af61 | 331 | } else if (pos == 2) { |
okini3939 | 2:6efb3541af61 | 332 | return _dstAddr[2]; |
okini3939 | 2:6efb3541af61 | 333 | } else if (pos == 3) { |
okini3939 | 2:6efb3541af61 | 334 | return _dstAddr[3]; |
okini3939 | 2:6efb3541af61 | 335 | } else if (pos == 4) { |
okini3939 | 2:6efb3541af61 | 336 | return (_dstPort >> 8) & 0xff; |
okini3939 | 2:6efb3541af61 | 337 | } else if (pos == 5) { |
okini3939 | 2:6efb3541af61 | 338 | return _dstPort & 0xff; |
okini3939 | 2:6efb3541af61 | 339 | } else if (pos == 6) { |
okini3939 | 2:6efb3541af61 | 340 | return (_srcPort >> 8) & 0xff; |
okini3939 | 2:6efb3541af61 | 341 | } else if (pos == 7) { |
okini3939 | 2:6efb3541af61 | 342 | return _srcPort & 0xff; |
okini3939 | 2:6efb3541af61 | 343 | } else if (pos == 8) { |
okini3939 | 2:6efb3541af61 | 344 | return _protocol; |
okini3939 | 2:6efb3541af61 | 345 | } else if (pos == 9) { |
okini3939 | 2:6efb3541af61 | 346 | return _option; |
okini3939 | 2:6efb3541af61 | 347 | } else { |
okini3939 | 2:6efb3541af61 | 348 | return getPayload()[pos - IPv4_TRANSMIT_REQUEST_API_LENGTH]; |
okini3939 | 2:6efb3541af61 | 349 | } |
okini3939 | 2:6efb3541af61 | 350 | } |
okini3939 | 2:6efb3541af61 | 351 | |
okini3939 | 2:6efb3541af61 | 352 | uint8_t IPv4TransmitRequest::getFrameDataLength() { |
okini3939 | 2:6efb3541af61 | 353 | return IPv4_TRANSMIT_REQUEST_API_LENGTH + getPayloadLength(); |
okini3939 | 2:6efb3541af61 | 354 | } |
okini3939 | 2:6efb3541af61 | 355 | |
okini3939 | 2:6efb3541af61 | 356 | IpAddr& IPv4TransmitRequest::getAddress() { |
okini3939 | 2:6efb3541af61 | 357 | return _dstAddr; |
okini3939 | 2:6efb3541af61 | 358 | } |
okini3939 | 2:6efb3541af61 | 359 | |
okini3939 | 2:6efb3541af61 | 360 | uint16_t IPv4TransmitRequest::getDstPort() { |
okini3939 | 2:6efb3541af61 | 361 | return _dstPort; |
okini3939 | 2:6efb3541af61 | 362 | } |
okini3939 | 2:6efb3541af61 | 363 | |
okini3939 | 2:6efb3541af61 | 364 | uint16_t IPv4TransmitRequest::getSrcPort() { |
okini3939 | 2:6efb3541af61 | 365 | return _srcPort; |
okini3939 | 2:6efb3541af61 | 366 | } |
okini3939 | 2:6efb3541af61 | 367 | |
okini3939 | 2:6efb3541af61 | 368 | uint8_t IPv4TransmitRequest::getProtocol() { |
okini3939 | 2:6efb3541af61 | 369 | return _protocol; |
okini3939 | 2:6efb3541af61 | 370 | } |
okini3939 | 2:6efb3541af61 | 371 | |
okini3939 | 2:6efb3541af61 | 372 | uint8_t IPv4TransmitRequest::getOption() { |
okini3939 | 2:6efb3541af61 | 373 | return _option; |
okini3939 | 2:6efb3541af61 | 374 | } |
okini3939 | 2:6efb3541af61 | 375 | |
okini3939 | 2:6efb3541af61 | 376 | void IPv4TransmitRequest::setAddress(IpAddr &dstAddr) { |
okini3939 | 2:6efb3541af61 | 377 | _dstAddr = dstAddr; |
okini3939 | 2:6efb3541af61 | 378 | } |
okini3939 | 2:6efb3541af61 | 379 | |
okini3939 | 2:6efb3541af61 | 380 | void IPv4TransmitRequest::setDstPort(uint16_t dstPort) { |
okini3939 | 2:6efb3541af61 | 381 | _dstPort = dstPort; |
okini3939 | 2:6efb3541af61 | 382 | } |
okini3939 | 2:6efb3541af61 | 383 | |
okini3939 | 2:6efb3541af61 | 384 | void IPv4TransmitRequest::setSrcPort(uint16_t srcPort) { |
okini3939 | 2:6efb3541af61 | 385 | _srcPort = srcPort; |
okini3939 | 2:6efb3541af61 | 386 | } |
okini3939 | 2:6efb3541af61 | 387 | |
okini3939 | 2:6efb3541af61 | 388 | void IPv4TransmitRequest::setProtocol(uint8_t protocol) { |
okini3939 | 2:6efb3541af61 | 389 | _protocol = protocol; |
okini3939 | 2:6efb3541af61 | 390 | } |
okini3939 | 2:6efb3541af61 | 391 | |
okini3939 | 2:6efb3541af61 | 392 | void IPv4TransmitRequest::setOption(uint8_t option) { |
okini3939 | 2:6efb3541af61 | 393 | _option = option; |
okini3939 | 2:6efb3541af61 | 394 | } |
okini3939 | 2:6efb3541af61 | 395 | |
okini3939 | 2:6efb3541af61 | 396 | |
okini3939 | 2:6efb3541af61 | 397 | Transmit_Status::Transmit_Status() : FrameIdResponse() { |
okini3939 | 2:6efb3541af61 | 398 | |
okini3939 | 2:6efb3541af61 | 399 | } |
okini3939 | 2:6efb3541af61 | 400 | |
okini3939 | 2:6efb3541af61 | 401 | uint8_t Transmit_Status::getStatus() { |
okini3939 | 2:6efb3541af61 | 402 | return getFrameData()[1]; |
okini3939 | 2:6efb3541af61 | 403 | } |
okini3939 | 2:6efb3541af61 | 404 | |
okini3939 | 2:6efb3541af61 | 405 | bool Transmit_Status::isSuccess() { |
okini3939 | 2:6efb3541af61 | 406 | return getStatus() == SUCCESS; |
okini3939 | 2:6efb3541af61 | 407 | } |
okini3939 | 2:6efb3541af61 | 408 | |
okini3939 | 2:6efb3541af61 | 409 | |
okini3939 | 2:6efb3541af61 | 410 | IpAddr& IPV4RxFrame::getSrcAddress() { |
okini3939 | 2:6efb3541af61 | 411 | _srcAddr = IpAddr(getFrameData()[4], getFrameData()[5], getFrameData()[6], getFrameData()[7]); |
okini3939 | 2:6efb3541af61 | 412 | return _srcAddr; |
okini3939 | 2:6efb3541af61 | 413 | } |
okini3939 | 2:6efb3541af61 | 414 | |
okini3939 | 2:6efb3541af61 | 415 | uint16_t IPV4RxFrame::getDstPort() { |
okini3939 | 2:6efb3541af61 | 416 | return (getFrameData()[8] << 8) + getFrameData()[9]; |
okini3939 | 2:6efb3541af61 | 417 | } |
okini3939 | 2:6efb3541af61 | 418 | |
okini3939 | 2:6efb3541af61 | 419 | uint16_t IPV4RxFrame::getSrcPort() { |
okini3939 | 2:6efb3541af61 | 420 | return (getFrameData()[10] << 8) + getFrameData()[11]; |
okini3939 | 2:6efb3541af61 | 421 | } |
okini3939 | 2:6efb3541af61 | 422 | |
okini3939 | 2:6efb3541af61 | 423 | uint8_t IPV4RxFrame::getProtocol() { |
okini3939 | 2:6efb3541af61 | 424 | return getFrameData()[12]; |
okini3939 | 2:6efb3541af61 | 425 | } |
okini3939 | 2:6efb3541af61 | 426 | |
okini3939 | 2:6efb3541af61 | 427 | uint8_t IPV4RxFrame::getStatus() { |
okini3939 | 2:6efb3541af61 | 428 | return getFrameData()[13]; |
okini3939 | 2:6efb3541af61 | 429 | } |
okini3939 | 2:6efb3541af61 | 430 | |
okini3939 | 2:6efb3541af61 | 431 | // markers to read data from packet array. this is the index, so the 12th item in the array |
okini3939 | 2:6efb3541af61 | 432 | uint8_t IPV4RxFrame::getDataOffset() { |
okini3939 | 2:6efb3541af61 | 433 | return 14; |
okini3939 | 2:6efb3541af61 | 434 | } |
okini3939 | 2:6efb3541af61 | 435 | |
okini3939 | 2:6efb3541af61 | 436 | uint8_t IPV4RxFrame::getDataLength() { |
okini3939 | 2:6efb3541af61 | 437 | return getPacketLength() - getDataOffset() - 1; |
okini3939 | 2:6efb3541af61 | 438 | } |