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