Upgrade ofono to 1.2
[profile/ivi/ofono.git] / gatchat / gsmdial.c
1 /*
2  *
3  *  AT chat library with GLib integration
4  *
5  *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sys/signalfd.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35
36 #include <glib.h>
37 #include <gatchat.h>
38 #include <gattty.h>
39 #include <gatppp.h>
40
41 #define IFCONFIG_PATH "/sbin/ifconfig"
42
43 static const char *none_prefix[] = { NULL };
44 static const char *cfun_prefix[] = { "+CFUN:", NULL };
45 static const char *creg_prefix[] = { "+CREG:", NULL };
46 static const char *cgreg_prefix[] = { "+CGREG:", NULL };
47
48 static gchar *option_ip = NULL;
49 static gint option_port = 0;
50 static gchar *option_modem = NULL;
51 static gchar *option_control = NULL;
52 static gint option_cid = 0;
53 static gchar *option_apn = NULL;
54 static gint option_offmode = 0;
55 static gboolean option_legacy = FALSE;
56 static gchar *option_username = NULL;
57 static gchar *option_password = NULL;
58 static gchar *option_pppdump = NULL;
59 static gboolean option_bluetooth = FALSE;
60 static gboolean option_acfc = FALSE;
61 static gboolean option_pfc = FALSE;
62
63 static GAtPPP *ppp;
64 static GAtChat *control;
65 static GAtChat *modem;
66 static GMainLoop *event_loop;
67
68 enum state {
69         STATE_NONE = 0,
70         STATE_REGISTERING,
71         STATE_ATTACHING,
72         STATE_ACTIVATING
73 };
74
75 static int state = 0;
76 static int oldmode = 0;
77
78 static void gsmdial_debug(const char *str, void *data)
79 {
80         g_print("%s: %s\n", (const char *) data, str);
81 }
82
83 static gboolean quit_eventloop(gpointer user_data)
84 {
85         g_main_loop_quit(event_loop);
86         return FALSE;
87 }
88
89 static void power_down(gboolean ok, GAtResult *result, gpointer user_data)
90 {
91         g_main_loop_quit(event_loop);
92 }
93
94 static void kill_ppp(gboolean ok, GAtResult *result, gpointer user_data)
95 {
96         g_print("kill_ppp: %d\n", ok);
97
98         if (ok == FALSE)
99                 return;
100
101         g_at_ppp_unref(ppp);
102         ppp = NULL;
103 }
104
105 static void ppp_suspend_ath0(gpointer user_data)
106 {
107         g_at_chat_resume(modem);
108         g_at_chat_send(modem, "ATH0", none_prefix, kill_ppp, NULL, NULL);
109 }
110
111 static void resume_ppp(gboolean ok, GAtResult *result, gpointer user_data)
112 {
113         g_print("resume_ppp: %d\n", ok);
114
115         if (ok == FALSE)
116                 return;
117
118         g_at_chat_suspend(modem);
119         g_at_ppp_resume(ppp);
120 }
121
122 static void ppp_suspend_ato0(gpointer user_data)
123 {
124         g_at_chat_resume(modem);
125         g_at_chat_send(modem, "ATO0", none_prefix, resume_ppp, NULL, NULL);
126 }
127
128 static gboolean signal_cb(GIOChannel *channel, GIOCondition cond, gpointer data)
129 {
130         static int terminated = 0;
131         int signal_fd = GPOINTER_TO_INT(data);
132         struct signalfd_siginfo si;
133         ssize_t res;
134
135         if (cond & (G_IO_NVAL | G_IO_ERR))
136                 return FALSE;
137
138         res = read(signal_fd, &si, sizeof(si));
139         if (res != sizeof(si))
140                 return FALSE;
141
142         switch (si.ssi_signo) {
143         case SIGINT:
144         case SIGTERM:
145                 if (terminated == 0) {
146                         g_timeout_add_seconds(10, quit_eventloop, NULL);
147
148                         if (ppp == NULL) {
149                                 char buf[64];
150                                 sprintf(buf, "AT+CFUN=%u", option_offmode);
151                                 g_at_chat_send(control, buf, none_prefix,
152                                                 power_down, NULL, NULL);
153                         } else
154                                 g_at_ppp_shutdown(ppp);
155                 }
156
157                 terminated++;
158                 break;
159         case SIGUSR1:
160                 if (ppp == NULL)
161                         break;
162
163                 g_at_ppp_set_suspend_function(ppp, ppp_suspend_ato0, NULL);
164                 g_at_ppp_suspend(ppp);
165                 break;
166         case SIGUSR2:
167                 if (ppp == NULL)
168                         break;
169
170                 g_at_ppp_set_suspend_function(ppp, ppp_suspend_ath0, NULL);
171                 g_at_ppp_suspend(ppp);
172                 break;
173         default:
174                 break;
175         }
176
177         return TRUE;
178 }
179
180 static gboolean at_util_parse_reg_unsolicited(GAtResult *result,
181                                                 const char *prefix, int *status,
182                                                 int *lac, int *ci, int *tech)
183 {
184         GAtResultIter iter;
185         int s;
186         int l = -1, c = -1, t = -1;
187         const char *str;
188
189         g_at_result_iter_init(&iter, result);
190
191         if (g_at_result_iter_next(&iter, prefix) == FALSE)
192                 return FALSE;
193
194         if (g_at_result_iter_next_number(&iter, &s) == FALSE)
195                 return FALSE;
196
197         if (g_at_result_iter_next_string(&iter, &str) == TRUE)
198                 l = strtol(str, NULL, 16);
199         else
200                 goto out;
201
202         if (g_at_result_iter_next_string(&iter, &str) == TRUE)
203                 c = strtol(str, NULL, 16);
204         else
205                 goto out;
206
207         g_at_result_iter_next_number(&iter, &t);
208
209 out:
210         if (status)
211                 *status = s;
212
213         if (lac)
214                 *lac = l;
215
216         if (ci)
217                 *ci = c;
218
219         if (tech)
220                 *tech = t;
221
222         return TRUE;
223 }
224
225 static gboolean at_util_parse_reg(GAtResult *result, const char *prefix,
226                                         int *mode, int *status,
227                                         int *lac, int *ci, int *tech)
228 {
229         GAtResultIter iter;
230         int m, s;
231         int l = -1, c = -1, t = -1;
232         const char *str;
233
234         g_at_result_iter_init(&iter, result);
235
236         while (g_at_result_iter_next(&iter, prefix)) {
237                 g_at_result_iter_next_number(&iter, &m);
238
239                 /* Sometimes we get an unsolicited CREG/CGREG here, skip it */
240                 if (g_at_result_iter_next_number(&iter, &s) == FALSE)
241                         continue;
242
243                 if (g_at_result_iter_next_string(&iter, &str) == TRUE)
244                         l = strtol(str, NULL, 16);
245                 else
246                         goto out;
247
248                 if (g_at_result_iter_next_string(&iter, &str) == TRUE)
249                         c = strtol(str, NULL, 16);
250                 else
251                         goto out;
252
253                 g_at_result_iter_next_number(&iter, &t);
254
255 out:
256                 if (mode)
257                         *mode = m;
258
259                 if (status)
260                         *status = s;
261
262                 if (lac)
263                         *lac = l;
264
265                 if (ci)
266                         *ci = c;
267
268                 if (tech)
269                         *tech = t;
270
271                 return TRUE;
272         }
273
274         return FALSE;
275 }
276
277 static gboolean execute(const char *cmd)
278 {
279         int status;
280
281         status = system(cmd);
282         if (status < 0) {
283                 g_print("Failed to execute command: %s\n", strerror(errno));
284                 return FALSE;
285         }
286
287         return TRUE;
288 }
289
290 static void ppp_connect(const char *iface, const char *local, const char *peer,
291                         const char *dns1, const char *dns2,
292                         gpointer user_data)
293 {
294         char buf[512];
295
296         /* print out the negotiated address and dns server */
297         g_print("Network Device: %s\n", iface);
298         g_print("IP Address: %s\n", local);
299         g_print("Peer IP Address: %s\n", peer);
300         g_print("Primary DNS Server: %s\n", dns1);
301         g_print("Secondary DNS Server: %s\n", dns2);
302
303         if (getuid() != 0) {
304                 g_print("Need root privilege to config PPP interface\n");
305                 return;
306         }
307
308         snprintf(buf, sizeof(buf), "%s %s up", IFCONFIG_PATH, iface);
309         execute(buf);
310
311         snprintf(buf, sizeof(buf), "%s %s %s pointopoint %s", IFCONFIG_PATH,
312                                 iface, local, peer);
313         execute(buf);
314 }
315
316 static void no_carrier_notify(GAtResult *result, gpointer user_data)
317 {
318         char buf[64];
319
320         if (option_bluetooth) {
321                 g_main_loop_quit(event_loop);
322                 return;
323         }
324
325         sprintf(buf, "AT+CFUN=%u", option_offmode);
326         g_at_chat_send(control, buf, none_prefix, power_down, NULL, NULL);
327 }
328
329 static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
330 {
331         g_print("PPP Link down: %d\n", reason);
332
333         g_at_ppp_unref(ppp);
334         ppp = NULL;
335
336         if (option_modem == NULL)
337                 g_at_chat_set_debug(modem, gsmdial_debug, "");
338         else
339                 g_at_chat_set_debug(modem, gsmdial_debug, "Modem");
340
341         g_at_chat_register(modem, "NO CARRIER", no_carrier_notify,
342                                         FALSE, NULL, NULL);
343         g_at_chat_resume(modem);
344 }
345
346 static void connect_cb(gboolean ok, GAtResult *result, gpointer user_data)
347 {
348         GAtIO *io;
349
350         if (!ok) {
351                 g_print("Unable to define context\n");
352                 exit(1);
353         }
354
355         /* get the data IO channel */
356         io = g_at_chat_get_io(modem);
357
358         /*
359          * shutdown gatchat or else it tries to take all the input
360          * from the modem and does not let PPP get it.
361          */
362         g_at_chat_suspend(modem);
363
364         /* open ppp */
365         ppp = g_at_ppp_new();
366         if (ppp == NULL) {
367                 g_print("Unable to create PPP object\n");
368                 exit(1);
369         }
370         g_at_ppp_set_debug(ppp, gsmdial_debug, "PPP");
371
372         g_at_ppp_set_credentials(ppp, option_username, option_password);
373
374         g_at_ppp_set_acfc_enabled(ppp, option_acfc);
375         g_at_ppp_set_pfc_enabled(ppp, option_pfc);
376
377         /* set connect and disconnect callbacks */
378         g_at_ppp_set_connect_function(ppp, ppp_connect, NULL);
379         g_at_ppp_set_disconnect_function(ppp, ppp_disconnect, NULL);
380
381         /* open the ppp connection */
382         g_at_ppp_open(ppp, io);
383
384         if (option_pppdump)
385                 g_at_ppp_set_recording(ppp, option_pppdump);
386 }
387
388 static void at_cgdcont_cb(gboolean ok, GAtResult *result, gpointer user_data)
389 {
390         char buf[64];
391
392         if (!ok) {
393                 g_print("Unable to define context\n");
394                 exit(1);
395         }
396
397         if (option_legacy == TRUE)
398                 sprintf(buf, "ATD*99***%u#", option_cid);
399         else
400                 sprintf(buf, "AT+CGDATA=\"PPP\",%u", option_cid);
401
402         g_at_chat_send(modem, buf, none_prefix, connect_cb, NULL, NULL);
403 }
404
405 static void setup_context(int status)
406 {
407         char buf[1024];
408         int len;
409
410         state = STATE_ACTIVATING;
411
412         g_print("Registered to GPRS network, roaming=%s\n",
413                                         status == 5 ? "true" : "false");
414
415         len = sprintf(buf, "AT+CGDCONT=%u,\"IP\"", option_cid);
416         snprintf(buf + len, sizeof(buf) - len - 3, ",\"%s\"", option_apn);
417         g_at_chat_send(control, buf, none_prefix, at_cgdcont_cb, NULL, NULL);
418 }
419
420 static void cgreg_notify(GAtResult *result, gpointer user_data)
421 {
422         int status, lac, ci, tech;
423
424         if (state != STATE_ATTACHING)
425                 return;
426
427         if (at_util_parse_reg_unsolicited(result, "+CGREG:", &status,
428                                                 &lac, &ci, &tech) == FALSE)
429                 return;
430
431         if (status != 1 && status != 5)
432                 return;
433
434         setup_context(status);
435 }
436
437 static void cgreg_cb(gboolean ok, GAtResult *result, gpointer user_data)
438 {
439         int status, lac, ci, tech;
440
441         if (!ok)
442                 return;
443
444         if (at_util_parse_reg(result, "+CGREG:", NULL, &status,
445                                                 &lac, &ci, &tech) == FALSE)
446                 return;
447
448         if (status != 1 && status != 5) {
449                 g_at_chat_register(control, "+CGREG:",
450                                         cgreg_notify, FALSE, NULL, NULL);
451                 return;
452         }
453
454         setup_context(status);
455 }
456
457 static void attached_cb(gboolean ok, GAtResult *result, gpointer user_data)
458 {
459         if (!ok)
460                 return;
461
462         g_at_chat_send(control, "AT+CGREG?", cgreg_prefix,
463                                                 cgreg_cb, NULL, NULL);
464 }
465
466 static void activate_gprs(int status)
467 {
468         state = STATE_ATTACHING;
469         g_print("Registered to network, roaming=%s\n",
470                                         status == 5 ? "true" : "false");
471
472         g_print("Activating GPRS network...\n");
473         g_at_chat_send(control, "AT+CGATT=1", none_prefix,
474                                                 attached_cb, NULL, NULL);
475 }
476
477 static void creg_notify(GAtResult *result, gpointer user_data)
478 {
479         int status, lac, ci, tech;
480
481         if (state != STATE_REGISTERING)
482                 return;
483
484         if (at_util_parse_reg_unsolicited(result, "+CREG:", &status,
485                                                 &lac, &ci, &tech) == FALSE)
486                 return;
487
488         if (status != 1 && status != 5)
489                 return;
490
491         activate_gprs(status);
492 }
493
494 static void creg_cb(gboolean ok, GAtResult *result, gpointer user_data)
495 {
496         int status, lac, ci, tech;
497
498         if (!ok)
499                 return;
500
501         if (at_util_parse_reg(result, "+CREG:", NULL, &status,
502                                                 &lac, &ci, &tech) == FALSE)
503                 return;
504
505         if (status != 1 && status != 5) {
506                 g_at_chat_register(control, "+CREG:",
507                                                 creg_notify, FALSE, NULL, NULL);
508                 return;
509         }
510
511         activate_gprs(status);
512 }
513
514 static void register_cb(gboolean ok, GAtResult *result, gpointer user_data)
515 {
516         if (!ok) {
517                 g_print("Couldn't register to network, exiting...\n");
518                 exit(1);
519         }
520
521         state = STATE_REGISTERING;
522         g_print("Waiting for network registration...\n");
523
524         g_at_chat_send(control, "AT+CREG?", creg_prefix,
525                                                 creg_cb, NULL, NULL);
526 }
527
528 static void start_dial(gboolean ok, GAtResult *result, gpointer user_data)
529 {
530         if (!ok) {
531                 g_print("Checking PIN status failed\n");
532                 exit(1);
533         }
534
535         g_at_chat_send(control, "AT+CREG=2", none_prefix, NULL, NULL, NULL);
536         g_at_chat_send(control, "AT+CGREG=2", none_prefix, NULL, NULL, NULL);
537
538         g_at_chat_send(control, "AT+COPS=0", none_prefix,
539                                                 register_cb, NULL, NULL);
540 }
541
542 static void check_pin(gboolean ok, GAtResult *result, gpointer user_data)
543 {
544         if (!ok) {
545                 g_print("Turning on the modem failed\n");
546                 exit(1);
547         }
548
549         g_at_chat_send(control, "AT+CPIN?", NULL, start_dial, NULL, NULL);
550 }
551
552 static void check_mode(gboolean ok, GAtResult *result, gpointer user_data)
553 {
554         GAtResultIter iter;
555
556         if (!ok) {
557                 g_print("Checking modem mode failed\n");
558                 exit(1);
559         }
560
561         g_at_result_iter_init(&iter, result);
562         g_at_result_iter_next(&iter, "+CFUN:");
563         g_at_result_iter_next_number(&iter, &oldmode);
564
565         g_print("Current modem mode is %d\n", oldmode);
566
567         if (oldmode == 1) {
568                 check_pin(ok, result, user_data);
569                 return;
570         }
571
572         g_at_chat_send(control, "AT+CFUN=1", NULL, check_pin, NULL, NULL);
573 }
574
575 static int open_serial(void)
576 {
577         GAtSyntax *syntax;
578         GIOChannel *channel;
579
580         channel = g_at_tty_open(option_control, NULL);
581         if (channel == NULL)
582                 return -EIO;
583
584         syntax = g_at_syntax_new_gsm_permissive();
585         control = g_at_chat_new(channel, syntax);
586         g_io_channel_unref(channel);
587         g_at_syntax_unref(syntax);
588
589         if (control == NULL)
590                 return -EIO;
591
592         if (option_modem == NULL) {
593                 g_at_chat_ref(control);
594                 modem = control;
595                 g_at_chat_set_debug(control, gsmdial_debug, "");
596         } else {
597                 g_at_chat_set_debug(control, gsmdial_debug, "Control");
598
599                 channel = g_at_tty_open(option_modem, NULL);
600                 if (channel == NULL)
601                         return -EIO;
602
603                 syntax = g_at_syntax_new_gsm_permissive();
604                 modem = g_at_chat_new(channel, syntax);
605                 g_io_channel_unref(channel);
606                 g_at_syntax_unref(syntax);
607
608                 if (modem == NULL)
609                         return -EIO;
610
611                 g_at_chat_set_debug(modem, gsmdial_debug, "Modem");
612         }
613
614         return 0;
615 }
616
617 static int open_ip(void)
618 {
619         int sk, err;
620         struct sockaddr_in addr;
621         GAtSyntax *syntax;
622         GIOChannel *channel;
623
624         sk = socket(PF_INET, SOCK_STREAM, 0);
625         if (sk < 0)
626                 return -EINVAL;
627
628         memset(&addr, 0, sizeof(addr));
629         addr.sin_family = AF_INET;
630         addr.sin_addr.s_addr = inet_addr(option_ip);
631         addr.sin_port = htons(option_port);
632
633         err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
634         if (err < 0) {
635                 close(sk);
636                 return err;
637         }
638
639         channel = g_io_channel_unix_new(sk);
640         if (channel == NULL) {
641                 close(sk);
642                 return -ENOMEM;
643         }
644
645         syntax = g_at_syntax_new_gsmv1();
646         control = g_at_chat_new(channel, syntax);
647         g_io_channel_unref(channel);
648         g_at_syntax_unref(syntax);
649
650         if (control == NULL)
651                 return -ENOMEM;
652
653         g_at_chat_ref(control);
654         modem = control;
655         g_at_chat_set_debug(control, gsmdial_debug, "");
656
657         return 0;
658 }
659
660 static GOptionEntry options[] = {
661         { "ip", 'i', 0, G_OPTION_ARG_STRING, &option_ip,
662                                 "Specify IP" },
663         { "port", 'p', 0, G_OPTION_ARG_INT, &option_port,
664                                 "Specify IP Port" },
665         { "control", 'n', 0, G_OPTION_ARG_FILENAME, &option_control,
666                                 "Specify Modem Control port" },
667         { "modem", 'm', 0, G_OPTION_ARG_FILENAME, &option_modem,
668                                 "Specify Modem port (ppp), if not provided"
669                                 " the control port will be used" },
670         { "cid", 'c', 0, G_OPTION_ARG_INT, &option_cid,
671                                 "Specify CID to use" },
672         { "apn", 'a', 0, G_OPTION_ARG_STRING, &option_apn,
673                                 "Specify APN" },
674         { "offmode", 'o', 0, G_OPTION_ARG_INT, &option_offmode,
675                                 "Specify CFUN offmode" },
676         { "legacy", 'l', 0, G_OPTION_ARG_NONE, &option_legacy,
677                                 "Use ATD*99***<cid>#" },
678         { "bluetooth", 'b', 0, G_OPTION_ARG_NONE, &option_bluetooth,
679                                 "Use only ATD*99" },
680         { "username", 'u', 0, G_OPTION_ARG_STRING, &option_username,
681                                 "Specify PPP username" },
682         { "password", 'w', 0, G_OPTION_ARG_STRING, &option_password,
683                                 "Specify PPP password" },
684         { "pppdump", 'D', 0, G_OPTION_ARG_STRING, &option_pppdump,
685                                 "Specify pppdump filename" },
686         { "pfc", 0, 0, G_OPTION_ARG_NONE, &option_pfc,
687                                 "Use Protocol Field Compression" },
688         { "acfc", 0, 0, G_OPTION_ARG_NONE, &option_acfc,
689                                 "Use Address & Control Field Compression" },
690         { NULL },
691 };
692
693 int main(int argc, char **argv)
694 {
695         GOptionContext *context;
696         GError *err = NULL;
697         sigset_t mask;
698         int signal_fd;
699         GIOChannel *signal_io;
700         int signal_source;
701
702         context = g_option_context_new(NULL);
703         g_option_context_add_main_entries(context, options, NULL);
704
705         if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
706                 if (err != NULL) {
707                         g_printerr("%s\n", err->message);
708                         g_error_free(err);
709                         return 1;
710                 }
711
712                 g_printerr("An unknown error occurred\n");
713                 return 1;
714         }
715
716         g_option_context_free(context);
717
718         if (option_control) {
719                 int ret;
720
721                 g_print("Control: %s\n", option_control);
722                 if (option_modem)
723                         g_print("Modem: %s\n", option_modem);
724
725                 ret = open_serial();
726                 g_free(option_control);
727                 g_free(option_modem);
728
729                 if (ret < 0)
730                         goto out;
731         } else {
732                 int ret;
733
734                 g_print("IP: %s\n", option_ip);
735                 g_print("Port: %d\n", option_port);
736                 ret = open_ip();
737                 g_free(option_ip);
738
739                 if (ret < 0)
740                         goto out;
741         }
742
743         g_print("APN: %s\n", option_apn);
744         g_print("CID: %d\n", option_cid);
745
746         sigemptyset(&mask);
747         sigaddset(&mask, SIGTERM);
748         sigaddset(&mask, SIGINT);
749         sigaddset(&mask, SIGUSR1);
750         sigaddset(&mask, SIGUSR2);
751         sigaddset(&mask, SIGPIPE);
752
753         if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
754                 perror("Can't set signal mask");
755                 return 1;
756         }
757
758         signal_fd = signalfd(-1, &mask, 0);
759         if (signal_fd < 0) {
760                 perror("Can't create signal filedescriptor");
761                 return 1;
762         }
763
764         signal_io = g_io_channel_unix_new(signal_fd);
765         g_io_channel_set_close_on_unref(signal_io, TRUE);
766         signal_source = g_io_add_watch(signal_io,
767                         G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
768                         signal_cb, GINT_TO_POINTER(signal_fd));
769         g_io_channel_unref(signal_io);
770
771         event_loop = g_main_loop_new(NULL, FALSE);
772
773         if (option_bluetooth) {
774                 g_at_chat_send(control, "ATD*99#", none_prefix, connect_cb,
775                                 NULL, NULL);
776         } else {
777                 g_at_chat_send(control, "ATE0Q0V1", NULL, NULL, NULL, NULL);
778                 g_at_chat_send(control, "AT+CFUN?", cfun_prefix,
779                                                         check_mode, NULL, NULL);
780         }
781
782         g_main_loop_run(event_loop);
783         g_source_remove(signal_source);
784         g_main_loop_unref(event_loop);
785
786 out:
787         if (ppp == NULL) {
788                 g_at_chat_unref(control);
789                 g_at_chat_unref(modem);
790         } else
791                 g_at_ppp_unref(ppp);
792
793         g_free(option_apn);
794
795         return 0;
796 }