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
GainSpan Wi-Fi library
The GS1011/GS2100 is an ultra low power 802.11b wireless module from GainSpan.
mbed RTOS supported.
- about this library: http://mbed.org/users/gsfan/notebook/GSwifiInterface/
- about Wi-Fi module: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
ゲインスパン Wi-Fi モジュール ライブラリ
ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011/GS2100 シリーズ用のライブラリです。
mbed RTOS に対応しています。(mbed2.0)
- このライブラリについて: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
- Wi-FIモジュールについて: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
- UARTコマンド、SPIデータインターフェースに対応しました。(2019/09)
Socket/UDPSocket.cpp@8:64184a968e3b, 2013-10-31 (annotated)
- Committer:
- gsfan
- Date:
- Thu Oct 31 06:41:45 2013 +0000
- Revision:
- 8:64184a968e3b
- Parent:
- 5:78943b3945b5
- Child:
- 10:7d8134e7df01
supported mbed RTOS
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 "UDPSocket.h" |
gsfan | 5:78943b3945b5 | 23 | |
gsfan | 5:78943b3945b5 | 24 | #include <string> |
gsfan | 5:78943b3945b5 | 25 | #include <algorithm> |
gsfan | 5:78943b3945b5 | 26 | |
gsfan | 5:78943b3945b5 | 27 | UDPSocket::UDPSocket() |
gsfan | 5:78943b3945b5 | 28 | { |
gsfan | 5:78943b3945b5 | 29 | endpoint_connected = false; |
gsfan | 5:78943b3945b5 | 30 | } |
gsfan | 5:78943b3945b5 | 31 | |
gsfan | 5:78943b3945b5 | 32 | int UDPSocket::init(void) |
gsfan | 5:78943b3945b5 | 33 | { |
gsfan | 5:78943b3945b5 | 34 | _server = false; |
gsfan | 5:78943b3945b5 | 35 | return 0; |
gsfan | 5:78943b3945b5 | 36 | } |
gsfan | 5:78943b3945b5 | 37 | |
gsfan | 5:78943b3945b5 | 38 | // Server initialization |
gsfan | 5:78943b3945b5 | 39 | int UDPSocket::bind(int port) |
gsfan | 5:78943b3945b5 | 40 | { |
gsfan | 5:78943b3945b5 | 41 | _port = port; |
gsfan | 5:78943b3945b5 | 42 | _server = true; |
gsfan | 5:78943b3945b5 | 43 | return 0; |
gsfan | 5:78943b3945b5 | 44 | } |
gsfan | 5:78943b3945b5 | 45 | |
gsfan | 5:78943b3945b5 | 46 | // -1 if unsuccessful, else number of bytes written |
gsfan | 5:78943b3945b5 | 47 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
gsfan | 5:78943b3945b5 | 48 | { |
gsfan | 5:78943b3945b5 | 49 | Timer tmr; |
gsfan | 5:78943b3945b5 | 50 | int idx = 0; |
gsfan | 5:78943b3945b5 | 51 | |
gsfan | 8:64184a968e3b | 52 | if (_cid < 0 && _wifi->isConnected()) { |
gsfan | 8:64184a968e3b | 53 | // Socket open |
gsfan | 8:64184a968e3b | 54 | if (_server) { |
gsfan | 8:64184a968e3b | 55 | _cid = _wifi->listen(GSwifi::PROTO_UDP, _port); |
gsfan | 8:64184a968e3b | 56 | } else { |
gsfan | 8:64184a968e3b | 57 | _cid = _wifi->open(GSwifi::PROTO_UDP, remote.get_address(), remote.get_port(), _port); |
gsfan | 8:64184a968e3b | 58 | } |
gsfan | 8:64184a968e3b | 59 | if (_cid < 0) return -1; |
gsfan | 8:64184a968e3b | 60 | } |
gsfan | 8:64184a968e3b | 61 | |
gsfan | 5:78943b3945b5 | 62 | tmr.start(); |
gsfan | 5:78943b3945b5 | 63 | |
gsfan | 5:78943b3945b5 | 64 | while ((tmr.read_ms() < _timeout) || _blocking) { |
gsfan | 5:78943b3945b5 | 65 | |
gsfan | 8:64184a968e3b | 66 | if (_server) { |
gsfan | 8:64184a968e3b | 67 | idx += _wifi->sendto(_cid, packet, length, remote.get_address(), remote.get_port()); |
gsfan | 8:64184a968e3b | 68 | } else { |
gsfan | 8:64184a968e3b | 69 | idx += _wifi->send(_cid, packet, length); |
gsfan | 8:64184a968e3b | 70 | } |
gsfan | 8:64184a968e3b | 71 | if (idx < 0) { |
gsfan | 8:64184a968e3b | 72 | if (!_wifi->isConnected(_cid)) _cid = -1; |
gsfan | 8:64184a968e3b | 73 | } |
gsfan | 5:78943b3945b5 | 74 | |
gsfan | 5:78943b3945b5 | 75 | if (idx == length) |
gsfan | 5:78943b3945b5 | 76 | return idx; |
gsfan | 5:78943b3945b5 | 77 | } |
gsfan | 5:78943b3945b5 | 78 | return (idx == 0) ? -1 : idx; |
gsfan | 5:78943b3945b5 | 79 | } |
gsfan | 5:78943b3945b5 | 80 | |
gsfan | 5:78943b3945b5 | 81 | // -1 if unsuccessful, else number of bytes received |
gsfan | 5:78943b3945b5 | 82 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
gsfan | 5:78943b3945b5 | 83 | { |
gsfan | 5:78943b3945b5 | 84 | Timer tmr; |
gsfan | 5:78943b3945b5 | 85 | int idx = 0; |
gsfan | 5:78943b3945b5 | 86 | int time = -1; |
gsfan | 8:64184a968e3b | 87 | char ip[16]; |
gsfan | 8:64184a968e3b | 88 | int port; |
gsfan | 5:78943b3945b5 | 89 | |
gsfan | 8:64184a968e3b | 90 | if (_cid < 0 && _wifi->isConnected()) { |
gsfan | 5:78943b3945b5 | 91 | // Socket open |
gsfan | 5:78943b3945b5 | 92 | if (_server) { |
gsfan | 8:64184a968e3b | 93 | _cid = _wifi->listen(GSwifi::PROTO_UDP, _port); |
gsfan | 5:78943b3945b5 | 94 | } else { |
gsfan | 8:64184a968e3b | 95 | _cid = _wifi->open(GSwifi::PROTO_UDP, remote.get_address(), remote.get_port(), _port); |
gsfan | 5:78943b3945b5 | 96 | } |
gsfan | 8:64184a968e3b | 97 | if (_cid < 0) return -1; |
gsfan | 5:78943b3945b5 | 98 | } |
gsfan | 5:78943b3945b5 | 99 | |
gsfan | 5:78943b3945b5 | 100 | if (_blocking) { |
gsfan | 8:64184a968e3b | 101 | tmr.start(); |
gsfan | 8:64184a968e3b | 102 | while (time < _timeout + 20) { |
gsfan | 8:64184a968e3b | 103 | if (_wifi->readable(_cid)) { |
gsfan | 5:78943b3945b5 | 104 | break; |
gsfan | 5:78943b3945b5 | 105 | } |
gsfan | 8:64184a968e3b | 106 | time = tmr.read_ms(); |
gsfan | 8:64184a968e3b | 107 | } |
gsfan | 8:64184a968e3b | 108 | if (time >= _timeout + 20) { |
gsfan | 8:64184a968e3b | 109 | return -1; |
gsfan | 5:78943b3945b5 | 110 | } |
gsfan | 5:78943b3945b5 | 111 | } |
gsfan | 5:78943b3945b5 | 112 | |
gsfan | 8:64184a968e3b | 113 | tmr.reset(); |
gsfan | 5:78943b3945b5 | 114 | tmr.start(); |
gsfan | 5:78943b3945b5 | 115 | |
gsfan | 5:78943b3945b5 | 116 | while (time < _timeout) { |
gsfan | 5:78943b3945b5 | 117 | |
gsfan | 8:64184a968e3b | 118 | if (_server) { |
gsfan | 8:64184a968e3b | 119 | idx += _wifi->recvfrom(_cid, &buffer[idx], length - idx, ip, &port); |
gsfan | 8:64184a968e3b | 120 | } else { |
gsfan | 8:64184a968e3b | 121 | idx += _wifi->recv(_cid, &buffer[idx], length - idx); |
gsfan | 8:64184a968e3b | 122 | } |
gsfan | 8:64184a968e3b | 123 | if (idx < 0) { |
gsfan | 8:64184a968e3b | 124 | if (!_wifi->isConnected(_cid)) _cid = -1; |
gsfan | 8:64184a968e3b | 125 | return -1; |
gsfan | 5:78943b3945b5 | 126 | } |
gsfan | 5:78943b3945b5 | 127 | |
gsfan | 5:78943b3945b5 | 128 | if (idx == length) { |
gsfan | 5:78943b3945b5 | 129 | break; |
gsfan | 5:78943b3945b5 | 130 | } |
gsfan | 5:78943b3945b5 | 131 | |
gsfan | 5:78943b3945b5 | 132 | time = tmr.read_ms(); |
gsfan | 5:78943b3945b5 | 133 | } |
gsfan | 5:78943b3945b5 | 134 | |
gsfan | 5:78943b3945b5 | 135 | if (_server) { |
gsfan | 8:64184a968e3b | 136 | remote.set_address(ip, port); |
gsfan | 5:78943b3945b5 | 137 | } |
gsfan | 5:78943b3945b5 | 138 | return (idx == 0) ? -1 : idx; |
gsfan | 5:78943b3945b5 | 139 | } |