see http://mbed.org/users/okini3939/notebook/wattmeter-shield-on-mbed/
Fork of GSwifi_xively by
GSwifiInterface/Socket/UDPSocket.cpp@4:9a2415f2ab07, 2013-11-27 (annotated)
- Committer:
- okini3939
- Date:
- Wed Nov 27 08:18:45 2013 +0000
- Revision:
- 4:9a2415f2ab07
update GSwifiInterface library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 4:9a2415f2ab07 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
okini3939 | 4:9a2415f2ab07 | 2 | * |
okini3939 | 4:9a2415f2ab07 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
okini3939 | 4:9a2415f2ab07 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
okini3939 | 4:9a2415f2ab07 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
okini3939 | 4:9a2415f2ab07 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
okini3939 | 4:9a2415f2ab07 | 7 | * furnished to do so, subject to the following conditions: |
okini3939 | 4:9a2415f2ab07 | 8 | * |
okini3939 | 4:9a2415f2ab07 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
okini3939 | 4:9a2415f2ab07 | 10 | * substantial portions of the Software. |
okini3939 | 4:9a2415f2ab07 | 11 | * |
okini3939 | 4:9a2415f2ab07 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
okini3939 | 4:9a2415f2ab07 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
okini3939 | 4:9a2415f2ab07 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
okini3939 | 4:9a2415f2ab07 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
okini3939 | 4:9a2415f2ab07 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
okini3939 | 4:9a2415f2ab07 | 17 | */ |
okini3939 | 4:9a2415f2ab07 | 18 | /* Copyright (C) 2013 gsfan, MIT License |
okini3939 | 4:9a2415f2ab07 | 19 | * port to the GainSpan Wi-FI module GS1011 |
okini3939 | 4:9a2415f2ab07 | 20 | */ |
okini3939 | 4:9a2415f2ab07 | 21 | |
okini3939 | 4:9a2415f2ab07 | 22 | #include "UDPSocket.h" |
okini3939 | 4:9a2415f2ab07 | 23 | |
okini3939 | 4:9a2415f2ab07 | 24 | #include <string> |
okini3939 | 4:9a2415f2ab07 | 25 | #include <algorithm> |
okini3939 | 4:9a2415f2ab07 | 26 | |
okini3939 | 4:9a2415f2ab07 | 27 | UDPSocket::UDPSocket() |
okini3939 | 4:9a2415f2ab07 | 28 | { |
okini3939 | 4:9a2415f2ab07 | 29 | endpoint_connected = false; |
okini3939 | 4:9a2415f2ab07 | 30 | } |
okini3939 | 4:9a2415f2ab07 | 31 | |
okini3939 | 4:9a2415f2ab07 | 32 | int UDPSocket::init(void) |
okini3939 | 4:9a2415f2ab07 | 33 | { |
okini3939 | 4:9a2415f2ab07 | 34 | _server = false; |
okini3939 | 4:9a2415f2ab07 | 35 | return 0; |
okini3939 | 4:9a2415f2ab07 | 36 | } |
okini3939 | 4:9a2415f2ab07 | 37 | |
okini3939 | 4:9a2415f2ab07 | 38 | // Server initialization |
okini3939 | 4:9a2415f2ab07 | 39 | int UDPSocket::bind(int port) |
okini3939 | 4:9a2415f2ab07 | 40 | { |
okini3939 | 4:9a2415f2ab07 | 41 | _port = port; |
okini3939 | 4:9a2415f2ab07 | 42 | _server = true; |
okini3939 | 4:9a2415f2ab07 | 43 | return 0; |
okini3939 | 4:9a2415f2ab07 | 44 | } |
okini3939 | 4:9a2415f2ab07 | 45 | |
okini3939 | 4:9a2415f2ab07 | 46 | // -1 if unsuccessful, else number of bytes written |
okini3939 | 4:9a2415f2ab07 | 47 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
okini3939 | 4:9a2415f2ab07 | 48 | { |
okini3939 | 4:9a2415f2ab07 | 49 | Timer tmr; |
okini3939 | 4:9a2415f2ab07 | 50 | int idx = 0; |
okini3939 | 4:9a2415f2ab07 | 51 | |
okini3939 | 4:9a2415f2ab07 | 52 | if (_cid < 0 && _wifi->isAssociated()) { |
okini3939 | 4:9a2415f2ab07 | 53 | // Socket open |
okini3939 | 4:9a2415f2ab07 | 54 | if (_server) { |
okini3939 | 4:9a2415f2ab07 | 55 | _cid = _wifi->listen(GSwifi::PROTO_UDP, _port); |
okini3939 | 4:9a2415f2ab07 | 56 | } else { |
okini3939 | 4:9a2415f2ab07 | 57 | _cid = _wifi->open(GSwifi::PROTO_UDP, remote.get_address(), remote.get_port(), _port); |
okini3939 | 4:9a2415f2ab07 | 58 | } |
okini3939 | 4:9a2415f2ab07 | 59 | if (_cid < 0) return -1; |
okini3939 | 4:9a2415f2ab07 | 60 | } |
okini3939 | 4:9a2415f2ab07 | 61 | |
okini3939 | 4:9a2415f2ab07 | 62 | tmr.start(); |
okini3939 | 4:9a2415f2ab07 | 63 | |
okini3939 | 4:9a2415f2ab07 | 64 | while ((tmr.read_ms() < _timeout) || _blocking) { |
okini3939 | 4:9a2415f2ab07 | 65 | |
okini3939 | 4:9a2415f2ab07 | 66 | if (_server) { |
okini3939 | 4:9a2415f2ab07 | 67 | idx += _wifi->sendto(_cid, packet, length, remote.get_address(), remote.get_port()); |
okini3939 | 4:9a2415f2ab07 | 68 | } else { |
okini3939 | 4:9a2415f2ab07 | 69 | idx += _wifi->send(_cid, packet, length); |
okini3939 | 4:9a2415f2ab07 | 70 | } |
okini3939 | 4:9a2415f2ab07 | 71 | if (idx < 0) { |
okini3939 | 4:9a2415f2ab07 | 72 | if (!_wifi->isConnected(_cid)) _cid = -1; |
okini3939 | 4:9a2415f2ab07 | 73 | } |
okini3939 | 4:9a2415f2ab07 | 74 | |
okini3939 | 4:9a2415f2ab07 | 75 | if (idx == length) |
okini3939 | 4:9a2415f2ab07 | 76 | return idx; |
okini3939 | 4:9a2415f2ab07 | 77 | } |
okini3939 | 4:9a2415f2ab07 | 78 | return (idx == 0) ? -1 : idx; |
okini3939 | 4:9a2415f2ab07 | 79 | } |
okini3939 | 4:9a2415f2ab07 | 80 | |
okini3939 | 4:9a2415f2ab07 | 81 | // -1 if unsuccessful, else number of bytes received |
okini3939 | 4:9a2415f2ab07 | 82 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
okini3939 | 4:9a2415f2ab07 | 83 | { |
okini3939 | 4:9a2415f2ab07 | 84 | Timer tmr; |
okini3939 | 4:9a2415f2ab07 | 85 | int idx = 0; |
okini3939 | 4:9a2415f2ab07 | 86 | int time = -1; |
okini3939 | 4:9a2415f2ab07 | 87 | char ip[16]; |
okini3939 | 4:9a2415f2ab07 | 88 | int port; |
okini3939 | 4:9a2415f2ab07 | 89 | |
okini3939 | 4:9a2415f2ab07 | 90 | if (_cid < 0 && _wifi->isAssociated()) { |
okini3939 | 4:9a2415f2ab07 | 91 | // Socket open |
okini3939 | 4:9a2415f2ab07 | 92 | if (_server) { |
okini3939 | 4:9a2415f2ab07 | 93 | _cid = _wifi->listen(GSwifi::PROTO_UDP, _port); |
okini3939 | 4:9a2415f2ab07 | 94 | } else { |
okini3939 | 4:9a2415f2ab07 | 95 | _cid = _wifi->open(GSwifi::PROTO_UDP, remote.get_address(), remote.get_port(), _port); |
okini3939 | 4:9a2415f2ab07 | 96 | } |
okini3939 | 4:9a2415f2ab07 | 97 | if (_cid < 0) return -1; |
okini3939 | 4:9a2415f2ab07 | 98 | } |
okini3939 | 4:9a2415f2ab07 | 99 | |
okini3939 | 4:9a2415f2ab07 | 100 | if (_blocking) { |
okini3939 | 4:9a2415f2ab07 | 101 | tmr.start(); |
okini3939 | 4:9a2415f2ab07 | 102 | while (time < _timeout + 20) { |
okini3939 | 4:9a2415f2ab07 | 103 | if (_wifi->readable(_cid)) { |
okini3939 | 4:9a2415f2ab07 | 104 | break; |
okini3939 | 4:9a2415f2ab07 | 105 | } |
okini3939 | 4:9a2415f2ab07 | 106 | time = tmr.read_ms(); |
okini3939 | 4:9a2415f2ab07 | 107 | } |
okini3939 | 4:9a2415f2ab07 | 108 | if (time >= _timeout + 20) { |
okini3939 | 4:9a2415f2ab07 | 109 | return -1; |
okini3939 | 4:9a2415f2ab07 | 110 | } |
okini3939 | 4:9a2415f2ab07 | 111 | } |
okini3939 | 4:9a2415f2ab07 | 112 | |
okini3939 | 4:9a2415f2ab07 | 113 | tmr.reset(); |
okini3939 | 4:9a2415f2ab07 | 114 | time = -1; |
okini3939 | 4:9a2415f2ab07 | 115 | tmr.start(); |
okini3939 | 4:9a2415f2ab07 | 116 | |
okini3939 | 4:9a2415f2ab07 | 117 | while (time < _timeout) { |
okini3939 | 4:9a2415f2ab07 | 118 | |
okini3939 | 4:9a2415f2ab07 | 119 | if (_server) { |
okini3939 | 4:9a2415f2ab07 | 120 | idx += _wifi->recvfrom(_cid, &buffer[idx], length - idx, ip, &port); |
okini3939 | 4:9a2415f2ab07 | 121 | } else { |
okini3939 | 4:9a2415f2ab07 | 122 | idx += _wifi->recv(_cid, &buffer[idx], length - idx); |
okini3939 | 4:9a2415f2ab07 | 123 | } |
okini3939 | 4:9a2415f2ab07 | 124 | if (idx < 0) { |
okini3939 | 4:9a2415f2ab07 | 125 | if (!_wifi->isConnected(_cid)) _cid = -1; |
okini3939 | 4:9a2415f2ab07 | 126 | return -1; |
okini3939 | 4:9a2415f2ab07 | 127 | } |
okini3939 | 4:9a2415f2ab07 | 128 | |
okini3939 | 4:9a2415f2ab07 | 129 | if (idx == length) { |
okini3939 | 4:9a2415f2ab07 | 130 | break; |
okini3939 | 4:9a2415f2ab07 | 131 | } |
okini3939 | 4:9a2415f2ab07 | 132 | |
okini3939 | 4:9a2415f2ab07 | 133 | time = tmr.read_ms(); |
okini3939 | 4:9a2415f2ab07 | 134 | } |
okini3939 | 4:9a2415f2ab07 | 135 | |
okini3939 | 4:9a2415f2ab07 | 136 | if (_server) { |
okini3939 | 4:9a2415f2ab07 | 137 | remote.set_address(ip, port); |
okini3939 | 4:9a2415f2ab07 | 138 | } |
okini3939 | 4:9a2415f2ab07 | 139 | return (idx == 0) ? -1 : idx; |
okini3939 | 4:9a2415f2ab07 | 140 | } |