summaryrefslogtreecommitdiff
path: root/code/esp32/components/eos/wifi.c
blob: 628c4fd9b271e763aa536f205957d3bca3b648b8 (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
#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_event_loop.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"

static const char *TAG = "EOS";

static esp_err_t wifi_event_handler(void *ctx, system_event_t *event) {
    switch(event->event_id) {
        case SYSTEM_EVENT_SCAN_DONE:
            break;

        case SYSTEM_EVENT_STA_START:
            break;

        case SYSTEM_EVENT_STA_STOP:
            break;

        case SYSTEM_EVENT_STA_CONNECTED:
            break;

        case SYSTEM_EVENT_STA_DISCONNECTED:
            ESP_LOGI(TAG, "DISCONNECTED");
            break;

        case SYSTEM_EVENT_STA_GOT_IP:
            ESP_LOGI(TAG, "********************************************");
            ESP_LOGI(TAG, "* We are now connected to AP");
            ESP_LOGI(TAG, "* - Our IP address is: " IPSTR, IP2STR(&event->event_info.got_ip.ip_info.ip));
            ESP_LOGI(TAG, "********************************************");
            unsigned char *buf = malloc(32);
            buf[0] = EOS_WIFI_MTYPE_CONNECT;
            eos_net_send(EOS_NET_MTYPE_WIFI, buf, 1, EOS_NET_FLAG_BUF_FREE);
            break;

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

    return ESP_OK;
}

static void wifi_handler(unsigned char _mtype, unsigned char *buffer, uint16_t size) {
    uint8_t mtype = buffer[0];
    char *ssid, *pass;

    switch (mtype) {
        case EOS_WIFI_MTYPE_SCAN:
            break;
        case EOS_WIFI_MTYPE_CONNECT:
            ssid = (char *)buffer+1;
            pass = ssid+strlen(ssid)+1;
            eos_wifi_connect(ssid, pass);
            break;
        case EOS_WIFI_MTYPE_DISCONNECT:
            eos_wifi_disconnect();
            break;
    }
    // eos_wifi_connect((char *)buffer, (char *)(buffer+strlen((char *)buffer)+1));
}

void eos_wifi_init(void) {
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    wifi_config_t wifi_config;

    memset(&wifi_config, 0, sizeof(wifi_config));
    tcpip_adapter_init();
    // ESP_ERROR_CHECK( nvs_flash_init() );
    ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) );
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
    ESP_ERROR_CHECK( esp_wifi_start() );
    eos_net_set_handler(EOS_NET_MTYPE_WIFI, wifi_handler);
}

void eos_wifi_connect(char *ssid, char *pass) {
    wifi_config_t wifi_config;

    memset(&wifi_config, 0, sizeof(wifi_config));
    strncpy((char *)wifi_config.sta.ssid, ssid, 31);
    strncpy((char *)wifi_config.sta.password, pass, 63);
    ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
    ESP_ERROR_CHECK( esp_wifi_connect() );
}

void eos_wifi_disconnect(void) {
    ESP_ERROR_CHECK( esp_wifi_disconnect() );
}