wifly/socket interface for wifly modules

Dependents:   WiFi neurGAI_WIFI thingspeak thingspeak2

Committer:
samux
Date:
Fri Aug 24 13:25:55 2012 +0000
Revision:
13:108340829acc
Parent:
11:b912f91e3212
reduce buffer size

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 if (_blocking) {
samux 4:74bfdd00362a 82 while (1) {
samux 4:74bfdd00362a 83 nb_available = wifi->readable();
samux 4:74bfdd00362a 84 if (nb_available != 0) {
samux 4:74bfdd00362a 85 break;
samux 4:74bfdd00362a 86 }
samux 4:74bfdd00362a 87 }
samux 4:74bfdd00362a 88 }
samux 4:74bfdd00362a 89
samux 4:74bfdd00362a 90 tmr.start();
samux 4:74bfdd00362a 91
samux 6:f281180726e8 92 while (time < _timeout) {
samux 4:74bfdd00362a 93
samux 4:74bfdd00362a 94 nb_available = wifi->readable();
samux 4:74bfdd00362a 95 for (int i = 0; i < min(nb_available, length); i++) {
samux 4:74bfdd00362a 96 buffer[idx] = wifi->getc();
samux 4:74bfdd00362a 97 idx++;
samux 4:74bfdd00362a 98 }
samux 4:74bfdd00362a 99
samux 4:74bfdd00362a 100 if (idx == length) {
samux 4:74bfdd00362a 101 break;
samux 4:74bfdd00362a 102 }
samux 6:f281180726e8 103
samux 6:f281180726e8 104 time = tmr.read_ms();
samux 4:74bfdd00362a 105 }
samux 4:74bfdd00362a 106
samux 4:74bfdd00362a 107 readEndpoint(remote);
samux 4:74bfdd00362a 108 return (idx == 0) ? -1 : idx;
samux 4:74bfdd00362a 109 }
samux 4:74bfdd00362a 110
samux 4:74bfdd00362a 111 bool UDPSocket::confEndpoint(Endpoint & ep)
samux 4:74bfdd00362a 112 {
samux 4:74bfdd00362a 113 char * host;
samux 4:74bfdd00362a 114 char cmd[30];
samux 4:74bfdd00362a 115 if (!endpoint_configured) {
samux 4:74bfdd00362a 116 host = ep.get_address();
samux 4:74bfdd00362a 117 if (host[0] != '\0') {
samux 11:b912f91e3212 118 // set host
samux 11:b912f91e3212 119 sprintf(cmd, "set i h %s\r", host);
samux 4:74bfdd00362a 120 if (!wifi->sendCommand(cmd, "AOK"))
samux 4:74bfdd00362a 121 return false;
samux 11:b912f91e3212 122
samux 11:b912f91e3212 123 // set remote port
samux 11:b912f91e3212 124 sprintf(cmd, "set i r %d\r", ep.get_port());
samux 4:74bfdd00362a 125 if (!wifi->sendCommand(cmd, "AOK"))
samux 4:74bfdd00362a 126 return false;
samux 11:b912f91e3212 127
samux 4:74bfdd00362a 128 wifi->exit();
samux 4:74bfdd00362a 129 endpoint_configured = true;
samux 4:74bfdd00362a 130 return true;
samux 4:74bfdd00362a 131 }
samux 4:74bfdd00362a 132 }
samux 4:74bfdd00362a 133 return true;
samux 4:74bfdd00362a 134 }
samux 4:74bfdd00362a 135
samux 4:74bfdd00362a 136 bool UDPSocket::readEndpoint(Endpoint & ep)
samux 4:74bfdd00362a 137 {
samux 4:74bfdd00362a 138 char recv[256];
samux 4:74bfdd00362a 139 int begin = 0;
samux 4:74bfdd00362a 140 int end = 0;
samux 4:74bfdd00362a 141 string str;
samux 4:74bfdd00362a 142 string addr;
samux 4:74bfdd00362a 143 int port;
samux 4:74bfdd00362a 144 if (!endpoint_read) {
samux 4:74bfdd00362a 145 if (!wifi->sendCommand("get ip\r", NULL, recv))
samux 4:74bfdd00362a 146 return false;
samux 4:74bfdd00362a 147 wifi->exit();
samux 4:74bfdd00362a 148 str = recv;
samux 4:74bfdd00362a 149 begin = str.find("HOST=");
samux 4:74bfdd00362a 150 end = str.find("PROTO=");
samux 4:74bfdd00362a 151 if (begin != string::npos && end != string::npos) {
samux 4:74bfdd00362a 152 str = str.substr(begin + 5, end - begin - 5);
samux 4:74bfdd00362a 153 int pos = str.find(":");
samux 4:74bfdd00362a 154 if (pos != string::npos) {
samux 4:74bfdd00362a 155 addr = str.substr(0, pos);
samux 4:74bfdd00362a 156 port = atoi(str.substr(pos + 1).c_str());
samux 4:74bfdd00362a 157 ep.set_address(addr.c_str(), port);
samux 4:74bfdd00362a 158 endpoint_read = true;
samux 4:74bfdd00362a 159 wifi->flush();
samux 4:74bfdd00362a 160 return true;
samux 4:74bfdd00362a 161 }
samux 4:74bfdd00362a 162 }
samux 4:74bfdd00362a 163 wifi->flush();
samux 4:74bfdd00362a 164 }
samux 4:74bfdd00362a 165 return false;
samux 4:74bfdd00362a 166 }