Compare commits

..

51 Commits

Author SHA1 Message Date
f85c89e2c4 (1.1.0) successful file transfer 2025-12-01 21:29:11 +00:00
6447692d4c (1.0.37) bump 2025-12-01 21:21:05 +00:00
855eb07d89 set file progress rather than adding 2025-12-01 21:20:55 +00:00
8e840c0966 (1.0.36) bump 2025-12-01 21:07:05 +00:00
5ab862d703 loading the file progress correctly 2025-12-01 21:06:56 +00:00
7c6f0e8a3e (1.0.35) bump 2025-12-01 20:59:49 +00:00
bda9dddd4f more logging 2025-12-01 20:59:41 +00:00
95709e0285 (1.0.34) bump 2025-12-01 20:56:24 +00:00
4c4b896978 more logging 2025-12-01 20:56:15 +00:00
c20c0b02cf (1.0.33) bump 2025-12-01 20:47:55 +00:00
46bd6f7161 track recieved data size to avoid false branching 2025-12-01 20:47:48 +00:00
eeb90f630d (1.0.32) bump 2025-12-01 20:45:31 +00:00
bccbd61ad5 log recieved file progress 2025-12-01 20:44:27 +00:00
2017847b9e (1.0.31) bump 2025-12-01 20:30:50 +00:00
b471d0e20f trying file progress from remote, due to packet loss 2025-12-01 20:30:37 +00:00
a7d5880057 (1.0.30) bump 2025-12-01 18:40:16 +00:00
e5ce96adcf only add to progress the number of bytes written 2025-12-01 18:40:08 +00:00
27403e0aed (1.0.29) bump 2025-12-01 18:33:57 +00:00
faf4e377d7 trying a different way to confirm mtu size 2025-12-01 18:33:48 +00:00
ed4890818f (1.0.28) bump 2025-12-01 18:24:51 +00:00
0f61fd26c5 mtu logging 2025-12-01 18:24:40 +00:00
d1927bc24e (1.0.27) bump 2025-12-01 18:08:02 +00:00
1b7719ac3d wait for mtu to be agreed 2025-12-01 18:07:50 +00:00
ebcfb38e41 (1.0.26) bump 2025-12-01 17:49:29 +00:00
ad7402f04b without responses again 2025-12-01 17:49:19 +00:00
e8e9547a20 (1.0.25) bump 2025-12-01 17:27:08 +00:00
852fcf67f4 going back to writing with response due to transmission issues 2025-12-01 17:26:43 +00:00
e798f997d3 (1.0.24) bump 2025-11-30 21:58:24 +00:00
d2f9809f52 (1.0.23) bump 2025-11-30 21:47:57 +00:00
3e7c26e3ba file progress in more places 2025-11-30 21:47:47 +00:00
e5912cd94e (1.0.22) bump 2025-11-30 21:43:00 +00:00
20c203ffa4 fix packet size issue 2025-11-30 21:42:47 +00:00
ad464e1c0d (1.0.21) bump 2025-11-30 21:20:50 +00:00
5009b0c8d1 Reducing number of awaits for speed 2025-11-30 21:20:33 +00:00
b13787b704 (1.0.20) bump 2025-11-30 21:11:31 +00:00
b155baed03 do file progress counter preemtively 2025-11-30 21:11:05 +00:00
41a2e30aca (1.0.19) bump 2025-11-30 17:42:03 +00:00
266044215d dbg 2025-11-30 17:41:52 +00:00
418df318ee (1.0.18) bump 2025-11-30 16:51:58 +00:00
896cccd7ff More debug 2025-11-30 16:51:45 +00:00
66cbb31dde (1.0.17) bump 2025-11-30 12:02:38 +00:00
90b8e08946 actually count up file progress 2025-11-30 12:02:21 +00:00
4900cf6acd (1.0.16) dbg 2025-11-30 11:47:25 +00:00
3e07911cac Some debug messages 2025-11-30 11:46:58 +00:00
8c3dd4c2e3 (1.0.15) bump 2025-11-29 23:15:37 +00:00
3bae392285 communicate error 2025-11-29 23:15:17 +00:00
b978ffa313 (1.0.14) file transfer go 1 2025-11-29 22:55:11 +00:00
5762bcb549 possible file transfer 2025-11-29 22:54:40 +00:00
836e0f70aa Update characteristic UUIDs 2025-11-28 15:31:17 +00:00
b0813ed082 (1.0.13) bug fix 2025-11-26 22:19:44 +00:00
6ffc5c1e8a needed cors proxy 2025-11-26 22:19:21 +00:00
5 changed files with 349 additions and 44 deletions

107
dist/index.cjs.js vendored
View File

@@ -8,15 +8,21 @@ class Updater {
feedType; feedType;
bleObject; bleObject;
bleDeviceId; bleDeviceId;
_updaterServiceUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e56193b"; _updaterServiceUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561900";
_updaterVersionCharactersiticUUID = "1978a3df-c009-4837-b295-57ef429dde8c"; _updaterVersionCharactersiticUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561910";
_updaterCommandCharacterisitcUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561920";
_updateFileCharacteristicUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561930";
file; file;
constructor(archiveURL = "/", feedType = "atom", bleObject) { _fileSize;
_fileProgress = 0;
_packetSize;
constructor(archiveURL = "/", feedType = "atom", bleObject, packetSize = 512) {
this.archiveURL = archiveURL; this.archiveURL = archiveURL;
this.feedType = feedType; this.feedType = feedType;
if (bleObject) { if (bleObject) {
this.bleObject = bleObject; this.bleObject = bleObject;
} }
this._packetSize = packetSize;
} }
/* /*
FEEDS FEEDS
@@ -154,9 +160,10 @@ class Updater {
*/ */
async getFirmware(version) { async getFirmware(version) {
try { try {
const res = await fetch(`${this.archiveURL}/download/${version.title}/firmware.bin`); const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}/download/${version.title}/firmware.bin`);
let buf = await res.arrayBuffer(); let buf = await res.arrayBuffer();
this.file = new Int8Array(buf); this.file = new Int8Array(buf);
this._fileSize = this.file.byteLength;
return true; return true;
} }
catch { catch {
@@ -164,7 +171,97 @@ class Updater {
} }
} }
getFileSize() { getFileSize() {
return this.file.byteLength; return this._fileSize;
}
async sendNextPacket() {
let packet = this.file.slice(this._fileProgress, this._fileProgress + this._packetSize);
// this._fileProgress += this._packetSize;
return new Promise((resolve, reject) => {
this.bleObject.writeWithoutResponse(this.bleDeviceId, this._updaterServiceUUID, this._updateFileCharacteristicUUID, packet.buffer, () => {
console.log(`Sent: ${packet}, progress: ${this._fileProgress}`);
resolve(true);
}, (error) => {
this._fileProgress -= this._packetSize;
reject(error);
});
});
}
async sendEndCmd(agree) {
const buffer = new ArrayBuffer(1);
let view = new Int8Array(buffer);
view[0] = agree ? 3 : 4;
await this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
return;
}
// start and autoconnect before you run this function, so that you can have an "uninterrupted" connection when the board reboots
async flashFirmware(progressCallback) {
// write filesize to board
// await notify from cmd - ready
// send a packet
// check for error
// write file length
return new Promise(async (resolve, reject) => {
// set mtu
this._packetSize = await this.bleObject.withPromises.requestMtu(this.bleDeviceId, this._packetSize);
// start notify
this.bleObject.startNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, (rawData) => {
let dataView = new Uint8Array(rawData);
console.log(dataView);
if (dataView[0] == 1 && dataView.length == 1) {
// send file
this.sendNextPacket();
progressCallback(`MTU: ${this._packetSize}; Sending (${Math.floor((this._fileProgress * 100) / this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
}
else if (dataView[0] == 2 && dataView.length == 1) {
// done logic
console.log(`progress >= filesize: ${this._fileProgress} vs ${this._fileSize}`);
if (this._fileProgress >= (this._fileSize - this._packetSize)) {
console.log("true");
// send agree
this.sendEndCmd(true);
progressCallback(`Complete!`);
this.bleObject.stopNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, () => {
// success
resolve(true);
}, (error) => {
reject("Error: Failed to stop notify");
});
}
else {
console.log("False");
// send disagree
this.sendEndCmd(false);
progressCallback(`Error, starting over: ${this._fileProgress} / ${this._fileSize}`);
this._fileProgress = 0;
}
}
else if (dataView[0] == 15 && dataView.length == 1) {
// error cmd
progressCallback(`Error on remote: ${this._fileProgress} / ${this._fileSize}`);
reject("Error on remote");
}
else if (dataView[0] == 0 && dataView.length == 1) {
// ignore no command
progressCallback(`Board is on`);
}
else {
// should be the file progress
let fileProgressView = new Uint32Array(rawData);
this._fileProgress = (dataView[3] << 24) |
(dataView[2] << 16) |
(dataView[1] << 8) | dataView[0];
this.sendNextPacket();
progressCallback(`Recieved progress ${fileProgressView[0]}; Sending (${Math.floor((this._fileProgress * 100) / this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
}
}, (error) => {
reject("Error: Failed to start notify");
console.error(error);
});
const buffer = new ArrayBuffer(4);
let view = new Int32Array(buffer);
view[0] = this._fileSize;
this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
});
} }
} }

10
dist/index.d.ts vendored
View File

@@ -11,8 +11,13 @@ export default class Updater {
protected bleDeviceId: string; protected bleDeviceId: string;
private readonly _updaterServiceUUID; private readonly _updaterServiceUUID;
private readonly _updaterVersionCharactersiticUUID; private readonly _updaterVersionCharactersiticUUID;
private readonly _updaterCommandCharacterisitcUUID;
private readonly _updateFileCharacteristicUUID;
file: Int8Array; file: Int8Array;
constructor(archiveURL?: string, feedType?: string, bleObject?: BLECentralPlugin.BLECentralPluginStatic); private _fileSize;
private _fileProgress;
private _packetSize;
constructor(archiveURL?: string, feedType?: string, bleObject?: BLECentralPlugin.BLECentralPluginStatic, packetSize?: number);
private getRawArchive; private getRawArchive;
private atomGetVersionDetails; private atomGetVersionDetails;
private atomGetArchive; private atomGetArchive;
@@ -27,4 +32,7 @@ export default class Updater {
getBoardVersion(): Promise<string>; getBoardVersion(): Promise<string>;
getFirmware(version: versionNotes): Promise<boolean>; getFirmware(version: versionNotes): Promise<boolean>;
getFileSize(): number; getFileSize(): number;
private sendNextPacket;
private sendEndCmd;
flashFirmware(progressCallback: (message: string) => void): Promise<boolean>;
} }

107
dist/index.es.js vendored
View File

@@ -6,15 +6,21 @@ class Updater {
feedType; feedType;
bleObject; bleObject;
bleDeviceId; bleDeviceId;
_updaterServiceUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e56193b"; _updaterServiceUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561900";
_updaterVersionCharactersiticUUID = "1978a3df-c009-4837-b295-57ef429dde8c"; _updaterVersionCharactersiticUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561910";
_updaterCommandCharacterisitcUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561920";
_updateFileCharacteristicUUID = "71a4438e-fd52-4b15-b3d2-ec0e3e561930";
file; file;
constructor(archiveURL = "/", feedType = "atom", bleObject) { _fileSize;
_fileProgress = 0;
_packetSize;
constructor(archiveURL = "/", feedType = "atom", bleObject, packetSize = 512) {
this.archiveURL = archiveURL; this.archiveURL = archiveURL;
this.feedType = feedType; this.feedType = feedType;
if (bleObject) { if (bleObject) {
this.bleObject = bleObject; this.bleObject = bleObject;
} }
this._packetSize = packetSize;
} }
/* /*
FEEDS FEEDS
@@ -152,9 +158,10 @@ class Updater {
*/ */
async getFirmware(version) { async getFirmware(version) {
try { try {
const res = await fetch(`${this.archiveURL}/download/${version.title}/firmware.bin`); const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}/download/${version.title}/firmware.bin`);
let buf = await res.arrayBuffer(); let buf = await res.arrayBuffer();
this.file = new Int8Array(buf); this.file = new Int8Array(buf);
this._fileSize = this.file.byteLength;
return true; return true;
} }
catch { catch {
@@ -162,7 +169,97 @@ class Updater {
} }
} }
getFileSize() { getFileSize() {
return this.file.byteLength; return this._fileSize;
}
async sendNextPacket() {
let packet = this.file.slice(this._fileProgress, this._fileProgress + this._packetSize);
// this._fileProgress += this._packetSize;
return new Promise((resolve, reject) => {
this.bleObject.writeWithoutResponse(this.bleDeviceId, this._updaterServiceUUID, this._updateFileCharacteristicUUID, packet.buffer, () => {
console.log(`Sent: ${packet}, progress: ${this._fileProgress}`);
resolve(true);
}, (error) => {
this._fileProgress -= this._packetSize;
reject(error);
});
});
}
async sendEndCmd(agree) {
const buffer = new ArrayBuffer(1);
let view = new Int8Array(buffer);
view[0] = agree ? 3 : 4;
await this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
return;
}
// start and autoconnect before you run this function, so that you can have an "uninterrupted" connection when the board reboots
async flashFirmware(progressCallback) {
// write filesize to board
// await notify from cmd - ready
// send a packet
// check for error
// write file length
return new Promise(async (resolve, reject) => {
// set mtu
this._packetSize = await this.bleObject.withPromises.requestMtu(this.bleDeviceId, this._packetSize);
// start notify
this.bleObject.startNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, (rawData) => {
let dataView = new Uint8Array(rawData);
console.log(dataView);
if (dataView[0] == 1 && dataView.length == 1) {
// send file
this.sendNextPacket();
progressCallback(`MTU: ${this._packetSize}; Sending (${Math.floor((this._fileProgress * 100) / this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
}
else if (dataView[0] == 2 && dataView.length == 1) {
// done logic
console.log(`progress >= filesize: ${this._fileProgress} vs ${this._fileSize}`);
if (this._fileProgress >= (this._fileSize - this._packetSize)) {
console.log("true");
// send agree
this.sendEndCmd(true);
progressCallback(`Complete!`);
this.bleObject.stopNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, () => {
// success
resolve(true);
}, (error) => {
reject("Error: Failed to stop notify");
});
}
else {
console.log("False");
// send disagree
this.sendEndCmd(false);
progressCallback(`Error, starting over: ${this._fileProgress} / ${this._fileSize}`);
this._fileProgress = 0;
}
}
else if (dataView[0] == 15 && dataView.length == 1) {
// error cmd
progressCallback(`Error on remote: ${this._fileProgress} / ${this._fileSize}`);
reject("Error on remote");
}
else if (dataView[0] == 0 && dataView.length == 1) {
// ignore no command
progressCallback(`Board is on`);
}
else {
// should be the file progress
let fileProgressView = new Uint32Array(rawData);
this._fileProgress = (dataView[3] << 24) |
(dataView[2] << 16) |
(dataView[1] << 8) | dataView[0];
this.sendNextPacket();
progressCallback(`Recieved progress ${fileProgressView[0]}; Sending (${Math.floor((this._fileProgress * 100) / this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
}
}, (error) => {
reject("Error: Failed to start notify");
console.error(error);
});
const buffer = new ArrayBuffer(4);
let view = new Int32Array(buffer);
view[0] = this._fileSize;
this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
});
} }
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "updaterweblibrary", "name": "updaterweblibrary",
"version": "1.0.12", "version": "1.1.0",
"description": "OTA Updater App frontend library", "description": "OTA Updater App frontend library",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -2,10 +2,10 @@
import { parseXml, XmlDocument, XmlElement, XmlText } from "@rgrove/parse-xml"; import { parseXml, XmlDocument, XmlElement, XmlText } from "@rgrove/parse-xml";
export type versionNotes = { export type versionNotes = {
title: string; title: string;
date: Date | string; date: Date | string;
link: string; link: string;
html: string; html: string;
} }
export default class Updater { export default class Updater {
@@ -15,19 +15,26 @@ export default class Updater {
public bleObject: BLECentralPlugin.BLECentralPluginStatic; public bleObject: BLECentralPlugin.BLECentralPluginStatic;
protected bleDeviceId: string; protected bleDeviceId: string;
private readonly _updaterServiceUUID: string = "71a4438e-fd52-4b15-b3d2-ec0e3e56193b"; private readonly _updaterServiceUUID: string = "71a4438e-fd52-4b15-b3d2-ec0e3e561900";
private readonly _updaterVersionCharactersiticUUID: string = "1978a3df-c009-4837-b295-57ef429dde8c"; private readonly _updaterVersionCharactersiticUUID: string = "71a4438e-fd52-4b15-b3d2-ec0e3e561910";
private readonly _updaterCommandCharacterisitcUUID: string = "71a4438e-fd52-4b15-b3d2-ec0e3e561920";
private readonly _updateFileCharacteristicUUID: string = "71a4438e-fd52-4b15-b3d2-ec0e3e561930";
public file: Int8Array; public file: Int8Array;
private _fileSize: number;
private _fileProgress: number = 0;
private _packetSize: number;
constructor(archiveURL: string = "/", feedType: string = "atom", bleObject?: BLECentralPlugin.BLECentralPluginStatic) { constructor(archiveURL: string = "/", feedType: string = "atom", bleObject?: BLECentralPlugin.BLECentralPluginStatic, packetSize: number = 512) {
this.archiveURL = archiveURL; this.archiveURL = archiveURL;
this.feedType = feedType; this.feedType = feedType;
if (bleObject) { if (bleObject) {
this.bleObject = bleObject; this.bleObject = bleObject;
} }
this._packetSize = packetSize;
} }
/* /*
FEEDS FEEDS
*/ */
@@ -42,7 +49,7 @@ export default class Updater {
// atom feeds // atom feeds
private atomGetVersionDetails(entry: XmlElement): versionNotes { private atomGetVersionDetails(entry: XmlElement): versionNotes {
let outEntry: versionNotes = {title: "", date: new Date, link: "", html: ""}; let outEntry: versionNotes = { title: "", date: new Date, link: "", html: "" };
entry.children.forEach((elm) => { entry.children.forEach((elm) => {
let element = elm as XmlElement; let element = elm as XmlElement;
if (element.name == "title") { if (element.name == "title") {
@@ -54,7 +61,7 @@ export default class Updater {
else if (element.name == "link") { else if (element.name == "link") {
outEntry.link = element.attributes["href"]; outEntry.link = element.attributes["href"];
} else if (element.name == "content") { } else if (element.name == "content") {
outEntry.html = (element.children[0] as XmlText).text; outEntry.html = (element.children[0] as XmlText).text;
} }
}) })
@@ -80,7 +87,7 @@ export default class Updater {
// rss feeds // rss feeds
private rssGetVersionDetails(entry: XmlElement): versionNotes { private rssGetVersionDetails(entry: XmlElement): versionNotes {
let outEntry: versionNotes = {title: "", date: new Date, link: "", html: ""}; let outEntry: versionNotes = { title: "", date: new Date, link: "", html: "" };
entry.children.forEach((elm) => { entry.children.forEach((elm) => {
let element = elm as XmlElement; let element = elm as XmlElement;
if (element.name == "title") { if (element.name == "title") {
@@ -92,7 +99,7 @@ export default class Updater {
else if (element.name == "link") { else if (element.name == "link") {
outEntry.link = (element.children[0] as XmlText).text; outEntry.link = (element.children[0] as XmlText).text;
} else if (element.name == "content") { } else if (element.name == "content") {
outEntry.html = (element.children[0] as XmlText).text; outEntry.html = (element.children[0] as XmlText).text;
} }
}) })
@@ -114,11 +121,11 @@ export default class Updater {
}) })
return output; return output;
} }
public async getArchive(): Promise<versionNotes[]> { public async getArchive(): Promise<versionNotes[]> {
if (this.feedType == "atom") { if (this.feedType == "atom") {
return this.atomGetArchive() return this.atomGetArchive()
} else if (this.feedType == "rss"){ } else if (this.feedType == "rss") {
return this.rssGetArchive() return this.rssGetArchive()
} }
} }
@@ -138,16 +145,17 @@ export default class Updater {
private async readVersionNumber(): Promise<string> { private async readVersionNumber(): Promise<string> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.bleObject.read( this.bleObject.read(
this.bleDeviceId, this.bleDeviceId,
this._updaterServiceUUID, this._updaterServiceUUID,
this._updaterVersionCharactersiticUUID, this._updaterVersionCharactersiticUUID,
(rawData: ArrayBuffer) => { (rawData: ArrayBuffer) => {
resolve(this.bytesToString(rawData)); resolve(this.bytesToString(rawData));
}, },
(error: string) => { (error: string) => {
reject(`Error: ${error}`); reject(`Error: ${error}`);
} }
)}); )
});
} }
private async getLatestVersion(): Promise<string> { private async getLatestVersion(): Promise<string> {
@@ -169,7 +177,7 @@ export default class Updater {
// compare with latest version // compare with latest version
const latestVersion = await this.getLatestVersion(); const latestVersion = await this.getLatestVersion();
if (deviceVersion != latestVersion) { if (deviceVersion != latestVersion) {
return true; return true;
} }
// update // update
return false; return false;
@@ -178,16 +186,17 @@ export default class Updater {
public async getBoardVersion(): Promise<string> { public async getBoardVersion(): Promise<string> {
return await this.readVersionNumber(); return await this.readVersionNumber();
} }
/* /*
FILE FLASHING FILE FLASHING
*/ */
public async getFirmware(version: versionNotes): Promise<boolean> { public async getFirmware(version: versionNotes): Promise<boolean> {
try { try {
const res = await fetch(`${this.archiveURL}/download/${version.title}/firmware.bin`); const res = await fetch(`http://cors.emaker.limited/?url=${this.archiveURL}/download/${version.title}/firmware.bin`);
let buf = await res.arrayBuffer(); let buf = await res.arrayBuffer();
this.file = new Int8Array(buf); this.file = new Int8Array(buf);
this._fileSize = this.file.byteLength;
return true; return true;
} catch { } catch {
return false; return false;
@@ -195,11 +204,105 @@ export default class Updater {
} }
public getFileSize(): number { public getFileSize(): number {
return this.file.byteLength; return this._fileSize;
} }
// public async flashFirmware(): Promise<boolean> { private async sendNextPacket(): Promise<boolean> {
let packet = this.file.slice(this._fileProgress, this._fileProgress+this._packetSize);
// } // this._fileProgress += this._packetSize;
return new Promise((resolve, reject) => {
this.bleObject.writeWithoutResponse(this.bleDeviceId, this._updaterServiceUUID, this._updateFileCharacteristicUUID, packet.buffer,
() => {
console.log(`Sent: ${packet}, progress: ${this._fileProgress}`);
resolve(true);
},
(error) => {
this._fileProgress -= this._packetSize;
reject(error);
}
)
});
}
private async sendEndCmd(agree: boolean): Promise<void> {
const buffer = new ArrayBuffer(1);
let view = new Int8Array(buffer);
view[0] = agree ? 3 : 4;
await this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
return;
}
// start and autoconnect before you run this function, so that you can have an "uninterrupted" connection when the board reboots
public async flashFirmware(progressCallback: (message: string) => void): Promise<boolean> {
// write filesize to board
// await notify from cmd - ready
// send a packet
// check for error
// write file length
return new Promise(async (resolve, reject) => {
// set mtu
this._packetSize = await this.bleObject.withPromises.requestMtu(this.bleDeviceId, this._packetSize);
// start notify
this.bleObject.startNotification(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID,
(rawData: ArrayBuffer): void => {
let dataView = new Uint8Array(rawData);
console.log(dataView);
if (dataView[0] == 1 && dataView.length == 1) {
// send file
this.sendNextPacket();
progressCallback(`MTU: ${this._packetSize}; Sending (${Math.floor((this._fileProgress *100)/this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
} else if (dataView[0] == 2 && dataView.length == 1) {
// done logic
console.log(`progress >= filesize: ${this._fileProgress} vs ${this._fileSize}`);
if (this._fileProgress >= (this._fileSize - this._packetSize)) {
console.log("true");
// send agree
this.sendEndCmd(true);
progressCallback(`Complete!`);
this.bleObject.stopNotification(this.bleDeviceId,
this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID,
() => {
// success
resolve(true);
},
(error) => {
reject("Error: Failed to stop notify");
}
);
} else {
console.log("False");
// send disagree
this.sendEndCmd(false);
progressCallback(`Error, starting over: ${this._fileProgress} / ${this._fileSize}`);
this._fileProgress = 0;
}
} else if (dataView[0] == 15 && dataView.length == 1) {
// error cmd
progressCallback(`Error on remote: ${this._fileProgress} / ${this._fileSize}`);
reject("Error on remote");
} else if (dataView[0] == 0 && dataView.length == 1) {
// ignore no command
progressCallback(`Board is on`);
}
else {
// should be the file progress
let fileProgressView = new Uint32Array(rawData);
this._fileProgress = (dataView[3] << 24) |
(dataView[2] << 16) |
(dataView[1] << 8) | dataView[0];
this.sendNextPacket();
progressCallback(`Recieved progress ${fileProgressView[0]}; Sending (${Math.floor((this._fileProgress *100)/this._fileSize)}%), ${this._fileProgress} / ${this._fileSize}`);
}
}, (error) => { reject("Error: Failed to start notify");
console.error(error);
}
);
const buffer = new ArrayBuffer(4)
let view = new Int32Array(buffer);
view[0] = this._fileSize;
this.bleObject.withPromises.write(this.bleDeviceId, this._updaterServiceUUID, this._updaterCommandCharacterisitcUUID, buffer);
});
}
} }