2021-10-13 16:08:35 -04:00
|
|
|
#include "components/ble/DfuService.h"
|
2020-05-20 12:39:25 -04:00
|
|
|
#include <cstring>
|
2020-10-02 15:16:48 -04:00
|
|
|
#include "components/ble/BleController.h"
|
2020-11-15 09:05:51 -05:00
|
|
|
#include "drivers/SpiNorFlash.h"
|
2020-10-02 15:16:48 -04:00
|
|
|
#include "systemtask/SystemTask.h"
|
2022-02-20 08:45:59 -05:00
|
|
|
#include <nrf_log.h>
|
2020-04-26 04:25:59 -04:00
|
|
|
|
|
|
|
using namespace Pinetime::Controllers;
|
|
|
|
|
2020-04-27 14:16:03 -04:00
|
|
|
constexpr ble_uuid128_t DfuService::serviceUuid;
|
|
|
|
constexpr ble_uuid128_t DfuService::controlPointCharacteristicUuid;
|
|
|
|
constexpr ble_uuid128_t DfuService::revisionCharacteristicUuid;
|
|
|
|
constexpr ble_uuid128_t DfuService::packetCharacteristicUuid;
|
2020-04-26 04:25:59 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int DfuServiceCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) {
|
|
|
|
auto dfuService = static_cast<DfuService*>(arg);
|
2020-04-27 14:16:03 -04:00
|
|
|
return dfuService->OnServiceData(conn_handle, attr_handle, ctxt);
|
2020-04-26 04:25:59 -04:00
|
|
|
}
|
|
|
|
|
2020-06-01 10:30:24 -04:00
|
|
|
void NotificationTimerCallback(TimerHandle_t xTimer) {
|
2021-04-18 13:28:14 -04:00
|
|
|
auto notificationManager = static_cast<DfuService::NotificationManager*>(pvTimerGetTimerID(xTimer));
|
2020-06-01 11:35:28 -04:00
|
|
|
notificationManager->OnNotificationTimer();
|
2020-06-01 03:22:54 -04:00
|
|
|
}
|
|
|
|
|
2020-06-01 10:30:24 -04:00
|
|
|
void TimeoutTimerCallback(TimerHandle_t xTimer) {
|
2021-04-18 13:28:14 -04:00
|
|
|
auto dfuService = static_cast<DfuService*>(pvTimerGetTimerID(xTimer));
|
2020-06-01 10:30:24 -04:00
|
|
|
dfuService->OnTimeout();
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
DfuService::DfuService(Pinetime::System::SystemTask& systemTask,
|
|
|
|
Pinetime::Controllers::Ble& bleController,
|
|
|
|
Pinetime::Drivers::SpiNorFlash& spiNorFlash)
|
|
|
|
: systemTask {systemTask},
|
|
|
|
bleController {bleController},
|
|
|
|
dfuImage {spiNorFlash},
|
|
|
|
characteristicDefinition {{
|
2021-07-24 11:40:06 -04:00
|
|
|
.uuid = &packetCharacteristicUuid.u,
|
2021-04-18 13:28:14 -04:00
|
|
|
.access_cb = DfuServiceCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE_NO_RSP,
|
|
|
|
.val_handle = nullptr,
|
|
|
|
},
|
|
|
|
{
|
2021-07-24 11:40:06 -04:00
|
|
|
.uuid = &controlPointCharacteristicUuid.u,
|
2021-04-18 13:28:14 -04:00
|
|
|
.access_cb = DfuServiceCallback,
|
|
|
|
.arg = this,
|
|
|
|
.flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_NOTIFY,
|
|
|
|
.val_handle = nullptr,
|
|
|
|
},
|
|
|
|
{
|
2021-07-24 11:40:06 -04:00
|
|
|
.uuid = &revisionCharacteristicUuid.u,
|
2021-04-18 13:28:14 -04:00
|
|
|
.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,
|
2021-07-24 11:40:06 -04:00
|
|
|
.uuid = &serviceUuid.u,
|
2021-04-18 13:28:14 -04:00
|
|
|
.characteristics = characteristicDefinition},
|
|
|
|
{0},
|
|
|
|
} {
|
|
|
|
timeoutTimer = xTimerCreate("notificationTimer", 10000, pdFALSE, this, TimeoutTimerCallback);
|
2020-04-27 14:16:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DfuService::Init() {
|
2020-06-16 14:36:24 -04:00
|
|
|
int res;
|
|
|
|
res = ble_gatts_count_cfg(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
|
|
|
|
|
|
|
res = ble_gatts_add_svcs(serviceDefinition);
|
|
|
|
ASSERT(res == 0);
|
2020-04-26 04:25:59 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int DfuService::OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt* context) {
|
|
|
|
if (bleController.IsFirmwareUpdating()) {
|
2020-06-01 10:30:24 -04:00
|
|
|
xTimerStart(timeoutTimer, 0);
|
|
|
|
}
|
|
|
|
|
2021-07-24 11:40:06 -04:00
|
|
|
ble_gatts_find_chr(&serviceUuid.u, &packetCharacteristicUuid.u, nullptr, &packetCharacteristicHandle);
|
|
|
|
ble_gatts_find_chr(&serviceUuid.u, &controlPointCharacteristicUuid.u, nullptr, &controlPointCharacteristicHandle);
|
|
|
|
ble_gatts_find_chr(&serviceUuid.u, &revisionCharacteristicUuid.u, nullptr, &revisionCharacteristicHandle);
|
2020-04-27 14:16:03 -04:00
|
|
|
|
2020-06-01 03:22:54 -04:00
|
|
|
if (attributeHandle == packetCharacteristicHandle) {
|
|
|
|
if (context->op == BLE_GATT_ACCESS_OP_WRITE_CHR)
|
2020-05-01 11:58:10 -04:00
|
|
|
return WritePacketHandler(connectionHandle, context->om);
|
2021-04-18 13:28:14 -04:00
|
|
|
else
|
|
|
|
return 0;
|
2020-06-01 03:22:54 -04:00
|
|
|
} else if (attributeHandle == controlPointCharacteristicHandle) {
|
|
|
|
if (context->op == BLE_GATT_ACCESS_OP_WRITE_CHR)
|
|
|
|
return ControlPointHandler(connectionHandle, context->om);
|
2021-04-18 13:28:14 -04:00
|
|
|
else
|
|
|
|
return 0;
|
2020-06-01 03:22:54 -04:00
|
|
|
} else if (attributeHandle == revisionCharacteristicHandle) {
|
|
|
|
if (context->op == BLE_GATT_ACCESS_OP_READ_CHR)
|
2020-05-01 11:58:10 -04:00
|
|
|
return SendDfuRevision(context->om);
|
2021-04-18 13:28:14 -04:00
|
|
|
else
|
|
|
|
return 0;
|
2020-05-01 11:58:10 -04:00
|
|
|
} else {
|
|
|
|
NRF_LOG_INFO("[DFU] Unknown Characteristic : %d", attributeHandle);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 09:36:48 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int DfuService::SendDfuRevision(os_mbuf* om) const {
|
2020-05-01 11:58:10 -04:00
|
|
|
int res = os_mbuf_append(om, &revision, sizeof(revision));
|
|
|
|
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
|
|
|
|
}
|
2020-05-01 09:36:48 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int DfuService::WritePacketHandler(uint16_t connectionHandle, os_mbuf* om) {
|
2020-06-01 03:22:54 -04:00
|
|
|
switch (state) {
|
2020-05-01 11:58:10 -04:00
|
|
|
case States::Start: {
|
2020-05-03 09:48:42 -04:00
|
|
|
softdeviceSize = om->om_data[0] + (om->om_data[1] << 8) + (om->om_data[2] << 16) + (om->om_data[3] << 24);
|
|
|
|
bootloaderSize = om->om_data[4] + (om->om_data[5] << 8) + (om->om_data[6] << 16) + (om->om_data[7] << 24);
|
|
|
|
applicationSize = om->om_data[8] + (om->om_data[9] << 8) + (om->om_data[10] << 16) + (om->om_data[11] << 24);
|
2020-05-20 12:39:25 -04:00
|
|
|
bleController.FirmwareUpdateTotalBytes(applicationSize);
|
2022-05-09 11:14:42 -04:00
|
|
|
NRF_LOG_INFO("[DFU] -> Start data received : SD size : %d, BT size : %d, app size : %d",
|
|
|
|
softdeviceSize,
|
|
|
|
bootloaderSize,
|
|
|
|
applicationSize);
|
2020-05-03 08:27:48 -04:00
|
|
|
|
2021-06-26 14:53:32 -04:00
|
|
|
// wait until SystemTask has finished waking up all devices
|
|
|
|
while (systemTask.IsSleeping()) {
|
|
|
|
vTaskDelay(50); // 50ms
|
|
|
|
}
|
|
|
|
|
2020-06-01 12:32:46 -04:00
|
|
|
dfuImage.Erase();
|
2020-05-11 12:50:37 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
uint8_t data[] {16, 1, 1};
|
2020-06-01 11:35:28 -04:00
|
|
|
notificationManager.Send(connectionHandle, controlPointCharacteristicHandle, data, 3);
|
2020-05-03 08:27:48 -04:00
|
|
|
state = States::Init;
|
2020-05-01 11:58:10 -04:00
|
|
|
}
|
|
|
|
return 0;
|
2020-05-03 09:48:42 -04:00
|
|
|
case States::Init: {
|
|
|
|
uint16_t deviceType = om->om_data[0] + (om->om_data[1] << 8);
|
|
|
|
uint16_t deviceRevision = om->om_data[2] + (om->om_data[3] << 8);
|
2021-04-18 13:28:14 -04:00
|
|
|
uint32_t applicationVersion = om->om_data[4] + (om->om_data[5] << 8) + (om->om_data[6] << 16) + (om->om_data[7] << 24);
|
2020-05-03 09:48:42 -04:00
|
|
|
uint16_t softdeviceArrayLength = om->om_data[8] + (om->om_data[9] << 8);
|
|
|
|
uint16_t sd[softdeviceArrayLength];
|
2020-06-01 03:22:54 -04:00
|
|
|
for (int i = 0; i < softdeviceArrayLength; i++) {
|
|
|
|
sd[i] = om->om_data[10 + (i * 2)] + (om->om_data[10 + (i * 2) + 1] << 8);
|
2020-05-03 09:48:42 -04:00
|
|
|
}
|
2021-04-18 13:28:14 -04:00
|
|
|
expectedCrc = om->om_data[10 + (softdeviceArrayLength * 2)] + (om->om_data[10 + (softdeviceArrayLength * 2) + 1] << 8);
|
2020-05-03 09:48:42 -04:00
|
|
|
|
2020-06-01 03:22:54 -04:00
|
|
|
NRF_LOG_INFO(
|
2021-04-18 13:28:14 -04:00
|
|
|
"[DFU] -> Init data received : deviceType = %d, deviceRevision = %d, applicationVersion = %d, nb SD = %d, First SD = %d, CRC = %u",
|
|
|
|
deviceType,
|
|
|
|
deviceRevision,
|
|
|
|
applicationVersion,
|
|
|
|
softdeviceArrayLength,
|
|
|
|
sd[0],
|
|
|
|
expectedCrc);
|
2020-05-03 09:48:42 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 11:58:10 -04:00
|
|
|
case States::Data: {
|
|
|
|
nbPacketReceived++;
|
2020-06-01 12:32:46 -04:00
|
|
|
dfuImage.Append(om->om_data, om->om_len);
|
2020-05-01 11:58:10 -04:00
|
|
|
bytesReceived += om->om_len;
|
2020-05-02 08:16:57 -04:00
|
|
|
bleController.FirmwareUpdateCurrentBytes(bytesReceived);
|
2020-05-11 12:50:37 -04:00
|
|
|
|
2020-06-01 03:22:54 -04:00
|
|
|
if ((nbPacketReceived % nbPacketsToNotify) == 0 && bytesReceived != applicationSize) {
|
2021-04-18 13:28:14 -04:00
|
|
|
uint8_t data[5] {static_cast<uint8_t>(Opcodes::PacketReceiptNotification),
|
2022-05-09 11:14:42 -04:00
|
|
|
static_cast<uint8_t>(bytesReceived & 0x000000FFu),
|
|
|
|
static_cast<uint8_t>(bytesReceived >> 8u),
|
|
|
|
static_cast<uint8_t>(bytesReceived >> 16u),
|
|
|
|
static_cast<uint8_t>(bytesReceived >> 24u)};
|
2020-06-01 03:22:54 -04:00
|
|
|
NRF_LOG_INFO("[DFU] -> Send packet notification: %d bytes received", bytesReceived);
|
2020-06-01 11:35:28 -04:00
|
|
|
notificationManager.Send(connectionHandle, controlPointCharacteristicHandle, data, 5);
|
2020-05-01 11:58:10 -04:00
|
|
|
}
|
2020-06-01 12:32:46 -04:00
|
|
|
if (dfuImage.IsComplete()) {
|
2021-04-18 13:28:14 -04:00
|
|
|
uint8_t data[3] {static_cast<uint8_t>(Opcodes::Response),
|
|
|
|
static_cast<uint8_t>(Opcodes::ReceiveFirmwareImage),
|
|
|
|
static_cast<uint8_t>(ErrorCodes::NoError)};
|
2020-06-01 10:30:24 -04:00
|
|
|
NRF_LOG_INFO("[DFU] -> Send packet notification : all bytes received!");
|
2020-06-01 11:35:28 -04:00
|
|
|
notificationManager.Send(connectionHandle, controlPointCharacteristicHandle, data, 3);
|
2020-05-01 11:58:10 -04:00
|
|
|
state = States::Validate;
|
2020-05-01 09:36:48 -04:00
|
|
|
}
|
2020-05-01 11:58:10 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
// Invalid state
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-01 09:36:48 -04:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
int DfuService::ControlPointHandler(uint16_t connectionHandle, os_mbuf* om) {
|
2020-05-01 11:58:10 -04:00
|
|
|
auto opcode = static_cast<Opcodes>(om->om_data[0]);
|
2020-05-03 08:27:48 -04:00
|
|
|
NRF_LOG_INFO("[DFU] -> ControlPointHandler");
|
|
|
|
|
2020-06-01 03:22:54 -04:00
|
|
|
switch (opcode) {
|
2020-05-01 11:58:10 -04:00
|
|
|
case Opcodes::StartDFU: {
|
2020-06-01 03:22:54 -04:00
|
|
|
if (state != States::Idle && state != States::Start) {
|
2020-05-01 11:58:10 -04:00
|
|
|
NRF_LOG_INFO("[DFU] -> Start DFU requested, but we are not in Idle state");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-06-01 03:22:54 -04:00
|
|
|
if (state == States::Start) {
|
2020-05-03 08:27:48 -04:00
|
|
|
NRF_LOG_INFO("[DFU] -> Start DFU requested, but we are already in Start state");
|
|
|
|
return 0;
|
|
|
|
}
|
2020-05-01 11:58:10 -04:00
|
|
|
auto imageType = static_cast<ImageTypes>(om->om_data[1]);
|
2020-06-01 03:22:54 -04:00
|
|
|
if (imageType == ImageTypes::Application) {
|
2020-05-01 11:58:10 -04:00
|
|
|
NRF_LOG_INFO("[DFU] -> Start DFU, mode = Application");
|
|
|
|
state = States::Start;
|
2020-05-02 08:16:57 -04:00
|
|
|
bleController.StartFirmwareUpdate();
|
2020-06-01 03:22:54 -04:00
|
|
|
bleController.State(Pinetime::Controllers::Ble::FirmwareUpdateStates::Running);
|
2020-05-20 12:39:25 -04:00
|
|
|
bleController.FirmwareUpdateTotalBytes(0xffffffffu);
|
2020-05-02 08:16:57 -04:00
|
|
|
bleController.FirmwareUpdateCurrentBytes(0);
|
2021-06-06 09:56:03 -04:00
|
|
|
systemTask.PushMessage(Pinetime::System::Messages::BleFirmwareUpdateStarted);
|
2020-05-01 11:58:10 -04:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
NRF_LOG_INFO("[DFU] -> Start DFU, mode %d not supported!", imageType);
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-18 13:28:14 -04:00
|
|
|
} break;
|
2020-05-01 11:58:10 -04:00
|
|
|
case Opcodes::InitDFUParameters: {
|
2020-05-03 08:27:48 -04:00
|
|
|
if (state != States::Init) {
|
|
|
|
NRF_LOG_INFO("[DFU] -> Init DFU requested, but we are not in Init state");
|
2020-05-01 11:58:10 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
bool isInitComplete = (om->om_data[1] != 0);
|
|
|
|
NRF_LOG_INFO("[DFU] -> Init DFU parameters %s", isInitComplete ? " complete" : " not complete");
|
|
|
|
|
|
|
|
if (isInitComplete) {
|
2021-04-18 13:28:14 -04:00
|
|
|
uint8_t data[3] {static_cast<uint8_t>(Opcodes::Response),
|
|
|
|
static_cast<uint8_t>(Opcodes::InitDFUParameters),
|
|
|
|
(isInitComplete ? uint8_t {1} : uint8_t {0})};
|
2020-06-01 11:35:28 -04:00
|
|
|
notificationManager.AsyncSend(connectionHandle, controlPointCharacteristicHandle, data, 3);
|
2020-05-01 11:58:10 -04:00
|
|
|
return 0;
|
2020-04-27 14:16:03 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-01 11:58:10 -04:00
|
|
|
return 0;
|
|
|
|
case Opcodes::PacketReceiptNotificationRequest:
|
|
|
|
nbPacketsToNotify = om->om_data[1];
|
|
|
|
NRF_LOG_INFO("[DFU] -> Receive Packet Notification Request, nb packet = %d", nbPacketsToNotify);
|
|
|
|
return 0;
|
|
|
|
case Opcodes::ReceiveFirmwareImage:
|
2020-06-01 03:22:54 -04:00
|
|
|
if (state != States::Init) {
|
2020-05-03 08:27:48 -04:00
|
|
|
NRF_LOG_INFO("[DFU] -> Receive firmware image requested, but we are not in Start Init");
|
2020-05-01 11:58:10 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-01 12:32:46 -04:00
|
|
|
// TODO the chunk size is dependant of the implementation of the host application...
|
|
|
|
dfuImage.Init(20, applicationSize, expectedCrc);
|
2020-05-01 11:58:10 -04:00
|
|
|
NRF_LOG_INFO("[DFU] -> Starting receive firmware");
|
|
|
|
state = States::Data;
|
|
|
|
return 0;
|
|
|
|
case Opcodes::ValidateFirmware: {
|
2020-06-01 03:22:54 -04:00
|
|
|
if (state != States::Validate) {
|
2020-06-01 10:30:24 -04:00
|
|
|
NRF_LOG_INFO("[DFU] -> Validate firmware image requested, but we are not in Data state %d", state);
|
2020-05-01 11:58:10 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-01 03:22:54 -04:00
|
|
|
|
|
|
|
NRF_LOG_INFO("[DFU] -> Validate firmware image requested -- %d", connectionHandle);
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
if (dfuImage.Validate()) {
|
2020-06-01 03:22:54 -04:00
|
|
|
state = States::Validated;
|
2020-06-01 12:32:46 -04:00
|
|
|
bleController.State(Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated);
|
|
|
|
NRF_LOG_INFO("Image OK");
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
uint8_t data[3] {static_cast<uint8_t>(Opcodes::Response),
|
|
|
|
static_cast<uint8_t>(Opcodes::ValidateFirmware),
|
|
|
|
static_cast<uint8_t>(ErrorCodes::NoError)};
|
2020-06-01 11:35:28 -04:00
|
|
|
notificationManager.AsyncSend(connectionHandle, controlPointCharacteristicHandle, data, 3);
|
2020-06-01 03:22:54 -04:00
|
|
|
} else {
|
2020-06-01 12:32:46 -04:00
|
|
|
NRF_LOG_INFO("Image Error : bad CRC");
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
uint8_t data[3] {static_cast<uint8_t>(Opcodes::Response),
|
|
|
|
static_cast<uint8_t>(Opcodes::ValidateFirmware),
|
|
|
|
static_cast<uint8_t>(ErrorCodes::CrcError)};
|
2020-06-01 11:35:28 -04:00
|
|
|
notificationManager.AsyncSend(connectionHandle, controlPointCharacteristicHandle, data, 3);
|
2021-07-22 15:57:45 -04:00
|
|
|
bleController.State(Pinetime::Controllers::Ble::FirmwareUpdateStates::Error);
|
|
|
|
Reset();
|
2020-06-01 03:22:54 -04:00
|
|
|
}
|
|
|
|
|
2020-05-01 11:58:10 -04:00
|
|
|
return 0;
|
2020-04-27 14:16:03 -04:00
|
|
|
}
|
2020-05-01 11:58:10 -04:00
|
|
|
case Opcodes::ActivateImageAndReset:
|
2020-06-01 03:22:54 -04:00
|
|
|
if (state != States::Validated) {
|
2020-05-01 11:58:10 -04:00
|
|
|
NRF_LOG_INFO("[DFU] -> Activate image and reset requested, but we are not in Validated state");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
NRF_LOG_INFO("[DFU] -> Activate image and reset!");
|
2020-06-01 10:30:24 -04:00
|
|
|
bleController.State(Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated);
|
2021-07-22 15:57:45 -04:00
|
|
|
Reset();
|
2020-05-01 11:58:10 -04:00
|
|
|
return 0;
|
2020-06-01 03:22:54 -04:00
|
|
|
default:
|
|
|
|
return 0;
|
2020-04-27 14:16:03 -04:00
|
|
|
}
|
2020-05-01 11:58:10 -04:00
|
|
|
}
|
2020-04-27 14:16:03 -04:00
|
|
|
|
2020-06-01 10:30:24 -04:00
|
|
|
void DfuService::OnTimeout() {
|
2021-07-22 15:57:45 -04:00
|
|
|
bleController.State(Pinetime::Controllers::Ble::FirmwareUpdateStates::Error);
|
2020-06-01 10:30:24 -04:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DfuService::Reset() {
|
|
|
|
state = States::Idle;
|
|
|
|
nbPacketsToNotify = 0;
|
|
|
|
nbPacketReceived = 0;
|
|
|
|
bytesReceived = 0;
|
|
|
|
softdeviceSize = 0;
|
|
|
|
bootloaderSize = 0;
|
|
|
|
applicationSize = 0;
|
|
|
|
expectedCrc = 0;
|
2020-06-01 11:35:28 -04:00
|
|
|
notificationManager.Reset();
|
2020-06-01 10:30:24 -04:00
|
|
|
bleController.StopFirmwareUpdate();
|
2021-06-06 09:56:03 -04:00
|
|
|
systemTask.PushMessage(Pinetime::System::Messages::BleFirmwareUpdateFinished);
|
2020-06-01 10:30:24 -04:00
|
|
|
}
|
2020-06-01 11:35:28 -04:00
|
|
|
|
|
|
|
DfuService::NotificationManager::NotificationManager() {
|
2021-04-18 13:28:14 -04:00
|
|
|
timer = xTimerCreate("notificationTimer", 1000, pdFALSE, this, NotificationTimerCallback);
|
2020-06-01 11:35:28 -04:00
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
bool DfuService::NotificationManager::AsyncSend(uint16_t connection, uint16_t charactHandle, uint8_t* data, size_t s) {
|
|
|
|
if (size != 0 || s > 10)
|
2020-06-01 11:35:28 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
connectionHandle = connection;
|
|
|
|
characteristicHandle = charactHandle;
|
|
|
|
size = s;
|
|
|
|
std::memcpy(buffer, data, size);
|
|
|
|
xTimerStart(timer, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DfuService::NotificationManager::OnNotificationTimer() {
|
2021-04-18 13:28:14 -04:00
|
|
|
if (size > 0) {
|
2020-06-01 11:35:28 -04:00
|
|
|
Send(connectionHandle, characteristicHandle, buffer, size);
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
void DfuService::NotificationManager::Send(uint16_t connection, uint16_t charactHandle, const uint8_t* data, const size_t s) {
|
|
|
|
auto* om = ble_hs_mbuf_from_flat(data, s);
|
2020-06-01 11:35:28 -04:00
|
|
|
auto ret = ble_gattc_notify_custom(connection, charactHandle, om);
|
|
|
|
ASSERT(ret == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DfuService::NotificationManager::Reset() {
|
|
|
|
connectionHandle = 0;
|
|
|
|
characteristicHandle = 0;
|
|
|
|
size = 0;
|
|
|
|
xTimerStop(timer, 0);
|
|
|
|
}
|
2020-06-01 12:32:46 -04:00
|
|
|
|
|
|
|
void DfuService::DfuImage::Init(size_t chunkSize, size_t totalSize, uint16_t expectedCrc) {
|
2021-04-18 13:28:14 -04:00
|
|
|
if (chunkSize != 20)
|
|
|
|
return;
|
2020-06-01 12:32:46 -04:00
|
|
|
this->chunkSize = chunkSize;
|
|
|
|
this->totalSize = totalSize;
|
|
|
|
this->expectedCrc = expectedCrc;
|
|
|
|
this->ready = true;
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
void DfuService::DfuImage::Append(uint8_t* data, size_t size) {
|
|
|
|
if (!ready)
|
|
|
|
return;
|
2020-06-01 12:32:46 -04:00
|
|
|
ASSERT(size <= 20);
|
|
|
|
|
|
|
|
std::memcpy(tempBuffer + bufferWriteIndex, data, size);
|
|
|
|
bufferWriteIndex += size;
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
if (bufferWriteIndex == bufferSize) {
|
2020-06-01 12:32:46 -04:00
|
|
|
spiNorFlash.Write(writeOffset + totalWriteIndex, tempBuffer, bufferWriteIndex);
|
|
|
|
totalWriteIndex += bufferWriteIndex;
|
|
|
|
bufferWriteIndex = 0;
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
if (bufferWriteIndex > 0 && totalWriteIndex + bufferWriteIndex == totalSize) {
|
2020-06-01 12:32:46 -04:00
|
|
|
spiNorFlash.Write(writeOffset + totalWriteIndex, tempBuffer, bufferWriteIndex);
|
|
|
|
totalWriteIndex += bufferWriteIndex;
|
2020-06-07 08:10:17 -04:00
|
|
|
if (totalSize < maxSize)
|
2020-06-01 12:32:46 -04:00
|
|
|
WriteMagicNumber();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DfuService::DfuImage::WriteMagicNumber() {
|
2021-04-18 13:28:14 -04:00
|
|
|
uint32_t magic[4] = {
|
|
|
|
// TODO When this variable is a static constexpr, the values written to the memory are not correct. Why?
|
|
|
|
0xf395c277,
|
|
|
|
0x7fefd260,
|
|
|
|
0x0f505235,
|
|
|
|
0x8079b62c,
|
2020-06-01 12:32:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t offset = writeOffset + (maxSize - (4 * sizeof(uint32_t)));
|
2021-04-18 13:28:14 -04:00
|
|
|
spiNorFlash.Write(offset, reinterpret_cast<const uint8_t*>(magic), 4 * sizeof(uint32_t));
|
2020-06-01 12:32:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void DfuService::DfuImage::Erase() {
|
2020-08-17 10:31:00 -04:00
|
|
|
for (size_t erased = 0; erased < maxSize; erased += 0x1000) {
|
2020-06-01 12:32:46 -04:00
|
|
|
spiNorFlash.SectorErase(writeOffset + erased);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DfuService::DfuImage::Validate() {
|
|
|
|
uint32_t chunkSize = 200;
|
2020-08-17 10:31:00 -04:00
|
|
|
size_t currentOffset = 0;
|
2020-06-01 12:32:46 -04:00
|
|
|
uint16_t crc = 0;
|
|
|
|
|
|
|
|
bool first = true;
|
|
|
|
while (currentOffset < totalSize) {
|
|
|
|
uint32_t readSize = (totalSize - currentOffset) > chunkSize ? chunkSize : (totalSize - currentOffset);
|
|
|
|
|
|
|
|
spiNorFlash.Read(writeOffset + currentOffset, tempBuffer, readSize);
|
|
|
|
if (first) {
|
|
|
|
crc = ComputeCrc(tempBuffer, readSize, NULL);
|
|
|
|
first = false;
|
|
|
|
} else
|
|
|
|
crc = ComputeCrc(tempBuffer, readSize, &crc);
|
|
|
|
currentOffset += readSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (crc == expectedCrc);
|
|
|
|
}
|
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
uint16_t DfuService::DfuImage::ComputeCrc(uint8_t const* p_data, uint32_t size, uint16_t const* p_crc) {
|
2020-06-01 12:32:46 -04:00
|
|
|
uint16_t crc = (p_crc == NULL) ? 0xFFFF : *p_crc;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < size; i++) {
|
2022-05-09 11:14:42 -04:00
|
|
|
crc = static_cast<uint8_t>(crc >> 8) | (crc << 8);
|
2020-06-01 12:32:46 -04:00
|
|
|
crc ^= p_data[i];
|
2022-05-09 11:14:42 -04:00
|
|
|
crc ^= static_cast<uint8_t>(crc & 0xFF) >> 4;
|
2020-06-01 12:32:46 -04:00
|
|
|
crc ^= (crc << 8) << 4;
|
|
|
|
crc ^= ((crc & 0xFF) << 4) << 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DfuService::DfuImage::IsComplete() {
|
2021-04-18 13:28:14 -04:00
|
|
|
if (!ready)
|
|
|
|
return false;
|
2020-06-01 12:32:46 -04:00
|
|
|
return totalWriteIndex == totalSize;
|
|
|
|
}
|