@@ -92,16 +92,14 @@ var platform = pfUnk;
92
92
Unknown ChromeOS Linux macOS Windows */
93
93
portPattern = [ "" , "/dev/ttyUSB" , "dev/tty" , "/dev/cu.usbserial" , "COM" ] ;
94
94
95
- // A list of connected websockets.
96
- var sockets = [ ] ;
97
-
98
95
// Http and ws servers
99
96
var server = new http . Server ( ) ;
100
97
var wsServer = new http . WebSocketServer ( server ) ;
101
98
var isServer = false ;
102
99
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 = [ ] ;
105
103
106
104
// Is verbose loggin turned on?
107
105
var verboseLogging = false ;
@@ -226,10 +224,14 @@ document.addEventListener('DOMContentLoaded', function() {
226
224
227
225
function connect ( ) {
228
226
connect_ws ( $ ( 'bpc-port' ) . value , $ ( 'bpc-url' ) . value ) ;
227
+ scanWPorts ( ) ;
228
+ wScannerInterval = setInterval ( scanWPorts , 6010 ) ; // 6010: Scan at different intervals than send processes
229
229
}
230
230
231
231
function disconnect ( ) {
232
232
closeSockets ( ) ;
233
+ clearInterval ( wScannerInterval ) ;
234
+ wScannerInterval = null ;
233
235
}
234
236
235
237
function updateStatus ( connected ) {
@@ -254,36 +256,17 @@ function closeServer() {
254
256
isServer = false ;
255
257
}
256
258
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
-
263
259
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 ) ;
282
269
}
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
- }
287
270
}
288
271
289
272
function connect_ws ( ws_port , url_path ) {
@@ -308,14 +291,7 @@ function connect_ws(ws_port, url_path) {
308
291
309
292
wsServer . addEventListener ( 'request' , function ( req ) {
310
293
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
+
319
295
socket . addEventListener ( 'message' , function ( e ) {
320
296
if ( isJson ( e . data ) ) {
321
297
var ws_msg = JSON . parse ( e . data ) ;
@@ -329,7 +305,11 @@ function connect_ws(ws_port, url_path) {
329
305
serialTerminal ( socket , ws_msg . action , ws_msg . portPath , ws_msg . baudrate , ws_msg . msg ) ; // action is "open", "close" or "msg"
330
306
// send an updated port list
331
307
} 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 } ) ;
333
313
// Handle unknown messages
334
314
} else if ( ws_msg . type === "hello-browser" ) {
335
315
helloClient ( socket , ws_msg . baudrate || 115200 ) ;
@@ -347,14 +327,18 @@ function connect_ws(ws_port, url_path) {
347
327
} ) ;
348
328
349
329
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 .
351
331
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 ( ) ;
358
342
}
359
343
} ) ;
360
344
@@ -405,49 +389,57 @@ function connect_ws(ws_port, url_path) {
405
389
}
406
390
407
391
function enableWX ( ) {
408
- wx_scanner_interval = setInterval ( function ( ) {
409
- discoverWirelessPorts ( ) ;
410
- ageWirelessPorts ( ) ;
411
- displayWirelessPorts ( ) ;
412
- } , 3500 ) ;
392
+ scanWXPorts ( ) ;
393
+ wScannerInterval = setInterval ( scanWXPorts , 3500 ) ;
413
394
}
414
395
415
396
function disableWX ( ) {
416
- if ( wx_scanner_interval ) {
417
- clearInterval ( wx_scanner_interval ) ;
397
+ if ( wScannerInterval ) {
398
+ clearInterval ( wScannerInterval ) ;
418
399
$ ( 'wx-list' ) . innerHTML = '' ;
419
400
}
420
401
}
421
402
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)
447
416
}
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 ) ;
448
442
}
449
- }
450
- ) ;
451
443
}
452
444
453
445
@@ -462,39 +454,34 @@ function serialTerminal(sock, action, portPath, baudrate, msg) {
462
454
// Find port from portPath
463
455
let port = findPort ( byPath , portPath ) ;
464
456
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 ) ;
486
460
} 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 ) ;
494
481
}
495
482
}
496
483
} 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.'} ;
498
485
sock . send ( JSON . stringify ( msg_to_send ) ) ;
499
486
}
500
487
}
0 commit comments