summaryrefslogtreecommitdiff
path: root/src/Components/Ble/DfuService.h
blob: 7077bf021b156eb1e96820837dc819757607f5c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#pragma once
#include <cstdint>
#include <array>

#include <host/ble_gap.h>

namespace Pinetime {
  namespace System {
    class SystemTask;
  }
  namespace Controllers {
    class Ble;
    class DfuService {
      public:
        DfuService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::Ble& bleController);
        void Init();

        int OnServiceData(uint16_t connectionHandle, uint16_t attributeHandle, ble_gatt_access_ctxt *context);
      private:
        Pinetime::System::SystemTask& systemTask;
        Pinetime::Controllers::Ble& bleController;

        static constexpr uint16_t dfuServiceId {0x1530};
        static constexpr uint16_t packetCharacteristicId {0x1532};
        static constexpr uint16_t controlPointCharacteristicId {0x1531};
        static constexpr uint16_t revisionCharacteristicId {0x1534};

        uint16_t revision {0x0008};

        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_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_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_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}
        };

        struct ble_gatt_chr_def characteristicDefinition[4];
        struct ble_gatt_svc_def serviceDefinition[2];
        uint16_t packetCharacteristicHandle;
        uint16_t controlPointCharacteristicHandle;
        uint16_t revisionCharacteristicHandle;

        enum class States : uint8_t {Idle, Init, Start, Data, Validate, Validated};
        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
        };

        enum class ErrorCodes { NoError = 0x01};

        uint8_t nbPacketsToNotify = 0;
        uint32_t nbPacketReceived = 0;
        uint32_t bytesReceived = 0;

        int SendDfuRevision(os_mbuf *om) const;
        void SendNotification(uint16_t connectionHandle, const uint8_t *data, const size_t size);
        int WritePacketHandler(uint16_t connectionHandle, os_mbuf *om);
        int ControlPointHandler(uint16_t connectionHandle, os_mbuf *om);
    };
  }
}