-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Add "Find my phone" app. #2053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vchigrin
wants to merge
21
commits into
InfiniTimeOrg:main
Choose a base branch
from
vchigrin:findmyphone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add "Find my phone" app. #2053
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4fb92b5
Try to implement find my phone feature #343
jmlich 286c5a4
update screen layout
jmlich 66c7078
Make it separate as Immediate Alert Client
jmlich 69381ca
enable discovery
jmlich 29f91d9
Fix delivering notification level change in ImmediateAlertClient.
vchigrin 080f1fb
Restore FindMyPhone UI after task sent.
vchigrin d6c94cf
Update documentation.
vchigrin b4c1a90
Update UI. Remove "Mild" level button.
vchigrin 6eb25b3
Fix spelling in documentation.
vchigrin e6c7277
Fix few style issues.
vchigrin 4f7d0c4
Small cosmetic changes.
vchigrin d531674
Fix log messages.
vchigrin 262cb44
Remove accidental change.
vchigrin d879784
Change colors in UI.
vchigrin ce83367
Avoid using uninitialized value in ImmediateAlertClient.
vchigrin d1b1542
Make more ImmediateAlertClient private.
vchigrin be30da1
Update FindMyPhone UI in case disconnect.
vchigrin e62f648
Rename members in FindMyPhoneUI.
vchigrin 0b78017
Avoid blinking during FindMyPhone app start.
vchigrin ace894c
Show communication failure in UI.
vchigrin c491f84
Fix after rebase.
vchigrin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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; | ||
|
|
||
| ImmediateAlertClient::ImmediateAlertClient(Pinetime::System::SystemTask& systemTask) : systemTask {systemTask} { | ||
| } | ||
|
|
||
| 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 (iasHandles.has_value()) { | ||
| NRF_LOG_INFO("[IAS] service found, starting characteristics discovery"); | ||
|
|
||
| ble_gattc_disc_all_chrs(connectionHandle, | ||
| iasHandles->startHandle, | ||
| iasHandles->endHandle, | ||
| OnImmediateAlertCharacteristicDiscoveredCallback, | ||
| this); | ||
| } else { | ||
| NRF_LOG_INFO("[IAS] service 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); | ||
| iasHandles.emplace(HandleRange { | ||
| .startHandle = service->start_handle, | ||
| .endHandle = 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 (!alertLevelHandle.has_value()) { | ||
| NRF_LOG_INFO("[IAS] Alert level characteristic not found."); | ||
| onServiceDiscovered(conn_handle); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| if (characteristic != nullptr && ble_uuid_cmp(&alertLevelCharacteristicUuid.u, &characteristic->uuid.u) == 0) { | ||
| NRF_LOG_INFO("[IAS] Characteristic discovered : 0x%x", characteristic->val_handle); | ||
| alertLevelHandle = characteristic->val_handle; | ||
| state = State::Connected; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| void ImmediateAlertClient::Discover(uint16_t connectionHandle, std::function<void(uint16_t)> onServiceDiscovered) { | ||
| NRF_LOG_INFO("[IAS] Starting discovery"); | ||
| this->onServiceDiscovered = onServiceDiscovered; | ||
| state = State::NoIAS; | ||
| ble_gattc_disc_svc_by_uuid(connectionHandle, &immediateAlertClientUuid.u, OnDiscoveryEventCallback, this); | ||
| } | ||
|
|
||
| void ImmediateAlertClient::Reset() { | ||
| state = State::NoConnection; | ||
| iasHandles = std::nullopt; | ||
| alertLevelHandle = std::nullopt; | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
| if (!alertLevelHandle.has_value()) { | ||
| return false; | ||
| } | ||
|
|
||
| return ble_gattc_write_no_rsp(connectionHandle, *alertLevelHandle, om) == 0; | ||
| } | ||
|
|
||
| int ImmediateAlertClient::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 ImmediateAlertClient::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); | ||
| } | ||
This file contains hidden or 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,77 @@ | ||
| #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 }; | ||
| enum class State { | ||
| NoConnection, | ||
| NoIAS, | ||
| Connected, | ||
| }; | ||
|
|
||
| explicit ImmediateAlertClient(Pinetime::System::SystemTask& systemTask); | ||
| void Init(); | ||
|
|
||
| bool SendImmediateAlert(Levels level); | ||
|
|
||
| State GetState() const { | ||
| return state; | ||
| } | ||
|
|
||
| void Discover(uint16_t connectionHandle, std::function<void(uint16_t)> lambda) override; | ||
| void Reset(); | ||
|
|
||
| private: | ||
| 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); | ||
|
|
||
| static constexpr const ble_uuid16_t* Uuid() { | ||
| return &ImmediateAlertClient::immediateAlertClientUuid; | ||
| } | ||
|
|
||
| static constexpr const ble_uuid16_t* AlertLevelCharacteristicUuid() { | ||
| return &ImmediateAlertClient::alertLevelCharacteristicUuid; | ||
| } | ||
|
|
||
| static int | ||
| OnDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error* error, const struct ble_gatt_svc* service, void* arg); | ||
| static int OnImmediateAlertCharacteristicDiscoveredCallback(uint16_t conn_handle, | ||
| const struct ble_gatt_error* error, | ||
| const struct ble_gatt_chr* chr, | ||
| void* arg); | ||
|
|
||
| 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}; | ||
|
|
||
| struct HandleRange { | ||
| uint16_t startHandle; | ||
| uint16_t endHandle; | ||
| }; | ||
|
|
||
| std::optional<HandleRange> iasHandles; | ||
| std::optional<uint16_t> alertLevelHandle; | ||
| std::function<void(uint16_t)> onServiceDiscovered; | ||
| State state {State::NoConnection}; | ||
| }; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ namespace Pinetime { | |
| SettingChimes, | ||
| SettingShakeThreshold, | ||
| SettingBluetooth, | ||
| FindMyPhone, | ||
| Error | ||
| }; | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.