summaryrefslogtreecommitdiff
path: root/tmk_core/common/webusb.c
blob: 2488e75de718b4567444ff98e9bd52946002645b (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
#include QMK_KEYBOARD_H
#include <string.h>
#include "webusb.h"
#include "wait.h"
webusb_state_t webusb_state = {
  .paired = false,
  .pairing = false,
};

void webusb_receive(uint8_t *data, uint8_t length) {
    uint8_t command = data[0];

    if(command == WEBUSB_CMD_PAIR && webusb_state.pairing == true) {
        uint8_t event[3];
        webusb_state.pairing = false;
        webusb_state.paired = true;
        event[0] = WEBUSB_STATUS_OK;
        event[1] = WEBUSB_EVT_PAIRED;
        event[2] = WEBUSB_STOP_BIT;
        webusb_send(event, sizeof(event));
        return;
    }

    if(command == WEBUSB_GET_FW_VERSION) {
        // Landing page + packet headers(2) + stop bit(1)
        uint8_t lp_size = sizeof(FIRMWARE_VERSION) + 3;
        uint8_t url[lp_size];

        uint8_t event[2];
        event[0] = WEBUSB_STATUS_OK;
        event[1] = WEBUSB_EVT_FW_VERSION;

        uint8_t stop[1];
        stop[0] = WEBUSB_STOP_BIT;

        memcpy(url, event, 2);
        memcpy(url + 2, FIRMWARE_VERSION, sizeof(FIRMWARE_VERSION));
        memcpy(url + 2 + sizeof(FIRMWARE_VERSION), stop, 1);
        webusb_send(url, lp_size);
        return;
    }

    if(webusb_state.paired == true) {
        switch(command) {
            //Handle commands in here
            case WEBUSB_GET_LAYER:
                webusb_layer_event();
                break;
            default:
                break;
        }
    } else {
        webusb_error(WEBUSB_STATUS_NOT_PAIRED);
    }
};

void webusb_layer_event() {
    uint8_t layer;
    uint8_t event[4];
    layer = biton32(layer_state);
    event[0] = WEBUSB_STATUS_OK;
    event[1] = WEBUSB_EVT_LAYER;
    event[2] = layer;
    event[3] = WEBUSB_STOP_BIT;
    webusb_send(event, sizeof(event));
}

void webusb_error(uint8_t code) {
    uint8_t buffer[1];
    buffer[0] = code;
    webusb_send(buffer, 1);
}