SNIC UART Interface library: Serial to Wi-Fi library for Murata TypeYD Wi-Fi module. For more information about TypeYD: http://www.murata.co.jp/products/microwave/module/lbwb1zzydz/index.html
Dependents: SNIC-xively-jumpstart-demo SNIC-FluentLogger-example TCPEchoServer murataDemo ... more
Fork of YDwifiInterface by
Revision 32:ae95309643aa, committed 2014-05-30
- Comitter:
- kishino
- Date:
- Fri May 30 08:30:40 2014 +0000
- Parent:
- 31:15c22824cc46
- Child:
- 33:33f1bc919486
- Commit message:
- Implemented a API of TCP server.
Changed in this revision
--- a/SNIC/SNIC_Core.cpp Thu May 29 03:23:21 2014 +0000
+++ b/SNIC/SNIC_Core.cpp Fri May 30 08:30:40 2014 +0000
@@ -262,18 +262,16 @@
{
uartRecvBuf_p = (tagMEMPOOL_BLOCK_T *)evt.value.p;
+#if 0
{
int i;
- printf("[rcv]:%d",uartRecvBuf_p->size );
-/*
for(i=0;i<uartRecvBuf_p->size;i++)
{
printf("%02x", uartRecvBuf_p->buf[i]);
}
-*/
printf("\r\n");
}
-
+#endif
unsigned char command_id;
// Get payload from received data from UART.
int payload_len = C_SNIC_UartMsgUtil::getResponsePayload( uartRecvBuf_p->size, uartRecvBuf_p->buf
@@ -284,6 +282,12 @@
// Packet buffering
uartCmdMgr_p->bufferredPacket( gUART_TEMP_BUF, payload_len );
}
+ // Check connected from TCP client
+ else if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_TCP_CLIENT_SOCKET_IND) )
+ {
+ // Connected from TCP client
+ uartCmdMgr_p->connectedTCPClient( gUART_TEMP_BUF, payload_len );
+ }
// Check scan results indication
else if( (command_id == UART_CMD_ID_WIFI) && (gUART_TEMP_BUF[0] == UART_CMD_SID_WIFI_SCAN_RESULT_IND) )
{
--- a/SNIC/SNIC_Core.h Thu May 29 03:23:21 2014 +0000
+++ b/SNIC/SNIC_Core.h Fri May 30 08:30:40 2014 +0000
@@ -28,6 +28,7 @@
#define MAX_SOCKET_ID 5
#define MEMPOOL_UART_RECV_NUM 2
+#define SNIC_UART_RECVBUF_SIZE 2048
/** Wi-Fi security
*/
@@ -75,6 +76,7 @@
friend class C_SNIC_WifiInterface;
friend class TCPSocketConnection;
friend class Socket;
+friend class TCPSocketServer;
private:
/** Wi-Fi Network type
@@ -92,6 +94,8 @@
CircBuffer<char> *recvbuf_p;
bool is_connected;
bool is_received;
+ int parent_socket;
+ bool is_accept;
}tagCONNECT_INFO_T;
/** GEN_FW_VER_GET_REQ Command */
@@ -158,6 +162,16 @@
unsigned char payload_len[2];
}tagSNIC_TCP_SEND_FROM_SOCKET_REQ_T;
+ /** SNIC_TCP_CREATE_CONNECTION_REQ */
+ typedef struct
+ {
+ unsigned char cmd_sid;
+ unsigned char seq;
+ unsigned char socket_id;
+ unsigned char recv_bufsize[2];
+ unsigned char max_client;
+ }tagSNIC_TCP_CREATE_CONNECTION_REQ_T;
+
/** SNIC_TCP_CONNECT_TO_SERVER_REQ */
typedef struct
{
@@ -170,6 +184,14 @@
unsigned char timeout;
}tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T;
+ /** SNIC_GET_DHCP_INFO_REQ */
+ typedef struct
+ {
+ unsigned char cmd_sid;
+ unsigned char seq;
+ unsigned char interface;
+ }tagSNIC_GET_DHCP_INFO_REQ_T;
+
/** WIFI_ON_REQ Command */
typedef struct
{
--- a/SNIC/SNIC_UartCommandManager.cpp Thu May 29 03:23:21 2014 +0000
+++ b/SNIC/SNIC_UartCommandManager.cpp Fri May 30 08:30:40 2014 +0000
@@ -183,3 +183,36 @@
}
con_info_p->is_received = true;
}
+
+void C_SNIC_UartCommandManager::connectedTCPClient( unsigned char *payload_p, int payload_len )
+{
+ if( (payload_p == NULL) || (payload_len == 0) )
+ {
+ return;
+ }
+
+ C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
+ int socket_id;
+
+ // Get socket id of client from payload
+ socket_id = payload_p[3];
+
+ printf("[connectedTCPClient] socket id:%d\r\n", socket_id);
+
+ // Get Connection information
+ C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = instance_p->getConnectInfo( socket_id );
+ if( con_info_p == NULL )
+ {
+ return;
+ }
+
+ if( con_info_p->recvbuf_p == NULL )
+ {
+ printf( "create recv buffer[socket:%d]\r\n", socket_id);
+ con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE);
+ }
+ con_info_p->is_connected = true;
+ con_info_p->is_received = false;
+ con_info_p->is_accept = true;
+ con_info_p->parent_socket = payload_p[2];
+}
--- a/SNIC/SNIC_UartCommandManager.h Thu May 29 03:23:21 2014 +0000
+++ b/SNIC/SNIC_UartCommandManager.h Fri May 30 08:30:40 2014 +0000
@@ -56,6 +56,7 @@
friend class C_SNIC_Core;
friend class C_SNIC_WifiInterface;
friend class TCPSocketConnection;
+friend class TCPSocketServer;
friend class Socket;
private:
@@ -110,6 +111,8 @@
void scanResultIndicate( unsigned char *payload_p, int payload_len );
+ void connectedTCPClient( unsigned char *payload_p, int payload_len );
+
/** Checks in the command which is waiting from Command ID and Sub ID.
@param command_id Command ID
@param payload_p Command payload
--- a/Socket/Socket.cpp Thu May 29 03:23:21 2014 +0000
+++ b/Socket/Socket.cpp Fri May 30 08:30:40 2014 +0000
@@ -132,12 +132,15 @@
req_len++;
if( bind != 0 )
{
-/*
- req.local_addr = local_addr;
- req_len++;
- req.local_port = port;
- req_len++;
-*/
+ // set ip addr ( byte order )
+ req.local_addr[0] = ( (local_addr & 0xFF000000) >> 24 );
+ req.local_addr[1] = ( (local_addr & 0xFF0000) >> 16 );
+ req.local_addr[2] = ( (local_addr & 0xFF00) >> 8 );
+ req.local_addr[3] = (local_addr & 0xFF);
+ req.local_port[0] = ( (port & 0xFF00) >> 8 );
+ req.local_port[1] = (port & 0xFF);
+
+ req_len = sizeof(C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T);
}
unsigned char *command_array = snic_core_p->getCommandBuf();
--- a/Socket/Socket.h Thu May 29 03:23:21 2014 +0000
+++ b/Socket/Socket.h Fri May 30 08:30:40 2014 +0000
@@ -35,8 +35,6 @@
typedef unsigned long socklen_t;
-#define SNIC_UART_RECVBUF_SIZE 2048
-
/** Socket file descriptor and select wrapper
*/
class Socket {
--- a/Socket/TCPSocketConnection.cpp Thu May 29 03:23:21 2014 +0000
+++ b/Socket/TCPSocketConnection.cpp Fri May 30 08:30:40 2014 +0000
@@ -255,3 +255,8 @@
return i;
}
+
+void TCPSocketConnection::setAcceptSocket( int socket_id )
+{
+ mSocketID = socket_id;
+}
--- a/Socket/TCPSocketConnection.h Thu May 29 03:23:21 2014 +0000
+++ b/Socket/TCPSocketConnection.h Fri May 30 08:30:40 2014 +0000
@@ -80,6 +80,7 @@
*/
int receive(char *data_p, int length);
+ void setAcceptSocket( int socket_id );
private:
};
--- 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;
}
--- a/Socket/TCPSocketServer.h Thu May 29 03:23:21 2014 +0000
+++ b/Socket/TCPSocketServer.h Fri May 30 08:30:40 2014 +0000
@@ -48,7 +48,7 @@
\param port The port to listen for incoming connections on.
\return 0 on success, -1 on failure.
*/
- int bind(int port);
+ int bind(unsigned short port);
/** Start listening for incoming connections.
\param backlog number of pending connections that can be queued up at any
@@ -61,7 +61,7 @@
\param connection A TCPSocketConnection instance that will handle the incoming connection.
\return 0 on success, -1 on failure.
*/
- int accept(TCPSocketConnection& connection);
+ int accept(TCPSocketConnection *connection);
};
#endif
muRata

Murata TypeYD