Skip to content

Commit 9f7180f

Browse files
committed
Enhanced to treat ports by friendly name in user's visual port list, and internally by name and path as needed.
1 parent 0f4719c commit 9f7180f

File tree

5 files changed

+70
-52
lines changed

5 files changed

+70
-52
lines changed

index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const winPortOriginLen = 4;
8181
/* Serial port ID pattern (index into with platform value)
8282
Unknown ChromeOS Linux macOS Windows */
8383
portPattern = ["", "/dev/ttyUSB", "dev/tty", "/dev/cu.usbserial", "COM"];
84+
portDelim = ["", "/", "/", "/", "\\"];
8485

8586
// Http and ws servers
8687
var server = new http.Server();
@@ -284,7 +285,7 @@ function connect_ws(ws_port, url_path) {
284285
socket.addEventListener('message', function(e) {
285286
if (isJson(e.data)) {
286287
var ws_msg = JSON.parse(e.data);
287-
288+
//Note: ws.msg.portPath is now really a "pathless" portName but kept named as-is for legacy support
288289
// load the propeller
289290
if (ws_msg.type === "load-prop") {
290291
log('Received Propeller Application for ' + ws_msg.action);
@@ -397,10 +398,10 @@ function scanWPorts() {
397398
let wln = [];
398399
// update wired ports
399400
portlist.forEach(function(port) {
400-
// If Windows, strip potential leading port origin path from port path
401-
if ((platform === pfWin) && (port.path.indexOf(winPortOrigin) === 0)) {port.path = port.path.slice(winPortOriginLen)}
401+
// Get consistently formatted port path; If Windows, strip off possible leading port origin path for ease in comparison
402+
var portPath = ((platform === pfWin) && (port.path.indexOf(winPortOrigin) === 0)) ? port.path.slice(winPortOriginLen) : port.path;
402403
// Add only proper port types (platform specific and excluding bluetooth ports)
403-
if ((port.path.indexOf(portPattern[platform]) === 0) && (port.displayName.indexOf(' bt ') === -1 && port.displayName.indexOf('bluetooth') === -1)) {
404+
if ((portPath.indexOf(portPattern[platform]) === 0) && (port.displayName.indexOf(' bt ') === -1 && port.displayName.indexOf('bluetooth') === -1)) {
404405
addPort({path: port.path});
405406
}
406407
});
@@ -422,7 +423,7 @@ function sendPortList(socket) {
422423
let wln = [];
423424
// log("sendPortList() for socket " + socket.pSocket_.socketId, mDbug);
424425
// gather separated and sorted port lists (wired names and wireless names)
425-
ports.forEach(function(p) {if (p.isWired) {wn.push(p.path)} else {wln.push(p.path)}});
426+
ports.forEach(function(p) {if (p.isWired) {wn.push(p.name)} else {wln.push(p.name)}});
426427
wn.sort();
427428
wln.sort();
428429

@@ -442,9 +443,9 @@ function helloClient(sock, baudrate) {
442443

443444
//TODO Check send results and act accordingly?
444445
//TODO refactor to combine usb and wx-based port code efficiently
445-
function serialTerminal(sock, action, portPath, baudrate, msg) {
446-
// Find port from portPath
447-
let port = findPort(byPath, portPath);
446+
function serialTerminal(sock, action, portName, baudrate, msg) {
447+
// Find port from portName
448+
let port = findPort(byName, portName);
448449
if (port) {
449450
// Convert msg from string or buffer to an ArrayBuffer
450451
if (typeof msg === 'string') {
@@ -454,10 +455,10 @@ function serialTerminal(sock, action, portPath, baudrate, msg) {
454455
}
455456
if (action === "open") {
456457
// Open port for terminal use
457-
openPort(sock, portPath, baudrate, 'debug')
458-
.then(function() {log('Connected terminal to ' + portPath + ' at ' + baudrate + ' baud.');})
458+
openPort(sock, port.name, baudrate, 'debug')
459+
.then(function() {log('Connected terminal to ' + portName + ' at ' + baudrate + ' baud.');})
459460
.catch(function() {
460-
log('Unable to connect terminal to ' + portPath);
461+
log('Unable to connect terminal to ' + portName);
461462
var msg_to_send = {type:'serial-terminal', msg:'Failed to connect.\rPlease close this terminal and select a connected port.'};
462463
sock.send(JSON.stringify(msg_to_send));
463464
});
@@ -473,7 +474,7 @@ function serialTerminal(sock, action, portPath, baudrate, msg) {
473474
}
474475
}
475476
} else {
476-
var msg_to_send = {type:'serial-terminal', msg:'Port ' + portPath + ' not found.\rPlease close this terminal and select an existing port.'};
477+
var msg_to_send = {type:'serial-terminal', msg:'Port ' + portName + ' not found.\rPlease close this terminal and select an existing port.'};
477478
sock.send(JSON.stringify(msg_to_send));
478479
}
479480
}

loader.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ function deferredPromise() {
9595
//TODO Determine why port not found error message is not getting back to the UI
9696
//TODO Determine how to gracefully handle the need to reset baudrate if error occurs but port is valid (as opposed to error caused by invalid port
9797
//TODO Remove hard-coded example applications
98-
function loadPropeller(sock, portPath, action, payload, debug) {
99-
/* Download payload to Propeller with action on portPath. If debug, keep port open for communication with sock.
98+
function loadPropeller(sock, portName, action, payload, debug) {
99+
/* Download payload to Propeller with action on portName. If debug, keep port open for communication with sock.
100100
sock may be null (if for development purposes)
101-
portPath is wired or wireless port's pathname
101+
portName is wired or wireless port's name
102102
action is 'RAM' or 'EEPROM'
103103
payload is base-64 encoded .elf, .binary, or .eeprom data containing the Propeller Application image
104104
debug is true if a terminal is intended to connect to the Propeller after download; false otherwise*/
@@ -114,7 +114,7 @@ function loadPropeller(sock, portPath, action, payload, debug) {
114114
}
115115

116116
// Look for an existing port
117-
let port = findPort(byPath, portPath);
117+
let port = findPort(byName, portName);
118118
if (port) {
119119
// Port found
120120
let connect;
@@ -129,7 +129,7 @@ function loadPropeller(sock, portPath, action, payload, debug) {
129129
} else {
130130
// No connection yet, prep to create one
131131
originalBaudrate = initialBaudrate;
132-
connect = function() {return openPort(sock, portPath, initialBaudrate, "programming")}
132+
connect = function() {return openPort(sock, portName, initialBaudrate, "programming")}
133133
}
134134
} else {
135135
//TODO Retrieve actual current baudrate
@@ -140,7 +140,7 @@ function loadPropeller(sock, portPath, action, payload, debug) {
140140
// Use connection to download application to the Propeller
141141
connect()
142142
.then(function() {listen(port, true)}) //Enable listener
143-
.then(function() {log(notice(000, ["Scanning port " + portPath]), mUser, sock)}) //Notify what port we're using
143+
.then(function() {log(notice(000, ["Scanning port " + portName]), mUser, sock)}) //Notify what port we're using
144144
.then(function() {return talkToProp(sock, port, binImage, action === 'EEPROM')}) //Download user application to RAM or EEPROM
145145
.then(function() {return changeBaudrate(port, originalBaudrate)}) //Restore original baudrate
146146
.then(function() { //Success! Open terminal or graph if necessary
@@ -167,7 +167,7 @@ function loadPropeller(sock, portPath, action, payload, debug) {
167167
.catch(function(e) {log(e.message, mAll, sock);});
168168
} else {
169169
// Port not found
170-
log(notice(neCanNotFindPort, [portPath]), mAll, sock);
170+
log(notice(neCanNotFindPort, [portName]), mAll, sock);
171171
}
172172
}
173173

port.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const byPHID = "phSocket"; //Represents numeric Propeller HTTP Socket ID
77
const byPTID = "ptSocket"; //Represents numeric Propeller Telnet Socket ID type
88
const byMAC = "mac"; //Represents alphanumeric MAC address type
99
const byPath = "path"; //Represents alphanumeric path (wired/wireless port identifier) type
10+
const byName = "name"; //Represents alphanumeric name (wired/wireless port identifier) type
1011

1112
// Port's max lifetime
1213
const wLife = 2;
@@ -25,7 +26,12 @@ const serPacket = {
2526
timer : null,
2627
};
2728

28-
function makePortName(mac) {
29+
function makePortName(name) {
30+
/* Return friendly port name, excluding leading path.*/
31+
return name.slice(name.lastIndexOf(portDelim[platform])+1);
32+
}
33+
34+
function makeWLPortName(mac) {
2935
/* Return wireless fabricated name in the form 'wx-#######' using last 6 digits of it's MAC address.*/
3036
return 'wx-' + mac.substr(9,16).replace(/\:/g,'');
3137
}
@@ -45,19 +51,20 @@ function addPort(alist) {
4551

4652
if (!exists("path", alist)) {return}
4753
if (!alist.path) {
48-
//Empty port path? If wireless port details provided, craft path from mac, else abort (return)
49-
if (get("ip", alist, "") && get("mac", alist, "")) {alist.path = makePortName(alist.mac)} else {return}
54+
//Empty port path? If wireless port details provided, craft port name from mac, else abort (return)
55+
if (get("ip", alist, "") && get("mac", alist, "")) {alist.path = makeWLPortName(alist.mac)} else {return}
5056
}
51-
// Look for existing port (mac used for wireless ports since path may have changed since last discovery)
57+
// Look for existing port (mac used for wireless ports since path/name may have changed since last discovery)
5258
let port = (get("mac", alist, "")) ? findPort(byMAC, alist.mac) : findPort(byPath, alist.path);
5359
if (port) {
54-
// Exists already? Update it's (portPath or iP)
60+
// Exists already? Update it's (portPath/Name or iP)
5561
updatePort(port, alist);
5662
} else {
5763
// else, add it as a new port record (all fields included; many with default values to be updated later)
5864
log("Adding port: " + alist.path, mDbug);
5965
ports.push({
60-
path : alist.path, /*[<>""] Wired port path, or wireless port's custom name, or fabricated name; never empty*/
66+
name : makePortName(alist.path), /*[<>""] Friendly port name; never empty, does not include path*/
67+
path : alist.path, /*[<>""] Wired port path+name, or wireless port's custom name, or fabricated name; never empty*/
6168
connId : get("connId", alist, null), /*[null+] Holds wired serial port's connection id (if open), null (if closed)*/
6269
mac : get("mac", alist, ""), /*[""+] Holds wireless port's MAC address*/
6370
ip : get("ip", alist, ""), /*[""+] Wireless port's IP address; */
@@ -81,6 +88,7 @@ function updatePort(port, alist) {
8188
/* Update port attributes if necessary. Automatically handles special case of baudrate changes.
8289
port: [required] port object to update
8390
alist: [required] one or more attributes of port to update. Unchanging attributes can be omitted. Possible attributes are:
91+
name: the friendly name of the port (not including path). Can be empty ("") and a name will be created from path
8492
path: the string path to the wired serial port, or custom name of wireless port. Can be empty ("") and a wireless name will be fabricated from the MAC address
8593
connId: unique identifier for the wired serial port connection id
8694
ip: the wireless port's IP address
@@ -96,12 +104,20 @@ function updatePort(port, alist) {
96104
if (exists(attr, alist)) {port[attr] = alist[attr]}
97105
}
98106

99-
if (exists("path", alist) && !alist.path) {
100-
// Empty port path? If wireless port, craft path from mac, else abort (reject)
101-
if (port.isWireless && port.mac) {alist.path = makePortName(port.mac)} else {reject(Error("path required!")); return}
107+
if (exists("path", alist)) {
108+
if (!alist.path) {
109+
// Empty port path? If wireless port, craft path from mac, else abort (reject)
110+
if (port.isWireless && port.mac) {alist.path = makeWLPortName(port.mac)} else {reject(Error("path required!")); return}
111+
}
112+
if (exists("name", alist) && !alist.name) {
113+
// Empty port name? Create it.
114+
alist.name = makePortName(alist.path);
115+
}
102116
}
117+
103118
// log("Updating port '" + port.path + "' with " + alist, mDbug);
104119
// Apply updates (if necessary) as well as special handling
120+
set("name");
105121
set("path");
106122
set("connId");
107123
set("ip");
@@ -131,6 +147,7 @@ function findPortIdx(type, clue) {
131147
byPTID / numeric Propeller Telnet Socket ID
132148
byMAC / alphanumeric MAC address
133149
byPath / alphanumeric path (wired/wireless port identifier)
150+
byName / alphanumeric name (wired/wireless port identifier)
134151
Returns -1 if not found*/
135152
return ports.findIndex(function(p) {return p[type] === clue});
136153
}
@@ -143,14 +160,15 @@ function findPort(type, clue) {
143160
byPTID / numeric Propeller Telnet Socket ID
144161
byMAC / alphanumeric MAC address
145162
byPath / alphanumeric path (wired/wireless port identifier)
163+
byName / alphanumeric name (wired/wireless port identifier)
146164
Returns null if not found*/
147165
let cn = 0;
148166
// Find port record based on scan function
149167
function findConn(scan) {
150168
while (cn < ports.length && !scan()) {cn++}
151169
return cn < ports.length ? ports[cn] : null;
152170
}
153-
// Scan for ID, MAC, or path
171+
// Scan for ID, MAC, path, or name
154172
return findConn(function() {return ports[cn][type] === clue})
155173
}
156174

@@ -161,7 +179,8 @@ function deletePort(type, clue) {
161179
byPHID / numeric Propeller HTTP Socket ID
162180
byPTID / numeric Propeller Telnet Socket ID
163181
byMAC / alphanumeric MAC address
164-
byPath / alphanumeric path (wired/wireless port identifier)*/
182+
byPath / alphanumeric path (wired/wireless port identifier)
183+
byName / alphanumeric name (wired/wireless port identifier)*/
165184
let idx = findPortIdx(type, clue);
166185
if (idx > -1) {
167186
log("Deleting port: " + ports[idx].path, mDbug);

0 commit comments

Comments
 (0)