Samuel Mokrani / WiflyInterface

Dependents:   WiFi neurGAI_WIFI thingspeak thingspeak2

Committer:
samux
Date:
Fri Aug 17 08:28:04 2012 +0000
Revision:
0:6ffb0aeb3972
Child:
4:74bfdd00362a
first commit

Who changed what in which revision?

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