wifly/socket interface for wifly modules

Dependents:   WiFi neurGAI_WIFI thingspeak thingspeak2

Committer:
samux
Date:
Thu Aug 23 10:32:45 2012 +0000
Revision:
4:74bfdd00362a
Parent:
0:6ffb0aeb3972
Child:
6:f281180726e8
no reboot needed/add timeout param in send function

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