/
esp_wifi.c
276 lines (219 loc) · 6.75 KB
/
esp_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
/**
* @file esp_wifi.c
*
* @section License
* Copyright (C) 2014-2016, Erik Moqvist
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* This file is part of the Simba project.
*/
#include "simba.h"
#include "espressif/esp_wifi.h"
struct module_t {
int initialized;
#if CONFIG_FS_CMD_ESP_WIFI_STATUS == 1
struct fs_command_t cmd_status;
#endif
};
struct module_t module;
#if CONFIG_FS_CMD_ESP_WIFI_STATUS == 1
static int cmd_status_cb(int argc,
const char *argv[],
void *out_p,
void *in_p,
void *arg_p,
void *call_arg_p)
{
esp_wifi_print(out_p);
return (0);
}
#endif
int esp_wifi_module_init(void)
{
/* Return immediately if the module is already initialized. */
if (module.initialized == 1) {
return (0);
}
module.initialized = 1;
#if CONFIG_FS_CMD_ESP_WIFI_STATUS == 1
fs_command_init(&module.cmd_status,
FSTR("/drivers/esp_wifi/status"),
cmd_status_cb,
NULL);
fs_command_register(&module.cmd_status);
#endif
return (0);
}
int esp_wifi_set_op_mode(enum esp_wifi_op_mode_t mode)
{
return (!wifi_set_opmode_current(mode));
}
enum esp_wifi_op_mode_t esp_wifi_get_op_mode()
{
return (wifi_get_opmode());
}
int esp_wifi_set_phy_mode(enum esp_wifi_phy_mode_t mode)
{
return (!wifi_set_phy_mode(mode));
}
enum esp_wifi_phy_mode_t esp_wifi_get_phy_mode()
{
return (wifi_get_phy_mode());
}
void esp_wifi_print(void *chout_p)
{
ASSERTN(chout_p != NULL, -EINVAL);
int i;
int number_of_infos;
struct esp_wifi_softap_station_info_t info[4];
struct inet_if_ip_info_t ip_info;
char buf[16];
char *op_mode_p;
char *phy_mode_p;
char *connection_status_p;
char *dhcp_status_p;
/* Get the operating mode. */
switch (esp_wifi_get_op_mode()) {
case esp_wifi_op_mode_null_t:
op_mode_p = "NULL";
break;
case esp_wifi_op_mode_station_t:
op_mode_p = "STA";
break;
case esp_wifi_op_mode_softap_t:
op_mode_p = "AP";
break;
case esp_wifi_op_mode_station_softap_t:
op_mode_p = "STA-AP";
break;
default:
op_mode_p = "INVALID";
break;
}
/* Get the physical mode. */
switch (esp_wifi_get_phy_mode()) {
case esp_wifi_phy_mode_11b_t:
phy_mode_p = "b";
break;
case esp_wifi_phy_mode_11g_t:
phy_mode_p = "g";
break;
case esp_wifi_phy_mode_11n_t:
phy_mode_p = "n";
break;
default:
phy_mode_p = "invalid";
break;
}
/* Get the DHCP server status. */
switch (esp_wifi_softap_dhcp_server_status()) {
case esp_wifi_dhcp_status_running_t:
dhcp_status_p = "running";
break;
case esp_wifi_dhcp_status_stopped_t:
dhcp_status_p = "stopped";
break;
default:
dhcp_status_p = "invalid";
break;
}
std_fprintf(chout_p,
FSTR("General information:\r\n"
" Operating mode: %s\r\n"
" Physical mode: 11%s\r\n"
"\r\n"),
op_mode_p,
phy_mode_p);
esp_wifi_softap_get_ip_info(&ip_info);
number_of_infos = esp_wifi_softap_get_station_info(info,
membersof(info));
std_fprintf(chout_p,
FSTR("SoftAP:\r\n"
" DHCP server status: %s\r\n"
" Address: %s\r\n"),
dhcp_status_p,
inet_ntoa(&ip_info.address, &buf[0]));
std_fprintf(chout_p,
FSTR(" Netmask: %s\r\n"),
inet_ntoa(&ip_info.netmask, &buf[0]));
std_fprintf(chout_p,
FSTR(" Gateway: %s\r\n"
" Number of connections: %d\r\n"),
inet_ntoa(&ip_info.gateway, &buf[0]),
number_of_infos);
for (i = 0; i < number_of_infos; i++) {
std_fprintf(chout_p,
FSTR(" [%d]: BSSID: %02x:%02x:%02x:%02x:%02x:%02x,"
" IP: %s\r\n"),
i,
(int)info[i].bssid[0],
(int)info[i].bssid[1],
(int)info[i].bssid[2],
(int)info[i].bssid[3],
(int)info[i].bssid[4],
(int)info[i].bssid[5],
inet_ntoa(&info[i].ip_address, &buf[0]));
}
/* Get the station connection status. */
switch (esp_wifi_station_get_connect_status()) {
case esp_wifi_station_status_idle_t:
connection_status_p = "idle";
break;
case esp_wifi_station_status_connecting_t:
connection_status_p = "connecting";
break;
case esp_wifi_station_status_wrong_password_t:
connection_status_p = "wrong-password";
break;
case esp_wifi_station_status_no_ap_found_t:
connection_status_p = "no-ap";
break;
case esp_wifi_station_status_connect_fail_t:
connection_status_p = "connect-fail";
break;
case esp_wifi_station_status_got_ip_t:
connection_status_p = "got-ip";
break;
default:
connection_status_p = "invalid";
break;
}
/* Get the DHCP client status. */
switch (esp_wifi_station_dhcp_client_status()) {
case esp_wifi_dhcp_status_running_t:
dhcp_status_p = "running";
break;
case esp_wifi_dhcp_status_stopped_t:
dhcp_status_p = "stopped";
break;
default:
dhcp_status_p = "invalid";
break;
}
esp_wifi_station_get_ip_info(&ip_info);
std_fprintf(chout_p,
FSTR("\r\n"
"Station:\r\n"
" DHCP client status: %s\r\n"
" Conenction status: %s\r\n"
" Address: %s\r\n"),
dhcp_status_p,
connection_status_p,
inet_ntoa(&ip_info.address, &buf[0]));
std_fprintf(chout_p,
FSTR(" Netmask: %s\r\n"),
inet_ntoa(&ip_info.netmask, &buf[0]));
std_fprintf(chout_p,
FSTR(" Gateway: %s\r\n"
"\r\n"),
inet_ntoa(&ip_info.gateway, &buf[0]));
}