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