customized SNICInterface

Dependents:   MurataTypeYD_Ping

Fork of NySNICInterface by Ryo Iizuka

Committer:
kishino
Date:
Wed Mar 26 04:38:12 2014 +0000
Revision:
14:54378c96d285
Parent:
12:0254eaccfda2
Child:
15:5eb637414df2
Added a comment of copyright.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kishino 14:54378c96d285 1 /******************* Murata Manufacturing Co.,Ltd. 2014 *****************
kishino 14:54378c96d285 2 *
kishino 14:54378c96d285 3 * Filename: SNIC_Core.cpp
kishino 14:54378c96d285 4 *
kishino 14:54378c96d285 5 * Purpose: This module has implementation of internal common function for API.
kishino 14:54378c96d285 6 *
kishino 14:54378c96d285 7 * $Author: kishino $
kishino 14:54378c96d285 8 *
kishino 14:54378c96d285 9 * $Date: 2014/03/26 $
kishino 14:54378c96d285 10 *
kishino 14:54378c96d285 11 * $Revision: 0.0.0.1 $
kishino 14:54378c96d285 12 * ***********************************************************************/
kishino 12:0254eaccfda2 13 #include "mbed.h"
kishino 12:0254eaccfda2 14 #include "SNIC_Core.h"
kishino 12:0254eaccfda2 15 #include "SNIC_UartMsgUtil.h"
kishino 12:0254eaccfda2 16 #include <string>
kishino 12:0254eaccfda2 17
kishino 12:0254eaccfda2 18 using namespace murata_wifi;
kishino 12:0254eaccfda2 19
kishino 12:0254eaccfda2 20 #define UART_RECVBUF_SIZE 2048
kishino 12:0254eaccfda2 21 typedef struct
kishino 12:0254eaccfda2 22 {
kishino 12:0254eaccfda2 23 unsigned char buf[UART_RECVBUF_SIZE];
kishino 12:0254eaccfda2 24 unsigned int size;
kishino 12:0254eaccfda2 25 bool is_receive;
kishino 12:0254eaccfda2 26 }tagUART_RECVBUF_T;
kishino 12:0254eaccfda2 27 tagUART_RECVBUF_T gUART_RCVBUF;
kishino 12:0254eaccfda2 28 unsigned char gUART_TEMP_BUF[UART_RECVBUF_SIZE];
kishino 12:0254eaccfda2 29
kishino 12:0254eaccfda2 30 C_SNIC_Core *C_SNIC_Core::mInstance_p = NULL;
kishino 12:0254eaccfda2 31
kishino 12:0254eaccfda2 32 C_SNIC_Core *C_SNIC_Core::getInstance()
kishino 12:0254eaccfda2 33 {
kishino 12:0254eaccfda2 34 if( mInstance_p == NULL )
kishino 12:0254eaccfda2 35 {
kishino 12:0254eaccfda2 36 mInstance_p = new C_SNIC_Core();
kishino 12:0254eaccfda2 37 }
kishino 12:0254eaccfda2 38 return mInstance_p;
kishino 12:0254eaccfda2 39 }
kishino 12:0254eaccfda2 40
kishino 12:0254eaccfda2 41 C_SNIC_Core::C_SNIC_Core()
kishino 12:0254eaccfda2 42 {
kishino 12:0254eaccfda2 43 int i;
kishino 12:0254eaccfda2 44
kishino 12:0254eaccfda2 45 for( i = 0; i < MAX_SOCKET_ID+1; i++ )
kishino 12:0254eaccfda2 46 {
kishino 12:0254eaccfda2 47 mConnectInfo[i].recvbuf_p = NULL;
kishino 12:0254eaccfda2 48 mConnectInfo[i].is_connected = false;
kishino 12:0254eaccfda2 49 }
kishino 12:0254eaccfda2 50
kishino 12:0254eaccfda2 51 mUartRecvThread_p = NULL;
kishino 12:0254eaccfda2 52 }
kishino 12:0254eaccfda2 53
kishino 12:0254eaccfda2 54 int C_SNIC_Core::initUart(PinName tx, PinName rx, int baud)
kishino 12:0254eaccfda2 55 {
kishino 12:0254eaccfda2 56 // printf("[C_SNIC_Core::initUart]1\r\n");
kishino 12:0254eaccfda2 57
kishino 12:0254eaccfda2 58 mUartRequestSeq = 0;
kishino 12:0254eaccfda2 59
kishino 12:0254eaccfda2 60 mUart_p = new RawSerial( tx, rx );
kishino 12:0254eaccfda2 61 mUart_p->baud( baud );
kishino 12:0254eaccfda2 62 mUart_p->format(8, SerialBase::None, 1);
kishino 12:0254eaccfda2 63
kishino 12:0254eaccfda2 64 // printf("[C_SNIC_Core::initUart]2\r\n");
kishino 12:0254eaccfda2 65 // Initialize uart
kishino 12:0254eaccfda2 66 gUART_RCVBUF.is_receive = false;
kishino 12:0254eaccfda2 67 gUART_RCVBUF.size = 0;
kishino 12:0254eaccfda2 68
kishino 12:0254eaccfda2 69 // Create UART recv thread
kishino 12:0254eaccfda2 70 mUartRecvThread_p = new Thread( C_SNIC_Core::uartRecvThread );
kishino 12:0254eaccfda2 71 // printf("[C_SNIC_Core::initUart]3\r\n");
kishino 12:0254eaccfda2 72 if( mUartRecvThread_p == NULL )
kishino 12:0254eaccfda2 73 {
kishino 12:0254eaccfda2 74 printf("[C_SNIC_Core::initUart] thread cread failed\r\n");
kishino 12:0254eaccfda2 75 return -1;
kishino 12:0254eaccfda2 76 }
kishino 12:0254eaccfda2 77
kishino 12:0254eaccfda2 78 return 0;
kishino 12:0254eaccfda2 79 }
kishino 12:0254eaccfda2 80
kishino 12:0254eaccfda2 81 unsigned int C_SNIC_Core::preparationSendCommand( unsigned char cmd_id, unsigned char cmd_sid
kishino 12:0254eaccfda2 82 , unsigned char *req_buf_p, unsigned int req_buf_len
kishino 12:0254eaccfda2 83 , unsigned char *response_buf_p, unsigned char *command_p )
kishino 12:0254eaccfda2 84 {
kishino 12:0254eaccfda2 85 unsigned char payload_array[UART_REQUEST_PAYLOAD_MAX];
kishino 12:0254eaccfda2 86 unsigned short payload_len;
kishino 12:0254eaccfda2 87 unsigned int command_len = 0;
kishino 12:0254eaccfda2 88
kishino 12:0254eaccfda2 89 // Make command payload
kishino 12:0254eaccfda2 90 payload_len = C_SNIC_UartMsgUtil::makePayload( req_buf_len, req_buf_p, payload_array );
kishino 12:0254eaccfda2 91 // Make all command request
kishino 12:0254eaccfda2 92 command_len = C_SNIC_UartMsgUtil::makeRequest( cmd_id, payload_array, payload_len, command_p );
kishino 12:0254eaccfda2 93
kishino 12:0254eaccfda2 94 // Set data for response
kishino 12:0254eaccfda2 95 mUartCommand.setCommandID( cmd_id );
kishino 12:0254eaccfda2 96 mUartCommand.setCommandSID( cmd_sid | 0x80 );
kishino 12:0254eaccfda2 97 mUartCommand.setResponseBuf( response_buf_p );
kishino 12:0254eaccfda2 98
kishino 12:0254eaccfda2 99 return command_len;
kishino 12:0254eaccfda2 100 }
kishino 12:0254eaccfda2 101
kishino 12:0254eaccfda2 102 int C_SNIC_Core::sendUart( unsigned int len, unsigned char *data )
kishino 12:0254eaccfda2 103 {
kishino 12:0254eaccfda2 104 int ret = 0;
kishino 12:0254eaccfda2 105
kishino 12:0254eaccfda2 106 mUartMutex.lock();
kishino 12:0254eaccfda2 107 for( int i = 0; i < len; i++ )
kishino 12:0254eaccfda2 108 {
kishino 12:0254eaccfda2 109 // Write to UART
kishino 12:0254eaccfda2 110 ret = mUart_p->putc( data[i] );
kishino 12:0254eaccfda2 111 if( ret == -1 )
kishino 12:0254eaccfda2 112 {
kishino 12:0254eaccfda2 113 ret = -1;
kishino 12:0254eaccfda2 114 break;
kishino 12:0254eaccfda2 115 }
kishino 12:0254eaccfda2 116 }
kishino 12:0254eaccfda2 117 mUartMutex.unlock();
kishino 12:0254eaccfda2 118 return ret;
kishino 12:0254eaccfda2 119 }
kishino 12:0254eaccfda2 120
kishino 12:0254eaccfda2 121 tagMEMPOOL_BLOCK_T *C_SNIC_Core::getAlocCmdBuf()
kishino 12:0254eaccfda2 122 {
kishino 12:0254eaccfda2 123 // Get buffer from MemoryPool
kishino 12:0254eaccfda2 124 return mMemPoolPayload.alloc();
kishino 12:0254eaccfda2 125 }
kishino 12:0254eaccfda2 126
kishino 12:0254eaccfda2 127 void C_SNIC_Core::freeCmdBuf( tagMEMPOOL_BLOCK_T *buf_p )
kishino 12:0254eaccfda2 128 {
kishino 12:0254eaccfda2 129 mMemPoolPayload.free( buf_p );
kishino 12:0254eaccfda2 130 }
kishino 12:0254eaccfda2 131
kishino 12:0254eaccfda2 132 tagCONNECT_INFO_T *C_SNIC_Core::getConnectInfo( int socket_id )
kishino 12:0254eaccfda2 133 {
kishino 12:0254eaccfda2 134 if( (socket_id < 0) || (socket_id > MAX_SOCKET_ID) )
kishino 12:0254eaccfda2 135 {
kishino 12:0254eaccfda2 136 return NULL;
kishino 12:0254eaccfda2 137 }
kishino 12:0254eaccfda2 138 return &mConnectInfo[socket_id];
kishino 12:0254eaccfda2 139 }
kishino 12:0254eaccfda2 140
kishino 12:0254eaccfda2 141 DigitalOut led1(LED1);
kishino 12:0254eaccfda2 142 void C_SNIC_Core::uartRecvThread (void const *args_p) {
kishino 12:0254eaccfda2 143
kishino 12:0254eaccfda2 144 C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
kishino 12:0254eaccfda2 145 if ( instance_p == NULL )
kishino 12:0254eaccfda2 146 {
kishino 12:0254eaccfda2 147 printf("Socket constructor error: no wifly instance available!\r\n");
kishino 12:0254eaccfda2 148 }
kishino 12:0254eaccfda2 149
kishino 12:0254eaccfda2 150 int recvdata = 0;
kishino 12:0254eaccfda2 151 int i;
kishino 12:0254eaccfda2 152
kishino 12:0254eaccfda2 153 /* UART recv thread main loop */
kishino 12:0254eaccfda2 154 for (;;)
kishino 12:0254eaccfda2 155 {
kishino 12:0254eaccfda2 156 while( instance_p->mUart_p->readable() )
kishino 12:0254eaccfda2 157 {
kishino 12:0254eaccfda2 158 // Receive data from UART.
kishino 12:0254eaccfda2 159 instance_p->mUartMutex.lock();
kishino 12:0254eaccfda2 160 recvdata = instance_p->mUart_p->getc();
kishino 12:0254eaccfda2 161 instance_p->mUartMutex.unlock();
kishino 12:0254eaccfda2 162
kishino 12:0254eaccfda2 163 // Check UART receiving flg
kishino 12:0254eaccfda2 164 if( gUART_RCVBUF.is_receive )
kishino 12:0254eaccfda2 165 {
kishino 12:0254eaccfda2 166 gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata;
kishino 12:0254eaccfda2 167 gUART_RCVBUF.size++;
kishino 12:0254eaccfda2 168 // Check received data is EOM.
kishino 12:0254eaccfda2 169 if( recvdata == UART_CMD_EOM )
kishino 12:0254eaccfda2 170 {
kishino 12:0254eaccfda2 171 led1 = 0;
kishino 12:0254eaccfda2 172 #if 0
kishino 12:0254eaccfda2 173 printf("[recv]\r\n");
kishino 12:0254eaccfda2 174 for( i = 0; i < gUART_RCVBUF.size; i++ )
kishino 12:0254eaccfda2 175 {
kishino 12:0254eaccfda2 176 printf("%02x ", gUART_RCVBUF.buf[i]);
kishino 12:0254eaccfda2 177 }
kishino 12:0254eaccfda2 178 printf("\r\n");
kishino 12:0254eaccfda2 179 #endif
kishino 12:0254eaccfda2 180 unsigned char command_id;
kishino 12:0254eaccfda2 181 // Get payload from received data from UART.
kishino 12:0254eaccfda2 182 int payload_len = C_SNIC_UartMsgUtil::getResponsePayload( gUART_RCVBUF.size, gUART_RCVBUF.buf
kishino 12:0254eaccfda2 183 , &command_id, gUART_TEMP_BUF );
kishino 12:0254eaccfda2 184
kishino 12:0254eaccfda2 185 // Check receive a TCP or UDP packet
kishino 12:0254eaccfda2 186 if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_CONNECTION_RECV_IND) )
kishino 12:0254eaccfda2 187 {
kishino 12:0254eaccfda2 188 // Packet buffering
kishino 12:0254eaccfda2 189 instance_p->mUartCommand.bufferredPacket( gUART_TEMP_BUF, payload_len );
kishino 12:0254eaccfda2 190 }
kishino 12:0254eaccfda2 191
kishino 12:0254eaccfda2 192 // Check scan results indication
kishino 12:0254eaccfda2 193 else if( (command_id == UART_CMD_ID_WIFI) && (gUART_TEMP_BUF[0] == UART_CMD_SID_WIFI_SCAN_RESULT_IND) )
kishino 12:0254eaccfda2 194 {
kishino 12:0254eaccfda2 195 // Scan result indicate
kishino 12:0254eaccfda2 196 instance_p->mUartCommand.scanResultIndicate( gUART_TEMP_BUF, payload_len );
kishino 12:0254eaccfda2 197 }
kishino 12:0254eaccfda2 198 // Checks in the command which is waiting.
kishino 12:0254eaccfda2 199 else if( instance_p->mUartCommand.isWaitingCommand(command_id, gUART_TEMP_BUF) )
kishino 12:0254eaccfda2 200 {
kishino 12:0254eaccfda2 201 // Get buffer for payload data
kishino 12:0254eaccfda2 202 unsigned char *payload_buf_p = instance_p->mUartCommand.getResponseBuf();
kishino 12:0254eaccfda2 203 if( payload_buf_p != NULL )
kishino 12:0254eaccfda2 204 {
kishino 12:0254eaccfda2 205 memcpy( payload_buf_p, gUART_TEMP_BUF, payload_len );
kishino 12:0254eaccfda2 206 instance_p->mUartCommand.setResponseBuf( NULL );
kishino 12:0254eaccfda2 207 }
kishino 12:0254eaccfda2 208 // Set status
kishino 12:0254eaccfda2 209 instance_p->mUartCommand.setCommandStatus( gUART_TEMP_BUF[2] );
kishino 12:0254eaccfda2 210 // Set signal for command response wait.
kishino 12:0254eaccfda2 211 instance_p->mUartCommand.signal();
kishino 12:0254eaccfda2 212 }
kishino 12:0254eaccfda2 213
kishino 12:0254eaccfda2 214 gUART_RCVBUF.size = 0;
kishino 12:0254eaccfda2 215 gUART_RCVBUF.is_receive = false;
kishino 12:0254eaccfda2 216 }
kishino 12:0254eaccfda2 217 }
kishino 12:0254eaccfda2 218 else
kishino 12:0254eaccfda2 219 {
kishino 12:0254eaccfda2 220 // Check received data is SOM.
kishino 12:0254eaccfda2 221 if( recvdata == UART_CMD_SOM )
kishino 12:0254eaccfda2 222 {
kishino 12:0254eaccfda2 223 led1 = 1;
kishino 12:0254eaccfda2 224 gUART_RCVBUF.size = 0;
kishino 12:0254eaccfda2 225 gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata;
kishino 12:0254eaccfda2 226 gUART_RCVBUF.size++;
kishino 12:0254eaccfda2 227 gUART_RCVBUF.is_receive = true;
kishino 12:0254eaccfda2 228 }
kishino 12:0254eaccfda2 229 }
kishino 12:0254eaccfda2 230 // Thread::yield();
kishino 12:0254eaccfda2 231 }
kishino 12:0254eaccfda2 232 Thread::yield();
kishino 12:0254eaccfda2 233 }
kishino 12:0254eaccfda2 234 }
kishino 12:0254eaccfda2 235