Initialize Tizen 2.3
[framework/telephony/libslp-tapi.git] / test_src / main.c
1 /*
2  * libslp-tapi
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Ja-young Gu <jygu@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include <glib.h>
27 #include <gio/gio.h>
28
29 #include <tapi_common.h>
30 #include <TapiUtility.h>
31
32 #include "menu.h"
33 #include "network.h"
34 #include "sat.h"
35 #include "sms.h"
36 #include "sim.h"
37 #include "phonebook.h"
38 #include "modem.h"
39 #include "call.h"
40 #include "ss.h"
41
42 #define SIM_SEL_MENU_KEY_COUNT 2
43
44 extern struct menu_data menu_modem[];
45 extern struct menu_data menu_sat[];
46 extern struct menu_data menu_net[];
47 extern struct menu_data menu_sms[];
48 extern struct menu_data menu_sim[];
49 extern struct menu_data menu_phonebook[];
50 extern struct menu_data menu_ss[];
51 extern struct menu_data menu_call[];
52
53 TapiHandle *handle = NULL;
54 char **cp_list = NULL;
55 int cp_count = 0;
56
57 static char data_subscription_type[MENU_DATA_SIZE + 1] = "";
58 static char data_property_int[MENU_DATA_SIZE + 1] = "org.tizen.telephony.Modem:power";
59 static char data_property_string[MENU_DATA_SIZE + 1] = "org.tizen.telephony.Network:network_name";
60 static char data_convert_string[MENU_DATA_SIZE + 1] = "";
61 static char data_convert_dcs[MENU_DATA_SIZE + 1] = "1";
62
63 static int get_modem_info(MManager *mm, struct menu_data *menu)
64 {
65         int i = 0;
66
67         while (cp_list[i]) {
68                 msg("[%02d] strlen(%d), '%s'", i, strlen(cp_list[i]), cp_list[i]);
69                 i++;
70         }
71
72         return 0;
73 }
74
75 static int init(MManager *mm, struct menu_data *menu)
76 {
77         int subs_type = atoi(data_subscription_type);
78
79         if (handle) {
80                 msg("already handle exist");
81                 return 0;
82         }
83         if (cp_list[subs_type-1]) {
84                 msg("call tel_init(%s)", cp_list[subs_type-1]);
85                 handle = tel_init(cp_list[subs_type-1]);
86         }
87         if (!handle)
88                 msg("handle is null");
89
90         menu_manager_set_user_data(mm, handle);
91
92         return 0;
93 }
94
95 static int deinit(MManager *mm, struct menu_data *menu)
96 {
97         if (!handle) {
98                 msg("handle is null");
99                 return 0;
100         }
101
102         msg("call tel_deinit()");
103         tel_deinit(handle);
104         handle = NULL;
105
106         menu_manager_set_user_data(mm, handle);
107
108         return 0;
109 }
110
111 static int get_property_int(MManager *mm, struct menu_data *menu)
112 {
113         TapiHandle *local_handle = menu_manager_ref_user_data(mm);
114         int result_int = -1;
115         int ret;
116
117         msg("call tel_get_property_int()");
118
119         ret = tel_get_property_int(local_handle, data_property_int, &result_int);
120         if (ret != TAPI_API_SUCCESS) {
121                 msg("failed. (return = %d)", ret);
122         }
123
124         msg(" - result = %d", result_int);
125
126         return 0;
127 }
128
129 static int get_property_string(MManager *mm, struct menu_data *menu)
130 {
131         TapiHandle *local_handle = menu_manager_ref_user_data(mm);
132         char *result_str = NULL;
133         int ret;
134
135         msg("call tel_get_property_string()");
136
137         ret = tel_get_property_string (local_handle, data_property_string, &result_str);
138         if (ret != TAPI_API_SUCCESS) {
139                 msg("failed. (return = %d)", ret);
140         }
141
142         msg(" - result = %s", result_str);
143
144         if (result_str)
145                 free (result_str);
146
147         return 0;
148 }
149
150 static int convert_string(MManager *mm, struct menu_data *menu)
151 {
152         TapiHandle *local_handle = menu_manager_ref_user_data(mm);
153         char *result_str = NULL;
154         int ret;
155
156         msg("call tel_get_property_string()");
157
158         ret = tel_get_property_string (local_handle, data_property_string, &result_str);
159         if (ret != TAPI_API_SUCCESS) {
160                 msg("failed. (return = %d)", ret);
161         }
162
163         msg(" - result = %s", result_str);
164
165         if (result_str)
166                 free (result_str);
167
168         return 0;
169 }
170
171 static struct menu_data menu_common[] = {
172         { "1", "tel_get_modem_info", NULL, get_modem_info, NULL},
173         { "2", "tel_init", NULL, init, NULL},
174         { "3", "tel_deinit", NULL,      deinit, NULL},
175         { "4", "tel_get_property_int",  NULL, get_property_int, NULL},
176         { "4s", " - property", NULL, NULL, data_property_int},
177         { "5", "tel_get_property_string", NULL, get_property_string, NULL},
178         { "5s", " - property", NULL, NULL, data_property_string},
179         { "6", "tcore_util_convert_string_to_utf8", NULL, convert_string, NULL},
180         { "6d", " - dcs", NULL, NULL, data_convert_dcs},
181         { "6s", " - string", NULL, NULL, data_convert_string},
182         { NULL, NULL, },
183 };
184
185 static struct menu_data menu_main[] = {
186         { "1", "Common",        menu_common,NULL,               NULL},
187         { "2", "Modem",         menu_modem,     NULL,           NULL},
188         { "3", "Network",       menu_net,       NULL,           NULL},
189         { "4", "SIM",           menu_sim,       NULL,           NULL},
190         { "5", "Phonebook",     menu_phonebook, NULL,   NULL},
191         { "6", "SMS",           menu_sms,       NULL,           NULL},
192         { "7", "SS",            menu_ss,        NULL,           NULL},
193         { "8", "SAT",           menu_sat,       NULL,           NULL},
194         { "9", "Call",          menu_call,      NULL,           NULL},
195         { NULL, NULL, },
196 };
197
198 static int __select_handle_register_event (MManager *mm, struct menu_data *menu)
199 {
200         int subs_type = atoi(data_subscription_type);
201
202         if (subs_type < 1 || subs_type > cp_count) {
203                 msg("Please select an existing CP Name");
204                 return RET_FAILURE;
205         }
206
207         if (handle) {
208                 tel_deinit(handle);
209                 handle = NULL;
210         }
211
212         handle = tel_init(cp_list[subs_type-1]);
213
214         msg("");
215
216         if (!handle) {
217                 msg("[SIM%d] tel_init(%s) - FAILED!!!.", subs_type, cp_list[subs_type-1]);
218                 return RET_FAILURE;
219         } else {
220                 msg("[SIM%d] tel_init(%s) - SUCCESS!!!.", subs_type, cp_list[subs_type-1]);
221         }
222
223         menu_manager_set_user_data(mm, handle);
224
225         register_network_event(handle);
226         register_modem_event(handle);
227         register_sat_event(handle);
228         register_sim_event(handle);
229         register_phonebook_event(handle);
230         register_sms_event(handle);
231         register_call_event(handle);
232         register_ss_event(handle);
233
234         return RET_SUCCESS;
235 }
236
237 static gboolean __create_sim_selection_menu(struct menu_data sim_selection_menu[SIM_SEL_MENU_KEY_COUNT+1])
238 {
239         unsigned int total_cp_len = 0;
240         unsigned int title_len = 0;
241         unsigned int index_count = 0;
242         unsigned int i = 0;
243
244         cp_list = tel_get_cp_name_list();
245         if (!cp_list) {
246                 msg("cp_list is null");
247                 return FALSE;
248         }
249
250         while (cp_list[i]) {
251                 total_cp_len += strlen(cp_list[i]);
252                 cp_count++;
253                 i++;
254         }
255
256         /**
257          * Available CPs are displayed as below (in UI) -
258          *     Modems(1=Modem0,2=Modem1,3=Modem2)
259          *
260          * Title length is calculated as,
261          *     title_len = total_cp_len + (2*cp_count) + (cp_count-1) + 8 + 1;
262          * where,
263          *     total_cp_len = strlen(Modem0)+strlen(Modem1)+ strlen(Modem2)
264          *     (2*cp_count) = To print "1="  "2="  "3="
265          *     cp_count-1   = Number of commas
266          *     8 = Modems()
267          *     1 = NULL termination
268          */
269         title_len = total_cp_len + (2*cp_count) + (cp_count-1) + 8 + 1;
270         sim_selection_menu[0].title = (char *)g_try_malloc0(title_len);
271         index_count += sprintf((char *)&sim_selection_menu[0].title[index_count], "Modems(");
272
273         i=0;
274         while (cp_list[i]) {
275                 if (i != 0)
276 //                      sim_selection_menu[0].title[index_count++] = ',';
277                         strncat((char *)&sim_selection_menu[0].title[index_count++], ",", sizeof(char));
278
279
280                 index_count += sprintf((char *)&sim_selection_menu[0].title[index_count], "%d=", (i+1));
281                 index_count += sprintf((char *)&sim_selection_menu[0].title[index_count], "%s", cp_list[i]);
282                 i++;
283         }
284
285         if (index_count <= title_len - 2) {
286                 strncat((char *)&sim_selection_menu[0].title[index_count++], ")", sizeof(char));
287         } else {
288                 msg("Invalid count");
289                 return FALSE;
290         }
291
292         sim_selection_menu[0].key = "1";
293         sim_selection_menu[0].sub_menu = NULL;
294         sim_selection_menu[0].callback = NULL;
295         sim_selection_menu[0].data = data_subscription_type;
296
297         sim_selection_menu[1].key = "2";
298         sim_selection_menu[1].title = "run";
299         sim_selection_menu[1].sub_menu = menu_main;
300         sim_selection_menu[1].callback = __select_handle_register_event;
301         sim_selection_menu[1].data = NULL;
302
303         return TRUE;
304 }
305
306 int main(int arg, char **argv)
307 {
308         GMainLoop *mainloop;
309         GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
310         MManager *manager;
311         struct menu_data sim_selection_menu[SIM_SEL_MENU_KEY_COUNT+1] = { {NULL, NULL, }, };
312
313         g_type_init();
314         mainloop = g_main_loop_new(NULL, FALSE);
315
316         msg("");
317         msg("* Telephony Test application ");
318         msg("* Build On: %s  %s", __DATE__, __TIME__);
319
320         if (__create_sim_selection_menu(sim_selection_menu) == FALSE)
321                 goto OUT;
322
323         manager = menu_manager_new(sim_selection_menu, mainloop);
324         menu_manager_run(manager);
325
326         g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
327                                 on_menu_manager_keyboard, manager);
328         g_main_loop_run(mainloop);
329
330 OUT:
331         g_strfreev(cp_list);
332         g_free((gpointer *)sim_selection_menu[0].title);
333         g_main_loop_unref(mainloop);
334         msg("******* Bye bye *******");
335
336         return 0;
337 }
338