forked from tschiemer/at-commander
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
810 lines (673 loc) · 22 KB
/
Copy pathindex.js
File metadata and controls
810 lines (673 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
"use strict";
var SerialPort = require('serialport');
exports.Modem = Modem;
exports.Command = Command;
exports.Notification = Notification;
const CommandStateInit = 'init';
const CommandStateRejected = 'rejected';
const CommandStateRunning = 'running';
const CommandStateFinished = 'finished';
const CommandStateFailed = 'failed';
const CommandStateTimeout = 'timeout';
const CommandStateAborted = 'aborted';
exports.CommandStates = {
Init : CommandStateInit,
Rejected : CommandStateRejected,
Running : CommandStateRunning,
Finished : CommandStateFinished,
Failed : CommandStateFailed,
Timeout : CommandStateTimeout,
Aborted : CommandStateAborted
};
var defaultConfig = {
parser: SerialPort.parsers.raw,
baudRate: 115200,
dataBits: 8,
stopBits: 1,
lineRegex: /^(.+)\r\n/,
EOL: "\r\n",
timeout: 500,
defaultExpectdResult: "OK"
};
var NextId = 1;
function getNextId(){
return NextId++;
}
function Modem(config)
{
this.serial = false;
this.inbuf = new Buffer(0);
this.events = {};
this.setConfig(config);
this.bufferTimeout = 0;
this.processCommands = false;
this.currentCommand = false;
this.pendingCommands = [];
this.notifications = {};
}
function Command(buf, expectedResult, opts)
{
if (typeof expectedResult === 'undefined') {
throw new Error("An expected result is required, none given");
}
this.id = getNextId();
this.state = CommandStateInit;
this.result = false;
this.buf = buf;
this.expectedResult = expectedResult;
opts = opts || {};
if (typeof opts.resultProcessor === 'function') {
this.resultProcessor = opts.resultProcessor;
} else if (typeof opts.resultProcessor === 'undefined' || opts.resultProcessor === true) {
if (typeof this.expectedResult === 'string') {
this.resultProcessor = function(buf, result) {
var r;
if (result instanceof Array){
r = result[1] == this.expectedResult;
} else {
r = result == this.expectedResult;
}
if (r){
return true;
} else {
return; // return undefined
}
};
} else if (this.expectedResult instanceof RegExp) {
this.resultProcessor = function(buf, matches) {
return matches;
}
} else if (typeof this.expectedResult === 'number') {
this.resultProcessor = function(buf, matches) {
return buf;
}
}
}
this.timeout = opts.timeout;
}
function Notification(name, regex, handler)
{
this.name = name;
this.regex = regex;
this.handler = handler;
}
Notification.prototype._generateId = function()
{
this.id = getNextId();
};
Modem.prototype.getConfig = function()
{
return this.config;
};
Modem.prototype.setConfig = function(newConfig){
if (typeof newConfig === 'string'){
newConfig = JSON.parse(config);
}
if (typeof newConfig !== 'object'){
newConfig = {};
}
if (typeof this.config === 'undefined'){
this.config = defaultConfig;
}
this.config = Object.assign(this.config, newConfig || {});
};
Modem.prototype.open = function(path)
{
if (this.serial instanceof SerialPort && this.serial.isOpen){
this.serial.close();
}
this.serial = new SerialPort(path, {
parser: this.config.parser,
baudRate: this.config.baudRate,
dataBits: this.config.dataBits,
stopBits: this.config.stopBits,
autoOpen: false
});
this._registerSerialEvents();
var modem = this;
return new Promise(function(resolve, reject){
modem.serial.open(function(error){
if (error) reject(error);
else {
// write a newline to the serial because it is unknown what was last written to the serial before opening
modem.serial.write(modem.config.EOL);
resolve(modem.serial.isOpen);
}
});
});
};
Modem.prototype._registerSerialEvents = function(){
var modem = this;
this.serial.on('open', function(error){
if (typeof modem.events.open === 'function'){
modem.events.open(error);
}
});
this.serial.on('data', function(data){
modem._onData(data);
if (typeof modem.events.data === 'function'){
modem.events.data(data);
}
});
this.serial.on('disconnect', function(error){
// console.log('disconnect');
if (typeof modem.events.disconnect === 'function'){
modem.events.disconnect(error);
}
});
this.serial.on('close', function(error){
// console.log('close');
if (typeof modem.events.close === 'function'){
modem.events.close(error);
}
});
this.serial.on('error', function(error){
// console.log('error', error);
if (typeof modem.events.error === 'function'){
modem.events.error(error);
}
});
};
Modem.prototype.isOpen = function()
{
if (!(this.serial instanceof SerialPort)){
return false;
}
return this.serial.isOpen;
};
Modem.prototype.pauseSerial = function()
{
if (!(this.serial instanceof SerialPort)){
return false;
}
this.serial.pause();
return this;
};
Modem.prototype.resumeSerial = function()
{
if (!(this.serial instanceof SerialPort)){
return false;
}
this.serial.resume();
return this;
};
Modem.prototype.close = function(cb)
{
return this._close(cb, true);
};
Modem.prototype.closeGracefully = function(cb)
{
return this._close(cb, false);
};
Modem.prototype._close = function(cb, gracefully)
{
if (typeof cb !== 'function') {
if (typeof this.events.close === 'function') {
cb = this.events.close;
} else {
cb = function(){};
}
}
if (this.serial instanceof SerialPort){
if (gracefully && this.processCommands && (this.currentCommand instanceof Command || this.pendingCommands.length)){
this.addCommand("AT").done(() => {
this.serial.close(cb);
});
} else {
this.stopProcessing(true);
this.serial.close(cb);
}
} else {
cb();
}
return this;
}
Modem.prototype.on = function(event, callback)
{
this.events[event] = callback;
return this;
};
Modem.prototype.getInBuffer = function()
{
return this.inbuf;
};
Modem.prototype.clearInBuffer = function()
{
this.inbuf = new Buffer(0);
if (this.bufferTimeout) {
clearTimeout(this.bufferTimeout);
this.bufferTimeout = 0;
}
return this;
};
Modem.prototype.getPendingCommands = function()
{
return this.pendingCommands;
};
Modem.prototype.clearPendingCommands = function()
{
this.pendingCommands = [];
return this;
};
Modem.prototype.isProcessingCommands = function()
{
return this.processCommands;
};
Modem.prototype.startProcessing = function()
{
this.processCommands = true;
this._checkPendingCommands();
return this;
};
Modem.prototype.stopProcessing = function(abortCurrent, stopCallback)
{
this.processCommands = false;
if (this.currentCommand instanceof Command && abortCurrent){
this.abortCurrentCommand();
}
if (typeof stopCallback === 'function'){
// if current command not yet finished, wait until it is done.
if (this.currentCommand instanceof Command) {
var modem = null;
var i = setInterval(function () {
if (modem.currentCommand instanceof Command){
return;
}
clearInterval(i);
stopCallback();
}, 100);
} else {
stopCallback();
}
}
return this;
};
Modem.prototype.getCurrentCommand = function()
{
return this.currentCommand;
};
Modem.prototype.abortCurrentCommand = function()
{
this.currentCommand = false;
this._clearBufferTimeout();
this._checkPendingCommands();
return this;
};
Modem.prototype.getNotifications = function()
{
return this.notifications;
};
Modem.prototype.addNotification = function(notification, regex, handler)
{
if (notification instanceof Notification){
this.notifications[notification.name] = notification;
} else {
this.notifications[notification] = new Notification(notification, regex, handler);
}
return this;
};
Modem.prototype.removeNotification = function(name)
{
delete this.notifications[name];
return this;
};
Modem.prototype.clearNotifications = function()
{
this.notifications = {};
return this;
};
/**
* Run command bypassing command list (processing option)
* @param command
*/
Modem.prototype.run = function(command, expected, opts)
{
if (!(command instanceof Command)){
command = this._newCommand(command, expected, opts);
}
if (this.currentCommand instanceof Command || this.inbuf.length > 0){
command.state = CommandStateRejected;
} else {
this._run(command);
}
return _promiseForCommand(command);
};
/**
* Add command to processing list
* @param command
*/
Modem.prototype.addCommand = function(command, expected, opts)
{
if (!(command instanceof Command)){
command = this._newCommand(command, expected, opts);
}
this.pendingCommands.push(command);
this._checkPendingCommands();
return _promiseForCommand(command);
};
function _promiseForCommand(command)
{
return new Promise(function(resolve, reject){
command._interval = setInterval(function(){
if (command.state == CommandStateInit || command.state == CommandStateRunning){
//just wait until not running anymore
return;
}
clearInterval(command._interval);
// console.log(command);
if (command.state == CommandStateFinished){
if (typeof command.result.processed !== 'undefined') {
resolve(command.result.processed);
} else {
resolve(command.result);
}
} else {
reject(command);
}
}, 100);
});
}
Modem.prototype._newCommand = function(command, expected, opts)
{
if (typeof expected === 'undefined'){
return new Command(command, this.config.defaultExpectdResult, opts);
} else {
return new Command(command, expected, opts);
}
}
/**
* Read n bytes without writing any command
* @param n
* @param cb
* @returns {*}
*/
Modem.prototype.read = function(n)
{
return this.run(new Command(false, n));
};
/**
* Write str/buffer to serial without awaiting any result
* @param str
* @param cb
* @returns {*}
*/
Modem.prototype.write = function(buf)
{
return this.run(new Command(buf, 0));
}
Modem.prototype._checkPendingCommands = function()
{
// let current command finish
if (this.currentCommand instanceof Command){
return;
}
// require there not to be anything left in the buffer, before starting another command
if (this.inbuf.length > 0){
this._setBufferTimeout();
return;
}
// if not processing just do nothing
if (!this.processCommands){
return;
}
// if no pending commands, we're done
if (this.pendingCommands.length == 0){
return;
}
var command = this.pendingCommands[0];
this.pendingCommands = this.pendingCommands.slice(1);
this._run(command);
};
Modem.prototype._run = function(command)
{
this.currentCommand = command;
command.state = CommandStateRunning;
if (typeof command.buf === 'string'){
// console.log("Serial.write",new Buffer(command.buf), command.buf);
this.serial.write(command.buf + this.config.EOL);
} else if (command.buf instanceof Buffer){
// console.log("Serial.write", command.buf);
this.serial.write(command.buf);
}
this._setBufferTimeout();
};
Modem.prototype._onData = function(data)
{
// update buffer
this.inbuf = Buffer.concat([this.inbuf, data]);
// remove newline prefixes
if (this._trimNewlinePrefix()){
// do not attempt to process if buffer is empty anyways
if (this.inbuf.length == 0){
return;
}
}
//console.log("INBUF", this.inbuf, this.inbuf.toString());
// if a command was previously sent, we are expecting a result
if (this.currentCommand instanceof Command){
var finishCommand = false;
var consumeBufBytes = 0;
var matches = null;
if (typeof this.currentCommand.expectedResult === 'string') {
/*
in mythfish's fork he changes the matchexpression to this.config.currentCommand.expectedResult
Actually this here is the wanted behaviour where we assume that the asnwer is given on a line, thus
the response is matched against lineRegex and not the given (string) expected result
The final check wether the command actually succeeded is made in the result processor for commands with a
string type expected result
*/
var str = this.inbuf.toString();
matches = str.match(this.config.lineRegex);
if (matches) {
consumeBufBytes = matches[0].length;
finishCommand = true;
}
} else if (this.currentCommand.expectedResult instanceof RegExp){
var str = this.inbuf.toString();
matches = str.match(this.currentCommand.expectedResult);
// console.log("matches?",str, matches, this.currentCommand.expectedResult.source);
if (matches){
finishCommand = true;
consumeBufBytes = matches[0].length;
// always assume
// matches = matches[1];
}
} else if (typeof this.currentCommand.expectedResult === 'number') {
// console.log('is type number');
if (this.currentCommand.expectedResult <= this.inbuf.length) {
finishCommand = true;
consumeBufBytes = this.currentCommand.expectedResult;
}
} else if (typeof this.currentCommand.expectedResult === 'function'){
consumeBufBytes = this.currentCommand.expectedResult(this.inbuf);
if (0 < consumeBufBytes){
finishCommand = true;
}
} else {
throw new Error('Invalid expectedResult for command');
}
var consumedBuf;
if (0 < consumeBufBytes) {
consumedBuf = this.inbuf.slice(0, consumeBufBytes);
this.inbuf = this.inbuf.slice(consumeBufBytes);
// console.log("consumed ",consumeBufBytes,"remaining",this.inbuf);
}
if (finishCommand){
// get copy of relevant buffer contents
// pass relevant in buffer to result handler
this._serveCommand(this.currentCommand, CommandStateFinished, consumedBuf, matches);
// matchings for commands might be incorrectly finished such that a newline is forgotten to be added
// because notifications do not expect there to be an initial newline, trim it away
this._trimNewlinePrefix();
}
}
// Ideally, if no command was sent we're likely dealing with an unsolicited notification if data is incoming.
// But due to timing/buffering effects there might be an overlap of a command being registered as running while
// an unsolicited message is incoming.
// That means it becomes a bit harder to differentiate between command responses and unsolicited messages - but quite
// likely this will go unnoticed, as typically command responses and unsolicited messages (even though they might
// relate to the same info) have slightly different formatting. That also means, that you as a user are responsible
// for making sure you specify precise enough match phrases.
while(this._checkForNotifications()){
// continue trying to consume notifications until there is none detected
}
this._setBufferTimeout();
};
Modem.prototype._trimNewlinePrefix = function()
{
var m = this.inbuf.toString().match(/^((\r|\n)+)/);
if (m) {
this.inbuf = this.inbuf.slice(m[0].length);
return true;
}
return false;
}
Modem.prototype._checkForNotifications = function()
{
var detected = false;
var str = this.inbuf.toString();
var line = str.match(this.config.lineRegex);
if (line){
// cons¿ole.log("matched a line");
for (var i in this.notifications){
var matches = str.match(this.notifications[i].regex);
// console.log("testing ",str," against ", this.notifications[i].regex);
if (matches !== null){
// copy matching buffer
var buf = this.inbuf.slice(0, matches[0].length);
// update inbuf consuming matching buffer
this.inbuf = this.inbuf.slice(matches[0].length);
// console.log("STRIPPING ",buf, buf.toString());
// console.log("REMAINING ",this.inbuf, this.inbuf.toString());
this._serveNotification(this.notifications[i], buf, matches);
// just for safety, trim any remaining newlines
this._trimNewlinePrefix();
detected = true;
break;
}
}
// this._serveNotification(false, new Buffer(), line);
// feed notification to generic notification handler
// if (typeof this.events.notification === 'function'){
// this.events.notification(buf);
// }
}
return detected;
};
Modem.prototype._discardLine = function()
{
var str = this.inbuf.toString();
var line = str.match(this.config.lineRegex);
if (line){
var buf = this.inbuf.slice(0,line[0].length);
this.inbuf = this.inbuf.slice(line[0].length);
if (typeof this.events.discarding === 'function'){
this.events.discarding(buf);
}
this._trimNewlinePrefix();
return true;
}
return false;
};
Modem.prototype._setBufferTimeout = function()
{
this._clearBufferTimeout();
// do not set timeout, if neither serving a command, nor any data in buffer
if (!(this.currentCommand instanceof Command) && this.inbuf.length == 0){
return;
}
var timeout = this.config.timeout;
if (this.currentCommand instanceof Command && typeof this.currentCommand.timeout === 'number'){
timeout = this.currentCommand.timeout;
}
// no timeout if value is zero
if (timeout == 0){
return;
}
var modem = this;
this.bufferTimeout = setTimeout(function(){
// console.log("timeout", modem.inbuf);
if (modem.currentCommand instanceof Command){
var command = modem.currentCommand;
command.result = {
buf: modem.inbuf
};
modem.inbuf = new Buffer(0);
modem.currentCommand = false;
command.state = CommandStateTimeout;
modem._checkPendingCommands();
} else {
// if there happen to be unexpected notifications they might block following notifications which we do
// not necessarily want to discard, so just discard one line at most
if (modem._discardLine()) {
if (modem.inbuf.length) {
while (modem._checkForNotifications()) {
// whilst there are notifications..
}
}
} else { // if no line detected, discard whole buffer
var buf = modem.inbuf;
modem.inbuf = new Buffer(0);
if (typeof modem.events.discarding === 'function'){
modem.events.discarding(buf);
}
}
if (modem.inbuf.length){
modem._checkPendingCommands();
}
}
}, timeout);
};
Modem.prototype._clearBufferTimeout = function()
{
if (this.bufferTimeout){
clearTimeout(this.bufferTimeout);
this.bufferTimeout = 0;
}
};
Modem.prototype._serveCommand = function(command, state, buf, matches)
{
// clear current command
this.currentCommand = false;
// set command result
// command._setResult(buf, matches);
command.result = {
buf: buf,
matches: matches
};
if (typeof command.resultProcessor === 'function'){
command.result.processed = command.resultProcessor(buf, matches);
// If a result processor is given, but it returns an undefined result, consider the command to have failed
// thereby any failing commands will be rejected (catched respectively), which is meaningful behaviour
// note: mostly this will be interesting for simple (string based) commands
if (typeof command.result.processed === 'undefined'){
state = CommandStateFailed;
}
}
// by setting the state to a final state, the promise will finish by itself
command.state = state;
// feed command to generic command result handler
if (typeof this.events.command === 'function'){
this.events.command(command, command.result.processed);
}
// console.log("buffer now ", this.inbuf);
this._checkPendingCommands();
};
Modem.prototype._serveNotification = function(notification, buf, matches)
{
if (notification instanceof Notification) {
// feed matches to notification handler (if set)
if (typeof notification.handler === 'function') {
notification.handler(buf, matches);
}
// feed notification to specific event handler
if (typeof this.events[notification.name] === 'function') {
this.events[notification.name](buf, matches);
}
} else {
// feed notification to generic notification handler
if (typeof this.events.notification === 'function') {
this.events.notification(matches);
}
}
};