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 Takao Kishino

Files at this revision

API Documentation at this revision

Comitter:
kishino
Date:
Tue Jun 03 08:53:07 2014 +0000
Parent:
32:ae95309643aa
Child:
34:8c3527b8f44e
Commit message:
Created functions of UDP client and UDP server.

Changed in this revision

SNIC/SNIC_Core.cpp Show annotated file Show diff for this revision Revisions of this file
SNIC/SNIC_Core.h Show annotated file Show diff for this revision Revisions of this file
SNIC/SNIC_UartCommandManager.cpp Show annotated file Show diff for this revision Revisions of this file
SNIC/SNIC_UartCommandManager.h Show annotated file Show diff for this revision Revisions of this file
SNIC/SNIC_UartMsgUtil.h Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.h Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.h Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.h Show annotated file Show diff for this revision Revisions of this file
--- a/SNIC/SNIC_Core.cpp	Fri May 30 08:30:40 2014 +0000
+++ b/SNIC/SNIC_Core.cpp	Tue Jun 03 08:53:07 2014 +0000
@@ -61,6 +61,9 @@
     {
         mConnectInfo[i].recvbuf_p    = NULL;
         mConnectInfo[i].is_connected = false;
+        
+        mUdpRecvInfo[i].recvbuf_p    = NULL;
+        mUdpRecvInfo[i].is_received  = false;
     }
     
     mUartRecvThread_p         = NULL;
@@ -176,6 +179,15 @@
     return &mConnectInfo[socket_id];
 }
 
+C_SNIC_Core::tagUDP_RECVINFO_T  *C_SNIC_Core::getUdpRecvInfo( int socket_id )
+{
+    if( (socket_id < 0) || (socket_id > MAX_SOCKET_ID) )
+    {
+        return NULL;
+    }
+    return &mUdpRecvInfo[socket_id];
+}
+
 C_SNIC_UartCommandManager *C_SNIC_Core::getUartCommand()
 {
     return mUartCommand_p;
@@ -276,7 +288,7 @@
                 // Get payload from received data from UART.
                 int payload_len = C_SNIC_UartMsgUtil::getResponsePayload( uartRecvBuf_p->size, uartRecvBuf_p->buf
                                                         , &command_id, gUART_TEMP_BUF );
-                // Check receive a TCP or UDP packet
+                // Check receive a TCP packet
                 if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_CONNECTION_RECV_IND) )
                 {
                     // Packet buffering
@@ -288,6 +300,12 @@
                     // Connected from TCP client
                     uartCmdMgr_p->connectedTCPClient( gUART_TEMP_BUF, payload_len );
                 }
+                // Check receive UDP packet
+                else if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_UDP_RECV_IND) )
+                {
+                    // UDP packet buffering
+                    uartCmdMgr_p->bufferredUDPPacket( 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	Fri May 30 08:30:40 2014 +0000
+++ b/SNIC/SNIC_Core.h	Tue Jun 03 08:53:07 2014 +0000
@@ -75,8 +75,9 @@
 friend class C_SNIC_UartCommandManager;
 friend class C_SNIC_WifiInterface;
 friend class TCPSocketConnection;
+friend class TCPSocketServer;
+friend class UDPSocket;
 friend class Socket;
-friend class TCPSocketServer;
 
 private:
     /** Wi-Fi Network type
@@ -98,6 +99,16 @@
         bool                is_accept;
     }tagCONNECT_INFO_T;
 
+    /** UDP Recv information
+    */
+    typedef struct {
+        CircBuffer<char>    *recvbuf_p;
+        int                 from_ip;
+        short               from_port;
+        int                 parent_socket;
+        bool                is_received;
+    }tagUDP_RECVINFO_T;
+
     /** GEN_FW_VER_GET_REQ Command */
     typedef struct 
     {
@@ -184,6 +195,35 @@
         unsigned char timeout;
     }tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T;
     
+    /** SNIC_UDP_SIMPLE_SEND_REQ */
+    typedef struct
+    {
+        unsigned char cmd_sid;
+        unsigned char seq;
+        unsigned char remote_ip[4];
+        unsigned char remote_port[2];
+        unsigned char payload_len[2];
+    }tagSNIC_UDP_SIMPLE_SEND_REQ_T;
+
+    /** SNIC_UDP_CREATE_SOCKET_REQ */
+    typedef struct
+    {
+        unsigned char  cmd_sid;
+        unsigned char  seq;
+        unsigned char  bind;
+        unsigned char  local_addr[4];
+        unsigned char  local_port[2];
+    }tagSNIC_UDP_CREATE_SOCKET_REQ_T;
+    
+    /** SNIC_UDP_START_RECV_REQ */
+    typedef struct
+    {
+        unsigned char  cmd_sid;
+        unsigned char  seq;
+        unsigned char  socket_id;
+        unsigned char  recv_bufsize[2];
+    }tagSNIC_UDP_START_RECV_REQ_T;
+
     /** SNIC_GET_DHCP_INFO_REQ */
     typedef struct
     {
@@ -296,7 +336,14 @@
     */
     C_SNIC_Core::tagCONNECT_INFO_T   *getConnectInfo( int socket_id );
 
-    /**
+    /** 
+        Get pointer of UDP Recv information.
+        @param socket_id    Socket ID
+        @return The pointer of UDP Recv information
+    */
+    C_SNIC_Core::tagUDP_RECVINFO_T   *getUdpRecvInfo( int socket_id );
+
+                                /**
         Get pointer of the instance of C_SNIC_UartCommandManager.
         @return The pointer of the instance of C_SNIC_UartCommandManager.
     */
@@ -326,7 +373,10 @@
     /** Socket buffer */
     tagCONNECT_INFO_T   mConnectInfo[MAX_SOCKET_ID+1];
   
-    /** Constructor
+    /** UDP Information */
+    tagUDP_RECVINFO_T   mUdpRecvInfo[MAX_SOCKET_ID+1];
+
+/** Constructor
      */
     C_SNIC_Core();
 
--- a/SNIC/SNIC_UartCommandManager.cpp	Fri May 30 08:30:40 2014 +0000
+++ b/SNIC/SNIC_UartCommandManager.cpp	Tue Jun 03 08:53:07 2014 +0000
@@ -208,7 +208,7 @@
 
     if( con_info_p->recvbuf_p == NULL )
     {
-        printf( "create recv buffer[socket:%d]\r\n", socket_id);
+//        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;
@@ -216,3 +216,47 @@
     con_info_p->is_accept     = true;
     con_info_p->parent_socket = payload_p[2];
 }
+
+void C_SNIC_UartCommandManager::bufferredUDPPacket( unsigned char *payload_p, int payload_len )
+{
+    if( (payload_p == NULL) || (payload_len == 0) )
+    {
+        return;
+    }
+    
+    C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
+
+    // Get Connection information
+    C_SNIC_Core::tagUDP_RECVINFO_T *con_info_p = instance_p->getUdpRecvInfo( payload_p[2] );
+    if( con_info_p == NULL )
+    {
+        return;
+    }
+
+    if( con_info_p->recvbuf_p == NULL )
+    {
+//        printf( "create recv buffer[socket:%d]\r\n", payload_p[2]);
+        con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE);
+    }
+
+    con_info_p->is_received = true;
+    
+    // Set remote IP address and remote port
+    con_info_p->from_ip   = ((payload_p[3] << 24) | (payload_p[4] << 16) | (payload_p[5] << 8) | payload_p[6]);
+    con_info_p->from_port = ((payload_p[7] << 8)  | payload_p[8]);
+    
+    unsigned short recv_len;
+    // Get receive length from payload
+    recv_len= ((payload_p[9]<<8) & 0xFF00) | payload_p[10];
+    for( int i = 0; i < recv_len; i++ )
+    {
+        if( con_info_p->recvbuf_p->isFull() )
+        {
+            printf("Receive buffer is full.\r\n");
+            break;
+        }
+        
+        // Add to receive buffer
+        con_info_p->recvbuf_p->queue( payload_p[11+i] );
+    }
+}
--- a/SNIC/SNIC_UartCommandManager.h	Fri May 30 08:30:40 2014 +0000
+++ b/SNIC/SNIC_UartCommandManager.h	Tue Jun 03 08:53:07 2014 +0000
@@ -57,6 +57,7 @@
 friend class C_SNIC_WifiInterface;
 friend class TCPSocketConnection;
 friend class TCPSocketServer;
+friend class UDPSocket;
 friend class Socket;
 
 private:
@@ -109,10 +110,12 @@
     
     void bufferredPacket( unsigned char *payload_p, int payload_len );
     
+    void bufferredUDPPacket( unsigned char *payload_p, int payload_len );
+
     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/SNIC/SNIC_UartMsgUtil.h	Fri May 30 08:30:40 2014 +0000
+++ b/SNIC/SNIC_UartMsgUtil.h	Tue Jun 03 08:53:07 2014 +0000
@@ -134,6 +134,7 @@
 {
 friend class C_SNIC_Core;
 friend class C_SNIC_WifiInterface;
+friend class UDPSocket;
     
 private:
     C_SNIC_UartMsgUtil();
@@ -182,4 +183,4 @@
 
 };
 
-#endif /* _YD_WIFI_UART_MSG_H_ */
\ No newline at end of file
+#endif /* _YD_WIFI_UART_MSG_H_ */
--- a/Socket/Endpoint.cpp	Fri May 30 08:30:40 2014 +0000
+++ b/Socket/Endpoint.cpp	Tue Jun 03 08:53:07 2014 +0000
@@ -43,7 +43,7 @@
 
 void Endpoint::reset_address(void)
 {
-    _ipAddress[0] = '\0';
+    mIpAddress[0] = '\0';
 }
 
 #include "stdio.h"
@@ -52,15 +52,18 @@
 {
     reset_address();
     
+    strcpy( mIpAddress, host );
+    mPort = port;
+    
     return 0;
 }
 
 char* Endpoint::get_address()
 {
-    return 0;
+    return mIpAddress;
 }
 
 int   Endpoint::get_port()
 {
-    return 0;
+    return mPort;
 }
--- a/Socket/Endpoint.h	Fri May 30 08:30:40 2014 +0000
+++ b/Socket/Endpoint.h	Tue Jun 03 08:53:07 2014 +0000
@@ -67,7 +67,8 @@
     int get_port(void);
 
 protected:
-    char _ipAddress[17];
+    char mIpAddress[17];
+    int  mPort;
 //    struct sockaddr_in _remoteHost;
 
 };
--- a/Socket/Socket.cpp	Fri May 30 08:30:40 2014 +0000
+++ b/Socket/Socket.cpp	Tue Jun 03 08:53:07 2014 +0000
@@ -30,6 +30,7 @@
 #include "Socket.h"
 #include <cstring>
 
+char gSOCKET_SEND_BUF[2048] __attribute__((section("AHBSRAM1")));
 Socket::Socket()
 {
     mSocketID = -1;
@@ -244,3 +245,8 @@
     
     return ip_addr;
 }
+
+char *Socket::getSocketSendBuf()
+{
+    return gSOCKET_SEND_BUF;
+}
--- a/Socket/Socket.h	Fri May 30 08:30:40 2014 +0000
+++ b/Socket/Socket.h	Tue Jun 03 08:53:07 2014 +0000
@@ -92,6 +92,8 @@
     int _port;
 #endif
     
+    char *getSocketSendBuf();
+
 private:
 
 //    int select(struct timeval *timeout, bool read, bool write);
--- a/Socket/UDPSocket.cpp	Fri May 30 08:30:40 2014 +0000
+++ b/Socket/UDPSocket.cpp	Tue Jun 03 08:53:07 2014 +0000
@@ -29,7 +29,6 @@
  * ***********************************************************************/
 
 #include "Socket/UDPSocket.h"
-
 #include <cstring>
 
 UDPSocket::UDPSocket() {
@@ -45,19 +44,233 @@
 }
 
 // Server initialization
-int UDPSocket::bind(int port) 
+int UDPSocket::bind(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("UDP 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( "UDP bind failed\r\n" );
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    
+    if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
+    {
+        printf("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    
+    unsigned int local_addr = (payload_buf->buf[9]  << 24)
+                            | (payload_buf->buf[10] << 16)
+                            | (payload_buf->buf[11] << 8)
+                            | (payload_buf->buf[12]);
+
+
+    C_SNIC_Core::tagSNIC_UDP_CREATE_SOCKET_REQ_T create_req;
+    
+    // Make request
+    create_req.cmd_sid  = UART_CMD_SID_SNIC_UDP_CREATE_SOCKET_REQ;
+    create_req.seq      = mUartRequestSeq++;
+    create_req.bind     = 1;
+    // set ip addr ( byte order )
+    C_SNIC_UartMsgUtil::convertIntToByteAdday( local_addr, (char *)create_req.local_addr );
+    create_req.local_port[0] = ( (port & 0xFF00) >> 8 );
+    create_req.local_port[1] = (port & 0xFF);
+
+    // Preparation of command
+    command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, create_req.cmd_sid, (unsigned char *)&create_req
+                            , sizeof(C_SNIC_Core::tagSNIC_UDP_CREATE_SOCKET_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( "UDP bind failed\r\n" );
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+
+    if( uartCmdMgr_p->getCommandStatus() != 0 )
+    {
+        printf("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    mSocketID = payload_buf->buf[3];
+    
+    C_SNIC_Core::tagSNIC_UDP_START_RECV_REQ_T recv_start_req;
+    
+    // Make request
+    recv_start_req.cmd_sid         = UART_CMD_SID_SNIC_UDP_START_RECV_REQ;
+    recv_start_req.seq             = mUartRequestSeq++;
+    recv_start_req.socket_id       = mSocketID;
+    recv_start_req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 );
+    recv_start_req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF);
+
+    // Preparation of command
+    command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, recv_start_req.cmd_sid, (unsigned char *)&recv_start_req
+                            , sizeof(C_SNIC_Core::tagSNIC_UDP_START_RECV_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( "UDP recv start failed\r\n" );
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+
+    if( uartCmdMgr_p->getCommandStatus() != 0 )
+    {
+        printf("UDP recv start status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+
+    snic_core_p->freeCmdBuf( payload_buf );
+    
     return 0;
 }
 
 // -1 if unsuccessful, else number of bytes written
 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
 {
+    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("connect payload_buf NULL\r\n");
+        return -1;
+    }
+    
+    C_SNIC_Core::tagSNIC_UDP_SIMPLE_SEND_REQ_T req;
+    // Make request
+    req.cmd_sid       = UART_CMD_SID_SNIC_UDP_SIMPLE_SEND_REQ;
+    req.seq           = mUartRequestSeq++;
+
+    int addr_temp;
+    addr_temp = C_SNIC_UartMsgUtil::addrToInteger( remote.get_address() );
+    C_SNIC_UartMsgUtil::convertIntToByteAdday( addr_temp, (char *)req.remote_ip );
+    req.remote_port[0]= ( (remote.get_port() & 0xFF00) >> 8 );
+    req.remote_port[1]= (remote.get_port() & 0xFF);
+    req.payload_len[0]= ( (length & 0xFF00) >> 8 );
+    req.payload_len[1]= (length & 0xFF);
+    
+    int req_size = sizeof(C_SNIC_Core::tagSNIC_UDP_SIMPLE_SEND_REQ_T);
+    
+    char *send_buf_p = getSocketSendBuf();
+    memcpy( send_buf_p, &req, req_size );
+    memcpy( &send_buf_p[req_size], packet, length );
+    
+    unsigned char *command_array = snic_core_p->getCommandBuf();
+    unsigned int   command_len;
+    // Preparation of command
+    command_len = snic_core_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)send_buf_p
+                            , req_size + length, payload_buf->buf, command_array );
+
+    // Send uart command request
+    snic_core_p->sendUart( command_len, command_array );
+
+    // Wait UART response
+    int ret = uartCmdMgr_p->wait();
+    if( ret != 0 )
+    {
+        printf( "send failed\r\n" );
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    
+    if( uartCmdMgr_p->getCommandStatus() != UART_CMD_RES_SNIC_SUCCESS )
+    {
+        printf("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus());
+        snic_core_p->freeCmdBuf( payload_buf );
+        return -1;
+    }
+    snic_core_p->freeCmdBuf( payload_buf );
+
+    // SNIC_SEND_FROM_SOCKET_REQ
     return 0;
 }
 
 // -1 if unsuccessful, else number of bytes received
-int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
+int UDPSocket::receiveFrom(Endpoint &remote, char *data_p, int length)
 {
-    return 0;
+    if( (data_p == NULL) || (length < 1) )
+    {
+        printf("UDPSocket::receiveFrom parameter error\r\n");
+        return -1;
+    }
+    
+    C_SNIC_Core                    *snic_core_p  = C_SNIC_Core::getInstance();
+    // Initialize connection information
+    C_SNIC_Core::tagUDP_RECVINFO_T *con_info_p = snic_core_p->getUdpRecvInfo( mSocketID );
+    if( con_info_p->recvbuf_p == NULL )
+    {
+//        printf("UDPSocket::receiveFrom Conncection info error\r\n");
+        return 0;
+    }
+    if( con_info_p->is_received == false )
+    {
+        return 0;
+    }
+
+    char remote_ip[20] = {'\0'};
+    sprintf( remote_ip, "%d.%d.%d.%d"
+        , (con_info_p->from_ip >>24) & 0x000000ff
+        , (con_info_p->from_ip >>16) & 0x000000ff
+        , (con_info_p->from_ip >>8)  & 0x000000ff
+        , (con_info_p->from_ip)      & 0x000000ff );
+    remote.set_address( remote_ip, con_info_p->from_port );
+    
+    int i;
+    // Get packet data from buffer for receive.
+    for (i = 0; i < length; i ++) 
+    {
+        if (con_info_p->recvbuf_p->dequeue(&data_p[i]) == false)
+        {
+            break;
+        }
+    }
+
+    if( con_info_p->recvbuf_p->isEmpty() )
+    {
+        con_info_p->is_received = false;
+    }
+
+    return i;
 }
--- a/Socket/UDPSocket.h	Fri May 30 08:30:40 2014 +0000
+++ b/Socket/UDPSocket.h	Tue Jun 03 08:53:07 2014 +0000
@@ -54,7 +54,7 @@
     \param port The port to listen for incoming connections on
     \return 0 on success, -1 on failure.
     */
-    int bind(int port);
+    int bind(short port);
     
     /** Send a packet to a remote endpoint
     \param remote   The remote endpoint