Skip to content

Commit

Permalink
refactor(ble): Extract API to get active profile connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
petejohanson committed Jun 28, 2024
1 parent 49f7275 commit 8404ee2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions app/include/zmk/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ int zmk_ble_prof_disconnect(uint8_t index);

int zmk_ble_active_profile_index(void);
int zmk_ble_profile_index(const bt_addr_le_t *addr);

bt_addr_le_t *zmk_ble_active_profile_addr(void);
struct bt_conn *zmk_ble_active_profile_conn(void);

bool zmk_ble_active_profile_is_open(void);
bool zmk_ble_active_profile_is_connected(void);
char *zmk_ble_active_profile_name(void);
Expand Down
15 changes: 15 additions & 0 deletions app/src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,21 @@ int zmk_ble_prof_disconnect(uint8_t index) {

bt_addr_le_t *zmk_ble_active_profile_addr(void) { return &profiles[active_profile].peer; }

struct bt_conn *zmk_ble_active_profile_conn(void) {
struct bt_conn *conn;
bt_addr_le_t *addr = zmk_ble_active_profile_addr();

if (!bt_addr_le_cmp(addr, BT_ADDR_LE_ANY)) {
LOG_WRN("Not sending, no active address for current profile");
return NULL;
} else if ((conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr)) == NULL) {
LOG_WRN("Not sending, not connected to active profile");
return NULL;
}

return conn;
}

char *zmk_ble_active_profile_name(void) { return profiles[active_profile].name; }

#if IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
Expand Down
19 changes: 2 additions & 17 deletions app/src/hog.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,21 +220,6 @@ BT_GATT_SERVICE_DEFINE(
BT_GATT_CHARACTERISTIC(BT_UUID_HIDS_CTRL_POINT, BT_GATT_CHRC_WRITE_WITHOUT_RESP,
BT_GATT_PERM_WRITE, NULL, write_ctrl_point, &ctrl_point));

struct bt_conn *destination_connection(void) {
struct bt_conn *conn;
bt_addr_le_t *addr = zmk_ble_active_profile_addr();
LOG_DBG("Address pointer %p", addr);
if (!bt_addr_le_cmp(addr, BT_ADDR_LE_ANY)) {
LOG_WRN("Not sending, no active address for current profile");
return NULL;
} else if ((conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, addr)) == NULL) {
LOG_WRN("Not sending, not connected to active profile");
return NULL;
}

return conn;
}

K_THREAD_STACK_DEFINE(hog_q_stack, CONFIG_ZMK_BLE_THREAD_STACK_SIZE);

struct k_work_q hog_work_q;
Expand All @@ -246,7 +231,7 @@ void send_keyboard_report_callback(struct k_work *work) {
struct zmk_hid_keyboard_report_body report;

while (k_msgq_get(&zmk_hog_keyboard_msgq, &report, K_NO_WAIT) == 0) {
struct bt_conn *conn = destination_connection();
struct bt_conn *conn = zmk_ble_active_profile_conn();
if (conn == NULL) {
return;
}
Expand Down Expand Up @@ -298,7 +283,7 @@ void send_consumer_report_callback(struct k_work *work) {
struct zmk_hid_consumer_report_body report;

while (k_msgq_get(&zmk_hog_consumer_msgq, &report, K_NO_WAIT) == 0) {
struct bt_conn *conn = destination_connection();
struct bt_conn *conn = zmk_ble_active_profile_conn();
if (conn == NULL) {
return;
}
Expand Down

0 comments on commit 8404ee2

Please sign in to comment.