Control a robot over the internet using UDP and a Wifly module (WiFi).
Dependencies: Motor TextLCD WiflyInterface mbed-rtos mbed
WiflyInterface/Socket/UDPSocket.cpp@0:c0dc3a76f3d4, 2013-10-17 (annotated)
- Committer:
- apatel336
- Date:
- Thu Oct 17 13:27:56 2013 +0000
- Revision:
- 0:c0dc3a76f3d4
Initial Release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apatel336 | 0:c0dc3a76f3d4 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
apatel336 | 0:c0dc3a76f3d4 | 2 | * |
apatel336 | 0:c0dc3a76f3d4 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
apatel336 | 0:c0dc3a76f3d4 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
apatel336 | 0:c0dc3a76f3d4 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
apatel336 | 0:c0dc3a76f3d4 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
apatel336 | 0:c0dc3a76f3d4 | 7 | * furnished to do so, subject to the following conditions: |
apatel336 | 0:c0dc3a76f3d4 | 8 | * |
apatel336 | 0:c0dc3a76f3d4 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
apatel336 | 0:c0dc3a76f3d4 | 10 | * substantial portions of the Software. |
apatel336 | 0:c0dc3a76f3d4 | 11 | * |
apatel336 | 0:c0dc3a76f3d4 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
apatel336 | 0:c0dc3a76f3d4 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
apatel336 | 0:c0dc3a76f3d4 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
apatel336 | 0:c0dc3a76f3d4 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
apatel336 | 0:c0dc3a76f3d4 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
apatel336 | 0:c0dc3a76f3d4 | 17 | */ |
apatel336 | 0:c0dc3a76f3d4 | 18 | |
apatel336 | 0:c0dc3a76f3d4 | 19 | #include "UDPSocket.h" |
apatel336 | 0:c0dc3a76f3d4 | 20 | |
apatel336 | 0:c0dc3a76f3d4 | 21 | #include <string> |
apatel336 | 0:c0dc3a76f3d4 | 22 | #include <algorithm> |
apatel336 | 0:c0dc3a76f3d4 | 23 | |
apatel336 | 0:c0dc3a76f3d4 | 24 | UDPSocket::UDPSocket() |
apatel336 | 0:c0dc3a76f3d4 | 25 | { |
apatel336 | 0:c0dc3a76f3d4 | 26 | endpoint_configured = false; |
apatel336 | 0:c0dc3a76f3d4 | 27 | endpoint_read = false; |
apatel336 | 0:c0dc3a76f3d4 | 28 | } |
apatel336 | 0:c0dc3a76f3d4 | 29 | |
apatel336 | 0:c0dc3a76f3d4 | 30 | int UDPSocket::init(void) |
apatel336 | 0:c0dc3a76f3d4 | 31 | { |
apatel336 | 0:c0dc3a76f3d4 | 32 | wifi->setProtocol(UDP); |
apatel336 | 0:c0dc3a76f3d4 | 33 | wifi->exit(); |
apatel336 | 0:c0dc3a76f3d4 | 34 | return 0; |
apatel336 | 0:c0dc3a76f3d4 | 35 | } |
apatel336 | 0:c0dc3a76f3d4 | 36 | |
apatel336 | 0:c0dc3a76f3d4 | 37 | // Server initialization |
apatel336 | 0:c0dc3a76f3d4 | 38 | int UDPSocket::bind(int port) |
apatel336 | 0:c0dc3a76f3d4 | 39 | { |
apatel336 | 0:c0dc3a76f3d4 | 40 | char cmd[17]; |
apatel336 | 0:c0dc3a76f3d4 | 41 | |
apatel336 | 0:c0dc3a76f3d4 | 42 | // set local port |
apatel336 | 0:c0dc3a76f3d4 | 43 | sprintf(cmd, "set i l %d\r", port); |
apatel336 | 0:c0dc3a76f3d4 | 44 | if (!wifi->sendCommand(cmd, "AOK")) |
apatel336 | 0:c0dc3a76f3d4 | 45 | return -1; |
apatel336 | 0:c0dc3a76f3d4 | 46 | |
apatel336 | 0:c0dc3a76f3d4 | 47 | // save |
apatel336 | 0:c0dc3a76f3d4 | 48 | if (!wifi->sendCommand("save\r", "Stor")) |
apatel336 | 0:c0dc3a76f3d4 | 49 | return -1; |
apatel336 | 0:c0dc3a76f3d4 | 50 | |
apatel336 | 0:c0dc3a76f3d4 | 51 | // reboot |
apatel336 | 0:c0dc3a76f3d4 | 52 | wifi->reboot(); |
apatel336 | 0:c0dc3a76f3d4 | 53 | |
apatel336 | 0:c0dc3a76f3d4 | 54 | // set udp protocol |
apatel336 | 0:c0dc3a76f3d4 | 55 | wifi->setProtocol(UDP); |
apatel336 | 0:c0dc3a76f3d4 | 56 | |
apatel336 | 0:c0dc3a76f3d4 | 57 | // connect the network |
apatel336 | 0:c0dc3a76f3d4 | 58 | if (wifi->isDHCP()) { |
apatel336 | 0:c0dc3a76f3d4 | 59 | if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000)) |
apatel336 | 0:c0dc3a76f3d4 | 60 | return -1; |
apatel336 | 0:c0dc3a76f3d4 | 61 | } else { |
apatel336 | 0:c0dc3a76f3d4 | 62 | if (!wifi->sendCommand("join\r", "Associated", NULL, 10000)) |
apatel336 | 0:c0dc3a76f3d4 | 63 | return -1; |
apatel336 | 0:c0dc3a76f3d4 | 64 | } |
apatel336 | 0:c0dc3a76f3d4 | 65 | |
apatel336 | 0:c0dc3a76f3d4 | 66 | // exit |
apatel336 | 0:c0dc3a76f3d4 | 67 | wifi->exit(); |
apatel336 | 0:c0dc3a76f3d4 | 68 | wifi->flush(); |
apatel336 | 0:c0dc3a76f3d4 | 69 | return 0; |
apatel336 | 0:c0dc3a76f3d4 | 70 | } |
apatel336 | 0:c0dc3a76f3d4 | 71 | |
apatel336 | 0:c0dc3a76f3d4 | 72 | // -1 if unsuccessful, else number of bytes written |
apatel336 | 0:c0dc3a76f3d4 | 73 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
apatel336 | 0:c0dc3a76f3d4 | 74 | { |
apatel336 | 0:c0dc3a76f3d4 | 75 | Timer tmr; |
apatel336 | 0:c0dc3a76f3d4 | 76 | int idx = 0; |
apatel336 | 0:c0dc3a76f3d4 | 77 | |
apatel336 | 0:c0dc3a76f3d4 | 78 | confEndpoint(remote); |
apatel336 | 0:c0dc3a76f3d4 | 79 | |
apatel336 | 0:c0dc3a76f3d4 | 80 | tmr.start(); |
apatel336 | 0:c0dc3a76f3d4 | 81 | |
apatel336 | 0:c0dc3a76f3d4 | 82 | while ((tmr.read_ms() < _timeout) || _blocking) { |
apatel336 | 0:c0dc3a76f3d4 | 83 | |
apatel336 | 0:c0dc3a76f3d4 | 84 | idx += wifi->send(packet, length); |
apatel336 | 0:c0dc3a76f3d4 | 85 | |
apatel336 | 0:c0dc3a76f3d4 | 86 | if (idx == length) |
apatel336 | 0:c0dc3a76f3d4 | 87 | return idx; |
apatel336 | 0:c0dc3a76f3d4 | 88 | } |
apatel336 | 0:c0dc3a76f3d4 | 89 | return (idx == 0) ? -1 : idx; |
apatel336 | 0:c0dc3a76f3d4 | 90 | } |
apatel336 | 0:c0dc3a76f3d4 | 91 | |
apatel336 | 0:c0dc3a76f3d4 | 92 | // -1 if unsuccessful, else number of bytes received |
apatel336 | 0:c0dc3a76f3d4 | 93 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
apatel336 | 0:c0dc3a76f3d4 | 94 | { |
apatel336 | 0:c0dc3a76f3d4 | 95 | Timer tmr; |
apatel336 | 0:c0dc3a76f3d4 | 96 | int idx = 0; |
apatel336 | 0:c0dc3a76f3d4 | 97 | int nb_available = 0; |
apatel336 | 0:c0dc3a76f3d4 | 98 | int time = -1; |
apatel336 | 0:c0dc3a76f3d4 | 99 | |
apatel336 | 0:c0dc3a76f3d4 | 100 | if (_blocking) { |
apatel336 | 0:c0dc3a76f3d4 | 101 | while (1) { |
apatel336 | 0:c0dc3a76f3d4 | 102 | nb_available = wifi->readable(); |
apatel336 | 0:c0dc3a76f3d4 | 103 | if (nb_available != 0) { |
apatel336 | 0:c0dc3a76f3d4 | 104 | break; |
apatel336 | 0:c0dc3a76f3d4 | 105 | } |
apatel336 | 0:c0dc3a76f3d4 | 106 | } |
apatel336 | 0:c0dc3a76f3d4 | 107 | } |
apatel336 | 0:c0dc3a76f3d4 | 108 | |
apatel336 | 0:c0dc3a76f3d4 | 109 | tmr.start(); |
apatel336 | 0:c0dc3a76f3d4 | 110 | |
apatel336 | 0:c0dc3a76f3d4 | 111 | while (time < _timeout) { |
apatel336 | 0:c0dc3a76f3d4 | 112 | |
apatel336 | 0:c0dc3a76f3d4 | 113 | nb_available = wifi->readable(); |
apatel336 | 0:c0dc3a76f3d4 | 114 | for (int i = 0; i < min(nb_available, length); i++) { |
apatel336 | 0:c0dc3a76f3d4 | 115 | buffer[idx] = wifi->getc(); |
apatel336 | 0:c0dc3a76f3d4 | 116 | idx++; |
apatel336 | 0:c0dc3a76f3d4 | 117 | } |
apatel336 | 0:c0dc3a76f3d4 | 118 | |
apatel336 | 0:c0dc3a76f3d4 | 119 | if (idx == length) { |
apatel336 | 0:c0dc3a76f3d4 | 120 | break; |
apatel336 | 0:c0dc3a76f3d4 | 121 | } |
apatel336 | 0:c0dc3a76f3d4 | 122 | |
apatel336 | 0:c0dc3a76f3d4 | 123 | time = tmr.read_ms(); |
apatel336 | 0:c0dc3a76f3d4 | 124 | } |
apatel336 | 0:c0dc3a76f3d4 | 125 | |
apatel336 | 0:c0dc3a76f3d4 | 126 | readEndpoint(remote); |
apatel336 | 0:c0dc3a76f3d4 | 127 | return (idx == 0) ? -1 : idx; |
apatel336 | 0:c0dc3a76f3d4 | 128 | } |
apatel336 | 0:c0dc3a76f3d4 | 129 | |
apatel336 | 0:c0dc3a76f3d4 | 130 | bool UDPSocket::confEndpoint(Endpoint & ep) |
apatel336 | 0:c0dc3a76f3d4 | 131 | { |
apatel336 | 0:c0dc3a76f3d4 | 132 | char * host; |
apatel336 | 0:c0dc3a76f3d4 | 133 | char cmd[30]; |
apatel336 | 0:c0dc3a76f3d4 | 134 | if (!endpoint_configured) { |
apatel336 | 0:c0dc3a76f3d4 | 135 | host = ep.get_address(); |
apatel336 | 0:c0dc3a76f3d4 | 136 | if (host[0] != '\0') { |
apatel336 | 0:c0dc3a76f3d4 | 137 | // set host |
apatel336 | 0:c0dc3a76f3d4 | 138 | sprintf(cmd, "set i h %s\r", host); |
apatel336 | 0:c0dc3a76f3d4 | 139 | if (!wifi->sendCommand(cmd, "AOK")) |
apatel336 | 0:c0dc3a76f3d4 | 140 | return false; |
apatel336 | 0:c0dc3a76f3d4 | 141 | |
apatel336 | 0:c0dc3a76f3d4 | 142 | // set remote port |
apatel336 | 0:c0dc3a76f3d4 | 143 | sprintf(cmd, "set i r %d\r", ep.get_port()); |
apatel336 | 0:c0dc3a76f3d4 | 144 | if (!wifi->sendCommand(cmd, "AOK")) |
apatel336 | 0:c0dc3a76f3d4 | 145 | return false; |
apatel336 | 0:c0dc3a76f3d4 | 146 | |
apatel336 | 0:c0dc3a76f3d4 | 147 | wifi->exit(); |
apatel336 | 0:c0dc3a76f3d4 | 148 | endpoint_configured = true; |
apatel336 | 0:c0dc3a76f3d4 | 149 | return true; |
apatel336 | 0:c0dc3a76f3d4 | 150 | } |
apatel336 | 0:c0dc3a76f3d4 | 151 | } |
apatel336 | 0:c0dc3a76f3d4 | 152 | return true; |
apatel336 | 0:c0dc3a76f3d4 | 153 | } |
apatel336 | 0:c0dc3a76f3d4 | 154 | |
apatel336 | 0:c0dc3a76f3d4 | 155 | bool UDPSocket::readEndpoint(Endpoint & ep) |
apatel336 | 0:c0dc3a76f3d4 | 156 | { |
apatel336 | 0:c0dc3a76f3d4 | 157 | char recv[256]; |
apatel336 | 0:c0dc3a76f3d4 | 158 | int begin = 0; |
apatel336 | 0:c0dc3a76f3d4 | 159 | int end = 0; |
apatel336 | 0:c0dc3a76f3d4 | 160 | string str; |
apatel336 | 0:c0dc3a76f3d4 | 161 | string addr; |
apatel336 | 0:c0dc3a76f3d4 | 162 | int port; |
apatel336 | 0:c0dc3a76f3d4 | 163 | if (!endpoint_read) { |
apatel336 | 0:c0dc3a76f3d4 | 164 | if (!wifi->sendCommand("get ip\r", NULL, recv)) |
apatel336 | 0:c0dc3a76f3d4 | 165 | return false; |
apatel336 | 0:c0dc3a76f3d4 | 166 | wifi->exit(); |
apatel336 | 0:c0dc3a76f3d4 | 167 | str = recv; |
apatel336 | 0:c0dc3a76f3d4 | 168 | begin = str.find("HOST="); |
apatel336 | 0:c0dc3a76f3d4 | 169 | end = str.find("PROTO="); |
apatel336 | 0:c0dc3a76f3d4 | 170 | if (begin != string::npos && end != string::npos) { |
apatel336 | 0:c0dc3a76f3d4 | 171 | str = str.substr(begin + 5, end - begin - 5); |
apatel336 | 0:c0dc3a76f3d4 | 172 | int pos = str.find(":"); |
apatel336 | 0:c0dc3a76f3d4 | 173 | if (pos != string::npos) { |
apatel336 | 0:c0dc3a76f3d4 | 174 | addr = str.substr(0, pos); |
apatel336 | 0:c0dc3a76f3d4 | 175 | port = atoi(str.substr(pos + 1).c_str()); |
apatel336 | 0:c0dc3a76f3d4 | 176 | ep.set_address(addr.c_str(), port); |
apatel336 | 0:c0dc3a76f3d4 | 177 | endpoint_read = true; |
apatel336 | 0:c0dc3a76f3d4 | 178 | wifi->flush(); |
apatel336 | 0:c0dc3a76f3d4 | 179 | return true; |
apatel336 | 0:c0dc3a76f3d4 | 180 | } |
apatel336 | 0:c0dc3a76f3d4 | 181 | } |
apatel336 | 0:c0dc3a76f3d4 | 182 | wifi->flush(); |
apatel336 | 0:c0dc3a76f3d4 | 183 | } |
apatel336 | 0:c0dc3a76f3d4 | 184 | return false; |
apatel336 | 0:c0dc3a76f3d4 | 185 | } |