Skip to content

Commit 1a3923e

Browse files
committed
fw: bluetooth: partially implement pebble pairing service
Signed-off-by: Liam McLoughlin <[email protected]>
1 parent 7e53dab commit 1a3923e

File tree

3 files changed

+123
-8
lines changed

3 files changed

+123
-8
lines changed

src/bluetooth-fw/nimble/init.c

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
static const uint32_t s_bt_stack_start_stop_timeout_ms = 500;
3838

39+
extern void pebble_pairing_service_init(void);
40+
3941
void ble_store_ram_init(void);
4042

4143
static TaskHandle_t s_host_task_handle;
@@ -102,6 +104,7 @@ bool bt_driver_start(BTDriverConfig *config) {
102104
ble_svc_gap_init();
103105
ble_svc_gatt_init();
104106
ble_svc_dis_init();
107+
pebble_pairing_service_init();
105108

106109
ble_hs_sched_start();
107110
bool started = xSemaphoreTake(s_host_started,

src/bluetooth-fw/nimble/pebble_pairing_service.c

+114-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,118 @@
1616

1717
#include <bluetooth/pebble_pairing_service.h>
1818

19-
void bt_driver_pebble_pairing_service_handle_status_change(const GAPLEConnection *connection) {}
19+
#include "host/ble_gap.h"
20+
#include "host/ble_gatt.h"
21+
#include "host/ble_uuid.h"
22+
#include "os/os_mbuf.h"
23+
#include "system/logging.h"
24+
#include "system/passert.h"
2025

21-
void bt_driver_pebble_pairing_service_handle_gatt_mtu_change(const GAPLEConnection *connection) {}
26+
static int prv_access_connection_status(uint16_t conn_handle, uint16_t attr_handle,
27+
struct ble_gatt_access_ctxt *ctxt, void *arg) {
28+
struct ble_gap_conn_desc desc;
29+
if (ble_gap_conn_find(conn_handle, &desc) != 0) {
30+
PBL_LOG_D(LOG_DOMAIN_BT, LOG_LEVEL_ERROR, "Failed to find connection descriptor when reading connection status");
31+
return -1;
32+
}
33+
34+
PebblePairingServiceConnectivityStatus status = {
35+
.is_reversed_ppogatt_enabled = false,
36+
.ble_is_connected = true,
37+
.supports_pinning_without_security_request = false,
38+
.ble_is_bonded = desc.sec_state.bonded,
39+
.ble_is_encrypted = desc.sec_state.encrypted,
40+
};
41+
os_mbuf_append(ctxt->om, &status, sizeof(status));
42+
return 0;
43+
}
44+
45+
static int prv_access_trigger_pairing(uint16_t conn_handle, uint16_t attr_handle,
46+
struct ble_gatt_access_ctxt *ctxt, void *arg) {
47+
int rc = 0;
48+
switch (ctxt->op) {
49+
case BLE_GATT_ACCESS_OP_READ_CHR:
50+
break;
51+
case BLE_GATT_ACCESS_OP_WRITE_CHR:
52+
rc = ble_gap_security_initiate(conn_handle);
53+
PBL_LOG_D(LOG_DOMAIN_BT, LOG_LEVEL_INFO, "security_init rc=%d", rc);
54+
break;
55+
}
56+
return rc;
57+
}
58+
59+
static int prv_access_gatt_mtu(uint16_t conn_handle, uint16_t attr_handle,
60+
struct ble_gatt_access_ctxt *ctxt, void *arg) {
61+
// TODO: implement
62+
return 0;
63+
}
64+
65+
static int prv_access_connection_params(uint16_t conn_handle, uint16_t attr_handle,
66+
struct ble_gatt_access_ctxt *ctxt, void *arg) {
67+
// TODO: implement
68+
return 0;
69+
}
70+
71+
static const struct ble_gatt_svc_def pebble_pairing_svc[] = {
72+
{
73+
.type = BLE_GATT_SVC_TYPE_PRIMARY,
74+
.uuid = BLE_UUID16_DECLARE(PEBBLE_BT_PAIRING_SERVICE_UUID_16BIT),
75+
.characteristics =
76+
(struct ble_gatt_chr_def[]){
77+
{
78+
.uuid = BLE_UUID128_DECLARE(PEBBLE_BT_PAIRING_SERVICE_CONNECTION_STATUS_UUID),
79+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
80+
.access_cb = prv_access_connection_status,
81+
},
82+
{
83+
.uuid = BLE_UUID128_DECLARE(PEBBLE_BT_PAIRING_SERVICE_TRIGGER_PAIRING_UUID),
84+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE,
85+
.access_cb = prv_access_trigger_pairing,
86+
},
87+
{
88+
.uuid = BLE_UUID128_DECLARE(PEBBLE_BT_PAIRING_SERVICE_GATT_MTU_UUID),
89+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY,
90+
.access_cb = prv_access_gatt_mtu,
91+
},
92+
{
93+
.uuid =
94+
BLE_UUID128_DECLARE(PEBBLE_BT_PAIRING_SERVICE_CONNECTION_PARAMETERS_UUID),
95+
.flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY,
96+
.access_cb = prv_access_connection_params,
97+
},
98+
{
99+
0, /* No more characteristics in this service */
100+
},
101+
},
102+
},
103+
{
104+
0, /* No more services */
105+
},
106+
};
107+
108+
void pebble_pairing_service_init(void) {
109+
int rc;
110+
111+
rc = ble_gatts_count_cfg(pebble_pairing_svc);
112+
PBL_ASSERTN(rc == 0);
113+
rc = ble_gatts_add_svcs(pebble_pairing_svc);
114+
PBL_ASSERTN(rc == 0);
115+
}
116+
117+
void prv_notify_chr_updated(const ble_uuid_t *chr_uuid) {
118+
uint16_t chr_val_handle;
119+
int rc = ble_gatts_find_chr(pebble_pairing_svc[0].uuid, chr_uuid, NULL, &chr_val_handle);
120+
if (rc != 0) {
121+
PBL_LOG_D(LOG_DOMAIN_BT, LOG_LEVEL_ERROR, "prv_notify_chr_updated: Failed to find characteristic handle");
122+
return;
123+
}
124+
ble_gatts_chr_updated(chr_val_handle);
125+
}
126+
127+
void bt_driver_pebble_pairing_service_handle_status_change(const GAPLEConnection *connection) {
128+
prv_notify_chr_updated(BLE_UUID128_DECLARE(PEBBLE_BT_PAIRING_SERVICE_CONNECTION_STATUS_UUID));
129+
}
130+
131+
void bt_driver_pebble_pairing_service_handle_gatt_mtu_change(const GAPLEConnection *connection) {
132+
prv_notify_chr_updated(BLE_UUID128_DECLARE(PEBBLE_BT_PAIRING_SERVICE_GATT_MTU_UUID));
133+
}

src/include/bluetooth/pebble_bt.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ void pebble_bt_uuid_expand(Uuid *uuid, uint32_t value);
6262

6363
//! Macro that does the same as pebble_bt_uuid_expand, but then at compile-time
6464
#define PEBBLE_BT_UUID_EXPAND(u) \
65-
(0xff & ((uint32_t) u) >> 24), \
66-
(0xff & ((uint32_t) u) >> 16), \
67-
(0xff & ((uint32_t) u) >> 8), \
65+
0xDA, 0xDA, 0x9B, 0x69, \
66+
0xA6, 0x1A, 0x42, 0xC6, \
67+
0xBB, 0x0F, 0x8E, 0x32, \
6868
(0xff & ((uint32_t) u) >> 0), \
69-
0x32, 0x8E, 0x0F, 0xBB, \
70-
0xC6, 0x42, 0x1A, 0xA6, \
71-
0x69, 0x9B, 0xDA, 0xDA
69+
(0xff & ((uint32_t) u) >> 8), \
70+
(0xff & ((uint32_t) u) >> 16), \
71+
(0xff & ((uint32_t) u) >> 24)

0 commit comments

Comments
 (0)