for EthernetInterface library compatibility.\\ ** Unoffical fix. may be a problem. **

Dependents:   SNIC-httpclient-example SNIC-ntpclient-example

Fork of SNICInterface by muRata

Revision:
32:ae95309643aa
Parent:
29:6a0ba999597d
Child:
36:f33fcf5975ab
--- a/Socket/TCPSocketServer.cpp	Thu May 29 03:23:21 2014 +0000
+++ b/Socket/TCPSocketServer.cpp	Fri May 30 08:30:40 2014 +0000
@@ -28,6 +28,7 @@
  * $Revision: 0.0.0.1 $
  * ***********************************************************************/
 #include "TCPSocketServer.h"
+#include "SNIC_Core.h"
 
 #include <cstring>
 
@@ -39,17 +40,141 @@
 {
 }
 
-int TCPSocketServer::bind(int port) 
+int TCPSocketServer::bind(unsigned short port) 
 {
+    int ret;
+    C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
+    C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
+    
+    // Get local ip address.
+    // Get buffer for response payload from MemoryPool
+    tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
+    if( payload_buf == NULL )
+    {
+        printf("bind payload_buf NULL\r\n");
+        return -1;
+    }
+
+    C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T req;
+    // Make request
+    req.cmd_sid      = UART_CMD_SID_SNIC_GET_DHCP_INFO_REQ;
+    req.seq          = mUartRequestSeq++;
+    req.interface    = 0;
+    
+    unsigned char command_array[UART_REQUEST_PAYLOAD_MAX];
+    unsigned int  command_len;
+    // Preparation of command
+    command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
+                            , sizeof(C_SNIC_Core::tagSNIC_GET_DHCP_INFO_REQ_T), payload_buf->buf, command_array );
+    // Send uart command request
+    snic_core_p->sendUart( command_len, command_array );
+    // Wait UART response
+    ret = uartCmdMgr_p->wait();
+    if( ret != 0 )
+    {
+        printf( "bind failed\r\n" );
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    
+    if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
+    {
+        printf("bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    
+    snic_core_p->freeCmdBuf( payload_buf );
+    
+    unsigned int local_addr = (payload_buf->buf[9]  << 24)
+                            | (payload_buf->buf[10] << 16)
+                            | (payload_buf->buf[11] << 8)
+                            | (payload_buf->buf[12]);
+   
+    // Socket create
+    ret = createSocket( 1, local_addr, port );
+    if( ret != 0 )
+    {
+        printf("bind error : %d\r\n", ret);
+        return -1;
+    }
+
     return 0;
 }
 
 int TCPSocketServer::listen(int max)
 {
+    C_SNIC_Core               *snic_core_p  = C_SNIC_Core::getInstance();
+    C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand();
+    
+    // Get buffer for response payload from MemoryPool
+    tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf();
+    if( payload_buf == NULL )
+    {
+        printf("listen payload_buf NULL\r\n");
+        return -1;
+    }
+
+    C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T req;
+    // Make request
+    req.cmd_sid         = UART_CMD_SID_SNIC_TCP_CREATE_CONNECTION_REQ;
+    req.seq             = mUartRequestSeq++;
+    req.socket_id       = mSocketID;
+    req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 );
+    req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF);
+    req.max_client      = max;
+    
+    unsigned char command_array[UART_REQUEST_PAYLOAD_MAX];
+    unsigned int  command_len;
+    // Preparation of command
+    command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
+                        , sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_CONNECTION_REQ_T), payload_buf->buf, command_array );
+
+    int ret;
+    
+    // Send uart command request
+    snic_core_p->sendUart( command_len, command_array );
+    
+    // Wait UART response
+    ret = uartCmdMgr_p->wait();
+    if( ret != 0 )
+    {
+        printf( "listen failed\r\n" );
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    
+    if( uartCmdMgr_p->getCommandStatus() != 0 )
+    {
+        printf("listen status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+
+    snic_core_p->freeCmdBuf( payload_buf );
     return 0;
 }
 
-int TCPSocketServer::accept(TCPSocketConnection& connection)
+int TCPSocketServer::accept(TCPSocketConnection *connection)
 {
-    return 0;
+    C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance();
+    int          i;
+    int          ret = -1;
+    
+    C_SNIC_Core::tagCONNECT_INFO_T *con_info_p;
+    for( i = 0; i < MAX_SOCKET_ID+1; i++ )
+    {
+        // Get connection information
+        con_info_p = snic_core_p->getConnectInfo( i );
+        if( (con_info_p->is_connected == true)
+            && (con_info_p->is_accept == true)
+            && (con_info_p->parent_socket == mSocketID) )
+        {
+            // Set socket id
+            connection->setAcceptSocket( i );
+            ret = 0;
+        }   
+    }
+    
+    return ret;
 }