Customized SNICInterface

Fork of NySNICInterface by Ryo Iizuka

Committer:
kishino
Date:
Tue Jun 10 10:19:48 2014 +0000
Revision:
35:e4e7f86fd975
Parent:
33:33f1bc919486
Child:
36:f33fcf5975ab
[Refactoring];  Deleted unnecessary code.

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