Sarah Marsh / ESP8266Interface

Fork of ESP8266Interface by ESP8266

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 
00019 #include "UDPSocket.h"
00020 
00021 #include <string>
00022 #include <algorithm>
00023 
00024 extern Serial pc;
00025 
00026 UDPSocket::UDPSocket()
00027 {
00028     endpoint_configured = false;
00029     endpoint_read = false;
00030     Endpoint currentEndpoint;
00031 }
00032 
00033 int UDPSocket::init(void)
00034 {
00035     return 0;
00036 }
00037 
00038 // Server initialization
00039 int UDPSocket::bind(int port)
00040 {
00041     return 0;
00042 }
00043 
00044 // -1 if unsuccessful, else number of bytes written
00045 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
00046 {
00047     Timer tmr;
00048     int idx = 0;
00049 
00050 
00051     confEndpoint(remote);
00052 
00053     // initialize transparent mode if not already done
00054     if(!endpoint_configured) {
00055         // initialize UDP (default id of -1 means transparent mode)
00056         //!wifi->start(ESP_UDP_TYPE, remote._ipAddress, remote._port, remote._id
00057         if(!wifi->startUDP(remote._ipAddress, remote._port, 0,length)) {
00058             return(-1);
00059         }
00060         endpoint_configured = true;
00061     }
00062 
00063     tmr.start();
00064 
00065     while ((tmr.read_ms() < _timeout) || _blocking) {
00066 
00067         idx += wifi->send(packet, length);
00068 
00069         if (idx == length)
00070             return idx;
00071     }
00072     return (idx == 0) ? -1 : idx;
00073 }
00074 
00075 // -1 if unsuccessful, else number of bytes received
00076 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
00077 {
00078     Timer tmr;
00079     int idx = 0;
00080     int nb_available = 0;
00081     int time = -1;
00082 
00083     //make this the non-blocking case and return if <= 0
00084     // remember to change the config to blocking
00085     // if ( ! _blocking) {
00086     // if ( wifi.readable <= 0 ) {
00087     // return (wifi.readable);
00088     // }
00089     // }
00090     //---
00091     tmr.start();
00092     if (_blocking) {
00093         while (1) {
00094             nb_available = wifi->readable();
00095             if (nb_available != 0) {
00096                 break;
00097             }
00098         }
00099     }
00100     //---
00101     // blocking case
00102     else {
00103         tmr.reset();
00104 
00105         while (time < _timeout) {
00106             nb_available = wifi->readable();
00107             if (nb_available < 0) return nb_available;
00108             if (nb_available > 0) break ;
00109             time = tmr.read_ms();
00110         }
00111 
00112         if (nb_available == 0) return nb_available;
00113     }
00114 
00115     // change this to < 20 mS timeout per byte to detect end of packet gap
00116     // this may not work due to buffering at the UART interface
00117     tmr.reset();
00118     // while ( tmr.read_ms() < 20 ) {
00119     // if ( wifi.readable() && (idx < length) ) {
00120     // buffer[idx++] = wifi->getc();
00121     // tmr.reset();
00122     // }
00123     // if ( idx == length ) {
00124     // break;
00125     // }
00126     // }
00127     //---
00128     while (time < _timeout) {
00129 
00130         nb_available = wifi->readable();
00131         //for (int i = 0; i < min(nb_available, length); i++) {
00132         for (int i = 0; i < min(nb_available, (length-idx)); i++) {
00133             buffer[idx] = wifi->getc();
00134             idx++;
00135         }
00136         if (idx == length) {
00137             break;
00138         }
00139         time = tmr.read_ms();
00140     }
00141     //---
00142     readEndpoint(remote);
00143     return (idx == 0) ? -1 : idx;
00144 }
00145 
00146 bool UDPSocket::confEndpoint(Endpoint & ep)
00147 {
00148     currentEndpoint = ep;
00149     return true;
00150 }
00151 
00152 bool UDPSocket::readEndpoint(Endpoint & ep)
00153 {
00154     ep = currentEndpoint;
00155     return true;
00156 }