-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiridium.js
535 lines (432 loc) · 14.3 KB
/
iridium.js
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
// Small Iridium SBD (short burst data) Node.JS Library
// Version 1.5 (2012-12-28)
// A continuous work in progress, check for the latest version at http://www.veri.fi/iridiumsbd.tar.gz (will move to GitHub at a certain point)
// See the sbd.js example to see how to use this. Feel free to send questions and feedback to the email below
// (C) 2012 Razvan Dragomirescu <[email protected]>
var sys = require('sys');
var clc = require('cli-color');
var async = require('async');
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var EventEmitter = require('events').EventEmitter;
var iridiumEvents = new EventEmitter();
exports.on = function(ev, callback) {
iridiumEvents.on(ev, callback);
}
var buffer = "";
var df;
var er;
var tf;
var bars=0;
var messagePending=0;
var debug = 0;
var port = "/dev/ttyUSB0";
var OK = /^OK\r/;
var ALL = /.*/;
var DEFAULT_TIMEOUT = 60000; // 60 seconds general timeout for all commands
var SIMPLE_COMMAND_TIMEOUT = 2000; // 2 seconds timeout for simple command such as "echo off" (ATE0)
var TIMEOUT_FOREVER = -1;
// this array contains all possible unsollicited response codes and their
// corresponding handling functions
var unsollicited = {
"SBDRING": {
pattern: /^SBDRING/,
execute: sbdring
},
"AREG": {
pattern: /^\+AREG/,
execute: areg
}
};
var errors = [
/ERROR/
];
// emit a 'ringalert' event if the SBDRING unsollicited response is received
function sbdring() {
iridiumEvents.emit('ringalert');
}
// log if debug enabled
function log(message) {
if (debug) sys.log(message);
}
// interpret the automatic registration result
function areg(line) {
var m = line.match(/^\+AREG:(\d+),(\d+)/);
var regevent = m[1];
var regerr = m[2];
log("Registration result: "+regevent+" with error "+regerr);
}
// send a binary message via SBD and call back when done
function sendBinaryMessage(message, callback, maxWait) {
if (message.length==0) {
sendMessage(message, callback, maxWait);
return;
}
var buffer = (message instanceof Buffer)?message:new Buffer(message);
var command = "AT+SBDWB="+buffer.length;
var ob = new Buffer(buffer.length+2);
var sum = 0;
for (var i=0;i<buffer.length;i++) {
ob[i]=buffer[i];
sum+=buffer[i];
}
ob[buffer.length+1]=sum&0xff;
sum>>=8;
ob[buffer.length]=sum&0xff;
// first write the binary message to storage - issue AT+SBDWB and wait for the modem to say READY
AT(command, /READY/, ALL, function(err, text) {
if (err) {
messagePending = 0;
clearMOBuffers(function() {
callback(err);
});
return;
}
// send the binary message and wait for OK
ATS(ob, OK, ALL, function(berr) {
if (berr) {
messagePending = 0;
clearMOBuffers(function() {
callback(berr);
});
return;
}
messagePending = 1;
waitForNetwork(function(xerr) {
if (xerr) {
messagePending = 0;
clearMOBuffers(function() {
callback(xerr);
});
return;
}
messagePending=2;
disableSignalMonitoring(function(xcallback) {
initiateSession(callback);
});
}, maxWait);
});
});
}
// export this function so that it can be called from any code requiring this
exports.sendBinaryMessage = sendBinaryMessage;
// send a message via SBD and call back when done
function sendMessage(message, callback, maxWait) {
// if no message is given, this is a mailbox check, so clear the MO storage
var command = message?"AT+SBDWT="+message:"AT+SBDD0";
// write the MO message, wait for network (+CIEV event)
// disable signal monitoring (+CIER=0) then send the message (+SBDIXA)
AT(command, OK, ALL, function(err, text) {
if (err) {
messagePending = 0;
clearMOBuffers(function() {
callback(err);
});
return;
}
messagePending = 1;
waitForNetwork(function(xerr) {
if (xerr) {
messagePending = 0;
clearMOBuffers(function() {
callback(xerr);
});
return;
}
messagePending=2;
disableSignalMonitoring(function(xcallback) {
initiateSession(callback);
});
}, maxWait);
});
}
// export this function so that it can be called from any code requiring this
exports.sendMessage = sendMessage;
var serialPort;
var data = "";
var binaryMode = false;
var binaryBuffer = new Buffer(512);
var binaryBufferCounter = 0;
var serialEmitter;
// in binary mode we do not stop at OK or any other regexp, it's all time-based (it reads all available data for bufferTimeout seconds)
function enableBinaryMode(bufferTimeout) {
binaryMode = true;
setTimeout(function() {
var ob = new Buffer(binaryBufferCounter);
binaryBuffer.copy(ob, 0, 0, ob.length);
serialEmitter.emit('data', ob);
binaryBufferCounter = 0;
binaryMode = false;
}, bufferTimeout);
}
// read line by line or a whole binary blob, depending on the mode
function readSBD(emitter, buffer) {
serialEmitter = emitter;
if (binaryMode) {
buffer.copy(binaryBuffer, binaryBufferCounter);
binaryBufferCounter+=buffer.length;
}
else {
// Collect data
data += buffer.toString('binary');
// Split collected data by delimiter
var parts = data.split("\n")
data = parts.pop();
parts.forEach(function (part, i, array) {
emitter.emit('data', part);
});
}
};
// open the serial port
// config options are: "debug" (set to 1 to monitor the AT commands and response
// and "port" (the actual device to use - defaults to /dev/ttyUSB0)
function open(config) {
if (config) {
if (config.debug) debug=config.debug;
if (config.port) port=config.port;
}
serialPort = new SerialPort(port, {
baudrate: 19200,
buffersize: 512,
parser: readSBD
});
serialPort.on("data", function (data) {
log(clc.red("<<< "+data));
if (!er) {
df(null, data);
delete(df);
delete(er);
return;
}
for (x in unsollicited) {
if (unsollicited[x].pattern.test(data)) {
unsollicited[x].execute(data);
return;
}
}
for (x in errors) {
if (errors[x].test(data)) {
df(errors[x], buffer);
buffer="";
delete(df);
delete(er);
return;
}
}
if (!kr || kr.test(data)) {
buffer+=(data+"\n");
}
if (er && er.test(data)) {
df(null, buffer);
buffer="";
delete(df);
delete(er);
}
});
serialPort.on("error", function (error) {
if (log) sys.log("ERROR: "+error);
});
serialPort.on("open", function() {
init();
});
}
// export the "open" function so that it can be called externally
exports.open = open;
function batchProcess(tasks) {
async.series(tasks, function(err, results) {
//sys.log("Batch process complete");
});
}
function initComplete(callback) {
iridiumEvents.emit('initialized');
callback(null);
}
// this is the modem initialization process - echo off, clear all buffers (MO & MT)
// query registration status (should return 2 = registered)
// enable ring alert (AT+SBDMTA=1)
function init() {
batchProcess([
echoOff,
clearBuffers,
enableRegistration,
ringAlertEnable,
initComplete
]
);
}
// expose the init() function to the outside world
exports.init = init;
function waitForNetwork(callback, maxWait) {
ATS("AT+CIER=1,1,0,0", /\+CIEV:0,[^0]/, ALL, callback, maxWait?maxWait:TIMEOUT_FOREVER);
}
function getSystemTime(callback) {
AT("AT+CCLK?", OK, ALL, function(err, result) {
if (err) callback(err);
else {
var m = result.match(/CCLK:(\d+)\/(\d+)\/(\d+),(\d+):(\d+):(\d+)/);
if (!m) callback("UNKNOWN_TIME");
else {
var ctime = new Date(Date.UTC(2000+Number(m[1]), m[2]-1, m[3], m[4], m[5], m[6]));
callback(null, ctime);
}
}
});
}
exports.getSystemTime = getSystemTime;
function disableSignalMonitoring(callback) {
ATS("AT+CIER=0,0,0,0", OK, ALL, callback, SIMPLE_COMMAND_TIMEOUT);
}
function ringAlertEnable(callback) {
ATS("AT+SBDMTA=1", OK, ALL, callback, SIMPLE_COMMAND_TIMEOUT);
}
function echoOff(callback) {
ATS("ATE0", OK, ALL, callback, SIMPLE_COMMAND_TIMEOUT);
}
function enableRegistration(callback) {
ATS("AT+SBDAREG=1", OK, ALL, callback, SIMPLE_COMMAND_TIMEOUT);
}
function clearMOBuffers(callback) {
ATS("AT+SBDD0", OK, ALL, callback, SIMPLE_COMMAND_TIMEOUT);
}
function clearMTBuffers(callback) {
ATS("AT+SBDD1", OK, ALL, callback, SIMPLE_COMMAND_TIMEOUT);
}
function clearBuffers(callback) {
ATS("AT+SBDD2", OK, ALL, callback, SIMPLE_COMMAND_TIMEOUT);
}
// emit a 'newmessage' event containing the message
// and the number of queued messages still waiting at the server
function readBinaryMessage(mtqueued, callback) {
enableBinaryMode(1000);
AT("AT+SBDRB", false, false, function(err, buffer) {
if (err) {
clearMTBuffers(function() {
callback(err);
});
return;
}
var ib = buffer;
var messageLength = ib.readUInt16BE(0);
var messageBuffer = new Buffer(messageLength);
ib.copy(messageBuffer, 0, 2, messageLength+2);
log("Received message is "+messageBuffer.toString('hex'));
binaryMode = false;
iridiumEvents.emit('newmessage', messageBuffer, mtqueued);
clearMTBuffers(callback);
}, SIMPLE_COMMAND_TIMEOUT);
}
// emit a 'newmessage' event containing the message
// and the number of queued messages still waiting at the server
function readMessage(mtqueued, callback) {
AT("AT+SBDRT", OK, ALL, function(err, text) {
if (err) {
clearMTBuffers(function() {
callback(err);
});
return;
}
var m = text.match(/SBDRT:[^]{2}(.*)/);
var rmessage = m[1];
log("Received message is "+rmessage);
iridiumEvents.emit('newmessage', rmessage, mtqueued);
clearMTBuffers(callback);
}, SIMPLE_COMMAND_TIMEOUT);
}
// most important function, initiates a SBD session and sends/receives messages
function initiateSession(callback) {
AT("AT+SBDIXA", OK, /\+SBDIX/, function(err, text) {
if (err) {
messagePending = 1;
clearMOBuffers(function() {
callback(err);
});
return;
}
var m = text.match(/\+SBDIX: (\d+), (\d+), (\d+), (\d+), (\d+), (\d+)/);
var status = m[1];
var momsn = m[2];
var mtstatus = m[3];
var mtmsn = m[4];
var mtlen = m[5];
var mtqueued = m[6];
if (status<=4) {
log("MO message transferred successfully");
messagePending = 0;
} else if (status==18) {
log("MO message failed, radio failure");
messagePending = 1;
clearMOBuffers(function() {
callback("radio failure");
});
return;
} else if (status==32) {
log("MO message failed, network failure");
messagePending = 1;
clearMOBuffers(function() {
callback("network failure");
});
return;
} else {
log("MO message failed, error "+status);
messagePending = 1;
clearMOBuffers(function() {
callback("unknown failure");
});
return;
}
if (mtqueued>0) {
log("There are still "+mtqueued+" messages waiting!");
}
if (mtstatus==0) {
log("No MT messages are pending");
} else if (mtstatus==1) {
log("A MT message has been transferred, use AT+SBDRT to read it");
readBinaryMessage(mtqueued, function() {
clearMOBuffers(function(err) {
callback(err, momsn);
});
}
);
return;
} else {
log("Error determining MT status: "+mtstatus);
}
clearMOBuffers(function(err) {
callback(err, momsn);
});
});
}
// simplified AT command function - when you don't care about the result
// the end callback is simply a null function (does nothing)
function ATS(command, endregexp, keepregexp, callback, timeout) {
AT(command, endregexp, keepregexp, function(err, text) {
return callback(err);
}, timeout);
}
// send an AT command to the modem and call datafunction when complete
// endregexp is the regular expression that marks the end of the response (usually the string OK)
// keepregexp tells it to filter the response and keep only the lines that match it
// datafunction is the function to call when the response is fully received
function AT(command, endregexp, keepregexp, datafunction, timeout) {
er = endregexp; // when to push the completed buffer to the datafunction
kr = keepregexp; // what lines to keep
if (tf) clearTimeout(tf); // any new AT command clears the previous command
delete tf;
df = function (err, text) {
if (tf) clearTimeout(tf);
delete(tf);
datafunction(err, text); // what to call when ended
};
if (!timeout) timeout=DEFAULT_TIMEOUT; // general timeout 60 seconds
if (timeout>0) tf = setTimeout(function() {
log("Sending a timeout event for command "+command);
datafunction("TIMEOUT");
}, timeout);
if (command instanceof Buffer) {
log(clc.blue("[BINARY] >>> "+command.toString('hex')+"\r"));
serialPort.write(command);
} else {
log(clc.green(">>> "+command+"\r"));
serialPort.write(command+"\r");
}
}