-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ImmediateAlertClient to support FindMyPhone functionality.
- Loading branch information
Showing
5 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#include "components/ble/ImmediateAlertClient.h" | ||
#include <cstring> | ||
#include <nrf_log.h> | ||
#include "systemtask/SystemTask.h" | ||
|
||
using namespace Pinetime::Controllers; | ||
|
||
constexpr ble_uuid16_t ImmediateAlertClient::immediateAlertClientUuid; | ||
constexpr ble_uuid16_t ImmediateAlertClient::alertLevelCharacteristicUuid; | ||
|
||
// namespace { | ||
// int OnDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error* error, const struct ble_gatt_svc* service, void* arg) { | ||
// auto client = static_cast<ImmediateAlertClient*>(arg); | ||
// return client->OnDiscoveryEvent(conn_handle, error, service); | ||
// } | ||
|
||
// int OnImmediateAlertCharacteristicDiscoveredCallback(uint16_t conn_handle, | ||
// const struct ble_gatt_error* error, | ||
// const struct ble_gatt_chr* chr, | ||
// void* arg) { | ||
// auto client = static_cast<ImmediateAlertClient*>(arg); | ||
// return client->OnCharacteristicDiscoveryEvent(conn_handle, error, chr); | ||
// } | ||
// } | ||
|
||
ImmediateAlertClient::ImmediateAlertClient(Pinetime::System::SystemTask& systemTask) | ||
: systemTask {systemTask}, | ||
characteristicDefinition {{ | ||
.uuid = &alertLevelCharacteristicUuid.u, | ||
.arg = this, | ||
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP, | ||
}, | ||
{0}}, | ||
serviceDefinition { | ||
{/* Device Information Service */ | ||
.type = BLE_GATT_SVC_TYPE_PRIMARY, | ||
.uuid = &immediateAlertClientUuid.u, | ||
.characteristics = characteristicDefinition}, | ||
{0}, | ||
} { | ||
} | ||
|
||
void ImmediateAlertClient::Init() { | ||
} | ||
|
||
bool ImmediateAlertClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service) { | ||
// if (service == nullptr && error->status == BLE_HS_EDONE) { | ||
// if (isDiscovered) { | ||
// NRF_LOG_INFO("IAS found, starting characteristics discovery"); | ||
|
||
// ble_gattc_disc_all_chrs(connectionHandle, iasStartHandle, iasEndHandle, OnImmediateAlertCharacteristicDiscoveredCallback, this); | ||
// } else { | ||
// NRF_LOG_INFO("IAS not found"); | ||
// onServiceDiscovered(connectionHandle); | ||
// } | ||
// return true; | ||
// } | ||
|
||
// if (service != nullptr && ble_uuid_cmp(&immediateAlertClientUuid.u, &service->uuid.u) == 0) { | ||
// NRF_LOG_INFO("IAS discovered : 0x%x - 0x%x", service->start_handle, service->end_handle); | ||
// isDiscovered = true; | ||
// iasStartHandle = service->start_handle; | ||
// iasEndHandle = service->end_handle; | ||
// } | ||
return false; | ||
} | ||
|
||
int ImmediateAlertClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, | ||
const ble_gatt_error* error, | ||
const ble_gatt_chr* characteristic) { | ||
|
||
// if (error->status != 0 && error->status != BLE_HS_EDONE) { | ||
// NRF_LOG_INFO("IAS Characteristic discovery ERROR"); | ||
// onServiceDiscovered(conn_handle); | ||
// return 0; | ||
// } | ||
|
||
// if (characteristic == nullptr && error->status == BLE_HS_EDONE) { | ||
// if (!isCharacteristicDiscovered) { | ||
// NRF_LOG_INFO("IAS Characteristic discovery unsuccessful"); | ||
// onServiceDiscovered(conn_handle); | ||
// } | ||
|
||
// return 0; | ||
// } | ||
|
||
// if (characteristic != nullptr && ble_uuid_cmp(&alertLevelCharacteristicUuid.u, &characteristic->uuid.u) == 0) { | ||
// NRF_LOG_INFO("AIS Characteristic discovered : 0x%x", characteristic->val_handle); | ||
// isCharacteristicDiscovered = true; | ||
// alertLevelHandle = characteristic->val_handle; | ||
// } | ||
return 0; | ||
} | ||
|
||
void ImmediateAlertClient::Discover(uint16_t connectionHandle, std::function<void(uint16_t)> onServiceDiscovered) { | ||
NRF_LOG_INFO("[IAS] Starting discovery"); | ||
this->onServiceDiscovered = onServiceDiscovered; | ||
// ble_gattc_disc_svc_by_uuid(connectionHandle, &immediateAlertClientUuid.u, OnDiscoveryEventCallback, this); | ||
} | ||
|
||
bool ImmediateAlertClient::sendImmediateAlert(ImmediateAlertClient::Levels level) { | ||
|
||
// auto* om = ble_hs_mbuf_from_flat(&level, 1); | ||
|
||
// uint16_t connectionHandle = systemTask.nimble().connHandle(); | ||
|
||
// if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { | ||
// return false; | ||
// } | ||
|
||
// ble_gattc_write_no_rsp(connectionHandle, alertLevelHandle, om); | ||
// return true; | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#pragma once | ||
#define min // workaround: nimble's min/max macros conflict with libstdc++ | ||
#define max | ||
#include <host/ble_gap.h> | ||
#undef max | ||
#undef min | ||
#include <cstdint> | ||
#include "components/ble/BleClient.h" | ||
|
||
namespace Pinetime { | ||
namespace System { | ||
class SystemTask; | ||
} | ||
|
||
namespace Controllers { | ||
class NotificationManager; | ||
|
||
class ImmediateAlertClient : public BleClient { | ||
public: | ||
enum class Levels : uint8_t { NoAlert = 0, MildAlert = 1, HighAlert = 2 }; | ||
|
||
ImmediateAlertClient(Pinetime::System::SystemTask& systemTask); | ||
void Init(); | ||
|
||
bool OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service); | ||
int OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error* error, const ble_gatt_chr* characteristic); | ||
|
||
bool sendImmediateAlert(Levels level); | ||
|
||
static constexpr const ble_uuid16_t* Uuid() { | ||
return &ImmediateAlertClient::immediateAlertClientUuid; | ||
} | ||
|
||
static constexpr const ble_uuid16_t* AlertLevelCharacteristicUuid() { | ||
return &ImmediateAlertClient::alertLevelCharacteristicUuid; | ||
} | ||
|
||
void Discover(uint16_t connectionHandle, std::function<void(uint16_t)> lambda) override; | ||
|
||
private: | ||
Pinetime::System::SystemTask& systemTask; | ||
|
||
static constexpr uint16_t immediateAlertClientId {0x1802}; | ||
static constexpr uint16_t alertLevelId {0x2A06}; | ||
|
||
static constexpr ble_uuid16_t immediateAlertClientUuid {.u {.type = BLE_UUID_TYPE_16}, .value = immediateAlertClientId}; | ||
static constexpr ble_uuid16_t alertLevelCharacteristicUuid {.u {.type = BLE_UUID_TYPE_16}, .value = alertLevelId}; | ||
|
||
bool isDiscovered = false; | ||
uint16_t iasStartHandle; | ||
uint16_t iasEndHandle; | ||
bool isCharacteristicDiscovered = false; | ||
|
||
struct ble_gatt_chr_def characteristicDefinition[3]; | ||
struct ble_gatt_svc_def serviceDefinition[2]; | ||
|
||
uint16_t alertLevelHandle; | ||
std::function<void(uint16_t)> onServiceDiscovered; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters