RC
Dependents: WizFi250_AP_HelloWorld
Fork of WizFi250Interface by
Socket/TCPSocketConnection.cpp@0:f2039204c8f6, 2014-06-02 (annotated)
- Committer:
- kaizen
- Date:
- Mon Jun 02 23:34:10 2014 +0000
- Revision:
- 0:f2039204c8f6
- Child:
- 5:8a0702aa91e3
WizFi250 Library First Release
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 | |
kaizen | 0:f2039204c8f6 | 23 | #include "TCPSocketConnection.h" |
kaizen | 0:f2039204c8f6 | 24 | #include <algorithm> |
kaizen | 0:f2039204c8f6 | 25 | |
kaizen | 0:f2039204c8f6 | 26 | TCPSocketConnection::TCPSocketConnection() {} |
kaizen | 0:f2039204c8f6 | 27 | |
kaizen | 0:f2039204c8f6 | 28 | int TCPSocketConnection::connect(const char* host, const int port) |
kaizen | 0:f2039204c8f6 | 29 | { |
kaizen | 0:f2039204c8f6 | 30 | if (set_address(host, port) != 0) return -1; |
kaizen | 0:f2039204c8f6 | 31 | |
kaizen | 0:f2039204c8f6 | 32 | _server = false; |
kaizen | 0:f2039204c8f6 | 33 | _cid = _wizfi250->open(WizFi250::PROTO_TCP, get_address(), get_port()); |
kaizen | 0:f2039204c8f6 | 34 | if (_cid < 0 ) return -1; |
kaizen | 0:f2039204c8f6 | 35 | |
kaizen | 0:f2039204c8f6 | 36 | return 0; |
kaizen | 0:f2039204c8f6 | 37 | } |
kaizen | 0:f2039204c8f6 | 38 | |
kaizen | 0:f2039204c8f6 | 39 | bool TCPSocketConnection::is_connected(void) |
kaizen | 0:f2039204c8f6 | 40 | { |
kaizen | 0:f2039204c8f6 | 41 | bool _is_connected = _wizfi250->isConnected(_cid); |
kaizen | 0:f2039204c8f6 | 42 | if(!_is_connected) _cid = -1; |
kaizen | 0:f2039204c8f6 | 43 | |
kaizen | 0:f2039204c8f6 | 44 | return _is_connected; |
kaizen | 0:f2039204c8f6 | 45 | } |
kaizen | 0:f2039204c8f6 | 46 | |
kaizen | 0:f2039204c8f6 | 47 | int TCPSocketConnection::send(char *data, int length) |
kaizen | 0:f2039204c8f6 | 48 | { |
kaizen | 0:f2039204c8f6 | 49 | if (_cid < 0 || !is_connected()) return -1; |
kaizen | 0:f2039204c8f6 | 50 | |
kaizen | 0:f2039204c8f6 | 51 | return _wizfi250->send(_cid, data, length); |
kaizen | 0:f2039204c8f6 | 52 | } |
kaizen | 0:f2039204c8f6 | 53 | |
kaizen | 0:f2039204c8f6 | 54 | int TCPSocketConnection::send_all(char *data, int length) |
kaizen | 0:f2039204c8f6 | 55 | { |
kaizen | 0:f2039204c8f6 | 56 | Timer tmr; |
kaizen | 0:f2039204c8f6 | 57 | int idx = 0; |
kaizen | 0:f2039204c8f6 | 58 | |
kaizen | 0:f2039204c8f6 | 59 | if(_cid <0 || !is_connected()) return -1; |
kaizen | 0:f2039204c8f6 | 60 | |
kaizen | 0:f2039204c8f6 | 61 | tmr.start(); |
kaizen | 0:f2039204c8f6 | 62 | while((tmr.read_ms() < _timeout) || _blocking) |
kaizen | 0:f2039204c8f6 | 63 | { |
kaizen | 0:f2039204c8f6 | 64 | idx += _wizfi250->send(_cid, &data[idx], length - idx); |
kaizen | 0:f2039204c8f6 | 65 | if(idx < 0) return -1; |
kaizen | 0:f2039204c8f6 | 66 | |
kaizen | 0:f2039204c8f6 | 67 | if(idx == length) |
kaizen | 0:f2039204c8f6 | 68 | return idx; |
kaizen | 0:f2039204c8f6 | 69 | } |
kaizen | 0:f2039204c8f6 | 70 | return (idx == 0) ? -1 : idx; |
kaizen | 0:f2039204c8f6 | 71 | } |
kaizen | 0:f2039204c8f6 | 72 | |
kaizen | 0:f2039204c8f6 | 73 | int TCPSocketConnection::receive(char* data, int length) |
kaizen | 0:f2039204c8f6 | 74 | { |
kaizen | 0:f2039204c8f6 | 75 | Timer tmr; |
kaizen | 0:f2039204c8f6 | 76 | int time = -1; |
kaizen | 0:f2039204c8f6 | 77 | |
kaizen | 0:f2039204c8f6 | 78 | if(_cid < 0 || !is_connected()) return -1; |
kaizen | 0:f2039204c8f6 | 79 | |
kaizen | 0:f2039204c8f6 | 80 | if(_blocking) |
kaizen | 0:f2039204c8f6 | 81 | { |
kaizen | 0:f2039204c8f6 | 82 | tmr.start(); |
kaizen | 0:f2039204c8f6 | 83 | while(time < _timeout + 20) |
kaizen | 0:f2039204c8f6 | 84 | { |
kaizen | 0:f2039204c8f6 | 85 | if(_wizfi250->readable(_cid)) |
kaizen | 0:f2039204c8f6 | 86 | { |
kaizen | 0:f2039204c8f6 | 87 | DBG("receive readable"); |
kaizen | 0:f2039204c8f6 | 88 | break; |
kaizen | 0:f2039204c8f6 | 89 | } |
kaizen | 0:f2039204c8f6 | 90 | time = tmr.read_ms(); |
kaizen | 0:f2039204c8f6 | 91 | } |
kaizen | 0:f2039204c8f6 | 92 | if(time >= _timeout + 20) |
kaizen | 0:f2039204c8f6 | 93 | { |
kaizen | 0:f2039204c8f6 | 94 | DBG("receive timeout"); |
kaizen | 0:f2039204c8f6 | 95 | return 0; |
kaizen | 0:f2039204c8f6 | 96 | } |
kaizen | 0:f2039204c8f6 | 97 | } |
kaizen | 0:f2039204c8f6 | 98 | |
kaizen | 0:f2039204c8f6 | 99 | int nb_available = _wizfi250->recv(_cid, data, length); |
kaizen | 0:f2039204c8f6 | 100 | |
kaizen | 0:f2039204c8f6 | 101 | return nb_available; |
kaizen | 0:f2039204c8f6 | 102 | } |
kaizen | 0:f2039204c8f6 | 103 | |
kaizen | 0:f2039204c8f6 | 104 | int TCPSocketConnection::receive_all(char* data, int length) |
kaizen | 0:f2039204c8f6 | 105 | { |
kaizen | 0:f2039204c8f6 | 106 | Timer tmr; |
kaizen | 0:f2039204c8f6 | 107 | int idx = 0; |
kaizen | 0:f2039204c8f6 | 108 | int time = -1; |
kaizen | 0:f2039204c8f6 | 109 | |
kaizen | 0:f2039204c8f6 | 110 | if(_cid < 0 || !is_connected()) return -1; |
kaizen | 0:f2039204c8f6 | 111 | |
kaizen | 0:f2039204c8f6 | 112 | tmr.start(); |
kaizen | 0:f2039204c8f6 | 113 | |
kaizen | 0:f2039204c8f6 | 114 | while(time < _timeout || _blocking) |
kaizen | 0:f2039204c8f6 | 115 | { |
kaizen | 0:f2039204c8f6 | 116 | idx += _wizfi250->recv(_cid, &data[idx], length - idx); |
kaizen | 0:f2039204c8f6 | 117 | if (idx < 0) return -1; |
kaizen | 0:f2039204c8f6 | 118 | |
kaizen | 0:f2039204c8f6 | 119 | if(idx == length) |
kaizen | 0:f2039204c8f6 | 120 | break; |
kaizen | 0:f2039204c8f6 | 121 | |
kaizen | 0:f2039204c8f6 | 122 | time = tmr.read_ms(); |
kaizen | 0:f2039204c8f6 | 123 | } |
kaizen | 0:f2039204c8f6 | 124 | |
kaizen | 0:f2039204c8f6 | 125 | return (idx == 0) ? -1 : idx; |
kaizen | 0:f2039204c8f6 | 126 | } |
kaizen | 0:f2039204c8f6 | 127 | |
kaizen | 0:f2039204c8f6 | 128 | void TCPSocketConnection::acceptCID (int cid) |
kaizen | 0:f2039204c8f6 | 129 | { |
kaizen | 0:f2039204c8f6 | 130 | char *ip; |
kaizen | 0:f2039204c8f6 | 131 | int port; |
kaizen | 0:f2039204c8f6 | 132 | _server = true; |
kaizen | 0:f2039204c8f6 | 133 | _cid = cid; |
kaizen | 0:f2039204c8f6 | 134 | |
kaizen | 0:f2039204c8f6 | 135 | |
kaizen | 0:f2039204c8f6 | 136 | if( _wizfi250->cmdSMGMT(cid) ) return; |
kaizen | 0:f2039204c8f6 | 137 | if( !_wizfi250->getRemote(_cid, &ip, &port)) |
kaizen | 0:f2039204c8f6 | 138 | { |
kaizen | 0:f2039204c8f6 | 139 | set_address(ip, port); |
kaizen | 0:f2039204c8f6 | 140 | } |
kaizen | 0:f2039204c8f6 | 141 | } |