-
Notifications
You must be signed in to change notification settings - Fork 20
/
tessel_wifi.c
400 lines (329 loc) · 9.36 KB
/
tessel_wifi.c
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
// Copyright 2014 Technical Machine. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tessel-specific wifi setup
#include <string.h>
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <lpc_types.h>
#include <lpc18xx_gpio.h>
#include "tessel_wifi.h"
#include "hw.h"
#include "variant.h"
#include "tm.h"
#include "utility/wlan.h"
#include "colony.h"
static uint8_t MAX_CC_BOOT_TICKS = 120;
int wifi_initialized = 0;
int wifi_is_connecting = 0;
volatile int validirqcount = 0;
volatile uint8_t CC3K_EVENT_ENABLED = 0;
volatile uint8_t CC3K_IRQ_FLAG = 0;
int CC3K_callback_err = 0;
char *CC3K_callback_payload = 0;
struct wifi_status_t {
int callback_err;
char* callback_payload;
bool post_connect;
};
void wifi_connection_callback();
void wifi_disconnect_callback();
/// The event triggered by the cc callback
tm_event wifi_connect_event = TM_EVENT_INIT(wifi_connection_callback);
tm_event wifi_disconnect_event = TM_EVENT_INIT(wifi_disconnect_callback);
struct wifi_status_t wifi_status = {
.callback_err = 0,
.callback_payload = NULL,
.post_connect = false,
};
void wifi_connection_callback (void) {
// Make sure the Lua state exists
lua_State* L = tm_lua_state;
if (!L) return;
tm_event_unref(&wifi_connect_event);
// Push the _colony_emit helper function onto the stack
lua_getglobal(L, "_colony_emit");
// The process message identifier
lua_pushstring(L, "wifi_connect_complete");
// push whether we got an error (1 or 0)
lua_pushnumber(L, wifi_status.callback_err);
if (wifi_status.callback_payload != NULL) {
lua_pushstring(L, wifi_status.callback_payload);
free(wifi_status.callback_payload);
} else {
lua_pushstring(L, "");
}
// Call _colony_emit to run the JS callback
tm_checked_call(L, 3);
}
void wifi_disconnect_callback(void) {
lua_State* L = tm_lua_state;
if (!L) return;
tm_event_unref(&wifi_disconnect_event);
lua_getglobal(L, "_colony_emit");
lua_pushstring(L, "wifi_disconnect_complete");
lua_pushnumber(L, 0);
tm_checked_call(L, 2);
}
uint8_t get_cc3k_irq_flag () {
return CC3K_IRQ_FLAG;
}
void set_cc3k_irq_flag (uint8_t value) {
CC3K_IRQ_FLAG = value;
}
void SPI_IRQ_CALLBACK_EVENT (tm_event* event)
{
(void) event;
CC3K_EVENT_ENABLED = 0;
if (CC3K_IRQ_FLAG) {
CC3K_IRQ_FLAG = 0;
SPI_IRQ();
}
}
tm_event cc3k_irq_event = TM_EVENT_INIT(SPI_IRQ_CALLBACK_EVENT);
void _tessel_cc3000_irq_interrupt ()
{
validirqcount++;
if (GPIO_GetIntStatus(CC3K_GPIO_INTERRUPT))
{
CC3K_IRQ_FLAG = 1;
if (!CC3K_EVENT_ENABLED) {
CC3K_EVENT_ENABLED = 1;
tm_event_trigger(&cc3k_irq_event);
}
GPIO_ClearInt(TM_INTERRUPT_MODE_FALLING, CC3K_GPIO_INTERRUPT);
}
}
void tessel_wifi_init ()
{
// Turn SW low
hw_digital_output(CC3K_SW_EN);
hw_digital_write(CC3K_SW_EN, 0);
// enable GPIO interrupt for CC3K_IRQ on interrupt #7
hw_interrupt_enable(CC3K_GPIO_INTERRUPT, CC3K_IRQ, TM_INTERRUPT_MODE_FALLING);
}
#ifdef TESSEL_FASTCONNECT
int cc_blink = 1;
#else
int cc_blink = 0;
#endif
int cc_bootup = 0;
void _cc3000_cb_animation_tick (size_t frame)
{
if (cc_blink) {
hw_digital_write(CC3K_CONN_LED, frame & 1 ? 1 : 0);
if (cc_bootup != -1 && cc_bootup < MAX_CC_BOOT_TICKS) {
cc_bootup++;
if (cc_bootup >= MAX_CC_BOOT_TICKS) {
hw_digital_write(CC3K_CONN_LED, 0);
cc_blink = 0;
cc_bootup = -1;
wifi_is_connecting = 0;
}
}
}
}
void _cc3000_cb_acquire ()
{
TM_COMMAND('W', "{\"event\": \"acquire\"}");
cc_blink = 1;
hw_digital_write(CC3K_ERR_LED, 0);
hw_digital_write(CC3K_CONN_LED, 0);
}
void _cc3000_cb_error (int connected)
{
TM_COMMAND('W', "{\"event\": \"error\", \"error\": %d, \"when\": \"acquire\"}", connected);
cc_blink = 0;
wifi_is_connecting = 0;
hw_digital_write(CC3K_ERR_LED, 1);
hw_digital_write(CC3K_CONN_LED, 0);
}
void _cc3000_cb_wifi_connect ()
{
TM_COMMAND('W', "{\"event\": \"connect\"}");
// only dhcp events that come after this are valid
wifi_status.post_connect = true;
cc_blink = 1;
hw_digital_write(CC3K_ERR_LED, 0);
hw_digital_write(CC3K_CONN_LED, 0);
}
void _cc3000_cb_wifi_disconnect ()
{
TM_COMMAND('W', "{\"event\": \"disconnect\"}");
cc_blink = 0;
wifi_is_connecting = 0;
hw_digital_write(CC3K_ERR_LED, 1);
hw_digital_write(CC3K_CONN_LED, 0);
tm_event_trigger(&wifi_disconnect_event);
}
void _cc3000_cb_dhcp_failed ()
{
TM_DEBUG("DHCP failed. Try reconnecting.");
TM_COMMAND('W', "{\"event\": \"dhcp-failed\"}");
wifi_is_connecting = 0;
cc_blink = 0;
cc_bootup = -1;
tessel_wifi_check(1);
hw_digital_write(CC3K_ERR_LED, 1);
hw_digital_write(CC3K_CONN_LED, 0);
}
void _cc3000_cb_dhcp_success ()
{
hw_digital_write(CC3K_CONN_LED, 1);
TM_COMMAND('W', "{\"event\": \"dhcp-success\"}");
wifi_is_connecting = 0;
tessel_wifi_check(1);
cc_blink = 0;
cc_bootup = -1;
hw_digital_write(CC3K_ERR_LED, 0);
hw_digital_write(CC3K_CONN_LED, 1);
}
void _cc3000_cb_tcp_close (int socket)
{
uint32_t s = socket;
if (tm_lua_state != NULL) {
colony_ipc_emit(tm_lua_state, "tcp-close", &s, sizeof(uint32_t));
}
}
int tessel_wifi_initialized(){
return wifi_initialized;
}
int tessel_wifi_is_connecting(){
return wifi_is_connecting;
}
int tessel_wifi_disconnect(){
// wlan_ioctl_set_connection_policy(0, 0, 1);
// prevent a connection from taking place before we can get a response
wifi_is_connecting = 1;
return hw_net_disconnect();
}
void tessel_wifi_disable ()
{
hw_net_disable();
wifi_initialized = 0;
}
void tessel_wifi_enable ()
{
if (!wifi_initialized) {
hw_net_initialize();
wifi_initialized = 1;
wifi_is_connecting = 1;
cc_bootup = 0;
tm_net_initialize_dhcp_server();
} else {
// disable and re-enable
tessel_wifi_disable();
tessel_wifi_enable();
}
}
void tessel_wifi_smart_config ()
{
if (hw_net_online_status()) {
tessel_wifi_disconnect();
}
// tm_task_idle_start(tm_task_default_loop(), StartSmartConfig, 0);
// tm_net_initialize();
StartSmartConfig();
}
#define TM_BYTE(A, B) ((A >> (B*8)) & 0xFF)
char* tessel_wifi_json(){
uint32_t ip_addr = (hw_wifi_ip[3] << 24) | (hw_wifi_ip[2] << 16) | (hw_wifi_ip[1] << 8) | (hw_wifi_ip[0]);
TM_DEBUG("IP Address: %ld.%ld.%ld.%ld", TM_BYTE(ip_addr, 3), TM_BYTE(ip_addr, 2), TM_BYTE(ip_addr, 1), TM_BYTE(ip_addr, 0));
uint32_t ip_dns = 0;
uint32_t ip_dhcp = 0;
uint32_t ip_gw = 0;
uint8_t connected = 0;
char ssid[33] = { 0 };
if (wifi_initialized && hw_net_online_status()) {
ip_dns = hw_net_dnsserver();
ip_dhcp = hw_net_dhcpserver();
ip_gw = hw_net_defaultgateway();
connected = 1;
hw_net_ssid((char*) ssid);
TM_DEBUG("DNS: %ld.%ld.%ld.%ld", TM_BYTE(ip_dns, 3), TM_BYTE(ip_dns, 2), TM_BYTE(ip_dns, 1), TM_BYTE(ip_dns, 0));
TM_DEBUG("DHCP server: %ld.%ld.%ld.%ld", TM_BYTE(ip_dhcp, 3), TM_BYTE(ip_dhcp, 2), TM_BYTE(ip_dhcp, 1), TM_BYTE(ip_dhcp, 0));
TM_DEBUG("Default Gateway: %ld.%ld.%ld.%ld", TM_BYTE(ip_gw, 3), TM_BYTE(ip_gw, 2), TM_BYTE(ip_gw, 1), TM_BYTE(ip_gw, 0));
TM_DEBUG("Connected to WiFi!");
} else {
TM_DEBUG("Not yet connected to wifi");
}
char* payload = 0;
asprintf(&payload, "{\"event\": \"status\""
"," "\"connected\": %d"
"," "\"ip\": \"%ld.%ld.%ld.%ld\""
"," "\"dns\": \"%ld.%ld.%ld.%ld\""
"," "\"dhcp\": \"%ld.%ld.%ld.%ld\""
"," "\"gateway\": \"%ld.%ld.%ld.%ld\""
"," "\"ssid\": \"%s\""
"}",
connected,
TM_BYTE(ip_addr, 3), TM_BYTE(ip_addr, 2), TM_BYTE(ip_addr, 1), TM_BYTE(ip_addr, 0),
TM_BYTE(ip_dns, 3), TM_BYTE(ip_dns, 2), TM_BYTE(ip_dns, 1), TM_BYTE(ip_dns, 0),
TM_BYTE(ip_dhcp, 3), TM_BYTE(ip_dhcp, 2), TM_BYTE(ip_dhcp, 1), TM_BYTE(ip_dhcp, 0),
TM_BYTE(ip_gw, 3), TM_BYTE(ip_gw, 2), TM_BYTE(ip_gw, 1), TM_BYTE(ip_gw, 0),
ssid
);
return payload;
}
void tessel_wifi_check (uint8_t asevent)
{
(void) asevent;
if (hw_net_online_status()){
char * payload = tessel_wifi_json();
TM_COMMAND('W', payload);
if (wifi_status.post_connect) {
wifi_status.callback_err = 0;
wifi_status.callback_payload = payload;
wifi_status.post_connect = false;
// callback
tm_event_trigger(&wifi_connect_event);
} else {
free(payload);
}
} else {
TM_COMMAND('W', "{\"event\": \"status\""
"," "\"connected\": 0"
"}");
if (wifi_status.post_connect) {
wifi_status.callback_err = 1;
wifi_status.callback_payload = NULL;
wifi_status.post_connect = false;
tm_event_trigger(&wifi_connect_event);
}
}
}
int tessel_wifi_connect(char * wifi_security, char * wifi_ssid, size_t ssidlen, char* wifi_pass, size_t passlen)
{
// Check arguments.
if (wifi_ssid[0] == 0) {
return 1;
}
// if (hw_net_online_status()){
// TM_DEBUG("Disconnecting from current network.");
// TM_COMMAND('W', "{\"connected\": 0}");
// tessel_wifi_disconnect();
// }
TM_DEBUG("Starting up wifi (already initialized: %d)", wifi_initialized);
// if (!wifi_initialized) {
// tessel_wifi_enable();
// }
// Connect to given network.
hw_net_connect(wifi_security, wifi_ssid, ssidlen, wifi_pass, passlen); // use this for using tessel wifi from command line
wifi_is_connecting = 1;
return 0;
}
// tries to connect straight away
void tessel_wifi_fastconnect() {
TM_DEBUG("Connecting to last available network...");
hw_net_initialize();
wifi_initialized = 1;
wifi_is_connecting = 1;
cc_bootup = 0;
}