From bdc10744fb338ae197692713a0b48a7ccc36f566 Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 26 Apr 2020 10:25:59 +0200 Subject: Add Nimble in libs directory --- src/Components/Ble/DfuService.cpp | 101 ++++++++++++++++++++++++++++++++++++++ src/Components/Ble/DfuService.h | 67 +++++++++++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 src/Components/Ble/DfuService.cpp create mode 100644 src/Components/Ble/DfuService.h (limited to 'src/Components/Ble') diff --git a/src/Components/Ble/DfuService.cpp b/src/Components/Ble/DfuService.cpp new file mode 100644 index 00000000..3099b1bf --- /dev/null +++ b/src/Components/Ble/DfuService.cpp @@ -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(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 + }, + } + { + +} + diff --git a/src/Components/Ble/DfuService.h b/src/Components/Ble/DfuService.h new file mode 100644 index 00000000..6249893d --- /dev/null +++ b/src/Components/Ble/DfuService.h @@ -0,0 +1,67 @@ +#pragma once +#include +#include + +#include + +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]; + + + }; + } +} \ No newline at end of file -- cgit v1.2.3