ACTUALIZED LIBRARY 0 Open (Default) 1 WEP-128 2 WPA1 3 Mixed WPA1 & WPA2-PSK 4 WPA2-PSK 5 Not Used 6 Adhoc, Join any Adhoc network
Dependents: mbed_Arduino_sensor
Fork of WiflyInterface by
Socket/TCPSocketServer.cpp@4:74bfdd00362a, 2012-08-23 (annotated)
- Committer:
- samux
- Date:
- Thu Aug 23 10:32:45 2012 +0000
- Revision:
- 4:74bfdd00362a
- Parent:
- 0:6ffb0aeb3972
no reboot needed/add timeout param in send function
Who changed what in which revision?
User | Revision | Line number | New 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 | // use udp auto pairing |
samux | 4:74bfdd00362a | 27 | char cmd[20]; |
samux | 4:74bfdd00362a | 28 | if (!wifi->sendCommand("set ip proto 2\r", "AOK")) |
samux | 4:74bfdd00362a | 29 | return -1; |
samux | 4:74bfdd00362a | 30 | if (!wifi->sendCommand("set ip flags 0x07\r", "AOK")) |
samux | 4:74bfdd00362a | 31 | return -1; |
samux | 4:74bfdd00362a | 32 | sprintf(cmd, "set ip local %d\r", port); |
samux | 4:74bfdd00362a | 33 | if (!wifi->sendCommand(cmd, "AOK")) |
samux | 4:74bfdd00362a | 34 | return -1; |
samux | 4:74bfdd00362a | 35 | if (!wifi->sendCommand("save\r", "Stor")) |
samux | 4:74bfdd00362a | 36 | return -1; |
samux | 4:74bfdd00362a | 37 | wifi->exit(); |
samux | 4:74bfdd00362a | 38 | return 0; |
samux | 4:74bfdd00362a | 39 | } |
samux | 4:74bfdd00362a | 40 | |
samux | 4:74bfdd00362a | 41 | int TCPSocketServer::listen(int backlog) { |
samux | 4:74bfdd00362a | 42 | if (backlog != 1) |
samux | 4:74bfdd00362a | 43 | return -1; |
samux | 4:74bfdd00362a | 44 | return 0; |
samux | 4:74bfdd00362a | 45 | } |
samux | 4:74bfdd00362a | 46 | |
samux | 4:74bfdd00362a | 47 | |
samux | 4:74bfdd00362a | 48 | int TCPSocketServer::accept(TCPSocketConnection& connection) { |
samux | 4:74bfdd00362a | 49 | int nb_available = 0, pos = 0; |
samux | 4:74bfdd00362a | 50 | char c; |
samux | 4:74bfdd00362a | 51 | string str; |
samux | 4:74bfdd00362a | 52 | bool o_find = false; |
samux | 4:74bfdd00362a | 53 | while (1) { |
samux | 4:74bfdd00362a | 54 | while(!wifi->readable()); |
samux | 4:74bfdd00362a | 55 | nb_available = wifi->readable(); |
samux | 4:74bfdd00362a | 56 | for (int i = 0; i < nb_available; i++) { |
samux | 4:74bfdd00362a | 57 | c = wifi->getc(); |
samux | 4:74bfdd00362a | 58 | if (c == '*') { |
samux | 4:74bfdd00362a | 59 | o_find = true; |
samux | 4:74bfdd00362a | 60 | } |
samux | 4:74bfdd00362a | 61 | if (o_find && c != '\r' && c != '\n') { |
samux | 4:74bfdd00362a | 62 | str += c; |
samux | 4:74bfdd00362a | 63 | pos = str.find("*OPEN*"); |
samux | 4:74bfdd00362a | 64 | if (pos != string::npos) { |
samux | 4:74bfdd00362a | 65 | wifi->flush(); |
samux | 4:74bfdd00362a | 66 | return 0; |
samux | 4:74bfdd00362a | 67 | } |
samux | 4:74bfdd00362a | 68 | } |
samux | 4:74bfdd00362a | 69 | } |
samux | 4:74bfdd00362a | 70 | } |
samux | 0:6ffb0aeb3972 | 71 | } |