Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ESP8266NodeMCUInterface by
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 if(!wifi->open(ESP_UDP_TYPE, remote._ipAddress, remote._port, remote._id)) { 00057 return(-1); 00058 } 00059 endpoint_configured = true; 00060 } 00061 00062 tmr.start(); 00063 00064 while ((tmr.read_ms() < _timeout) || _blocking) { 00065 00066 idx += wifi->send(packet, length); 00067 00068 if (idx == length) 00069 return idx; 00070 } 00071 return (idx == 0) ? -1 : idx; 00072 } 00073 00074 // -1 if unsuccessful, else number of bytes received 00075 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) 00076 { 00077 Timer tmr; 00078 int idx = 0; 00079 int nb_available = 0; 00080 int time = -1; 00081 00082 //make this the non-blocking case and return if <= 0 00083 // remember to change the config to blocking 00084 // if ( ! _blocking) { 00085 // if ( wifi.readable <= 0 ) { 00086 // return (wifi.readable); 00087 // } 00088 // } 00089 //--- 00090 tmr.start(); 00091 if (_blocking) { 00092 while (1) { 00093 nb_available = wifi->readable(); 00094 if (nb_available != 0) { 00095 break; 00096 } 00097 } 00098 } 00099 //--- 00100 // blocking case 00101 else { 00102 tmr.reset(); 00103 00104 while (time < _timeout) { 00105 nb_available = wifi->readable(); 00106 if (nb_available < 0) return nb_available; 00107 if (nb_available > 0) break ; 00108 time = tmr.read_ms(); 00109 } 00110 00111 if (nb_available == 0) return nb_available; 00112 } 00113 00114 // change this to < 20 mS timeout per byte to detect end of packet gap 00115 // this may not work due to buffering at the UART interface 00116 tmr.reset(); 00117 // while ( tmr.read_ms() < 20 ) { 00118 // if ( wifi.readable() && (idx < length) ) { 00119 // buffer[idx++] = wifi->getc(); 00120 // tmr.reset(); 00121 // } 00122 // if ( idx == length ) { 00123 // break; 00124 // } 00125 // } 00126 //--- 00127 while (time < _timeout) { 00128 00129 nb_available = wifi->readable(); 00130 //for (int i = 0; i < min(nb_available, length); i++) { 00131 for (int i = 0; i < min(nb_available, (length-idx)); i++) { 00132 buffer[idx] = wifi->getc(); 00133 idx++; 00134 } 00135 if (idx == length) { 00136 break; 00137 } 00138 time = tmr.read_ms(); 00139 } 00140 //--- 00141 readEndpoint(remote); 00142 return (idx == 0) ? -1 : idx; 00143 } 00144 00145 bool UDPSocket::confEndpoint(Endpoint & ep) 00146 { 00147 currentEndpoint = ep; 00148 return true; 00149 } 00150 00151 bool UDPSocket::readEndpoint(Endpoint & ep) 00152 { 00153 ep = currentEndpoint; 00154 return true; 00155 }
Generated on Thu Jul 14 2022 16:33:07 by
1.7.2
