SNIC UART Interface library for Murata Type-YD module

Dependents:   WebSocketServerTest

Fork of SNICInterface_mod by Toyomasa Watarai

Committer:
kishino
Date:
Tue Mar 25 02:21:11 2014 +0000
Revision:
13:53e6471d5753
Parent:
12:0254eaccfda2
Child:
14:54378c96d285
Created the function of the TCP / IP client. (packet send, receive)

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 4:99cc93fe7d88 18 #include "Socket.h"
kishino 4:99cc93fe7d88 19 #include <cstring>
kishino 4:99cc93fe7d88 20
kishino 4:99cc93fe7d88 21 using namespace murata_wifi;
kishino 4:99cc93fe7d88 22
kishino 4:99cc93fe7d88 23 Socket::Socket()
kishino 4:99cc93fe7d88 24 {
kishino 12:0254eaccfda2 25 mSnicWifi_p = C_SNIC_Core::getInstance();
kishino 12:0254eaccfda2 26 mSocketID = -1;
kishino 4:99cc93fe7d88 27 }
kishino 4:99cc93fe7d88 28
kishino 4:99cc93fe7d88 29 Socket::~Socket() {
kishino 4:99cc93fe7d88 30 // close(); //Don't want to leak
kishino 4:99cc93fe7d88 31 }
kishino 4:99cc93fe7d88 32
kishino 4:99cc93fe7d88 33 int Socket::set_option(int level, int optname, const void *optval, socklen_t optlen) {
kishino 4:99cc93fe7d88 34 return 0;
kishino 4:99cc93fe7d88 35 }
kishino 4:99cc93fe7d88 36
kishino 4:99cc93fe7d88 37 int Socket::get_option(int level, int optname, void *optval, socklen_t *optlen) {
kishino 4:99cc93fe7d88 38 return 0;
kishino 4:99cc93fe7d88 39 }
kishino 4:99cc93fe7d88 40
kishino 4:99cc93fe7d88 41 int Socket::close(bool shutdown)
kishino 4:99cc93fe7d88 42 {
kishino 4:99cc93fe7d88 43
kishino 4:99cc93fe7d88 44 return 0;
kishino 4:99cc93fe7d88 45 }
kishino 4:99cc93fe7d88 46
kishino 4:99cc93fe7d88 47 #if 0
kishino 4:99cc93fe7d88 48 int Socket::select(struct timeval *timeout, bool read, bool write)
kishino 4:99cc93fe7d88 49 {
kishino 4:99cc93fe7d88 50 return 0;
kishino 4:99cc93fe7d88 51 }
kishino 12:0254eaccfda2 52 #endif
kishino 12:0254eaccfda2 53
kishino 12:0254eaccfda2 54 int Socket::createSocket( unsigned char bind, unsigned int local_addr, unsigned short port )
kishino 12:0254eaccfda2 55 {
kishino 12:0254eaccfda2 56 // Get buffer for response payload from MemoryPool
kishino 12:0254eaccfda2 57 tagMEMPOOL_BLOCK_T *payload_buf = mSnicWifi_p->getAlocCmdBuf();
kishino 12:0254eaccfda2 58 if( payload_buf == NULL )
kishino 12:0254eaccfda2 59 {
kishino 12:0254eaccfda2 60 printf("createSocket payload_buf NULL\r\n");
kishino 12:0254eaccfda2 61 return -1;
kishino 12:0254eaccfda2 62 }
kishino 12:0254eaccfda2 63
kishino 12:0254eaccfda2 64 tagSNIC_TCP_CREATE_SOCKET_REQ_T req;
kishino 12:0254eaccfda2 65 int req_len = 0;
kishino 12:0254eaccfda2 66
kishino 12:0254eaccfda2 67 // Make request
kishino 12:0254eaccfda2 68 req.cmd_sid = UART_CMD_SID_SNIC_TCP_CREATE_SOCKET_REQ;
kishino 12:0254eaccfda2 69 req_len++;
kishino 12:0254eaccfda2 70 req.seq = mUartRequestSeq++;
kishino 12:0254eaccfda2 71 req_len++;
kishino 12:0254eaccfda2 72 req.bind = bind;
kishino 12:0254eaccfda2 73 req_len++;
kishino 12:0254eaccfda2 74 if( bind != 0 )
kishino 12:0254eaccfda2 75 {
kishino 13:53e6471d5753 76 /*
kishino 12:0254eaccfda2 77 req.local_addr = local_addr;
kishino 12:0254eaccfda2 78 req_len++;
kishino 12:0254eaccfda2 79 req.local_port = port;
kishino 12:0254eaccfda2 80 req_len++;
kishino 13:53e6471d5753 81 */
kishino 12:0254eaccfda2 82 }
kishino 12:0254eaccfda2 83
kishino 12:0254eaccfda2 84 unsigned char command_array[UART_REQUEST_PAYLOAD_MAX];
kishino 12:0254eaccfda2 85 unsigned int command_len;
kishino 12:0254eaccfda2 86 // Preparation of command
kishino 12:0254eaccfda2 87 command_len = mSnicWifi_p->preparationSendCommand( UART_CMD_ID_SNIC, req.cmd_sid, (unsigned char *)&req
kishino 12:0254eaccfda2 88 , req_len, payload_buf->buf, command_array );
kishino 12:0254eaccfda2 89
kishino 12:0254eaccfda2 90 // Send uart command request
kishino 12:0254eaccfda2 91 mSnicWifi_p->sendUart( command_len, command_array );
kishino 12:0254eaccfda2 92
kishino 12:0254eaccfda2 93 int ret;
kishino 12:0254eaccfda2 94 // Wait UART response
kishino 12:0254eaccfda2 95 ret = mSnicWifi_p->mUartCommand.wait();
kishino 12:0254eaccfda2 96 if( ret != 0 )
kishino 12:0254eaccfda2 97 {
kishino 12:0254eaccfda2 98 printf( "createSocket failed\r\n" );
kishino 12:0254eaccfda2 99 mSnicWifi_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 100 return -1;
kishino 12:0254eaccfda2 101 }
kishino 12:0254eaccfda2 102
kishino 12:0254eaccfda2 103 if( mSnicWifi_p->mUartCommand.getCommandStatus() != 0 )
kishino 12:0254eaccfda2 104 {
kishino 12:0254eaccfda2 105 printf("createSocket status:%02x\r\n", mSnicWifi_p->mUartCommand.getCommandStatus());
kishino 12:0254eaccfda2 106 mSnicWifi_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 107 return -1;
kishino 12:0254eaccfda2 108 }
kishino 12:0254eaccfda2 109 mSocketID = payload_buf->buf[3];
kishino 12:0254eaccfda2 110 mSnicWifi_p->freeCmdBuf( payload_buf );
kishino 12:0254eaccfda2 111
kishino 12:0254eaccfda2 112 return 0;
kishino 12:0254eaccfda2 113 }