Customized SNICInterface

Fork of NySNICInterface by Ryo Iizuka

Committer:
kishino
Date:
Fri Mar 28 03:03:40 2014 +0000
Revision:
26:f2e1030964e4
Parent:
22:a9ec0cad4f84
Child:
29:6a0ba999597d
Created a virtual destructor

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 26:f2e1030964e4 55 C_SNIC_Core::~C_SNIC_Core()
kishino 26:f2e1030964e4 56 {
kishino 26:f2e1030964e4 57 }
kishino 26:f2e1030964e4 58
kishino 20:dd736d328de6 59 int C_SNIC_Core::initUart(PinName tx, PinName rx, int baud)
kishino 20:dd736d328de6 60 {
kishino 20:dd736d328de6 61 // printf("[C_SNIC_Core::initUart]1\r\n");
kishino 20:dd736d328de6 62
kishino 20:dd736d328de6 63 mUartRequestSeq = 0;
kishino 20:dd736d328de6 64
kishino 20:dd736d328de6 65 mUart_p = new RawSerial( tx, rx );
kishino 20:dd736d328de6 66 mUart_p->baud( baud );
kishino 20:dd736d328de6 67 mUart_p->format(8, SerialBase::None, 1);
kishino 20:dd736d328de6 68
kishino 20:dd736d328de6 69 // printf("[C_SNIC_Core::initUart]2\r\n");
kishino 20:dd736d328de6 70 // Initialize uart
kishino 20:dd736d328de6 71 gUART_RCVBUF.is_receive = false;
kishino 20:dd736d328de6 72 gUART_RCVBUF.size = 0;
kishino 20:dd736d328de6 73
kishino 20:dd736d328de6 74 // Create UART recv thread
kishino 20:dd736d328de6 75 mUartRecvThread_p = new Thread( C_SNIC_Core::uartRecvThread );
kishino 20:dd736d328de6 76 // printf("[C_SNIC_Core::initUart]3\r\n");
kishino 20:dd736d328de6 77 if( mUartRecvThread_p == NULL )
kishino 20:dd736d328de6 78 {
kishino 20:dd736d328de6 79 printf("[C_SNIC_Core::initUart] thread cread failed\r\n");
kishino 20:dd736d328de6 80 return -1;
kishino 20:dd736d328de6 81 }
kishino 20:dd736d328de6 82
kishino 20:dd736d328de6 83 return 0;
kishino 20:dd736d328de6 84 }
kishino 20:dd736d328de6 85
kishino 20:dd736d328de6 86 unsigned int C_SNIC_Core::preparationSendCommand( unsigned char cmd_id, unsigned char cmd_sid
kishino 20:dd736d328de6 87 , unsigned char *req_buf_p, unsigned int req_buf_len
kishino 20:dd736d328de6 88 , unsigned char *response_buf_p, unsigned char *command_p )
kishino 20:dd736d328de6 89 {
kishino 20:dd736d328de6 90 unsigned char payload_array[UART_REQUEST_PAYLOAD_MAX];
kishino 20:dd736d328de6 91 unsigned short payload_len;
kishino 20:dd736d328de6 92 unsigned int command_len = 0;
kishino 20:dd736d328de6 93
kishino 20:dd736d328de6 94 // Make command payload
kishino 20:dd736d328de6 95 payload_len = C_SNIC_UartMsgUtil::makePayload( req_buf_len, req_buf_p, payload_array );
kishino 20:dd736d328de6 96 // Make all command request
kishino 20:dd736d328de6 97 command_len = C_SNIC_UartMsgUtil::makeRequest( cmd_id, payload_array, payload_len, command_p );
kishino 20:dd736d328de6 98
kishino 20:dd736d328de6 99 // Set data for response
kishino 20:dd736d328de6 100 mUartCommand_p->setCommandID( cmd_id );
kishino 20:dd736d328de6 101 mUartCommand_p->setCommandSID( cmd_sid | 0x80 );
kishino 20:dd736d328de6 102 mUartCommand_p->setResponseBuf( response_buf_p );
kishino 20:dd736d328de6 103
kishino 20:dd736d328de6 104 return command_len;
kishino 20:dd736d328de6 105 }
kishino 20:dd736d328de6 106
kishino 20:dd736d328de6 107 int C_SNIC_Core::sendUart( unsigned int len, unsigned char *data )
kishino 20:dd736d328de6 108 {
kishino 20:dd736d328de6 109 int ret = 0;
kishino 20:dd736d328de6 110
kishino 20:dd736d328de6 111 mUartMutex.lock();
kishino 20:dd736d328de6 112 for( int i = 0; i < len; i++ )
kishino 20:dd736d328de6 113 {
kishino 20:dd736d328de6 114 // Write to UART
kishino 20:dd736d328de6 115 ret = mUart_p->putc( data[i] );
kishino 20:dd736d328de6 116 if( ret == -1 )
kishino 20:dd736d328de6 117 {
kishino 20:dd736d328de6 118 ret = -1;
kishino 20:dd736d328de6 119 break;
kishino 20:dd736d328de6 120 }
kishino 20:dd736d328de6 121 }
kishino 20:dd736d328de6 122 mUartMutex.unlock();
kishino 20:dd736d328de6 123 return ret;
kishino 20:dd736d328de6 124 }
kishino 20:dd736d328de6 125
kishino 22:a9ec0cad4f84 126 C_SNIC_Core::tagMEMPOOL_BLOCK_T *C_SNIC_Core::allocCmdBuf()
kishino 20:dd736d328de6 127 {
kishino 20:dd736d328de6 128 // Get buffer from MemoryPool
kishino 20:dd736d328de6 129 return mMemPoolPayload.alloc();
kishino 20:dd736d328de6 130 }
kishino 20:dd736d328de6 131
kishino 20:dd736d328de6 132 void C_SNIC_Core::freeCmdBuf( tagMEMPOOL_BLOCK_T *buf_p )
kishino 20:dd736d328de6 133 {
kishino 20:dd736d328de6 134 mMemPoolPayload.free( buf_p );
kishino 20:dd736d328de6 135 }
kishino 20:dd736d328de6 136
kishino 22:a9ec0cad4f84 137 C_SNIC_Core::tagCONNECT_INFO_T *C_SNIC_Core::getConnectInfo( int socket_id )
kishino 20:dd736d328de6 138 {
kishino 20:dd736d328de6 139 if( (socket_id < 0) || (socket_id > MAX_SOCKET_ID) )
kishino 20:dd736d328de6 140 {
kishino 20:dd736d328de6 141 return NULL;
kishino 20:dd736d328de6 142 }
kishino 20:dd736d328de6 143 return &mConnectInfo[socket_id];
kishino 20:dd736d328de6 144 }
kishino 20:dd736d328de6 145
kishino 20:dd736d328de6 146 C_SNIC_UartCommandManager *C_SNIC_Core::getUartCommand()
kishino 20:dd736d328de6 147 {
kishino 20:dd736d328de6 148 return mUartCommand_p;
kishino 20:dd736d328de6 149 }
kishino 20:dd736d328de6 150
kishino 20:dd736d328de6 151 DigitalOut led1(LED1);
kishino 20:dd736d328de6 152 void C_SNIC_Core::uartRecvThread (void const *args_p) {
kishino 20:dd736d328de6 153
kishino 20:dd736d328de6 154 C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
kishino 20:dd736d328de6 155 if ( instance_p == NULL )
kishino 20:dd736d328de6 156 {
kishino 20:dd736d328de6 157 printf("Socket constructor error: no wifly instance available!\r\n");
kishino 20:dd736d328de6 158 }
kishino 20:dd736d328de6 159 C_SNIC_UartCommandManager *uartCmdMgr_p = instance_p->getUartCommand();
kishino 20:dd736d328de6 160
kishino 20:dd736d328de6 161 int recvdata = 0;
kishino 20:dd736d328de6 162 int i;
kishino 20:dd736d328de6 163
kishino 20:dd736d328de6 164 /* UART recv thread main loop */
kishino 20:dd736d328de6 165 for (;;)
kishino 20:dd736d328de6 166 {
kishino 20:dd736d328de6 167 while( instance_p->mUart_p->readable() )
kishino 20:dd736d328de6 168 {
kishino 20:dd736d328de6 169 // Receive data from UART.
kishino 20:dd736d328de6 170 instance_p->mUartMutex.lock();
kishino 20:dd736d328de6 171 recvdata = instance_p->mUart_p->getc();
kishino 20:dd736d328de6 172 instance_p->mUartMutex.unlock();
kishino 20:dd736d328de6 173
kishino 20:dd736d328de6 174 // Check UART receiving flg
kishino 20:dd736d328de6 175 if( gUART_RCVBUF.is_receive )
kishino 20:dd736d328de6 176 {
kishino 20:dd736d328de6 177 gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata;
kishino 20:dd736d328de6 178 gUART_RCVBUF.size++;
kishino 20:dd736d328de6 179 // Check received data is EOM.
kishino 20:dd736d328de6 180 if( recvdata == UART_CMD_EOM )
kishino 20:dd736d328de6 181 {
kishino 20:dd736d328de6 182 led1 = 0;
kishino 20:dd736d328de6 183 #if 0
kishino 20:dd736d328de6 184 printf("[recv]\r\n");
kishino 20:dd736d328de6 185 for( i = 0; i < gUART_RCVBUF.size; i++ )
kishino 20:dd736d328de6 186 {
kishino 20:dd736d328de6 187 printf("%02x ", gUART_RCVBUF.buf[i]);
kishino 20:dd736d328de6 188 }
kishino 20:dd736d328de6 189 printf("\r\n");
kishino 20:dd736d328de6 190 #endif
kishino 20:dd736d328de6 191 unsigned char command_id;
kishino 20:dd736d328de6 192 // Get payload from received data from UART.
kishino 20:dd736d328de6 193 int payload_len = C_SNIC_UartMsgUtil::getResponsePayload( gUART_RCVBUF.size, gUART_RCVBUF.buf
kishino 20:dd736d328de6 194 , &command_id, gUART_TEMP_BUF );
kishino 20:dd736d328de6 195
kishino 20:dd736d328de6 196 // Check receive a TCP or UDP packet
kishino 20:dd736d328de6 197 if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_CONNECTION_RECV_IND) )
kishino 20:dd736d328de6 198 {
kishino 20:dd736d328de6 199 // Packet buffering
kishino 20:dd736d328de6 200 uartCmdMgr_p->bufferredPacket( gUART_TEMP_BUF, payload_len );
kishino 20:dd736d328de6 201 }
kishino 20:dd736d328de6 202
kishino 20:dd736d328de6 203 // Check scan results indication
kishino 20:dd736d328de6 204 else if( (command_id == UART_CMD_ID_WIFI) && (gUART_TEMP_BUF[0] == UART_CMD_SID_WIFI_SCAN_RESULT_IND) )
kishino 20:dd736d328de6 205 {
kishino 20:dd736d328de6 206 // Scan result indicate
kishino 20:dd736d328de6 207 uartCmdMgr_p->scanResultIndicate( gUART_TEMP_BUF, payload_len );
kishino 20:dd736d328de6 208 }
kishino 20:dd736d328de6 209 // Checks in the command which is waiting.
kishino 20:dd736d328de6 210 else if( uartCmdMgr_p->isWaitingCommand(command_id, gUART_TEMP_BUF) )
kishino 20:dd736d328de6 211 {
kishino 20:dd736d328de6 212 // Get buffer for payload data
kishino 20:dd736d328de6 213 unsigned char *payload_buf_p = uartCmdMgr_p->getResponseBuf();
kishino 20:dd736d328de6 214 if( payload_buf_p != NULL )
kishino 20:dd736d328de6 215 {
kishino 20:dd736d328de6 216 memcpy( payload_buf_p, gUART_TEMP_BUF, payload_len );
kishino 20:dd736d328de6 217 uartCmdMgr_p->setResponseBuf( NULL );
kishino 20:dd736d328de6 218 }
kishino 20:dd736d328de6 219 // Set status
kishino 20:dd736d328de6 220 uartCmdMgr_p->setCommandStatus( gUART_TEMP_BUF[2] );
kishino 20:dd736d328de6 221 // Set signal for command response wait.
kishino 20:dd736d328de6 222 uartCmdMgr_p->signal();
kishino 20:dd736d328de6 223 }
kishino 20:dd736d328de6 224
kishino 20:dd736d328de6 225 gUART_RCVBUF.size = 0;
kishino 20:dd736d328de6 226 gUART_RCVBUF.is_receive = false;
kishino 20:dd736d328de6 227 }
kishino 20:dd736d328de6 228 }
kishino 20:dd736d328de6 229 else
kishino 20:dd736d328de6 230 {
kishino 20:dd736d328de6 231 // Check received data is SOM.
kishino 20:dd736d328de6 232 if( recvdata == UART_CMD_SOM )
kishino 20:dd736d328de6 233 {
kishino 20:dd736d328de6 234 led1 = 1;
kishino 20:dd736d328de6 235 gUART_RCVBUF.size = 0;
kishino 20:dd736d328de6 236 gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata;
kishino 20:dd736d328de6 237 gUART_RCVBUF.size++;
kishino 20:dd736d328de6 238 gUART_RCVBUF.is_receive = true;
kishino 20:dd736d328de6 239 }
kishino 20:dd736d328de6 240 }
kishino 20:dd736d328de6 241 // Thread::yield();
kishino 20:dd736d328de6 242 }
kishino 20:dd736d328de6 243 Thread::yield();
kishino 20:dd736d328de6 244 }
kishino 20:dd736d328de6 245 }
kishino 20:dd736d328de6 246