2020-04-23 18:57:53 +00:00
|
|
|
#pragma once
|
2020-11-15 14:05:51 +00:00
|
|
|
#define min // workaround: nimble's min/max macros conflict with libstdc++
|
|
|
|
#define max
|
|
|
|
#include <host/ble_gap.h>
|
|
|
|
#undef max
|
|
|
|
#undef min
|
2020-04-23 18:57:53 +00:00
|
|
|
#include <cstdint>
|
2021-10-13 20:08:35 +00:00
|
|
|
#include "components/ble/BleClient.h"
|
2020-04-23 18:57:53 +00:00
|
|
|
|
|
|
|
namespace Pinetime {
|
2021-04-18 17:28:14 +00:00
|
|
|
namespace Controllers {
|
|
|
|
class DateTime;
|
2020-04-23 18:57:53 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
class CurrentTimeClient : public BleClient {
|
2021-04-24 09:00:45 +00:00
|
|
|
public:
|
2021-04-18 17:28:14 +00:00
|
|
|
explicit CurrentTimeClient(DateTime& dateTimeController);
|
|
|
|
void Init();
|
|
|
|
void Reset();
|
|
|
|
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);
|
|
|
|
int OnCurrentTimeReadResult(uint16_t conn_handle, const ble_gatt_error* error, const ble_gatt_attr* attribute);
|
|
|
|
static constexpr const ble_uuid16_t* Uuid() {
|
|
|
|
return &CurrentTimeClient::ctsServiceUuid;
|
|
|
|
}
|
|
|
|
static constexpr const ble_uuid16_t* CurrentTimeCharacteristicUuid() {
|
|
|
|
return &CurrentTimeClient::currentTimeCharacteristicUuid;
|
|
|
|
}
|
|
|
|
void Discover(uint16_t connectionHandle, std::function<void(uint16_t)> lambda) override;
|
2020-10-29 15:06:01 +00:00
|
|
|
|
2021-04-24 09:00:45 +00:00
|
|
|
private:
|
2021-04-18 17:28:14 +00:00
|
|
|
typedef struct __attribute__((packed)) {
|
|
|
|
uint16_t year;
|
|
|
|
uint8_t month;
|
|
|
|
uint8_t dayofmonth;
|
|
|
|
uint8_t hour;
|
|
|
|
uint8_t minute;
|
|
|
|
uint8_t second;
|
|
|
|
uint8_t millis;
|
|
|
|
uint8_t reason;
|
|
|
|
} CtsData;
|
2020-04-23 18:57:53 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
static constexpr uint16_t ctsServiceId {0x1805};
|
|
|
|
static constexpr uint16_t currentTimeCharacteristicId {0x2a2b};
|
2020-04-23 18:57:53 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
static constexpr ble_uuid16_t ctsServiceUuid {.u {.type = BLE_UUID_TYPE_16}, .value = ctsServiceId};
|
|
|
|
static constexpr ble_uuid16_t currentTimeCharacteristicUuid {.u {.type = BLE_UUID_TYPE_16}, .value = currentTimeCharacteristicId};
|
2020-04-23 18:57:53 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
DateTime& dateTimeController;
|
|
|
|
bool isDiscovered = false;
|
|
|
|
uint16_t ctsStartHandle;
|
|
|
|
uint16_t ctsEndHandle;
|
2020-10-28 17:23:09 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
bool isCharacteristicDiscovered = false;
|
|
|
|
uint16_t currentTimeHandle;
|
|
|
|
std::function<void(uint16_t)> onServiceDiscovered;
|
|
|
|
};
|
|
|
|
}
|
2020-04-23 18:57:53 +00:00
|
|
|
}
|