tizen 2.3.1 release
[framework/telephony/libtcore.git] / unit-test / test-at.c
1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 #include <glib.h>
7
8 #include <tcore.h>
9 #include <at.h>
10
11 static gboolean on_cmti(TcoreAT *at, const GSList *lines, void *user_data)
12 {
13         int *checked = user_data;
14
15         *checked = 1;
16
17         printf("data = [%s]\n", (char *)lines->data);
18
19         return FALSE;
20 }
21
22 static gboolean on_cmt(TcoreAT *at, const GSList *lines, void *user_data)
23 {
24         int *checked = user_data;
25
26         if (g_slist_length((GSList *)lines) != 2) {
27                 printf("invalid message\n");
28                 return TRUE;
29         }
30
31         *checked = 2;
32
33         printf("data1 = [%s]\n", (char *)lines->data);
34         printf("data2 = [%s]\n", (char *)lines->next->data);
35
36         return TRUE;
37 }
38
39 static void test_noti(void)
40 {
41         TcoreAT *at;
42         TReturn ret;
43         int checked = 0;
44         char *msg = NULL;
45
46         at = tcore_at_new(NULL);
47         g_assert(at);
48
49         ret = tcore_at_add_notification(at, "+CMTI:", FALSE, on_cmti, &checked);
50         g_assert(ret == TCORE_RETURN_SUCCESS);
51
52         ret = tcore_at_add_notification(at, "+CMT:", TRUE, on_cmt, &checked);
53         g_assert(ret == TCORE_RETURN_SUCCESS);
54
55
56         /* Non type PDU message */
57         msg = (char *)"+CMTI: \"SM\", 1\r\n";
58         ret = tcore_at_process(at, strlen(msg), msg);
59         g_assert(ret == TCORE_RETURN_SUCCESS);
60
61         /* callback check */
62         g_assert(checked == 1);
63
64
65         /* Completed PDU message */
66         msg = (char *)"+CMT: 19\r\n038121F3100481214301112170137192410404F17B590E\r\n";
67         ret = tcore_at_process(at, strlen(msg), msg);
68         g_assert(ret == TCORE_RETURN_SUCCESS);
69
70         /* callback check */
71         g_assert(checked == 2);
72
73
74         /* Split PDU message */
75         checked = 0;
76         msg = (char *)"+CMT: 19\r\n";
77         ret = tcore_at_process(at, strlen(msg), msg);
78         g_assert(ret == TCORE_RETURN_SUCCESS);
79
80         /* callback check */
81         g_assert(checked == 0);
82
83         msg = (char *)"038121F310";
84         ret = tcore_at_process(at, strlen(msg), msg);
85         g_assert(ret == TCORE_RETURN_SUCCESS);
86
87         /* callback check */
88         g_assert(checked == 0);
89
90         msg = (char *)"0481214301112170137192410404F17B590E\r\n";
91         ret = tcore_at_process(at, strlen(msg), msg);
92         g_assert(ret == TCORE_RETURN_SUCCESS);
93
94         /* callback check */
95         g_assert(checked == 2);
96
97 }
98
99 static void test_buf_write(void)
100 {
101 #define BUF_SIZE 1024
102         TcoreAT *at;
103         TReturn ret;
104         char *msg_1 = (char *)"ATZ";
105         char msg_2[BUF_SIZE + 1];
106
107         at = tcore_at_new(NULL);
108         g_assert(at);
109
110         ret = tcore_at_buf_write(at, strlen(msg_1), msg_1);
111         g_assert(ret == TCORE_RETURN_SUCCESS);
112
113         memset(msg_2, '1', BUF_SIZE);
114         msg_2[BUF_SIZE] = '\0';
115         ret = tcore_at_buf_write(at, strlen(msg_2), msg_2);
116         g_assert(ret == TCORE_RETURN_SUCCESS);
117
118         tcore_at_free (at);
119 }
120
121 static void test_tok(void)
122 {
123         GSList *tokens;
124         char *buf;
125
126         /* test unsolicited message */
127         tokens = tcore_at_tok_new("CSQ: 31, 99");
128         g_assert(tokens);
129
130         g_assert_cmpstr((char *)g_slist_nth_data(tokens, 0), ==, "31");
131         g_assert_cmpstr(tcore_at_tok_nth(tokens, 1), ==, "99");
132         g_assert(g_slist_nth_data(tokens, 2) == NULL);
133
134         tcore_at_tok_free(tokens);
135
136         /* test sub token (list type) */
137         tokens = tcore_at_tok_new("(2, \"T-Meego\", \"TTAU\", \"23401\", 2)");
138         g_assert(tokens);
139
140         g_assert_cmpstr((char *)g_slist_nth_data(tokens, 0), ==, "2");
141         g_assert_cmpstr(tcore_at_tok_nth(tokens, 1), ==, "\"T-Meego\"");
142         g_assert_cmpstr(tcore_at_tok_nth(tokens, 2), ==, "\"TTAU\"");
143         g_assert_cmpstr(tcore_at_tok_nth(tokens, 3), ==, "\"23401\"");
144         g_assert_cmpstr(tcore_at_tok_nth(tokens, 4), ==, "2");
145
146         /* test extract */
147         buf = tcore_at_tok_extract(tcore_at_tok_nth(tokens, 2));
148         g_assert(buf);
149         g_assert_cmpstr(buf, ==, "TTAU");
150         free(buf);
151
152         tcore_at_tok_free(tokens);
153
154         /* test extract */
155         buf = tcore_at_tok_extract("(haha)");
156         g_assert(buf);
157         g_assert_cmpstr(buf, ==, "haha");
158         free(buf);
159
160         /* test extract */
161         buf = tcore_at_tok_extract("(a)");
162         g_assert(buf);
163         g_assert_cmpstr(buf, ==, "a");
164         free(buf);
165
166         /* test extract */
167         buf = tcore_at_tok_extract("()");
168         g_assert(buf);
169         g_assert_cmpstr(buf, ==, "");
170         free(buf);
171
172         /* test extract */
173         buf = tcore_at_tok_extract("(");
174         g_assert(buf);
175         g_assert_cmpstr(buf, ==, "(");
176         free(buf);
177
178         /* test extract */
179         buf = tcore_at_tok_extract(")");
180         g_assert(buf);
181         g_assert_cmpstr(buf, ==, ")");
182         free(buf);
183
184         /* test extract */
185         buf = tcore_at_tok_extract("");
186         g_assert(buf);
187         g_assert_cmpstr(buf, ==, "");
188         free(buf);
189 }
190
191 static void test_pdu_resp(void)
192 {
193         TcoreAT *at;
194         TcoreATRequest *req;
195         TReturn ret;
196         char *msg;
197
198         at = tcore_at_new(NULL);
199         g_assert(at);
200
201         req = tcore_at_request_new("AT+CMGR=1", "+CMGR", TCORE_AT_PDU);
202         g_assert(req);
203
204         ret = tcore_at_set_request(at, req, TRUE);
205
206         msg = (char *)"+CMGR: 1,,105\r\n12345678901234567890\r\nOK\r\n";
207         ret = tcore_at_process(at, strlen(msg), msg);
208         g_assert(ret == TRUE);
209
210         tcore_at_free (at);
211 }
212
213 int main(int argc, char **argv)
214 {
215         g_test_init(&argc, &argv, NULL);
216
217         g_test_add_func("/at/buf_write", test_buf_write);
218         g_test_add_func("/at/tok", test_tok);
219         g_test_add_func("/at/noti", test_noti);
220         g_test_add_func("/at/pdu_resp", test_pdu_resp);
221
222         return g_test_run();
223 }