2021-10-13 16:08:35 -04:00
|
|
|
#include "components/ble/CurrentTimeClient.h"
|
2021-02-02 16:09:00 -05:00
|
|
|
#include <nrf_log.h>
|
2020-11-15 09:05:51 -05:00
|
|
|
#include "components/datetime/DateTimeController.h"
|
2020-04-23 14:57:53 -04:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
|
|
|
constexpr ble_uuid16_t CurrentTimeClient::ctsServiceUuid;
|
|
|
|
constexpr ble_uuid16_t CurrentTimeClient::currentTimeCharacteristicUuid;
|
|
|
|
|
2020-10-29 11:06:01 -04:00
|
|
|
namespace {
|
2021-04-18 13:28:14 -04:00
|
|
|
int OnDiscoveryEventCallback(uint16_t conn_handle, const struct ble_gatt_error* error, const struct ble_gatt_svc* service, void* arg) {
|
|
|
|
auto client = static_cast<CurrentTimeClient*>(arg);
|
2020-10-29 11:06:01 -04:00
|
|
|
return client->OnDiscoveryEvent(conn_handle, error, service);
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int OnCurrentTimeCharacteristicDiscoveredCallback(uint16_t conn_handle,
|
|
|
|
const struct ble_gatt_error* error,
|
|
|
|
const struct ble_gatt_chr* chr,
|
|
|
|
void* arg) {
|
|
|
|
auto client = static_cast<CurrentTimeClient*>(arg);
|
2020-10-29 11:06:01 -04:00
|
|
|
return client->OnCharacteristicDiscoveryEvent(conn_handle, error, chr);
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int CurrentTimeReadCallback(uint16_t conn_handle, const struct ble_gatt_error* error, struct ble_gatt_attr* attr, void* arg) {
|
|
|
|
auto client = static_cast<CurrentTimeClient*>(arg);
|
2020-10-29 11:06:01 -04:00
|
|
|
return client->OnCurrentTimeReadResult(conn_handle, error, attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
CurrentTimeClient::CurrentTimeClient(DateTime& dateTimeController) : dateTimeController {dateTimeController} {
|
2020-04-23 14:57:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentTimeClient::Init() {
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
bool CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error* error, const ble_gatt_svc* service) {
|
2020-10-29 11:06:01 -04:00
|
|
|
if (service == nullptr && error->status == BLE_HS_EDONE) {
|
|
|
|
if (isDiscovered) {
|
|
|
|
NRF_LOG_INFO("CTS found, starting characteristics discovery");
|
2020-04-23 14:57:53 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
ble_gattc_disc_all_chrs(connectionHandle, ctsStartHandle, ctsEndHandle, OnCurrentTimeCharacteristicDiscoveredCallback, this);
|
2020-10-29 11:06:01 -04:00
|
|
|
} else {
|
|
|
|
NRF_LOG_INFO("CTS not found");
|
|
|
|
onServiceDiscovered(connectionHandle);
|
2020-05-24 10:51:50 -04:00
|
|
|
}
|
2020-10-29 11:06:01 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-24 11:40:06 -04:00
|
|
|
if (service != nullptr && ble_uuid_cmp(&ctsServiceUuid.u, &service->uuid.u) == 0) {
|
2020-10-29 11:06:01 -04:00
|
|
|
NRF_LOG_INFO("CTS discovered : 0x%x - 0x%x", service->start_handle, service->end_handle);
|
|
|
|
isDiscovered = true;
|
|
|
|
ctsStartHandle = service->start_handle;
|
|
|
|
ctsEndHandle = service->end_handle;
|
2020-04-25 09:52:00 -04:00
|
|
|
return false;
|
2020-10-29 11:06:01 -04:00
|
|
|
}
|
|
|
|
return false;
|
2020-04-23 14:57:53 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle,
|
|
|
|
const ble_gatt_error* error,
|
|
|
|
const ble_gatt_chr* characteristic) {
|
2020-10-28 13:23:09 -04:00
|
|
|
if (characteristic == nullptr && error->status == BLE_HS_EDONE) {
|
2020-10-29 11:06:01 -04:00
|
|
|
if (isCharacteristicDiscovered) {
|
|
|
|
NRF_LOG_INFO("CTS Characteristic discovery complete, fetching time");
|
|
|
|
ble_gattc_read(conn_handle, currentTimeHandle, CurrentTimeReadCallback, this);
|
|
|
|
} else {
|
|
|
|
NRF_LOG_INFO("CTS Characteristic discovery unsuccessful");
|
|
|
|
onServiceDiscovered(conn_handle);
|
|
|
|
}
|
|
|
|
|
2020-05-24 10:51:50 -04:00
|
|
|
return 0;
|
2020-10-28 13:23:09 -04:00
|
|
|
}
|
|
|
|
|
2021-07-24 11:40:06 -04:00
|
|
|
if (characteristic != nullptr && ble_uuid_cmp(¤tTimeCharacteristicUuid.u, &characteristic->uuid.u) == 0) {
|
2020-10-28 13:23:09 -04:00
|
|
|
NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle);
|
|
|
|
isCharacteristicDiscovered = true;
|
|
|
|
currentTimeHandle = characteristic->val_handle;
|
|
|
|
}
|
|
|
|
return 0;
|
2020-04-23 14:57:53 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int CurrentTimeClient::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error* error, const ble_gatt_attr* attribute) {
|
2020-10-29 11:06:01 -04:00
|
|
|
if (error->status == 0) {
|
|
|
|
// TODO check that attribute->handle equals the handle discovered in OnCharacteristicDiscoveryEvent
|
|
|
|
CtsData result;
|
|
|
|
os_mbuf_copydata(attribute->om, 0, sizeof(CtsData), &result);
|
2022-05-28 08:33:04 -04:00
|
|
|
uint16_t year = ((uint16_t) result.year_MSO << 8) + result.year_LSO;
|
|
|
|
|
|
|
|
NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", year, result.month, result.dayofmonth, result.hour, result.minute, result.second);
|
2023-02-24 00:38:16 -05:00
|
|
|
dateTimeController.SetTime(year, result.month, result.dayofmonth, result.hour, result.minute, result.second);
|
2020-10-29 11:06:01 -04:00
|
|
|
} else {
|
|
|
|
NRF_LOG_INFO("Error retrieving current time: %d", error->status);
|
|
|
|
}
|
2020-04-25 09:52:00 -04:00
|
|
|
|
2020-10-29 11:06:01 -04:00
|
|
|
onServiceDiscovered(conn_handle);
|
|
|
|
return 0;
|
2020-10-28 13:23:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentTimeClient::Reset() {
|
|
|
|
isDiscovered = false;
|
|
|
|
isCharacteristicDiscovered = false;
|
|
|
|
}
|
|
|
|
|
2020-10-29 11:06:01 -04:00
|
|
|
void CurrentTimeClient::Discover(uint16_t connectionHandle, std::function<void(uint16_t)> onServiceDiscovered) {
|
|
|
|
NRF_LOG_INFO("[CTS] Starting discovery");
|
|
|
|
this->onServiceDiscovered = onServiceDiscovered;
|
|
|
|
ble_gattc_disc_svc_by_uuid(connectionHandle, &ctsServiceUuid.u, OnDiscoveryEventCallback, this);
|
2020-10-28 13:23:09 -04:00
|
|
|
}
|