-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsendATCommands.ts
More file actions
156 lines (141 loc) · 4.28 KB
/
Copy pathsendATCommands.ts
File metadata and controls
156 lines (141 loc) · 4.28 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
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-4-Clause
*/
import {
createSerialPort,
SerialPort,
ShellParser,
shellParser,
xTerminalShellParserWrapper,
} from '@nordicsemiconductor/pc-nrfconnect-shared';
import { Terminal } from '@xterm/headless';
import { formatResponse } from './formatATResponse';
export interface Command {
command: string;
responseRegex: string;
}
const sendCommandShellMode = (parser: ShellParser, command: string) =>
new Promise<string>((resolve, reject) => {
parser.enqueueRequest(`at ${command}`, {
onSuccess: res => {
resolve(res);
},
onError: err => {
reject(err);
},
onTimeout: () => {
reject(new Error('timeout'));
},
});
});
const decoder = new TextDecoder();
const sendCommandLineMode = (serialPort: SerialPort, command: string) =>
new Promise<string>((resolve, reject) => {
let response = '';
let finishedWriting = false;
let timeout: NodeJS.Timeout;
const handler = serialPort.onData(data => {
if (!finishedWriting) return;
response += decoder.decode(data);
const isCompleteResponse =
response.includes('\nOK') || response.includes('\nERROR');
if (isCompleteResponse) {
clearTimeout(timeout);
finishedWriting = false;
handler();
if (response.includes('ERROR')) {
reject(new Error(`Response has ERROR ${response}`));
}
if (response.includes('OK')) {
resolve(response);
}
}
});
timeout = setTimeout(() => {
handler();
reject(new Error('Timed out'));
}, 2000);
serialPort.write(`${command}\r\n`).then(() => {
finishedWriting = true;
});
});
export default async (
commands: Command[],
path: string,
mode?: 'LINE' | 'SHELL',
) => {
let serialPort: {
sendCommand: (cmd: string) => Promise<string>;
unregister: () => Promise<void>;
};
const createdSerialPort = await createSerialPort(
{
path,
baudRate: 115200,
},
{ overwrite: true, settingsLocked: true },
);
if (!mode) {
try {
await sendCommandLineMode(createdSerialPort, 'at AT');
mode = 'SHELL';
} catch {
mode = 'LINE';
}
}
if (mode === 'SHELL') {
const sp = await shellParser(
createdSerialPort,
xTerminalShellParserWrapper(
new Terminal({
allowProposedApi: true,
cols: 999,
}),
),
{
logRegex:
/[[][0-9]{2,}:[0-9]{2}:[0-9]{2}.[0-9]{3},[0-9]{3}] <([^<^>]+)> ([^:]+): .*(\r\n|\r|\n)$/,
errorRegex: /ERROR/,
timeout: 1000,
columnWidth: 80,
},
);
serialPort = {
sendCommand: (cmd: string) => sendCommandShellMode(sp, cmd),
unregister: async () => {
sp.unregister();
await createdSerialPort.close();
},
};
} else {
serialPort = {
sendCommand: (cmd: string) =>
sendCommandLineMode(createdSerialPort, cmd),
unregister: () => createdSerialPort.close(),
};
}
const newResponses: string[] = [];
const reducedPromise = commands.reduce(
(acc, next) =>
acc.then(() =>
serialPort.sendCommand(next.command).then(value => {
newResponses.push(
formatResponse(value, next.responseRegex),
);
return Promise.resolve();
}),
),
Promise.resolve(),
);
try {
await reducedPromise;
} catch {
serialPort.unregister();
await serialPort.unregister();
throw new Error('Received ERROR as return value from AT command');
}
await serialPort.unregister();
return newResponses;
};