2020-04-23 18:57:53 +00:00
|
|
|
#include "CurrentTimeClient.h"
|
2020-11-15 14:05:51 +00:00
|
|
|
#include <hal/nrf_rtc.h>
|
2021-02-02 21:09:00 +00:00
|
|
|
#include <nrf_log.h>
|
2020-11-15 14:05:51 +00:00
|
|
|
#include "components/datetime/DateTimeController.h"
|
2020-04-23 18:57:53 +00:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
|
|
|
constexpr ble_uuid16_t CurrentTimeClient::ctsServiceUuid;
|
|
|
|
constexpr ble_uuid16_t CurrentTimeClient::currentTimeCharacteristicUuid;
|
|
|
|
|
2020-10-29 15:06:01 +00:00
|
|
|
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<CurrentTimeClient *>(arg);
|
|
|
|
return client->OnDiscoveryEvent(conn_handle, error, service);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
return client->OnCharacteristicDiscoveryEvent(conn_handle, error, chr);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
return client->OnCurrentTimeReadResult(conn_handle, error, attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentTimeClient::CurrentTimeClient(DateTime &dateTimeController) : dateTimeController{dateTimeController} {
|
2020-04-23 18:57:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentTimeClient::Init() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-10-29 15:06:01 +00:00
|
|
|
bool CurrentTimeClient::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("CTS found, starting characteristics discovery");
|
2020-04-23 18:57:53 +00:00
|
|
|
|
2020-10-29 15:06:01 +00:00
|
|
|
ble_gattc_disc_all_chrs(connectionHandle, ctsStartHandle, ctsEndHandle,
|
|
|
|
OnCurrentTimeCharacteristicDiscoveredCallback, this);
|
|
|
|
} else {
|
|
|
|
NRF_LOG_INFO("CTS not found");
|
|
|
|
onServiceDiscovered(connectionHandle);
|
2020-05-24 14:51:50 +00:00
|
|
|
}
|
2020-10-29 15:06:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (service != nullptr && ble_uuid_cmp(((ble_uuid_t *) &ctsServiceUuid), &service->uuid.u) == 0) {
|
|
|
|
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 13:52:00 +00:00
|
|
|
return false;
|
2020-10-29 15:06:01 +00:00
|
|
|
}
|
|
|
|
return false;
|
2020-04-23 18:57:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error,
|
2020-05-24 14:51:50 +00:00
|
|
|
const ble_gatt_chr *characteristic) {
|
2020-10-28 17:23:09 +00:00
|
|
|
if (characteristic == nullptr && error->status == BLE_HS_EDONE) {
|
2020-10-29 15:06:01 +00: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 14:51:50 +00:00
|
|
|
return 0;
|
2020-10-28 17:23:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 15:06:01 +00:00
|
|
|
if (characteristic != nullptr &&
|
|
|
|
ble_uuid_cmp(((ble_uuid_t *) ¤tTimeCharacteristicUuid), &characteristic->uuid.u) == 0) {
|
2020-10-28 17:23:09 +00:00
|
|
|
NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle);
|
|
|
|
isCharacteristicDiscovered = true;
|
|
|
|
currentTimeHandle = characteristic->val_handle;
|
|
|
|
}
|
|
|
|
return 0;
|
2020-04-23 18:57:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 15:06:01 +00:00
|
|
|
int CurrentTimeClient::OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error *error,
|
|
|
|
const ble_gatt_attr *attribute) {
|
|
|
|
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);
|
|
|
|
NRF_LOG_INFO("Received data: %d-%d-%d %d:%d:%d", result.year,
|
|
|
|
result.month, result.dayofmonth,
|
|
|
|
result.hour, result.minute, result.second);
|
|
|
|
dateTimeController.SetTime(result.year, result.month, result.dayofmonth,
|
|
|
|
0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG));
|
|
|
|
} else {
|
|
|
|
NRF_LOG_INFO("Error retrieving current time: %d", error->status);
|
|
|
|
}
|
2020-04-25 13:52:00 +00:00
|
|
|
|
2020-10-29 15:06:01 +00:00
|
|
|
onServiceDiscovered(conn_handle);
|
|
|
|
return 0;
|
2020-10-28 17:23:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CurrentTimeClient::Reset() {
|
|
|
|
isDiscovered = false;
|
|
|
|
isCharacteristicDiscovered = false;
|
|
|
|
}
|
|
|
|
|
2020-10-29 15:06:01 +00: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 17:23:09 +00:00
|
|
|
}
|