Add a simple service to allow setting the time from a controlling

application
This commit is contained in:
Adam Pigg 2020-04-28 11:21:35 +01:00
parent 332b51464a
commit b760b3f98c
5 changed files with 129 additions and 0 deletions

View File

@ -319,6 +319,7 @@ list(APPEND SOURCE_FILES
Components/Ble/DeviceInformationService.cpp
Components/Ble/CurrentTimeClient.cpp
Components/Ble/AlertNotificationClient.cpp
Components/Ble/PinetimeService.cpp
drivers/Cst816s.cpp
FreeRTOS/port.c
FreeRTOS/port_cmsis_systick.c

View File

@ -74,6 +74,9 @@ void NimbleController::Init() {
deviceInformationService.Init();
currentTimeClient.Init();
pinetimeService.Init();
pinetimeService.setDateTimeController(&dateTimeController);
int res;
res = ble_hs_util_ensure_addr(0);
res = ble_hs_id_infer_auto(0, &addrType);

View File

@ -4,6 +4,7 @@
#include "AlertNotificationClient.h"
#include "DeviceInformationService.h"
#include "CurrentTimeClient.h"
#include "PinetimeService.h"
#include <host/ble_gap.h>
namespace Pinetime {
@ -35,6 +36,7 @@ namespace Pinetime {
DeviceInformationService deviceInformationService;
CurrentTimeClient currentTimeClient;
AlertNotificationClient alertNotificationClient;
PinetimeService pinetimeService;
uint8_t addrType;
uint16_t connectionHandle;
};

View File

@ -0,0 +1,73 @@
#include "PinetimeService.h"
#include <hal/nrf_rtc.h>
using namespace Pinetime::Controllers;
constexpr ble_uuid16_t PinetimeService::pinetimeUuid;
constexpr ble_uuid16_t PinetimeService::timeUuid;
int PinetimeTimeCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
auto pinetimeService = static_cast<PinetimeService*>(arg);
return pinetimeService->OnTimeAccessed(conn_handle, attr_handle, ctxt);
}
void PinetimeService::Init() {
ble_gatts_count_cfg(serviceDefinition);
ble_gatts_add_svcs(serviceDefinition);
}
int PinetimeService::OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt) {
NRF_LOG_INFO("Setting time...");
if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
if (m_dateTimeController) {
CtsData result;
os_mbuf_copydata(ctxt->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);
m_dateTimeController->SetTime(result.year, result.month, result.dayofmonth,
0, result.hour, result.minute, result.second, nrf_rtc_counter_get(portNRF_RTC_REG));
}
}
return 0;
}
PinetimeService::PinetimeService() :
characteristicDefinition{
{
.uuid = (ble_uuid_t *) &timeUuid,
.access_cb = PinetimeTimeCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_WRITE
},
{
0
}
},
serviceDefinition{
{
/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = (ble_uuid_t *) &pinetimeUuid,
.characteristics = characteristicDefinition
},
{
0
},
}
{
}
void PinetimeService::setDateTimeController(DateTime *dateTimeController)
{
m_dateTimeController = dateTimeController;
}

View File

@ -0,0 +1,50 @@
#pragma once
#include <cstdint>
#include <array>
#include <Components/DateTime/DateTimeController.h>
#include <host/ble_gap.h>
namespace Pinetime {
namespace Controllers {
class PinetimeService {
public:
PinetimeService();
void Init();
int OnTimeAccessed(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt);
void setDateTimeController(DateTime *dateTimeController);
private:
static constexpr uint16_t pinetimeId {0x6666};
static constexpr uint16_t timeCharId {0x6667};
static constexpr ble_uuid16_t pinetimeUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = pinetimeId
};
static constexpr ble_uuid16_t timeUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = timeCharId
};
struct ble_gatt_chr_def characteristicDefinition[2];
struct ble_gatt_svc_def serviceDefinition[2];
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;
DateTime *m_dateTimeController = nullptr;
};
}
}