2020-04-26 08:25:59 +00:00
|
|
|
#pragma once
|
2020-06-01 07:22:54 +00:00
|
|
|
|
2020-04-26 08:25:59 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
#include <host/ble_gap.h>
|
|
|
|
|
|
|
|
namespace Pinetime {
|
2020-05-02 12:16:57 +00:00
|
|
|
namespace System {
|
|
|
|
class SystemTask;
|
|
|
|
}
|
2020-05-11 16:50:37 +00:00
|
|
|
namespace Drivers {
|
|
|
|
class SpiNorFlash;
|
|
|
|
}
|
2020-04-26 08:25:59 +00:00
|
|
|
namespace Controllers {
|
2020-05-02 12:16:57 +00:00
|
|
|
class Ble;
|
2020-06-01 07:22:54 +00:00
|
|
|
|
2020-04-27 18:16:03 +00:00
|
|
|
class DfuService {
|
2020-04-26 08:25:59 +00:00
|
|
|
public:
|
2020-06-01 07:22:54 +00:00
|
|
|
DfuService(Pinetime::System::SystemTask &systemTask, Pinetime::Controllers::Ble &bleController,
|
|
|
|
Pinetime::Drivers::SpiNorFlash &spiNorFlash);
|
|
|
|
|
2020-04-26 08:25:59 +00:00
|
|
|
void Init();
|
2020-06-01 07:22:54 +00:00
|
|
|
|
|
|
|
bool Validate();
|
2020-04-26 08:25:59 +00:00
|
|
|
|
2020-04-27 18:16:03 +00:00
|
|
|
int OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context);
|
2020-06-01 07:22:54 +00:00
|
|
|
void OnNotificationTimer();
|
2020-06-01 14:30:24 +00:00
|
|
|
void OnTimeout();
|
|
|
|
|
|
|
|
void Reset();
|
2020-06-01 07:22:54 +00:00
|
|
|
|
2020-04-26 08:25:59 +00:00
|
|
|
private:
|
2020-06-01 07:22:54 +00:00
|
|
|
Pinetime::System::SystemTask &systemTask;
|
|
|
|
Pinetime::Controllers::Ble &bleController;
|
|
|
|
Pinetime::Drivers::SpiNorFlash &spiNorFlash;
|
2020-05-02 12:16:57 +00:00
|
|
|
|
2020-06-01 07:22:54 +00:00
|
|
|
static constexpr uint16_t dfuServiceId{0x1530};
|
|
|
|
static constexpr uint16_t packetCharacteristicId{0x1532};
|
|
|
|
static constexpr uint16_t controlPointCharacteristicId{0x1531};
|
|
|
|
static constexpr uint16_t revisionCharacteristicId{0x1534};
|
2020-04-26 08:25:59 +00:00
|
|
|
|
2020-06-01 07:22:54 +00:00
|
|
|
uint16_t revision{0x0008};
|
2020-04-26 08:25:59 +00:00
|
|
|
|
2020-06-01 07:22:54 +00:00
|
|
|
static constexpr ble_uuid128_t serviceUuid{
|
|
|
|
.u {.type = BLE_UUID_TYPE_128},
|
2020-04-27 18:16:03 +00:00
|
|
|
.value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
|
|
|
|
0xDE, 0xEF, 0x12, 0x12, 0x30, 0x15, 0x00, 0x00}
|
2020-04-26 08:25:59 +00:00
|
|
|
};
|
|
|
|
|
2020-06-01 07:22:54 +00:00
|
|
|
static constexpr ble_uuid128_t packetCharacteristicUuid{
|
|
|
|
.u {.type = BLE_UUID_TYPE_128},
|
2020-04-27 18:16:03 +00:00
|
|
|
.value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
|
|
|
|
0xDE, 0xEF, 0x12, 0x12, 0x32, 0x15, 0x00, 0x00}
|
2020-04-26 08:25:59 +00:00
|
|
|
};
|
|
|
|
|
2020-06-01 07:22:54 +00:00
|
|
|
static constexpr ble_uuid128_t controlPointCharacteristicUuid{
|
|
|
|
.u {.type = BLE_UUID_TYPE_128},
|
2020-04-27 18:16:03 +00:00
|
|
|
.value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
|
|
|
|
0xDE, 0xEF, 0x12, 0x12, 0x31, 0x15, 0x00, 0x00}
|
2020-04-26 08:25:59 +00:00
|
|
|
};
|
|
|
|
|
2020-06-01 07:22:54 +00:00
|
|
|
static constexpr ble_uuid128_t revisionCharacteristicUuid{
|
|
|
|
.u {.type = BLE_UUID_TYPE_128},
|
2020-04-27 18:16:03 +00:00
|
|
|
.value = {0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
|
|
|
|
0xDE, 0xEF, 0x12, 0x12, 0x34, 0x15, 0x00, 0x00}
|
2020-04-26 08:25:59 +00:00
|
|
|
};
|
|
|
|
|
2020-04-27 18:16:03 +00:00
|
|
|
struct ble_gatt_chr_def characteristicDefinition[4];
|
2020-04-26 08:25:59 +00:00
|
|
|
struct ble_gatt_svc_def serviceDefinition[2];
|
2020-04-27 18:16:03 +00:00
|
|
|
uint16_t packetCharacteristicHandle;
|
|
|
|
uint16_t controlPointCharacteristicHandle;
|
|
|
|
uint16_t revisionCharacteristicHandle;
|
2020-05-01 15:58:10 +00:00
|
|
|
|
2020-06-01 07:22:54 +00:00
|
|
|
enum class States : uint8_t {
|
|
|
|
Idle, Init, Start, Data, Validate, Validated
|
|
|
|
};
|
2020-05-01 15:58:10 +00:00
|
|
|
States state = States::Idle;
|
|
|
|
|
|
|
|
enum class ImageTypes : uint8_t {
|
|
|
|
NoImage = 0x00,
|
|
|
|
SoftDevice = 0x01,
|
|
|
|
Bootloader = 0x02,
|
|
|
|
SoftDeviceAndBootloader = 0x03,
|
|
|
|
Application = 0x04
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class Opcodes : uint8_t {
|
|
|
|
StartDFU = 0x01,
|
|
|
|
InitDFUParameters = 0x02,
|
|
|
|
ReceiveFirmwareImage = 0x03,
|
|
|
|
ValidateFirmware = 0x04,
|
|
|
|
ActivateImageAndReset = 0x05,
|
|
|
|
PacketReceiptNotificationRequest = 0x08,
|
|
|
|
Response = 0x10,
|
|
|
|
PacketReceiptNotification = 0x11
|
|
|
|
};
|
|
|
|
|
2020-06-01 07:22:54 +00:00
|
|
|
enum class ErrorCodes {
|
|
|
|
NoError = 0x01,
|
|
|
|
InvalidState = 0x02,
|
|
|
|
NotSupported = 0x03,
|
|
|
|
DataSizeExceedsLimits = 0x04,
|
|
|
|
CrcError = 0x05,
|
|
|
|
OperationFailed = 0x06
|
|
|
|
};
|
2020-05-01 15:58:10 +00:00
|
|
|
|
2020-05-01 13:36:48 +00:00
|
|
|
uint8_t nbPacketsToNotify = 0;
|
|
|
|
uint32_t nbPacketReceived = 0;
|
|
|
|
uint32_t bytesReceived = 0;
|
2020-05-24 07:15:59 +00:00
|
|
|
uint32_t writeOffset = 0x40000;
|
2020-05-01 15:58:10 +00:00
|
|
|
|
2020-05-03 13:48:42 +00:00
|
|
|
uint32_t softdeviceSize = 0;
|
|
|
|
uint32_t bootloaderSize = 0;
|
2020-05-24 07:15:59 +00:00
|
|
|
uint32_t applicationSize = 0;
|
|
|
|
static constexpr uint32_t maxImageSize = 475136;
|
2020-06-01 07:22:54 +00:00
|
|
|
uint16_t expectedCrc = 0;
|
2020-05-03 13:48:42 +00:00
|
|
|
|
2020-05-01 15:58:10 +00:00
|
|
|
int SendDfuRevision(os_mbuf *om) const;
|
2020-06-01 07:22:54 +00:00
|
|
|
|
2020-05-01 15:58:10 +00:00
|
|
|
void SendNotification(uint16_t connectionHandle, const uint8_t *data, const size_t size);
|
2020-06-01 07:22:54 +00:00
|
|
|
|
2020-05-01 15:58:10 +00:00
|
|
|
int WritePacketHandler(uint16_t connectionHandle, os_mbuf *om);
|
2020-06-01 07:22:54 +00:00
|
|
|
|
2020-05-01 15:58:10 +00:00
|
|
|
int ControlPointHandler(uint16_t connectionHandle, os_mbuf *om);
|
2020-05-20 16:39:25 +00:00
|
|
|
|
|
|
|
uint8_t tempBuffer[200];
|
2020-06-01 07:22:54 +00:00
|
|
|
|
|
|
|
uint16_t ComputeCrc(uint8_t const *p_data, uint32_t size, uint16_t const *p_crc);
|
2020-05-20 16:39:25 +00:00
|
|
|
|
2020-05-24 07:15:59 +00:00
|
|
|
void WriteMagicNumber();
|
2020-06-01 07:22:54 +00:00
|
|
|
TimerHandle_t notificationTimer;
|
2020-06-01 14:30:24 +00:00
|
|
|
TimerHandle_t timeoutTimer;
|
2020-06-01 07:22:54 +00:00
|
|
|
|
|
|
|
uint16_t notificatonConnectionHandle = 0;
|
|
|
|
size_t notificationSize = 0;
|
|
|
|
uint8_t notificationBuffer[10];
|
2020-04-26 08:25:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|