SNIC UART Interface library for Murata Type-YD module

Dependents:   WebSocketServerTest

Fork of SNICInterface_mod by Toyomasa Watarai

Committer:
kishino
Date:
Thu Mar 27 05:12:22 2014 +0000
Revision:
15:5eb637414df2
Parent:
14:54378c96d285
Child:
16:6100acfeb1f1
Method name was changed. [C_SNIC_Core::getAlocCmdBuf -> C_SNIC_Core::allocCmdBuf.]

Who changed what in which revision?

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