mirror of
https://github.com/thegecko/web-bluetooth-dfu.git
synced 2025-12-15 13:38:12 +08:00
Improve how init packet is implemented. Some minor clean up.
This commit is contained in:
43
dist/dfu.js
vendored
43
dist/dfu.js
vendored
@@ -61,6 +61,16 @@
|
|||||||
SoftDevice_Bootloader: 3,
|
SoftDevice_Bootloader: 3,
|
||||||
Application: 4
|
Application: 4
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: This should be configurable by the user. For now this will work with any of Nordic's SDK examples.
|
||||||
|
var initPacket = {
|
||||||
|
device_type: 0xFFFF,
|
||||||
|
device_rev: 0xFFFF,
|
||||||
|
app_version: 0xFFFFFFFF,
|
||||||
|
softdevice_len: 0x0001,
|
||||||
|
softdevice: 0xFFFE,
|
||||||
|
crc: 0x0000
|
||||||
|
};
|
||||||
|
|
||||||
var loggers = [];
|
var loggers = [];
|
||||||
function addLogger(loggerFn) {
|
function addLogger(loggerFn) {
|
||||||
@@ -90,7 +100,7 @@
|
|||||||
*/
|
*/
|
||||||
function writeMode(device) {
|
function writeMode(device) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
var characteristics = null;
|
var controlChar = null;
|
||||||
/*
|
/*
|
||||||
// Disconnect event currently not implemented...
|
// Disconnect event currently not implemented...
|
||||||
device.addEventListener("gattserverdisconnected", () => {
|
device.addEventListener("gattserverdisconnected", () => {
|
||||||
@@ -102,15 +112,15 @@
|
|||||||
connect(device)
|
connect(device)
|
||||||
.then(chars => {
|
.then(chars => {
|
||||||
log("enabling notifications");
|
log("enabling notifications");
|
||||||
characteristics = chars;
|
controlChar = chars.controlChar;
|
||||||
return characteristics.controlChar.startNotifications()
|
return controlChar.startNotifications()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
characteristics.controlChar.addEventListener('characteristicvaluechanged', handleNotifications);
|
controlChar.addEventListener('characteristicvaluechanged', handleNotifications);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
log("writing modeData");
|
log("writing modeData");
|
||||||
return characteristics.controlChar.writeValue(new Uint8Array([1, 4]))
|
return controlChar.writeValue(new Uint8Array([1, 4]))
|
||||||
.then(() => {
|
.then(() => {
|
||||||
log("modeData written");
|
log("modeData written");
|
||||||
resolve(device); // TODO: once disconnect event is implemented we should resolve in its callback...
|
resolve(device); // TODO: once disconnect event is implemented we should resolve in its callback...
|
||||||
@@ -133,23 +143,30 @@
|
|||||||
* Init packet used for pre-checking to ensure the following image is compatible with the device.
|
* Init packet used for pre-checking to ensure the following image is compatible with the device.
|
||||||
* Contains information on device type, revision, and supported SoftDevices along with a CRC or hash of firmware image.
|
* Contains information on device type, revision, and supported SoftDevices along with a CRC or hash of firmware image.
|
||||||
*
|
*
|
||||||
* Not used in mbed bootloader.
|
* Not used in mbed bootloader (init packet was optional in SDK v6.x).
|
||||||
*/
|
*/
|
||||||
function generateInitPacket() {
|
function generateInitPacket() {
|
||||||
return new Uint8Array([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0xFE, 0xFF, 0x00, 0x00]); // Temporary init packet.
|
var buffer = new ArrayBuffer(14);
|
||||||
|
var view = new DataView(buffer);
|
||||||
|
view.setUint16(0, initPacket.device_type, LITTLE_ENDIAN);
|
||||||
|
view.setUint16(2, initPacket.device_rev, LITTLE_ENDIAN);
|
||||||
|
view.setUint32(4, initPacket.app_version, LITTLE_ENDIAN); // Application version for the image software. This field allows for additional checking, for example ensuring that a downgrade is not allowed.
|
||||||
|
view.setUint16(8, initPacket.softdevice_len, LITTLE_ENDIAN); // Number of different SoftDevice revisions compatible with this application.
|
||||||
|
view.setUint16(10, initPacket.softdevice, LITTLE_ENDIAN); // Variable length array of SoftDevices compatible with this application. The length of the array is specified in the length (softdevice_len) field. 0xFFFE indicates any SoftDevice.
|
||||||
|
view.setUint16(12, initPacket.crc, LITTLE_ENDIAN);
|
||||||
|
return view;
|
||||||
}
|
}
|
||||||
|
|
||||||
function provision(device, arrayBuffer, imageType) {
|
function provision(device, arrayBuffer, imageType) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
log('function provision(device, arrayBuffer, imageType)');
|
var versionChar = null;
|
||||||
|
|
||||||
imageType = imageType || ImageType.Application;
|
imageType = imageType || ImageType.Application;
|
||||||
|
|
||||||
connect(device)
|
connect(device)
|
||||||
.then(chars => {
|
.then(chars => {
|
||||||
// Older DFU implementations (from older Nordic SDKs < 7.0) have no DFU Version characteristic.
|
versionChar = chars.versionChar;
|
||||||
if (chars.versionChar) {
|
if (versionChar) { // Older DFU implementations (from older Nordic SDKs < 7.0) have no DFU Version characteristic.
|
||||||
return chars.versionChar.readValue()
|
return versionChar.readValue()
|
||||||
.then(data => {
|
.then(data => {
|
||||||
console.log('read versionChar');
|
console.log('read versionChar');
|
||||||
var view = new DataView(data);
|
var view = new DataView(data);
|
||||||
@@ -174,8 +191,6 @@
|
|||||||
|
|
||||||
function connect(device) {
|
function connect(device) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
log('function connect(device)');
|
|
||||||
|
|
||||||
var server = null;
|
var server = null;
|
||||||
var service = null;
|
var service = null;
|
||||||
var controlChar = null;
|
var controlChar = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user