|
16 | 16 |
|
17 | 17 | #include <bluetooth/pebble_pairing_service.h>
|
18 | 18 |
|
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" |
20 | 25 |
|
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 | +} |
0 commit comments