for EthernetInterface library compatibility.\\ ** Unoffical fix. may be a problem. **
Dependents: SNIC-httpclient-example SNIC-ntpclient-example
Fork of SNICInterface by
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 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License 00019 * port to the muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART. 00020 */ 00021 #include "TCPSocketConnection.h" 00022 #include <cstring> 00023 00024 TCPSocketConnection::TCPSocketConnection() 00025 { 00026 } 00027 00028 TCPSocketConnection::~TCPSocketConnection() 00029 { 00030 } 00031 00032 int TCPSocketConnection::connect( const char *host_p, unsigned short port) 00033 { 00034 int ret; 00035 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00036 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00037 00038 FUNC_IN(); 00039 // Socket create 00040 ret = createSocket(); 00041 if( ret != 0 ) 00042 { 00043 DEBUG_PRINT("createSocket error : %d\r\n", ret); 00044 FUNC_OUT(); 00045 return -1; 00046 } 00047 00048 int ip_addr = resolveHostName( host_p ); 00049 //lcd_printf("connect to [%s](%08x)\r\n", host_p, ip_addr); 00050 if( ( ip_addr == 0) || (ip_addr == -1) ) 00051 { 00052 DEBUG_PRINT("connect resolveHostName failed\r\n"); 00053 FUNC_OUT(); 00054 return -1; 00055 } 00056 00057 // Get buffer for response payload from MemoryPool 00058 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00059 if( payload_buf_p == NULL ) 00060 { 00061 DEBUG_PRINT("connect payload_buf_p NULL\r\n"); 00062 FUNC_OUT(); 00063 return -1; 00064 } 00065 00066 // IP address convert to number from strings. 00067 // unsigned int ip_addr = addrToInteger(ip_addr_p); 00068 00069 // 00070 C_SNIC_Core::tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T req; 00071 // Make request 00072 req.cmd_sid = UART_CMD_SID_SNIC_TCP_CONNECT_TO_SERVER_REQ; 00073 req.seq = mUartRequestSeq++; 00074 req.socket_id = mSocketID; 00075 00076 // set ip addr ( byte order ) 00077 C_SNIC_UartMsgUtil::convertIntToByteAdday( ip_addr, (char *)req.remote_addr ); 00078 req.remote_port[0] = ( (port & 0xFF00) >> 8 ); 00079 req.remote_port[1] = (port & 0xFF); 00080 req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 ); 00081 req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF); 00082 req.timeout = 60; 00083 00084 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00085 unsigned int command_len; 00086 // Preparation of command 00087 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req 00088 , sizeof(C_SNIC_Core::tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T), payload_buf_p->buf, command_array_p ); 00089 00090 uartCmdMgr_p->setCommandSID( UART_CMD_SID_SNIC_TCP_CONNECTION_STATUS_IND ); 00091 00092 // Send uart command request 00093 snic_core_p->sendUart( command_len, command_array_p ); 00094 00095 // Wait UART response 00096 ret = uartCmdMgr_p->wait(); 00097 if( ret != 0 ) 00098 { 00099 DEBUG_PRINT( "connect failed\r\n" ); 00100 snic_core_p->freeCmdBuf( payload_buf_p ); 00101 FUNC_OUT(); 00102 return -1; 00103 } 00104 00105 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_CONNECTION_UP ) 00106 { 00107 DEBUG_PRINT("connect status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); 00108 snic_core_p->freeCmdBuf( payload_buf_p ); 00109 FUNC_OUT(); 00110 return -1; 00111 } 00112 00113 snic_core_p->freeCmdBuf( payload_buf_p ); 00114 00115 // Initialize connection information 00116 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); 00117 if( con_info_p->recvbuf_p == NULL ) 00118 { 00119 DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", mSocketID); 00120 con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE); 00121 } 00122 con_info_p->is_connected = true; 00123 con_info_p->is_received = false; 00124 FUNC_OUT(); 00125 return 0; 00126 } 00127 00128 bool TCPSocketConnection::is_connected(void) 00129 { 00130 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00131 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); 00132 return con_info_p->is_connected; 00133 } 00134 00135 int TCPSocketConnection::send(char* data_p, int length) 00136 { 00137 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00138 C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); 00139 00140 FUNC_IN(); 00141 // Get buffer for response payload from MemoryPool 00142 tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); 00143 if( payload_buf_p == NULL ) 00144 { 00145 DEBUG_PRINT("connect payload_buf_p NULL\r\n"); 00146 FUNC_OUT(); 00147 return -1; 00148 } 00149 00150 C_SNIC_Core::tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T req; 00151 // Make request 00152 req.cmd_sid = UART_CMD_SID_SNIC_SEND_FROM_SOCKET_REQ; 00153 req.seq = mUartRequestSeq++; 00154 req.socket_id = mSocketID; 00155 req.option = 0; 00156 req.payload_len[0]= ( (length & 0xFF00) >> 8 ); 00157 req.payload_len[1]= (length & 0xFF); 00158 00159 int req_size = sizeof(C_SNIC_Core::tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T); 00160 char *send_buf_p = getSocketSendBuf(); 00161 memcpy( send_buf_p, &req, req_size ); 00162 memcpy( &send_buf_p[req_size], data_p, length ); 00163 00164 unsigned char *command_array_p = snic_core_p->getCommandBuf(); 00165 unsigned int command_len; 00166 // Preparation of command 00167 command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)send_buf_p 00168 , req_size + length, payload_buf_p->buf, command_array_p ); 00169 00170 // Send uart command request 00171 snic_core_p->sendUart( command_len, command_array_p ); 00172 00173 // Wait UART response 00174 int ret = uartCmdMgr_p->wait(); 00175 if( ret != 0 ) 00176 { 00177 DEBUG_PRINT( "send failed\r\n" ); 00178 snic_core_p->freeCmdBuf( payload_buf_p ); 00179 FUNC_OUT(); 00180 return -1; 00181 } 00182 00183 if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS ) 00184 { 00185 DEBUG_PRINT("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); 00186 snic_core_p->freeCmdBuf( payload_buf_p ); 00187 FUNC_OUT(); 00188 return -1; 00189 } 00190 snic_core_p->freeCmdBuf( payload_buf_p ); 00191 00192 // SNIC_SEND_FROM_SOCKET_REQ 00193 FUNC_OUT(); 00194 return length; 00195 } 00196 00197 int TCPSocketConnection::send_all(char *data_p, int length) 00198 { 00199 return send( data_p, length ); 00200 } 00201 00202 int TCPSocketConnection::receive(char* data_p, int length) 00203 { 00204 int i = 0; 00205 00206 FUNC_IN(); 00207 if( (data_p == NULL) || (length < 1) ) 00208 { 00209 DEBUG_PRINT("TCPSocketConnection::receive parameter error\r\n"); 00210 FUNC_OUT(); 00211 return -1; 00212 } 00213 00214 C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); 00215 // Initialize connection information 00216 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); 00217 if( con_info_p->recvbuf_p == NULL ) 00218 { 00219 DEBUG_PRINT("TCPSocketConnection::receive Conncection info error\r\n"); 00220 FUNC_OUT(); 00221 return -1; 00222 } 00223 00224 // Check connection 00225 if( con_info_p->is_connected == false ) 00226 { 00227 DEBUG_PRINT(" Socket id \"%d\" is not connected\r\n", mSocketID); 00228 FUNC_OUT(); 00229 return -1; 00230 } 00231 con_info_p->is_receive_complete = true; 00232 if( con_info_p->is_received == false ) 00233 { 00234 // Try receive 00235 Thread::yield(); 00236 00237 if( con_info_p->is_received == false ) 00238 { 00239 // No data received. 00240 FUNC_OUT(); 00241 return 0; 00242 } 00243 } 00244 // Get packet data from buffer for receive. 00245 for (i = 0; i < length; i ++) 00246 { 00247 if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false) 00248 { 00249 break; 00250 } 00251 } 00252 00253 if( con_info_p->recvbuf_p->isEmpty() ) 00254 { 00255 con_info_p->mutex.lock(); 00256 con_info_p->is_received = false; 00257 con_info_p->mutex.unlock(); 00258 } 00259 00260 FUNC_OUT(); 00261 return i; 00262 } 00263 00264 int TCPSocketConnection::receive_all(char* data_p, int length) 00265 { 00266 return receive( data_p, length ); 00267 } 00268 00269 void TCPSocketConnection::setAcceptSocket( int socket_id ) 00270 { 00271 FUNC_IN(); 00272 mSocketID = socket_id; 00273 }
Generated on Tue Jul 12 2022 18:43:50 by 1.7.2