GSwifiInterface library (interface for GainSpan Wi-Fi GS1011 modules) Please see https://mbed.org/users/gsfan/notebook/GSwifiInterface/

Dependents:   GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more

Fork of WiflyInterface by mbed official

GainSpan Wi-Fi library

The GS1011/GS2100 is an ultra low power 802.11b wireless module from GainSpan.

mbed RTOS supported.

/media/uploads/gsfan/gs_im_002.jpg /media/uploads/gsfan/gs1011m_2.jpg

ゲインスパン Wi-Fi モジュール ライブラリ

ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011/GS2100 シリーズ用のライブラリです。

mbed RTOS に対応しています。(mbed2.0)

Committer:
gsfan
Date:
Tue Sep 24 06:24:37 2019 +0000
Revision:
22:d25a5a0d2497
Parent:
11:71d67fea5ace
UART Command and SPI Data supported.; bug fix.;

Who changed what in which revision?

UserRevisionLine numberNew 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 8:64184a968e3b 29 if (set_address(host, port) != 0) return -1;
gsfan 5:78943b3945b5 30
gsfan 8:64184a968e3b 31 _server = false;
gsfan 8:64184a968e3b 32 _cid = _wifi->open(GSwifi::PROTO_TCP, get_address(), get_port());
gsfan 8:64184a968e3b 33 if (_cid < 0) return -1;
gsfan 5:78943b3945b5 34
gsfan 8:64184a968e3b 35 return 0;
gsfan 5:78943b3945b5 36 }
gsfan 5:78943b3945b5 37
gsfan 5:78943b3945b5 38 bool TCPSocketConnection::is_connected(void)
gsfan 5:78943b3945b5 39 {
gsfan 8:64184a968e3b 40 bool _is_connected = _wifi->isConnected(_cid);
gsfan 8:64184a968e3b 41 if (!_is_connected) _cid = -1;
gsfan 8:64184a968e3b 42 return _is_connected;
gsfan 5:78943b3945b5 43 }
gsfan 5:78943b3945b5 44
gsfan 5:78943b3945b5 45 int TCPSocketConnection::send(char* data, int length)
gsfan 5:78943b3945b5 46 {
gsfan 8:64184a968e3b 47 if (_cid < 0 || !is_connected()) return -1;
gsfan 5:78943b3945b5 48
gsfan 5:78943b3945b5 49 // TCP Client/Server
gsfan 8:64184a968e3b 50 return _wifi->send(_cid, data, length);
gsfan 5:78943b3945b5 51 }
gsfan 5:78943b3945b5 52
gsfan 5:78943b3945b5 53 // -1 if unsuccessful, else number of bytes written
gsfan 5:78943b3945b5 54 int TCPSocketConnection::send_all(char* data, int length)
gsfan 5:78943b3945b5 55 {
gsfan 5:78943b3945b5 56 Timer tmr;
gsfan 5:78943b3945b5 57 int idx = 0;
gsfan 8:64184a968e3b 58
gsfan 8:64184a968e3b 59 if (_cid < 0 || !is_connected()) return -1;
gsfan 8:64184a968e3b 60
gsfan 5:78943b3945b5 61 tmr.start();
gsfan 5:78943b3945b5 62 // TCP Client/Server
gsfan 5:78943b3945b5 63 while ((tmr.read_ms() < _timeout) || _blocking) {
gsfan 5:78943b3945b5 64
gsfan 8:64184a968e3b 65 idx += _wifi->send(_cid, &data[idx], length - idx);
gsfan 8:64184a968e3b 66 if (idx < 0) return -1;
gsfan 5:78943b3945b5 67
gsfan 5:78943b3945b5 68 if (idx == length)
gsfan 5:78943b3945b5 69 return idx;
gsfan 5:78943b3945b5 70 }
gsfan 5:78943b3945b5 71 return (idx == 0) ? -1 : idx;
gsfan 5:78943b3945b5 72 }
gsfan 5:78943b3945b5 73
gsfan 5:78943b3945b5 74 // -1 if unsuccessful, else number of bytes received
gsfan 5:78943b3945b5 75 int TCPSocketConnection::receive(char* data, int length)
gsfan 5:78943b3945b5 76 {
gsfan 5:78943b3945b5 77 Timer tmr;
gsfan 5:78943b3945b5 78 int time = -1;
gsfan 8:64184a968e3b 79
gsfan 8:64184a968e3b 80 if (_cid < 0 || !is_connected()) return -1;
gsfan 8:64184a968e3b 81
gsfan 8:64184a968e3b 82 if (_blocking) {
gsfan 5:78943b3945b5 83 tmr.start();
gsfan 5:78943b3945b5 84 while (time < _timeout + 20) {
gsfan 5:78943b3945b5 85 if (_wifi->readable(_cid)) {
gsfan 11:71d67fea5ace 86 DBG("receive readable");
gsfan 5:78943b3945b5 87 break;
gsfan 5:78943b3945b5 88 }
gsfan 22:d25a5a0d2497 89 Thread::wait(1);
gsfan 5:78943b3945b5 90 time = tmr.read_ms();
gsfan 5:78943b3945b5 91 }
gsfan 5:78943b3945b5 92 if (time >= _timeout + 20) {
gsfan 11:71d67fea5ace 93 DBG("receive timeout");
gsfan 11:71d67fea5ace 94 return 0;
gsfan 5:78943b3945b5 95 }
gsfan 5:78943b3945b5 96 }
gsfan 5:78943b3945b5 97
gsfan 8:64184a968e3b 98 int nb_available = _wifi->recv(_cid, data, length);
gsfan 5:78943b3945b5 99
gsfan 8:64184a968e3b 100 return nb_available;
gsfan 5:78943b3945b5 101 }
gsfan 5:78943b3945b5 102
gsfan 5:78943b3945b5 103
gsfan 5:78943b3945b5 104 // -1 if unsuccessful, else number of bytes received
gsfan 5:78943b3945b5 105 int TCPSocketConnection::receive_all(char* data, int length)
gsfan 5:78943b3945b5 106 {
gsfan 5:78943b3945b5 107 Timer tmr;
gsfan 5:78943b3945b5 108 int idx = 0;
gsfan 5:78943b3945b5 109 int time = -1;
gsfan 5:78943b3945b5 110
gsfan 8:64184a968e3b 111 if (_cid < 0 || !is_connected()) return -1;
gsfan 8:64184a968e3b 112
gsfan 5:78943b3945b5 113 tmr.start();
gsfan 5:78943b3945b5 114
gsfan 5:78943b3945b5 115 while (time < _timeout || _blocking) {
gsfan 5:78943b3945b5 116
gsfan 8:64184a968e3b 117 idx += _wifi->recv(_cid, &data[idx], length - idx);
gsfan 8:64184a968e3b 118 if (idx < 0) return -1;
gsfan 5:78943b3945b5 119
gsfan 5:78943b3945b5 120 if (idx == length)
gsfan 5:78943b3945b5 121 break;
gsfan 5:78943b3945b5 122
gsfan 22:d25a5a0d2497 123 Thread::wait(1);
gsfan 5:78943b3945b5 124 time = tmr.read_ms();
gsfan 5:78943b3945b5 125 }
gsfan 5:78943b3945b5 126
gsfan 5:78943b3945b5 127 return (idx == 0) ? -1 : idx;
gsfan 5:78943b3945b5 128 }
gsfan 5:78943b3945b5 129
gsfan 8:64184a968e3b 130 void TCPSocketConnection::acceptCID (int cid) {
gsfan 5:78943b3945b5 131 char *ip;
gsfan 5:78943b3945b5 132 int port;
gsfan 5:78943b3945b5 133 _server = true;
gsfan 5:78943b3945b5 134 _cid = cid;
gsfan 8:64184a968e3b 135 if (!_wifi->getRemote(_cid, &ip, &port)) {
gsfan 5:78943b3945b5 136 set_address(ip, port);
gsfan 5:78943b3945b5 137 }
gsfan 5:78943b3945b5 138 }