1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
//#define _DEBUG_
#include "server_drv.h"
#include "Arduino.h"
#include "spi_drv.h"
extern "C" {
#include "wl_types.h"
#include "debug.h"
}
// Start server TCP on port specified
void ServerDrv::startServer(uint16_t port, uint8_t sock, uint8_t protMode)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_3);
SpiDrv::sendParam(port);
SpiDrv::sendParam(&sock, 1);
SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseCmd(START_SERVER_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
}
// Start server TCP on port specified
void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_4);
SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress));
SpiDrv::sendParam(port);
SpiDrv::sendParam(&sock, 1);
SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
}
// Start server TCP on port specified
void ServerDrv::stopClient(uint8_t sock)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, 1, LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
}
uint8_t ServerDrv::getServerState(uint8_t sock)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
return _data;
}
uint8_t ServerDrv::getClientState(uint8_t sock)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
return _data;
}
uint16_t ServerDrv::availData(uint8_t sock)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _dataLen = 0;
uint16_t len = 0;
SpiDrv::waitResponseCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, (uint8_t*)&len, &_dataLen);
SpiDrv::spiSlaveDeselect();
return len;
}
bool ServerDrv::getData(uint8_t sock, uint8_t *data, uint8_t peek)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_2);
SpiDrv::sendParam(&sock, sizeof(sock));
SpiDrv::sendParam(peek, LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseData8(GET_DATA_TCP_CMD, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (_dataLen!=0)
{
*data = _data;
return true;
}
return false;
}
bool ServerDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(GET_DATABUF_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendBuffer(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
if (!SpiDrv::waitResponseData16(GET_DATABUF_TCP_CMD, _data, _dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (*_dataLen!=0)
{
return true;
}
return false;
}
bool ServerDrv::insertDataBuf(uint8_t sock, const uint8_t *data, uint16_t _len)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(INSERT_DATABUF_CMD, PARAM_NUMS_2);
SpiDrv::sendBuffer(&sock, sizeof(sock));
SpiDrv::sendBuffer((uint8_t *)data, _len, LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseData8(INSERT_DATABUF_CMD, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (_dataLen!=0)
{
return (_data == 1);
}
return false;
}
bool ServerDrv::sendUdpData(uint8_t sock)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(SEND_DATA_UDP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseData8(SEND_DATA_UDP_CMD, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (_dataLen!=0)
{
return (_data == 1);
}
return false;
}
bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(SEND_DATA_TCP_CMD, PARAM_NUMS_2);
SpiDrv::sendBuffer(&sock, sizeof(sock));
SpiDrv::sendBuffer((uint8_t *)data, len, LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseData8(SEND_DATA_TCP_CMD, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (_dataLen!=0)
{
return (_data == 1);
}
return false;
}
uint8_t ServerDrv::checkDataSent(uint8_t sock)
{
const uint16_t TIMEOUT_DATA_SENT = 25;
uint16_t timeout = 0;
uint8_t _data = 0;
uint8_t _dataLen = 0;
do {
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse isDataSent");
}
SpiDrv::spiSlaveDeselect();
if (_data) timeout = 0;
else{
++timeout;
delay(100);
}
}while((_data==0)&&(timeout<TIMEOUT_DATA_SENT));
return (timeout==TIMEOUT_DATA_SENT)?0:1;
}
ServerDrv serverDrv;
|