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/TCPSocketConnection.cpp@0:6ffb0aeb3972, 2012-08-17 (annotated)
- Committer:
- samux
- Date:
- Fri Aug 17 08:28:04 2012 +0000
- Revision:
- 0:6ffb0aeb3972
- Child:
- 1:8f04181f9ad8
first commit
Who changed what in which revision?
User | Revision | Line number | New 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 "TCPSocketConnection.h" |
samux | 0:6ffb0aeb3972 | 20 | #include <algorithm> |
samux | 0:6ffb0aeb3972 | 21 | |
samux | 0:6ffb0aeb3972 | 22 | TCPSocketConnection::TCPSocketConnection() {} |
samux | 0:6ffb0aeb3972 | 23 | |
samux | 0:6ffb0aeb3972 | 24 | int TCPSocketConnection::connect(const char* host, const int port) { |
samux | 0:6ffb0aeb3972 | 25 | //open the connection |
samux | 0:6ffb0aeb3972 | 26 | char cmd[30]; |
samux | 0:6ffb0aeb3972 | 27 | char rcv[40]; |
samux | 0:6ffb0aeb3972 | 28 | |
samux | 0:6ffb0aeb3972 | 29 | // if we cannot enter in cmd mode, maybe we have a tcp connection opened. |
samux | 0:6ffb0aeb3972 | 30 | // in this case we need to wait at least 0.25s to enter in cmd mode |
samux | 0:6ffb0aeb3972 | 31 | if (!wifi->cmdMode()) { |
samux | 0:6ffb0aeb3972 | 32 | wait(0.25); |
samux | 0:6ffb0aeb3972 | 33 | if (!wifi->cmdMode()) { |
samux | 0:6ffb0aeb3972 | 34 | wifi->send("\r", 1); |
samux | 0:6ffb0aeb3972 | 35 | wait(0.1); |
samux | 0:6ffb0aeb3972 | 36 | wifi->exit(); |
samux | 0:6ffb0aeb3972 | 37 | return -1; |
samux | 0:6ffb0aeb3972 | 38 | } |
samux | 0:6ffb0aeb3972 | 39 | } |
samux | 0:6ffb0aeb3972 | 40 | |
samux | 0:6ffb0aeb3972 | 41 | sprintf(cmd, "open %s %d\r", host, port); |
samux | 0:6ffb0aeb3972 | 42 | if (wifi->sendCommand(cmd, NULL, rcv)) { |
samux | 0:6ffb0aeb3972 | 43 | if (strstr(rcv, "OPEN") == NULL) { |
samux | 0:6ffb0aeb3972 | 44 | if (strstr(rcv, "Connected") != NULL) { |
samux | 0:6ffb0aeb3972 | 45 | if (!wifi->sendCommand("close\r", "CLOS")) |
samux | 0:6ffb0aeb3972 | 46 | return -1; |
samux | 0:6ffb0aeb3972 | 47 | if (!wifi->sendCommand(cmd, "OPEN")) |
samux | 0:6ffb0aeb3972 | 48 | return -1; |
samux | 0:6ffb0aeb3972 | 49 | } else { |
samux | 0:6ffb0aeb3972 | 50 | return -1; |
samux | 0:6ffb0aeb3972 | 51 | } |
samux | 0:6ffb0aeb3972 | 52 | } |
samux | 0:6ffb0aeb3972 | 53 | } else { |
samux | 0:6ffb0aeb3972 | 54 | return -1; |
samux | 0:6ffb0aeb3972 | 55 | } |
samux | 0:6ffb0aeb3972 | 56 | |
samux | 0:6ffb0aeb3972 | 57 | wifi->flush(); |
samux | 0:6ffb0aeb3972 | 58 | return 0; |
samux | 0:6ffb0aeb3972 | 59 | } |
samux | 0:6ffb0aeb3972 | 60 | |
samux | 0:6ffb0aeb3972 | 61 | bool TCPSocketConnection::is_connected(void) { |
samux | 0:6ffb0aeb3972 | 62 | return wifi->is_connected(); |
samux | 0:6ffb0aeb3972 | 63 | } |
samux | 0:6ffb0aeb3972 | 64 | |
samux | 0:6ffb0aeb3972 | 65 | int TCPSocketConnection::send(char* data, int length) { |
samux | 0:6ffb0aeb3972 | 66 | Timer tmr; |
samux | 0:6ffb0aeb3972 | 67 | |
samux | 0:6ffb0aeb3972 | 68 | if (!_blocking) { |
samux | 0:6ffb0aeb3972 | 69 | tmr.start(); |
samux | 0:6ffb0aeb3972 | 70 | while (tmr.read_ms() < _timeout) { |
samux | 0:6ffb0aeb3972 | 71 | if (wifi->writeable()) |
samux | 0:6ffb0aeb3972 | 72 | break; |
samux | 0:6ffb0aeb3972 | 73 | } |
samux | 0:6ffb0aeb3972 | 74 | if (tmr.read_ms() >= _timeout) { |
samux | 0:6ffb0aeb3972 | 75 | return -1; |
samux | 0:6ffb0aeb3972 | 76 | } |
samux | 0:6ffb0aeb3972 | 77 | } |
samux | 0:6ffb0aeb3972 | 78 | return wifi->send(data, length); |
samux | 0:6ffb0aeb3972 | 79 | } |
samux | 0:6ffb0aeb3972 | 80 | |
samux | 0:6ffb0aeb3972 | 81 | // -1 if unsuccessful, else number of bytes written |
samux | 0:6ffb0aeb3972 | 82 | int TCPSocketConnection::send_all(char* data, int length) { |
samux | 0:6ffb0aeb3972 | 83 | Timer tmr; |
samux | 0:6ffb0aeb3972 | 84 | int idx = 0; |
samux | 0:6ffb0aeb3972 | 85 | tmr.start(); |
samux | 0:6ffb0aeb3972 | 86 | |
samux | 0:6ffb0aeb3972 | 87 | while ((tmr.read_ms() < _timeout) || _blocking) { |
samux | 0:6ffb0aeb3972 | 88 | |
samux | 0:6ffb0aeb3972 | 89 | idx += wifi->send(data, length); |
samux | 0:6ffb0aeb3972 | 90 | |
samux | 0:6ffb0aeb3972 | 91 | if (idx == length) |
samux | 0:6ffb0aeb3972 | 92 | return idx; |
samux | 0:6ffb0aeb3972 | 93 | } |
samux | 0:6ffb0aeb3972 | 94 | return (idx == 0) ? -1 : idx; |
samux | 0:6ffb0aeb3972 | 95 | } |
samux | 0:6ffb0aeb3972 | 96 | |
samux | 0:6ffb0aeb3972 | 97 | // -1 if unsuccessful, else number of bytes received |
samux | 0:6ffb0aeb3972 | 98 | int TCPSocketConnection::receive(char* data, int length) { |
samux | 0:6ffb0aeb3972 | 99 | Timer tmr; |
samux | 0:6ffb0aeb3972 | 100 | |
samux | 0:6ffb0aeb3972 | 101 | if (!_blocking) { |
samux | 0:6ffb0aeb3972 | 102 | tmr.start(); |
samux | 0:6ffb0aeb3972 | 103 | while (tmr.read_ms() < _timeout) { |
samux | 0:6ffb0aeb3972 | 104 | if (wifi->readable()) |
samux | 0:6ffb0aeb3972 | 105 | break; |
samux | 0:6ffb0aeb3972 | 106 | } |
samux | 0:6ffb0aeb3972 | 107 | if (tmr.read_ms() >= _timeout) |
samux | 0:6ffb0aeb3972 | 108 | return -1; |
samux | 0:6ffb0aeb3972 | 109 | } |
samux | 0:6ffb0aeb3972 | 110 | |
samux | 0:6ffb0aeb3972 | 111 | while(!wifi->readable()); |
samux | 0:6ffb0aeb3972 | 112 | int nb_available = wifi->readable(); |
samux | 0:6ffb0aeb3972 | 113 | for (int i = 0; i < min(nb_available, length); i++) { |
samux | 0:6ffb0aeb3972 | 114 | data[i] = wifi->getc(); |
samux | 0:6ffb0aeb3972 | 115 | } |
samux | 0:6ffb0aeb3972 | 116 | return min(nb_available, length); |
samux | 0:6ffb0aeb3972 | 117 | } |
samux | 0:6ffb0aeb3972 | 118 | |
samux | 0:6ffb0aeb3972 | 119 | |
samux | 0:6ffb0aeb3972 | 120 | // -1 if unsuccessful, else number of bytes received |
samux | 0:6ffb0aeb3972 | 121 | int TCPSocketConnection::receive_all(char* data, int length) { |
samux | 0:6ffb0aeb3972 | 122 | Timer tmr; |
samux | 0:6ffb0aeb3972 | 123 | int idx = 0; |
samux | 0:6ffb0aeb3972 | 124 | |
samux | 0:6ffb0aeb3972 | 125 | tmr.start(); |
samux | 0:6ffb0aeb3972 | 126 | |
samux | 0:6ffb0aeb3972 | 127 | while (tmr.read_ms() < _timeout || _blocking) { |
samux | 0:6ffb0aeb3972 | 128 | |
samux | 0:6ffb0aeb3972 | 129 | int nb_available = wifi->readable(); |
samux | 0:6ffb0aeb3972 | 130 | for (int i = 0; i < min(nb_available, length); i++) { |
samux | 0:6ffb0aeb3972 | 131 | data[idx++] = wifi->getc(); |
samux | 0:6ffb0aeb3972 | 132 | } |
samux | 0:6ffb0aeb3972 | 133 | |
samux | 0:6ffb0aeb3972 | 134 | if (idx == length) |
samux | 0:6ffb0aeb3972 | 135 | return idx; |
samux | 0:6ffb0aeb3972 | 136 | } |
samux | 0:6ffb0aeb3972 | 137 | |
samux | 0:6ffb0aeb3972 | 138 | return (idx == 0) ? -1 : idx; |
samux | 0:6ffb0aeb3972 | 139 | } |