Poerting the Xbee library to use teh wifi functionality of the LPC4088.
Dependents: XBee_WiFi_EA_LPC4088
Fork of XBee by
Diff: XBeeWiFi.cpp
- Revision:
- 4:f6d73acc1f75
- Parent:
- 3:8573b122fa84
- Child:
- 5:547cfff7adf7
diff -r 8573b122fa84 -r f6d73acc1f75 XBeeWiFi.cpp --- a/XBeeWiFi.cpp Thu Mar 08 17:41:29 2012 +0000 +++ b/XBeeWiFi.cpp Tue Mar 13 09:01:52 2012 +0000 @@ -19,7 +19,7 @@ #define REVERSE_ENDIAN(x) (uint16_t)(((uint16_t)x >> 8) | ((uint16_t)x << 8)) #ifdef USE_WIFICLASS -XBeeWiFi::XBeeWiFi (PinName p_tx, PinName p_rx, PinName p_cts) : XBee(p_tx, p_rx, p_cts) { +XBeeWiFi::XBeeWiFi (PinName p_tx, PinName p_rx, PinName p_cts, PinName p_rts) : XBee(p_tx, p_rx, p_cts, p_rts) { } int XBeeWiFi::setup (int security, const char *ssid, const char *pin) { @@ -86,6 +86,36 @@ return getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId()); } +int XBeeWiFi::baud (int b) { + // RESET + uint8_t cmd[2] = {'B', 'D'}; + char val[4]; + AtCommandRequest atRequest; + int r, len; + + if (b < 0x100) { + val[0] = b; + len = 1; + } else { + val[0] = (b >> 24) & 0xff; + val[1] = (b >> 16) & 0xff; + val[2] = (b >> 8) & 0xff; + val[3] = b & 0xff; + len = 4; + } + atRequest.setCommand(cmd); + atRequest.setCommandValue((uint8_t*)val); + atRequest.setCommandValueLength(len); + atRequest.setFrameId(getNextFrameId()); + send(atRequest); + r = getWiResponse(AT_COMMAND_RESPONSE, atRequest.getFrameId()); + + if (r == 0) { + begin(b); + } + return r; +} + int XBeeWiFi::setAddress () { // DHCP uint8_t cmd[2] = {'M', 'A'}; @@ -281,9 +311,11 @@ return 0; } +// *** Note: wifi is turned off when XBee send the port 53 udp packet. int XBeeWiFi::getHostByName (const char* name, IpAddr &addr) { - char buf[1024]; - int i, r, len; + Timer timeout; + char buf[600]; + int r, len; uint8_t cmd[2] = {'C', '0'}; char val[2]; IPv4TransmitRequest dnsRequest; @@ -294,7 +326,7 @@ addr = IpAddr(127, 0, 0, 1); return 0; } - +/* // bind src port val[0] = (DNS_SRC_PORT >> 8) & 0xff; val[1] = DNS_SRC_PORT & 0xff; @@ -305,7 +337,7 @@ send(atRequest); r = getWiResponse(AT_COMMAND_RESPONSE); DBG("wifi C0: %d\r\n", r); - +*/ // send DNS request len = createDnsRequest(name, buf); dnsRequest.setAddress(_nameserver); @@ -314,18 +346,23 @@ dnsRequest.setProtocol(PROTOCOL_UDP); dnsRequest.setPayload((uint8_t*)buf); dnsRequest.setPayloadLength(len); - for (i = 0; i < DNS_TIMEOUT; i ++) { + + // wait responce + timeout.reset(); + timeout.start(); + while (timeout.read_ms() < DNS_TIMEOUT) { dnsRequest.setFrameId(getNextFrameId()); send(dnsRequest); r = getWiResponse(TX_STATUS_RESPONSE, dnsRequest.getFrameId()); DBG("wifi TX: %d\r\n", r); - if (r == 0) { + if (r >= 0) { // recv DNS request r = getWiResponse(IPv4_RX_FRAME, 0, 3000); DBG("wifi RX: %d\r\n", r); if (r >= 0) { + timeout.stop(); getResponse().getAtCommandResponse(atResponse); return getDnsResponse(atResponse.getValue() + 6, atResponse.getValueLength() - 6, addr); } @@ -333,6 +370,7 @@ break; } } + timeout.stop(); return -1; } @@ -418,7 +456,7 @@ IPv4TransmitRequest::IPv4TransmitRequest() : PayloadRequest(IPv4_TRANSMIT_REQUEST, DEFAULT_FRAME_ID, NULL, 0) { } -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) { +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) { _dstAddr = dstAddr; _dstPort = dstPort; _srcPort = srcPort; @@ -426,7 +464,7 @@ _option = option; } -IPv4TransmitRequest::IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint8_t *data, uint8_t dataLength): PayloadRequest(IPv4_TRANSMIT_REQUEST, DEFAULT_FRAME_ID, data, dataLength) { +IPv4TransmitRequest::IPv4TransmitRequest(IpAddr &dstAddr, uint16_t dstPort, uint8_t *data, uint16_t dataLength): PayloadRequest(IPv4_TRANSMIT_REQUEST, DEFAULT_FRAME_ID, data, dataLength) { _dstAddr = dstAddr; _dstPort = dstPort; _srcPort = 0x270f; @@ -434,7 +472,7 @@ _option = 0; } -uint8_t IPv4TransmitRequest::getFrameData(uint8_t pos) { +uint8_t IPv4TransmitRequest::getFrameData(uint16_t pos) { if (pos == 0) { return _dstAddr[0]; } else if (pos == 1) { @@ -460,7 +498,7 @@ } } -uint8_t IPv4TransmitRequest::getFrameDataLength() { +uint16_t IPv4TransmitRequest::getFrameDataLength() { return IPv4_TRANSMIT_REQUEST_API_LENGTH + getPayloadLength(); } @@ -518,32 +556,35 @@ } +IPV4RxFrame::IPV4RxFrame () { +} + IpAddr& IPV4RxFrame::getSrcAddress() { - _srcAddr = IpAddr(getFrameData()[4], getFrameData()[5], getFrameData()[6], getFrameData()[7]); + _srcAddr = IpAddr(getFrameData()[0], getFrameData()[1], getFrameData()[2], getFrameData()[3]); return _srcAddr; } uint16_t IPV4RxFrame::getDstPort() { - return (getFrameData()[8] << 8) + getFrameData()[9]; + return (getFrameData()[4] << 8) + getFrameData()[5]; } uint16_t IPV4RxFrame::getSrcPort() { - return (getFrameData()[10] << 8) + getFrameData()[11]; + return (getFrameData()[6] << 8) + getFrameData()[7]; } uint8_t IPV4RxFrame::getProtocol() { - return getFrameData()[12]; + return getFrameData()[8]; } uint8_t IPV4RxFrame::getStatus() { - return getFrameData()[13]; + return getFrameData()[9]; } // markers to read data from packet array. this is the index, so the 12th item in the array uint8_t IPV4RxFrame::getDataOffset() { - return 14; + return 10; } -uint8_t IPV4RxFrame::getDataLength() { +uint16_t IPV4RxFrame::getDataLength() { return getPacketLength() - getDataOffset() - 1; }