-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·273 lines (247 loc) · 9.06 KB
/
index.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
#!/usr/bin/env node
var serialport = require('serialport');
var Q = require('q');
var events = require("events");
var modem = require("modem");
var fs = require("fs");
function dongle() {
this.info = {};
this.info_commands = ['imsi', 'imei', 'signalstrength', 'cellid', 'service', 'subscriberid'];
return this;
};
/* clone prototype from event emitter */
dongle.prototype = Object.create(events.EventEmitter.prototype);
dongle.prototype.scanModem = function (callback) {
var self = this;
// list serial ports:
serialport.list(function (err, ports) {
if (err) {
return callback(false);
}
self.checkModem(ports, callback);
});
};
dongle.prototype.checkModem = function (ports, callback) {
var self = this;
var port = ports.shift();
if (!port) {
return callback(false);
}
self.init(port.comName, function (err, modem) {
if (err) {
self.checkModem(ports, callback);
} else {
callback(modem);
}
});
}
dongle.prototype.send = function (command, callback, iteration) {
var iteration = (!iteration) ? 0 : iteration;
if (iteration >= 1) return callback(new Error("SIM does not reply"));
var self = this;
return self.modem.execute(command, function (data, status) {
status = (typeof status === "string") ? status.trim() : false;
data = (typeof data === "string") ? data.trim() : "";
if (status === "+CME ERROR: SIM busy") {
self.emit("sim-busy");
return setTimeout(function () {
self.send(command, callback, iteration++);
}, 500);
};
self.emit("command", command, data, status);
callback(null, status, data);
}, false, 500);
};
dongle.prototype.init = function (device, callback) {
var self = this;
self.device = device;
self.modem = new modem.Modem();
self.open = false;
self.modem.open(device, function () {
self.open = true;
self.send("AT", function (err, status, data) {
if (err) return callback(err);
if (status !== "OK") return callback(new Error("AT returned " + status));
return self.prepare(function () {
callback(null, device);
});
}).on('timeout', function () {
callback(new Error("AT did not return!"));
});
self.modem.on("error", function (err) {
return self.emit("error", err);
});
self.modem.on('sms received', function (smsinfo) {
self.emit('sms-received', smsinfo);
});
self.modem.on("close", function (err) {
if (self.open) {
self.open = false;
self.emit("close");
try {
self.modem.close();
} catch (e) {
return self.emit("error", e);
}
}
});
});
};
// Get all information and compile it
dongle.prototype.check = function () {
var self = this;
var deferreds = self.info_commands.map(function (command) {
var q = Q.defer();
self[command](function (err, res) {
if (!err) {
self.info[command] = res;
}
q.resolve(res);
});
return q.promise;
});
Q.all(deferreds).then(function (res) {
self.emit('data', self.info);
});
};
dongle.prototype.prepare = function (callback) {
var self = this;
self.send("AT+CTZU=1", function (err, status, data) {
if (err) return callback(err);
if (status !== "OK") return callback(new Error("AT+CTZU=1 failed: " + status + " " + data));
// enable cell id
self.send("AT+CREG=2", function (err, status, data) {
if (err) return callback(err);
if (status !== "OK") return callback(new Error("AT+CREG=2 failed: " + status + " " + data));
// enable numeric operator format
self.send("AT+COPS=3,2", function (err, status, data) {
if (err) return callback(err);
if (status !== "OK") return callback(new Error("AT+COPS=3,2 failed: " + status + " " + data));
callback(null);
});
});
});
}
dongle.prototype.imsi = function (callback) {
var self = this;
self.send("AT+CIMI", function (err, status, data) {
if (err) return callback(err);
if (status !== "OK") return callback(new Error("Failed AT+CIMI"));
var result = (data.match(/^([0-9]{6,15})$/));
if (!result) return callback(new Error("Failed AT+CIMI"));
callback(null, parseInt(result[1], 10));
});
};
dongle.prototype.imei = function (callback) {
var self = this;
self.send("AT+CGSN", function (err, status, data) {
if (err) return callback(err);
if (status !== "OK") return callback(new Error("Failed AT+CGSN"));
var result = (data.match(/^([0-9]{14,15})/));
if (!result) return callback(new Error("Failed AT+CGSN"));
callback(null, parseInt(result[1], 10));
});
};
dongle.prototype.networktime = function (callback) {
var self = this;
self.send("AT+CCLK?", function (err, status, data) {
if (err) return callback(err);
if (status !== "OK") return callback(new Error("Failed AT+CCLK"));
var result = (data.match(/^\+CCLK: \"(.+)\"/));
if (!result) return callback(new Error("Failed AT+CCLK"));
callback(null, result[1]);
});
};
dongle.prototype.signalstrength = function (callback) {
var self = this;
self.send("AT+CSQ", function (err, status, data) {
if (err) return callback(err);
if (status !== "OK") return callback(new Error("Failed AT+CSQ"));
var signal = (data.match(/^\+CSQ: ([0-9]{1,2}),99$/));
if (!signal) return callback(new Error("Failed AT+CSQ"));
if (signal[1] === "99") return callback(null, -Infinity);
callback(null, (-113 + (parseInt(signal[1], 10) * 2)));
});
};
dongle.prototype.service = function (callback) {
var self = this;
self.send("AT+COPS?", function (err, status, data) {
if (err) return callback(err);
if (status !== "OK") return callback(new Error("Failed Request AT+CREG?"));
var result = data.match(/^\+COPS: ([0-4])(,([0-2]),"([^"]+)"(,([0-7]))?)?$/);
if (!result) return callback(new Error("Parse Error AT+COPS?"));
callback(null, {
operator: (typeof result[4] === "string") ? result[4] : null,
mode: (typeof result[6] === "string") ? parseInt(result[6], 10) : null
});
});
};
dongle.prototype.cellid = function (callback) {
var self = this;
self.send("AT+CREG?", function (err, status, data) {
if (err) return callback(err);
if (status !== "OK") return callback(new Error("Failed Request AT+CREG?"));
var cellid = data.match(/^\+CREG: ([0-2]),([0-5])(,\s?["]?([0-9A-F]+)["]?,\s?["]?([0-9A-F]+)["]?(, ?([0-7]))?)?$/);
if (!cellid) return callback(new Error("Parse Error AT+CREG?"));
callback(null, {
stat: parseInt(cellid[2], 10),
lac: (typeof cellid[4] === "string") ? parseInt(cellid[4].toLowerCase(), 16) : null,
cell: (typeof cellid[5] === "string") ? parseInt(cellid[5].toLowerCase(), 16) : null,
act: (typeof cellid[7] === "string") ? parseInt(cellid[7], 10) : null
});
});
};
dongle.prototype.subscriberid = function (callback) {
var self = this;
var ussd = new modem.Ussd_Session();
ussd.modem = self.modem;
ussd.callback = callback;
ussd.parseResponse = function (response_code, message) {
var match = message.match(/(\d{10})/);
if (!match) {
if (this.callback)
this.callback(new Error("Parse Error AT+CUSD"));
return;
}
if (this.callback)
this.callback(null, match[1]);
}
ussd.execute = function () {
self.modem.ussd_pdu = false;
this.query('*282#', ussd.parseResponse);
}
ussd.start();
self.modem.ussd_pdu = true;
};
/*
* Send an SMS using the dongle.
* @param message Object
* text String message body. Longs messages will be splitted and sent in multiple parts transparently.
* receiver String receiver number.
* encoding String. '16bit' or '7bit'. Use 7bit in case of English messages.
* @param callback Fucntion(err, references) is called when sending is done.
*
* @return references Array contains reference ids for each part of sent message.
*/
dongle.prototype.sendsms = function (message) {
var self = this;
message.encoding = (message.encoding !== '7bit') ? '16bit' : '7bit';
this.modem.sms(message, function () {});
};
module.exports = dongle;
if (require.main === module) {
var d = new dongle();
var command = process.argv[2];
d.scanModem(function (modem) {
if (!modem) process.exit(-1);
if(command && d[command]) {
d[command](function(err, r) {
console.log(JSON.stringify(r));
process.exit(0);
});
} else {
console.log('No command found!');
process.exit(-1);
}
});
}