From 081c2975cead659ed1dbf8db978ad420e441a1e2 Mon Sep 17 00:00:00 2001 From: Jeff Martin Date: Mon, 7 Aug 2017 11:57:51 -0700 Subject: [PATCH 1/3] [#37] Partial fix or Terminal keyboard chokes BPL. Still possible to lose connection and not reassociate new socket with open serial port due to issue on cloud-side. This commit has numerous debugging logs for socket and port list monitoring. --- index.js | 19 +++++++++++--- manifest.json | 2 +- serial.js | 71 +++++++++++++++++++++++++++++++-------------------- 3 files changed, 60 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index 01cf4fd..c172617 100644 --- a/index.js +++ b/index.js @@ -234,13 +234,15 @@ function deleteSocket(socketOrIdx) { /* Delete socket from lists (sockets and ports) socketOrIdx is socket object or index of socket record to delete*/ let idx = (typeof socketOrIdx === "number") ? socketOrIdx : findSocketIdx(socketOrIdx); + log("Deleting socket at index " + idx, mDbug); if (idx > -1 && idx < sockets.length) { // Clear USB's knowledge of socket connection record if (sockets[idx].serialIdx > -1) { + log(" Clearing port index " + sockets[idx].serialIdx + " reference to this socket", mDbug); ports[sockets[idx].serialIdx].socket = null; ports[sockets[idx].serialIdx].socketIdx = -1; } - // Delete socket connection record and adjust USB's later references down, if any + // Delete socket connection record and adjust ports' later references down, if any sockets.splice(idx, 1); ports.forEach(function(v) {if (v.socketIdx > idx) {v.socketIdx--}}); } @@ -268,6 +270,7 @@ function connect_ws(ws_port, url_path) { wsServer.addEventListener('request', function(req) { var socket = req.accept(); + log("Adding socket at index " + sockets.length, mDbug); sockets.push({socket:socket, serialIdx:-1}); //Listen for ports @@ -289,7 +292,7 @@ function connect_ws(ws_port, url_path) { // open or close the serial port for terminal/debug } else if (ws_msg.type === "serial-terminal") { - serialTerminal(socket, ws_msg.action, ws_msg.portPath, ws_msg.baudrate, ws_msg.msg); // action is "open" or "close" + serialTerminal(socket, ws_msg.action, ws_msg.portPath, ws_msg.baudrate, ws_msg.msg); // action is "open", "close" or "msg" // send an updated port list } else if (ws_msg.type === "port-list-request") { @@ -417,7 +420,17 @@ function serialTerminal(sock, action, portPath, baudrate, msg) { if (conn) {conn.mode = 'none'} } else if (action === "msg") { // Serial message to send to the device - send(findPortId(portPath), msg); + // Find port connection id from portPath or socket + let cid = findPortId(portPath); + if (!cid) { + let sIdx = findSocketIdx(sock); + if (sIdx > -1) { + cid = (sockets[sIdx].serialIdx > -1) ? ports[sockets[sIdx].serialIdx].connId : null; + } + } + if (cid) { + send(cid, msg); + } } } diff --git a/manifest.json b/manifest.json index 17d441f..36d2235 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "name": "BlocklyProp Launcher", "description": "A Chrome application that connects your Propeller-Powered Hardware to the BlocklyProp website.", - "version": "0.7.2", + "version": "0.7.3", "manifest_version": 2, "minimum_chrome_version": "45", diff --git a/serial.js b/serial.js index de54cc0..fbfd36e 100644 --- a/serial.js +++ b/serial.js @@ -110,9 +110,7 @@ function openPort(sock, portPath, baudrate, connMode) { var cid = findPortId(portPath); if (cid) { //Already open; ensure correct baudrate, socket, and connMode, then resolve. - changeBaudrate(cid, baudrate) - .then(function() {updatePortSocket(cid, sock)}) - .then(function() {findPort(cid).mode = connMode}) + updatePort(sock, cid, connMode, baudrate) .then(function() {resolve(cid)}) .catch(function (e) {reject(e)}); } else { @@ -127,7 +125,7 @@ function openPort(sock, portPath, baudrate, connMode) { function (openInfo) { if (!chrome.runtime.lastError) { // No error; create serial port object - addPort(sock, openInfo.connectionId, connMode, portPath, baudrate); + addPort(openInfo.connectionId, sock, connMode, portPath, baudrate); log("Port " + portPath + " open with ID " + openInfo.connectionId, mStat); resolve(openInfo.connectionId); } else { @@ -281,36 +279,20 @@ chrome.serial.onReceiveError.addListener(function(info) { // log("Error: PortID "+info.connectionId+" "+info.error, mDeep); }); -function updatePortSocket(cid, newSocket) { - /* Update port cid's socket references - cid is the open port's connection identifier - newSocket is the new socket object*/ - let cIdx = findPortIdx(cid); - if (cIdx > -1) { - let sIdx = (newSocket) ? findSocketIdx(newSocket) : -1; - if (ports[cIdx].socketIdx !== sIdx) { - // newSocket is different; update required - if (ports[cIdx].socketIdx !== -1) { - // Adjust existing socket's record - sockets[ports[cIdx].socketIdx].serialIdx = -1; - } - // Update port and socket records - ports[cIdx].socket = newSocket; - ports[cIdx].socketIdx = sIdx; - sockets[sIdx].serialIdx = cIdx; - } - } -} - -function addPort(socket, cid, connMode, portPath, portBaudrate) { +function addPort(cid, socket, connMode, portPath, portBaudrate) { // Add new serial port record let idx = findSocketIdx(socket); + if (idx = -1) { + log("Adding port at index " + ports.length, mDbug); + } else { + log("Adding port at index " + sockets.length + " referencing socket at index " + idx, mDbug); + } ports.push({ + connId : cid, + path : portPath, socket : socket, socketIdx : idx, - connId : cid, mode : connMode, - path : portPath, baud : portBaudrate, packet : {} }); @@ -320,6 +302,39 @@ function addPort(socket, cid, connMode, portPath, portBaudrate) { if (idx > -1) {sockets[idx].serialIdx = ports.length-1} } +function updatePort(socket, cid, connMode, portBaudrate) { +// Update port attributes if necessary +// Automatically handles special cases like baudrate changes and sockets<->ports links + return new Promise(function(resolve, reject) { + let cIdx = findPortIdx(cid); + log("Updating port at index " + cIdx, mDbug); + if (cIdx > -1) { + //Update sockets<->ports links as necessary + let sIdx = (socket) ? findSocketIdx(socket) : -1; + if (ports[cIdx].socketIdx !== sIdx) { + // newSocket is different; update required + log(" Linking to socket index " + sIdx, mDbug); + if (ports[cIdx].socketIdx !== -1) { + // Adjust existing socket's record + sockets[ports[cIdx].socketIdx].serialIdx = -1; + } + // Update port and socket records + ports[cIdx].socket = socket; + ports[cIdx].socketIdx = sIdx; + if (sIdx > -1) { + sockets[sIdx].serialIdx = cIdx; + } + } + //Update connection mode + ports[cIdx].mode = connMode; + //Update baudrate + changeBaudrate(cid, portBaudrate) + .then(function (p) {resolve(p)}) + .catch(function (e) {reject(e)}); + } + }) +} + function findPortId(portPath) { /* Return id (cid) of serial port associated with portPath Returns null if not found*/ From 01d455042ba6d111b7cc18f9fc3e85ea8db7fbd0 Mon Sep 17 00:00:00 2001 From: Jeff Martin Date: Mon, 7 Aug 2017 13:39:28 -0700 Subject: [PATCH 2/3] Commented out socket and port adjustment logs. --- index.js | 6 +++--- serial.js | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index c172617..eccb202 100644 --- a/index.js +++ b/index.js @@ -234,11 +234,11 @@ function deleteSocket(socketOrIdx) { /* Delete socket from lists (sockets and ports) socketOrIdx is socket object or index of socket record to delete*/ let idx = (typeof socketOrIdx === "number") ? socketOrIdx : findSocketIdx(socketOrIdx); - log("Deleting socket at index " + idx, mDbug); +// log("Deleting socket at index " + idx, mDbug); if (idx > -1 && idx < sockets.length) { // Clear USB's knowledge of socket connection record if (sockets[idx].serialIdx > -1) { - log(" Clearing port index " + sockets[idx].serialIdx + " reference to this socket", mDbug); +// log(" Clearing port index " + sockets[idx].serialIdx + " reference to this socket", mDbug); ports[sockets[idx].serialIdx].socket = null; ports[sockets[idx].serialIdx].socketIdx = -1; } @@ -270,7 +270,7 @@ function connect_ws(ws_port, url_path) { wsServer.addEventListener('request', function(req) { var socket = req.accept(); - log("Adding socket at index " + sockets.length, mDbug); +// log("Adding socket at index " + sockets.length, mDbug); sockets.push({socket:socket, serialIdx:-1}); //Listen for ports diff --git a/serial.js b/serial.js index fbfd36e..28bb2c7 100644 --- a/serial.js +++ b/serial.js @@ -282,11 +282,11 @@ chrome.serial.onReceiveError.addListener(function(info) { function addPort(cid, socket, connMode, portPath, portBaudrate) { // Add new serial port record let idx = findSocketIdx(socket); - if (idx = -1) { +/* if (idx = -1) { log("Adding port at index " + ports.length, mDbug); } else { log("Adding port at index " + sockets.length + " referencing socket at index " + idx, mDbug); - } + }*/ ports.push({ connId : cid, path : portPath, @@ -307,13 +307,13 @@ function updatePort(socket, cid, connMode, portBaudrate) { // Automatically handles special cases like baudrate changes and sockets<->ports links return new Promise(function(resolve, reject) { let cIdx = findPortIdx(cid); - log("Updating port at index " + cIdx, mDbug); +// log("Updating port at index " + cIdx, mDbug); if (cIdx > -1) { //Update sockets<->ports links as necessary let sIdx = (socket) ? findSocketIdx(socket) : -1; if (ports[cIdx].socketIdx !== sIdx) { // newSocket is different; update required - log(" Linking to socket index " + sIdx, mDbug); +// log(" Linking to socket index " + sIdx, mDbug); if (ports[cIdx].socketIdx !== -1) { // Adjust existing socket's record sockets[ports[cIdx].socketIdx].serialIdx = -1; From 241dca511520b0302b9a4e7763db498f0dfb1785 Mon Sep 17 00:00:00 2001 From: Jeff Martin Date: Mon, 7 Aug 2017 13:53:48 -0700 Subject: [PATCH 3/3] Bumped version to v0.7.4. --- manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifest.json b/manifest.json index 36d2235..36d280f 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "name": "BlocklyProp Launcher", "description": "A Chrome application that connects your Propeller-Powered Hardware to the BlocklyProp website.", - "version": "0.7.3", + "version": "0.7.4", "manifest_version": 2, "minimum_chrome_version": "45",