summaryrefslogtreecommitdiff
path: root/fw/fe310/test/wifi.c
blob: 86f6b627e6f088aa4f293ca93788837eb3d99be3 (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <eos.h>
#include <event.h>
#include <spi.h>
#include <uart.h>
#include <net.h>
#include <wifi.h>

#include <unicode.h>

#include <eve/eve.h>
#include <eve/eve_kbd.h>

#include <eve/screen/screen.h>
#include <eve/screen/window.h>
#include <eve/screen/view.h>
#include <eve/screen/page.h>
#include <eve/screen/form.h>

#include <eve/widget/widgets.h>

#include <app/root.h>

#include "status.h"
#include "wifi.h"

extern EVEFont *_app_font_default;

static void wifi_scan(void) {
    unsigned char *buffer = eos_net_alloc();
    buffer[0] = EOS_WIFI_MTYPE_SCAN;
    eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, 0);
}

static void wifi_connect(const char *ssid, const char *pass) {
    unsigned char *buffer, *p;

    buffer = eos_net_alloc();
    buffer[0] = EOS_WIFI_MTYPE_CONFIG;
    p = buffer + 1;
    strcpy(p, ssid);
    p += strlen(ssid) + 1;
    strcpy(p, pass);
    p += strlen(pass) + 1;
    eos_net_send(EOS_NET_MTYPE_WIFI, buffer, p - buffer, 1);

    buffer = eos_net_alloc();
    buffer[0] = EOS_WIFI_MTYPE_CONNECT;
    eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, 0);
}

static void wifi_disconnect(void) {
    unsigned char *buffer = eos_net_alloc();
    buffer[0] = EOS_WIFI_MTYPE_DISCONNECT;
    eos_net_send(EOS_NET_MTYPE_WIFI, buffer, 1, 0);
}

void wifi_scan_handler(unsigned char type, unsigned char *buffer, uint16_t size) {
    EVEScreen *screen = app_screen();
    EVEWindow *window = eve_window_get(screen, "main");
    EVEForm *form = (EVEForm *)window->view;
    EVESelectWidget *select = (EVESelectWidget *)eve_form_widget(form, 0);

    eve_selectw_option_set(select, buffer + 1, size - 1);
    eos_net_free(buffer, 0);

    eos_spi_dev_start(EOS_DEV_DISP);
    eve_screen_draw(app_screen());
    eos_spi_dev_stop();
}

static void wifi_connect_handler(unsigned char type, unsigned char *buffer, uint16_t size) {
    app_status_msg_set("WiFi connected", 1);
    eos_net_free(buffer, 0);
}

static void wifi_disconnect_handler(unsigned char type, unsigned char *buffer, uint16_t size) {
    app_status_msg_set("WiFi disconnected", 1);
    eos_net_free(buffer, 0);
}

void app_wifi(EVEWindow *window, EVEViewStack *stack) {
    char *title = "Password:";
    uint16_t w = eve_font_str_w(_app_font_default, title) + 10;
    APPWidgetSpec spec[] = {
        {
            .label.g.w = APP_SCREEN_W,
            .label.font = _app_font_default,
            .label.title = "Select network:",

            .widget.type = EVE_WIDGET_TYPE_SELECT,
            .widget.g.w = APP_SCREEN_W,
            .widget.spec.select.font = _app_font_default,
            .widget.spec.select.option_size = 1500,
        },
        {
            .widget.type = EVE_WIDGET_TYPE_SPACER,
            .widget.g.w = APP_SCREEN_W,
            .widget.g.h = 50,
        },
        {
            .label.g.w = w,
            .label.font = _app_font_default,
            .label.title = title,

            .widget.type = EVE_WIDGET_TYPE_STR,
            .widget.g.w = APP_SCREEN_W - w,
            .widget.spec.str.font = _app_font_default,
            .widget.spec.str.str_size = 128,
        },
    };

    EVEForm *form = app_form_create(window, stack, spec, 3, app_wifi_action, app_wifi_close);
    wifi_scan();
}

void app_wifi_action(EVEForm *form) {
    EVESelectWidget *sel = (EVESelectWidget *)eve_form_widget(form, 0);
    EVEStrWidget *str = (EVEStrWidget *)eve_form_widget(form, 2);
    char *ssid = eve_selectw_option_get_select(sel);

    if (ssid) wifi_connect(ssid, str->str);
}

void app_wifi_close(EVEForm *form) {
    app_form_destroy(form);
}

void app_wifi_init(void) {
    eos_wifi_set_handler(EOS_WIFI_MTYPE_SCAN, wifi_scan_handler);
    eos_wifi_set_handler(EOS_WIFI_MTYPE_CONNECT, wifi_connect_handler);
    eos_wifi_set_handler(EOS_WIFI_MTYPE_DISCONNECT, wifi_disconnect_handler);
}