summaryrefslogtreecommitdiff
path: root/drivers/bluetooth
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2022-09-30 03:38:09 +1000
committerGitHub <noreply@github.com>2022-09-29 18:38:09 +0100
commitf80058d96e2f1606f648166d320cda15986ced4c (patch)
treedb60471363d59bb52d124a276bde9cf93e93c591 /drivers/bluetooth
parentcbbb45c13f3de5ec28f62b6cd4a8b77143026500 (diff)
Start Bluetooth API (#18366)
Diffstat (limited to 'drivers/bluetooth')
-rw-r--r--drivers/bluetooth/bluefruit_le.cpp58
-rw-r--r--drivers/bluetooth/bluefruit_le.h12
-rw-r--r--drivers/bluetooth/bluetooth.c62
-rw-r--r--drivers/bluetooth/bluetooth.h52
4 files changed, 140 insertions, 44 deletions
diff --git a/drivers/bluetooth/bluefruit_le.cpp b/drivers/bluetooth/bluefruit_le.cpp
index 50170b83fe..39c14ddd13 100644
--- a/drivers/bluetooth/bluefruit_le.cpp
+++ b/drivers/bluetooth/bluefruit_le.cpp
@@ -5,7 +5,7 @@
5#include <alloca.h> 5#include <alloca.h>
6#include "debug.h" 6#include "debug.h"
7#include "timer.h" 7#include "timer.h"
8#include "action_util.h" 8#include "gpio.h"
9#include "ringbuffer.hpp" 9#include "ringbuffer.hpp"
10#include <string.h> 10#include <string.h>
11#include "spi_master.h" 11#include "spi_master.h"
@@ -288,7 +288,7 @@ static void resp_buf_wait(const char *cmd) {
288 } 288 }
289} 289}
290 290
291static bool ble_init(void) { 291void bluefruit_le_init(void) {
292 state.initialized = false; 292 state.initialized = false;
293 state.configured = false; 293 state.configured = false;
294 state.is_connected = false; 294 state.is_connected = false;
@@ -307,7 +307,6 @@ static bool ble_init(void) {
307 wait_ms(1000); // Give it a second to initialize 307 wait_ms(1000); // Give it a second to initialize
308 308
309 state.initialized = true; 309 state.initialized = true;
310 return state.initialized;
311} 310}
312 311
313static inline uint8_t min(uint8_t a, uint8_t b) { 312static inline uint8_t min(uint8_t a, uint8_t b) {
@@ -431,7 +430,7 @@ bool bluefruit_le_is_connected(void) {
431bool bluefruit_le_enable_keyboard(void) { 430bool bluefruit_le_enable_keyboard(void) {
432 char resbuf[128]; 431 char resbuf[128];
433 432
434 if (!state.initialized && !ble_init()) { 433 if (!state.initialized) {
435 return false; 434 return false;
436 } 435 }
437 436
@@ -613,41 +612,24 @@ static bool process_queue_item(struct queue_item *item, uint16_t timeout) {
613 } 612 }
614} 613}
615 614
616void bluefruit_le_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys) { 615void bluefruit_le_send_keyboard(report_keyboard_t *report) {
617 struct queue_item item; 616 struct queue_item item;
618 bool didWait = false;
619 617
620 item.queue_type = QTKeyReport; 618 item.queue_type = QTKeyReport;
621 item.key.modifier = hid_modifier_mask; 619 item.key.modifier = report->mods;
622 item.added = timer_read(); 620 item.key.keys[0] = report->keys[0];
623 621 item.key.keys[1] = report->keys[1];
624 while (nkeys >= 0) { 622 item.key.keys[2] = report->keys[2];
625 item.key.keys[0] = keys[0]; 623 item.key.keys[3] = report->keys[3];
626 item.key.keys[1] = nkeys >= 1 ? keys[1] : 0; 624 item.key.keys[4] = report->keys[4];
627 item.key.keys[2] = nkeys >= 2 ? keys[2] : 0; 625 item.key.keys[5] = report->keys[5];
628 item.key.keys[3] = nkeys >= 3 ? keys[3] : 0;
629 item.key.keys[4] = nkeys >= 4 ? keys[4] : 0;
630 item.key.keys[5] = nkeys >= 5 ? keys[5] : 0;
631
632 if (!send_buf.enqueue(item)) {
633 if (!didWait) {
634 dprint("wait for buf space\n");
635 didWait = true;
636 }
637 send_buf_send_one();
638 continue;
639 }
640
641 if (nkeys <= 6) {
642 return;
643 }
644 626
645 nkeys -= 6; 627 while (!send_buf.enqueue(item)) {
646 keys += 6; 628 send_buf_send_one();
647 } 629 }
648} 630}
649 631
650void bluefruit_le_send_consumer_key(uint16_t usage) { 632void bluefruit_le_send_consumer(uint16_t usage) {
651 struct queue_item item; 633 struct queue_item item;
652 634
653 item.queue_type = QTConsumer; 635 item.queue_type = QTConsumer;
@@ -658,15 +640,15 @@ void bluefruit_le_send_consumer_key(uint16_t usage) {
658 } 640 }
659} 641}
660 642
661void bluefruit_le_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons) { 643void bluefruit_le_send_mouse(report_mouse_t *report) {
662 struct queue_item item; 644 struct queue_item item;
663 645
664 item.queue_type = QTMouseMove; 646 item.queue_type = QTMouseMove;
665 item.mousemove.x = x; 647 item.mousemove.x = report->x;
666 item.mousemove.y = y; 648 item.mousemove.y = report->y;
667 item.mousemove.scroll = scroll; 649 item.mousemove.scroll = report->v;
668 item.mousemove.pan = pan; 650 item.mousemove.pan = report->h;
669 item.mousemove.buttons = buttons; 651 item.mousemove.buttons = report->buttons;
670 652
671 while (!send_buf.enqueue(item)) { 653 while (!send_buf.enqueue(item)) {
672 send_buf_send_one(); 654 send_buf_send_one();
diff --git a/drivers/bluetooth/bluefruit_le.h b/drivers/bluetooth/bluefruit_le.h
index 731ba2e370..a3de03c35c 100644
--- a/drivers/bluetooth/bluefruit_le.h
+++ b/drivers/bluetooth/bluefruit_le.h
@@ -7,9 +7,7 @@
7 7
8#include <stdbool.h> 8#include <stdbool.h>
9#include <stdint.h> 9#include <stdint.h>
10#include <string.h> 10#include "report.h"
11
12#include "config_common.h"
13 11
14#ifdef __cplusplus 12#ifdef __cplusplus
15extern "C" { 13extern "C" {
@@ -26,6 +24,8 @@ extern bool bluefruit_le_query_is_connected(void);
26 * calling ble_task() periodically. */ 24 * calling ble_task() periodically. */
27extern bool bluefruit_le_is_connected(void); 25extern bool bluefruit_le_is_connected(void);
28 26
27extern void bluefruit_le_init(void);
28
29/* Call this periodically to process BLE-originated things */ 29/* Call this periodically to process BLE-originated things */
30extern void bluefruit_le_task(void); 30extern void bluefruit_le_task(void);
31 31
@@ -34,16 +34,16 @@ extern void bluefruit_le_task(void);
34 * this set of keys. 34 * this set of keys.
35 * Also sends a key release indicator, so that the keys do not remain 35 * Also sends a key release indicator, so that the keys do not remain
36 * held down. */ 36 * held down. */
37extern void bluefruit_le_send_keys(uint8_t hid_modifier_mask, uint8_t *keys, uint8_t nkeys); 37extern void bluefruit_le_send_keyboard(report_keyboard_t *report);
38 38
39/* Send a consumer usage. 39/* Send a consumer usage.
40 * (milliseconds) */ 40 * (milliseconds) */
41extern void bluefruit_le_send_consumer_key(uint16_t usage); 41extern void bluefruit_le_send_consumer(uint16_t usage);
42 42
43/* Send a mouse/wheel movement report. 43/* Send a mouse/wheel movement report.
44 * The parameters are signed and indicate positive or negative direction 44 * The parameters are signed and indicate positive or negative direction
45 * change. */ 45 * change. */
46extern void bluefruit_le_send_mouse_move(int8_t x, int8_t y, int8_t scroll, int8_t pan, uint8_t buttons); 46extern void bluefruit_le_send_mouse(report_mouse_t *report);
47 47
48/* Compute battery voltage by reading an analog pin. 48/* Compute battery voltage by reading an analog pin.
49 * Returns the integer number of millivolts */ 49 * Returns the integer number of millivolts */
diff --git a/drivers/bluetooth/bluetooth.c b/drivers/bluetooth/bluetooth.c
new file mode 100644
index 0000000000..d5382401e7
--- /dev/null
+++ b/drivers/bluetooth/bluetooth.c
@@ -0,0 +1,62 @@
1/*
2 * Copyright 2022
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "bluetooth.h"
19
20#if defined(BLUETOOTH_BLUEFRUIT_LE)
21# include "bluefruit_le.h"
22#elif defined(BLUETOOTH_RN42)
23# include "rn42.h"
24#endif
25
26void bluetooth_init(void) {
27#if defined(BLUETOOTH_BLUEFRUIT_LE)
28 bluefruit_le_init();
29#elif defined(BLUETOOTH_RN42)
30 rn42_init();
31#endif
32}
33
34void bluetooth_task(void) {
35#if defined(BLUETOOTH_BLUEFRUIT_LE)
36 bluefruit_le_task();
37#endif
38}
39
40void bluetooth_send_keyboard(report_keyboard_t *report) {
41#if defined(BLUETOOTH_BLUEFRUIT_LE)
42 bluefruit_le_send_keyboard(report);
43#elif defined(BLUETOOTH_RN42)
44 rn42_send_keyboard(report);
45#endif
46}
47
48void bluetooth_send_mouse(report_mouse_t *report) {
49#if defined(BLUETOOTH_BLUEFRUIT_LE)
50 bluefruit_le_send_mouse(report);
51#elif defined(BLUETOOTH_RN42)
52 rn42_send_mouse(report);
53#endif
54}
55
56void bluetooth_send_consumer(uint16_t usage) {
57#if defined(BLUETOOTH_BLUEFRUIT_LE)
58 bluefruit_le_send_consumer(usage);
59#elif defined(BLUETOOTH_RN42)
60 rn42_send_consumer(usage);
61#endif
62}
diff --git a/drivers/bluetooth/bluetooth.h b/drivers/bluetooth/bluetooth.h
new file mode 100644
index 0000000000..a923dfe60c
--- /dev/null
+++ b/drivers/bluetooth/bluetooth.h
@@ -0,0 +1,52 @@
1/*
2 * Copyright 2022
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <stdint.h>
21#include "report.h"
22
23/**
24 * \brief Initialize the Bluetooth system.
25 */
26void bluetooth_init(void);
27
28/**
29 * \brief Perform housekeeping tasks.
30 */
31void bluetooth_task(void);
32
33/**
34 * \brief Send a keyboard report.
35 *
36 * \param report The keyboard report to send.
37 */
38void bluetooth_send_keyboard(report_keyboard_t *report);
39
40/**
41 * \brief Send a mouse report.
42 *
43 * \param report The mouse report to send.
44 */
45void bluetooth_send_mouse(report_mouse_t *report);
46
47/**
48 * \brief Send a consumer usage.
49 *
50 * \param usage The consumer usage to send.
51 */
52void bluetooth_send_consumer(uint16_t usage);