forked from jiahuang/CC3000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm_net.cpp
383 lines (319 loc) · 8.93 KB
/
tm_net.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
#include <Arduino.h>
#include <string.h>
#include "tm_net.h"
#include "tm_debug.h"
#include "host_spi.h"
#include "utility/nvmem.h"
#include "utility/wlan.h"
#include "utility/hci.h"
#include "utility/security.h"
#include "utility/os.h"
#include "utility/netapp.h"
#include "utility/evnt_handler.h"
/**
* Configuration
*/
int tm_net_is_connected ()
{
return ulCC3000Connected;
}
int tm_net_has_ip ()
{
return ulCC3000DHCP;
}
void tm_net_block_until_dhcp ()
{
// Wait for interrupts
while (!tm_net_is_connected() || !tm_net_has_ip()) {
delayMicroseconds(100);
}
}
int tm_net_block_until_dhcp_wait (int waitLength)
{
// Wait for interrupts
int i = 0;
while (i < waitLength && (!tm_net_is_connected() || !tm_net_has_ip())) {
delayMicroseconds(100);
i++;
}
if (tm_net_is_connected() && tm_net_has_ip()){
return 1;
}
return 0;
}
int tm_net_ssid (char ssid[33])
{
tNetappIpconfigRetArgs ipinfo;
netapp_ipconfig(&ipinfo);
memset(ssid, 0, 33);
memcpy(ssid, ipinfo.uaSSID, 32);
return 0;
}
// int tm_net_rssi ()
// {
// uint8_t results[64];
// int res = wlan_ioctl_get_scan_results(10000, results);
// if (res == 0) {
// return results[4 + 4] & 0x7F; // lower 7 bits
// }
// return res;
// }
uint32_t tm_net_local_ip ()
{
tNetappIpconfigRetArgs ipinfo;
netapp_ipconfig(&ipinfo);
return *((uint32_t *) ipinfo.aucIP);
}
int tm_net_mac (uint8_t mac[MAC_ADDR_LEN])
{
return nvmem_get_mac_address(mac);
}
void tm_net_initialize_dhcp_server (void)
{
// Added by Hai Ta
//
// Network mask is assumed to be 255.255.255.0
//
uint8_t pucSubnetMask[4], pucIP_Addr[4], pucIP_DefaultGWAddr[4];
unsigned long pucDNS = 0x04040808;
pucSubnetMask[0] = 0;
pucSubnetMask[1] = 0;
pucSubnetMask[2] = 0;
pucSubnetMask[3] = 0;
pucIP_Addr[0] = 0;
pucIP_Addr[1] = 0;
pucIP_Addr[2] = 0;
pucIP_Addr[3] = 0;
// Use default gateway 192.168.1.1 here
pucIP_DefaultGWAddr[0] = 0;
pucIP_DefaultGWAddr[1] = 0;
pucIP_DefaultGWAddr[2] = 0;
pucIP_DefaultGWAddr[3] = 0;
// In order for gethostbyname( ) to work, it requires DNS server to be configured prior to its usage
// so I am gonna add full static
// Netapp_Dhcp is used to configure the network interface, static or dynamic (DHCP).
// In order to activate DHCP mode, aucIP, aucSubnetMask, aucDefaultGateway must be 0.The default mode of CC3000 is DHCP mode.
netapp_dhcp((unsigned long *)pucIP_Addr, (unsigned long *)pucSubnetMask, (unsigned long *)pucIP_DefaultGWAddr, &pucDNS);
}
int tm_net_is_readable (int ulSocket)
{
fd_set readSet; // Socket file descriptors we want to wake up for, using select()
FD_ZERO(&readSet);
FD_SET(ulSocket, &readSet);
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = 0;
int rcount = select( ulSocket+1, &readSet, (fd_set *) 0, (fd_set *) 0, &timeout );
int flag = FD_ISSET(ulSocket, &readSet);
return flag;
}
int tm_net_block_until_readable (int ulSocket, int timeout) {
while (true) {
if (tm_net_is_readable(ulSocket)) {
break;
}
if (timeout > 0 && --timeout == 0) {
return -1;
}
delay(100);
}
return 0;
}
void tm_net_initialize (void)
{
SpiInit();
delayMicroseconds(100);
TM_DEBUG("Calling wlan_init\n");
wlan_init(CC3000_UsynchCallback, NULL, NULL, NULL, ReadWlanInterruptPin,
WlanInterruptEnable, WlanInterruptDisable, WriteWlanPin);
TM_DEBUG("Calling wlan_start...\n");
wlan_start(0);
// tm_net_initialize_dhcp_server();
// wlan_stop();
// delay(2000);
// wlan_start(0);
TM_DEBUG("setting event mask\n");
wlan_set_event_mask(HCI_EVNT_WLAN_KEEPALIVE|HCI_EVNT_WLAN_UNSOL_INIT|HCI_EVNT_WLAN_ASYNC_PING_REPORT);
// TM_DEBUG("config wlan\n");
// wlan_ioctl_set_connection_policy(0, 1, 0);
unsigned char version[2];
if (!nvmem_read_sp_version(version)) {
TM_DEBUG("Firmware version: %d.%d\n", version[0], version[1]);
} else {
TM_DEBUG("Failed to read version\n");
}
}
void tm_net_set_policy(){
wlan_ioctl_set_connection_policy(0, 1, 0);
}
void tm_net_smartconfig_initialize(void){
StartSmartConfig();
}
void tm_net_disconnect (void)
{
wlan_stop();
}
int tm_net_connect_wpa2 (const char *ssid, const char *keys)
{
TM_DEBUG("Attempting to connect...\n");
int connected = wlan_connect(WLAN_SEC_WPA2, (char *) ssid, strlen(ssid), 0, (unsigned char *) keys, strlen(keys));
TM_DEBUG("Connected: %d\n", connected);
return connected;
}
/**
* UDP
*/
int tm_net_udp_open_socket ()
{
return socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
}
int tm_net_udp_close_socket (int ulSocket)
{
closesocket(ulSocket);
return 0xFFFFFFFF;
}
int tm_net_udp_listen (int ulSocket, int port)
{
sockaddr localSocketAddr;
localSocketAddr.sa_family = AF_INET;
localSocketAddr.sa_data[0] = (port & 0xFF00) >> 8;
localSocketAddr.sa_data[1] = (port & 0x00FF);
localSocketAddr.sa_data[2] = 0;
localSocketAddr.sa_data[3] = 0;
localSocketAddr.sa_data[4] = 0;
localSocketAddr.sa_data[5] = 0;
// Bind socket
int sockStatus;
if ( (sockStatus = bind(ulSocket, &localSocketAddr, sizeof(sockaddr)) ) != 0) {
TM_DEBUG("Binding UDP socket failed: %d\n", sockStatus);
}
return sockStatus;
}
int tm_net_udp_receive (int ulSocket, uint8_t *buf, unsigned long buf_len, sockaddr *from, socklen_t *from_len)
{
signed long ret = recvfrom(ulSocket, buf, buf_len, 0, from, from_len);
if (ret <= 0) {
TM_DEBUG("No data recieved\n");
} else {
TM_DEBUG("Recieved %d UDP bytes\n", ret);
}
return ret;
}
int tm_net_udp_send (int ulSocket, uint8_t ip0, uint8_t ip1, uint8_t ip2, uint8_t ip3, int port, uint8_t *buf, unsigned long buf_len)
{
sockaddr tSocketAddr;
tSocketAddr.sa_family = AF_INET;
tSocketAddr.sa_data[0] = (port & 0xFF00) >> 8;
tSocketAddr.sa_data[1] = (port & 0x00FF);
tSocketAddr.sa_data[2] = ip0;
tSocketAddr.sa_data[3] = ip1;
tSocketAddr.sa_data[4] = ip2;
tSocketAddr.sa_data[5] = ip3;
return sendto(ulSocket, buf, buf_len, 0, &tSocketAddr, sizeof(sockaddr));
}
/**
* TCP
*/
int tm_net_tcp_open_socket ()
{
int ulSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
setsockopt(ulSocket, SOL_SOCKET, SOCKOPT_ACCEPT_NONBLOCK, SOCK_ON, 4);
return ulSocket;
}
int tm_net_tcp_close_socket (int ulSocket)
{
closesocket(ulSocket);
return 0xFFFFFFFF;
}
int tm_net_tcp_connect (int ulSocket, uint8_t ip0, uint8_t ip1, uint8_t ip2, uint8_t ip3, int port)
{
// the family is always AF_INET
sockaddr remoteSocketAddr;
remoteSocketAddr.sa_family = AF_INET;
remoteSocketAddr.sa_data[0] = (port & 0xFF00) >> 8;
remoteSocketAddr.sa_data[1] = (port & 0x00FF);
remoteSocketAddr.sa_data[2] = ip0;
remoteSocketAddr.sa_data[3] = ip1;
remoteSocketAddr.sa_data[4] = ip2;
remoteSocketAddr.sa_data[5] = ip3;
int lerr = connect(ulSocket, &remoteSocketAddr, sizeof(sockaddr));
if (lerr != ESUCCESS) {
TM_DEBUG("Error Connecting\r\n");
}
return lerr;
}
int tm_net_tcp_write (int ulSocket, const uint8_t *buf, unsigned long buf_len)
{
int sentLen = send(ulSocket, buf, buf_len, 0);
// TM_DEBUG("Wrote %d bytes to TCP socket.\n", sentLen);
return sentLen;
}
int tm_net_tcp_read (int ulSocket, uint8_t *buf, int buf_len)
{
return recv(ulSocket, buf, buf_len, 0);
}
int tm_net_tcp_read_byte (int ulSocket)
{
uint8_t buf[1];
int status = tm_net_tcp_read(ulSocket, buf, 1);
if (status <= 0) {
return -1;
}
return buf[0];
}
int tm_net_tcp_listen (int ulSocket, int port)
{
sockaddr localSocketAddr;
localSocketAddr.sa_family = AF_INET;
localSocketAddr.sa_data[0] = (port & 0xFF00) >> 8; //ascii_to_char(0x01, 0x01);
localSocketAddr.sa_data[1] = (port & 0x00FF); //ascii_to_char(0x05, 0x0c);
localSocketAddr.sa_data[2] = 0;
localSocketAddr.sa_data[3] = 0;
localSocketAddr.sa_data[4] = 0;
localSocketAddr.sa_data[5] = 0;
// Bind socket
TM_DEBUG("Binding local socket...\n");
int sockStatus;
if ((sockStatus = bind(ulSocket, &localSocketAddr, sizeof(sockaddr))) != 0) {
TM_DEBUG("binding failed: %d\n", sockStatus);
return -1;
}
TM_DEBUG("Listening on local socket...\n");
int listenStatus = listen(ulSocket, 1);
if (listenStatus != 0) {
TM_DEBUG("cannot listen to socket: %d\n", listenStatus);
return -1;
}
return 0;
}
int tm_net_tcp_accept (int ulSocket, sockaddr *addrClient, socklen_t *addrlen)
{
return accept(ulSocket, addrClient, addrlen);
}
int tm_net_tcp_block_until_accepting (int ulSocket, sockaddr *addrClient, socklen_t *addrlen)
{
int result = 0;
*addrlen = sizeof(sockaddr);
while ((result = tm_net_tcp_accept(ulSocket, addrClient, addrlen)) < 0) {
delayMicroseconds(100);
}
return result;
}
void tm_net_tcp_read_debug (int ulSocket)
{
uint8_t buf[30];
// If no content is readable, stop blocking.
if (tm_net_block_until_readable(ulSocket, 10) < 0) {
return;
}
// Dump bytes.
int tcpc = 0;
while (tm_net_is_readable(ulSocket)) {
int count = tm_net_tcp_read(ulSocket, buf, 30);
tcpc += count;
for (int i = 0; i < count; i++) {
TM_DEBUG("%c", buf[i]);
}
}
TM_DEBUG("\n(Read %d bytes.)\n", tcpc);
}