forked from darthcloud/BlueRetroWebCfg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathota.js
More file actions
253 lines (236 loc) · 7.7 KB
/
Copy pathota.js
File metadata and controls
253 lines (236 loc) · 7.7 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Base on https://www.html5rocks.com/en/tutorials/file/dndfiles//
var brUuid = [
'56830f56-5180-fab0-314b-2fa176799a00',
'56830f56-5180-fab0-314b-2fa176799a01',
'56830f56-5180-fab0-314b-2fa176799a02',
'56830f56-5180-fab0-314b-2fa176799a03',
'56830f56-5180-fab0-314b-2fa176799a04',
'56830f56-5180-fab0-314b-2fa176799a05',
'56830f56-5180-fab0-314b-2fa176799a06',
'56830f56-5180-fab0-314b-2fa176799a07',
'56830f56-5180-fab0-314b-2fa176799a08',
'56830f56-5180-fab0-314b-2fa176799a09',
'56830f56-5180-fab0-314b-2fa176799a0a',
'56830f56-5180-fab0-314b-2fa176799a0b',
'56830f56-5180-fab0-314b-2fa176799a0c',
];
const mtu = 244;
const ota_start = 0xA5;
const ota_abort = 0xDE;
const ota_end = 0x5A;
const sys_deep_sleep = 0x37;
var bluetoothDevice;
let brService = null;
var reader;
var progress = document.querySelector('.percent');
var start;
var end;
var cancel = 0;
var bdaddr;
var app_ver;
var name;
function getAppVersion() {
return new Promise(function(resolve, reject) {
log('Get Api version CHRC...');
brService.getCharacteristic(brUuid[9])
.then(chrc => {
log('Reading App version...');
return chrc.readValue();
})
.then(value => {
var enc = new TextDecoder("utf-8");
app_ver = enc.decode(value);
log('App version: ' + app_ver);
resolve();
})
.catch(error => {
resolve();
});
});
}
function getBdAddr() {
return new Promise(function(resolve, reject) {
log('Get BD_ADDR CHRC...');
brService.getCharacteristic(brUuid[12])
.then(chrc => {
log('Reading BD_ADDR...');
return chrc.readValue();
})
.then(value => {
bdaddr = value.getUint8(5).toString(16).padStart(2, '0') + ':'
+ value.getUint8(4).toString(16).padStart(2, '0') + ':'
+ value.getUint8(3).toString(16).padStart(2, '0') + ':'
+ value.getUint8(2).toString(16).padStart(2, '0') + ':'
+ value.getUint8(1).toString(16).padStart(2, '0') + ':'
+ value.getUint8(0).toString(16).padStart(2, '0');
log('BD_ADDR: ' + bdaddr);
resolve();
})
.catch(error => {
resolve();
});
});
}
function abortFwUpdate() {
cancel = 1;
}
function errorHandler(evt) {
switch(evt.target.error.code) {
case evt.target.error.NOT_FOUND_ERR:
log('File Not Found!');
break;
case evt.target.error.NOT_READABLE_ERR:
log('File is not readable');
break;
case evt.target.error.ABORT_ERR:
break; // noop
default:
log('An error occurred reading this file.');
};
}
function updateProgress(total, loaded) {
var percentLoaded = Math.round((loaded / total) * 100);
// Increase the progress bar length.
if (percentLoaded < 100) {
progress.style.width = percentLoaded + '%';
progress.textContent = percentLoaded + '%';
}
}
function firmwareUpdate(evt) {
// Reset progress indicator on new file selection.
progress.style.width = '0%';
progress.textContent = '0%';
reader = new FileReader();
reader.onerror = errorHandler;
reader.onabort = function(e) {
log('File read cancelled');
};
reader.onload = function(e) {
writeFirmware(reader.result, 0);
}
// Read in the image file as a binary string.
reader.readAsArrayBuffer(document.getElementById("fwFile").files[0]);
}
function writeFwRecursive(chrc, data, offset) {
return new Promise(function(resolve, reject) {
if (cancel == 1) {
throw 'Cancelled';
}
updateProgress(data.byteLength, offset);
var tmpViewSize = data.byteLength - offset;
if (tmpViewSize > mtu) {
tmpViewSize = mtu;
}
var tmpView = new DataView(data, offset, tmpViewSize);
chrc.writeValue(tmpView)
.then(_ => {
offset += Number(mtu);
if (offset < data.byteLength) {
resolve(writeFwRecursive(chrc, data, offset));
}
else {
end = performance.now();
progress.style.width = '100%';
progress.textContent = '100%';
log('FW upload done. Took: ' + (end - start)/1000 + ' sec');
resolve();
}
})
.catch(error => {
reject(error);
});
});
}
function writeFirmware(data) {
var cmd = new Uint8Array(1);
let ctrl_chrc = null;
document.getElementById('progress_bar').className = 'loading';
document.getElementById("divBtConn").style.display = 'none';
document.getElementById("divInfo").style.display = 'block';
document.getElementById("divFwSelect").style.display = 'none';
document.getElementById("divFwUpdate").style.display = 'block';
brService.getCharacteristic(brUuid[7])
.then(chrc => {
ctrl_chrc = chrc;
cmd[0] = ota_start;
return ctrl_chrc.writeValue(cmd)
})
.then(_ => {
return brService.getCharacteristic(brUuid[8])
})
.then(chrc => {
start = performance.now();
return writeFwRecursive(chrc, data, 0);
})
.then(_ => {
cmd[0] = ota_end;
return ctrl_chrc.writeValue(cmd)
})
.catch(error => {
log('Argh! ' + error);
document.getElementById("divBtConn").style.display = 'none';
document.getElementById("divInfo").style.display = 'block';
document.getElementById("divFwSelect").style.display = 'block';
document.getElementById("divFwUpdate").style.display = 'none';
cancel = 0;
cmd[0] = ota_abort;
return ctrl_chrc.writeValue(cmd)
});
}
function setDeepSleep() {
var cmd = new Uint8Array(1);
let ctrl_chrc = null;
brService.getCharacteristic(brUuid[7])
.then(chrc => {
ctrl_chrc = chrc;
cmd[0] = sys_deep_sleep;
return ctrl_chrc.writeValue(cmd)
})
.catch(error => {
log('Argh! ' + error);
return ctrl_chrc.writeValue(cmd)
});
}
function onDisconnected() {
log('> Bluetooth Device disconnected');
cancel = 0;
document.getElementById("divBtConn").style.display = 'block';
document.getElementById("divInfo").style.display = 'none';
document.getElementById("divFwSelect").style.display = 'none';
document.getElementById("divFwUpdate").style.display = 'none';
}
function btConn() {
log('Requesting Bluetooth Device...');
navigator.bluetooth.requestDevice(
{filters: [{namePrefix: 'BlueRetro'}],
optionalServices: [brUuid[0]]})
.then(device => {
log('Connecting to GATT Server...');
name = device.name;
bluetoothDevice = device;
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
return bluetoothDevice.gatt.connect();
})
.then(server => {
log('Getting BlueRetro Service...');
return server.getPrimaryService(brUuid[0]);
})
.then(service => {
brService = service;
return getBdAddr();
})
.then(_ => {
return getAppVersion();
})
.then(_ => {
document.getElementById("divInfo").innerHTML = 'Connected to: ' + name + ' (' + bdaddr + ') [' + app_ver + ']';
log('Init Cfg DOM...');
document.getElementById("divBtConn").style.display = 'none';
document.getElementById("divInfo").style.display = 'block';
document.getElementById("divFwSelect").style.display = 'block';
document.getElementById("divFwUpdate").style.display = 'none';
})
.catch(error => {
log('Argh! ' + error);
});
}