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
Socket/Socket.cpp@22:a9ec0cad4f84, 2014-03-27 (annotated)
- Committer:
- kishino
- Date:
- Thu Mar 27 07:37:55 2014 +0000
- Revision:
- 22:a9ec0cad4f84
- Parent:
- 20:dd736d328de6
- Child:
- 24:987e412ae879
Refactoring
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kishino | 20:dd736d328de6 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
kishino | 20:dd736d328de6 | 2 | * |
kishino | 20:dd736d328de6 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kishino | 20:dd736d328de6 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kishino | 20:dd736d328de6 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kishino | 20:dd736d328de6 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kishino | 20:dd736d328de6 | 7 | * furnished to do so, subject to the following conditions: |
kishino | 20:dd736d328de6 | 8 | * |
kishino | 20:dd736d328de6 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
kishino | 20:dd736d328de6 | 10 | * substantial portions of the Software. |
kishino | 20:dd736d328de6 | 11 | * |
kishino | 20:dd736d328de6 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kishino | 20:dd736d328de6 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kishino | 20:dd736d328de6 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kishino | 20:dd736d328de6 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kishino | 20:dd736d328de6 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kishino | 20:dd736d328de6 | 17 | */ |
kishino | 20:dd736d328de6 | 18 | /******************* Murata Manufacturing Co.,Ltd. 2014 ***************** |
kishino | 20:dd736d328de6 | 19 | * |
kishino | 20:dd736d328de6 | 20 | * Filename: Socket.cpp |
kishino | 20:dd736d328de6 | 21 | * |
kishino | 20:dd736d328de6 | 22 | * Purpose: This module has implementation of socket. |
kishino | 20:dd736d328de6 | 23 | * |
kishino | 20:dd736d328de6 | 24 | * $Author: kishino $ |
kishino | 20:dd736d328de6 | 25 | * |
kishino | 20:dd736d328de6 | 26 | * $Date: 2014/03/26 $ |
kishino | 20:dd736d328de6 | 27 | * |
kishino | 20:dd736d328de6 | 28 | * $Revision: 0.0.0.1 $ |
kishino | 20:dd736d328de6 | 29 | * ***********************************************************************/ |
kishino | 20:dd736d328de6 | 30 | #include "Socket.h" |
kishino | 20:dd736d328de6 | 31 | #include <cstring> |
kishino | 20:dd736d328de6 | 32 | |
kishino | 20:dd736d328de6 | 33 | using namespace murata_wifi; |
kishino | 20:dd736d328de6 | 34 | |
kishino | 20:dd736d328de6 | 35 | Socket::Socket() |
kishino | 20:dd736d328de6 | 36 | { |
kishino | 20:dd736d328de6 | 37 | mSnicWifi_p = C_SNIC_Core::getInstance(); |
kishino | 20:dd736d328de6 | 38 | mSocketID = -1; |
kishino | 20:dd736d328de6 | 39 | } |
kishino | 20:dd736d328de6 | 40 | |
kishino | 20:dd736d328de6 | 41 | Socket::~Socket() { |
kishino | 20:dd736d328de6 | 42 | // close(); //Don't want to leak |
kishino | 20:dd736d328de6 | 43 | } |
kishino | 20:dd736d328de6 | 44 | |
kishino | 20:dd736d328de6 | 45 | int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) { |
kishino | 20:dd736d328de6 | 46 | return 0; |
kishino | 20:dd736d328de6 | 47 | } |
kishino | 20:dd736d328de6 | 48 | |
kishino | 20:dd736d328de6 | 49 | int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) { |
kishino | 20:dd736d328de6 | 50 | return 0; |
kishino | 20:dd736d328de6 | 51 | } |
kishino | 20:dd736d328de6 | 52 | |
kishino | 20:dd736d328de6 | 53 | int Socket::close(bool shutdown) |
kishino | 20:dd736d328de6 | 54 | { |
kishino | 20:dd736d328de6 | 55 | |
kishino | 20:dd736d328de6 | 56 | return 0; |
kishino | 20:dd736d328de6 | 57 | } |
kishino | 20:dd736d328de6 | 58 | |
kishino | 20:dd736d328de6 | 59 | #if 0 |
kishino | 20:dd736d328de6 | 60 | int Socket::select(struct timeval *timeout, bool read, bool write) |
kishino | 20:dd736d328de6 | 61 | { |
kishino | 20:dd736d328de6 | 62 | return 0; |
kishino | 20:dd736d328de6 | 63 | } |
kishino | 20:dd736d328de6 | 64 | #endif |
kishino | 20:dd736d328de6 | 65 | |
kishino | 20:dd736d328de6 | 66 | int Socket::createSocket( unsigned char bind, unsigned int local_addr, unsigned short port ) |
kishino | 20:dd736d328de6 | 67 | { |
kishino | 20:dd736d328de6 | 68 | C_SNIC_UartCommandManager *uartCmdMgr_p = mSnicWifi_p->getUartCommand(); |
kishino | 20:dd736d328de6 | 69 | // Get buffer for response payload from MemoryPool |
kishino | 22:a9ec0cad4f84 | 70 | C_SNIC_Core::tagMEMPOOL_BLOCK_T *payload_buf = mSnicWifi_p->allocCmdBuf(); |
kishino | 20:dd736d328de6 | 71 | if( payload_buf == NULL ) |
kishino | 20:dd736d328de6 | 72 | { |
kishino | 20:dd736d328de6 | 73 | printf("createSocket payload_buf NULL\r\n"); |
kishino | 20:dd736d328de6 | 74 | return -1; |
kishino | 20:dd736d328de6 | 75 | } |
kishino | 20:dd736d328de6 | 76 | |
kishino | 22:a9ec0cad4f84 | 77 | C_SNIC_Core::tagSNIC_TCP_CREATE_SOCKET_REQ_T req; |
kishino | 20:dd736d328de6 | 78 | int req_len = 0; |
kishino | 20:dd736d328de6 | 79 | |
kishino | 20:dd736d328de6 | 80 | // Make request |
kishino | 20:dd736d328de6 | 81 | req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_SOCKET_REQ; |
kishino | 20:dd736d328de6 | 82 | req_len++; |
kishino | 20:dd736d328de6 | 83 | req.seq = mUartRequestSeq++; |
kishino | 20:dd736d328de6 | 84 | req_len++; |
kishino | 20:dd736d328de6 | 85 | req.bind = bind; |
kishino | 20:dd736d328de6 | 86 | req_len++; |
kishino | 20:dd736d328de6 | 87 | if( bind != 0 ) |
kishino | 20:dd736d328de6 | 88 | { |
kishino | 20:dd736d328de6 | 89 | /* |
kishino | 20:dd736d328de6 | 90 | req.local_addr = local_addr; |
kishino | 20:dd736d328de6 | 91 | req_len++; |
kishino | 20:dd736d328de6 | 92 | req.local_port = port; |
kishino | 20:dd736d328de6 | 93 | req_len++; |
kishino | 20:dd736d328de6 | 94 | */ |
kishino | 20:dd736d328de6 | 95 | } |
kishino | 20:dd736d328de6 | 96 | |
kishino | 20:dd736d328de6 | 97 | unsigned char command_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 20:dd736d328de6 | 98 | unsigned int command_len; |
kishino | 20:dd736d328de6 | 99 | // Preparation of command |
kishino | 20:dd736d328de6 | 100 | command_len = mSnicWifi_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req |
kishino | 20:dd736d328de6 | 101 | , req_len, payload_buf->buf, command_array ); |
kishino | 20:dd736d328de6 | 102 | |
kishino | 20:dd736d328de6 | 103 | // Send uart command request |
kishino | 20:dd736d328de6 | 104 | mSnicWifi_p->sendUart( command_len, command_array ); |
kishino | 20:dd736d328de6 | 105 | |
kishino | 20:dd736d328de6 | 106 | int ret; |
kishino | 20:dd736d328de6 | 107 | // Wait UART response |
kishino | 20:dd736d328de6 | 108 | ret = uartCmdMgr_p->wait(); |
kishino | 20:dd736d328de6 | 109 | if( ret != 0 ) |
kishino | 20:dd736d328de6 | 110 | { |
kishino | 20:dd736d328de6 | 111 | printf( "createSocket failed\r\n" ); |
kishino | 20:dd736d328de6 | 112 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 20:dd736d328de6 | 113 | return -1; |
kishino | 20:dd736d328de6 | 114 | } |
kishino | 20:dd736d328de6 | 115 | |
kishino | 20:dd736d328de6 | 116 | if( uartCmdMgr_p->getCommandStatus() != 0 ) |
kishino | 20:dd736d328de6 | 117 | { |
kishino | 20:dd736d328de6 | 118 | printf("createSocket status:%02x\r\n", uartCmdMgr_p->getCommandStatus()); |
kishino | 20:dd736d328de6 | 119 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 20:dd736d328de6 | 120 | return -1; |
kishino | 20:dd736d328de6 | 121 | } |
kishino | 20:dd736d328de6 | 122 | mSocketID = payload_buf->buf[3]; |
kishino | 20:dd736d328de6 | 123 | mSnicWifi_p->freeCmdBuf( payload_buf ); |
kishino | 20:dd736d328de6 | 124 | |
kishino | 20:dd736d328de6 | 125 | return 0; |
kishino | 20:dd736d328de6 | 126 | } |