Skip to content

Commit cb6a83b

Browse files
authored
Merge pull request #42 from PropGit/master
Fixed Issue #37 - BPL chokes when user types into terminal
2 parents 91a7137 + 241dca5 commit cb6a83b

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,15 @@ function deleteSocket(socketOrIdx) {
234234
/* Delete socket from lists (sockets and ports)
235235
socketOrIdx is socket object or index of socket record to delete*/
236236
let idx = (typeof socketOrIdx === "number") ? socketOrIdx : findSocketIdx(socketOrIdx);
237+
// log("Deleting socket at index " + idx, mDbug);
237238
if (idx > -1 && idx < sockets.length) {
238239
// Clear USB's knowledge of socket connection record
239240
if (sockets[idx].serialIdx > -1) {
241+
// log(" Clearing port index " + sockets[idx].serialIdx + " reference to this socket", mDbug);
240242
ports[sockets[idx].serialIdx].socket = null;
241243
ports[sockets[idx].serialIdx].socketIdx = -1;
242244
}
243-
// Delete socket connection record and adjust USB's later references down, if any
245+
// Delete socket connection record and adjust ports' later references down, if any
244246
sockets.splice(idx, 1);
245247
ports.forEach(function(v) {if (v.socketIdx > idx) {v.socketIdx--}});
246248
}
@@ -268,6 +270,7 @@ function connect_ws(ws_port, url_path) {
268270

269271
wsServer.addEventListener('request', function(req) {
270272
var socket = req.accept();
273+
// log("Adding socket at index " + sockets.length, mDbug);
271274
sockets.push({socket:socket, serialIdx:-1});
272275

273276
//Listen for ports
@@ -289,7 +292,7 @@ function connect_ws(ws_port, url_path) {
289292

290293
// open or close the serial port for terminal/debug
291294
} else if (ws_msg.type === "serial-terminal") {
292-
serialTerminal(socket, ws_msg.action, ws_msg.portPath, ws_msg.baudrate, ws_msg.msg); // action is "open" or "close"
295+
serialTerminal(socket, ws_msg.action, ws_msg.portPath, ws_msg.baudrate, ws_msg.msg); // action is "open", "close" or "msg"
293296

294297
// send an updated port list
295298
} else if (ws_msg.type === "port-list-request") {
@@ -417,7 +420,17 @@ function serialTerminal(sock, action, portPath, baudrate, msg) {
417420
if (conn) {conn.mode = 'none'}
418421
} else if (action === "msg") {
419422
// Serial message to send to the device
420-
send(findPortId(portPath), msg);
423+
// Find port connection id from portPath or socket
424+
let cid = findPortId(portPath);
425+
if (!cid) {
426+
let sIdx = findSocketIdx(sock);
427+
if (sIdx > -1) {
428+
cid = (sockets[sIdx].serialIdx > -1) ? ports[sockets[sIdx].serialIdx].connId : null;
429+
}
430+
}
431+
if (cid) {
432+
send(cid, msg);
433+
}
421434
}
422435
}
423436

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "BlocklyProp Launcher",
33
"description": "A Chrome application that connects your Propeller-Powered Hardware to the BlocklyProp website.",
4-
"version": "0.7.2",
4+
"version": "0.7.4",
55
"manifest_version": 2,
66
"minimum_chrome_version": "45",
77

serial.js

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ function openPort(sock, portPath, baudrate, connMode) {
110110
var cid = findPortId(portPath);
111111
if (cid) {
112112
//Already open; ensure correct baudrate, socket, and connMode, then resolve.
113-
changeBaudrate(cid, baudrate)
114-
.then(function() {updatePortSocket(cid, sock)})
115-
.then(function() {findPort(cid).mode = connMode})
113+
updatePort(sock, cid, connMode, baudrate)
116114
.then(function() {resolve(cid)})
117115
.catch(function (e) {reject(e)});
118116
} else {
@@ -127,7 +125,7 @@ function openPort(sock, portPath, baudrate, connMode) {
127125
function (openInfo) {
128126
if (!chrome.runtime.lastError) {
129127
// No error; create serial port object
130-
addPort(sock, openInfo.connectionId, connMode, portPath, baudrate);
128+
addPort(openInfo.connectionId, sock, connMode, portPath, baudrate);
131129
log("Port " + portPath + " open with ID " + openInfo.connectionId, mStat);
132130
resolve(openInfo.connectionId);
133131
} else {
@@ -281,36 +279,20 @@ chrome.serial.onReceiveError.addListener(function(info) {
281279
// log("Error: PortID "+info.connectionId+" "+info.error, mDeep);
282280
});
283281

284-
function updatePortSocket(cid, newSocket) {
285-
/* Update port cid's socket references
286-
cid is the open port's connection identifier
287-
newSocket is the new socket object*/
288-
let cIdx = findPortIdx(cid);
289-
if (cIdx > -1) {
290-
let sIdx = (newSocket) ? findSocketIdx(newSocket) : -1;
291-
if (ports[cIdx].socketIdx !== sIdx) {
292-
// newSocket is different; update required
293-
if (ports[cIdx].socketIdx !== -1) {
294-
// Adjust existing socket's record
295-
sockets[ports[cIdx].socketIdx].serialIdx = -1;
296-
}
297-
// Update port and socket records
298-
ports[cIdx].socket = newSocket;
299-
ports[cIdx].socketIdx = sIdx;
300-
sockets[sIdx].serialIdx = cIdx;
301-
}
302-
}
303-
}
304-
305-
function addPort(socket, cid, connMode, portPath, portBaudrate) {
282+
function addPort(cid, socket, connMode, portPath, portBaudrate) {
306283
// Add new serial port record
307284
let idx = findSocketIdx(socket);
285+
/* if (idx = -1) {
286+
log("Adding port at index " + ports.length, mDbug);
287+
} else {
288+
log("Adding port at index " + sockets.length + " referencing socket at index " + idx, mDbug);
289+
}*/
308290
ports.push({
291+
connId : cid,
292+
path : portPath,
309293
socket : socket,
310294
socketIdx : idx,
311-
connId : cid,
312295
mode : connMode,
313-
path : portPath,
314296
baud : portBaudrate,
315297
packet : {}
316298
});
@@ -320,6 +302,39 @@ function addPort(socket, cid, connMode, portPath, portBaudrate) {
320302
if (idx > -1) {sockets[idx].serialIdx = ports.length-1}
321303
}
322304

305+
function updatePort(socket, cid, connMode, portBaudrate) {
306+
// Update port attributes if necessary
307+
// Automatically handles special cases like baudrate changes and sockets<->ports links
308+
return new Promise(function(resolve, reject) {
309+
let cIdx = findPortIdx(cid);
310+
// log("Updating port at index " + cIdx, mDbug);
311+
if (cIdx > -1) {
312+
//Update sockets<->ports links as necessary
313+
let sIdx = (socket) ? findSocketIdx(socket) : -1;
314+
if (ports[cIdx].socketIdx !== sIdx) {
315+
// newSocket is different; update required
316+
// log(" Linking to socket index " + sIdx, mDbug);
317+
if (ports[cIdx].socketIdx !== -1) {
318+
// Adjust existing socket's record
319+
sockets[ports[cIdx].socketIdx].serialIdx = -1;
320+
}
321+
// Update port and socket records
322+
ports[cIdx].socket = socket;
323+
ports[cIdx].socketIdx = sIdx;
324+
if (sIdx > -1) {
325+
sockets[sIdx].serialIdx = cIdx;
326+
}
327+
}
328+
//Update connection mode
329+
ports[cIdx].mode = connMode;
330+
//Update baudrate
331+
changeBaudrate(cid, portBaudrate)
332+
.then(function (p) {resolve(p)})
333+
.catch(function (e) {reject(e)});
334+
}
335+
})
336+
}
337+
323338
function findPortId(portPath) {
324339
/* Return id (cid) of serial port associated with portPath
325340
Returns null if not found*/

0 commit comments

Comments
 (0)