wifly/socket interface for wifly modules
Dependents: WiFi neurGAI_WIFI thingspeak thingspeak2
Socket/TCPSocketConnection.cpp@9:aeddb7353e6e, 2012-08-23 (annotated)
- Committer:
- samux
- Date:
- Thu Aug 23 15:18:48 2012 +0000
- Revision:
- 9:aeddb7353e6e
- Parent:
- 6:f281180726e8
- Child:
- 11:b912f91e3212
no cmd mode on connect
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:8f04181f9ad8 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
samux | 1:8f04181f9ad8 | 2 | * |
samux | 1:8f04181f9ad8 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 1:8f04181f9ad8 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
samux | 1:8f04181f9ad8 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
samux | 1:8f04181f9ad8 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
samux | 1:8f04181f9ad8 | 7 | * furnished to do so, subject to the following conditions: |
samux | 1:8f04181f9ad8 | 8 | * |
samux | 1:8f04181f9ad8 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 1:8f04181f9ad8 | 10 | * substantial portions of the Software. |
samux | 1:8f04181f9ad8 | 11 | * |
samux | 1:8f04181f9ad8 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 1:8f04181f9ad8 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 1:8f04181f9ad8 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 1:8f04181f9ad8 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 1:8f04181f9ad8 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 1:8f04181f9ad8 | 17 | */ |
samux | 1:8f04181f9ad8 | 18 | |
samux | 1:8f04181f9ad8 | 19 | #include "TCPSocketConnection.h" |
samux | 1:8f04181f9ad8 | 20 | #include <algorithm> |
samux | 1:8f04181f9ad8 | 21 | |
samux | 1:8f04181f9ad8 | 22 | TCPSocketConnection::TCPSocketConnection() {} |
samux | 1:8f04181f9ad8 | 23 | |
samux | 1:8f04181f9ad8 | 24 | int TCPSocketConnection::connect(const char* host, const int port) |
samux | 1:8f04181f9ad8 | 25 | { |
samux | 1:8f04181f9ad8 | 26 | //open the connection |
samux | 1:8f04181f9ad8 | 27 | char cmd[30]; |
samux | 1:8f04181f9ad8 | 28 | char rcv[40]; |
samux | 1:8f04181f9ad8 | 29 | |
samux | 1:8f04181f9ad8 | 30 | if (wifi->dnsLookup(host, rcv)) { |
samux | 1:8f04181f9ad8 | 31 | sprintf(cmd, "set ip host %s\r", rcv); |
samux | 1:8f04181f9ad8 | 32 | if (!wifi->sendCommand(cmd, "AOK")) |
samux | 1:8f04181f9ad8 | 33 | return -1; |
samux | 1:8f04181f9ad8 | 34 | } else { |
samux | 1:8f04181f9ad8 | 35 | return -1; |
samux | 1:8f04181f9ad8 | 36 | } |
samux | 1:8f04181f9ad8 | 37 | |
samux | 1:8f04181f9ad8 | 38 | sprintf(cmd, "set ip remote %d\r", port); |
samux | 1:8f04181f9ad8 | 39 | if (!wifi->sendCommand(cmd, "AOK")) |
samux | 1:8f04181f9ad8 | 40 | return -1; |
samux | 1:8f04181f9ad8 | 41 | |
samux | 1:8f04181f9ad8 | 42 | if (wifi->sendCommand("open\r", NULL, rcv)) { |
samux | 1:8f04181f9ad8 | 43 | if (strstr(rcv, "OPEN") == NULL) { |
samux | 1:8f04181f9ad8 | 44 | if (strstr(rcv, "Connected") != NULL) { |
samux | 1:8f04181f9ad8 | 45 | if (!wifi->sendCommand("close\r", "CLOS")) |
samux | 1:8f04181f9ad8 | 46 | return -1; |
samux | 1:8f04181f9ad8 | 47 | if (!wifi->sendCommand(cmd, "OPEN")) |
samux | 1:8f04181f9ad8 | 48 | return -1; |
samux | 1:8f04181f9ad8 | 49 | } else { |
samux | 1:8f04181f9ad8 | 50 | return -1; |
samux | 1:8f04181f9ad8 | 51 | } |
samux | 1:8f04181f9ad8 | 52 | } |
samux | 1:8f04181f9ad8 | 53 | } else { |
samux | 1:8f04181f9ad8 | 54 | return -1; |
samux | 1:8f04181f9ad8 | 55 | } |
samux | 1:8f04181f9ad8 | 56 | |
samux | 1:8f04181f9ad8 | 57 | wifi->flush(); |
samux | 1:8f04181f9ad8 | 58 | return 0; |
samux | 1:8f04181f9ad8 | 59 | } |
samux | 1:8f04181f9ad8 | 60 | |
samux | 1:8f04181f9ad8 | 61 | bool TCPSocketConnection::is_connected(void) |
samux | 1:8f04181f9ad8 | 62 | { |
samux | 1:8f04181f9ad8 | 63 | return wifi->is_connected(); |
samux | 1:8f04181f9ad8 | 64 | } |
samux | 1:8f04181f9ad8 | 65 | |
samux | 1:8f04181f9ad8 | 66 | int TCPSocketConnection::send(char* data, int length) |
samux | 1:8f04181f9ad8 | 67 | { |
samux | 1:8f04181f9ad8 | 68 | Timer tmr; |
samux | 1:8f04181f9ad8 | 69 | |
samux | 1:8f04181f9ad8 | 70 | if (!_blocking) { |
samux | 1:8f04181f9ad8 | 71 | tmr.start(); |
samux | 1:8f04181f9ad8 | 72 | while (tmr.read_ms() < _timeout) { |
samux | 1:8f04181f9ad8 | 73 | if (wifi->writeable()) |
samux | 1:8f04181f9ad8 | 74 | break; |
samux | 1:8f04181f9ad8 | 75 | } |
samux | 1:8f04181f9ad8 | 76 | if (tmr.read_ms() >= _timeout) { |
samux | 1:8f04181f9ad8 | 77 | return -1; |
samux | 1:8f04181f9ad8 | 78 | } |
samux | 1:8f04181f9ad8 | 79 | } |
samux | 1:8f04181f9ad8 | 80 | return wifi->send(data, length); |
samux | 1:8f04181f9ad8 | 81 | } |
samux | 1:8f04181f9ad8 | 82 | |
samux | 1:8f04181f9ad8 | 83 | // -1 if unsuccessful, else number of bytes written |
samux | 1:8f04181f9ad8 | 84 | int TCPSocketConnection::send_all(char* data, int length) |
samux | 1:8f04181f9ad8 | 85 | { |
samux | 1:8f04181f9ad8 | 86 | Timer tmr; |
samux | 1:8f04181f9ad8 | 87 | int idx = 0; |
samux | 1:8f04181f9ad8 | 88 | tmr.start(); |
samux | 1:8f04181f9ad8 | 89 | |
samux | 1:8f04181f9ad8 | 90 | while ((tmr.read_ms() < _timeout) || _blocking) { |
samux | 1:8f04181f9ad8 | 91 | |
samux | 1:8f04181f9ad8 | 92 | idx += wifi->send(data, length); |
samux | 1:8f04181f9ad8 | 93 | |
samux | 1:8f04181f9ad8 | 94 | if (idx == length) |
samux | 1:8f04181f9ad8 | 95 | return idx; |
samux | 1:8f04181f9ad8 | 96 | } |
samux | 1:8f04181f9ad8 | 97 | return (idx == 0) ? -1 : idx; |
samux | 1:8f04181f9ad8 | 98 | } |
samux | 1:8f04181f9ad8 | 99 | |
samux | 1:8f04181f9ad8 | 100 | // -1 if unsuccessful, else number of bytes received |
samux | 1:8f04181f9ad8 | 101 | int TCPSocketConnection::receive(char* data, int length) |
samux | 1:8f04181f9ad8 | 102 | { |
samux | 1:8f04181f9ad8 | 103 | Timer tmr; |
samux | 6:f281180726e8 | 104 | int time = -1; |
samux | 6:f281180726e8 | 105 | |
samux | 6:f281180726e8 | 106 | |
samux | 1:8f04181f9ad8 | 107 | if (!_blocking) { |
samux | 1:8f04181f9ad8 | 108 | tmr.start(); |
samux | 6:f281180726e8 | 109 | while (time < _timeout + 20) { |
samux | 6:f281180726e8 | 110 | if (wifi->readable()) { |
samux | 1:8f04181f9ad8 | 111 | break; |
samux | 6:f281180726e8 | 112 | } |
samux | 6:f281180726e8 | 113 | time = tmr.read_ms(); |
samux | 1:8f04181f9ad8 | 114 | } |
samux | 6:f281180726e8 | 115 | if (time >= _timeout + 20) { |
samux | 1:8f04181f9ad8 | 116 | return -1; |
samux | 6:f281180726e8 | 117 | } |
samux | 1:8f04181f9ad8 | 118 | } |
samux | 1:8f04181f9ad8 | 119 | |
samux | 6:f281180726e8 | 120 | |
samux | 1:8f04181f9ad8 | 121 | while(!wifi->readable()); |
samux | 1:8f04181f9ad8 | 122 | int nb_available = wifi->readable(); |
samux | 1:8f04181f9ad8 | 123 | for (int i = 0; i < min(nb_available, length); i++) { |
samux | 1:8f04181f9ad8 | 124 | data[i] = wifi->getc(); |
samux | 1:8f04181f9ad8 | 125 | } |
samux | 6:f281180726e8 | 126 | |
samux | 1:8f04181f9ad8 | 127 | return min(nb_available, length); |
samux | 1:8f04181f9ad8 | 128 | } |
samux | 1:8f04181f9ad8 | 129 | |
samux | 1:8f04181f9ad8 | 130 | |
samux | 1:8f04181f9ad8 | 131 | // -1 if unsuccessful, else number of bytes received |
samux | 1:8f04181f9ad8 | 132 | int TCPSocketConnection::receive_all(char* data, int length) |
samux | 1:8f04181f9ad8 | 133 | { |
samux | 1:8f04181f9ad8 | 134 | Timer tmr; |
samux | 1:8f04181f9ad8 | 135 | int idx = 0; |
samux | 6:f281180726e8 | 136 | int time = -1; |
samux | 1:8f04181f9ad8 | 137 | |
samux | 1:8f04181f9ad8 | 138 | tmr.start(); |
samux | 6:f281180726e8 | 139 | |
samux | 6:f281180726e8 | 140 | while (time < _timeout || _blocking) { |
samux | 1:8f04181f9ad8 | 141 | |
samux | 1:8f04181f9ad8 | 142 | int nb_available = wifi->readable(); |
samux | 1:8f04181f9ad8 | 143 | for (int i = 0; i < min(nb_available, length); i++) { |
samux | 1:8f04181f9ad8 | 144 | data[idx++] = wifi->getc(); |
samux | 1:8f04181f9ad8 | 145 | } |
samux | 1:8f04181f9ad8 | 146 | |
samux | 1:8f04181f9ad8 | 147 | if (idx == length) |
samux | 6:f281180726e8 | 148 | break; |
samux | 6:f281180726e8 | 149 | |
samux | 6:f281180726e8 | 150 | time = tmr.read_ms(); |
samux | 1:8f04181f9ad8 | 151 | } |
samux | 1:8f04181f9ad8 | 152 | |
samux | 1:8f04181f9ad8 | 153 | return (idx == 0) ? -1 : idx; |
samux | 1:8f04181f9ad8 | 154 | } |