Akshay Tom / WizFi310Interface_Legacynew

Dependencies:   WizFi310Interface_Legacynew

Dependents:   w7500-mqtt-wizfi310 w7500-mqtt-wizfi310 w7500-mqtt-wizfi310

Fork of WizFi310Interface_Legacynew by ajeet prajapati

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UDPSocket.cpp Source File

UDPSocket.cpp

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