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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/**************************************************************************/
/*!
@file BLEDfu.cpp
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2018, Adafruit Industries (adafruit.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**************************************************************************/
#include "bluefruit.h"
#define DFU_REV_APPMODE 0x0001
/* DFU Serivce : 00001530-1212-EFDE-1523-785FEABCD123
* DFU Control : 00001531-1212-EFDE-1523-785FEABCD123
* DFU Packet : 00001532-1212-EFDE-1523-785FEABCD123
* DFU Revision: 00001534-1212-EFDE-1523-785FEABCD123
*/
const uint8_t UUID128_SVC_DFU_OTA[16] =
{
0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
0xDE, 0xEF, 0x12, 0x12, 0x30, 0x15, 0x00, 0x00
};
const uint8_t UUID128_CHR_DFU_CONTROL[16] =
{
0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
0xDE, 0xEF, 0x12, 0x12, 0x31, 0x15, 0x00, 0x00
};
const uint8_t UUID128_CHR_DFU_PACKET[16] =
{
0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
0xDE, 0xEF, 0x12, 0x12, 0x32, 0x15, 0x00, 0x00
};
const uint8_t UUID128_CHR_DFU_REVISON[16] =
{
0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
0xDE, 0xEF, 0x12, 0x12, 0x34, 0x15, 0x00, 0x00
};
extern "C" void bootloader_util_app_start(uint32_t start_addr);
static uint16_t crc16(const uint8_t* data_p, uint8_t length)
{
uint8_t x;
uint16_t crc = 0xFFFF;
while ( length-- )
{
x = crc >> 8 ^ *data_p++;
x ^= x >> 4;
crc = (crc << 8) ^ ((uint16_t) (x << 12)) ^ ((uint16_t) (x << 5)) ^ ((uint16_t) x);
}
return crc;
}
static void bledfu_control_wr_authorize_cb(BLECharacteristic& chr, ble_gatts_evt_write_t* request)
{
if ( (request->handle == chr.handles().value_handle) &&
(request->op != BLE_GATTS_OP_PREP_WRITE_REQ) &&
(request->op != BLE_GATTS_OP_EXEC_WRITE_REQ_NOW) &&
(request->op != BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL))
{
uint16_t conn_hdl = Bluefruit.connHandle();
ble_gatts_rw_authorize_reply_params_t reply = { .type = BLE_GATTS_AUTHORIZE_TYPE_WRITE };
if ( !chr.notifyEnabled() )
{
reply.params.write.gatt_status = BLE_GATT_STATUS_ATTERR_CPS_CCCD_CONFIG_ERROR;
sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply);
return;
}
reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
sd_ble_gatts_rw_authorize_reply(conn_hdl, &reply);
enum { START_DFU = 1 };
if ( request->data[0] == START_DFU )
{
// Peer data information so that bootloader could re-connect after reboot
typedef struct {
ble_gap_addr_t addr;
ble_gap_irk_t irk;
ble_gap_enc_key_t enc_key;
uint8_t sys_attr[8];
uint16_t crc16;
}peer_data_t;
VERIFY_STATIC(offsetof(peer_data_t, crc16) == 60);
/* Save Peer data
* Peer data address is defined in bootloader linker @0x20007F80
* - If bonded : save Security information
* - Otherwise : save Address for direct advertising
*
* TODO may force bonded only for security reason
*/
peer_data_t* peer_data = (peer_data_t*) (0x20007F80UL);
varclr(peer_data);
// Get CCCD
uint16_t sysattr_len = sizeof(peer_data->sys_attr);
sd_ble_gatts_sys_attr_get(conn_hdl, peer_data->sys_attr, &sysattr_len, BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS);
// Get Bond Data or using Address if not bonded
peer_data->addr = Bluefruit.Gap.getPeerAddr(conn_hdl);
if ( Bluefruit.Gap.paired(conn_hdl) )
{
bond_keys_t bkeys;
if ( bond_load_keys( BLE_GAP_ROLE_PERIPH, Bluefruit.Gap._get_peer(conn_hdl)->ediv, &bkeys ) )
{
peer_data->addr = bkeys.peer_id.id_addr_info;
peer_data->irk = bkeys.peer_id.id_info;
peer_data->enc_key = bkeys.own_enc;
}
}
// Calculate crc
peer_data->crc16 = crc16((uint8_t*) peer_data, offsetof(peer_data_t, crc16));
// Initiate DFU Sequence and reboot into DFU OTA mode
Bluefruit.Advertising.restartOnDisconnect(false);
Bluefruit.disconnect();
// Set GPReset to DFU OTA
enum { DFU_OTA_MAGIC = 0xB1 };
sd_power_gpregret_clr(0, 0xFF);
VERIFY_STATUS( sd_power_gpregret_set(0, DFU_OTA_MAGIC), );
VERIFY_STATUS( sd_softdevice_disable(), );
// Disable all interrupts
#if defined(NRF52832_XXAA)
#define MAX_NUMBER_INTERRUPTS 39
#elif defined(NRF52840_XXAA)
#define MAX_NUMBER_INTERRUPTS 48
#endif
NVIC_ClearPendingIRQ(SD_EVT_IRQn);
for(int i=0; i < MAX_NUMBER_INTERRUPTS; i++)
{
NVIC_DisableIRQ( (IRQn_Type) i );
}
// Clear RTC1 timer to prevent Interrupt happens after changing vector table
// NRF_RTC1->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
// NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
// NRF_RTC1->TASKS_STOP = 1;
// NRF_RTC1->TASKS_CLEAR = 1;
VERIFY_STATUS( sd_softdevice_vector_table_base_set(NRF_UICR->NRFFW[0]), );
__set_CONTROL(0); // switch to MSP, required if using FreeRTOS
bootloader_util_app_start(NRF_UICR->NRFFW[0]);
}
}
}
BLEDfu::BLEDfu(void) : BLEService(UUID128_SVC_DFU_OTA), _chr_control(UUID128_CHR_DFU_CONTROL)
{
}
err_t BLEDfu::begin(void)
{
// Invoke base class begin()
VERIFY_STATUS( BLEService::begin() );
// No need to keep packet & revision characteristics
BLECharacteristic chr_packet(UUID128_CHR_DFU_PACKET);
chr_packet.setTempMemory();
chr_packet.setProperties(CHR_PROPS_WRITE_WO_RESP);
chr_packet.setMaxLen(20);
VERIFY_STATUS( chr_packet.begin() );
_chr_control.setProperties(CHR_PROPS_WRITE | CHR_PROPS_NOTIFY);
_chr_control.setMaxLen(23);
_chr_control.setWriteAuthorizeCallback(bledfu_control_wr_authorize_cb);
VERIFY_STATUS( _chr_control.begin() );
BLECharacteristic chr_revision(UUID128_CHR_DFU_REVISON);
chr_revision.setTempMemory();
chr_revision.setProperties(CHR_PROPS_READ);
chr_revision.setFixedLen(2);
VERIFY_STATUS( chr_revision.begin());
chr_revision.write16(DFU_REV_APPMODE);
return ERROR_NONE;
}
|