|
| 1 | +#include "components/ble/ImmediateAlertClient.h" |
| 2 | +#include <cstring> |
| 3 | +#include <nrf_log.h> |
| 4 | +#include "systemtask/SystemTask.h" |
| 5 | + |
| 6 | +using namespace Pinetime::Controllers; |
| 7 | + |
| 8 | +constexpr ble_uuid16_t ImmediateAlertClient::immediateAlertClientUuid; |
| 9 | +constexpr ble_uuid16_t ImmediateAlertClient::alertLevelCharacteristicUuid; |
| 10 | + |
| 11 | +// namespace { |
| 12 | +// int OnDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error* error, const struct ble_gatt_svc* service, void* arg) { |
| 13 | +// auto client = static_cast<ImmediateAlertClient*>(arg); |
| 14 | +// return client->OnDiscoveryEvent(conn_handle, error, service); |
| 15 | +// } |
| 16 | + |
| 17 | +// int OnImmediateAlertCharacteristicDiscoveredCallback(uint16_t conn_handle, |
| 18 | +// const struct ble_gatt_error* error, |
| 19 | +// const struct ble_gatt_chr* chr, |
| 20 | +// void* arg) { |
| 21 | +// auto client = static_cast<ImmediateAlertClient*>(arg); |
| 22 | +// return client->OnCharacteristicDiscoveryEvent(conn_handle, error, chr); |
| 23 | +// } |
| 24 | +// } |
| 25 | + |
| 26 | +ImmediateAlertClient::ImmediateAlertClient(Pinetime::System::SystemTask& systemTask) |
| 27 | + : systemTask {systemTask}, |
| 28 | + characteristicDefinition {{ |
| 29 | + .uuid = &alertLevelCharacteristicUuid.u, |
| 30 | + .arg = this, |
| 31 | + .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, |
| 32 | + }, |
| 33 | + {0}}, |
| 34 | + serviceDefinition { |
| 35 | + {/* Device Information Service */ |
| 36 | + .type = BLE_GATT_SVC_TYPE_PRIMARY, |
| 37 | + .uuid = &immediateAlertClientUuid.u, |
| 38 | + .characteristics = characteristicDefinition}, |
| 39 | + {0}, |
| 40 | + } { |
| 41 | +} |
| 42 | + |
| 43 | +void ImmediateAlertClient::Init() { |
| 44 | +} |
| 45 | + |
| 46 | +bool ImmediateAlertClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service) { |
| 47 | + // if (service == nullptr && error->status == BLE_HS_EDONE) { |
| 48 | + // if (isDiscovered) { |
| 49 | + // NRF_LOG_INFO("IAS found, starting characteristics discovery"); |
| 50 | + |
| 51 | + // ble_gattc_disc_all_chrs(connectionHandle, iasStartHandle, iasEndHandle, OnImmediateAlertCharacteristicDiscoveredCallback, this); |
| 52 | + // } else { |
| 53 | + // NRF_LOG_INFO("IAS not found"); |
| 54 | + // onServiceDiscovered(connectionHandle); |
| 55 | + // } |
| 56 | + // return true; |
| 57 | + // } |
| 58 | + |
| 59 | + // if (service != nullptr && ble_uuid_cmp(&immediateAlertClientUuid.u, &service->uuid.u) == 0) { |
| 60 | + // NRF_LOG_INFO("IAS discovered : 0x%x - 0x%x", service->start_handle, service->end_handle); |
| 61 | + // isDiscovered = true; |
| 62 | + // iasStartHandle = service->start_handle; |
| 63 | + // iasEndHandle = service->end_handle; |
| 64 | + // } |
| 65 | + return false; |
| 66 | +} |
| 67 | + |
| 68 | +int ImmediateAlertClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, |
| 69 | + const ble_gatt_error* error, |
| 70 | + const ble_gatt_chr* characteristic) { |
| 71 | + |
| 72 | + // if (error->status != 0 && error->status != BLE_HS_EDONE) { |
| 73 | + // NRF_LOG_INFO("IAS Characteristic discovery ERROR"); |
| 74 | + // onServiceDiscovered(conn_handle); |
| 75 | + // return 0; |
| 76 | + // } |
| 77 | + |
| 78 | + // if (characteristic == nullptr && error->status == BLE_HS_EDONE) { |
| 79 | + // if (!isCharacteristicDiscovered) { |
| 80 | + // NRF_LOG_INFO("IAS Characteristic discovery unsuccessful"); |
| 81 | + // onServiceDiscovered(conn_handle); |
| 82 | + // } |
| 83 | + |
| 84 | + // return 0; |
| 85 | + // } |
| 86 | + |
| 87 | + // if (characteristic != nullptr && ble_uuid_cmp(&alertLevelCharacteristicUuid.u, &characteristic->uuid.u) == 0) { |
| 88 | + // NRF_LOG_INFO("AIS Characteristic discovered : 0x%x", characteristic->val_handle); |
| 89 | + // isCharacteristicDiscovered = true; |
| 90 | + // alertLevelHandle = characteristic->val_handle; |
| 91 | + // } |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +void ImmediateAlertClient::Discover(uint16_t connectionHandle, std::function<void(uint16_t)> onServiceDiscovered) { |
| 96 | + NRF_LOG_INFO("[IAS] Starting discovery"); |
| 97 | + this->onServiceDiscovered = onServiceDiscovered; |
| 98 | + // ble_gattc_disc_svc_by_uuid(connectionHandle, &immediateAlertClientUuid.u, OnDiscoveryEventCallback, this); |
| 99 | +} |
| 100 | + |
| 101 | +bool ImmediateAlertClient::sendImmediateAlert(ImmediateAlertClient::Levels level) { |
| 102 | + |
| 103 | + // auto* om = ble_hs_mbuf_from_flat(&level, 1); |
| 104 | + |
| 105 | + // uint16_t connectionHandle = systemTask.nimble().connHandle(); |
| 106 | + |
| 107 | + // if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { |
| 108 | + // return false; |
| 109 | + // } |
| 110 | + |
| 111 | + // ble_gattc_write_no_rsp(connectionHandle, alertLevelHandle, om); |
| 112 | + // return true; |
| 113 | + return false; |
| 114 | +} |
0 commit comments