-
Notifications
You must be signed in to change notification settings - Fork 38
/
smartlink.c
150 lines (134 loc) · 3.67 KB
/
smartlink.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
#include "c_types.h"
#include "osapi.h"
#include "network_80211.h"
#include "user_interface.h"
#define SMARTLK_TOKEN "<!-SL-!>"
#define SMARTLK_TOKEN_LEN (strlen(SMARTLK_TOKEN))
void smartlink_wifi_promiscuous_rx(uint8_t*, uint16);
void smartlink_received(char*, char*);
void delay_ms(uint16);
void delay_s(uint16);
void (*smartlink_received_cb)(void*) = NULL;
void *smartlink_received_cb_args = NULL;
void (*smartlink_failed_cb)() = NULL;
uint8_t max_wait = 5;
void delay_s(uint16 ss)
{
uint16 i;
for(i = 0; i < ss; i++)
{
delay_ms(1000);
}
}
void delay_ms(uint16 sms)
{
uint16 i;
for(i = 0; i < sms; i++)
{
os_delay_us(1000);
}
}
void ICACHE_FLASH_ATTR
smartlink_init(void* cb, void* args, void* fail, uint8_t max)
{
os_printf("\n\n");
os_printf("[*] reset wifi config \n");
{
wifi_station_disconnect();
wifi_station_set_config(NULL);
}
os_printf("[*] set opmode=0x01\n");
wifi_set_opmode(0x01);
os_printf("[*] set smartlink_received callback function\n");
smartlink_received_cb = cb;
smartlink_received_cb_args = args;
smartlink_failed_cb = fail;
max_wait = max;
os_printf("[*] enable wifi promiscuous\n");
wifi_promiscuous_enable(1);
wifi_set_promiscuous_rx_cb(smartlink_wifi_promiscuous_rx);
}
void ICACHE_FLASH_ATTR
smartlink_wifi_promiscuous_rx(uint8_t *buf, uint16 len)
{
uint16 i;
uint8_t type;
// os_printf("smartlink_wifi_promiscuous_rx -----\n");
lpframectrl_80211 framectrl;
struct router_info *info = NULL;
if (len < 64) {
return;
}
struct sniffer_buf *sniffer = (struct sniffer_buf*)buf;
buf +=sizeof(struct RxControl);
struct probe_request_80211 *probe_buf = (struct probe_request_80211*)buf;
if (FRAME_TYPE_MANAGEMENT == probe_buf->framectrl.Type) {
/* Management frame */
if (FRAME_SUBTYPE_PROBE_REQUEST == probe_buf->framectrl.Subtype) {
/* Probe Request */
ptagged_parameter tag = (ptagged_parameter)(buf + sizeof(probe_request));
if (tag->tag_length != 0)
{
uint8_t ssid_buff[32];
os_memset(ssid_buff, 0, 32);
os_memcpy(ssid_buff, (uint8_t *)tag + 2, tag->tag_length);
char *pos = (char *)os_strstr(ssid_buff, SMARTLK_TOKEN);
if (pos != NULL)
{
uint8_t ssid[40];
uint8_t password[40];
os_memset(ssid, 0, 32);
os_memset(password, 0, 32);
/* ssid */
os_memcpy(ssid, ssid_buff, pos - (char *)ssid_buff);
/* password */
os_memcpy(password, pos + SMARTLK_TOKEN_LEN, tag->tag_length - SMARTLK_TOKEN_LEN - strlen(ssid));
smartlink_received(ssid, password);
}
}
}
}
}
void ICACHE_FLASH_ATTR
smartlink_received(char* ssid, char* password)
{
os_printf("[*] received SSID:%s PASSWORD:%s\n", ssid, password);
//wifi_promiscuous_enable(0);
os_printf("[*] change mode: STATION \n");
wifi_set_opmode(0x01);
{
struct station_config config;
os_strcpy(config.ssid, ssid);
os_strcpy(config.password, password);
wifi_station_set_config(&config);
}
system_restart();
#if 0
os_printf("[*] connecting to %s ... \n", ssid);
wifi_station_connect();
os_printf("[*] check connection to %s ... \n", ssid);
{
uint8_t status;
uint8_t retry;
for(retry = 0; retry < max_wait; ++retry)
{
status = wifi_station_get_connect_status();
if (STATION_IDLE == status || STATION_GOT_IP == status)
{
os_printf("[*] connected to %s", ssid);
if (smartlink_received_cb)
{
(*smartlink_received_cb)(smartlink_received_cb_args);
}
} else {
os_printf("[*] probing AP:%s\n", ssid);
//vTaskDelay(1000 / portTICK_RATE_MS);
}
}
}
if (smartlink_failed_cb)
{
(*smartlink_failed_cb)();
}
#endif
}