2020-04-23 18:34:38 +00:00
|
|
|
#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;
|
2020-09-02 19:31:31 +00:00
|
|
|
constexpr ble_uuid16_t DeviceInformationService::swRevisionUuid;
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
int DeviceInformationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
|
2020-04-23 18:34:38 +00:00
|
|
|
auto deviceInformationService = static_cast<DeviceInformationService*>(arg);
|
|
|
|
return deviceInformationService->OnDeviceInfoRequested(conn_handle, attr_handle, ctxt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceInformationService::Init() {
|
2020-06-16 18:36:24 +00:00
|
|
|
int res = 0;
|
|
|
|
res = ble_gatts_count_cfg(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
|
|
|
|
|
|
|
res = ble_gatts_add_svcs(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
2020-04-23 18:34:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
int DeviceInformationService::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) {
|
|
|
|
const char* str;
|
2020-04-23 18:34:38 +00:00
|
|
|
|
|
|
|
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;
|
2020-09-02 19:31:31 +00:00
|
|
|
case swRevisionId:
|
|
|
|
str = swRevision;
|
|
|
|
break;
|
2020-04-23 18:34:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
DeviceInformationService::DeviceInformationService()
|
|
|
|
: characteristicDefinition {{
|
2021-07-24 15:40:06 +00:00
|
|
|
.uuid = &manufacturerNameUuid.u,
|
2021-04-18 17:28:14 +00:00
|
|
|
.access_cb = DeviceInformationCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_READ,
|
|
|
|
},
|
|
|
|
{
|
2021-07-24 15:40:06 +00:00
|
|
|
.uuid = &modelNumberUuid.u,
|
2021-04-18 17:28:14 +00:00
|
|
|
.access_cb = DeviceInformationCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_READ,
|
|
|
|
},
|
|
|
|
{
|
2021-07-24 15:40:06 +00:00
|
|
|
.uuid = &serialNumberUuid.u,
|
2021-04-18 17:28:14 +00:00
|
|
|
.access_cb = DeviceInformationCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_READ,
|
|
|
|
},
|
|
|
|
{
|
2021-07-24 15:40:06 +00:00
|
|
|
.uuid = &fwRevisionUuid.u,
|
2021-04-18 17:28:14 +00:00
|
|
|
.access_cb = DeviceInformationCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_READ,
|
|
|
|
},
|
|
|
|
{
|
2021-07-24 15:40:06 +00:00
|
|
|
.uuid = &hwRevisionUuid.u,
|
2021-04-18 17:28:14 +00:00
|
|
|
.access_cb = DeviceInformationCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_READ,
|
|
|
|
},
|
|
|
|
{
|
2021-07-24 15:40:06 +00:00
|
|
|
.uuid = &swRevisionUuid.u,
|
2021-04-18 17:28:14 +00:00
|
|
|
.access_cb = DeviceInformationCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_READ,
|
|
|
|
},
|
|
|
|
{0}},
|
|
|
|
serviceDefinition {
|
|
|
|
{/* Device Information Service */
|
|
|
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
2021-07-24 15:40:06 +00:00
|
|
|
.uuid = &deviceInfoUuid.u,
|
2021-04-18 17:28:14 +00:00
|
|
|
.characteristics = characteristicDefinition},
|
|
|
|
{0},
|
|
|
|
} {
|
2020-04-23 18:34:38 +00:00
|
|
|
}
|