Skip to content

Commit 9863912

Browse files
authored
Merge pull request #56 from PropGit/Wifi_debug
Adds Wi-Fi based debugging
2 parents ad7b72d + 2b37689 commit 9863912

File tree

7 files changed

+315
-297
lines changed

7 files changed

+315
-297
lines changed

http.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,16 @@ PSocket.prototype.write = function(data) {
8080
var that = this;
8181
return new Promise(function(resolve, reject) {
8282
chrome.sockets.tcp.send(that.socketId, data, function(info) {
83-
if (info && info.resultCode >= 0)
84-
resolve(info.bytesSent);
85-
else
86-
reject(new Error('chrome sockets.tcp error ' + (info && info.resultCode)));
83+
if (!chrome.runtime.lastError) {
84+
if (info.resultCode === 0)
85+
resolve(info.bytesSent);
86+
else
87+
reject(new Error('Socket TCP error ' + (info.resultCode)));
88+
} else {
89+
let emsg = 'Socket ID ' + that.socketId + ' error: ' + chrome.runtime.lastError.message;
90+
console.log(emsg);
91+
reject(new Error(emsg));
92+
}
8793
});
8894
});
8995
};

index.js

Lines changed: 99 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,14 @@ var platform = pfUnk;
9292
Unknown ChromeOS Linux macOS Windows */
9393
portPattern = ["", "/dev/ttyUSB", "dev/tty", "/dev/cu.usbserial", "COM"];
9494

95-
// A list of connected websockets.
96-
var sockets = [];
97-
9895
// Http and ws servers
9996
var server = new http.Server();
10097
var wsServer = new http.WebSocketServer(server);
10198
var isServer = false;
10299

103-
// Keep track of the interval that sends the port list so it can be turned off
104-
var portListener = null;
100+
// Timer(s) to scan and send the port list
101+
var wScannerInterval = null;
102+
var portLister = [];
105103

106104
// Is verbose loggin turned on?
107105
var verboseLogging = false;
@@ -226,10 +224,14 @@ document.addEventListener('DOMContentLoaded', function() {
226224

227225
function connect() {
228226
connect_ws($('bpc-port').value, $('bpc-url').value);
227+
scanWPorts();
228+
wScannerInterval = setInterval(scanWPorts, 6010); // 6010: Scan at different intervals than send processes
229229
}
230230

231231
function disconnect() {
232232
closeSockets();
233+
clearInterval(wScannerInterval);
234+
wScannerInterval = null;
233235
}
234236

235237
function updateStatus(connected) {
@@ -254,36 +256,17 @@ function closeServer() {
254256
isServer = false;
255257
}
256258

257-
function findSocketIdx(socket) {
258-
/* Return index of socket in sockets list
259-
Returns -1 if not found*/
260-
return sockets.findIndex(function(s) {return s.socket === socket});
261-
}
262-
263259
function closeSockets() {
264-
// Close all sockets and remove them from the list
265-
while (sockets.length) {
266-
sockets[0].socket.close();
267-
deleteSocket(0);
268-
}
269-
}
270-
271-
function deleteSocket(socketOrIdx) {
272-
/* Delete socket from lists (sockets and ports)
273-
socketOrIdx is socket object or index of socket record to delete*/
274-
let idx = (typeof socketOrIdx === "number") ? socketOrIdx : findSocketIdx(socketOrIdx);
275-
// log("Deleting socket at index " + idx, mDbug);
276-
if (idx > -1 && idx < sockets.length) {
277-
// Clear port's knowledge of socket connection record
278-
if (sockets[idx].portIdx > -1) {
279-
// log(" Clearing port index " + sockets[idx].portIdx + " reference to this socket", mDbug);
280-
ports[sockets[idx].portIdx].bSocket = null;
281-
ports[sockets[idx].portIdx].bSocketIdx = -1;
260+
// Close all sockets and remove them from the ports and portLister lists
261+
ports.forEach(function(p) {
262+
if (p.bSocket) {
263+
p.bSocket.close();
264+
p.bSocket = null;
265+
}});
266+
while (portLister.length) {
267+
clearInterval(portLister[0].scanner);
268+
portLister.splice(0, 1);
282269
}
283-
// Delete socket connection record and adjust ports' later references down, if any
284-
sockets.splice(idx, 1);
285-
ports.forEach(function(v) {if (v.bSocketIdx > idx) {v.bSocketIdx--}});
286-
}
287270
}
288271

289272
function connect_ws(ws_port, url_path) {
@@ -308,14 +291,7 @@ function connect_ws(ws_port, url_path) {
308291

309292
wsServer.addEventListener('request', function(req) {
310293
var socket = req.accept();
311-
// log("Adding socket at index " + sockets.length, mDbug);
312-
sockets.push({socket:socket, portIdx:-1});
313-
314-
//Listen for ports
315-
if(portListener === null) {
316-
portListener = setInterval(function() {sendPortList();}, 5000);
317-
}
318-
294+
319295
socket.addEventListener('message', function(e) {
320296
if (isJson(e.data)) {
321297
var ws_msg = JSON.parse(e.data);
@@ -329,7 +305,11 @@ function connect_ws(ws_port, url_path) {
329305
serialTerminal(socket, ws_msg.action, ws_msg.portPath, ws_msg.baudrate, ws_msg.msg); // action is "open", "close" or "msg"
330306
// send an updated port list
331307
} else if (ws_msg.type === "port-list-request") {
332-
sendPortList();
308+
// Send port list now and set up scanner to send port list on regular interval
309+
// log("Browser requested port-list for socket " + socket.pSocket_.socketId, mDbug);
310+
sendPortList(socket);
311+
let s = setInterval(function() {sendPortList(socket)}, 5000);
312+
portLister.push({socket: socket, scanner: s});
333313
// Handle unknown messages
334314
} else if (ws_msg.type === "hello-browser") {
335315
helloClient(socket, ws_msg.baudrate || 115200);
@@ -347,14 +327,18 @@ function connect_ws(ws_port, url_path) {
347327
});
348328

349329

350-
// When a socket is closed, remove it from the list of connected sockets.
330+
// Browser socket closed; terminate its port scans and remove it from list of ports.
351331
socket.addEventListener('close', function() {
352-
deleteSocket(socket);
353-
if (sockets.length === 0) {
354-
updateStatus(false);
355-
clearInterval(portListener);
356-
portListener = null;
357-
chrome.app.window.current().drawAttention();
332+
log("Browser socket closing: " + socket.pSocket_.socketId, mDbug);
333+
let Idx = portLister.findIndex(function(s) {return s.socket === socket});
334+
if (Idx > -1) {
335+
clearInterval(portLister[Idx].scanner);
336+
portLister.splice(Idx, 1);
337+
}
338+
ports.forEach(function(p) {if (p.bSocket === socket) {p.bSocket = null}});
339+
if (!portLister.length) {
340+
updateStatus(false);
341+
chrome.app.window.current().drawAttention();
358342
}
359343
});
360344

@@ -405,49 +389,57 @@ function connect_ws(ws_port, url_path) {
405389
}
406390

407391
function enableWX() {
408-
wx_scanner_interval = setInterval(function() {
409-
discoverWirelessPorts();
410-
ageWirelessPorts();
411-
displayWirelessPorts();
412-
}, 3500);
392+
scanWXPorts();
393+
wScannerInterval = setInterval(scanWXPorts, 3500);
413394
}
414395

415396
function disableWX() {
416-
if(wx_scanner_interval) {
417-
clearInterval(wx_scanner_interval);
397+
if(wScannerInterval) {
398+
clearInterval(wScannerInterval);
418399
$('wx-list').innerHTML = '';
419400
}
420401
}
421402

422-
function sendPortList() {
423-
// find and send list of communication ports (filtered according to platform and type)
424-
chrome.serial.getDevices(
425-
function(portlist) {
426-
let wn = [];
427-
let wln = [];
428-
// update wired ports
429-
portlist.forEach(function(port) {
430-
if ((port.path.indexOf(portPattern[platform]) === 0) && (port.displayName.indexOf(' bt ') === -1 && port.displayName.indexOf('bluetooth') === -1)) {
431-
addPort({path: port.path});
432-
}
433-
});
434-
ageWiredPorts(); //Note, wired ports age here (just scanned) and wireless ports age elsewhere (where they are scanned)
435-
436-
// gather separated and sorted port lists (wired names and wireless names)
437-
ports.forEach(function(p) {if (p.isWired) {wn.push(p.path)} else {wln.push(p.path)}});
438-
wn.sort();
439-
wln.sort();
440-
441-
// report back to editor
442-
var msg_to_send = {type:'port-list',ports:wn.concat(wln)};
443-
for (var i = 0; i < sockets.length; i++) {
444-
sockets[i].socket.send(JSON.stringify(msg_to_send));
445-
if (chrome.runtime.lastError) {
446-
console.log(chrome.runtime.lastError);
403+
function scanWPorts() {
404+
// Generate list of current wired ports (filtered according to platform and type)
405+
chrome.serial.getDevices(
406+
function(portlist) {
407+
let wn = [];
408+
let wln = [];
409+
// update wired ports
410+
portlist.forEach(function(port) {
411+
if ((port.path.indexOf(portPattern[platform]) === 0) && (port.displayName.indexOf(' bt ') === -1 && port.displayName.indexOf('bluetooth') === -1)) {
412+
addPort({path: port.path});
413+
}
414+
});
415+
ageWiredPorts(); //Note, wired ports age here (just scanned) and wireless ports age elsewhere (where they are scanned)
447416
}
417+
);
418+
}
419+
420+
function scanWXPorts() {
421+
// Generate list of current wireless ports
422+
discoverWirelessPorts();
423+
ageWirelessPorts();
424+
displayWirelessPorts();
425+
}
426+
427+
function sendPortList(socket) {
428+
// Find and send list of communication ports (filtered according to platform and type) to browser via socket
429+
let wn = [];
430+
let wln = [];
431+
// log("sendPortList() for socket " + socket.pSocket_.socketId, mDbug);
432+
// gather separated and sorted port lists (wired names and wireless names)
433+
ports.forEach(function(p) {if (p.isWired) {wn.push(p.path)} else {wln.push(p.path)}});
434+
wn.sort();
435+
wln.sort();
436+
437+
// report back to editor
438+
var msg_to_send = {type:'port-list',ports:wn.concat(wln)};
439+
socket.send(JSON.stringify(msg_to_send));
440+
if (chrome.runtime.lastError) {
441+
console.log(chrome.runtime.lastError);
448442
}
449-
}
450-
);
451443
}
452444

453445

@@ -462,39 +454,34 @@ function serialTerminal(sock, action, portPath, baudrate, msg) {
462454
// Find port from portPath
463455
let port = findPort(byPath, portPath);
464456
if (port) {
465-
if(port.isWired) {
466-
if (action === "open") {
467-
// Open port for terminal use
468-
openPort(sock, portPath, baudrate, 'debug')
469-
.then(function() {log('Connected terminal to ' + portPath + ' at ' + baudrate + ' baud.');})
470-
.catch(function() {
471-
log('Unable to connect terminal to ' + portPath);
472-
var msg_to_send = {type:'serial-terminal', msg:'Failed to connect.\rPlease close this terminal and select a connected serial port.'};
473-
sock.send(JSON.stringify(msg_to_send));
474-
});
475-
} else if (action === "close") {
476-
/* Terminal closed. Keep port open because chrome.serial always toggles DTR upon closing (resetting the Propeller) which causes
477-
lots of unnecessary confusion (especially if an older version of the user's app is in the Propeller's EEPROM).
478-
Instead, update the connection mode so that serial debug data halts.*/
479-
port.mode = 'none';
480-
} else if (action === "msg") {
481-
// Message to send to the Propeller
482-
if (port.connId) {
483-
send(port, msg, false);
484-
}
485-
}
457+
// Convert msg from string or buffer to an ArrayBuffer
458+
if (typeof msg === 'string') {
459+
msg = str2ab(msg);
486460
} else {
487-
// TODO add WX module debug passthrough functions
488-
if (action === 'open') {
489-
490-
} else if (action === 'close') {
491-
492-
} else if (action === 'msg') {
493-
461+
if (msg instanceof ArrayBuffer === false) {msg = buf2ab(msg);}
462+
}
463+
if (action === "open") {
464+
// Open port for terminal use
465+
openPort(sock, portPath, baudrate, 'debug')
466+
.then(function() {log('Connected terminal to ' + portPath + ' at ' + baudrate + ' baud.');})
467+
.catch(function() {
468+
log('Unable to connect terminal to ' + portPath);
469+
var msg_to_send = {type:'serial-terminal', msg:'Failed to connect.\rPlease close this terminal and select a connected port.'};
470+
sock.send(JSON.stringify(msg_to_send));
471+
});
472+
} else if (action === "close") {
473+
/* Terminal closed. Keep wired port open because chrome.serial always toggles DTR upon closing (resetting the Propeller) which causes
474+
lots of unnecessary confusion (especially if an older version of the user's app is in the Propeller's EEPROM).
475+
Instead, update the connection mode so that serial debug data halts.*/
476+
port.mode = 'none';
477+
} else if (action === "msg") {
478+
// Message to send to the Propeller
479+
if ((port.isWired && port.connId) || (port.isWireless && port.ptSocket)) { //Send only if port is open
480+
send(port, msg, false);
494481
}
495482
}
496483
} else {
497-
var msg_to_send = {type:'serial-terminal', msg:'Failed to connect.\rPlease close this terminal and select a valid serial port.'};
484+
var msg_to_send = {type:'serial-terminal', msg:'Port ' + portPath + ' not found.\rPlease close this terminal and select an existing port.'};
498485
sock.send(JSON.stringify(msg_to_send));
499486
}
500487
}

0 commit comments

Comments
 (0)