DongEun Koak / WizFi250Interface

Dependents:   WebSocket_WizFi250_HelloWorld IFTTT_WizFi250 AxedaGo-WizFi250 FANARM_AP_udp_server ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPSocket.cpp Source File

UDPSocket.cpp

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 /* Copyright (C) 2014 Wiznet, MIT License
00019  *  port to the Wiznet Module WizFi250
00020  */
00021 
00022 #include "UDPSocket.h"
00023 
00024 #include <string>
00025 #include <algorithm>
00026 
00027 UDPSocket::UDPSocket()
00028 {
00029     endpoint_connected = false;
00030 }
00031 
00032 int UDPSocket::init(void)
00033 {
00034     _server = false;
00035     return 0;
00036 }
00037 
00038 // Server initialization
00039 int UDPSocket::bind(int port)
00040 {
00041     if(port == 0)
00042     {
00043         _port = 5000;
00044         _server = false;
00045     }
00046     else
00047     {
00048         _port = port;
00049         _server = true;
00050     }
00051     
00052     return 0;
00053 }
00054 
00055 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
00056 {
00057     Timer tmr;
00058     int idx = 0;
00059 
00060     if (_cid < 0 && _wizfi250->isAssociated())
00061     {
00062         if (_server)
00063         {
00064             _cid = _wizfi250->listen(WizFi250::PROTO_UDP, _port);
00065         }
00066         else
00067         {
00068             _cid = _wizfi250->open(WizFi250::PROTO_UDP, remote.get_address(), remote.get_port(), _port);
00069         }
00070         if (_cid < 0)   return -1;
00071     }
00072 
00073     tmr.start();
00074 
00075     while ((tmr.read_ms() < _timeout) || _blocking)
00076     {
00077         if(_server)
00078         {
00079             idx += _wizfi250->sendto(_cid, packet, length, remote.get_address(), remote.get_port());
00080         }
00081         else
00082         {
00083             idx += _wizfi250->send(_cid, packet, length);
00084         }
00085         if (idx < 0)
00086         {
00087             if (!_wizfi250->isConnected(_cid)) _cid = -1;
00088         }
00089 
00090         if (idx == length)
00091             return idx;
00092     }
00093     return (idx == 0) ? -1 : idx;
00094 }
00095 
00096 
00097 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
00098 {
00099     Timer tmr;
00100     int idx = 0;
00101     int time = -1;
00102     char ip[16];
00103     int port;
00104 
00105     if(_cid < 0 && _wizfi250->isAssociated())
00106     {
00107         // Socket open
00108         if (_server) {
00109             _cid = _wizfi250->listen(WizFi250::PROTO_UDP, _port);
00110             //WIZ_DBG("TEST CID : %d",_cid);
00111         }
00112         else
00113         {
00114             _cid = _wizfi250->open(WizFi250::PROTO_UDP, remote.get_address(), remote.get_port(), _port);
00115         }
00116         if (_cid < 0)   return -1;
00117     }
00118 
00119     if (_blocking)
00120     {
00121         tmr.start();
00122         while (time < _timeout + 20)
00123         {
00124             if(_wizfi250->readable(_cid))
00125             {
00126                 break;
00127             }
00128             time = tmr.read_ms();
00129         }
00130         if (time >= _timeout + 20)
00131         {
00132             return -1;
00133         }
00134     }
00135     else
00136     {
00137         if(!_wizfi250->readable(_cid))
00138         {
00139             return 0;
00140         }
00141     }
00142    
00143 
00144     tmr.reset();
00145     time = -1;
00146     tmr.start();
00147 
00148     while (time < _timeout)
00149     {
00150         if(_server)
00151         {
00152             idx += _wizfi250->recvfrom(_cid, &buffer[idx], length - idx, ip, &port);
00153         }
00154         else
00155         {
00156             //idx += _wizfi250->recv(_cid, &buffer[idx], length - idx);
00157             idx += _wizfi250->recvfrom(_cid, &buffer[idx], length - idx, ip, &port);
00158         }
00159 
00160         if (idx == length)
00161             break;
00162 
00163         
00164         time = tmr.read_ms();
00165     }
00166 
00167     remote.set_address(ip, port);
00168     return (idx==0) ? -1 : idx;
00169 }