SNICInterface for development
Fork of SNICInterface by
Diff: Socket/UDPSocket.cpp
- Revision:
- 43:d80bbb12ffe6
- Parent:
- 41:1c1b5ad4d491
--- a/Socket/UDPSocket.cpp Fri Sep 19 01:46:17 2014 +0000 +++ b/Socket/UDPSocket.cpp Fri Nov 14 00:52:31 2014 +0000 @@ -40,12 +40,14 @@ C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + FUNC_IN(); // Get local ip address. // Get buffer for response payload from MemoryPool tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); if( payload_buf_p == NULL ) { DEBUG_PRINT("UDP bind payload_buf_p NULL\r\n"); + FUNC_OUT(); return -1; } @@ -68,6 +70,7 @@ { DEBUG_PRINT( "UDP bind failed\r\n" ); snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); return -1; } @@ -75,6 +78,7 @@ { DEBUG_PRINT("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); return -1; } @@ -107,6 +111,7 @@ { DEBUG_PRINT( "UDP bind failed\r\n" ); snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); return -1; } @@ -114,10 +119,21 @@ { DEBUG_PRINT("UDP bind status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); return -1; } mSocketID = payload_buf_p->buf[3]; + // Initialize connection information + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); + if( con_info_p->recvbuf_p == NULL ) + { + DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", mSocketID); + con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE); + } + con_info_p->is_connected = true; + con_info_p->is_received = false; + C_SNIC_Core::tagSNIC_UDP_START_RECV_REQ_T recv_start_req; // Make request @@ -139,6 +155,7 @@ { DEBUG_PRINT( "UDP recv start failed\r\n" ); snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); return -1; } @@ -146,11 +163,12 @@ { DEBUG_PRINT("UDP recv start status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); return -1; } snic_core_p->freeCmdBuf( payload_buf_p ); - + FUNC_OUT(); return 0; } @@ -160,28 +178,51 @@ C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); + osThreadId tid = Thread::gettid(); + +// pc.printf("send[%08x] len:%d(%04x)\r\n", tid, length, length); + +#if 0 // TODO: Not wait for command response(Tentative) + snic_core_p->lockAPI(); +#endif + FUNC_IN(); + +#if 0 // TODO: Not wait for command response(Tentative) // Get buffer for response payload from MemoryPool tagMEMPOOL_BLOCK_T *payload_buf_p = snic_core_p->allocCmdBuf(); if( payload_buf_p == NULL ) { DEBUG_PRINT("connect payload_buf_p NULL\r\n"); + FUNC_OUT(); + snic_core_p->unlockAPI(); return -1; } - - C_SNIC_Core::tagSNIC_UDP_SIMPLE_SEND_REQ_T req; +#endif + + C_SNIC_Core::tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T req; // Make request - req.cmd_sid = UART_CMD_SID_SNIC_UDP_SIMPLE_SEND_REQ; + req.cmd_sid = UART_CMD_SID_SNIC_UDP_SEND_FROM_SOCKET_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); + 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); + req.socket_id = mSocketID; + req.connection_mode = 1; - int req_size = sizeof(C_SNIC_Core::tagSNIC_UDP_SIMPLE_SEND_REQ_T); + // Initialize connection information + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); + if( con_info_p != NULL ) + { + con_info_p->from_ip = addr_temp; + con_info_p->from_port = remote.get_port(); + } + + int req_size = sizeof(C_SNIC_Core::tagSNIC_UDP_SEND_FROM_SOCKET_REQ_T); char *send_buf_p = getSocketSendBuf(); memcpy( send_buf_p, &req, req_size ); @@ -189,19 +230,22 @@ unsigned char *command_array_p = 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_p->buf, command_array_p ); + + // Make all command request + command_len = C_SNIC_UartMsgUtil::makeRequest( UART_CMD_ID_SNIC, (unsigned char *)send_buf_p, req_size + length, command_array_p ); // Send uart command request snic_core_p->sendUart( command_len, command_array_p ); +#if 0 // TODO: Not wait for command response(Tentative) // Wait UART response int ret = uartCmdMgr_p->wait(); if( ret != 0 ) { DEBUG_PRINT( "send failed\r\n" ); snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + snic_core_p->unlockAPI(); return -1; } @@ -209,34 +253,43 @@ { DEBUG_PRINT("send status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); snic_core_p->freeCmdBuf( payload_buf_p ); + FUNC_OUT(); + snic_core_p->unlockAPI(); return -1; } snic_core_p->freeCmdBuf( payload_buf_p ); - +#endif + + FUNC_OUT(); +#if 0 // TODO: Not wait for command response(Tentative) + snic_core_p->unlockAPI(); +#endif // SNIC_SEND_FROM_SOCKET_REQ - return 0; + wait(0.05); + + return length; +// return 0; } // -1 if unsuccessful, else number of bytes received int UDPSocket::receiveFrom(Endpoint &remote, char *data_p, int length) { + FUNC_IN(); if( (data_p == NULL) || (length < 1) ) { DEBUG_PRINT("UDPSocket::receiveFrom parameter error\r\n"); + FUNC_OUT(); 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 ); + C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = snic_core_p->getConnectInfo( mSocketID ); if( con_info_p->recvbuf_p == NULL ) { -// DEBUG_PRINT("UDPSocket::receiveFrom Conncection info error\r\n"); - return 0; - } - if( con_info_p->is_received == false ) - { - return 0; + DEBUG_PRINT("UDPSocket::receiveFrom Conncection info error\r\n"); + FUNC_OUT(); + return -1; } char remote_ip[20] = {'\0'}; @@ -247,8 +300,15 @@ , (con_info_p->from_ip) & 0x000000ff ); remote.set_address( remote_ip, con_info_p->from_port ); + con_info_p->mutex.lock(); + con_info_p->is_receive_complete = true; + con_info_p->mutex.unlock(); + while( con_info_p->is_received == false ) + { + Thread::yield(); + } + // Get packet data from buffer for receive. 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) @@ -256,13 +316,12 @@ break; } } - if( con_info_p->recvbuf_p->isEmpty() ) { con_info_p->mutex.lock(); con_info_p->is_received = false; con_info_p->mutex.unlock(); } - + FUNC_OUT(); return i; }