customized SNICInterface

Dependents:   MurataTypeYD_Ping

Fork of NySNICInterface by Ryo Iizuka

Committer:
kishino
Date:
Thu Mar 27 07:37:55 2014 +0000
Revision:
22:a9ec0cad4f84
Parent:
20:dd736d328de6
Child:
26:f2e1030964e4
Refactoring

Who changed what in which revision?

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