forked from Sweet-Peas/SP3000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket-support.cpp
509 lines (432 loc) · 13.3 KB
/
socket-support.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
* socket-support.cpp
*
* Created on: 22 sep 2013
* Author: pontus
*
* Comments:
* No efforts have been made to make this library thread safe. Instead focus
* has been on making the library as small, and to use as little stack space
* as possible.
*/
#include "sp3000.h"
#include "debug.hpp"
#include "spi.hpp"
uint8_t txbuf[BUFFER_SIZE];
uint8_t rxbuf[BUFFER_SIZE];
#if BUFFER_SIZE > 255
uint16_t buf_siz = 0;
uint16_t rx_ptr = 0;
#else
uint8_t buf_siz = 0;
uint8_t rx_ptr = 0;
#endif
static char *device_name = "CC3000";
// The Simple Config Prefix always needs to be 'TTT'
static char simpleConfigPrefix[] = { 'T', 'T', 'T' };
//*****************************************************************************
//*
//* Description:
//* A short debug function for printing an IP address in a proper format.
//* This function is not exported or even built when the DEBUG flag is not
//* defined.
//*
//*****************************************************************************
#ifdef DEBUG
void dbgIP (uint32_t IP)
{
char buff[16];
sprintf (buff, "%d.%d.%d.%d",
(unsigned char)(IP >> 24), (unsigned char)(IP >> 16) & 0xff,
(unsigned char)(IP >> 8) & 0xff, (unsigned char)IP & 0xff);
lSer.print (buff);
}
#endif
//*****************************************************************************
//*
//* Description:
//* This function attempts to create a TCP connection to the provided
//* destination host and port. If successful it returns the socket handle
//* associated with the connection and if it fails it simply returns 0.
//*
//*****************************************************************************
int sp_connect(uint32_t destIP, uint16_t destPort, uint32_t type)
{
sockaddr socketAddress;
uint32_t sock;
buf_siz = 0;
rx_ptr = 0;
if (type != IPPROTO_TCP && type != IPPROTO_UDP) {
PRINTLN (F("Invalid socket type !"));
return -1;
}
// Create a socket for the connection
PRINTLN (F("Creating socket !"));
sock = socket(AF_INET, SOCK_STREAM, type);
if (sock < 0) {
PRINTLN(F("Failed to open socket"));
return sock;
}
// Try to open the socket
memset(&socketAddress, 0x00, sizeof(socketAddress));
socketAddress.sa_family = AF_INET;
socketAddress.sa_data[0] = (destPort & 0xFF00) >> 8; // Set the Port Number
socketAddress.sa_data[1] = (destPort & 0x00FF);
socketAddress.sa_data[2] = destIP >> 24;
socketAddress.sa_data[3] = destIP >> 16;
socketAddress.sa_data[4] = destIP >> 8;
socketAddress.sa_data[5] = destIP;
#ifdef DEBUG
PRINT(F("Trying to connect to "));
dbgIP (destIP);
PRINT(':');
PRINTLN(destPort);
#endif
PRINTLN (F("Connecting socket."));
if (-1 == connect(sock, &socketAddress, sizeof(socketAddress))) {
PRINTLN(F("Connection error"));
closesocket(sock);
return -1;
} PRINTLN (F("Successfully connected !"));
return sock;
}
//*****************************************************************************
//*
//* Description:
//* A replacement for the socket method "closesocket" which first ensures
//* that all data has been transmitted by CC3000 before attempting to close
//* the socket.
//*
//*****************************************************************************
int sp_close(int sd)
{
while (tSLInformation.NumberOfSentPackets !=
tSLInformation.NumberOfReleasedPackets);
return closesocket(sd);
}
//*****************************************************************************
//*
//* Description:
//* Sends the content of the supplied string
//*
//*****************************************************************************
int sp_send (uint32_t s, char *str)
{
uint8_t i = 0;
size_t n = 0;
int ret;
PRINTLN (F("Entered send_c"));
if (s < 0)
return -1;
while (1) {
if (!str[i])
break;
txbuf[i] = str[i];
i++;
if (i >= BUFFER_SIZE) {
// Only reached when there is more in the buffer to send
n += send(s, txbuf, BUFFER_SIZE, 0);
i = 0;
}
}
if (0 > (ret = send(s, txbuf, i, 0))) return ret;
n += ret;
PRINTLN (F("Leaving send_c"));
return n;
}
//*****************************************************************************
//*
//* Description:
//* Sends the content of the supplied long int over a socket
//*
//*****************************************************************************
int sp_send (uint32_t s, long value)
{
uint8_t i = 0;
int ret;
PRINTLN (F("Entered send_l"));
if (s < 0)
return -1;
ltoa (value, (char *)txbuf, 10);
i = strlen((char*)txbuf);
if (0 > (ret = send(s, txbuf, i, 0))) return ret;
PRINTLN (F("Leaving send_l"));
return ret;
}
//*****************************************************************************
//*
//* Description:
//* Sends the content of a PROGMEM string over a socket.
//*
//*****************************************************************************
int sp_send (uint32_t s, const __FlashStringHelper *string)
{
uint8_t i = 0;
size_t n = 0;
int ret;
PRINTLN (F("Entered send_F"));
if (s < 0)
return -1;
const char PROGMEM *p = (const char PROGMEM *) string;
while (1) {
unsigned char c = pgm_read_byte(p++);
if (!c)
break;
txbuf[i++] = c;
if (i >= BUFFER_SIZE) {
if (0 > (ret = send (s, txbuf, BUFFER_SIZE, 0))) return ret;
n += ret;
i = 0;
}
}
if ((ret = send(s, txbuf, i, 0)) < 0) return ret;
n += ret;
PRINTLN (F("Leaving send_F"));
return n;
}
//*****************************************************************************
//*
//* Description:
//* Peeks at the data from an incoming socket stream
//*
//*****************************************************************************
uint8_t sp_peek(int16_t s)
{
PRINTLN (F("Entered sp_peek"));
while ((buf_siz <= 0) || (buf_siz == rx_ptr)) {
buf_siz = recv(s, rxbuf, sizeof(rxbuf), 0);
if (buf_siz == -57) {
closesocket(s);
lSer.println (F("Oops, nasty things happen"));
return 0;
}
rx_ptr = 0;
}
PRINTLN (F("Leaving sp_peek"));
return rxbuf[rx_ptr];
}
//*****************************************************************************
//*
//* Description:
//* Reads data from an incoming socket stream
//*
//*****************************************************************************
uint8_t sp_read(int16_t s)
{
uint8_t c;
PRINTLN (F("Entered sp_read"));
c = sp_peek(s);
rx_ptr++;
PRINTLN (F("Leaving sp_read"));
return c;
}
//*****************************************************************************
//*
//* Description:
//* Reads a line from the incoming socket
//*
//*****************************************************************************
int sp_read_line (int16_t s, char *output, int len)
{
char c;
int cnt = 0;
PRINTLN (F("Entered sp_read_line"));
if (s < 0)
return -1;
while (data_available(s) && len--) {
c = sp_read (s);
if ((c == '\n') || (c == 0)) {
*output = 0;
return cnt;
}
*output++ = c;
cnt++;
}
if (!len)
return cnt;
// We only end up here if we ran out of data to soon.
return -1;
}
//*****************************************************************************
//*
//* Description:
//* Checks if there is data available in the supplied socket stream
//*
//*****************************************************************************
uint8_t data_available(int16_t s, long secs, long usecs)
{
timeval timeout;
fd_set fd_read;
PRINTLN (F("Entered data_available"));
// Make sure the bloody socket is open
if (s < 0) {
PRINTLN(F("Socket is closed."));
return 0;
}
// Check if we already have some data that can be read from the buffer
if ((buf_siz > 0) && (rx_ptr < buf_siz))
return (buf_siz - rx_ptr);
// Prepare to do select
memset(&fd_read, 0, sizeof(fd_read));
FD_SET(s, &fd_read);
timeout.tv_sec = secs;
timeout.tv_usec = usecs;
PRINTLN (F("Leaving data_available"));
return ((select(s + 1, &fd_read, NULL, NULL, &timeout) == 1) ? 1 : 0);
}
//*****************************************************************************
//*
//* Description:
//* Checks if there is data available in the socket stream. The number of
//* seconds before it times out can be set.
//*
//*****************************************************************************
uint8_t data_available(int16_t s, long secs)
{
return data_available (s, secs, 0);
}
//*****************************************************************************
//*
//* Description:
//* Checks if there is data available in the socket stream. The timeout
//* is 1 second.
//*
//*****************************************************************************
uint8_t data_available(int16_t s)
{
return data_available (s, 1, 0);
}
static sockaddr_in addr;
//*****************************************************************************
//*
//* Description:
//* Creates a socket, binds it to the local address and a specified port
//* and starts listening to it. It returns the listener socket.
//*
//* Note: The blocking part has not been implemented yet.
//*
//*****************************************************************************
int sp_create_listener (uint16_t port, boolean blocking)
{
int ls;
ls = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ls < 0) {
PRINTLN (F("Failed to created socket !"));
return -SP_SOCK_CREATE_FAILED;
}
memset (&addr, 0, sizeof (addr));
addr.sin_family = htons(AF_INET);
addr.sin_port = htons(port);
if (bind(ls, (const sockaddr*)&addr, sizeof(addr)) != 0) {
PRINTLN (F("Failed to bind address to socket !"));
closesocket (ls);
return -SP_SOCK_BIND_FAILED;
}
if (listen(ls, 10) != 0) {
PRINTLN (F("Failed to connect listener to socket !"));
closesocket (ls);
return -SP_SOCK_LISTEN_FAILED;
}
return ls;
}
//*****************************************************************************
//*
//* Description:
//* A helper function for scanning CC3000 neighborhood
//*
//*****************************************************************************
#ifndef CC3000_TINY_DRIVER
int sp_scan_ssid(uint32_t time)
{
int ret;
const unsigned long intervalTime[16] = { 2000, 2000, 2000, 2000, 2000, 2000,
2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000 };
PRINTLN (F("Entered scanSSIDs"));
ret = wlan_ioctl_set_scan_params(time, 20, 100, 5, 0x7FF, -120, 0, 300,
(unsigned long *) &intervalTime);
PRINTLN (F("Leaving scanSSIDs"));
return ret;
}
#endif
//*****************************************************************************
//*
//* Description:
//* Performs a smart config sequence
//*
//*****************************************************************************
int sp_smart_config(void)
{
int rval;
long timeoutCounter;
PRINTLN (F("Entered do_smart_config"));
PRINTLN (F("Disabling auto-connect policy."));
if ((rval = wlan_ioctl_set_connection_policy(0, 0, 0)) != 0) {
PRINT (F("Setting auto connection policy failed, error: ")); PRINTLN (rval);
return rval;
}
PRINTLN (F("Deleting all existing profiles."));
if ((rval = wlan_ioctl_del_profile(255)) != 0) {
PRINT (F("Deleting all profiles failed, error: ")); PRINTLN (rval);
return rval;
}
PRINTLN (F("Waiting until disconnected..."));
while (ulCC3000Connected == 1);
PRINTLN (F("Setting smart config prefix."));
if ((rval = wlan_smart_config_set_prefix(simpleConfigPrefix)) != 0) {
PRINT (F("Setting smart config prefix failed, error: ")); PRINTLN (rval);
return rval;
}
PRINTLN (F("Starting smart config !"));
if ((rval = wlan_smart_config_start(0)) != 0) {
PRINT (F("Starting smart config failed, error: ")); PRINTLN (rval);
return rval;
}
// Wait for Smartconfig process complete or return an error if we have
// timed out.
timeoutCounter = millis();
while (ulSmartConfigFinished == 0) {
if (millis() - timeoutCounter > 30000) {
PRINTLN (F("Timed out waiting for Smart Config to finish!"));
return -1;
}
}
PRINTLN (F("Enabling auto-connect policy..."));
if ((rval = wlan_ioctl_set_connection_policy(DISABLE,DISABLE,ENABLE)) != 0) {
PRINT (F("Setting auto connection policy failed, error: ")); PRINTLN (rval);
return rval;
}
PRINTLN (F("Stopping CC3000..."));
wlan_stop(); // no error returned here, so nothing to check
PRINTLN (F("Pausing for 2 seconds..."));
delay(2000);
PRINTLN (F("Restarting CC3000... "));
wlan_start(0); // no error returned here, so nothing to check
PRINTLN (F("Waiting for connection to AP and DHCP lease to arrive..."));
while (!ulCC3000Connected && !ulCC3000DHCP);
PRINTLN (F("Sending mDNS broadcast to signal we're done with Smart Config..."));
mdnsAdvertiser (1, device_name, strlen(device_name));
PRINTLN (F("Smart Config finished!"));
return 0;
}
//*****************************************************************************
//*
//* Description:
//* Sets the connection policy of the device
//*
//*****************************************************************************
void sp_set_connection_policy (int policy)
{
byte open_ap = 0;
byte fast_connect = 0;
byte use_profiles = 0;
PRINT ("Connection policy: ");
PRINTLN (policy);
if (policy & SP_CONNECT_TO_OPEN_AP)
open_ap = 1;
if (policy & SP_FAST_CONNECT)
fast_connect = 1;
if (policy & SP_AUTO_START)
use_profiles = 1;
wlan_ioctl_set_connection_policy ((uint32_t)open_ap,
(uint32_t)fast_connect, (uint32_t)use_profiles);
}