diff --git a/index.js b/index.js index 01cf4fd..eccb202 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..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.2", + "version": "0.7.4", "manifest_version": 2, "minimum_chrome_version": "45", diff --git a/serial.js b/serial.js index de54cc0..28bb2c7 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*/