Customized SNICInterface

Fork of NySNICInterface by Ryo Iizuka

Committer:
kishino
Date:
Fri May 30 08:30:40 2014 +0000
Revision:
32:ae95309643aa
Parent:
29:6a0ba999597d
Child:
33:33f1bc919486
Implemented  a API of TCP server.

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 29:6a0ba999597d 18 /** Wait signal ID of UART recv */
kishino 29:6a0ba999597d 19 #define UART_DISPATCH_SIGNAL 0x00000002
kishino 29:6a0ba999597d 20 #define UART_RECEIVE_SIGNAL 0x00000004
kishino 20:dd736d328de6 21
kishino 29:6a0ba999597d 22 #define UART_RECVBUF_SIZE 2048
kishino 29:6a0ba999597d 23 #define UART_THREAD_STACK_SIZE 512
kishino 29:6a0ba999597d 24
kishino 20:dd736d328de6 25 typedef struct
kishino 20:dd736d328de6 26 {
kishino 29:6a0ba999597d 27 tagMEMPOOL_BLOCK_T *mem_p;
kishino 20:dd736d328de6 28 unsigned int size;
kishino 20:dd736d328de6 29 }tagUART_RECVBUF_T;
kishino 20:dd736d328de6 30
kishino 29:6a0ba999597d 31 /*
kishino 29:6a0ba999597d 32 Define the global buffer using the area for Ethernet.
kishino 29:6a0ba999597d 33 */
kishino 29:6a0ba999597d 34 unsigned char gUART_TEMP_BUF[UART_RECVBUF_SIZE] __attribute__((section("AHBSRAM1")));
kishino 29:6a0ba999597d 35 unsigned char gUART_COMMAND_BUF[UART_REQUEST_PAYLOAD_MAX] __attribute__((section("AHBSRAM1")));
kishino 29:6a0ba999597d 36 unsigned char gPAYLOAD_ARRAY[UART_REQUEST_PAYLOAD_MAX] __attribute__((section("AHBSRAM1")));
kishino 29:6a0ba999597d 37 /** MemoryPool for payload of UART response */
kishino 29:6a0ba999597d 38 MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_PAYLOAD_NUM> mMemPoolPayload __attribute__((section("AHBSRAM1")));
kishino 29:6a0ba999597d 39 /** MemoryPool for UART receive */
kishino 29:6a0ba999597d 40 MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_UART_RECV_NUM> mMemPoolUartRecv __attribute__((section("AHBSRAM1")));
kishino 29:6a0ba999597d 41 Queue<tagMEMPOOL_BLOCK_T, MEMPOOL_UART_RECV_NUM> mUartRecvQueue;
kishino 29:6a0ba999597d 42
kishino 29:6a0ba999597d 43 tagMEMPOOL_BLOCK_T *gUART_RCVBUF_p;
kishino 20:dd736d328de6 44 C_SNIC_Core *C_SNIC_Core::mInstance_p = NULL;
kishino 20:dd736d328de6 45
kishino 20:dd736d328de6 46 C_SNIC_Core *C_SNIC_Core::getInstance()
kishino 20:dd736d328de6 47 {
kishino 20:dd736d328de6 48 if( mInstance_p == NULL )
kishino 20:dd736d328de6 49 {
kishino 20:dd736d328de6 50 mInstance_p = new C_SNIC_Core();
kishino 20:dd736d328de6 51 }
kishino 20:dd736d328de6 52 return mInstance_p;
kishino 20:dd736d328de6 53 }
kishino 20:dd736d328de6 54
kishino 20:dd736d328de6 55 C_SNIC_Core::C_SNIC_Core()
kishino 20:dd736d328de6 56 {
kishino 20:dd736d328de6 57 int i;
kishino 20:dd736d328de6 58
kishino 20:dd736d328de6 59 mUartCommand_p = new C_SNIC_UartCommandManager();
kishino 20:dd736d328de6 60 for( i = 0; i < MAX_SOCKET_ID+1; i++ )
kishino 20:dd736d328de6 61 {
kishino 20:dd736d328de6 62 mConnectInfo[i].recvbuf_p = NULL;
kishino 20:dd736d328de6 63 mConnectInfo[i].is_connected = false;
kishino 20:dd736d328de6 64 }
kishino 20:dd736d328de6 65
kishino 29:6a0ba999597d 66 mUartRecvThread_p = NULL;
kishino 29:6a0ba999597d 67 mUartRecvDispatchThread_p = NULL;
kishino 20:dd736d328de6 68 }
kishino 20:dd736d328de6 69
kishino 26:f2e1030964e4 70 C_SNIC_Core::~C_SNIC_Core()
kishino 26:f2e1030964e4 71 {
kishino 26:f2e1030964e4 72 }
kishino 26:f2e1030964e4 73
kishino 29:6a0ba999597d 74 int C_SNIC_Core::resetModule( PinName reset )
kishino 29:6a0ba999597d 75 {
kishino 29:6a0ba999597d 76 DigitalOut reset_pin( reset );
kishino 29:6a0ba999597d 77
kishino 29:6a0ba999597d 78 reset_pin = 0;
kishino 29:6a0ba999597d 79 wait(0.2);
kishino 29:6a0ba999597d 80 reset_pin = 1;
kishino 29:6a0ba999597d 81 wait(0.2);
kishino 29:6a0ba999597d 82
kishino 29:6a0ba999597d 83 return 0;
kishino 29:6a0ba999597d 84 }
kishino 29:6a0ba999597d 85
kishino 20:dd736d328de6 86 int C_SNIC_Core::initUart(PinName tx, PinName rx, int baud)
kishino 20:dd736d328de6 87 {
kishino 20:dd736d328de6 88 // printf("[C_SNIC_Core::initUart]1\r\n");
kishino 20:dd736d328de6 89
kishino 20:dd736d328de6 90 mUartRequestSeq = 0;
kishino 20:dd736d328de6 91
kishino 20:dd736d328de6 92 mUart_p = new RawSerial( tx, rx );
kishino 20:dd736d328de6 93 mUart_p->baud( baud );
kishino 20:dd736d328de6 94 mUart_p->format(8, SerialBase::None, 1);
kishino 29:6a0ba999597d 95
kishino 20:dd736d328de6 96 // Initialize uart
kishino 29:6a0ba999597d 97 gUART_RCVBUF_p = NULL;
kishino 20:dd736d328de6 98
kishino 29:6a0ba999597d 99 mUart_p->attach( C_SNIC_Core::uartRecvCallback );
kishino 29:6a0ba999597d 100 // Create UART recv dispatch thread
kishino 29:6a0ba999597d 101 mUartRecvDispatchThread_p = new Thread( C_SNIC_Core::uartRecvDispatchThread, NULL, osPriorityNormal, UART_THREAD_STACK_SIZE);
kishino 29:6a0ba999597d 102 if( mUartRecvDispatchThread_p == NULL )
kishino 20:dd736d328de6 103 {
kishino 29:6a0ba999597d 104 printf("[C_SNIC_Core::initUart] thread create failed\r\n");
kishino 20:dd736d328de6 105 return -1;
kishino 20:dd736d328de6 106 }
kishino 20:dd736d328de6 107
kishino 20:dd736d328de6 108 return 0;
kishino 20:dd736d328de6 109 }
kishino 29:6a0ba999597d 110 unsigned int C_SNIC_Core::preparationSendCommand( unsigned char cmd_id, unsigned char cmd_sid
kishino 20:dd736d328de6 111 , unsigned char *req_buf_p, unsigned int req_buf_len
kishino 20:dd736d328de6 112 , unsigned char *response_buf_p, unsigned char *command_p )
kishino 20:dd736d328de6 113 {
kishino 20:dd736d328de6 114 unsigned short payload_len;
kishino 20:dd736d328de6 115 unsigned int command_len = 0;
kishino 20:dd736d328de6 116
kishino 20:dd736d328de6 117 // Make command payload
kishino 29:6a0ba999597d 118 payload_len = C_SNIC_UartMsgUtil::makePayload( req_buf_len, req_buf_p, gPAYLOAD_ARRAY );
kishino 20:dd736d328de6 119 // Make all command request
kishino 29:6a0ba999597d 120 command_len = C_SNIC_UartMsgUtil::makeRequest( cmd_id, gPAYLOAD_ARRAY, payload_len, command_p );
kishino 20:dd736d328de6 121
kishino 20:dd736d328de6 122 // Set data for response
kishino 20:dd736d328de6 123 mUartCommand_p->setCommandID( cmd_id );
kishino 20:dd736d328de6 124 mUartCommand_p->setCommandSID( cmd_sid | 0x80 );
kishino 20:dd736d328de6 125 mUartCommand_p->setResponseBuf( response_buf_p );
kishino 20:dd736d328de6 126
kishino 20:dd736d328de6 127 return command_len;
kishino 20:dd736d328de6 128 }
kishino 20:dd736d328de6 129
kishino 20:dd736d328de6 130 int C_SNIC_Core::sendUart( unsigned int len, unsigned char *data )
kishino 20:dd736d328de6 131 {
kishino 20:dd736d328de6 132 int ret = 0;
kishino 20:dd736d328de6 133 mUartMutex.lock();
kishino 20:dd736d328de6 134 for( int i = 0; i < len; i++ )
kishino 20:dd736d328de6 135 {
kishino 20:dd736d328de6 136 // Write to UART
kishino 20:dd736d328de6 137 ret = mUart_p->putc( data[i] );
kishino 20:dd736d328de6 138 if( ret == -1 )
kishino 20:dd736d328de6 139 {
kishino 20:dd736d328de6 140 ret = -1;
kishino 20:dd736d328de6 141 break;
kishino 20:dd736d328de6 142 }
kishino 20:dd736d328de6 143 }
kishino 20:dd736d328de6 144 mUartMutex.unlock();
kishino 20:dd736d328de6 145 return ret;
kishino 20:dd736d328de6 146 }
kishino 20:dd736d328de6 147
kishino 29:6a0ba999597d 148 tagMEMPOOL_BLOCK_T *C_SNIC_Core::allocCmdBuf()
kishino 20:dd736d328de6 149 {
kishino 20:dd736d328de6 150 // Get buffer from MemoryPool
kishino 20:dd736d328de6 151 return mMemPoolPayload.alloc();
kishino 20:dd736d328de6 152 }
kishino 20:dd736d328de6 153
kishino 20:dd736d328de6 154 void C_SNIC_Core::freeCmdBuf( tagMEMPOOL_BLOCK_T *buf_p )
kishino 20:dd736d328de6 155 {
kishino 20:dd736d328de6 156 mMemPoolPayload.free( buf_p );
kishino 20:dd736d328de6 157 }
kishino 20:dd736d328de6 158
kishino 29:6a0ba999597d 159 tagMEMPOOL_BLOCK_T *C_SNIC_Core::allocUartRcvBuf()
kishino 29:6a0ba999597d 160 {
kishino 29:6a0ba999597d 161 // Get buffer from MemoryPool
kishino 29:6a0ba999597d 162 return mMemPoolUartRecv.alloc();
kishino 29:6a0ba999597d 163 }
kishino 29:6a0ba999597d 164
kishino 29:6a0ba999597d 165 void C_SNIC_Core::freeUartRecvBuf( tagMEMPOOL_BLOCK_T *buf_p )
kishino 29:6a0ba999597d 166 {
kishino 29:6a0ba999597d 167 mMemPoolUartRecv.free( buf_p );
kishino 29:6a0ba999597d 168 }
kishino 29:6a0ba999597d 169
kishino 22:a9ec0cad4f84 170 C_SNIC_Core::tagCONNECT_INFO_T *C_SNIC_Core::getConnectInfo( int socket_id )
kishino 20:dd736d328de6 171 {
kishino 20:dd736d328de6 172 if( (socket_id < 0) || (socket_id > MAX_SOCKET_ID) )
kishino 20:dd736d328de6 173 {
kishino 20:dd736d328de6 174 return NULL;
kishino 20:dd736d328de6 175 }
kishino 20:dd736d328de6 176 return &mConnectInfo[socket_id];
kishino 20:dd736d328de6 177 }
kishino 20:dd736d328de6 178
kishino 20:dd736d328de6 179 C_SNIC_UartCommandManager *C_SNIC_Core::getUartCommand()
kishino 20:dd736d328de6 180 {
kishino 20:dd736d328de6 181 return mUartCommand_p;
kishino 20:dd736d328de6 182 }
kishino 20:dd736d328de6 183
kishino 29:6a0ba999597d 184 unsigned char *C_SNIC_Core::getCommandBuf()
kishino 29:6a0ba999597d 185 {
kishino 29:6a0ba999597d 186 return gUART_COMMAND_BUF;
kishino 29:6a0ba999597d 187 }
kishino 20:dd736d328de6 188
kishino 29:6a0ba999597d 189 DigitalOut led1(LED1);
kishino 29:6a0ba999597d 190
kishino 29:6a0ba999597d 191 void C_SNIC_Core::uartRecvCallback( void )
kishino 29:6a0ba999597d 192 {
kishino 20:dd736d328de6 193 C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
kishino 29:6a0ba999597d 194 if( instance_p != NULL )
kishino 20:dd736d328de6 195 {
kishino 29:6a0ba999597d 196 int recvdata = 0;
kishino 20:dd736d328de6 197
kishino 29:6a0ba999597d 198 // set signal
kishino 29:6a0ba999597d 199 // instance_p->mUartRecvThread_p->signal_set( UART_RECEIVE_SIGNAL );
kishino 29:6a0ba999597d 200 if( instance_p->mUart_p->readable() )
kishino 20:dd736d328de6 201 {
kishino 20:dd736d328de6 202 // Receive data from UART.
kishino 20:dd736d328de6 203 instance_p->mUartMutex.lock();
kishino 20:dd736d328de6 204 recvdata = instance_p->mUart_p->getc();
kishino 20:dd736d328de6 205 instance_p->mUartMutex.unlock();
kishino 20:dd736d328de6 206
kishino 29:6a0ba999597d 207 // Check UART receiving buffer
kishino 29:6a0ba999597d 208 if( gUART_RCVBUF_p != NULL )
kishino 20:dd736d328de6 209 {
kishino 29:6a0ba999597d 210 gUART_RCVBUF_p->buf[ gUART_RCVBUF_p->size ] = (unsigned char)recvdata;
kishino 29:6a0ba999597d 211 gUART_RCVBUF_p->size++;
kishino 29:6a0ba999597d 212
kishino 20:dd736d328de6 213 // Check received data is EOM.
kishino 20:dd736d328de6 214 if( recvdata == UART_CMD_EOM )
kishino 20:dd736d328de6 215 {
kishino 20:dd736d328de6 216 led1 = 0;
kishino 29:6a0ba999597d 217 // Add queue
kishino 29:6a0ba999597d 218 mUartRecvQueue.put( gUART_RCVBUF_p );
kishino 20:dd736d328de6 219
kishino 29:6a0ba999597d 220 gUART_RCVBUF_p = NULL;
kishino 29:6a0ba999597d 221
kishino 29:6a0ba999597d 222 // set signal for dispatch thread
kishino 29:6a0ba999597d 223 instance_p->mUartRecvDispatchThread_p->signal_set( UART_DISPATCH_SIGNAL );
kishino 20:dd736d328de6 224 }
kishino 20:dd736d328de6 225 }
kishino 20:dd736d328de6 226 else
kishino 20:dd736d328de6 227 {
kishino 20:dd736d328de6 228 // Check received data is SOM.
kishino 20:dd736d328de6 229 if( recvdata == UART_CMD_SOM )
kishino 20:dd736d328de6 230 {
kishino 20:dd736d328de6 231 led1 = 1;
kishino 29:6a0ba999597d 232 gUART_RCVBUF_p = instance_p->allocUartRcvBuf();
kishino 29:6a0ba999597d 233 gUART_RCVBUF_p->size = 0;
kishino 29:6a0ba999597d 234 // get buffer for Uart receive
kishino 29:6a0ba999597d 235 gUART_RCVBUF_p->buf[ 0 ] = (unsigned char)recvdata;
kishino 29:6a0ba999597d 236
kishino 29:6a0ba999597d 237 gUART_RCVBUF_p->size++;
kishino 20:dd736d328de6 238 }
kishino 20:dd736d328de6 239 }
kishino 20:dd736d328de6 240 }
kishino 20:dd736d328de6 241 }
kishino 20:dd736d328de6 242 }
kishino 20:dd736d328de6 243
kishino 29:6a0ba999597d 244 void C_SNIC_Core::uartRecvDispatchThread (void const *args_p)
kishino 29:6a0ba999597d 245 {
kishino 29:6a0ba999597d 246 C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
kishino 29:6a0ba999597d 247 C_SNIC_UartCommandManager *uartCmdMgr_p = instance_p->getUartCommand();
kishino 29:6a0ba999597d 248
kishino 29:6a0ba999597d 249 tagMEMPOOL_BLOCK_T *uartRecvBuf_p;
kishino 29:6a0ba999597d 250 osEvent evt;
kishino 29:6a0ba999597d 251
kishino 29:6a0ba999597d 252 for(;;)
kishino 29:6a0ba999597d 253 {
kishino 29:6a0ba999597d 254 // wait
kishino 29:6a0ba999597d 255 Thread::signal_wait( UART_DISPATCH_SIGNAL );
kishino 29:6a0ba999597d 256
kishino 29:6a0ba999597d 257 // Get scanresults from queue
kishino 29:6a0ba999597d 258 evt = mUartRecvQueue.get(500);
kishino 29:6a0ba999597d 259 if (evt.status == osEventMessage)
kishino 29:6a0ba999597d 260 {
kishino 29:6a0ba999597d 261 do
kishino 29:6a0ba999597d 262 {
kishino 29:6a0ba999597d 263 uartRecvBuf_p = (tagMEMPOOL_BLOCK_T *)evt.value.p;
kishino 29:6a0ba999597d 264
kishino 32:ae95309643aa 265 #if 0
kishino 29:6a0ba999597d 266 {
kishino 29:6a0ba999597d 267 int i;
kishino 29:6a0ba999597d 268 for(i=0;i<uartRecvBuf_p->size;i++)
kishino 29:6a0ba999597d 269 {
kishino 29:6a0ba999597d 270 printf("%02x", uartRecvBuf_p->buf[i]);
kishino 29:6a0ba999597d 271 }
kishino 29:6a0ba999597d 272 printf("\r\n");
kishino 29:6a0ba999597d 273 }
kishino 32:ae95309643aa 274 #endif
kishino 29:6a0ba999597d 275 unsigned char command_id;
kishino 29:6a0ba999597d 276 // Get payload from received data from UART.
kishino 29:6a0ba999597d 277 int payload_len = C_SNIC_UartMsgUtil::getResponsePayload( uartRecvBuf_p->size, uartRecvBuf_p->buf
kishino 29:6a0ba999597d 278 , &command_id, gUART_TEMP_BUF );
kishino 29:6a0ba999597d 279 // Check receive a TCP or UDP packet
kishino 29:6a0ba999597d 280 if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_CONNECTION_RECV_IND) )
kishino 29:6a0ba999597d 281 {
kishino 29:6a0ba999597d 282 // Packet buffering
kishino 29:6a0ba999597d 283 uartCmdMgr_p->bufferredPacket( gUART_TEMP_BUF, payload_len );
kishino 29:6a0ba999597d 284 }
kishino 32:ae95309643aa 285 // Check connected from TCP client
kishino 32:ae95309643aa 286 else if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_TCP_CLIENT_SOCKET_IND) )
kishino 32:ae95309643aa 287 {
kishino 32:ae95309643aa 288 // Connected from TCP client
kishino 32:ae95309643aa 289 uartCmdMgr_p->connectedTCPClient( gUART_TEMP_BUF, payload_len );
kishino 32:ae95309643aa 290 }
kishino 29:6a0ba999597d 291 // Check scan results indication
kishino 29:6a0ba999597d 292 else if( (command_id == UART_CMD_ID_WIFI) && (gUART_TEMP_BUF[0] == UART_CMD_SID_WIFI_SCAN_RESULT_IND) )
kishino 29:6a0ba999597d 293 {
kishino 29:6a0ba999597d 294 // Scan result indicate
kishino 29:6a0ba999597d 295 uartCmdMgr_p->scanResultIndicate( gUART_TEMP_BUF, payload_len );
kishino 29:6a0ba999597d 296 }
kishino 29:6a0ba999597d 297 // Checks in the command which is waiting.
kishino 29:6a0ba999597d 298 else if( uartCmdMgr_p->isWaitingCommand(command_id, gUART_TEMP_BUF) )
kishino 29:6a0ba999597d 299 {
kishino 29:6a0ba999597d 300 //lcd_printf("cid:%02x scid:%02x\r\n", command_id, gUART_TEMP_BUF[0]);
kishino 29:6a0ba999597d 301
kishino 29:6a0ba999597d 302 // Get buffer for payload data
kishino 29:6a0ba999597d 303 unsigned char *payload_buf_p = uartCmdMgr_p->getResponseBuf();
kishino 29:6a0ba999597d 304 if( payload_buf_p != NULL )
kishino 29:6a0ba999597d 305 {
kishino 29:6a0ba999597d 306 memcpy( payload_buf_p, gUART_TEMP_BUF, payload_len );
kishino 29:6a0ba999597d 307 uartCmdMgr_p->setResponseBuf( NULL );
kishino 29:6a0ba999597d 308 }
kishino 29:6a0ba999597d 309 // Set status
kishino 29:6a0ba999597d 310 uartCmdMgr_p->setCommandStatus( gUART_TEMP_BUF[2] );
kishino 29:6a0ba999597d 311 // Set signal for command response wait.
kishino 29:6a0ba999597d 312 uartCmdMgr_p->signal();
kishino 29:6a0ba999597d 313 }
kishino 29:6a0ba999597d 314 //
kishino 29:6a0ba999597d 315 instance_p->freeUartRecvBuf( uartRecvBuf_p );
kishino 29:6a0ba999597d 316
kishino 29:6a0ba999597d 317 evt = mUartRecvQueue.get(500);
kishino 29:6a0ba999597d 318 Thread::yield();
kishino 29:6a0ba999597d 319 } while( evt.status == osEventMessage );
kishino 29:6a0ba999597d 320 }
kishino 29:6a0ba999597d 321 }
kishino 29:6a0ba999597d 322 }