Samuel Mokrani / WiflyInterface

Dependents:   WiFi neurGAI_WIFI thingspeak thingspeak2

Committer:
samux
Date:
Fri Aug 24 13:07:01 2012 +0000
Revision:
11:b912f91e3212
Parent:
6:f281180726e8
Child:
13:108340829acc
add state data structure

Who changed what in which revision?

UserRevisionLine numberNew 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 11:b912f91e3212 32 wifi->setProtocol(UDP);
samux 4:74bfdd00362a 33 wifi->exit();
samux 4:74bfdd00362a 34 return 0;
samux 4:74bfdd00362a 35 }
samux 4:74bfdd00362a 36
samux 4:74bfdd00362a 37 // Server initialization
samux 4:74bfdd00362a 38 int UDPSocket::bind(int port)
samux 4:74bfdd00362a 39 {
samux 11:b912f91e3212 40 char cmd[17];
samux 11:b912f91e3212 41
samux 11:b912f91e3212 42 // set udp protocol
samux 11:b912f91e3212 43 wifi->setProtocol(UDP);
samux 11:b912f91e3212 44
samux 11:b912f91e3212 45 // set local port
samux 11:b912f91e3212 46 sprintf(cmd, "set i l %d\r", port);
samux 4:74bfdd00362a 47 if (!wifi->sendCommand(cmd, "AOK"))
samux 4:74bfdd00362a 48 return -1;
samux 4:74bfdd00362a 49 wifi->exit();
samux 4:74bfdd00362a 50 return 0;
samux 4:74bfdd00362a 51 }
samux 4:74bfdd00362a 52
samux 4:74bfdd00362a 53 // -1 if unsuccessful, else number of bytes written
samux 4:74bfdd00362a 54 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
samux 4:74bfdd00362a 55 {
samux 4:74bfdd00362a 56 Timer tmr;
samux 4:74bfdd00362a 57 int idx = 0;
samux 4:74bfdd00362a 58
samux 4:74bfdd00362a 59 confEndpoint(remote);
samux 4:74bfdd00362a 60
samux 4:74bfdd00362a 61 tmr.start();
samux 4:74bfdd00362a 62
samux 4:74bfdd00362a 63 while ((tmr.read_ms() < _timeout) || _blocking) {
samux 4:74bfdd00362a 64
samux 4:74bfdd00362a 65 idx += wifi->send(packet, length);
samux 4:74bfdd00362a 66
samux 4:74bfdd00362a 67 if (idx == length)
samux 4:74bfdd00362a 68 return idx;
samux 4:74bfdd00362a 69 }
samux 4:74bfdd00362a 70 return (idx == 0) ? -1 : idx;
samux 4:74bfdd00362a 71 }
samux 4:74bfdd00362a 72
samux 4:74bfdd00362a 73 // -1 if unsuccessful, else number of bytes received
samux 4:74bfdd00362a 74 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
samux 4:74bfdd00362a 75 {
samux 4:74bfdd00362a 76 Timer tmr;
samux 4:74bfdd00362a 77 int idx = 0;
samux 4:74bfdd00362a 78 int nb_available = 0;
samux 6:f281180726e8 79 int time = -1;
samux 4:74bfdd00362a 80
samux 4:74bfdd00362a 81
samux 4:74bfdd00362a 82
samux 4:74bfdd00362a 83 if (_blocking) {
samux 4:74bfdd00362a 84 while (1) {
samux 4:74bfdd00362a 85 nb_available = wifi->readable();
samux 4:74bfdd00362a 86 if (nb_available != 0) {
samux 4:74bfdd00362a 87 break;
samux 4:74bfdd00362a 88 }
samux 4:74bfdd00362a 89 }
samux 4:74bfdd00362a 90 }
samux 4:74bfdd00362a 91
samux 4:74bfdd00362a 92 tmr.start();
samux 4:74bfdd00362a 93
samux 6:f281180726e8 94 while (time < _timeout) {
samux 4:74bfdd00362a 95
samux 4:74bfdd00362a 96 nb_available = wifi->readable();
samux 4:74bfdd00362a 97 for (int i = 0; i < min(nb_available, length); i++) {
samux 4:74bfdd00362a 98 buffer[idx] = wifi->getc();
samux 4:74bfdd00362a 99 idx++;
samux 4:74bfdd00362a 100 }
samux 4:74bfdd00362a 101
samux 4:74bfdd00362a 102 if (idx == length) {
samux 4:74bfdd00362a 103 break;
samux 4:74bfdd00362a 104 }
samux 6:f281180726e8 105
samux 6:f281180726e8 106 time = tmr.read_ms();
samux 4:74bfdd00362a 107 }
samux 4:74bfdd00362a 108
samux 4:74bfdd00362a 109 readEndpoint(remote);
samux 4:74bfdd00362a 110 return (idx == 0) ? -1 : idx;
samux 4:74bfdd00362a 111 }
samux 4:74bfdd00362a 112
samux 4:74bfdd00362a 113 bool UDPSocket::confEndpoint(Endpoint & ep)
samux 4:74bfdd00362a 114 {
samux 4:74bfdd00362a 115 char * host;
samux 4:74bfdd00362a 116 char cmd[30];
samux 4:74bfdd00362a 117 if (!endpoint_configured) {
samux 4:74bfdd00362a 118 host = ep.get_address();
samux 4:74bfdd00362a 119 if (host[0] != '\0') {
samux 11:b912f91e3212 120 // set host
samux 11:b912f91e3212 121 sprintf(cmd, "set i h %s\r", host);
samux 4:74bfdd00362a 122 if (!wifi->sendCommand(cmd, "AOK"))
samux 4:74bfdd00362a 123 return false;
samux 11:b912f91e3212 124
samux 11:b912f91e3212 125 // set remote port
samux 11:b912f91e3212 126 sprintf(cmd, "set i r %d\r", ep.get_port());
samux 4:74bfdd00362a 127 if (!wifi->sendCommand(cmd, "AOK"))
samux 4:74bfdd00362a 128 return false;
samux 11:b912f91e3212 129
samux 4:74bfdd00362a 130 wifi->exit();
samux 4:74bfdd00362a 131 endpoint_configured = true;
samux 4:74bfdd00362a 132 return true;
samux 4:74bfdd00362a 133 }
samux 4:74bfdd00362a 134 }
samux 4:74bfdd00362a 135 return true;
samux 4:74bfdd00362a 136 }
samux 4:74bfdd00362a 137
samux 4:74bfdd00362a 138 bool UDPSocket::readEndpoint(Endpoint & ep)
samux 4:74bfdd00362a 139 {
samux 4:74bfdd00362a 140 char recv[256];
samux 4:74bfdd00362a 141 int begin = 0;
samux 4:74bfdd00362a 142 int end = 0;
samux 4:74bfdd00362a 143 string str;
samux 4:74bfdd00362a 144 string addr;
samux 4:74bfdd00362a 145 int port;
samux 4:74bfdd00362a 146 if (!endpoint_read) {
samux 4:74bfdd00362a 147 if (!wifi->sendCommand("get ip\r", NULL, recv))
samux 4:74bfdd00362a 148 return false;
samux 4:74bfdd00362a 149 wifi->exit();
samux 4:74bfdd00362a 150 str = recv;
samux 4:74bfdd00362a 151 begin = str.find("HOST=");
samux 4:74bfdd00362a 152 end = str.find("PROTO=");
samux 4:74bfdd00362a 153 if (begin != string::npos && end != string::npos) {
samux 4:74bfdd00362a 154 str = str.substr(begin + 5, end - begin - 5);
samux 4:74bfdd00362a 155 int pos = str.find(":");
samux 4:74bfdd00362a 156 if (pos != string::npos) {
samux 4:74bfdd00362a 157 addr = str.substr(0, pos);
samux 4:74bfdd00362a 158 port = atoi(str.substr(pos + 1).c_str());
samux 4:74bfdd00362a 159 ep.set_address(addr.c_str(), port);
samux 4:74bfdd00362a 160 endpoint_read = true;
samux 4:74bfdd00362a 161 wifi->flush();
samux 4:74bfdd00362a 162 return true;
samux 4:74bfdd00362a 163 }
samux 4:74bfdd00362a 164 }
samux 4:74bfdd00362a 165 wifi->flush();
samux 4:74bfdd00362a 166 }
samux 4:74bfdd00362a 167 return false;
samux 4:74bfdd00362a 168 }