NimbleController : Encapsulate device info service in its own class.

This commit is contained in:
JF 2020-04-23 20:34:38 +02:00
parent 14d6954466
commit 24a7b6e397
6 changed files with 175 additions and 119 deletions

View File

@ -317,6 +317,7 @@ list(APPEND SOURCE_FILES
Components/DateTime/DateTimeController.cpp Components/DateTime/DateTimeController.cpp
Components/Brightness/BrightnessController.cpp Components/Brightness/BrightnessController.cpp
Components/Ble/NimbleController.cpp Components/Ble/NimbleController.cpp
Components/Ble/DeviceInformationService.cpp
drivers/Cst816s.cpp drivers/Cst816s.cpp
FreeRTOS/port.c FreeRTOS/port.c
FreeRTOS/port_cmsis_systick.c FreeRTOS/port_cmsis_systick.c
@ -362,6 +363,7 @@ set(INCLUDE_FILES
Components/DateTime/DateTimeController.h Components/DateTime/DateTimeController.h
Components/Brightness/BrightnessController.h Components/Brightness/BrightnessController.h
Components/Ble/NimbleController.h Components/Ble/NimbleController.h
Components/Ble/DeviceInformationService.h
drivers/Cst816s.h drivers/Cst816s.h
FreeRTOS/portmacro.h FreeRTOS/portmacro.h
FreeRTOS/portmacro_cmsis.h FreeRTOS/portmacro_cmsis.h

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <FreeRTOS.h>> #include <FreeRTOS.h>
#include <queue.h> #include <queue.h>
namespace Pinetime { namespace Pinetime {

View File

@ -0,0 +1,101 @@
#include "DeviceInformationService.h"
using namespace Pinetime::Controllers;
constexpr ble_uuid16_t DeviceInformationService::manufacturerNameUuid;
constexpr ble_uuid16_t DeviceInformationService::modelNumberUuid;
constexpr ble_uuid16_t DeviceInformationService::serialNumberUuid;
constexpr ble_uuid16_t DeviceInformationService::fwRevisionUuid;
constexpr ble_uuid16_t DeviceInformationService::deviceInfoUuid;
constexpr ble_uuid16_t DeviceInformationService::hwRevisionUuid;
int DeviceInformationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) {
auto deviceInformationService = static_cast<DeviceInformationService*>(arg);
return deviceInformationService->OnDeviceInfoRequested(conn_handle, attr_handle, ctxt);
}
void DeviceInformationService::Init() {
ble_gatts_count_cfg(serviceDefinition);
ble_gatts_add_svcs(serviceDefinition);
}
int DeviceInformationService::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt) {
const char *str;
switch (ble_uuid_u16(ctxt->chr->uuid)) {
case manufacturerNameId:
str = manufacturerName;
break;
case modelNumberId:
str = modelNumber;
break;
case serialNumberId:
str = serialNumber;
break;
case fwRevisionId:
str = fwRevision;
break;
case hwRevisionId:
str = hwRevision;
break;
default:
return BLE_ATT_ERR_UNLIKELY;
}
int res = os_mbuf_append(ctxt->om, str, strlen(str));
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
DeviceInformationService::DeviceInformationService() :
characteristicDefinition{
{
.uuid = (ble_uuid_t *) &manufacturerNameUuid,
.access_cb = DeviceInformationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
},
{
.uuid = (ble_uuid_t *) &modelNumberUuid,
.access_cb = DeviceInformationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
},
{
.uuid = (ble_uuid_t *) &serialNumberUuid,
.access_cb = DeviceInformationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
},
{
.uuid = (ble_uuid_t *) &fwRevisionUuid,
.access_cb = DeviceInformationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
},
{
.uuid = (ble_uuid_t *) &hwRevisionUuid,
.access_cb = DeviceInformationCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
},
{
0
}
},
serviceDefinition{
{
/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = (ble_uuid_t *) &deviceInfoUuid,
.characteristics = characteristicDefinition
},
{
0
},
}
{
}

View File

@ -0,0 +1,67 @@
#pragma once
#include <cstdint>
#include <array>
#include <host/ble_gap.h>
namespace Pinetime {
namespace Controllers {
class DeviceInformationService {
public:
DeviceInformationService();
void Init();
int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt);
private:
static constexpr uint16_t deviceInfoId {0x180a};
static constexpr uint16_t manufacturerNameId {0x2a29};
static constexpr uint16_t modelNumberId {0x2a24};
static constexpr uint16_t serialNumberId {0x2a25};
static constexpr uint16_t fwRevisionId {0x2a26};
static constexpr uint16_t hwRevisionId {0x2a27};
static constexpr char* manufacturerName = "Codingfield";
static constexpr char* modelNumber = "1";
static constexpr char* serialNumber = "9.8.7.6.5.4";
static constexpr char* fwRevision = "0.5.0";
static constexpr char* hwRevision = "1.0.0";
static constexpr ble_uuid16_t deviceInfoUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = deviceInfoId
};
static constexpr ble_uuid16_t manufacturerNameUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = manufacturerNameId
};
static constexpr ble_uuid16_t modelNumberUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = modelNumberId
};
static constexpr ble_uuid16_t serialNumberUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = serialNumberId
};
static constexpr ble_uuid16_t fwRevisionUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = fwRevisionId
};
static constexpr ble_uuid16_t hwRevisionUuid {
.u {.type = BLE_UUID_TYPE_16},
.value = hwRevisionId
};
struct ble_gatt_chr_def characteristicDefinition[6];
struct ble_gatt_svc_def serviceDefinition[2];
};
}
}

View File

@ -9,127 +9,11 @@
#include <host/ble_gap.h> #include <host/ble_gap.h>
#include <hal/nrf_rtc.h> #include <hal/nrf_rtc.h>
using namespace Pinetime::Controllers; using namespace Pinetime::Controllers;
// TODO c++ify the following code // TODO c++ify the following code
// - device info should be in its own class
// - cts should be in it own class // - cts should be in it own class
#define BLE_GATT_SVC_DEVINFO (0x180a) /**< device info */
#define BLE_GATT_CHAR_MANUFACTURER_NAME (0x2a29) /**< manufacturer name */
#define BLE_GATT_CHAR_MODEL_NUMBER_STR (0x2a24) /**< model number */
#define BLE_GATT_CHAR_SERIAL_NUMBER_STR (0x2a25) /**< serial number */
#define BLE_GATT_CHAR_FW_REV_STR (0x2a26) /**< firmware revision */
#define BLE_GATT_CHAR_HW_REV_STR (0x2a27) /**< hardware revision */
static int _devinfo_handler(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg) {
const char *str;
switch (ble_uuid_u16(ctxt->chr->uuid)) {
case BLE_GATT_CHAR_MANUFACTURER_NAME:
str = "Codingfield";
break;
case BLE_GATT_CHAR_MODEL_NUMBER_STR:
str = "1";
break;
case BLE_GATT_CHAR_SERIAL_NUMBER_STR:
str = "1.2.3.4.5.6";
break;
case BLE_GATT_CHAR_FW_REV_STR:
str = "0.5.0";
break;
case BLE_GATT_CHAR_HW_REV_STR:
str = "1.0";
break;
default:
return BLE_ATT_ERR_UNLIKELY;
}
int res = os_mbuf_append(ctxt->om, str, strlen(str));
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
ble_uuid16_t deviceInfoUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = BLE_GATT_SVC_DEVINFO
};
ble_uuid16_t manufacturerNameUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = BLE_GATT_CHAR_MANUFACTURER_NAME
};
ble_uuid16_t modelNumberUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = BLE_GATT_CHAR_MODEL_NUMBER_STR
};
ble_uuid16_t serialNumberUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = BLE_GATT_CHAR_SERIAL_NUMBER_STR
};
ble_uuid16_t fwRevisionUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = BLE_GATT_CHAR_FW_REV_STR
};
ble_uuid16_t hwRevisionUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = BLE_GATT_CHAR_HW_REV_STR
};
static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
{
/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = (ble_uuid_t*)&deviceInfoUuid,
.characteristics = (struct ble_gatt_chr_def[]) {
{
.uuid = (ble_uuid_t*)&manufacturerNameUuid,
.access_cb = _devinfo_handler,
.arg = nullptr,
.flags = BLE_GATT_CHR_F_READ,
},
{
.uuid = (ble_uuid_t*)&modelNumberUuid,
.access_cb = _devinfo_handler,
.arg = nullptr,
.flags = BLE_GATT_CHR_F_READ,
},
{
.uuid = (ble_uuid_t*)&serialNumberUuid,
.access_cb = _devinfo_handler,
.arg = nullptr,
.flags = BLE_GATT_CHR_F_READ,
},
{
.uuid = (ble_uuid_t*)&fwRevisionUuid,
.access_cb = _devinfo_handler,
.arg = nullptr,
.flags = BLE_GATT_CHR_F_READ,
},
{
.uuid = (ble_uuid_t*)&hwRevisionUuid,
.access_cb = _devinfo_handler,
.arg = nullptr,
.flags = BLE_GATT_CHR_F_READ,
},
{
0, /* no more characteristics in this service */
},
}
},
{
0, /* no more services */
},
};
NimbleController::NimbleController(DateTime& datetimeController) : dateTimeController{datetimeController} { NimbleController::NimbleController(DateTime& datetimeController) : dateTimeController{datetimeController} {
ctsUuid.u.type = BLE_UUID_TYPE_16; ctsUuid.u.type = BLE_UUID_TYPE_16;
ctsUuid.value = BleGatServiceCts; ctsUuid.value = BleGatServiceCts;
@ -168,8 +52,7 @@ void NimbleController::Init() {
ble_svc_gap_init(); ble_svc_gap_init();
ble_svc_gatt_init(); ble_svc_gatt_init();
ble_gatts_count_cfg(gatt_svr_svcs); deviceInformationService.Init();
ble_gatts_add_svcs(gatt_svr_svcs);
int res; int res;
res = ble_hs_util_ensure_addr(0); res = ble_hs_util_ensure_addr(0);
res = ble_hs_id_infer_auto(0, &addrType); res = ble_hs_id_infer_auto(0, &addrType);

View File

@ -1,6 +1,8 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#include "DeviceInformationService.h"
#include <host/ble_gap.h> #include <host/ble_gap.h>
namespace Pinetime { namespace Pinetime {
@ -33,6 +35,7 @@ namespace Pinetime {
} CtsData; } CtsData;
DateTime& dateTimeController; DateTime& dateTimeController;
DeviceInformationService deviceInformationService;
ble_uuid16_t ctsUuid; ble_uuid16_t ctsUuid;