Host library for controlling a WiConnect enabled Wi-Fi module.
Dependents: wiconnect-ota_example wiconnect-web_setup_example wiconnect-test-console wiconnect-tcp_server_example ... more
TCPSocketConnection.cpp
00001 /** 00002 * ACKme WiConnect Host Library is licensed under the BSD licence: 00003 * 00004 * Copyright (c)2014 ACKme Networks. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without modification, 00008 * are permitted provided that the following conditions are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright notice, 00011 * this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form must reproduce the above copyright notice, 00013 * this list of conditions and the following disclaimer in the documentation 00014 * and/or other materials provided with the distribution. 00015 * 3. The name of the author may not be used to endorse or promote products 00016 * derived from this software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS AND ANY EXPRESS OR IMPLIED 00019 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00020 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00021 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00022 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00023 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00026 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00027 * OF SUCH DAMAGE. 00028 */ 00029 00030 #include "Wiconnect.h" 00031 #include "api/types/Socket/TCPSocketConnection.h" 00032 00033 00034 00035 /*************************************************************************************************/ 00036 TCPSocketConnection::TCPSocketConnection(int rxBufferLen, void *rxBuffer, int txBufferLen, void *txBuffer) 00037 : Socket(rxBufferLen, rxBuffer, txBufferLen, txBuffer) 00038 { 00039 } 00040 00041 /*************************************************************************************************/ 00042 int TCPSocketConnection::connect(const char* host, const int port) 00043 { 00044 return (Wiconnect::getInstance()->tcpConnect(socket, host, (uint16_t)port) == WICONNECT_SUCCESS) ? 0 : -1; 00045 } 00046 00047 /*************************************************************************************************/ 00048 bool TCPSocketConnection::is_connected(void) 00049 { 00050 return socket.isConnected(); 00051 } 00052 00053 /*************************************************************************************************/ 00054 // currently there's no way to timeout writing data to WiConnect, so we just have to try and send it all. 00055 // NOTE: this WILL be fixed in the next version 00056 int TCPSocketConnection::send(char* data, int length) 00057 { 00058 return send_all(data, length); 00059 } 00060 00061 /*************************************************************************************************/ 00062 // -1 if unsuccessful, else number of bytes written 00063 int TCPSocketConnection::send_all(char* data, int length) 00064 { 00065 return (socket.write(data, length, true) == WICONNECT_SUCCESS) ? length : -1; 00066 } 00067 00068 /*************************************************************************************************/ 00069 int TCPSocketConnection::receive(char* data, int length) 00070 { 00071 uint16_t bytesRead; 00072 00073 if (!_blocking) 00074 { 00075 if(socket.getRxBufferSize() == 0 || socket.getRxBufferBytesPending() == 0) 00076 { 00077 TimeoutTimer timer; 00078 00079 for(;;) 00080 { 00081 bool hasData; 00082 00083 if(socket.poll(&hasData) != WICONNECT_SUCCESS) 00084 { 00085 return -1; 00086 } 00087 else if(hasData) 00088 { 00089 break; 00090 } 00091 else if(timer.timedOut(_timeout)) 00092 { 00093 return -1; 00094 } 00095 } 00096 } 00097 } 00098 00099 if(length == 1 && socket.getRxBufferSize() > 0) 00100 { 00101 return (socket.getc((uint8_t*)data) == WICONNECT_SUCCESS) ? 1 : -1; 00102 } 00103 else 00104 { 00105 for(;;) 00106 { 00107 if(socket.read(data, length, &bytesRead) != WICONNECT_SUCCESS) 00108 { 00109 return -1; 00110 } 00111 else if(bytesRead > 0) 00112 { 00113 return bytesRead; 00114 } 00115 } 00116 } 00117 } 00118 00119 /*************************************************************************************************/ 00120 // -1 if unsuccessful, else number of bytes received 00121 int TCPSocketConnection::receive_all(char* data, int length) 00122 { 00123 char *ptr = data; 00124 int totReadSize = length; 00125 00126 while(length > 0) 00127 { 00128 int bytesRead = receive(ptr, length); 00129 if(bytesRead == -1) 00130 { 00131 return -1; 00132 } 00133 ptr += bytesRead; 00134 length -= bytesRead; 00135 } 00136 00137 return totReadSize; 00138 } 00139 00140
Generated on Tue Jul 12 2022 17:35:58 by 1.7.2