3 * AT chat library with GLib integration
5 * Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
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.
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.
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
31 #include <sys/signalfd.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
41 #define IFCONFIG_PATH "/sbin/ifconfig"
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 };
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;
64 static GAtChat *control;
65 static GAtChat *modem;
66 static GMainLoop *event_loop;
76 static int oldmode = 0;
78 static void gsmdial_debug(const char *str, void *data)
80 g_print("%s: %s\n", (const char *) data, str);
83 static gboolean quit_eventloop(gpointer user_data)
85 g_main_loop_quit(event_loop);
89 static void power_down(gboolean ok, GAtResult *result, gpointer user_data)
91 g_main_loop_quit(event_loop);
94 static void kill_ppp(gboolean ok, GAtResult *result, gpointer user_data)
96 g_print("kill_ppp: %d\n", ok);
105 static void ppp_suspend_ath0(gpointer user_data)
107 g_at_chat_resume(modem);
108 g_at_chat_send(modem, "ATH0", none_prefix, kill_ppp, NULL, NULL);
111 static void resume_ppp(gboolean ok, GAtResult *result, gpointer user_data)
113 g_print("resume_ppp: %d\n", ok);
118 g_at_chat_suspend(modem);
119 g_at_ppp_resume(ppp);
122 static void ppp_suspend_ato0(gpointer user_data)
124 g_at_chat_resume(modem);
125 g_at_chat_send(modem, "ATO0", none_prefix, resume_ppp, NULL, NULL);
128 static gboolean signal_cb(GIOChannel *channel, GIOCondition cond, gpointer data)
130 static int terminated = 0;
131 int signal_fd = GPOINTER_TO_INT(data);
132 struct signalfd_siginfo si;
135 if (cond & (G_IO_NVAL | G_IO_ERR))
138 res = read(signal_fd, &si, sizeof(si));
139 if (res != sizeof(si))
142 switch (si.ssi_signo) {
145 if (terminated == 0) {
146 g_timeout_add_seconds(10, quit_eventloop, NULL);
150 sprintf(buf, "AT+CFUN=%u", option_offmode);
151 g_at_chat_send(control, buf, none_prefix,
152 power_down, NULL, NULL);
154 g_at_ppp_shutdown(ppp);
163 g_at_ppp_set_suspend_function(ppp, ppp_suspend_ato0, NULL);
164 g_at_ppp_suspend(ppp);
170 g_at_ppp_set_suspend_function(ppp, ppp_suspend_ath0, NULL);
171 g_at_ppp_suspend(ppp);
180 static gboolean at_util_parse_reg_unsolicited(GAtResult *result,
181 const char *prefix, int *status,
182 int *lac, int *ci, int *tech)
186 int l = -1, c = -1, t = -1;
189 g_at_result_iter_init(&iter, result);
191 if (g_at_result_iter_next(&iter, prefix) == FALSE)
194 if (g_at_result_iter_next_number(&iter, &s) == FALSE)
197 if (g_at_result_iter_next_string(&iter, &str) == TRUE)
198 l = strtol(str, NULL, 16);
202 if (g_at_result_iter_next_string(&iter, &str) == TRUE)
203 c = strtol(str, NULL, 16);
207 g_at_result_iter_next_number(&iter, &t);
225 static gboolean at_util_parse_reg(GAtResult *result, const char *prefix,
226 int *mode, int *status,
227 int *lac, int *ci, int *tech)
231 int l = -1, c = -1, t = -1;
234 g_at_result_iter_init(&iter, result);
236 while (g_at_result_iter_next(&iter, prefix)) {
237 g_at_result_iter_next_number(&iter, &m);
239 /* Sometimes we get an unsolicited CREG/CGREG here, skip it */
240 if (g_at_result_iter_next_number(&iter, &s) == FALSE)
243 if (g_at_result_iter_next_string(&iter, &str) == TRUE)
244 l = strtol(str, NULL, 16);
248 if (g_at_result_iter_next_string(&iter, &str) == TRUE)
249 c = strtol(str, NULL, 16);
253 g_at_result_iter_next_number(&iter, &t);
277 static gboolean execute(const char *cmd)
281 status = system(cmd);
283 g_print("Failed to execute command: %s\n", strerror(errno));
290 static void ppp_connect(const char *iface, const char *local, const char *peer,
291 const char *dns1, const char *dns2,
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);
304 g_print("Need root privilege to config PPP interface\n");
308 snprintf(buf, sizeof(buf), "%s %s up", IFCONFIG_PATH, iface);
311 snprintf(buf, sizeof(buf), "%s %s %s pointopoint %s", IFCONFIG_PATH,
316 static void no_carrier_notify(GAtResult *result, gpointer user_data)
320 if (option_bluetooth) {
321 g_main_loop_quit(event_loop);
325 sprintf(buf, "AT+CFUN=%u", option_offmode);
326 g_at_chat_send(control, buf, none_prefix, power_down, NULL, NULL);
329 static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
331 g_print("PPP Link down: %d\n", reason);
336 if (option_modem == NULL)
337 g_at_chat_set_debug(modem, gsmdial_debug, "");
339 g_at_chat_set_debug(modem, gsmdial_debug, "Modem");
341 g_at_chat_register(modem, "NO CARRIER", no_carrier_notify,
343 g_at_chat_resume(modem);
346 static void connect_cb(gboolean ok, GAtResult *result, gpointer user_data)
351 g_print("Unable to define context\n");
355 /* get the data IO channel */
356 io = g_at_chat_get_io(modem);
359 * shutdown gatchat or else it tries to take all the input
360 * from the modem and does not let PPP get it.
362 g_at_chat_suspend(modem);
365 ppp = g_at_ppp_new();
367 g_print("Unable to create PPP object\n");
370 g_at_ppp_set_debug(ppp, gsmdial_debug, "PPP");
372 g_at_ppp_set_credentials(ppp, option_username, option_password);
374 g_at_ppp_set_acfc_enabled(ppp, option_acfc);
375 g_at_ppp_set_pfc_enabled(ppp, option_pfc);
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);
381 /* open the ppp connection */
382 g_at_ppp_open(ppp, io);
385 g_at_ppp_set_recording(ppp, option_pppdump);
388 static void at_cgdcont_cb(gboolean ok, GAtResult *result, gpointer user_data)
393 g_print("Unable to define context\n");
397 if (option_legacy == TRUE)
398 sprintf(buf, "ATD*99***%u#", option_cid);
400 sprintf(buf, "AT+CGDATA=\"PPP\",%u", option_cid);
402 g_at_chat_send(modem, buf, none_prefix, connect_cb, NULL, NULL);
405 static void setup_context(int status)
410 state = STATE_ACTIVATING;
412 g_print("Registered to GPRS network, roaming=%s\n",
413 status == 5 ? "true" : "false");
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);
420 static void cgreg_notify(GAtResult *result, gpointer user_data)
422 int status, lac, ci, tech;
424 if (state != STATE_ATTACHING)
427 if (at_util_parse_reg_unsolicited(result, "+CGREG:", &status,
428 &lac, &ci, &tech) == FALSE)
431 if (status != 1 && status != 5)
434 setup_context(status);
437 static void cgreg_cb(gboolean ok, GAtResult *result, gpointer user_data)
439 int status, lac, ci, tech;
444 if (at_util_parse_reg(result, "+CGREG:", NULL, &status,
445 &lac, &ci, &tech) == FALSE)
448 if (status != 1 && status != 5) {
449 g_at_chat_register(control, "+CGREG:",
450 cgreg_notify, FALSE, NULL, NULL);
454 setup_context(status);
457 static void attached_cb(gboolean ok, GAtResult *result, gpointer user_data)
462 g_at_chat_send(control, "AT+CGREG?", cgreg_prefix,
463 cgreg_cb, NULL, NULL);
466 static void activate_gprs(int status)
468 state = STATE_ATTACHING;
469 g_print("Registered to network, roaming=%s\n",
470 status == 5 ? "true" : "false");
472 g_print("Activating GPRS network...\n");
473 g_at_chat_send(control, "AT+CGATT=1", none_prefix,
474 attached_cb, NULL, NULL);
477 static void creg_notify(GAtResult *result, gpointer user_data)
479 int status, lac, ci, tech;
481 if (state != STATE_REGISTERING)
484 if (at_util_parse_reg_unsolicited(result, "+CREG:", &status,
485 &lac, &ci, &tech) == FALSE)
488 if (status != 1 && status != 5)
491 activate_gprs(status);
494 static void creg_cb(gboolean ok, GAtResult *result, gpointer user_data)
496 int status, lac, ci, tech;
501 if (at_util_parse_reg(result, "+CREG:", NULL, &status,
502 &lac, &ci, &tech) == FALSE)
505 if (status != 1 && status != 5) {
506 g_at_chat_register(control, "+CREG:",
507 creg_notify, FALSE, NULL, NULL);
511 activate_gprs(status);
514 static void register_cb(gboolean ok, GAtResult *result, gpointer user_data)
517 g_print("Couldn't register to network, exiting...\n");
521 state = STATE_REGISTERING;
522 g_print("Waiting for network registration...\n");
524 g_at_chat_send(control, "AT+CREG?", creg_prefix,
525 creg_cb, NULL, NULL);
528 static void start_dial(gboolean ok, GAtResult *result, gpointer user_data)
531 g_print("Checking PIN status failed\n");
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);
538 g_at_chat_send(control, "AT+COPS=0", none_prefix,
539 register_cb, NULL, NULL);
542 static void check_pin(gboolean ok, GAtResult *result, gpointer user_data)
545 g_print("Turning on the modem failed\n");
549 g_at_chat_send(control, "AT+CPIN?", NULL, start_dial, NULL, NULL);
552 static void check_mode(gboolean ok, GAtResult *result, gpointer user_data)
557 g_print("Checking modem mode failed\n");
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);
565 g_print("Current modem mode is %d\n", oldmode);
568 check_pin(ok, result, user_data);
572 g_at_chat_send(control, "AT+CFUN=1", NULL, check_pin, NULL, NULL);
575 static int open_serial(void)
580 channel = g_at_tty_open(option_control, NULL);
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);
592 if (option_modem == NULL) {
593 g_at_chat_ref(control);
595 g_at_chat_set_debug(control, gsmdial_debug, "");
597 g_at_chat_set_debug(control, gsmdial_debug, "Control");
599 channel = g_at_tty_open(option_modem, NULL);
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);
611 g_at_chat_set_debug(modem, gsmdial_debug, "Modem");
617 static int open_ip(void)
620 struct sockaddr_in addr;
624 sk = socket(PF_INET, SOCK_STREAM, 0);
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);
633 err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
639 channel = g_io_channel_unix_new(sk);
640 if (channel == NULL) {
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);
653 g_at_chat_ref(control);
655 g_at_chat_set_debug(control, gsmdial_debug, "");
660 static GOptionEntry options[] = {
661 { "ip", 'i', 0, G_OPTION_ARG_STRING, &option_ip,
663 { "port", 'p', 0, G_OPTION_ARG_INT, &option_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,
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,
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" },
693 int main(int argc, char **argv)
695 GOptionContext *context;
699 GIOChannel *signal_io;
702 context = g_option_context_new(NULL);
703 g_option_context_add_main_entries(context, options, NULL);
705 if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
707 g_printerr("%s\n", err->message);
712 g_printerr("An unknown error occurred\n");
716 g_option_context_free(context);
718 if (option_control) {
721 g_print("Control: %s\n", option_control);
723 g_print("Modem: %s\n", option_modem);
726 g_free(option_control);
727 g_free(option_modem);
734 g_print("IP: %s\n", option_ip);
735 g_print("Port: %d\n", option_port);
743 g_print("APN: %s\n", option_apn);
744 g_print("CID: %d\n", option_cid);
747 sigaddset(&mask, SIGTERM);
748 sigaddset(&mask, SIGINT);
749 sigaddset(&mask, SIGUSR1);
750 sigaddset(&mask, SIGUSR2);
751 sigaddset(&mask, SIGPIPE);
753 if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
754 perror("Can't set signal mask");
758 signal_fd = signalfd(-1, &mask, 0);
760 perror("Can't create signal filedescriptor");
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);
771 event_loop = g_main_loop_new(NULL, FALSE);
773 if (option_bluetooth) {
774 g_at_chat_send(control, "ATD*99#", none_prefix, connect_cb,
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);
782 g_main_loop_run(event_loop);
783 g_source_remove(signal_source);
784 g_main_loop_unref(event_loop);
788 g_at_chat_unref(control);
789 g_at_chat_unref(modem);