debug version
Dependents: HTTPClient_WiFi_HelloWorld_src
Fork of SNICInterface by
Diff: Socket/TCPSocketConnection.cpp
- Revision:
- 29:6a0ba999597d
- Parent:
- 27:dcc4f34448f0
- Child:
- 30:944b8c04b5ff
--- a/Socket/TCPSocketConnection.cpp Tue Apr 01 07:19:19 2014 +0000 +++ b/Socket/TCPSocketConnection.cpp Mon May 26 05:17:28 2014 +0000 @@ -30,8 +30,6 @@ #include "TCPSocketConnection.h" #include <cstring> -using namespace murata_wifi; - TCPSocketConnection::TCPSocketConnection() { } @@ -40,12 +38,12 @@ { } -int TCPSocketConnection::connect( const char *ip_addr_p, unsigned short port) +int TCPSocketConnection::connect( const char *host_p, unsigned short port) { int ret; C_SNIC_Core *snic_core_p = C_SNIC_Core::getInstance(); C_SNIC_UartCommandManager *uartCmdMgr_p = snic_core_p->getUartCommand(); - + // Socket create ret = createSocket(); if( ret != 0 ) @@ -53,19 +51,28 @@ printf("createSocket error : %d\r\n", ret); return -1; } + printf("socket created : %d\r\n", mSocketID); + int ip_addr = resolveHostName( host_p ); + lcd_printf("connect to [%s](%08x)\r\n", host_p, ip_addr); + if( ( ip_addr == 0) || (ip_addr == -1) ) + { + printf("connect resolveHostName failed\r\n"); + return -1; + } + // Get buffer for response payload from MemoryPool - C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); + tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); if( payload_buf == NULL ) { - printf("connect payload_buf NULL\r\n"); + lcd_printf("connect payload_buf NULL\r\n"); return -1; } - + // IP address convert to number from strings. - unsigned int ip_addr = addrToInteger(ip_addr_p); - printf("connect to [%s](%08x)\r\n", ip_addr_p, ip_addr); +// unsigned int ip_addr = addrToInteger(ip_addr_p); + // C_SNIC_Core::tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T req; // Make request req.cmd_sid = UART_CMD_SID_SNIC_TCP_CONNECT_TO_SERVER_REQ; @@ -73,27 +80,33 @@ req.socket_id = mSocketID; // set ip addr ( byte order ) - req.remote_addr[0] = ( (ip_addr & 0xFF000000) >> 24 ); - req.remote_addr[1] = ( (ip_addr & 0xFF0000) >> 16 ); - req.remote_addr[2] = ( (ip_addr & 0xFF00) >> 8 ); - req.remote_addr[3] = (ip_addr & 0xFF); + req.remote_addr[0] = ((ip_addr & 0xFF000000) >> 24 ); + req.remote_addr[1] = ((ip_addr & 0xFF0000) >> 16 ); + req.remote_addr[2] = ((ip_addr & 0xFF00) >> 8 ); + req.remote_addr[3] = ( ip_addr & 0xFF); +/* + req.remote_addr[0] = 0xD8; + req.remote_addr[1] = 0x34; + req.remote_addr[2] = 0xE9; + req.remote_addr[3] = 0x78; +*/ req.remote_port[0] = ( (port & 0xFF00) >> 8 ); req.remote_port[1] = (port & 0xFF); req.recv_bufsize[0] = ( (SNIC_UART_RECVBUF_SIZE & 0xFF00) >> 8 ); req.recv_bufsize[1] = (SNIC_UART_RECVBUF_SIZE & 0xFF); req.timeout = 60; - unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; + 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 *)&req , sizeof(C_SNIC_Core::tagSNIC_TCP_CONNECT_TO_SERVER_REQ_T), payload_buf->buf, command_array ); + uartCmdMgr_p->setCommandSID( UART_CMD_SID_SNIC_TCP_CONNECTION_STATUS_IND ); + // Send uart command request snic_core_p->sendUart( command_len, command_array ); - uartCmdMgr_p->setCommandSID( UART_CMD_SID_SNIC_TCP_CONNECTION_STATUS_IND ); - // Wait UART response ret = uartCmdMgr_p->wait(); if( ret != 0 ) @@ -109,6 +122,7 @@ snic_core_p->freeCmdBuf( payload_buf ); return -1; } + snic_core_p->freeCmdBuf( payload_buf ); // Initialize connection information @@ -116,11 +130,11 @@ if( con_info_p->recvbuf_p == NULL ) { printf( "create recv buffer[socket:%d]\r\n", mSocketID); - con_info_p->recvbuf_p = new CircBuffer<unsigned char>(SNIC_UART_RECVBUF_SIZE); + con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE); } con_info_p->is_connected = true; con_info_p->is_received = false; - + return 0; } @@ -132,19 +146,19 @@ } unsigned char gTCP_SEND_BUF[2048]; -int TCPSocketConnection::send(unsigned char* data_p, int length) +int TCPSocketConnection::send(char* data_p, 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 - C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = snic_core_p->allocCmdBuf(); + 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_TCP_SEND_FROM_SOCKET_REQ_T req; // Make request req.cmd_sid = UART_CMD_SID_SNIC_SEND_FROM_SOCKET_REQ; @@ -158,8 +172,8 @@ memcpy( gTCP_SEND_BUF, &req, req_size ); memcpy( &gTCP_SEND_BUF[req_size], data_p, length ); - unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; - unsigned int command_len; + 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, gTCP_SEND_BUF , req_size + length, payload_buf->buf, command_array ); @@ -188,7 +202,12 @@ return 0; } -int TCPSocketConnection::receive(unsigned char* data_p, int length) +int TCPSocketConnection::send_all(char *data_p, int length) +{ + return send( data_p, length ); +} + +int TCPSocketConnection::receive(char* data_p, int length) { int i = 0;