RC

Dependents:   WizFi250_AP_HelloWorld

Fork of WizFi250Interface by DongEun Koak

Committer:
kaizen
Date:
Wed Jun 24 06:25:56 2015 +0000
Revision:
11:e52b91d1a5d5
Parent:
9:12ecb377f63f
Modified problem when MCU call the send all function as revision history.; Modified receive function.

Who changed what in which revision?

UserRevisionLine numberNew 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 11:e52b91d1a5d5 77 int len = 0;
kaizen 0:f2039204c8f6 78
kaizen 0:f2039204c8f6 79 if(_cid < 0 || !is_connected()) return -1;
kaizen 0:f2039204c8f6 80
kaizen 0:f2039204c8f6 81 if(_blocking)
kaizen 0:f2039204c8f6 82 {
kaizen 0:f2039204c8f6 83 tmr.start();
kaizen8501 9:12ecb377f63f 84 while(time < _timeout)
kaizen 0:f2039204c8f6 85 {
kaizen 11:e52b91d1a5d5 86 len = _wizfi250->readable(_cid);
kaizen 11:e52b91d1a5d5 87 if(len == -1)
kaizen 11:e52b91d1a5d5 88 return len;
kaizen 11:e52b91d1a5d5 89
kaizen 11:e52b91d1a5d5 90 if(len > 0)
kaizen 11:e52b91d1a5d5 91 {
kaizen 11:e52b91d1a5d5 92 WIZ_DBG("receive readable : %d\r\n",len);
kaizen 11:e52b91d1a5d5 93 break;
kaizen 11:e52b91d1a5d5 94 }
kaizen 0:f2039204c8f6 95 time = tmr.read_ms();
kaizen 0:f2039204c8f6 96 }
kaizen8501 9:12ecb377f63f 97 if(time >= _timeout)
kaizen 0:f2039204c8f6 98 {
kaizen8501 9:12ecb377f63f 99 //WIZ_DBG("receive timeout");
kaizen 0:f2039204c8f6 100 return 0;
kaizen 0:f2039204c8f6 101 }
kaizen 0:f2039204c8f6 102 }
kaizen 0:f2039204c8f6 103
kaizen 11:e52b91d1a5d5 104 return _wizfi250->recv(_cid, data, length);
kaizen 0:f2039204c8f6 105 }
kaizen 0:f2039204c8f6 106
kaizen 0:f2039204c8f6 107 int TCPSocketConnection::receive_all(char* data, int length)
kaizen 0:f2039204c8f6 108 {
kaizen 0:f2039204c8f6 109 Timer tmr;
kaizen 0:f2039204c8f6 110 int idx = 0;
kaizen 0:f2039204c8f6 111 int time = -1;
kaizen 0:f2039204c8f6 112
kaizen 0:f2039204c8f6 113 if(_cid < 0 || !is_connected()) return -1;
kaizen 0:f2039204c8f6 114
kaizen 0:f2039204c8f6 115 tmr.start();
kaizen 0:f2039204c8f6 116
kaizen 0:f2039204c8f6 117 while(time < _timeout || _blocking)
kaizen 0:f2039204c8f6 118 {
kaizen 0:f2039204c8f6 119 idx += _wizfi250->recv(_cid, &data[idx], length - idx);
kaizen 0:f2039204c8f6 120 if (idx < 0) return -1;
kaizen 0:f2039204c8f6 121
kaizen 0:f2039204c8f6 122 if(idx == length)
kaizen 0:f2039204c8f6 123 break;
kaizen 0:f2039204c8f6 124
kaizen 0:f2039204c8f6 125 time = tmr.read_ms();
kaizen 0:f2039204c8f6 126 }
kaizen 0:f2039204c8f6 127
kaizen 0:f2039204c8f6 128 return (idx == 0) ? -1 : idx;
kaizen 0:f2039204c8f6 129 }
kaizen 0:f2039204c8f6 130
kaizen 0:f2039204c8f6 131 void TCPSocketConnection::acceptCID (int cid)
kaizen 0:f2039204c8f6 132 {
kaizen 0:f2039204c8f6 133 char *ip;
kaizen 0:f2039204c8f6 134 int port;
kaizen 0:f2039204c8f6 135 _server = true;
kaizen 0:f2039204c8f6 136 _cid = cid;
kaizen 0:f2039204c8f6 137
kaizen 0:f2039204c8f6 138
kaizen 0:f2039204c8f6 139 if( _wizfi250->cmdSMGMT(cid) ) return;
kaizen 0:f2039204c8f6 140 if( !_wizfi250->getRemote(_cid, &ip, &port))
kaizen 0:f2039204c8f6 141 {
kaizen 0:f2039204c8f6 142 set_address(ip, port);
kaizen 0:f2039204c8f6 143 }
kaizen 0:f2039204c8f6 144 }