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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
|
/*
*
* AT chat library with GLib integration
*
* Copyright (C) 2008-2010 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/signalfd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <glib.h>
#include <gatchat.h>
#include <gattty.h>
#include <gatppp.h>
static const char *none_prefix[] = { NULL };
static const char *cfun_prefix[] = { "+CFUN:", NULL };
static const char *creg_prefix[] = { "+CREG:", NULL };
static const char *cgreg_prefix[] = { "+CGREG:", NULL };
static gchar *option_ip = NULL;
static gint option_port = 0;
static gchar *option_modem = NULL;
static gchar *option_control = NULL;
static gint option_cid = 0;
static gchar *option_apn = NULL;
static gint option_offmode = 0;
static gboolean option_legacy = FALSE;
static gchar *option_username = NULL;
static gchar *option_password = NULL;
static gchar *option_pppdump = NULL;
static GAtPPP *ppp;
static GAtChat *control;
static GAtChat *modem;
static GMainLoop *event_loop;
enum state {
STATE_NONE = 0,
STATE_REGISTERING,
STATE_ATTACHING,
STATE_ACTIVATING
};
static int state = 0;
static int oldmode = 0;
static void gsmdial_debug(const char *str, void *data)
{
g_print("%s: %s\n", (const char *) data, str);
}
static gboolean quit_eventloop(gpointer user_data)
{
g_main_loop_quit(event_loop);
return FALSE;
}
static void power_down(gboolean ok, GAtResult *result, gpointer user_data)
{
g_main_loop_quit(event_loop);
}
static gboolean signal_cb(GIOChannel *channel, GIOCondition cond, gpointer data)
{
static int terminated = 0;
int signal_fd = GPOINTER_TO_INT(data);
struct signalfd_siginfo si;
ssize_t res;
if (cond & (G_IO_NVAL | G_IO_ERR))
return FALSE;
res = read(signal_fd, &si, sizeof(si));
if (res != sizeof(si))
return FALSE;
switch (si.ssi_signo) {
case SIGINT:
case SIGTERM:
if (terminated == 0) {
char buf[64];
if (ppp)
g_at_ppp_shutdown(ppp);
g_timeout_add_seconds(10, quit_eventloop, NULL);
sprintf(buf, "AT+CFUN=%u", option_offmode);
g_at_chat_send(control, buf, none_prefix,
power_down, NULL, NULL);
}
terminated++;
break;
default:
break;
}
return TRUE;
}
static gboolean at_util_parse_reg_unsolicited(GAtResult *result,
const char *prefix, int *status,
int *lac, int *ci, int *tech)
{
GAtResultIter iter;
int s;
int l = -1, c = -1, t = -1;
const char *str;
g_at_result_iter_init(&iter, result);
if (g_at_result_iter_next(&iter, prefix) == FALSE)
return FALSE;
if (g_at_result_iter_next_number(&iter, &s) == FALSE)
return FALSE;
if (g_at_result_iter_next_string(&iter, &str) == TRUE)
l = strtol(str, NULL, 16);
else
goto out;
if (g_at_result_iter_next_string(&iter, &str) == TRUE)
c = strtol(str, NULL, 16);
else
goto out;
g_at_result_iter_next_number(&iter, &t);
out:
if (status)
*status = s;
if (lac)
*lac = l;
if (ci)
*ci = c;
if (tech)
*tech = t;
return TRUE;
}
static gboolean at_util_parse_reg(GAtResult *result, const char *prefix,
int *mode, int *status,
int *lac, int *ci, int *tech)
{
GAtResultIter iter;
int m, s;
int l = -1, c = -1, t = -1;
const char *str;
g_at_result_iter_init(&iter, result);
while (g_at_result_iter_next(&iter, prefix)) {
g_at_result_iter_next_number(&iter, &m);
/* Sometimes we get an unsolicited CREG/CGREG here, skip it */
if (g_at_result_iter_next_number(&iter, &s) == FALSE)
continue;
if (g_at_result_iter_next_string(&iter, &str) == TRUE)
l = strtol(str, NULL, 16);
else
goto out;
if (g_at_result_iter_next_string(&iter, &str) == TRUE)
c = strtol(str, NULL, 16);
else
goto out;
g_at_result_iter_next_number(&iter, &t);
out:
if (mode)
*mode = m;
if (status)
*status = s;
if (lac)
*lac = l;
if (ci)
*ci = c;
if (tech)
*tech = t;
return TRUE;
}
return FALSE;
}
static void ppp_connect(GAtPPPConnectStatus success,
const char *iface, const char *ip,
const char *dns1, const char *dns2,
gpointer user_data)
{
if (success != G_AT_PPP_CONNECT_SUCCESS) {
g_print("Failed to create PPP interface!\n");
return;
}
/* print out the negotiated address and dns server */
g_print("Network Device: %s\n", iface);
g_print("IP Address: %s\n", ip);
g_print("Primary DNS Server: %s\n", dns1);
g_print("Secondary DNS Server: %s\n", dns2);
}
static void ppp_disconnect(gpointer user_data)
{
g_print("PPP Link down\n");
}
static void connect_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
GIOChannel *channel;
if (!ok) {
g_print("Unable to define context\n");
exit(1);
}
/* get the data IO channel */
channel = g_at_chat_get_channel(modem);
/*
* shutdown gatchat or else it tries to take all the input
* from the modem and does not let PPP get it.
*/
g_at_chat_shutdown(control);
g_at_chat_shutdown(modem);
/* open ppp */
ppp = g_at_ppp_new(channel);
if (!ppp) {
g_print("Unable to create PPP object\n");
exit(1);
}
g_at_ppp_set_debug(ppp, gsmdial_debug, "PPP");
if (option_pppdump)
g_at_ppp_set_recording(ppp, option_pppdump);
g_at_ppp_set_credentials(ppp, option_username, option_password);
/* set connect and disconnect callbacks */
g_at_ppp_set_connect_function(ppp, ppp_connect, NULL);
g_at_ppp_set_disconnect_function(ppp, ppp_disconnect, NULL);
/* open the ppp connection */
g_at_ppp_open(ppp);
}
static void at_cgdcont_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
char buf[64];
if (!ok) {
g_print("Unable to define context\n");
exit(1);
}
if (option_legacy == TRUE)
sprintf(buf, "ATD*99***%u#", option_cid);
else
sprintf(buf, "AT+CGDATA=\"PPP\",%u", option_cid);
g_at_chat_send(modem, buf, none_prefix, connect_cb, NULL, NULL);
}
static void setup_context(int status)
{
char buf[1024];
int len;
state = STATE_ACTIVATING;
g_print("Registered to GPRS network, roaming=%s\n",
status == 5 ? "true" : "false");
len = sprintf(buf, "AT+CGDCONT=%u,\"IP\"", option_cid);
snprintf(buf + len, sizeof(buf) - len - 3, ",\"%s\"", option_apn);
g_at_chat_send(control, buf, none_prefix, at_cgdcont_cb, NULL, NULL);
}
static void cgreg_notify(GAtResult *result, gpointer user_data)
{
int status, lac, ci, tech;
if (state != STATE_ATTACHING)
return;
if (at_util_parse_reg_unsolicited(result, "+CGREG:", &status,
&lac, &ci, &tech) == FALSE)
return;
if (status != 1 && status != 5)
return;
setup_context(status);
}
static void cgreg_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
int status, lac, ci, tech;
if (!ok)
return;
if (at_util_parse_reg(result, "+CGREG:", NULL, &status,
&lac, &ci, &tech) == FALSE)
return;
if (status != 1 && status != 5) {
g_at_chat_register(control, "+CGREG:",
cgreg_notify, FALSE, NULL, NULL);
return;
}
setup_context(status);
}
static void attached_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
if (!ok)
return;
g_at_chat_send(control, "AT+CGREG?", cgreg_prefix,
cgreg_cb, NULL, NULL);
}
static void activate_gprs(int status)
{
state = STATE_ATTACHING;
g_print("Registered to network, roaming=%s\n",
status == 5 ? "true" : "false");
g_print("Activating GPRS network...\n");
g_at_chat_send(control, "AT+CGATT=1", none_prefix,
attached_cb, NULL, NULL);
}
static void creg_notify(GAtResult *result, gpointer user_data)
{
int status, lac, ci, tech;
if (state != STATE_REGISTERING)
return;
if (at_util_parse_reg_unsolicited(result, "+CREG:", &status,
&lac, &ci, &tech) == FALSE)
return;
if (status != 1 && status != 5)
return;
activate_gprs(status);
}
static void creg_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
int status, lac, ci, tech;
if (!ok)
return;
if (at_util_parse_reg(result, "+CREG:", NULL, &status,
&lac, &ci, &tech) == FALSE)
return;
if (status != 1 && status != 5) {
g_at_chat_register(control, "+CREG:",
creg_notify, FALSE, NULL, NULL);
return;
}
activate_gprs(status);
}
static void register_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
if (!ok) {
g_print("Couldn't register to network, exiting...\n");
exit(1);
}
state = STATE_REGISTERING;
g_print("Waiting for network registration...\n");
g_at_chat_send(control, "AT+CREG?", creg_prefix,
creg_cb, NULL, NULL);
}
static void start_dial(gboolean ok, GAtResult *result, gpointer user_data)
{
if (!ok) {
g_print("Turning on the modem failed\n");
exit(1);
}
g_at_chat_send(control, "AT+CREG=2", none_prefix, NULL, NULL, NULL);
g_at_chat_send(control, "AT+CGREG=2", none_prefix, NULL, NULL, NULL);
g_at_chat_send(control, "AT+COPS=0", none_prefix,
register_cb, NULL, NULL);
}
static void check_mode(gboolean ok, GAtResult *result, gpointer user_data)
{
GAtResultIter iter;
if (!ok) {
g_print("Checking modem mode failed\n");
exit(1);
}
g_at_result_iter_init(&iter, result);
g_at_result_iter_next(&iter, "+CFUN:");
g_at_result_iter_next_number(&iter, &oldmode);
g_print("Current modem mode is %d\n", oldmode);
if (oldmode == 1) {
start_dial(ok, result, user_data);
return;
}
g_at_chat_send(control, "AT+CFUN=1", NULL, start_dial, NULL, NULL);
}
static int open_serial()
{
GAtSyntax *syntax;
GIOChannel *channel;
channel = g_at_tty_open(option_control, NULL);
if (channel == NULL)
return -EIO;
syntax = g_at_syntax_new_gsm_permissive();
control = g_at_chat_new(channel, syntax);
g_io_channel_unref(channel);
g_at_syntax_unref(syntax);
if (control == NULL)
return -EIO;
if (option_modem == NULL) {
g_at_chat_ref(control);
modem = control;
g_at_chat_set_debug(control, gsmdial_debug, "");
} else {
g_at_chat_set_debug(control, gsmdial_debug, "Control");
channel = g_at_tty_open(option_modem, NULL);
if (channel == NULL)
return -EIO;
syntax = g_at_syntax_new_gsm_permissive();
modem = g_at_chat_new(channel, syntax);
g_io_channel_unref(channel);
g_at_syntax_unref(syntax);
if (modem == NULL)
return -EIO;
g_at_chat_set_debug(modem, gsmdial_debug, "Modem");
}
return 0;
}
static int open_ip()
{
int sk, err;
struct sockaddr_in addr;
GAtSyntax *syntax;
GIOChannel *channel;
sk = socket(PF_INET, SOCK_STREAM, 0);
if (sk < 0)
return -EINVAL;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(option_ip);
addr.sin_port = htons(option_port);
err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
if (err < 0) {
close(sk);
return err;
}
channel = g_io_channel_unix_new(sk);
if (channel == NULL) {
close(sk);
return -ENOMEM;
}
syntax = g_at_syntax_new_gsmv1();
control = g_at_chat_new(channel, syntax);
g_io_channel_unref(channel);
g_at_syntax_unref(syntax);
if (control == NULL)
return -ENOMEM;
g_at_chat_ref(control);
modem = control;
g_at_chat_set_debug(control, gsmdial_debug, "");
return 0;
}
static GOptionEntry options[] = {
{ "ip", 'i', 0, G_OPTION_ARG_STRING, &option_ip,
"Specify IP" },
{ "port", 'p', 0, G_OPTION_ARG_INT, &option_port,
"Specify IP Port" },
{ "control", 'n', 0, G_OPTION_ARG_FILENAME, &option_control,
"Specify Modem Control port" },
{ "modem", 'm', 0, G_OPTION_ARG_FILENAME, &option_modem,
"Specify Modem port (ppp), if not provided"
" the control port will be used" },
{ "cid", 'c', 0, G_OPTION_ARG_INT, &option_cid,
"Specify CID to use" },
{ "apn", 'a', 0, G_OPTION_ARG_STRING, &option_apn,
"Specify APN" },
{ "offmode", 'o', 0, G_OPTION_ARG_INT, &option_offmode,
"Specify CFUN offmode" },
{ "legacy", 'l', 0, G_OPTION_ARG_NONE, &option_legacy,
"Use ATD*99***<cid>#" },
{ "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
"Specify PPP username" },
{ "password", 'w', 0, G_OPTION_ARG_STRING, &option_password,
"Specify PPP password" },
{ "pppdump", 'D', 0, G_OPTION_ARG_STRING, &option_pppdump,
"Specify pppdump filename" },
{ NULL },
};
int main(int argc, char **argv)
{
GOptionContext *context;
GError *err = NULL;
sigset_t mask;
int signal_fd;
GIOChannel *signal_io;
int signal_source;
context = g_option_context_new(NULL);
g_option_context_add_main_entries(context, options, NULL);
if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
if (err != NULL) {
g_printerr("%s\n", err->message);
g_error_free(err);
return 1;
}
g_printerr("An unknown error occurred\n");
return 1;
}
g_option_context_free(context);
if (option_control) {
int ret;
g_print("Control: %s\n", option_control);
if (option_modem)
g_print("Modem: %s\n", option_modem);
ret = open_serial();
g_free(option_control);
g_free(option_modem);
if (ret < 0)
goto out;
} else {
int ret;
g_print("IP: %s\n", option_ip);
g_print("Port: %d\n", option_port);
ret = open_ip();
g_free(option_ip);
if (ret < 0)
goto out;
}
g_print("APN: %s\n", option_apn);
g_print("CID: %d\n", option_cid);
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGUSR2);
sigaddset(&mask, SIGPIPE);
if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
perror("Can't set signal mask");
return 1;
}
signal_fd = signalfd(-1, &mask, 0);
if (signal_fd < 0) {
perror("Can't create signal filedescriptor");
return 1;
}
signal_io = g_io_channel_unix_new(signal_fd);
g_io_channel_set_close_on_unref(signal_io, TRUE);
signal_source = g_io_add_watch(signal_io,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
signal_cb, GINT_TO_POINTER(signal_fd));
g_io_channel_unref(signal_io);
event_loop = g_main_loop_new(NULL, FALSE);
g_at_chat_send(control, "ATE0Q0V1", NULL, NULL, NULL, NULL);
g_at_chat_send(control, "AT+CFUN?", cfun_prefix,
check_mode, NULL, NULL);
g_main_loop_run(event_loop);
g_source_remove(signal_source);
g_main_loop_unref(event_loop);
out:
g_at_chat_unref(control);
g_at_chat_unref(modem);
g_free(option_apn);
return 0;
}
|