-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
242 lines (201 loc) · 7.2 KB
/
Copy pathtest.js
File metadata and controls
242 lines (201 loc) · 7.2 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
import { SerialPort } from "serialport";
import { TiptelParser } from "./TiptelSerial/TiptelSerial.js";
import { Command, State } from "./TiptelSerial/types.js";
import { s as string, n as number, b as bitmask, h as hexadecimal, d as digits } from "./TiptelSerial/TitelCommandParser.js";
import { parseInput, writeCommandList } from "./helpers/helpers.js";
const {stdin, stdout } = process;
const serialport = new SerialPort({ path: "/dev/ttyUSB0", baudRate: 9600, autoOpen: false, hupcl: true, dtr: true });
// Pipe serial port from and to TiptelParser
const tiptel = serialport.pipe(new TiptelParser());
tiptel.pipe(serialport);
stdout.write("Tests\n=====\n");
stdout.write("DataParser:\t");
console.assert(string(Buffer.from([0x42, 0x34, 0x32, 65]), 1, 3) === "42A", "string failed");
console.assert(hexadecimal(Buffer.from([0x42, 0x01, 0xff]), 1, 2) === "01ff", "hexadecimal failed");
console.assert(number(Buffer.from([0x42, 0x01, 0xff]), 1, 2) === 511, "number failed");
const flags = bitmask(Buffer.from([0x42, 0x0e, 0x71]), 1, 2);
const match = [false, false, false, false, true, true, true, false, false, true, true, true, false, false, false, true];
flags.forEach((f, n) => console.assert(f === match[n]), `bitmask failed`);
console.assert(digits(Buffer.from([0x42, 0x08, 0x9A]), 1, 2) === "890", "digits failed");
stdout.write("done\n");
// Checksum
stdout.write("checksum:\t");
console.assert(tiptel.checksum(Buffer.from([0x42, 0x66, 0x66])), "Receive checksum read failed");
console.assert(tiptel.checksum(Buffer.from([0x06, 0x42, 0x66, 0x66])), "Send checksum read failed");
let buffer = Buffer.from([0x42, 0x66, 0x00]);
console.assert(!tiptel.checksum(buffer), "Unexpected receive checksum");
// TODO: we might not want to alter receive checksum
console.assert(buffer[buffer.length - 1] === 0x66, "Receive checksum write failed");
buffer = Buffer.from([0x06, 0x42, 0x66, 0x00]);
console.assert(!tiptel.checksum(buffer), "Unexpected send checksum");
console.assert(buffer[buffer.length - 1] === 0x66, "Send checksum write failed");
stdout.write("done\n");
// Input
stdout.write("input parser:\t");
console.assert(JSON.stringify(parseInput("01\t42 00011000 A ff")) === JSON.stringify([0x01, 0x42, 0x18, 0x0A, 0xFF]), "Input parser failed");
console.assert(JSON.stringify(parseInput("G 42")) === JSON.stringify(false), "Unexpected output");
console.assert(JSON.stringify(parseInput("42 Q")) === JSON.stringify(false), "Unexpected output");
stdout.write("done\n");
stdout.write("invokePabx:\t");
// Verify data is array
let called = false;
tiptel.invokePabx = async (command, data, waitForState) => {
console.assert(command, "Command not found");
console.assert(Array.isArray(data), "Data not array, should not happen");
console.assert(waitForState === State.WAIT_ACK || waitForState === State.WAIT_ACK_READY, `Unexpected waitForState: ${waitForState}`);
// console.assert(waitForState === 1 || waitForState === 3, "Unexpected waitForState");
called = true;
return Promise.resolve(data);
};
called = false;
tiptel.readPabx(0x99);
tiptel.readPabx(0x99, []);
tiptel.readPabx(0x99, 0x55, 0x66);
const testData = [0x55, 0x66];
const resultData = await tiptel.readPabx(0x99, testData);
console.assert(resultData === testData, "Data not returned");
console.assert(called, "invokePabx (read) not invoked!");
called = false;
tiptel.writePabx(0x99);
tiptel.writePabx(0x99, []);
tiptel.writePabx(0x99, 0x55, 0x66);
tiptel.writePabx(0x99, [0x55, 0x66]);
console.assert(called, "invokePabx (write) not invoked!");
delete tiptel.invokePabx;
stdout.write("done\n\n");
void writeCommandList();
tiptel.on("dtr", (dtr) => {
// console.log(`DTR ${dtr ? "enabled (low)" : "disabled (high)"}`);
serialport.set({ dtr });
})
tiptel.on("ready", () => {
console.log("Tiptel ready.");
})
tiptel.on("command", (data) => {
console.log("Command", data);
})
// without this, we would only get streams once enter is pressed
// NOTE: not available in debug
stdin.setRawMode( true );
// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
stdin.resume();
// No binary, plain text
stdin.setEncoding( "utf8" );
let hexMode = false;
// on any data into stdin
stdin.on( "data", function( key, a2, a3 )
{
if (key === "\u0003")
process.exit();
if (hexMode !== false)
{
switch (key.charCodeAt(0))
{
case 27: // Misc
break;
case 127: // backspace
hexMode = hexMode.substr(0,hexMode.length - 1);
break;
case 10:
case 13:
const data = parseInput(hexMode);
if (data)
{
void tiptel.readPabx(...data);
} else {
console.log("input failed");
}
hexMode = false;
break;
default:
stdout.write(key);
hexMode += key;
}
return;
}
switch (key)
{
case "l":
case "L":
console.log("log enabled");
// log = !log;
serialport.on("data", (d) => console.log("serial",d))
tiptel.on("data", (d) => console.log("parser",d))
break;
case "i":
case "I":
console.log("request isdn version");
void tiptel.readPabx(Command.ISDN_VERSION, 0x13);
break;
case "a":
case "A":
console.log("request analog version");
void tiptel.readPabx(Command.ANALOG_VERSION);
break;
case "p":
case "P":
console.log("programming pin");
void tiptel.readPabx(Command.PIN, 0x02);
break;
case "c":
case "C":
console.log("call charge pin");
void tiptel.readPabx(Command.PIN, 0x01);
break;
case "m":
case "M":
console.log("MSN allocation");
void tiptel.readPabx(Command.MSN_ALLOCATION, 0x01);
break;
case "s":
case "S":
console.log("MSN assignment");
void tiptel.readPabx(Command.MSN_ASSIGNMENT, 0x0C);
// 0x0B is beginning of blocked numbers..
break;
case "h":
case "H":
console.log("Hex (raw) mode enabled");
hexMode = "";
break;
case "q":
case "Q":
// void tiptel.readPabx(0x90); // 15 91 90 93 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 92
break;
default:
// write the key to stdout all normal like
stdout.write( key );
}
});
serialport.on("close", () => console.log("closed"));
serialport.on("end", () => console.log("end"));
serialport.on("error", () => console.log("error"));
serialport.on("finish", () => console.log("finish"));
// TODO: handle DTR flow in-stream
// serialport.on("pause", () => {
// console.log("paused");
// // serialport.set({dtr: false});
// });
// serialport.on("resume", () => {
// console.log("resumed");
// // serialport.set({dtr: true});
// });
serialport.on("drain", () => console.log("drain"));
serialport.open(async (e) => {
if (e) {
console.error(e);
process.exit(1);
}
// Stop flow
serialport.set({dtr: false});
// "Menu"
stdout.write("Commands\n========\n");
stdout.write("L: enable on-screen serial logging\n");
stdout.write("I: get ISDN firmware version\n");
stdout.write("A: get analog firmware version\n");
stdout.write("P: get programming PIN code\n");
stdout.write("C: get call charge PIN code\n");
stdout.write("\n");
stdout.write("CTRL+C: exit\n");
});