summaryrefslogtreecommitdiff
path: root/fw/esp32/components/eos/wifi.c
blob: 537aebaa2e1c2da051525cc9c61e49a091d3052a (plain)
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
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include <freertos/FreeRTOS.h>
#include <freertos/task.h>

#include <esp_system.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_err.h>
#include <esp_wifi.h>
#include <nvs_flash.h>

#include "eos.h"
#include "net.h"
#include "wifi.h"

// XXX: WiFi fail due to no DHCP server

#define WIFI_MAX_CONNECT_ATTEMPTS   3

static const char *TAG = "EOS WIFI";

static esp_netif_t *wifi_netif;

static void wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
    static int reconnect_cnt = 0;
    static int stop = 0;

    if (event_base == WIFI_EVENT) {
        switch (event_id) {
            case WIFI_EVENT_SCAN_DONE: {
                ESP_LOGI(TAG, "Event scan done");
                eos_wifi_send_scan();
                break;
            }

            case WIFI_EVENT_STA_START:
            case WIFI_EVENT_STA_STOP:
            case WIFI_EVENT_STA_CONNECTED: {
                ESP_LOGI(TAG, "Event start/stop/conneced");
                stop = 0;
                reconnect_cnt = 0;
                eos_wifi_send_status();
                break;
            }

            case WIFI_EVENT_STA_DISCONNECTED: {
                wifi_event_sta_disconnected_t *sta_disconnected;

                sta_disconnected = (wifi_event_sta_disconnected_t *)event_data;
                ESP_LOGI(TAG, "Event disconnected - reason: %d", sta_disconnected->reason);

                if (sta_disconnected->reason == WIFI_REASON_ASSOC_LEAVE) {
                    stop = 0;
                    reconnect_cnt = 0;
                    eos_wifi_send_status();
                    break;
                }
                if ((reconnect_cnt == 0) && (sta_disconnected->reason == WIFI_REASON_BEACON_TIMEOUT)) {
                    stop = 1;
                }
                if (reconnect_cnt < WIFI_MAX_CONNECT_ATTEMPTS) {
                    esp_wifi_connect();

                    reconnect_cnt++;
                } else {
                    eos_wifi_send_status();

                    reconnect_cnt = 0;
                    if (stop) {
                        esp_wifi_stop();
                        stop = 0;
                    }
                }
                break;
            }

            default:    // Ignore the other event types
                break;
        }
    } else if (event_base == IP_EVENT) {
        switch (event_id) {
            case IP_EVENT_STA_GOT_IP: {
                ip_event_got_ip_t *got_ip;

                got_ip = (ip_event_got_ip_t *)event_data;
                ESP_LOGI(TAG, "Event got IP - addres: " IPSTR, IP2STR(&got_ip->ip_info.ip));
                eos_wifi_send_status();

                /*
                if (got_ip->ip_changed) {
                    recreate all sockets
                }
                */
                break;
            }

            default:    // Ignore the other event types
                break;
        }
    }

}

static void wifi_handler(unsigned char _mtype, unsigned char *buffer, uint16_t buf_len) {
    esp_err_t ret = ESP_OK;
    uint8_t mtype;

    if (buf_len < 1) return;

    mtype = buffer[0];
    switch (mtype) {
        case EOS_WIFI_MTYPE_STATUS: {
            int reply;
            ssize_t rv;

            reply = _mtype & EOS_NET_MTYPE_FLAG_REPL;

            if (reply) {
                rv = eos_wifi_get_status(buffer + 1);
                if (rv < 0) break;

                eos_net_reply(EOS_NET_MTYPE_WIFI, buffer, rv + 1);
            } else {
                unsigned char *buf;

                buf = eos_net_alloc();
                buf[0] = EOS_WIFI_MTYPE_STATUS;
                rv = eos_wifi_get_status(buf + 1);
                if (rv < 0) break;

                eos_net_send(EOS_NET_MTYPE_WIFI, buf, rv + 1);
            }
            break;
        }

        case EOS_WIFI_MTYPE_START: {
            ret = esp_wifi_start();
            break;
        }

        case EOS_WIFI_MTYPE_STOP: {
            ret = esp_wifi_stop();
            break;
        }

        case EOS_WIFI_MTYPE_SCAN: {
            ret = esp_wifi_scan_start(NULL, 0);
            break;
        }

        case EOS_WIFI_MTYPE_CONNECT: {
            wifi_config_t wifi_sta_config;
            size_t ssid_size = sizeof(wifi_sta_config.sta.ssid);
            size_t pwd_size = sizeof(wifi_sta_config.sta.password) - 1;
            size_t ssid_len, pwd_len;
            char *ssid, *pwd;

            buffer++;
            buf_len--;

            ssid = (char *)buffer;
            ssid_len = strnlen(ssid, buf_len);
            if (ssid_len == buf_len) break;
            if (ssid_len > ssid_size) break;
            buffer += ssid_len + 1;
            buf_len -= ssid_len + 1;

            pwd = (char *)buffer;
            pwd_len = strnlen(pwd, buf_len);
            if (pwd_len == buf_len) break;
            if (pwd_len >= pwd_size) break;
            buffer += pwd_len + 1;
            buf_len -= pwd_len + 1;

            memset(&wifi_sta_config, 0, sizeof(wifi_sta_config));
            if (ssid_len < ssid_size) {
                strcpy((char *)wifi_sta_config.sta.ssid, ssid);
            } else {
                memcpy(wifi_sta_config.sta.ssid, ssid, ssid_size);
            }
            strcpy((char *)wifi_sta_config.sta.password, pwd);
            wifi_sta_config.sta.password[pwd_size] = '\0';

            ret = esp_wifi_set_config(WIFI_IF_STA, &wifi_sta_config);
            if (ret) break;

            ret = esp_wifi_connect();
            break;
        }

        case EOS_WIFI_MTYPE_DISCONNECT: {
            ret = esp_wifi_disconnect();
            break;
        }
    }

    if (ret) {
        ESP_LOGE(TAG, "HANDLER ERR:%d MTYPE:%d", ret, mtype);
        eos_wifi_send_status();
    }
}

void eos_wifi_init(void) {
    esp_err_t ret;
    wifi_init_config_t wifi_config = WIFI_INIT_CONFIG_DEFAULT();

    wifi_netif = esp_netif_create_default_wifi_sta();

    ret = esp_wifi_init(&wifi_config);
    assert(ret == ESP_OK);

    ret = esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL);
    assert(ret == ESP_OK);

    ret = esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_event_handler, NULL, NULL);
    assert(ret == ESP_OK);

    ret = esp_wifi_set_storage(WIFI_STORAGE_RAM);
    assert(ret == ESP_OK);

    ret = esp_wifi_set_mode(WIFI_MODE_STA);
    assert(ret == ESP_OK);

    ret = esp_wifi_stop();
    assert(ret == ESP_OK);

    eos_net_set_handler(EOS_NET_MTYPE_WIFI, wifi_handler);
    ESP_LOGI(TAG, "INIT");
}

ssize_t eos_wifi_get_status(unsigned char *buffer) {
    unsigned char *p;
    wifi_ap_record_t ap_info;
    esp_err_t ret;

    p = buffer;
    ret = esp_wifi_sta_get_ap_info(&ap_info);
    switch (ret) {
        case ESP_OK: {  // Station connected
            size_t len;
            esp_netif_ip_info_t ip_info;

            len = strnlen((char *)ap_info.ssid, sizeof(ap_info.ssid));
            if (len == sizeof(ap_info.ssid)) {
                ESP_LOGE(TAG, "STATUS BAD SSID");
                return EOS_ERR;
            }
            ret = esp_netif_get_ip_info(wifi_netif, &ip_info);
            if (ret) {
                ESP_LOGE(TAG, "STATUS GET IP INFO ERR:%d", ret);
                return EOS_ERR;
            }

            if (ip_info.ip.addr) {
                *p = EOS_WIFI_STATUS_GOT_IP;
                p++;

                memcpy(p, &ip_info.ip.addr, sizeof(uint32_t));
                p += sizeof(uint32_t);
            } else {
                *p = EOS_WIFI_STATUS_CONNECTED;
                p++;
            }

            strcpy((char *)p, (char *)ap_info.ssid);
            p += len + 1;

            break;
        }

        case ESP_ERR_WIFI_NOT_CONNECT: {
            *p = EOS_WIFI_STATUS_DISCONNECTED;
            p++;
            break;
        }

        case ESP_ERR_WIFI_STOP_STATE:
        case ESP_ERR_WIFI_CONN: {
            *p = EOS_WIFI_STATUS_OFF;
            p++;
            break;
        }

        default: {
            ESP_LOGE(TAG, "STATUS GET AP INFO ERR:%d", ret);
            return EOS_ERR;
        }
    }

    return p - buffer;
}

void eos_wifi_send_status(void) {
    unsigned char *rbuf;
    ssize_t rv;

    rbuf = eos_net_alloc();
    rbuf[0] = EOS_WIFI_MTYPE_STATUS;
    rv = eos_wifi_get_status(rbuf + 1);
    if (rv < 0) {
        eos_net_free(rbuf);
        return;
    }

    eos_net_send(EOS_NET_MTYPE_WIFI, rbuf, rv + 1);
}

void eos_wifi_send_scan(void) {
    static wifi_ap_record_t scan_r[EOS_WIFI_MAX_SCAN_RECORDS];
    static uint16_t scan_n;
    unsigned char *rbuf, *p;
    int i;
    size_t len;
    esp_err_t ret;

    scan_n = EOS_WIFI_MAX_SCAN_RECORDS;
    memset(scan_r, 0, sizeof(scan_r));
    ret = esp_wifi_scan_get_ap_records(&scan_n, scan_r);
    if (ret) {
        ESP_LOGE(TAG, "SCAN GET RECORDS ERR:%d", ret);
        return;
    }

    ESP_LOGI(TAG, "Scan done: %d", scan_n);

    rbuf = eos_net_alloc();
    rbuf[0] = EOS_WIFI_MTYPE_SCAN;
    p = rbuf + 1;

    for (i=0; i<scan_n; i++) {
        len = strnlen((char *)scan_r[i].ssid, sizeof(scan_r[i].ssid));
        if (len == sizeof(scan_r[i].ssid)) continue;
        if (p - rbuf + len + 1 > EOS_NET_MTU) break;

        strcpy((char *)p, (char *)scan_r[i].ssid);
        p += len + 1;
    }
    eos_net_send(EOS_NET_MTYPE_WIFI, rbuf, p - rbuf);
}