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