summaryrefslogtreecommitdiff
path: root/src/Components/Ble/CurrentTimeClient.cpp
blob: 524c37d5f52a794c06b82a656ee8d26f0652a865 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <hal/nrf_rtc.h>
#include "CurrentTimeClient.h"

using namespace Pinetime::Controllers;

constexpr ble_uuid16_t CurrentTimeClient::ctsServiceUuid;
constexpr ble_uuid16_t CurrentTimeClient::currentTimeCharacteristicUuid;

int Pinetime::Controllers::CurrentTimeDiscoveryEventCallback(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 Pinetime::Controllers::CurrentTimeCharacteristicDiscoveredCallback(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 Pinetime::Controllers::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} {

}

void CurrentTimeClient::Init() {

}

void CurrentTimeClient::StartDiscovery(uint16_t connectionHandle) {
  ble_gattc_disc_svc_by_uuid(connectionHandle, ((ble_uuid_t*)&ctsServiceUuid), CurrentTimeDiscoveryEventCallback, this);
}

int CurrentTimeClient::OnDiscoveryEvent(uint16_t connectionHandle, const ble_gatt_error *error, const ble_gatt_svc *service) {
  if(service == nullptr && error->status == BLE_HS_EDONE)
    NRF_LOG_INFO("Discovery complete");

  if(service != nullptr && ble_uuid_cmp(((ble_uuid_t*)&ctsServiceUuid), &service->uuid.u) == 0) {
    NRF_LOG_INFO("CTS discovered : 0x%x", service->start_handle);
    ble_gattc_disc_chrs_by_uuid(connectionHandle, service->start_handle, service->end_handle, ((ble_uuid_t*)&currentTimeCharacteristicUuid), CurrentTimeCharacteristicDiscoveredCallback, this);
  }
  return 0;
}

int CurrentTimeClient::OnCharacteristicDiscoveryEvent(uint16_t conn_handle, const ble_gatt_error *error,
                                   const ble_gatt_chr *characteristic) {
  if(characteristic == nullptr && error->status == BLE_HS_EDONE)
    NRF_LOG_INFO("Characteristic discovery complete");

  if(characteristic != nullptr && ble_uuid_cmp(((ble_uuid_t*)&currentTimeCharacteristicUuid), &characteristic->uuid.u) == 0) {
    NRF_LOG_INFO("CTS Characteristic discovered : 0x%x", characteristic->val_handle);

    ble_gattc_read(conn_handle, characteristic->val_handle, CurrentTimeReadCallback, this);
  }
  return 0;
}

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);
  }
  return 0;
}