Ethernet for Nucleo and Disco board STM32F746 works with gcc and arm. IAC is untested
Dependents: STM32F746_iothub_client_sample_mqtt DISCO-F746NG_Ethernet Nucleo_F746ZG_Ethernet thethingsiO-DISCO_F746NG-mqtt ... more
TCPSocketConnection.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 #include "TCPSocketConnection.h" 00019 #include <cstring> 00020 00021 using std::memset; 00022 using std::memcpy; 00023 00024 TCPSocketConnection::TCPSocketConnection() : 00025 _is_connected(false) { 00026 } 00027 00028 int TCPSocketConnection::set_source_port(const int port) { 00029 tcp_preselect_port(port); 00030 return 0; 00031 } 00032 00033 int TCPSocketConnection::connect(const char* host, const int port) { 00034 00035 if (init_socket(SOCK_STREAM) < 0) 00036 return -1; 00037 00038 if (set_address(host, port) != 0) 00039 return -1; 00040 00041 if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) { 00042 close(); 00043 return -1; 00044 } 00045 _is_connected = true; 00046 00047 return 0; 00048 } 00049 00050 bool TCPSocketConnection::is_connected(void) { 00051 if(_sock_fd<0)_is_connected=false; 00052 return _is_connected; 00053 } 00054 00055 int TCPSocketConnection::send(char* data, int length) { 00056 if ((_sock_fd < 0) || !_is_connected) 00057 return -1; 00058 00059 if (!_blocking) { 00060 TimeInterval timeout(_timeout); 00061 if (wait_writable(timeout) != 0) 00062 return -1; 00063 } 00064 00065 int n = lwip_send(_sock_fd, data, length, 0); 00066 _is_connected = (n != 0); 00067 00068 return n; 00069 } 00070 00071 00072 // -1 if unsuccessful, else number of bytes written 00073 int TCPSocketConnection::send_all(char* data, int length) { 00074 if ((_sock_fd < 0) || !_is_connected) 00075 return -1; 00076 00077 int writtenLen = 0; 00078 TimeInterval timeout(_timeout); 00079 while (writtenLen < length) { 00080 if (!_blocking) { 00081 // Wait for socket to be writeable 00082 if (wait_writable(timeout) != 0) 00083 return writtenLen; 00084 } 00085 00086 int ret = lwip_send(_sock_fd, data + writtenLen, length - writtenLen, 0); 00087 if (ret > 0) { 00088 writtenLen += ret; 00089 continue; 00090 } else if (ret == 0) { 00091 _is_connected = false; 00092 return writtenLen; 00093 } else { 00094 return -1; //Connnection error 00095 } 00096 } 00097 return writtenLen; 00098 } 00099 00100 int TCPSocketConnection::receive(char* data, int length) { 00101 if ((_sock_fd < 0) || !_is_connected) 00102 return -1; 00103 00104 if (!_blocking) { 00105 TimeInterval timeout(_timeout); 00106 if (wait_readable(timeout) != 0) 00107 return -1; 00108 } 00109 00110 int n = lwip_recv(_sock_fd, data, length, 0); 00111 _is_connected = (n != 0); 00112 00113 return n; 00114 } 00115 00116 // -1 if unsuccessful, else number of bytes received 00117 int TCPSocketConnection::receive_all(char* data, int length) { 00118 if ((_sock_fd < 0) || !_is_connected) 00119 return -1; 00120 00121 int readLen = 0; 00122 TimeInterval timeout(_timeout); 00123 while (readLen < length) { 00124 if (!_blocking) { 00125 //Wait for socket to be readable 00126 if (wait_readable(timeout) != 0) 00127 return readLen; 00128 } 00129 00130 int ret = lwip_recv(_sock_fd, data + readLen, length - readLen, 0); 00131 if (ret > 0) { 00132 readLen += ret; 00133 } else if (ret == 0) { 00134 _is_connected = false; 00135 return readLen; 00136 } else { 00137 return -1; //Connnection error 00138 } 00139 } 00140 return readLen; 00141 }
Generated on Tue Jul 12 2022 18:14:55 by 1.7.2