Welcome to mirror list, hosted at ThFree Co, Russian Federation.

hid_service.c « ble_glue « f7 « targets « firmware - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec8854e6652783fee0ac5c867224123026a653a5 (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
#include "hid_service.h"
#include "app_common.h"
#include "ble.h"

#include <furi.h>

#define TAG "BtHid"

typedef struct {
    uint16_t svc_handle;
    uint16_t protocol_mode_char_handle;
    uint16_t report_char_handle;
    uint16_t report_ref_desc_handle;
    uint16_t report_map_char_handle;
    uint16_t keyboard_boot_char_handle;
    uint16_t info_char_handle;
    uint16_t ctrl_point_char_handle;
} HIDSvc;

static HIDSvc* hid_svc = NULL;

static SVCCTL_EvtAckStatus_t hid_svc_event_handler(void* event) {
    SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
    hci_event_pckt* event_pckt = (hci_event_pckt*)(((hci_uart_pckt*)event)->data);
    evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
    // aci_gatt_attribute_modified_event_rp0* attribute_modified;
    if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
        if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
            // Process modification events
            ret = SVCCTL_EvtAckFlowEnable;
        } else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
            // Process notification confirmation
            ret = SVCCTL_EvtAckFlowEnable;
        }
    }
    return ret;
}

void hid_svc_start() {
    tBleStatus status;
    hid_svc = furi_alloc(sizeof(HIDSvc));
    Service_UUID_t svc_uuid = {};
    Char_Desc_Uuid_t desc_uuid = {};
    Char_UUID_t char_uuid = {};

    // Register event handler
    SVCCTL_RegisterSvcHandler(hid_svc_event_handler);
    // Add service
    svc_uuid.Service_UUID_16 = HUMAN_INTERFACE_DEVICE_SERVICE_UUID;
    status =
        aci_gatt_add_service(UUID_TYPE_16, &svc_uuid, PRIMARY_SERVICE, 30, &hid_svc->svc_handle);
    if(status) {
        FURI_LOG_E(TAG, "Failed to add HID service: %d", status);
    }
    // Add Protocol mode characterstics
    char_uuid.Char_UUID_16 = PROTOCOL_MODE_CHAR_UUID;
    status = aci_gatt_add_char(
        hid_svc->svc_handle,
        UUID_TYPE_16,
        &char_uuid,
        1,
        CHAR_PROP_READ | CHAR_PROP_WRITE_WITHOUT_RESP,
        ATTR_PERMISSION_NONE,
        GATT_NOTIFY_ATTRIBUTE_WRITE,
        10,
        CHAR_VALUE_LEN_CONSTANT,
        &hid_svc->protocol_mode_char_handle);
    if(status) {
        FURI_LOG_E(TAG, "Failed to add protocol mode characteristic: %d", status);
    }
    // Update Protocol mode characteristic
    uint8_t protocol_mode = 1;
    status = aci_gatt_update_char_value(
        hid_svc->svc_handle, hid_svc->protocol_mode_char_handle, 0, 1, &protocol_mode);
    if(status) {
        FURI_LOG_E(TAG, "Failed to update protocol mode characteristic: %d", status);
    }
    // Add Report characterstics
    char_uuid.Char_UUID_16 = REPORT_CHAR_UUID;
    status = aci_gatt_add_char(
        hid_svc->svc_handle,
        UUID_TYPE_16,
        &char_uuid,
        HID_SVC_REPORT_MAX_LEN,
        CHAR_PROP_READ | CHAR_PROP_NOTIFY,
        ATTR_PERMISSION_NONE,
        GATT_DONT_NOTIFY_EVENTS,
        10,
        CHAR_VALUE_LEN_VARIABLE,
        &hid_svc->report_char_handle);
    if(status) {
        FURI_LOG_E(TAG, "Failed to add report characteristic: %d", status);
    }
    // Add Report descriptor
    uint8_t desc_val[] = {0x00, 0x01};
    desc_uuid.Char_UUID_16 = REPORT_REFERENCE_DESCRIPTOR_UUID;
    status = aci_gatt_add_char_desc(
        hid_svc->svc_handle,
        hid_svc->report_char_handle,
        UUID_TYPE_16,
        &desc_uuid,
        HID_SVC_REPORT_REF_LEN,
        HID_SVC_REPORT_REF_LEN,
        desc_val,
        ATTR_PERMISSION_NONE,
        ATTR_ACCESS_READ_ONLY,
        GATT_DONT_NOTIFY_EVENTS,
        MIN_ENCRY_KEY_SIZE,
        CHAR_VALUE_LEN_CONSTANT,
        &hid_svc->report_ref_desc_handle);
    if(status) {
        FURI_LOG_E(TAG, "Failed to add report reference descriptor: %d", status);
    }
    // Add Report Map characteristic
    char_uuid.Char_UUID_16 = REPORT_MAP_CHAR_UUID;
    status = aci_gatt_add_char(
        hid_svc->svc_handle,
        UUID_TYPE_16,
        &char_uuid,
        HID_SVC_REPORT_MAP_MAX_LEN,
        CHAR_PROP_READ,
        ATTR_PERMISSION_NONE,
        GATT_DONT_NOTIFY_EVENTS,
        10,
        CHAR_VALUE_LEN_VARIABLE,
        &hid_svc->report_map_char_handle);
    if(status) {
        FURI_LOG_E(TAG, "Failed to add report map characteristic: %d", status);
    }
    // Add Boot Keyboard characteristic
    char_uuid.Char_UUID_16 = BOOT_KEYBOARD_INPUT_REPORT_CHAR_UUID;
    status = aci_gatt_add_char(
        hid_svc->svc_handle,
        UUID_TYPE_16,
        &char_uuid,
        HID_SVC_BOOT_KEYBOARD_INPUT_REPORT_MAX_LEN,
        CHAR_PROP_READ | CHAR_PROP_NOTIFY,
        ATTR_PERMISSION_NONE,
        GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP,
        10,
        CHAR_VALUE_LEN_VARIABLE,
        &hid_svc->keyboard_boot_char_handle);
    if(status) {
        FURI_LOG_E(TAG, "Failed to add report map characteristic: %d", status);
    }
    // Add Information characteristic
    char_uuid.Char_UUID_16 = HID_INFORMATION_CHAR_UUID;
    status = aci_gatt_add_char(
        hid_svc->svc_handle,
        UUID_TYPE_16,
        &char_uuid,
        HID_SVC_INFO_LEN,
        CHAR_PROP_READ,
        ATTR_PERMISSION_NONE,
        GATT_DONT_NOTIFY_EVENTS,
        10,
        CHAR_VALUE_LEN_CONSTANT,
        &hid_svc->info_char_handle);
    if(status) {
        FURI_LOG_E(TAG, "Failed to add information characteristic: %d", status);
    }
    // Add Control Point characteristic
    char_uuid.Char_UUID_16 = HID_CONTROL_POINT_CHAR_UUID;
    status = aci_gatt_add_char(
        hid_svc->svc_handle,
        UUID_TYPE_16,
        &char_uuid,
        HID_SVC_CONTROL_POINT_LEN,
        CHAR_PROP_WRITE_WITHOUT_RESP,
        ATTR_PERMISSION_NONE,
        GATT_NOTIFY_ATTRIBUTE_WRITE,
        10,
        CHAR_VALUE_LEN_CONSTANT,
        &hid_svc->ctrl_point_char_handle);
    if(status) {
        FURI_LOG_E(TAG, "Failed to add control point characteristic: %d", status);
    }
}

bool hid_svc_update_report_map(uint8_t* data, uint16_t len) {
    furi_assert(data);
    furi_assert(hid_svc);

    tBleStatus status = aci_gatt_update_char_value(
        hid_svc->svc_handle, hid_svc->report_map_char_handle, 0, len, data);
    if(status) {
        FURI_LOG_E(TAG, "Failed updating report map characteristic");
        return false;
    }
    return true;
}

bool hid_svc_update_input_report(uint8_t* data, uint16_t len) {
    furi_assert(data);
    furi_assert(hid_svc);

    tBleStatus status =
        aci_gatt_update_char_value(hid_svc->svc_handle, hid_svc->report_char_handle, 0, len, data);
    if(status) {
        FURI_LOG_E(TAG, "Failed updating report characteristic");
        return false;
    }
    return true;
}

bool hid_svc_update_info(uint8_t* data, uint16_t len) {
    furi_assert(data);
    furi_assert(hid_svc);

    tBleStatus status =
        aci_gatt_update_char_value(hid_svc->svc_handle, hid_svc->info_char_handle, 0, len, data);
    if(status) {
        FURI_LOG_E(TAG, "Failed updating info characteristic");
        return false;
    }
    return true;
}

bool hid_svc_is_started() {
    return hid_svc != NULL;
}

void hid_svc_stop() {
    tBleStatus status;
    if(hid_svc) {
        // Delete characteristics
        status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->report_map_char_handle);
        if(status) {
            FURI_LOG_E(TAG, "Failed to delete Report Map characteristic: %d", status);
        }
        status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->report_char_handle);
        if(status) {
            FURI_LOG_E(TAG, "Failed to delete Report characteristic: %d", status);
        }
        status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->protocol_mode_char_handle);
        if(status) {
            FURI_LOG_E(TAG, "Failed to delete Protocol Mode characteristic: %d", status);
        }
        status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->keyboard_boot_char_handle);
        if(status) {
            FURI_LOG_E(TAG, "Failed to delete Keyboard Boot characteristic: %d", status);
        }
        status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->info_char_handle);
        if(status) {
            FURI_LOG_E(TAG, "Failed to delete Information characteristic: %d", status);
        }
        status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->ctrl_point_char_handle);
        if(status) {
            FURI_LOG_E(TAG, "Failed to delete Control Point characteristic: %d", status);
        }
        // Delete service
        status = aci_gatt_del_service(hid_svc->svc_handle);
        if(status) {
            FURI_LOG_E(TAG, "Failed to delete HID service: %d", status);
        }
        // Delete buffer size mutex
        free(hid_svc);
        hid_svc = NULL;
    }
}