wifly/socket interface for wifly modules

Dependents:   WiFi neurGAI_WIFI thingspeak thingspeak2

Committer:
samux
Date:
Wed Aug 22 15:52:29 2012 +0000
Revision:
1:8f04181f9ad8
Parent:
0:6ffb0aeb3972
Child:
6:f281180726e8
add dnsLookup method

Who changed what in which revision?

UserRevisionLine numberNew 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 we cannot enter in cmd mode, maybe we have a tcp connection opened.
samux 1:8f04181f9ad8 31 // in this case we need to wait at least 0.25s to enter in cmd mode
samux 1:8f04181f9ad8 32 if (!wifi->cmdMode()) {
samux 1:8f04181f9ad8 33 wait(0.25);
samux 1:8f04181f9ad8 34 if (!wifi->cmdMode()) {
samux 1:8f04181f9ad8 35 wifi->send("\r", 1);
samux 1:8f04181f9ad8 36 wait(0.1);
samux 1:8f04181f9ad8 37 wifi->exit();
samux 1:8f04181f9ad8 38 return -1;
samux 1:8f04181f9ad8 39 }
samux 1:8f04181f9ad8 40 }
samux 1:8f04181f9ad8 41
samux 1:8f04181f9ad8 42 if (wifi->dnsLookup(host, rcv)) {
samux 1:8f04181f9ad8 43 sprintf(cmd, "set ip host %s\r", rcv);
samux 1:8f04181f9ad8 44 if (!wifi->sendCommand(cmd, "AOK"))
samux 1:8f04181f9ad8 45 return -1;
samux 1:8f04181f9ad8 46 } else {
samux 1:8f04181f9ad8 47 return -1;
samux 1:8f04181f9ad8 48 }
samux 1:8f04181f9ad8 49
samux 1:8f04181f9ad8 50 sprintf(cmd, "set ip remote %d\r", port);
samux 1:8f04181f9ad8 51 if (!wifi->sendCommand(cmd, "AOK"))
samux 1:8f04181f9ad8 52 return -1;
samux 1:8f04181f9ad8 53
samux 1:8f04181f9ad8 54 if (wifi->sendCommand("open\r", NULL, rcv)) {
samux 1:8f04181f9ad8 55 if (strstr(rcv, "OPEN") == NULL) {
samux 1:8f04181f9ad8 56 if (strstr(rcv, "Connected") != NULL) {
samux 1:8f04181f9ad8 57 if (!wifi->sendCommand("close\r", "CLOS"))
samux 1:8f04181f9ad8 58 return -1;
samux 1:8f04181f9ad8 59 if (!wifi->sendCommand(cmd, "OPEN"))
samux 1:8f04181f9ad8 60 return -1;
samux 1:8f04181f9ad8 61 } else {
samux 1:8f04181f9ad8 62 return -1;
samux 1:8f04181f9ad8 63 }
samux 1:8f04181f9ad8 64 }
samux 1:8f04181f9ad8 65 } else {
samux 1:8f04181f9ad8 66 return -1;
samux 1:8f04181f9ad8 67 }
samux 1:8f04181f9ad8 68
samux 1:8f04181f9ad8 69 wifi->flush();
samux 1:8f04181f9ad8 70 return 0;
samux 1:8f04181f9ad8 71 }
samux 1:8f04181f9ad8 72
samux 1:8f04181f9ad8 73 bool TCPSocketConnection::is_connected(void)
samux 1:8f04181f9ad8 74 {
samux 1:8f04181f9ad8 75 return wifi->is_connected();
samux 1:8f04181f9ad8 76 }
samux 1:8f04181f9ad8 77
samux 1:8f04181f9ad8 78 int TCPSocketConnection::send(char* data, int length)
samux 1:8f04181f9ad8 79 {
samux 1:8f04181f9ad8 80 Timer tmr;
samux 1:8f04181f9ad8 81
samux 1:8f04181f9ad8 82 if (!_blocking) {
samux 1:8f04181f9ad8 83 tmr.start();
samux 1:8f04181f9ad8 84 while (tmr.read_ms() < _timeout) {
samux 1:8f04181f9ad8 85 if (wifi->writeable())
samux 1:8f04181f9ad8 86 break;
samux 1:8f04181f9ad8 87 }
samux 1:8f04181f9ad8 88 if (tmr.read_ms() >= _timeout) {
samux 1:8f04181f9ad8 89 return -1;
samux 1:8f04181f9ad8 90 }
samux 1:8f04181f9ad8 91 }
samux 1:8f04181f9ad8 92 return wifi->send(data, length);
samux 1:8f04181f9ad8 93 }
samux 1:8f04181f9ad8 94
samux 1:8f04181f9ad8 95 // -1 if unsuccessful, else number of bytes written
samux 1:8f04181f9ad8 96 int TCPSocketConnection::send_all(char* data, int length)
samux 1:8f04181f9ad8 97 {
samux 1:8f04181f9ad8 98 Timer tmr;
samux 1:8f04181f9ad8 99 int idx = 0;
samux 1:8f04181f9ad8 100 tmr.start();
samux 1:8f04181f9ad8 101
samux 1:8f04181f9ad8 102 while ((tmr.read_ms() < _timeout) || _blocking) {
samux 1:8f04181f9ad8 103
samux 1:8f04181f9ad8 104 idx += wifi->send(data, length);
samux 1:8f04181f9ad8 105
samux 1:8f04181f9ad8 106 if (idx == length)
samux 1:8f04181f9ad8 107 return idx;
samux 1:8f04181f9ad8 108 }
samux 1:8f04181f9ad8 109 return (idx == 0) ? -1 : idx;
samux 1:8f04181f9ad8 110 }
samux 1:8f04181f9ad8 111
samux 1:8f04181f9ad8 112 // -1 if unsuccessful, else number of bytes received
samux 1:8f04181f9ad8 113 int TCPSocketConnection::receive(char* data, int length)
samux 1:8f04181f9ad8 114 {
samux 1:8f04181f9ad8 115 Timer tmr;
samux 1:8f04181f9ad8 116
samux 1:8f04181f9ad8 117 if (!_blocking) {
samux 1:8f04181f9ad8 118 tmr.start();
samux 1:8f04181f9ad8 119 while (tmr.read_ms() < _timeout) {
samux 1:8f04181f9ad8 120 if (wifi->readable())
samux 1:8f04181f9ad8 121 break;
samux 1:8f04181f9ad8 122 }
samux 1:8f04181f9ad8 123 if (tmr.read_ms() >= _timeout)
samux 1:8f04181f9ad8 124 return -1;
samux 1:8f04181f9ad8 125 }
samux 1:8f04181f9ad8 126
samux 1:8f04181f9ad8 127 while(!wifi->readable());
samux 1:8f04181f9ad8 128 int nb_available = wifi->readable();
samux 1:8f04181f9ad8 129 for (int i = 0; i < min(nb_available, length); i++) {
samux 1:8f04181f9ad8 130 data[i] = wifi->getc();
samux 1:8f04181f9ad8 131 }
samux 1:8f04181f9ad8 132 return min(nb_available, length);
samux 1:8f04181f9ad8 133 }
samux 1:8f04181f9ad8 134
samux 1:8f04181f9ad8 135
samux 1:8f04181f9ad8 136 // -1 if unsuccessful, else number of bytes received
samux 1:8f04181f9ad8 137 int TCPSocketConnection::receive_all(char* data, int length)
samux 1:8f04181f9ad8 138 {
samux 1:8f04181f9ad8 139 Timer tmr;
samux 1:8f04181f9ad8 140 int idx = 0;
samux 1:8f04181f9ad8 141
samux 1:8f04181f9ad8 142 tmr.start();
samux 1:8f04181f9ad8 143
samux 1:8f04181f9ad8 144 while (tmr.read_ms() < _timeout || _blocking) {
samux 1:8f04181f9ad8 145
samux 1:8f04181f9ad8 146 int nb_available = wifi->readable();
samux 1:8f04181f9ad8 147 for (int i = 0; i < min(nb_available, length); i++) {
samux 1:8f04181f9ad8 148 data[idx++] = wifi->getc();
samux 1:8f04181f9ad8 149 }
samux 1:8f04181f9ad8 150
samux 1:8f04181f9ad8 151 if (idx == length)
samux 1:8f04181f9ad8 152 return idx;
samux 1:8f04181f9ad8 153 }
samux 1:8f04181f9ad8 154
samux 1:8f04181f9ad8 155 return (idx == 0) ? -1 : idx;
samux 1:8f04181f9ad8 156 }