packaging: Bump to 1.17
[platform/upstream/ofono.git] / unit / test-caif.c
1 /*
2  *
3  *  oFono - Open Source Telephony
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 <errno.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
33
34 #include <glib.h>
35 #include <glib/gprintf.h>
36
37 #include <gatchat.h>
38
39 #include <drivers/stemodem/caif_socket.h>
40 #include <drivers/stemodem/if_caif.h>
41
42 static GMainLoop *mainloop;
43
44 static int do_open(void)
45 {
46         int fd;
47
48         fd = open("/dev/chnlat11", O_RDWR);
49         if (fd < 0) {
50                 g_printerr("Open of chnlat11 failed (%d)\n", errno);
51                 return -EIO;
52         }
53
54         return fd;
55 }
56
57 static int do_connect(void)
58 {
59         struct sockaddr_caif addr;
60         int sk, err;
61
62         /* Create a CAIF socket for AT Service */
63         sk = socket(AF_CAIF, SOCK_SEQPACKET, CAIFPROTO_AT);
64         if (sk < 0) {
65                 g_printerr("CAIF socket creation failed (%d)\n", errno);
66                 return -EIO;
67         }
68
69         memset(&addr, 0, sizeof(addr));
70         addr.family = AF_CAIF;
71         addr.u.at.type = CAIF_ATTYPE_PLAIN;
72
73         /* Connect to the AT Service at the modem */
74         err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
75         if (err < 0) {
76                 g_printerr("CAIF socket connect failed (%d)\n", errno);
77                 close(sk);
78                 return err;
79         }
80
81         return sk;
82 }
83
84 static void caif_debug(const char *str, void *data)
85 {
86         g_print("%s\n", str);
87 }
88
89 static void caif_init(gboolean ok, GAtResult *result, gpointer data)
90 {
91         GAtChat *chat = data;
92
93         g_print("caif_init: %d\n", ok);
94
95         if (ok == FALSE) {
96                 g_at_chat_unref(chat);
97                 g_main_loop_quit(mainloop);
98                 return;
99         }
100
101         g_at_chat_unref(chat);
102         g_main_loop_quit(mainloop);
103 }
104
105 static void test_connect(gboolean use_socket)
106 {
107         GIOChannel *io;
108         GAtChat *chat;
109         GAtSyntax *syntax;
110         int fd;
111
112         if (use_socket == TRUE)
113                 fd = do_connect();
114         else
115                 fd = do_open();
116
117         if (fd < 0)
118                 return;
119
120         io = g_io_channel_unix_new(fd);
121         g_io_channel_set_close_on_unref(io, TRUE);
122
123         syntax = g_at_syntax_new_gsm_permissive();
124         chat = g_at_chat_new_blocking(io, syntax);
125         g_at_syntax_unref(syntax);
126
127         g_io_channel_unref(io);
128
129         if (chat == NULL) {
130                 g_printerr("Chat creation failed\n");
131                 return;
132         }
133
134         g_at_chat_set_debug(chat, caif_debug, NULL);
135         g_at_chat_send(chat, "ATE0 +CMEE=1", NULL, caif_init, chat, NULL);
136
137         mainloop = g_main_loop_new(NULL, FALSE);
138
139         g_main_loop_run(mainloop);
140         g_main_loop_unref(mainloop);
141 }
142
143 static void test_basic(void)
144 {
145         if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) {
146                 test_connect(TRUE);
147                 exit(0);
148         }
149
150         g_test_trap_assert_passed();
151         //g_test_trap_assert_stderr("failed");
152 }
153
154 static void test_chnlat(void)
155 {
156         if (g_test_trap_fork(60 * 1000 * 1000, 0) == TRUE) {
157                 test_connect(FALSE);
158                 exit(0);
159         }
160
161         g_test_trap_assert_passed();
162         //g_test_trap_assert_stderr("failed");
163 }
164
165 int main(int argc, char **argv)
166 {
167         g_test_init(&argc, &argv, NULL);
168
169         g_test_add_func("/testcaif/basic", test_basic);
170         g_test_add_func("/testcaif/chnlat", test_chnlat);
171
172         return g_test_run();
173 }