Customized SNICInterface
Fork of NySNICInterface by
SNIC/SNIC_Core.cpp@40:b6b10c22a121, 2014-07-15 (annotated)
- Committer:
- kishino
- Date:
- Tue Jul 15 09:56:37 2014 +0000
- Revision:
- 40:b6b10c22a121
- Parent:
- 39:a1233ca02edf
- Child:
- 41:1c1b5ad4d491
The platform-dependent code was modified to implemented in ifdef.; The process of debug log output was changed to macro.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kishino | 39:a1233ca02edf | 1 | /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License |
kishino | 39:a1233ca02edf | 2 | * muRata, SWITCH SCIENCE Wi-FI module TypeYD-SNIC UART. |
kishino | 20:dd736d328de6 | 3 | * |
kishino | 39:a1233ca02edf | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
kishino | 39:a1233ca02edf | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
kishino | 39:a1233ca02edf | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
kishino | 39:a1233ca02edf | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
kishino | 39:a1233ca02edf | 8 | * furnished to do so, subject to the following conditions: |
kishino | 20:dd736d328de6 | 9 | * |
kishino | 39:a1233ca02edf | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
kishino | 39:a1233ca02edf | 11 | * substantial portions of the Software. |
kishino | 20:dd736d328de6 | 12 | * |
kishino | 39:a1233ca02edf | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
kishino | 39:a1233ca02edf | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
kishino | 39:a1233ca02edf | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kishino | 39:a1233ca02edf | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kishino | 39:a1233ca02edf | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kishino | 39:a1233ca02edf | 18 | */ |
kishino | 20:dd736d328de6 | 19 | #include "mbed.h" |
kishino | 20:dd736d328de6 | 20 | #include "SNIC_Core.h" |
kishino | 20:dd736d328de6 | 21 | #include "SNIC_UartMsgUtil.h" |
kishino | 20:dd736d328de6 | 22 | #include <string> |
kishino | 20:dd736d328de6 | 23 | |
kishino | 29:6a0ba999597d | 24 | /** Wait signal ID of UART recv */ |
kishino | 35:e4e7f86fd975 | 25 | #define UART_DISPATCH_SIGNAL 0x00000002 |
kishino | 20:dd736d328de6 | 26 | |
kishino | 35:e4e7f86fd975 | 27 | #define UART_RECVBUF_SIZE 2048 |
kishino | 29:6a0ba999597d | 28 | #define UART_THREAD_STACK_SIZE 512 |
kishino | 39:a1233ca02edf | 29 | #define UART_FIXED_HEADER_SIZE 3 |
kishino | 39:a1233ca02edf | 30 | #define UART_FIXED_SIZE_IN_FRAME 6 |
kishino | 39:a1233ca02edf | 31 | #define UART_RECV_QUEUE_TIMEOUT 500 |
kishino | 29:6a0ba999597d | 32 | |
kishino | 20:dd736d328de6 | 33 | typedef struct |
kishino | 20:dd736d328de6 | 34 | { |
kishino | 35:e4e7f86fd975 | 35 | tagMEMPOOL_BLOCK_T *mem_p; |
kishino | 20:dd736d328de6 | 36 | unsigned int size; |
kishino | 20:dd736d328de6 | 37 | }tagUART_RECVBUF_T; |
kishino | 20:dd736d328de6 | 38 | |
kishino | 29:6a0ba999597d | 39 | /* |
kishino | 29:6a0ba999597d | 40 | Define the global buffer using the area for Ethernet. |
kishino | 29:6a0ba999597d | 41 | */ |
kishino | 29:6a0ba999597d | 42 | unsigned char gUART_TEMP_BUF[UART_RECVBUF_SIZE] __attribute__((section("AHBSRAM1"))); |
kishino | 29:6a0ba999597d | 43 | unsigned char gUART_COMMAND_BUF[UART_REQUEST_PAYLOAD_MAX] __attribute__((section("AHBSRAM1"))); |
kishino | 29:6a0ba999597d | 44 | /** MemoryPool for payload of UART response */ |
kishino | 29:6a0ba999597d | 45 | MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_PAYLOAD_NUM> mMemPoolPayload __attribute__((section("AHBSRAM1"))); |
kishino | 29:6a0ba999597d | 46 | /** MemoryPool for UART receive */ |
kishino | 29:6a0ba999597d | 47 | MemoryPool<tagMEMPOOL_BLOCK_T, MEMPOOL_UART_RECV_NUM> mMemPoolUartRecv __attribute__((section("AHBSRAM1"))); |
kishino | 35:e4e7f86fd975 | 48 | Queue<tagMEMPOOL_BLOCK_T, MEMPOOL_UART_RECV_NUM> mUartRecvQueue; |
kishino | 29:6a0ba999597d | 49 | |
kishino | 29:6a0ba999597d | 50 | tagMEMPOOL_BLOCK_T *gUART_RCVBUF_p; |
kishino | 20:dd736d328de6 | 51 | C_SNIC_Core *C_SNIC_Core::mInstance_p = NULL; |
kishino | 20:dd736d328de6 | 52 | |
kishino | 20:dd736d328de6 | 53 | C_SNIC_Core *C_SNIC_Core::getInstance() |
kishino | 20:dd736d328de6 | 54 | { |
kishino | 20:dd736d328de6 | 55 | if( mInstance_p == NULL ) |
kishino | 20:dd736d328de6 | 56 | { |
kishino | 20:dd736d328de6 | 57 | mInstance_p = new C_SNIC_Core(); |
kishino | 20:dd736d328de6 | 58 | } |
kishino | 20:dd736d328de6 | 59 | return mInstance_p; |
kishino | 20:dd736d328de6 | 60 | } |
kishino | 20:dd736d328de6 | 61 | |
kishino | 20:dd736d328de6 | 62 | C_SNIC_Core::C_SNIC_Core() |
kishino | 20:dd736d328de6 | 63 | { |
kishino | 20:dd736d328de6 | 64 | int i; |
kishino | 20:dd736d328de6 | 65 | |
kishino | 20:dd736d328de6 | 66 | mUartCommand_p = new C_SNIC_UartCommandManager(); |
kishino | 20:dd736d328de6 | 67 | for( i = 0; i < MAX_SOCKET_ID+1; i++ ) |
kishino | 20:dd736d328de6 | 68 | { |
kishino | 20:dd736d328de6 | 69 | mConnectInfo[i].recvbuf_p = NULL; |
kishino | 20:dd736d328de6 | 70 | mConnectInfo[i].is_connected = false; |
kishino | 33:33f1bc919486 | 71 | |
kishino | 33:33f1bc919486 | 72 | mUdpRecvInfo[i].recvbuf_p = NULL; |
kishino | 33:33f1bc919486 | 73 | mUdpRecvInfo[i].is_received = false; |
kishino | 20:dd736d328de6 | 74 | } |
kishino | 20:dd736d328de6 | 75 | |
kishino | 29:6a0ba999597d | 76 | mUartRecvThread_p = NULL; |
kishino | 29:6a0ba999597d | 77 | mUartRecvDispatchThread_p = NULL; |
kishino | 20:dd736d328de6 | 78 | } |
kishino | 20:dd736d328de6 | 79 | |
kishino | 26:f2e1030964e4 | 80 | C_SNIC_Core::~C_SNIC_Core() |
kishino | 26:f2e1030964e4 | 81 | { |
kishino | 26:f2e1030964e4 | 82 | } |
kishino | 26:f2e1030964e4 | 83 | |
kishino | 29:6a0ba999597d | 84 | int C_SNIC_Core::resetModule( PinName reset ) |
kishino | 29:6a0ba999597d | 85 | { |
kishino | 29:6a0ba999597d | 86 | DigitalOut reset_pin( reset ); |
kishino | 29:6a0ba999597d | 87 | |
kishino | 29:6a0ba999597d | 88 | reset_pin = 0; |
kishino | 37:f3a2053627c2 | 89 | wait(0.1); |
kishino | 29:6a0ba999597d | 90 | reset_pin = 1; |
kishino | 37:f3a2053627c2 | 91 | wait(0.1); |
kishino | 29:6a0ba999597d | 92 | |
kishino | 29:6a0ba999597d | 93 | return 0; |
kishino | 29:6a0ba999597d | 94 | } |
kishino | 29:6a0ba999597d | 95 | |
kishino | 20:dd736d328de6 | 96 | int C_SNIC_Core::initUart(PinName tx, PinName rx, int baud) |
kishino | 20:dd736d328de6 | 97 | { |
kishino | 38:f13e4e563d65 | 98 | mUartRequestSeq = 0; |
kishino | 20:dd736d328de6 | 99 | |
kishino | 20:dd736d328de6 | 100 | mUart_p = new RawSerial( tx, rx ); |
kishino | 20:dd736d328de6 | 101 | mUart_p->baud( baud ); |
kishino | 20:dd736d328de6 | 102 | mUart_p->format(8, SerialBase::None, 1); |
kishino | 29:6a0ba999597d | 103 | |
kishino | 20:dd736d328de6 | 104 | // Initialize uart |
kishino | 38:f13e4e563d65 | 105 | gUART_RCVBUF_p = NULL; |
kishino | 20:dd736d328de6 | 106 | |
kishino | 35:e4e7f86fd975 | 107 | mUart_p->attach( C_SNIC_Core::uartRecvCallback ); |
kishino | 29:6a0ba999597d | 108 | // Create UART recv dispatch thread |
kishino | 29:6a0ba999597d | 109 | mUartRecvDispatchThread_p = new Thread( C_SNIC_Core::uartRecvDispatchThread, NULL, osPriorityNormal, UART_THREAD_STACK_SIZE); |
kishino | 29:6a0ba999597d | 110 | if( mUartRecvDispatchThread_p == NULL ) |
kishino | 20:dd736d328de6 | 111 | { |
kishino | 40:b6b10c22a121 | 112 | DEBUG_PRINT("[C_SNIC_Core::initUart] thread create failed\r\n"); |
kishino | 20:dd736d328de6 | 113 | return -1; |
kishino | 20:dd736d328de6 | 114 | } |
kishino | 20:dd736d328de6 | 115 | |
kishino | 20:dd736d328de6 | 116 | return 0; |
kishino | 20:dd736d328de6 | 117 | } |
kishino | 38:f13e4e563d65 | 118 | unsigned int C_SNIC_Core::preparationSendCommand( unsigned char cmd_id, unsigned char cmd_sid |
kishino | 20:dd736d328de6 | 119 | , unsigned char *req_buf_p, unsigned int req_buf_len |
kishino | 20:dd736d328de6 | 120 | , unsigned char *response_buf_p, unsigned char *command_p ) |
kishino | 20:dd736d328de6 | 121 | { |
kishino | 38:f13e4e563d65 | 122 | unsigned int command_len = 0; |
kishino | 20:dd736d328de6 | 123 | |
kishino | 20:dd736d328de6 | 124 | // Make all command request |
kishino | 36:f33fcf5975ab | 125 | command_len = C_SNIC_UartMsgUtil::makeRequest( cmd_id, req_buf_p, req_buf_len, command_p ); |
kishino | 20:dd736d328de6 | 126 | |
kishino | 20:dd736d328de6 | 127 | // Set data for response |
kishino | 20:dd736d328de6 | 128 | mUartCommand_p->setCommandID( cmd_id ); |
kishino | 20:dd736d328de6 | 129 | mUartCommand_p->setCommandSID( cmd_sid | 0x80 ); |
kishino | 20:dd736d328de6 | 130 | mUartCommand_p->setResponseBuf( response_buf_p ); |
kishino | 20:dd736d328de6 | 131 | |
kishino | 20:dd736d328de6 | 132 | return command_len; |
kishino | 20:dd736d328de6 | 133 | } |
kishino | 20:dd736d328de6 | 134 | |
kishino | 20:dd736d328de6 | 135 | int C_SNIC_Core::sendUart( unsigned int len, unsigned char *data ) |
kishino | 20:dd736d328de6 | 136 | { |
kishino | 20:dd736d328de6 | 137 | int ret = 0; |
kishino | 20:dd736d328de6 | 138 | mUartMutex.lock(); |
kishino | 20:dd736d328de6 | 139 | for( int i = 0; i < len; i++ ) |
kishino | 20:dd736d328de6 | 140 | { |
kishino | 20:dd736d328de6 | 141 | // Write to UART |
kishino | 20:dd736d328de6 | 142 | ret = mUart_p->putc( data[i] ); |
kishino | 20:dd736d328de6 | 143 | if( ret == -1 ) |
kishino | 20:dd736d328de6 | 144 | { |
kishino | 20:dd736d328de6 | 145 | break; |
kishino | 20:dd736d328de6 | 146 | } |
kishino | 20:dd736d328de6 | 147 | } |
kishino | 20:dd736d328de6 | 148 | mUartMutex.unlock(); |
kishino | 36:f33fcf5975ab | 149 | |
kishino | 20:dd736d328de6 | 150 | return ret; |
kishino | 20:dd736d328de6 | 151 | } |
kishino | 20:dd736d328de6 | 152 | |
kishino | 29:6a0ba999597d | 153 | tagMEMPOOL_BLOCK_T *C_SNIC_Core::allocCmdBuf() |
kishino | 20:dd736d328de6 | 154 | { |
kishino | 20:dd736d328de6 | 155 | // Get buffer from MemoryPool |
kishino | 20:dd736d328de6 | 156 | return mMemPoolPayload.alloc(); |
kishino | 20:dd736d328de6 | 157 | } |
kishino | 20:dd736d328de6 | 158 | |
kishino | 20:dd736d328de6 | 159 | void C_SNIC_Core::freeCmdBuf( tagMEMPOOL_BLOCK_T *buf_p ) |
kishino | 20:dd736d328de6 | 160 | { |
kishino | 20:dd736d328de6 | 161 | mMemPoolPayload.free( buf_p ); |
kishino | 20:dd736d328de6 | 162 | } |
kishino | 20:dd736d328de6 | 163 | |
kishino | 29:6a0ba999597d | 164 | tagMEMPOOL_BLOCK_T *C_SNIC_Core::allocUartRcvBuf() |
kishino | 29:6a0ba999597d | 165 | { |
kishino | 29:6a0ba999597d | 166 | // Get buffer from MemoryPool |
kishino | 29:6a0ba999597d | 167 | return mMemPoolUartRecv.alloc(); |
kishino | 29:6a0ba999597d | 168 | } |
kishino | 29:6a0ba999597d | 169 | |
kishino | 29:6a0ba999597d | 170 | void C_SNIC_Core::freeUartRecvBuf( tagMEMPOOL_BLOCK_T *buf_p ) |
kishino | 29:6a0ba999597d | 171 | { |
kishino | 29:6a0ba999597d | 172 | mMemPoolUartRecv.free( buf_p ); |
kishino | 29:6a0ba999597d | 173 | } |
kishino | 29:6a0ba999597d | 174 | |
kishino | 22:a9ec0cad4f84 | 175 | C_SNIC_Core::tagCONNECT_INFO_T *C_SNIC_Core::getConnectInfo( int socket_id ) |
kishino | 20:dd736d328de6 | 176 | { |
kishino | 20:dd736d328de6 | 177 | if( (socket_id < 0) || (socket_id > MAX_SOCKET_ID) ) |
kishino | 20:dd736d328de6 | 178 | { |
kishino | 20:dd736d328de6 | 179 | return NULL; |
kishino | 20:dd736d328de6 | 180 | } |
kishino | 20:dd736d328de6 | 181 | return &mConnectInfo[socket_id]; |
kishino | 20:dd736d328de6 | 182 | } |
kishino | 20:dd736d328de6 | 183 | |
kishino | 33:33f1bc919486 | 184 | C_SNIC_Core::tagUDP_RECVINFO_T *C_SNIC_Core::getUdpRecvInfo( int socket_id ) |
kishino | 33:33f1bc919486 | 185 | { |
kishino | 33:33f1bc919486 | 186 | if( (socket_id < 0) || (socket_id > MAX_SOCKET_ID) ) |
kishino | 33:33f1bc919486 | 187 | { |
kishino | 33:33f1bc919486 | 188 | return NULL; |
kishino | 33:33f1bc919486 | 189 | } |
kishino | 33:33f1bc919486 | 190 | return &mUdpRecvInfo[socket_id]; |
kishino | 33:33f1bc919486 | 191 | } |
kishino | 33:33f1bc919486 | 192 | |
kishino | 20:dd736d328de6 | 193 | C_SNIC_UartCommandManager *C_SNIC_Core::getUartCommand() |
kishino | 20:dd736d328de6 | 194 | { |
kishino | 20:dd736d328de6 | 195 | return mUartCommand_p; |
kishino | 20:dd736d328de6 | 196 | } |
kishino | 20:dd736d328de6 | 197 | |
kishino | 29:6a0ba999597d | 198 | unsigned char *C_SNIC_Core::getCommandBuf() |
kishino | 29:6a0ba999597d | 199 | { |
kishino | 29:6a0ba999597d | 200 | return gUART_COMMAND_BUF; |
kishino | 29:6a0ba999597d | 201 | } |
kishino | 20:dd736d328de6 | 202 | |
kishino | 29:6a0ba999597d | 203 | void C_SNIC_Core::uartRecvCallback( void ) |
kishino | 29:6a0ba999597d | 204 | { |
kishino | 20:dd736d328de6 | 205 | C_SNIC_Core *instance_p = C_SNIC_Core::getInstance(); |
kishino | 29:6a0ba999597d | 206 | if( instance_p != NULL ) |
kishino | 20:dd736d328de6 | 207 | { |
kishino | 29:6a0ba999597d | 208 | int recvdata = 0; |
kishino | 20:dd736d328de6 | 209 | |
kishino | 39:a1233ca02edf | 210 | // Check received data from UART. |
kishino | 39:a1233ca02edf | 211 | while( instance_p->mUart_p->readable() ) |
kishino | 20:dd736d328de6 | 212 | { |
kishino | 20:dd736d328de6 | 213 | // Receive data from UART. |
kishino | 20:dd736d328de6 | 214 | recvdata = instance_p->mUart_p->getc(); |
kishino | 20:dd736d328de6 | 215 | |
kishino | 29:6a0ba999597d | 216 | // Check UART receiving buffer |
kishino | 29:6a0ba999597d | 217 | if( gUART_RCVBUF_p != NULL ) |
kishino | 20:dd736d328de6 | 218 | { |
kishino | 29:6a0ba999597d | 219 | gUART_RCVBUF_p->buf[ gUART_RCVBUF_p->size ] = (unsigned char)recvdata; |
kishino | 29:6a0ba999597d | 220 | gUART_RCVBUF_p->size++; |
kishino | 29:6a0ba999597d | 221 | |
kishino | 39:a1233ca02edf | 222 | if( gUART_RCVBUF_p->size == UART_FIXED_HEADER_SIZE ) |
kishino | 20:dd736d328de6 | 223 | { |
kishino | 36:f33fcf5975ab | 224 | // get demand size |
kishino | 36:f33fcf5975ab | 225 | unsigned short payload_len = ( ( (gUART_RCVBUF_p->buf[1] & ~0x80) & 0xff) | ( ( (gUART_RCVBUF_p->buf[2] & ~0xC0) << 7) & 0xff80) ); |
kishino | 39:a1233ca02edf | 226 | gUART_RCVBUF_p->demand_size = payload_len + UART_FIXED_SIZE_IN_FRAME; |
kishino | 39:a1233ca02edf | 227 | if( gUART_RCVBUF_p->demand_size > MEMPOOL_BLOCK_SIZE ) |
kishino | 39:a1233ca02edf | 228 | { |
kishino | 39:a1233ca02edf | 229 | gUART_RCVBUF_p->demand_size = MEMPOOL_BLOCK_SIZE; |
kishino | 39:a1233ca02edf | 230 | } |
kishino | 36:f33fcf5975ab | 231 | } |
kishino | 36:f33fcf5975ab | 232 | |
kishino | 36:f33fcf5975ab | 233 | if( gUART_RCVBUF_p->demand_size > 0 ) |
kishino | 36:f33fcf5975ab | 234 | { |
kishino | 36:f33fcf5975ab | 235 | // Check size of received data. |
kishino | 36:f33fcf5975ab | 236 | if( gUART_RCVBUF_p->size >= gUART_RCVBUF_p->demand_size ) |
kishino | 36:f33fcf5975ab | 237 | { |
kishino | 36:f33fcf5975ab | 238 | // Add queue |
kishino | 36:f33fcf5975ab | 239 | mUartRecvQueue.put( gUART_RCVBUF_p ); |
kishino | 36:f33fcf5975ab | 240 | |
kishino | 36:f33fcf5975ab | 241 | gUART_RCVBUF_p = NULL; |
kishino | 20:dd736d328de6 | 242 | |
kishino | 36:f33fcf5975ab | 243 | // set signal for dispatch thread |
kishino | 36:f33fcf5975ab | 244 | instance_p->mUartRecvDispatchThread_p->signal_set( UART_DISPATCH_SIGNAL ); |
kishino | 36:f33fcf5975ab | 245 | } |
kishino | 20:dd736d328de6 | 246 | } |
kishino | 36:f33fcf5975ab | 247 | |
kishino | 20:dd736d328de6 | 248 | } |
kishino | 20:dd736d328de6 | 249 | else |
kishino | 20:dd736d328de6 | 250 | { |
kishino | 20:dd736d328de6 | 251 | // Check received data is SOM. |
kishino | 20:dd736d328de6 | 252 | if( recvdata == UART_CMD_SOM ) |
kishino | 20:dd736d328de6 | 253 | { |
kishino | 29:6a0ba999597d | 254 | gUART_RCVBUF_p = instance_p->allocUartRcvBuf(); |
kishino | 29:6a0ba999597d | 255 | gUART_RCVBUF_p->size = 0; |
kishino | 36:f33fcf5975ab | 256 | gUART_RCVBUF_p->demand_size = 0; |
kishino | 29:6a0ba999597d | 257 | // get buffer for Uart receive |
kishino | 29:6a0ba999597d | 258 | gUART_RCVBUF_p->buf[ 0 ] = (unsigned char)recvdata; |
kishino | 29:6a0ba999597d | 259 | |
kishino | 29:6a0ba999597d | 260 | gUART_RCVBUF_p->size++; |
kishino | 20:dd736d328de6 | 261 | } |
kishino | 20:dd736d328de6 | 262 | } |
kishino | 20:dd736d328de6 | 263 | } |
kishino | 20:dd736d328de6 | 264 | } |
kishino | 20:dd736d328de6 | 265 | } |
kishino | 20:dd736d328de6 | 266 | |
kishino | 29:6a0ba999597d | 267 | void C_SNIC_Core::uartRecvDispatchThread (void const *args_p) |
kishino | 29:6a0ba999597d | 268 | { |
kishino | 29:6a0ba999597d | 269 | C_SNIC_Core *instance_p = C_SNIC_Core::getInstance(); |
kishino | 29:6a0ba999597d | 270 | C_SNIC_UartCommandManager *uartCmdMgr_p = instance_p->getUartCommand(); |
kishino | 29:6a0ba999597d | 271 | |
kishino | 29:6a0ba999597d | 272 | tagMEMPOOL_BLOCK_T *uartRecvBuf_p; |
kishino | 29:6a0ba999597d | 273 | osEvent evt; |
kishino | 29:6a0ba999597d | 274 | |
kishino | 29:6a0ba999597d | 275 | for(;;) |
kishino | 29:6a0ba999597d | 276 | { |
kishino | 29:6a0ba999597d | 277 | // wait |
kishino | 29:6a0ba999597d | 278 | Thread::signal_wait( UART_DISPATCH_SIGNAL ); |
kishino | 29:6a0ba999597d | 279 | |
kishino | 29:6a0ba999597d | 280 | // Get scanresults from queue |
kishino | 39:a1233ca02edf | 281 | evt = mUartRecvQueue.get(UART_RECV_QUEUE_TIMEOUT); |
kishino | 29:6a0ba999597d | 282 | if (evt.status == osEventMessage) |
kishino | 29:6a0ba999597d | 283 | { |
kishino | 29:6a0ba999597d | 284 | do |
kishino | 29:6a0ba999597d | 285 | { |
kishino | 29:6a0ba999597d | 286 | uartRecvBuf_p = (tagMEMPOOL_BLOCK_T *)evt.value.p; |
kishino | 29:6a0ba999597d | 287 | |
kishino | 38:f13e4e563d65 | 288 | #if 0 /* for Debug */ |
kishino | 29:6a0ba999597d | 289 | { |
kishino | 29:6a0ba999597d | 290 | int i; |
kishino | 29:6a0ba999597d | 291 | for(i=0;i<uartRecvBuf_p->size;i++) |
kishino | 29:6a0ba999597d | 292 | { |
kishino | 40:b6b10c22a121 | 293 | DEBUG_PRINT("%02x", uartRecvBuf_p->buf[i]); |
kishino | 29:6a0ba999597d | 294 | } |
kishino | 40:b6b10c22a121 | 295 | DEBUG_PRINT("\r\n"); |
kishino | 29:6a0ba999597d | 296 | } |
kishino | 32:ae95309643aa | 297 | #endif |
kishino | 29:6a0ba999597d | 298 | unsigned char command_id; |
kishino | 29:6a0ba999597d | 299 | // Get payload from received data from UART. |
kishino | 29:6a0ba999597d | 300 | int payload_len = C_SNIC_UartMsgUtil::getResponsePayload( uartRecvBuf_p->size, uartRecvBuf_p->buf |
kishino | 29:6a0ba999597d | 301 | , &command_id, gUART_TEMP_BUF ); |
kishino | 33:33f1bc919486 | 302 | // Check receive a TCP packet |
kishino | 29:6a0ba999597d | 303 | if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_CONNECTION_RECV_IND) ) |
kishino | 29:6a0ba999597d | 304 | { |
kishino | 29:6a0ba999597d | 305 | // Packet buffering |
kishino | 29:6a0ba999597d | 306 | uartCmdMgr_p->bufferredPacket( gUART_TEMP_BUF, payload_len ); |
kishino | 29:6a0ba999597d | 307 | } |
kishino | 32:ae95309643aa | 308 | // Check connected from TCP client |
kishino | 32:ae95309643aa | 309 | else if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_TCP_CLIENT_SOCKET_IND) ) |
kishino | 32:ae95309643aa | 310 | { |
kishino | 32:ae95309643aa | 311 | // Connected from TCP client |
kishino | 32:ae95309643aa | 312 | uartCmdMgr_p->connectedTCPClient( gUART_TEMP_BUF, payload_len ); |
kishino | 32:ae95309643aa | 313 | } |
kishino | 33:33f1bc919486 | 314 | // Check receive UDP packet |
kishino | 33:33f1bc919486 | 315 | else if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_UDP_RECV_IND) ) |
kishino | 33:33f1bc919486 | 316 | { |
kishino | 33:33f1bc919486 | 317 | // UDP packet buffering |
kishino | 33:33f1bc919486 | 318 | uartCmdMgr_p->bufferredUDPPacket( gUART_TEMP_BUF, payload_len ); |
kishino | 33:33f1bc919486 | 319 | } |
kishino | 29:6a0ba999597d | 320 | // Check scan results indication |
kishino | 29:6a0ba999597d | 321 | else if( (command_id == UART_CMD_ID_WIFI) && (gUART_TEMP_BUF[0] == UART_CMD_SID_WIFI_SCAN_RESULT_IND) ) |
kishino | 29:6a0ba999597d | 322 | { |
kishino | 29:6a0ba999597d | 323 | // Scan result indicate |
kishino | 29:6a0ba999597d | 324 | uartCmdMgr_p->scanResultIndicate( gUART_TEMP_BUF, payload_len ); |
kishino | 29:6a0ba999597d | 325 | } |
kishino | 29:6a0ba999597d | 326 | // Checks in the command which is waiting. |
kishino | 29:6a0ba999597d | 327 | else if( uartCmdMgr_p->isWaitingCommand(command_id, gUART_TEMP_BUF) ) |
kishino | 29:6a0ba999597d | 328 | { |
kishino | 29:6a0ba999597d | 329 | // Get buffer for payload data |
kishino | 29:6a0ba999597d | 330 | unsigned char *payload_buf_p = uartCmdMgr_p->getResponseBuf(); |
kishino | 29:6a0ba999597d | 331 | if( payload_buf_p != NULL ) |
kishino | 29:6a0ba999597d | 332 | { |
kishino | 29:6a0ba999597d | 333 | memcpy( payload_buf_p, gUART_TEMP_BUF, payload_len ); |
kishino | 29:6a0ba999597d | 334 | uartCmdMgr_p->setResponseBuf( NULL ); |
kishino | 29:6a0ba999597d | 335 | } |
kishino | 29:6a0ba999597d | 336 | // Set status |
kishino | 29:6a0ba999597d | 337 | uartCmdMgr_p->setCommandStatus( gUART_TEMP_BUF[2] ); |
kishino | 29:6a0ba999597d | 338 | // Set signal for command response wait. |
kishino | 29:6a0ba999597d | 339 | uartCmdMgr_p->signal(); |
kishino | 29:6a0ba999597d | 340 | } |
kishino | 39:a1233ca02edf | 341 | else |
kishino | 39:a1233ca02edf | 342 | { |
kishino | 40:b6b10c22a121 | 343 | DEBUG_PRINT(" The received data is not expected.\r\n"); |
kishino | 39:a1233ca02edf | 344 | } |
kishino | 39:a1233ca02edf | 345 | |
kishino | 29:6a0ba999597d | 346 | // |
kishino | 29:6a0ba999597d | 347 | instance_p->freeUartRecvBuf( uartRecvBuf_p ); |
kishino | 29:6a0ba999597d | 348 | |
kishino | 29:6a0ba999597d | 349 | evt = mUartRecvQueue.get(500); |
kishino | 29:6a0ba999597d | 350 | } while( evt.status == osEventMessage ); |
kishino | 29:6a0ba999597d | 351 | } |
kishino | 29:6a0ba999597d | 352 | } |
kishino | 29:6a0ba999597d | 353 | } |