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 "TCPSocketServer.h"
samux 4:74bfdd00362a 20 #include <string>
samux 4:74bfdd00362a 21
samux 4:74bfdd00362a 22 TCPSocketServer::TCPSocketServer() {}
samux 4:74bfdd00362a 23
samux 4:74bfdd00362a 24 // Server initialization
samux 4:74bfdd00362a 25 int TCPSocketServer::bind(int port) {
samux 4:74bfdd00362a 26 char cmd[20];
samux 13:108340829acc 27
samux 13:108340829acc 28 // set TCP protocol
samux 11:b912f91e3212 29 wifi->setProtocol(TCP);
samux 13:108340829acc 30
samux 13:108340829acc 31 // set local port
samux 13:108340829acc 32 sprintf(cmd, "set i l %d\r", port);
samux 4:74bfdd00362a 33 if (!wifi->sendCommand(cmd, "AOK"))
samux 4:74bfdd00362a 34 return -1;
samux 13:108340829acc 35
samux 4:74bfdd00362a 36 wifi->exit();
samux 4:74bfdd00362a 37 return 0;
samux 4:74bfdd00362a 38 }
samux 4:74bfdd00362a 39
samux 4:74bfdd00362a 40 int TCPSocketServer::listen(int backlog) {
samux 4:74bfdd00362a 41 if (backlog != 1)
samux 4:74bfdd00362a 42 return -1;
samux 4:74bfdd00362a 43 return 0;
samux 4:74bfdd00362a 44 }
samux 4:74bfdd00362a 45
samux 4:74bfdd00362a 46
samux 4:74bfdd00362a 47 int TCPSocketServer::accept(TCPSocketConnection& connection) {
samux 4:74bfdd00362a 48 int nb_available = 0, pos = 0;
samux 4:74bfdd00362a 49 char c;
samux 4:74bfdd00362a 50 string str;
samux 4:74bfdd00362a 51 bool o_find = false;
samux 4:74bfdd00362a 52 while (1) {
samux 4:74bfdd00362a 53 while(!wifi->readable());
samux 4:74bfdd00362a 54 nb_available = wifi->readable();
samux 4:74bfdd00362a 55 for (int i = 0; i < nb_available; i++) {
samux 4:74bfdd00362a 56 c = wifi->getc();
samux 4:74bfdd00362a 57 if (c == '*') {
samux 4:74bfdd00362a 58 o_find = true;
samux 4:74bfdd00362a 59 }
samux 4:74bfdd00362a 60 if (o_find && c != '\r' && c != '\n') {
samux 4:74bfdd00362a 61 str += c;
samux 4:74bfdd00362a 62 pos = str.find("*OPEN*");
samux 4:74bfdd00362a 63 if (pos != string::npos) {
samux 4:74bfdd00362a 64 wifi->flush();
samux 4:74bfdd00362a 65 return 0;
samux 4:74bfdd00362a 66 }
samux 4:74bfdd00362a 67 }
samux 4:74bfdd00362a 68 }
samux 4:74bfdd00362a 69 }
samux 0:6ffb0aeb3972 70 }