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