GSwifiInterface library (interface for GainSpan Wi-Fi GS1011 modules) Please see https://mbed.org/users/gsfan/notebook/GSwifiInterface/

Dependents:   GSwifiInterface_HelloWorld GSwifiInterface_HelloServo GSwifiInterface_UDPEchoServer GSwifiInterface_UDPEchoClient ... more

Fork of WiflyInterface by mbed official

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) 2013 gsfan, MIT License
00019  *  port to the GainSpan Wi-FI module GS1011
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     _port = port;
00042     _server = true;
00043 
00044     if (_cid < 0 && _wifi->isAssociated()) {
00045         // Socket open
00046         _cid = _wifi->listen(GSwifi::PROTO_UDP, _port);
00047         if (_cid < 0) return -1;
00048     }
00049 
00050     return 0;
00051 }
00052 
00053 // -1 if unsuccessful, else number of bytes written
00054 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
00055 {
00056     Timer tmr;
00057     int idx = 0;
00058 
00059     if (_cid < 0 && _wifi->isAssociated()) {
00060         // Socket open
00061         if (_server) {
00062             _cid = _wifi->listen(GSwifi::PROTO_UDP, _port);
00063         } else {
00064             _cid = _wifi->open(GSwifi::PROTO_UDP, remote.get_address(), remote.get_port(), _port);
00065         }
00066         if (_cid < 0) return -1;
00067     }
00068 
00069     tmr.start();
00070 
00071     while ((tmr.read_ms() < _timeout) || _blocking) {
00072 
00073         if (_server) {
00074             idx += _wifi->sendto(_cid, packet, length, remote.get_address(), remote.get_port());
00075         } else {
00076             idx += _wifi->send(_cid, packet, length);
00077         }
00078         if (idx < 0) {
00079             if (!_wifi->isConnected(_cid)) _cid = -1;
00080         }
00081 
00082         if (idx == length)
00083             return idx;
00084     }
00085     return (idx == 0) ? -1 : idx;
00086 }
00087 
00088 // -1 if unsuccessful, else number of bytes received
00089 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
00090 {
00091     Timer tmr;
00092     int idx = 0;
00093     int time = -1;
00094     char ip[16];
00095     int port;
00096 
00097     if (_cid < 0 && _wifi->isAssociated()) {
00098         // Socket open
00099         if (_server) {
00100             _cid = _wifi->listen(GSwifi::PROTO_UDP, _port);
00101         } else {
00102             _cid = _wifi->open(GSwifi::PROTO_UDP, remote.get_address(), remote.get_port(), _port);
00103         }
00104         if (_cid < 0) return -1;
00105     }
00106 
00107     if (_blocking) {
00108         tmr.start();
00109         while (time < _timeout + 20) {
00110             if (_wifi->readable(_cid)) {
00111                 break;
00112             }
00113             Thread::wait(1);
00114             time = tmr.read_ms();
00115         }
00116         if (time >= _timeout + 20) {
00117             return 0;
00118         }
00119     }
00120 
00121     tmr.reset();
00122     time = -1;
00123     tmr.start();
00124 
00125     while (time < _timeout) {
00126 
00127         if (_server) {
00128             idx += _wifi->recvfrom(_cid, &buffer[idx], length - idx, ip, &port);
00129         } else {
00130             idx += _wifi->recv(_cid, &buffer[idx], length - idx);
00131         }
00132         if (idx < 0) {
00133             if (!_wifi->isConnected(_cid)) _cid = -1;
00134             return -1;
00135         }
00136 
00137         if (idx == length || idx > 0) {
00138             break;
00139         }
00140         
00141         time = tmr.read_ms();
00142     }
00143 
00144     if (_server) {
00145         remote.set_address(ip, port);
00146     }
00147 //    return (idx == 0) ? -1 : idx;
00148     return idx;
00149 }