-
Notifications
You must be signed in to change notification settings - Fork 717
Expand file tree
/
Copy pathchat_main.c
More file actions
389 lines (321 loc) · 10 KB
/
chat_main.c
File metadata and controls
389 lines (321 loc) · 10 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
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
/****************************************************************************
* apps/examples/chat/chat_main.c
*
* SPDX-License-Identifier: BSD-3-Clause
* SPDX-FileCopyrightText: 2016 Vladimir Komendantskiy. All rights reserved.
* SPDX-FileContributor: Vladimir Komendantskiy <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <assert.h>
#include <nuttx/debug.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "netutils/chat.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define CHAT_TTYNAME_SIZE 32
/****************************************************************************
* Private Types
****************************************************************************/
/* Chat app private state. A supertype of 'chat_ctl'. */
struct chat_app
{
struct chat_ctl ctl; /* Embedded 'chat_ctl' type. */
/* Private fields */
int argc; /* number of command-line arguments */
FAR char **argv; /* command-line arguments */
char tty[CHAT_TTYNAME_SIZE]; /* modem TTY device node */
FAR const char *script; /* raw chat script - input to the parser */
bool script_dynalloc; /* true if the script should be freed */
};
/****************************************************************************
* Private Data
****************************************************************************/
/* Preset chat scripts */
FAR const char g_chat_script0[] = CONFIG_EXAMPLES_CHAT_PRESET0;
FAR const char g_chat_script1[] = CONFIG_EXAMPLES_CHAT_PRESET1;
FAR const char g_chat_script2[] = CONFIG_EXAMPLES_CHAT_PRESET2;
FAR const char g_chat_script3[] = CONFIG_EXAMPLES_CHAT_PRESET3;
/****************************************************************************
* Private Functions
****************************************************************************/
static void chat_show_usage(void)
{
printf("Usage: chat [options]\n"
" where [options] is a possibly empty list of\n"
"-d<file> : modem TTY device node\n"
"-e : echo modem output to stderr\n"
"-f<file> : chat script file\n"
"-p<number> : preprogrammed script\n"
"-t<number> : default modem response timeout\n"
"-v : verbose mode\n");
}
static int chat_chardev(FAR struct chat_app *priv)
{
int flags;
flags = fcntl(priv->ctl.fd, F_GETFL, 0);
if (flags < 0)
{
return flags;
}
flags = fcntl(priv->ctl.fd, F_SETFL, flags | O_NONBLOCK);
if (flags < 0)
{
return flags;
}
return 0;
}
static int chat_script_preset(FAR struct chat_app *priv, int script_number)
{
int ret = 0;
_info("preset script %d\n", script_number);
switch (script_number)
{
case 0:
priv->script = g_chat_script0;
break;
case 1:
priv->script = g_chat_script1;
break;
case 2:
priv->script = g_chat_script2;
break;
case 3:
priv->script = g_chat_script3;
break;
default:
ret = -ERANGE;
break;
}
return ret;
}
static int chat_script_read(FAR struct chat_app *priv,
FAR const char *filepath)
{
FAR char *scriptp;
size_t spare_size = CONFIG_EXAMPLES_CHAT_SIZE - 1;
ssize_t read_size;
bool eof = false;
int ret = 0;
int fd;
scriptp = malloc(CONFIG_EXAMPLES_CHAT_SIZE);
if (scriptp == NULL)
{
return -ENOMEM;
}
priv->script_dynalloc = true;
priv->script = scriptp;
memset(scriptp, 0, CONFIG_EXAMPLES_CHAT_SIZE);
fd = open(filepath, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "Cannot open %s, error %d\n", filepath, errno);
ret = -ENOENT;
}
while (!ret && !eof && spare_size > 0)
{
read_size = read(fd, scriptp, spare_size);
if (read_size < 0)
{
/* EINTR is not a read error. It simply means that a signal was
* received while waiting for the read to complete.
*/
if (errno != EINTR)
{
fprintf(stderr, "Cannot read %s, error %d\n",
filepath, errno);
ret = -EIO;
}
}
else if (read_size == 0)
{
eof = true;
}
else
{
/* read_size > 0 */
DEBUGASSERT(read_size <= spare_size);
scriptp += read_size;
spare_size -= read_size;
}
}
close(fd);
return ret;
}
static int chat_parse_args(FAR struct chat_app *priv)
{
/* -d TTY device node (non-Linux feature)
* -e echo to stderr
* -f script file
* -p preprogrammed script (non-Linux feature)
* -t timeout
* -v verbose mode
*/
int numarg;
int ret = 0;
int i;
DEBUGASSERT(priv != NULL);
if (priv->argc < 2)
{
ret = -EINVAL;
}
/* Iterate through command-line arguments and parse those */
for (i = 1; !ret && i < priv->argc && priv->argv[i]; i++)
{
if (priv->argv[i][0] != '-')
{
ret = -EINVAL;
}
else
{
switch (priv->argv[i][1])
{
case 'd':
/* set the TTY device node */
strlcpy(priv->tty, priv->argv[i] + 2, CHAT_TTYNAME_SIZE);
break;
case 'e':
priv->ctl.echo = true;
break;
case 'f':
ret = chat_script_read(priv, priv->argv[i] + 2);
break;
case 'p':
numarg = strtol(priv->argv[i] + 2, NULL, 10);
if (errno < 0)
{
ret = -EINVAL;
break;
}
ret = chat_script_preset(priv, numarg);
break;
case 't':
numarg = strtol(priv->argv[i] + 2, NULL, 10);
if (errno < 0 || numarg < 0)
{
ret = -EINVAL;
}
else
{
priv->ctl.timeout = numarg;
}
break;
case 'v':
priv->ctl.verbose = true;
break;
default:
ret = -EINVAL;
break;
}
}
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: chat_main
*
* Description:
* Chat command entry point.
*
****************************************************************************/
int main(int argc, FAR char **argv)
{
struct chat_app priv;
int ret;
int exit_code = EXIT_SUCCESS;
priv.argc = argc;
priv.argv = argv;
priv.ctl.echo = false;
priv.ctl.verbose = false;
priv.ctl.timeout = CONFIG_EXAMPLES_CHAT_TIMEOUT_SECONDS;
priv.script = NULL;
priv.script_dynalloc = false;
strlcpy(priv.tty, CONFIG_EXAMPLES_CHAT_TTY_DEVNODE, CHAT_TTYNAME_SIZE);
_info("parsing the arguments\n");
ret = chat_parse_args((FAR struct chat_app *)&priv);
if (ret < 0)
{
_info("Command line parsing failed: code %d, errno %d\n", ret, errno);
chat_show_usage();
exit_code = EXIT_FAILURE;
goto with_script;
}
if (priv.script == NULL)
{
fprintf(stderr, "No chat script given\n");
exit_code = EXIT_FAILURE;
goto no_script;
}
_info("opening %s\n", priv.tty);
priv.ctl.fd = open(priv.tty, O_RDWR);
if (priv.ctl.fd < 0)
{
_info("Failed to open %s: %d\n", priv.tty, errno);
exit_code = EXIT_FAILURE;
goto with_script;
}
_info("setting up character device\n");
ret = chat_chardev(&priv);
if (ret < 0)
{
_info("Failed to open %s: %d\n", priv.tty, errno);
exit_code = EXIT_FAILURE;
goto with_tty_dev;
}
ret = chat((FAR struct chat_ctl *)&priv.ctl, priv.script);
with_tty_dev:
close(priv.ctl.fd);
with_script:
if (priv.script_dynalloc)
{
free((FAR char *)priv.script);
}
no_script:
fflush(stderr);
fflush(stdout);
_info("Exit code %d\n", exit_code);
return exit_code;
}