Skip to content

Commit 7e53dab

Browse files
committed
fw: bluetooth: implement gatt client operations
Signed-off-by: Liam McLoughlin <lmcloughlin@google.com>
1 parent 484af62 commit 7e53dab

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

src/bluetooth-fw/nimble/gatt_client_operations.c

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,71 @@
1717
#include <bluetooth/gatt.h>
1818
#include <btutil/bt_device.h>
1919

20+
#include "host/ble_gatt.h"
21+
#include "nimble_type_conversions.h"
22+
#include "os/os_mbuf.h"
23+
24+
static int prv_gatt_write_event_cb(uint16_t conn_handle, const struct ble_gatt_error *error,
25+
struct ble_gatt_attr *attr, void *arg) {
26+
GattClientOpWriteReponse resp = {
27+
.hdr = {
28+
.type = GattClientOpResponseWrite,
29+
.error_code = error->status == 0 ? 0 : BTErrnoInternalErrorBegin + error->status,
30+
.context = arg,
31+
}};
32+
bt_driver_cb_gatt_client_operations_handle_response(&resp.hdr);
33+
return 0;
34+
}
35+
36+
static int prv_gatt_read_event_cb(uint16_t conn_handle, const struct ble_gatt_error *error,
37+
struct ble_gatt_attr *attr, void *arg) {
38+
GattClientOpReadReponse resp = {
39+
.hdr =
40+
{
41+
.type = GattClientOpResponseRead,
42+
.error_code = error->status == 0 ? 0 : BTErrnoInternalErrorBegin + error->status,
43+
.context = arg,
44+
},
45+
.value = attr->om->om_data,
46+
.value_length = attr->om->om_len,
47+
};
48+
bt_driver_cb_gatt_client_operations_handle_response(&resp.hdr);
49+
return 0;
50+
}
51+
2052
BTErrno bt_driver_gatt_write_without_response(GAPLEConnection *connection, const uint8_t *value,
2153
size_t value_length, uint16_t att_handle) {
22-
return 0;
54+
PBL_LOG_D(LOG_DOMAIN_BT, LOG_LEVEL_DEBUG, "bt_driver_gatt_write_without_response: %d",
55+
att_handle);
56+
uint16_t conn_handle;
57+
if (!pebble_device_to_nimble_conn_handle(&connection->device, &conn_handle)) {
58+
return BTErrnoInvalidState;
59+
}
60+
61+
int rc = ble_gattc_write_no_rsp_flat(conn_handle, att_handle, value, value_length);
62+
return rc == 0 ? BTErrnoOK : BTErrnoInternalErrorBegin + rc;
2363
}
2464

2565
BTErrno bt_driver_gatt_write(GAPLEConnection *connection, const uint8_t *value, size_t value_length,
2666
uint16_t att_handle, void *context) {
27-
return 0;
67+
PBL_LOG_D(LOG_DOMAIN_BT, LOG_LEVEL_DEBUG, "bt_driver_gatt_write: %d", att_handle);
68+
uint16_t conn_handle;
69+
if (!pebble_device_to_nimble_conn_handle(&connection->device, &conn_handle)) {
70+
return BTErrnoInvalidState;
71+
}
72+
73+
int rc = ble_gattc_write_flat(conn_handle, att_handle, value, value_length,
74+
prv_gatt_write_event_cb, context);
75+
return rc == 0 ? BTErrnoOK : BTErrnoInternalErrorBegin + rc;
2876
}
2977

3078
BTErrno bt_driver_gatt_read(GAPLEConnection *connection, uint16_t att_handle, void *context) {
31-
return 0;
79+
PBL_LOG_D(LOG_DOMAIN_BT, LOG_LEVEL_DEBUG, "bt_driver_gatt_read: %d", att_handle);
80+
uint16_t conn_handle;
81+
if (!pebble_device_to_nimble_conn_handle(&connection->device, &conn_handle)) {
82+
return BTErrnoInvalidState;
83+
}
84+
85+
int rc = ble_gattc_read(conn_handle, att_handle, prv_gatt_read_event_cb, context);
86+
return rc == 0 ? BTErrnoOK : BTErrnoInternalErrorBegin + rc;
3287
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include <bluetooth/gap_le_connect.h>
20+
#include <bluetooth/responsiveness.h>
21+
22+
#include "host/ble_gap.h"
23+
24+
void nimble_addr_to_pebble_addr(ble_addr_t *addr, BTDeviceAddress *addr_out);
25+
26+
void pebble_device_to_nimble_addr(const BTDeviceInternal *device, ble_addr_t *addr_out);
27+
28+
void nimble_addr_to_pebble_device(ble_addr_t *stack_addr, BTDeviceInternal *host_addr);
29+
30+
bool pebble_device_to_nimble_conn_handle(const BTDeviceInternal *device, uint16_t *handle);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <bluetooth/gap_le_connect.h>
18+
#include <bluetooth/responsiveness.h>
19+
20+
#include "host/ble_gap.h"
21+
22+
void nimble_addr_to_pebble_addr(ble_addr_t *addr, BTDeviceAddress *addr_out) {
23+
memcpy(&addr_out->octets, &addr->val, BLE_DEV_ADDR_LEN);
24+
}
25+
26+
void pebble_device_to_nimble_addr(const BTDeviceInternal *device, ble_addr_t *addr_out) {
27+
addr_out->type = device->is_random_address ? BLE_ADDR_RANDOM : BLE_ADDR_PUBLIC;
28+
memcpy(&addr_out->val, &device->address.octets, BLE_DEV_ADDR_LEN);
29+
}
30+
31+
void nimble_addr_to_pebble_device(ble_addr_t *stack_addr, BTDeviceInternal *host_addr) {
32+
nimble_addr_to_pebble_addr(stack_addr, &host_addr->address);
33+
host_addr->is_random_address = stack_addr->type == BLE_ADDR_RANDOM;
34+
host_addr->is_classic = false;
35+
}
36+
37+
bool pebble_device_to_nimble_conn_handle(const BTDeviceInternal *device, uint16_t *handle) {
38+
ble_addr_t addr;
39+
struct ble_gap_conn_desc desc;
40+
41+
pebble_device_to_nimble_addr(device, &addr);
42+
43+
int rc = ble_gap_conn_find_by_addr(&addr, &desc);
44+
if (rc == 0) {
45+
*handle = desc.conn_handle;
46+
}
47+
48+
return rc == 0;
49+
}

0 commit comments

Comments
 (0)