-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathatinout.c
280 lines (259 loc) · 8.21 KB
/
atinout.c
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
/*
* atinout
*
* This program takes AT commands as input, sends them to the modem and outputs
* the responses.
*
* Copyright (C) 2013 Håkon Løvdal <[email protected]>
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
#include <getopt.h>
#define MAX_LINE_LENGTH (4 * 1024)
static char buf[MAX_LINE_LENGTH];
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'V'},
{"usage", no_argument, NULL, 0},
{NULL, 0, NULL, 0}
};
static const char *short_options = "hV";
static void usage(const char * const argv0)
{
printf("Usage: %s <input_file> <modem_device> <output_file>\n", argv0);
printf("\n");
printf("\t<input_file> is a file with AT commands to be executed. Value '-' means standard input.\n");
printf("\t<modem_device> is the device file for the modem (e.g. /dev/ttyACM0, /dev/ttyS0, etc).\n");
printf("\t<output_file> is a file the responses to the AT commands are saved. Value '-' means standard output.\n");
printf("\n");
printf("In addition the program supports the following options as the only argument:\n");
printf("\n");
printf("\t-h|--help\n");
printf("\t-V|--version\n");
printf("\t--usage\n");
printf("\n");
}
static void help(const char * const argv0)
{
printf("This program takes AT commands as input, sends them to the modem and outputs\n");
printf("the responses.\n");
printf("\n");
printf("Example:\n");
printf("\n");
printf("$ echo 'AT+CPBS=\"ME\",\"ME\"; +CPBR=?' | %s - /dev/ttyACM0 -\n", argv0);
printf("AT+CPBS=\"ME\",\"ME\"; +CPBR=?\n");
printf("\n");
printf("+CPBR: (1-7000),80,62\n");
printf("\n");
printf("OK\n");
printf("$\n");
printf("\n");
printf("See also the --usage command line option\n");
}
/**
* Replace '\n' with '\r', aka `tr '\012' '\015'` for a single line.
* Supports both "\n" and "\r\n" line endings.
*/
static bool tr_lf_cr(const char *s)
{
char *p;
p = strchr(s, '\n');
if (p == NULL || p[1] != '\0') {
return false;
}
if (p > s && p[-1] == '\r') {
*p = '\0';
} else {
*p = '\r';
}
return true;
}
static void strip_cr(char *s)
{
char *from, *to;
from = to = s;
while (*from != '\0') {
if (*from == '\r') {
from++;
continue;
}
*to++ = *from++;
}
*to = '\0';
}
static bool is_final_result(const char * const response)
{
#define STARTS_WITH(a, b) ( strncmp((a), (b), strlen(b)) == 0)
switch (response[0]) {
case '+':
if (STARTS_WITH(&response[1], "CME ERROR:")) {
return true;
}
if (STARTS_WITH(&response[1], "CMS ERROR:")) {
return true;
}
return false;
case 'B':
if (strcmp(&response[1], "USY\n") == 0) {
return true;
}
return false;
case 'E':
if (strcmp(&response[1], "RROR\n") == 0) {
return true;
}
return false;
case 'N':
if (strcmp(&response[1], "O ANSWER\n") == 0) {
return true;
}
if (strcmp(&response[1], "O CARRIER\n") == 0) {
return true;
}
if (strcmp(&response[1], "O DIALTONE\n") == 0) {
return true;
}
return false;
case 'O':
if (strcmp(&response[1], "K\n") == 0) {
return true;
}
// fallthrough
default:
return false;
}
}
int main(int argc, char *argv[])
{
FILE *atcmds;
FILE *modem;
FILE *output;
char *line;
bool success;
int res;
int option_index = 0;
int c;
while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
switch (c) {
case 'h':
help(argv[0]);
return EXIT_SUCCESS;
case 'V':
printf("atinout version " VERSION "\n");
if (argc == 2) {
printf("Copyright (C) 2013 Håkon Løvdal <[email protected]>\n"
"This program comes with ABSOLUTELY NO WARRANTY.\n"
"This is free software, and you are welcome to redistribute it under\n"
"certain conditions; see http://www.gnu.org/licenses/gpl.html for details.\n");
return EXIT_SUCCESS;
}
break;
case 0:
if (strcmp("usage", long_options[option_index].name) == 0) {
usage(argv[0]);
return EXIT_SUCCESS;
}
break;
case '?':
break;
default:
fprintf(stderr, "getopt returned character code 0x%02x\n", (unsigned int)c);
}
}
if ((argc - optind) != 3) {
usage(argv[0]);
return EXIT_FAILURE;
}
#define INPUT_FILE argv[optind + 0]
#define MODEM_DEVICE argv[optind + 1]
#define OUTPUT_FILE argv[optind + 2]
if (strcmp(INPUT_FILE, "-") == 0) {
atcmds = stdin;
} else {
atcmds = fopen(INPUT_FILE, "rb");
if (atcmds == NULL) {
fprintf(stderr, "fopen(%s) failed: %s\n", INPUT_FILE, strerror(errno));
return EXIT_FAILURE;
}
}
modem = fopen(MODEM_DEVICE, "r+b");
if (modem == NULL) {
fprintf(stderr, "fopen(%s) failed: %s\n", MODEM_DEVICE, strerror(errno));
return EXIT_FAILURE;
}
if (strcmp(OUTPUT_FILE, "-") == 0) {
output = stdout;
} else {
output = fopen(OUTPUT_FILE, "wb");
if (output == NULL) {
fprintf(stderr, "fopen(%s) failed: %s\n", OUTPUT_FILE, strerror(errno));
return EXIT_FAILURE;
}
}
while ((line = fgets(buf, (int)sizeof(buf), atcmds)) != NULL) {
success = tr_lf_cr(line);
if (! success) {
fprintf(stderr, "invalid string: '%s'\n", line);
return EXIT_FAILURE;
}
res = fputs(line, modem);
if (res < 0) {
fprintf(stderr, "failed to send '%s' to modem (res = %d)\n", line, res);
return EXIT_FAILURE;
}
do {
line = fgets(buf, (int)sizeof(buf), modem);
if (line == NULL) {
fprintf(stderr, "EOF from modem\n");
return EXIT_FAILURE;
}
strip_cr(line);
res = fputs(line, output);
if (res < 0) {
fprintf(stderr, "failed to write '%s' to output file (res = %d)\n", line, res);
return EXIT_FAILURE;
}
} while (! is_final_result(line));
}
if (strcmp(OUTPUT_FILE, "-") != 0) {
res = fclose(output);
if (res != 0) {
fprintf(stderr, "closing output failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
}
res = fclose(modem);
if (res != 0) {
fprintf(stderr, "closing modem failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
if (strcmp(INPUT_FILE, "-") != 0) {
res = fclose(atcmds);
if (res != 0) {
fprintf(stderr, "closing input failed: %s\n", strerror(errno));
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}