|
17 | 17 | #include <bluetooth/gatt.h> |
18 | 18 | #include <btutil/bt_device.h> |
19 | 19 |
|
| 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 | + |
20 | 52 | BTErrno bt_driver_gatt_write_without_response(GAPLEConnection *connection, const uint8_t *value, |
21 | 53 | 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; |
23 | 63 | } |
24 | 64 |
|
25 | 65 | BTErrno bt_driver_gatt_write(GAPLEConnection *connection, const uint8_t *value, size_t value_length, |
26 | 66 | 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; |
28 | 76 | } |
29 | 77 |
|
30 | 78 | 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; |
32 | 87 | } |
0 commit comments