1
0
Fork 0

Nimble OTA : work in progress

This commit is contained in:
JF 2020-04-27 20:16:03 +02:00
parent 746c164c59
commit 5291bcc7de
5 changed files with 135 additions and 113 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/DfuService.cpp
drivers/Cst816s.cpp
FreeRTOS/port.c
FreeRTOS/port_cmsis_systick.c
@ -366,6 +367,7 @@ set(INCLUDE_FILES
Components/Ble/DeviceInformationService.h
Components/Ble/CurrentTimeClient.h
Components/Ble/AlertNotificationClient.h
Components/Ble/DfuService.h
drivers/Cst816s.h
FreeRTOS/portmacro.h
FreeRTOS/portmacro_cmsis.h

View file

@ -1,101 +1,122 @@
#include "DeviceInformationService.h"
#include "DfuService.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;
constexpr ble_uuid128_t DfuService::serviceUuid;
constexpr ble_uuid128_t DfuService::controlPointCharacteristicUuid;
constexpr ble_uuid128_t DfuService::revisionCharacteristicUuid;
constexpr ble_uuid128_t DfuService::packetCharacteristicUuid;
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);
int DfuServiceCallback(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt, void *arg) {
auto dfuService = static_cast<DfuService*>(arg);
return dfuService->OnServiceData(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() :
DfuService::DfuService() :
characteristicDefinition{
{
.uuid = (ble_uuid_t *) &manufacturerNameUuid,
.access_cb = DeviceInformationCallback,
.uuid = (ble_uuid_t *) &packetCharacteristicUuid,
.access_cb = DfuServiceCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
.val_handle = nullptr,
},
{
.uuid = (ble_uuid_t *) &modelNumberUuid,
.access_cb = DeviceInformationCallback,
.uuid = (ble_uuid_t *) &controlPointCharacteristicUuid,
.access_cb = DfuServiceCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY,
.val_handle = nullptr,
},
{
.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,
.uuid = (ble_uuid_t *) &revisionCharacteristicUuid,
.access_cb = DfuServiceCallback,
.arg = this,
.flags = BLE_GATT_CHR_F_READ,
.val_handle = &revision,
},
{
0
}
},
serviceDefinition {
{
/* Device Information Service */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid = (ble_uuid_t *) &deviceInfoUuid,
.uuid = (ble_uuid_t *) &serviceUuid,
.characteristics = characteristicDefinition
},
{
0
},
}
{
}
void DfuService::Init() {
ble_gatts_count_cfg(serviceDefinition);
ble_gatts_add_svcs(serviceDefinition);
}
int DfuService::OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context) {
ble_gatts_find_chr((ble_uuid_t*)&serviceUuid, (ble_uuid_t*)&packetCharacteristicUuid, nullptr, &packetCharacteristicHandle);
ble_gatts_find_chr((ble_uuid_t*)&serviceUuid, (ble_uuid_t*)&controlPointCharacteristicUuid, nullptr, &controlPointCharacteristicHandle);
ble_gatts_find_chr((ble_uuid_t*)&serviceUuid, (ble_uuid_t*)&revisionCharacteristicUuid, nullptr, &revisionCharacteristicHandle);
/* * o BLE_GATT_ACCESS_OP_READ_CHR
* o BLE_GATT_ACCESS_OP_WRITE_CHR
* o BLE_GATT_ACCESS_OP_READ_DSC
* o BLE_GATT_ACCESS_OP_WRITE_DSC
* */
char* op;
switch(context->op) {
case BLE_GATT_ACCESS_OP_READ_CHR: op = "Read Characteristic"; break;
case BLE_GATT_ACCESS_OP_WRITE_CHR: op = "Write Characteristic"; break;
case BLE_GATT_ACCESS_OP_READ_DSC: op = "Read Descriptor"; break;
case BLE_GATT_ACCESS_OP_WRITE_DSC: op = "Write Descriptor"; break;
}
if(attributeHandle == packetCharacteristicHandle) {
NRF_LOG_INFO("[DFU] Packet Characteristic : %d - %s", attributeHandle, op);
if(context->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
NRF_LOG_INFO("[DFU] -> Write %dB", context->om->om_len);
uint8_t data[3] {16, 1, 1};
auto* om = ble_hs_mbuf_from_flat(data, 3);
ble_gattc_notify_custom(connectionHandle, controlPointCharacteristicHandle, om);
}
} else if (attributeHandle == controlPointCharacteristicHandle) {
NRF_LOG_INFO("[DFU] ControlPoint Characteristic : %d - %s", attributeHandle, op);
if(context->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
NRF_LOG_INFO("[DFU] -> Write %dB", context->om->om_len);
switch(context->om->om_data[0]) {
case 0x01: {// START DFU
NRF_LOG_INFO("[DFU] -> Start DFU, mode = %d", context->om->om_data[1]);
}
break;
}
}
} else if(attributeHandle == revisionCharacteristicHandle) {
NRF_LOG_INFO("[DFU] Revision Characteristic : %d - %s", attributeHandle, op);
if(context->op == BLE_GATT_ACCESS_OP_READ_CHR) {
int res = os_mbuf_append(context->om, &revision, 2);
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
} else {
NRF_LOG_INFO("[DFU] Unknown Characteristic : %d - %s", attributeHandle, op);
}
return 0;
}

View file

@ -6,62 +6,51 @@
namespace Pinetime {
namespace Controllers {
class DeviceInformationService {
class DfuService {
public:
DeviceInformationService();
DfuService();
void Init();
int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle,
struct ble_gatt_access_ctxt *ctxt);
int OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context);
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 uint16_t dfuServiceId {0x1530};
static constexpr uint16_t packetCharacteristicId {0x1532};
static constexpr uint16_t controlPointCharacteristicId {0x1531};
static constexpr uint16_t revisionCharacteristicId {0x1534};
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";
uint16_t revision {0x0008};
static constexpr ble_uuid16_t deviceInfoUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = deviceInfoId
static constexpr uint16_t opcodeInit = 0x0002;
static constexpr ble_uuid128_t serviceUuid {
.u { .type = BLE_UUID_TYPE_128},
.value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
0xDE, 0xEF, 0x12, 0x12, 0x30, 0x15, 0x00, 0x00}
};
static constexpr ble_uuid16_t manufacturerNameUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = manufacturerNameId
static constexpr ble_uuid128_t packetCharacteristicUuid {
.u { .type = BLE_UUID_TYPE_128},
.value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
0xDE, 0xEF, 0x12, 0x12, 0x32, 0x15, 0x00, 0x00}
};
static constexpr ble_uuid16_t modelNumberUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = modelNumberId
static constexpr ble_uuid128_t controlPointCharacteristicUuid {
.u { .type = BLE_UUID_TYPE_128},
.value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
0xDE, 0xEF, 0x12, 0x12, 0x31, 0x15, 0x00, 0x00}
};
static constexpr ble_uuid16_t serialNumberUuid {
.u { .type = BLE_UUID_TYPE_16 },
.value = serialNumberId
static constexpr ble_uuid128_t revisionCharacteristicUuid {
.u { .type = BLE_UUID_TYPE_128},
.value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
0xDE, 0xEF, 0x12, 0x12, 0x34, 0x15, 0x00, 0x00}
};
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_chr_def characteristicDefinition[4];
struct ble_gatt_svc_def serviceDefinition[2];
uint16_t packetCharacteristicHandle;
uint16_t controlPointCharacteristicHandle;
uint16_t revisionCharacteristicHandle;
};
}
}

View file

@ -73,7 +73,8 @@ void NimbleController::Init() {
ble_svc_gatt_init();
deviceInformationService.Init();
currentTimeClient.Init();
// currentTimeClient.Init();
dfuService.Init();
int res;
res = ble_hs_util_ensure_addr(0);
res = ble_hs_id_infer_auto(0, &addrType);
@ -105,8 +106,9 @@ void NimbleController::StartAdvertising() {
// fields.uuids128 = BLE_UUID128(BLE_UUID128_DECLARE(
// 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
// 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff));
fields.num_uuids128 = 0;
fields.uuids128_is_complete = 0;;
fields.uuids128 = &dfuServiceUuid;
fields.num_uuids128 = 1;
fields.uuids128_is_complete = 1;
fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
rsp_fields.name = (uint8_t *)"Pinetime-JF";
@ -155,7 +157,7 @@ int NimbleController::OnGAPEvent(ble_gap_event *event) {
} else {
bleController.Connect();
connectionHandle = event->connect.conn_handle;
ble_gattc_disc_all_svcs(connectionHandle, OnAllSvrDisco, this);
// ble_gattc_disc_all_svcs(connectionHandle, OnAllSvrDisco, this);
}
}
break;

View file

@ -4,6 +4,7 @@
#include "AlertNotificationClient.h"
#include "DeviceInformationService.h"
#include "CurrentTimeClient.h"
#include "DfuService.h"
#include <host/ble_gap.h>
namespace Pinetime {
@ -31,12 +32,19 @@ namespace Pinetime {
Pinetime::Controllers::Ble& bleController;
DateTime& dateTimeController;
Pinetime::Controllers::NotificationManager& notificationManager;
DfuService dfuService;
DeviceInformationService deviceInformationService;
CurrentTimeClient currentTimeClient;
AlertNotificationClient alertNotificationClient;
uint8_t addrType;
uint16_t connectionHandle;
ble_uuid128_t dfuServiceUuid {
.u { .type = BLE_UUID_TYPE_128},
.value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
0xDE, 0xEF, 0x12, 0x12, 0x30, 0x15, 0x00, 0x00}
};
};
}
}