vfv
Fork of WizFi310Interface_Legacynew by
Socket/UDPSocket.cpp@0:774ff1e8b26b, 2017-04-19 (annotated)
- Committer:
- kaizen
- Date:
- Wed Apr 19 00:46:44 2017 +0000
- Revision:
- 0:774ff1e8b26b
First release WizFi310Interface for mbed OS2 ( Legacy version )
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kaizen | 0:774ff1e8b26b | 1 | /* |
kaizen | 0:774ff1e8b26b | 2 | * Copyright (C) 2013 gsfan, MIT License |
kaizen | 0:774ff1e8b26b | 3 | * |
kaizen | 0:774ff1e8b26b | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kaizen | 0:774ff1e8b26b | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kaizen | 0:774ff1e8b26b | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kaizen | 0:774ff1e8b26b | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kaizen | 0:774ff1e8b26b | 8 | * furnished to do so, subject to the following conditions: |
kaizen | 0:774ff1e8b26b | 9 | * |
kaizen | 0:774ff1e8b26b | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
kaizen | 0:774ff1e8b26b | 11 | * substantial portions of the Software. |
kaizen | 0:774ff1e8b26b | 12 | * |
kaizen | 0:774ff1e8b26b | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kaizen | 0:774ff1e8b26b | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kaizen | 0:774ff1e8b26b | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kaizen | 0:774ff1e8b26b | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kaizen | 0:774ff1e8b26b | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kaizen | 0:774ff1e8b26b | 18 | */ |
kaizen | 0:774ff1e8b26b | 19 | /* Copyright (C) 2014 Wiznet, MIT License |
kaizen | 0:774ff1e8b26b | 20 | * port to the Wiznet Module WizFi250 |
kaizen | 0:774ff1e8b26b | 21 | */ |
kaizen | 0:774ff1e8b26b | 22 | /* Copyright (C) 2017 Wiznet, MIT License |
kaizen | 0:774ff1e8b26b | 23 | * port to the Wiznet Module WizFi310 |
kaizen | 0:774ff1e8b26b | 24 | */ |
kaizen | 0:774ff1e8b26b | 25 | |
kaizen | 0:774ff1e8b26b | 26 | #include "UDPSocket.h" |
kaizen | 0:774ff1e8b26b | 27 | |
kaizen | 0:774ff1e8b26b | 28 | #include <string> |
kaizen | 0:774ff1e8b26b | 29 | #include <algorithm> |
kaizen | 0:774ff1e8b26b | 30 | |
kaizen | 0:774ff1e8b26b | 31 | UDPSocket::UDPSocket() |
kaizen | 0:774ff1e8b26b | 32 | { |
kaizen | 0:774ff1e8b26b | 33 | endpoint_connected = false; |
kaizen | 0:774ff1e8b26b | 34 | } |
kaizen | 0:774ff1e8b26b | 35 | |
kaizen | 0:774ff1e8b26b | 36 | int UDPSocket::init(void) |
kaizen | 0:774ff1e8b26b | 37 | { |
kaizen | 0:774ff1e8b26b | 38 | _server = false; |
kaizen | 0:774ff1e8b26b | 39 | return 0; |
kaizen | 0:774ff1e8b26b | 40 | } |
kaizen | 0:774ff1e8b26b | 41 | |
kaizen | 0:774ff1e8b26b | 42 | // Server initialization |
kaizen | 0:774ff1e8b26b | 43 | int UDPSocket::bind(int port) |
kaizen | 0:774ff1e8b26b | 44 | { |
kaizen | 0:774ff1e8b26b | 45 | if(port == 0) |
kaizen | 0:774ff1e8b26b | 46 | { |
kaizen | 0:774ff1e8b26b | 47 | _port = 5000; |
kaizen | 0:774ff1e8b26b | 48 | _server = false; |
kaizen | 0:774ff1e8b26b | 49 | } |
kaizen | 0:774ff1e8b26b | 50 | else |
kaizen | 0:774ff1e8b26b | 51 | { |
kaizen | 0:774ff1e8b26b | 52 | _port = port; |
kaizen | 0:774ff1e8b26b | 53 | _server = true; |
kaizen | 0:774ff1e8b26b | 54 | } |
kaizen | 0:774ff1e8b26b | 55 | |
kaizen | 0:774ff1e8b26b | 56 | return 0; |
kaizen | 0:774ff1e8b26b | 57 | } |
kaizen | 0:774ff1e8b26b | 58 | |
kaizen | 0:774ff1e8b26b | 59 | int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) |
kaizen | 0:774ff1e8b26b | 60 | { |
kaizen | 0:774ff1e8b26b | 61 | Timer tmr; |
kaizen | 0:774ff1e8b26b | 62 | int idx = 0; |
kaizen | 0:774ff1e8b26b | 63 | |
kaizen | 0:774ff1e8b26b | 64 | if (_cid < 0 && _wizfi310->isAssociated()) |
kaizen | 0:774ff1e8b26b | 65 | { |
kaizen | 0:774ff1e8b26b | 66 | if (_server) |
kaizen | 0:774ff1e8b26b | 67 | { |
kaizen | 0:774ff1e8b26b | 68 | _cid = _wizfi310->listen(WizFi310::PROTO_UDP, _port); |
kaizen | 0:774ff1e8b26b | 69 | } |
kaizen | 0:774ff1e8b26b | 70 | else |
kaizen | 0:774ff1e8b26b | 71 | { |
kaizen | 0:774ff1e8b26b | 72 | _cid = _wizfi310->open(WizFi310::PROTO_UDP, remote.get_address(), remote.get_port(), _port); |
kaizen | 0:774ff1e8b26b | 73 | } |
kaizen | 0:774ff1e8b26b | 74 | if (_cid < 0) return -1; |
kaizen | 0:774ff1e8b26b | 75 | } |
kaizen | 0:774ff1e8b26b | 76 | |
kaizen | 0:774ff1e8b26b | 77 | tmr.start(); |
kaizen | 0:774ff1e8b26b | 78 | |
kaizen | 0:774ff1e8b26b | 79 | while ((tmr.read_ms() < _timeout) || _blocking) |
kaizen | 0:774ff1e8b26b | 80 | { |
kaizen | 0:774ff1e8b26b | 81 | if(_server) |
kaizen | 0:774ff1e8b26b | 82 | { |
kaizen | 0:774ff1e8b26b | 83 | idx += _wizfi310->sendto(_cid, packet, length, remote.get_address(), remote.get_port()); |
kaizen | 0:774ff1e8b26b | 84 | } |
kaizen | 0:774ff1e8b26b | 85 | else |
kaizen | 0:774ff1e8b26b | 86 | { |
kaizen | 0:774ff1e8b26b | 87 | idx += _wizfi310->send(_cid, packet, length); |
kaizen | 0:774ff1e8b26b | 88 | } |
kaizen | 0:774ff1e8b26b | 89 | if (idx < 0) |
kaizen | 0:774ff1e8b26b | 90 | { |
kaizen | 0:774ff1e8b26b | 91 | if (!_wizfi310->isConnected(_cid)) _cid = -1; |
kaizen | 0:774ff1e8b26b | 92 | } |
kaizen | 0:774ff1e8b26b | 93 | |
kaizen | 0:774ff1e8b26b | 94 | if (idx == length) |
kaizen | 0:774ff1e8b26b | 95 | return idx; |
kaizen | 0:774ff1e8b26b | 96 | } |
kaizen | 0:774ff1e8b26b | 97 | return (idx == 0) ? -1 : idx; |
kaizen | 0:774ff1e8b26b | 98 | } |
kaizen | 0:774ff1e8b26b | 99 | |
kaizen | 0:774ff1e8b26b | 100 | |
kaizen | 0:774ff1e8b26b | 101 | int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) |
kaizen | 0:774ff1e8b26b | 102 | { |
kaizen | 0:774ff1e8b26b | 103 | Timer tmr; |
kaizen | 0:774ff1e8b26b | 104 | int idx = 0; |
kaizen | 0:774ff1e8b26b | 105 | int time = -1; |
kaizen | 0:774ff1e8b26b | 106 | char ip[16]; |
kaizen | 0:774ff1e8b26b | 107 | int port; |
kaizen | 0:774ff1e8b26b | 108 | |
kaizen | 0:774ff1e8b26b | 109 | if(_cid < 0 && _wizfi310->isAssociated()) |
kaizen | 0:774ff1e8b26b | 110 | { |
kaizen | 0:774ff1e8b26b | 111 | // Socket open |
kaizen | 0:774ff1e8b26b | 112 | if (_server) { |
kaizen | 0:774ff1e8b26b | 113 | _cid = _wizfi310->listen(WizFi310::PROTO_UDP, _port); |
kaizen | 0:774ff1e8b26b | 114 | //WIZ_DBG("TEST CID : %d",_cid); |
kaizen | 0:774ff1e8b26b | 115 | } |
kaizen | 0:774ff1e8b26b | 116 | else |
kaizen | 0:774ff1e8b26b | 117 | { |
kaizen | 0:774ff1e8b26b | 118 | _cid = _wizfi310->open(WizFi310::PROTO_UDP, remote.get_address(), remote.get_port(), _port); |
kaizen | 0:774ff1e8b26b | 119 | } |
kaizen | 0:774ff1e8b26b | 120 | if (_cid < 0) return -1; |
kaizen | 0:774ff1e8b26b | 121 | } |
kaizen | 0:774ff1e8b26b | 122 | |
kaizen | 0:774ff1e8b26b | 123 | if (_blocking) |
kaizen | 0:774ff1e8b26b | 124 | { |
kaizen | 0:774ff1e8b26b | 125 | tmr.start(); |
kaizen | 0:774ff1e8b26b | 126 | while (time < _timeout + 20) |
kaizen | 0:774ff1e8b26b | 127 | { |
kaizen | 0:774ff1e8b26b | 128 | if(_wizfi310->readable(_cid)) |
kaizen | 0:774ff1e8b26b | 129 | { |
kaizen | 0:774ff1e8b26b | 130 | break; |
kaizen | 0:774ff1e8b26b | 131 | } |
kaizen | 0:774ff1e8b26b | 132 | time = tmr.read_ms(); |
kaizen | 0:774ff1e8b26b | 133 | } |
kaizen | 0:774ff1e8b26b | 134 | if (time >= _timeout + 20) |
kaizen | 0:774ff1e8b26b | 135 | { |
kaizen | 0:774ff1e8b26b | 136 | return -1; |
kaizen | 0:774ff1e8b26b | 137 | } |
kaizen | 0:774ff1e8b26b | 138 | } |
kaizen | 0:774ff1e8b26b | 139 | else |
kaizen | 0:774ff1e8b26b | 140 | { |
kaizen | 0:774ff1e8b26b | 141 | if(!_wizfi310->readable(_cid)) |
kaizen | 0:774ff1e8b26b | 142 | { |
kaizen | 0:774ff1e8b26b | 143 | return 0; |
kaizen | 0:774ff1e8b26b | 144 | } |
kaizen | 0:774ff1e8b26b | 145 | } |
kaizen | 0:774ff1e8b26b | 146 | |
kaizen | 0:774ff1e8b26b | 147 | |
kaizen | 0:774ff1e8b26b | 148 | tmr.reset(); |
kaizen | 0:774ff1e8b26b | 149 | time = -1; |
kaizen | 0:774ff1e8b26b | 150 | tmr.start(); |
kaizen | 0:774ff1e8b26b | 151 | |
kaizen | 0:774ff1e8b26b | 152 | while (time < _timeout) |
kaizen | 0:774ff1e8b26b | 153 | { |
kaizen | 0:774ff1e8b26b | 154 | if(_server) |
kaizen | 0:774ff1e8b26b | 155 | { |
kaizen | 0:774ff1e8b26b | 156 | idx += _wizfi310->recvfrom(_cid, &buffer[idx], length - idx, ip, &port); |
kaizen | 0:774ff1e8b26b | 157 | } |
kaizen | 0:774ff1e8b26b | 158 | else |
kaizen | 0:774ff1e8b26b | 159 | { |
kaizen | 0:774ff1e8b26b | 160 | //idx += _wizfi250->recv(_cid, &buffer[idx], length - idx); |
kaizen | 0:774ff1e8b26b | 161 | idx += _wizfi310->recvfrom(_cid, &buffer[idx], length - idx, ip, &port); |
kaizen | 0:774ff1e8b26b | 162 | } |
kaizen | 0:774ff1e8b26b | 163 | |
kaizen | 0:774ff1e8b26b | 164 | if (idx == length) |
kaizen | 0:774ff1e8b26b | 165 | break; |
kaizen | 0:774ff1e8b26b | 166 | |
kaizen | 0:774ff1e8b26b | 167 | |
kaizen | 0:774ff1e8b26b | 168 | time = tmr.read_ms(); |
kaizen | 0:774ff1e8b26b | 169 | } |
kaizen | 0:774ff1e8b26b | 170 | |
kaizen | 0:774ff1e8b26b | 171 | remote.set_address(ip, port); |
kaizen | 0:774ff1e8b26b | 172 | return (idx==0) ? -1 : idx; |
kaizen | 0:774ff1e8b26b | 173 | } |