summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Challis <git@zvecr.com>2025-05-05 04:05:04 +0100
committerGitHub <noreply@github.com>2025-05-05 04:05:04 +0100
commit842c8401452f83e691a9df97ffba52a389c885c8 (patch)
tree39cc609985e6d8bb6cfa859520ee062cb4793805
parent614b631ee2649de67a830a39393af416acee58a8 (diff)
Bind Bluetooth driver to `host_driver_t` (#25199)
-rw-r--r--builddefs/common_features.mk5
-rw-r--r--drivers/bluetooth/bluetooth.c70
-rw-r--r--drivers/bluetooth/bluetooth.h26
-rw-r--r--drivers/bluetooth/bluetooth_drivers.c71
-rw-r--r--quantum/connection/connection.c14
-rw-r--r--tmk_core/protocol/host.c83
6 files changed, 175 insertions, 94 deletions
diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk
index 5d007e099d..f62b925817 100644
--- a/builddefs/common_features.mk
+++ b/builddefs/common_features.mk
@@ -898,16 +898,17 @@ ifeq ($(strip $(BLUETOOTH_ENABLE)), yes)
898 NO_USB_STARTUP_CHECK := yes 898 NO_USB_STARTUP_CHECK := yes
899 CONNECTION_ENABLE := yes 899 CONNECTION_ENABLE := yes
900 COMMON_VPATH += $(DRIVER_PATH)/bluetooth 900 COMMON_VPATH += $(DRIVER_PATH)/bluetooth
901 SRC += $(DRIVER_PATH)/bluetooth/bluetooth.c
901 902
902 ifeq ($(strip $(BLUETOOTH_DRIVER)), bluefruit_le) 903 ifeq ($(strip $(BLUETOOTH_DRIVER)), bluefruit_le)
903 SPI_DRIVER_REQUIRED = yes 904 SPI_DRIVER_REQUIRED = yes
904 SRC += $(DRIVER_PATH)/bluetooth/bluetooth.c 905 SRC += $(DRIVER_PATH)/bluetooth/bluetooth_drivers.c
905 SRC += $(DRIVER_PATH)/bluetooth/bluefruit_le.cpp 906 SRC += $(DRIVER_PATH)/bluetooth/bluefruit_le.cpp
906 endif 907 endif
907 908
908 ifeq ($(strip $(BLUETOOTH_DRIVER)), rn42) 909 ifeq ($(strip $(BLUETOOTH_DRIVER)), rn42)
909 UART_DRIVER_REQUIRED = yes 910 UART_DRIVER_REQUIRED = yes
910 SRC += $(DRIVER_PATH)/bluetooth/bluetooth.c 911 SRC += $(DRIVER_PATH)/bluetooth/bluetooth_drivers.c
911 SRC += $(DRIVER_PATH)/bluetooth/rn42.c 912 SRC += $(DRIVER_PATH)/bluetooth/rn42.c
912 endif 913 endif
913endif 914endif
diff --git a/drivers/bluetooth/bluetooth.c b/drivers/bluetooth/bluetooth.c
index d5382401e7..3d41d8b11c 100644
--- a/drivers/bluetooth/bluetooth.c
+++ b/drivers/bluetooth/bluetooth.c
@@ -1,62 +1,26 @@
1/* 1// Copyright 2025 QMK
2 * Copyright 2022 2// SPDX-License-Identifier: GPL-2.0-or-later
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 3
18#include "bluetooth.h" 4#include "bluetooth.h"
19 5
20#if defined(BLUETOOTH_BLUEFRUIT_LE) 6__attribute__((weak)) void bluetooth_init(void) {}
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 7
34void bluetooth_task(void) { 8__attribute__((weak)) void bluetooth_task(void) {}
35#if defined(BLUETOOTH_BLUEFRUIT_LE)
36 bluefruit_le_task();
37#endif
38}
39 9
40void bluetooth_send_keyboard(report_keyboard_t *report) { 10__attribute__((weak)) bool bluetooth_is_connected(void) {
41#if defined(BLUETOOTH_BLUEFRUIT_LE) 11 return true;
42 bluefruit_le_send_keyboard(report);
43#elif defined(BLUETOOTH_RN42)
44 rn42_send_keyboard(report);
45#endif
46} 12}
47 13
48void bluetooth_send_mouse(report_mouse_t *report) { 14__attribute__((weak)) uint8_t bluetooth_keyboard_leds(void) {
49#if defined(BLUETOOTH_BLUEFRUIT_LE) 15 return 0;
50 bluefruit_le_send_mouse(report);
51#elif defined(BLUETOOTH_RN42)
52 rn42_send_mouse(report);
53#endif
54} 16}
55 17
56void bluetooth_send_consumer(uint16_t usage) { 18__attribute__((weak)) void bluetooth_send_keyboard(report_keyboard_t *report) {}
57#if defined(BLUETOOTH_BLUEFRUIT_LE) 19
58 bluefruit_le_send_consumer(usage); 20__attribute__((weak)) void bluetooth_send_nkro(report_nkro_t *report) {}
59#elif defined(BLUETOOTH_RN42) 21
60 rn42_send_consumer(usage); 22__attribute__((weak)) void bluetooth_send_mouse(report_mouse_t *report) {}
61#endif 23
62} 24__attribute__((weak)) void bluetooth_send_consumer(uint16_t usage) {}
25
26__attribute__((weak)) void bluetooth_send_system(uint16_t usage) {}
diff --git a/drivers/bluetooth/bluetooth.h b/drivers/bluetooth/bluetooth.h
index 2e4d0df538..fe67c0c968 100644
--- a/drivers/bluetooth/bluetooth.h
+++ b/drivers/bluetooth/bluetooth.h
@@ -31,6 +31,18 @@ void bluetooth_init(void);
31void bluetooth_task(void); 31void bluetooth_task(void);
32 32
33/** 33/**
34 * \brief Detects if Bluetooth is connected.
35 *
36 * \return `true` if connected, `false` otherwise.
37 */
38bool bluetooth_is_connected(void);
39
40/**
41 * \brief Get current LED state.
42 */
43uint8_t bluetooth_keyboard_leds(void);
44
45/**
34 * \brief Send a keyboard report. 46 * \brief Send a keyboard report.
35 * 47 *
36 * \param report The keyboard report to send. 48 * \param report The keyboard report to send.
@@ -38,6 +50,13 @@ void bluetooth_task(void);
38void bluetooth_send_keyboard(report_keyboard_t *report); 50void bluetooth_send_keyboard(report_keyboard_t *report);
39 51
40/** 52/**
53 * \brief Send a nkro report.
54 *
55 * \param report The nkro report to send.
56 */
57void bluetooth_send_nkro(report_nkro_t *report);
58
59/**
41 * \brief Send a mouse report. 60 * \brief Send a mouse report.
42 * 61 *
43 * \param report The mouse report to send. 62 * \param report The mouse report to send.
@@ -50,3 +69,10 @@ void bluetooth_send_mouse(report_mouse_t *report);
50 * \param usage The consumer usage to send. 69 * \param usage The consumer usage to send.
51 */ 70 */
52void bluetooth_send_consumer(uint16_t usage); 71void bluetooth_send_consumer(uint16_t usage);
72
73/**
74 * \brief Send a system usage.
75 *
76 * \param usage The system usage to send.
77 */
78void bluetooth_send_system(uint16_t usage);
diff --git a/drivers/bluetooth/bluetooth_drivers.c b/drivers/bluetooth/bluetooth_drivers.c
new file mode 100644
index 0000000000..b91d4501e6
--- /dev/null
+++ b/drivers/bluetooth/bluetooth_drivers.c
@@ -0,0 +1,71 @@
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
40bool bluetooth_is_connected(void) {
41#if defined(BLUETOOTH_BLUEFRUIT_LE)
42 return bluefruit_le_is_connected();
43#else
44 // TODO: drivers should check if BT is connected here
45 return true;
46#endif
47}
48
49void bluetooth_send_keyboard(report_keyboard_t *report) {
50#if defined(BLUETOOTH_BLUEFRUIT_LE)
51 bluefruit_le_send_keyboard(report);
52#elif defined(BLUETOOTH_RN42)
53 rn42_send_keyboard(report);
54#endif
55}
56
57void bluetooth_send_mouse(report_mouse_t *report) {
58#if defined(BLUETOOTH_BLUEFRUIT_LE)
59 bluefruit_le_send_mouse(report);
60#elif defined(BLUETOOTH_RN42)
61 rn42_send_mouse(report);
62#endif
63}
64
65void bluetooth_send_consumer(uint16_t usage) {
66#if defined(BLUETOOTH_BLUEFRUIT_LE)
67 bluefruit_le_send_consumer(usage);
68#elif defined(BLUETOOTH_RN42)
69 rn42_send_consumer(usage);
70#endif
71}
diff --git a/quantum/connection/connection.c b/quantum/connection/connection.c
index c7f3c4b424..1ff8876876 100644
--- a/quantum/connection/connection.c
+++ b/quantum/connection/connection.c
@@ -5,6 +5,10 @@
5#include "usb_util.h" 5#include "usb_util.h"
6#include "util.h" 6#include "util.h"
7 7
8#ifdef BLUETOOTH_ENABLE
9# include "bluetooth.h"
10#endif
11
8// ======== DEPRECATED DEFINES - DO NOT USE ======== 12// ======== DEPRECATED DEFINES - DO NOT USE ========
9#ifdef OUTPUT_DEFAULT 13#ifdef OUTPUT_DEFAULT
10# undef CONNECTION_HOST_DEFAULT 14# undef CONNECTION_HOST_DEFAULT
@@ -14,16 +18,6 @@
14__attribute__((weak)) void set_output_user(uint8_t output) {} 18__attribute__((weak)) void set_output_user(uint8_t output) {}
15// ======== 19// ========
16 20
17#ifdef BLUETOOTH_ENABLE
18# ifdef BLUETOOTH_BLUEFRUIT_LE
19# include "bluefruit_le.h"
20# define bluetooth_is_connected() bluefruit_le_is_connected()
21# else
22// TODO: drivers should check if BT is connected here
23# define bluetooth_is_connected() true
24# endif
25#endif
26
27#define CONNECTION_HOST_INVALID 0xFF 21#define CONNECTION_HOST_INVALID 0xFF
28 22
29#ifndef CONNECTION_HOST_DEFAULT 23#ifndef CONNECTION_HOST_DEFAULT
diff --git a/tmk_core/protocol/host.c b/tmk_core/protocol/host.c
index 453952049f..4f3867df08 100644
--- a/tmk_core/protocol/host.c
+++ b/tmk_core/protocol/host.c
@@ -30,12 +30,31 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
30# include "joystick.h" 30# include "joystick.h"
31#endif 31#endif
32 32
33#ifdef BLUETOOTH_ENABLE 33#ifdef CONNECTION_ENABLE
34# ifndef CONNECTION_ENABLE
35# error CONNECTION_ENABLE required and not enabled
36# endif
37# include "connection.h" 34# include "connection.h"
35#endif
36
37#ifdef BLUETOOTH_ENABLE
38# include "bluetooth.h" 38# include "bluetooth.h"
39
40static void bluetooth_send_extra(report_extra_t *report) {
41 switch (report->report_id) {
42 case REPORT_ID_SYSTEM:
43 bluetooth_send_system(report->usage);
44 return;
45 case REPORT_ID_CONSUMER:
46 bluetooth_send_consumer(report->usage);
47 return;
48 }
49}
50
51host_driver_t bt_driver = {
52 .keyboard_leds = bluetooth_keyboard_leds,
53 .send_keyboard = bluetooth_send_keyboard,
54 .send_nkro = bluetooth_send_nkro,
55 .send_mouse = bluetooth_send_mouse,
56 .send_extra = bluetooth_send_extra,
57};
39#endif 58#endif
40 59
41#ifdef NKRO_ENABLE 60#ifdef NKRO_ENABLE
@@ -55,6 +74,22 @@ host_driver_t *host_get_driver(void) {
55 return driver; 74 return driver;
56} 75}
57 76
77static host_driver_t *host_get_active_driver(void) {
78#ifdef CONNECTION_ENABLE
79 switch (connection_get_host()) {
80# ifdef BLUETOOTH_ENABLE
81 case CONNECTION_HOST_BLUETOOTH:
82 return &bt_driver;
83# endif
84 case CONNECTION_HOST_NONE:
85 return NULL;
86 default:
87 break;
88 }
89#endif
90 return driver;
91}
92
58#ifdef SPLIT_KEYBOARD 93#ifdef SPLIT_KEYBOARD
59uint8_t split_led_state = 0; 94uint8_t split_led_state = 0;
60void set_split_host_keyboard_leds(uint8_t led_state) { 95void set_split_host_keyboard_leds(uint8_t led_state) {
@@ -66,7 +101,10 @@ uint8_t host_keyboard_leds(void) {
66#ifdef SPLIT_KEYBOARD 101#ifdef SPLIT_KEYBOARD
67 if (!is_keyboard_master()) return split_led_state; 102 if (!is_keyboard_master()) return split_led_state;
68#endif 103#endif
69 if (!driver) return 0; 104
105 host_driver_t *driver = host_get_active_driver();
106 if (!driver || !driver->keyboard_leds) return 0;
107
70 return (*driver->keyboard_leds)(); 108 return (*driver->keyboard_leds)();
71} 109}
72 110
@@ -76,14 +114,9 @@ led_t host_keyboard_led_state(void) {
76 114
77/* send report */ 115/* send report */
78void host_keyboard_send(report_keyboard_t *report) { 116void host_keyboard_send(report_keyboard_t *report) {
79#ifdef BLUETOOTH_ENABLE 117 host_driver_t *driver = host_get_active_driver();
80 if (connection_get_host() == CONNECTION_HOST_BLUETOOTH) { 118 if (!driver || !driver->send_keyboard) return;
81 bluetooth_send_keyboard(report);
82 return;
83 }
84#endif
85 119
86 if (!driver) return;
87#ifdef KEYBOARD_SHARED_EP 120#ifdef KEYBOARD_SHARED_EP
88 report->report_id = REPORT_ID_KEYBOARD; 121 report->report_id = REPORT_ID_KEYBOARD;
89#endif 122#endif
@@ -99,7 +132,9 @@ void host_keyboard_send(report_keyboard_t *report) {
99} 132}
100 133
101void host_nkro_send(report_nkro_t *report) { 134void host_nkro_send(report_nkro_t *report) {
102 if (!driver) return; 135 host_driver_t *driver = host_get_active_driver();
136 if (!driver || !driver->send_nkro) return;
137
103 report->report_id = REPORT_ID_NKRO; 138 report->report_id = REPORT_ID_NKRO;
104 (*driver->send_nkro)(report); 139 (*driver->send_nkro)(report);
105 140
@@ -113,14 +148,9 @@ void host_nkro_send(report_nkro_t *report) {
113} 148}
114 149
115void host_mouse_send(report_mouse_t *report) { 150void host_mouse_send(report_mouse_t *report) {
116#ifdef BLUETOOTH_ENABLE 151 host_driver_t *driver = host_get_active_driver();
117 if (connection_get_host() == CONNECTION_HOST_BLUETOOTH) { 152 if (!driver || !driver->send_mouse) return;
118 bluetooth_send_mouse(report);
119 return;
120 }
121#endif
122 153
123 if (!driver) return;
124#ifdef MOUSE_SHARED_EP 154#ifdef MOUSE_SHARED_EP
125 report->report_id = REPORT_ID_MOUSE; 155 report->report_id = REPORT_ID_MOUSE;
126#endif 156#endif
@@ -136,7 +166,8 @@ void host_system_send(uint16_t usage) {
136 if (usage == last_system_usage) return; 166 if (usage == last_system_usage) return;
137 last_system_usage = usage; 167 last_system_usage = usage;
138 168
139 if (!driver) return; 169 host_driver_t *driver = host_get_active_driver();
170 if (!driver || !driver->send_extra) return;
140 171
141 report_extra_t report = { 172 report_extra_t report = {
142 .report_id = REPORT_ID_SYSTEM, 173 .report_id = REPORT_ID_SYSTEM,
@@ -149,14 +180,8 @@ void host_consumer_send(uint16_t usage) {
149 if (usage == last_consumer_usage) return; 180 if (usage == last_consumer_usage) return;
150 last_consumer_usage = usage; 181 last_consumer_usage = usage;
151 182
152#ifdef BLUETOOTH_ENABLE 183 host_driver_t *driver = host_get_active_driver();
153 if (connection_get_host() == CONNECTION_HOST_BLUETOOTH) { 184 if (!driver || !driver->send_extra) return;
154 bluetooth_send_consumer(usage);
155 return;
156 }
157#endif
158
159 if (!driver) return;
160 185
161 report_extra_t report = { 186 report_extra_t report = {
162 .report_id = REPORT_ID_CONSUMER, 187 .report_id = REPORT_ID_CONSUMER,