27d160f3590f83948ca50f0c2a155f6962047ec9
[platform/core/telephony/libtapi.git] / src / tapi_common.c
1 /*
2  * libslp-tapi
3  *
4  * Copyright (c) 2012 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 #define _GNU_SOURCE
22 #include <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <glib.h>
28 #include <glib-object.h>
29 #include <gio/gio.h>
30
31 #include "tapi_common.h"
32 #include "TapiUtility.h"
33
34 //
35 #include "ITapiPS.h"
36 #include "TelNetwork.h"
37 #include "TelSms.h"
38 #include "TelSat.h"
39 #include "TelSs.h"
40 #include "TelCall.h"
41
42 //
43 #include "common.h"
44 #include "tapi_log.h"
45
46 #define CALLBACK_CALL(data) \
47         if (evt_cb_data->cb_fn) { \
48                 evt_cb_data->cb_fn(handle, noti_id, (data), evt_cb_data->user_data); \
49         } \
50
51 extern char *g_cookie_name;
52 extern int g_cookie_size;
53
54 TelSatEventDownloadType_t g_event_list[TAPI_SAT_EVENT_LIST_MAX_COUNT] = {-1};
55
56 /*static void _cookie_deinit(void)
57 {
58         if (g_cookie_name)
59                 free(g_cookie_name);
60
61         g_cookie_name = NULL;
62         g_cookie_size = 0;
63 }*/
64
65 static void _process_network_event(const gchar *sig, GVariant *param,
66         TapiHandle *handle, char *noti_id, struct tapi_evt_cb *evt_cb_data)
67 {
68         if (!evt_cb_data)
69                 return;
70
71         if (!g_strcmp0(sig, "RegistrationStatus")) {
72                 struct tel_noti_network_registration_status noti;
73
74                 g_variant_get(param, "(iiib)", &noti.cs, &noti.ps, &noti.type, &noti.is_roaming);
75
76                 CALLBACK_CALL(&noti);
77         }
78         else if (!g_strcmp0(sig, "Info")) {
79                 struct tel_noti_network_info noti;
80
81                 memset(&noti, 0, sizeof(struct tel_noti_network_info));
82                 g_variant_get(param, "(ii)", &noti.rssi, &noti.battery);
83
84                 CALLBACK_CALL(&noti);
85         }
86         else if (!g_strcmp0(sig, "CellInfo")) {
87                 struct tel_noti_network_cell_info noti;
88
89                 memset(&noti, 0, sizeof(struct tel_noti_network_cell_info));
90                 g_variant_get(param, "(ii)", &noti.lac, &noti.cell_id);
91
92                 CALLBACK_CALL(&noti);
93         }
94         else if (!g_strcmp0(sig, "Change")) {
95                 struct tel_noti_network_change noti;
96                 char *plmn = NULL;
97                 int act;
98                 memset(&noti, 0, sizeof(struct tel_noti_network_change));
99
100                 g_variant_get(param, "(isi)", &act, &plmn, &noti.lac);
101
102                 switch (act) {
103                         case 0x1:
104                                 noti.act = TAPI_NETWORK_SYSTEM_GSM;
105                                 break;
106                         case 0x2:
107                                 noti.act = TAPI_NETWORK_SYSTEM_GPRS;
108                                 break;
109                         case 0x3:
110                                 noti.act = TAPI_NETWORK_SYSTEM_EGPRS;
111                                 break;
112                         case 0x4:
113                                 noti.act = TAPI_NETWORK_SYSTEM_UMTS;
114                                 break;
115                         case 0x5:
116                                 noti.act = TAPI_NETWORK_SYSTEM_GSM_AND_UMTS;
117                                 break;
118                         case 0x11:
119                                 noti.act = TAPI_NETWORK_SYSTEM_IS95A;
120                                 break;
121                         case 0x12:
122                                 noti.act = TAPI_NETWORK_SYSTEM_IS95B;
123                                 break;
124                         case 0x13:
125                                 noti.act = TAPI_NETWORK_SYSTEM_CDMA_1X;
126                                 break;
127                         case 0x14:
128                                 noti.act = TAPI_NETWORK_SYSTEM_EVDO_REV_0;
129                                 break;
130                         case 0x15:
131                                 noti.act = TAPI_NETWORK_SYSTEM_1X_EVDO_REV_0_HYBRID;
132                                 break;
133                         case 0x16:
134                                 noti.act = TAPI_NETWORK_SYSTEM_EVDO_REV_A;
135                                 break;
136                         case 0x17:
137                                 noti.act = TAPI_NETWORK_SYSTEM_1X_EVDO_REV_A_HYBRID;
138                                 break;
139                         case 0x18:
140                                 noti.act = TAPI_NETWORK_SYSTEM_EVDV;
141                                 break;
142                         case 0x21:
143                                 noti.act = TAPI_NETWORK_SYSTEM_LTE;
144                                 break;
145                         default:
146                                 noti.act = TAPI_NETWORK_SYSTEM_NO_SRV;
147                                 break;
148                 }
149
150                 if (plmn) {
151                         snprintf(noti.plmn, 7, "%s", plmn);
152                 }
153
154                 CALLBACK_CALL(&noti);
155         }
156         else if (!g_strcmp0(sig, "TimeInfo")) {
157                 struct tel_noti_network_time_info noti;
158                 char *plmn = NULL;
159
160                 memset(&noti, 0, sizeof(struct tel_noti_network_time_info));
161                 g_variant_get(param, "(iiiiiiiiibs)", &noti.year, &noti.month, &noti.day,
162                                 &noti.hour, &noti.minute, &noti.second,
163                                 &noti.wday, &noti.gmtoff, &noti.dstoff, &noti.isdst,
164                                 &plmn);
165
166                 if (plmn) {
167                         snprintf(noti.plmn, 7, "%s", plmn);
168                 }
169
170                 CALLBACK_CALL(&noti);
171         }
172         else if (!g_strcmp0(sig, "Identity")) {
173                 struct tel_noti_network_identity noti;
174                 char *plmn = NULL, *s_name = NULL, *f_name = NULL;
175
176                 memset(&noti, 0, sizeof(struct tel_noti_network_identity));
177
178                 g_variant_get(param, "(sss)", &plmn, &s_name, &f_name);
179
180                 if (plmn) {
181                         snprintf(noti.plmn, 7, "%s", plmn);
182                 }
183                 if (s_name) {
184                         snprintf(noti.short_name, 17, "%s", s_name);
185                 }
186                 if (f_name) {
187                         snprintf(noti.full_name, 33, "%s", f_name);
188                 }
189
190                 CALLBACK_CALL(&noti);
191         }
192         else {
193                 dbg("not handled Network noti[%s]", sig);
194         }
195 }
196
197 static void _process_sms_event(const gchar *sig, GVariant *param,
198         TapiHandle *handle, char *noti_id, struct tapi_evt_cb *evt_cb_data)
199 {
200         if (!evt_cb_data)
201                 return;
202
203         if (!g_strcmp0(sig, "IncommingMsg")) {
204                 struct tel_noti_sms_incomming_msg noti;
205
206                 const char *sca = NULL;
207                 const char *tpdu = NULL;
208                 gsize length;
209                 guchar *decoded_sca = NULL;
210                 guchar *decoded_tpdu = NULL;
211
212                 memset(&noti, 0, sizeof(struct tel_noti_sms_incomming_msg));
213
214                 g_variant_get(param, "(sis)", &sca, &noti.MsgLength, &tpdu);
215
216                 decoded_sca = g_base64_decode(sca, &length);
217                 if (decoded_sca) {
218                         memcpy(&(noti.Sca[0]), decoded_sca, TAPI_SMS_ENCODED_SCA_LEN_MAX);
219                         g_free(decoded_sca);
220                 }
221
222                 decoded_tpdu = g_base64_decode(tpdu, &length);
223                 if (decoded_tpdu) {
224                         memcpy(&(noti.szData[0]), decoded_tpdu, TAPI_NETTEXT_SMDATA_SIZE_MAX + 1);
225                         g_free(decoded_tpdu);
226                 }
227
228                 CALLBACK_CALL(&noti);
229         }
230         else if (!g_strcmp0(sig, "IncommingCbMsg")) {
231                 struct tel_noti_sms_incomming_cb_msg noti;
232
233                 const char *cbMsg = NULL;
234                 gsize length;
235                 guchar *decoded_cbMsg = NULL;
236
237                 memset(&noti, 0, sizeof(struct tel_noti_sms_incomming_cb_msg));
238                 g_variant_get(param, "(iis)", &noti.CbMsgType, &noti.Length, &cbMsg);
239
240                 decoded_cbMsg = g_base64_decode(cbMsg, &length);
241                 if (decoded_cbMsg) {
242                         memcpy(&(noti.szMsgData[0]), decoded_cbMsg, TAPI_NETTEXT_CB_SIZE_MAX + 1);
243                         g_free(decoded_cbMsg);
244                 }
245
246                 CALLBACK_CALL(&noti);
247         }
248         else if (!g_strcmp0(sig, "IncommingEtwsMsg")) {
249                 struct tel_noti_sms_incomming_etws_msg noti;
250
251                 const char *etwsMsg = NULL;
252                 gsize length;
253                 guchar *decoded_etwsMsg = NULL;
254
255                 memset(&noti, 0, sizeof(struct tel_noti_sms_incomming_etws_msg));
256                 g_variant_get(param, "(iis)", &noti.EtwsMsgType, &noti.Length, &etwsMsg);
257
258                 decoded_etwsMsg = g_base64_decode(etwsMsg, &length);
259                 if (decoded_etwsMsg) {
260                         memcpy(&(noti.szMsgData[0]), decoded_etwsMsg, TAPI_NETTEXT_ETWS_SIZE_MAX + 1);
261                         g_free(decoded_etwsMsg);
262                 }
263
264                 CALLBACK_CALL(&noti);
265         }
266         else if (!g_strcmp0(sig, "MemoryStatus")) {
267                 int noti = 0;
268
269                 g_variant_get(param, "(i)", &noti);
270                 CALLBACK_CALL(&noti);
271         }
272         else if (!g_strcmp0(sig, "SmsReady")) {
273                 gboolean noti = 0;
274
275                 g_variant_get(param, "(b)", &noti);
276                 CALLBACK_CALL(&noti);
277         }
278         else {
279                 dbg("not handled Sms noti[%s]", sig);
280         }
281 }
282
283 static void _process_call_event(const gchar *sig, GVariant *param,
284         TapiHandle *handle, char *noti_id, struct tapi_evt_cb *evt_cb_data)
285 {
286         if (!evt_cb_data)
287                 return;
288
289         if (!g_strcmp0(sig, "VoiceCallStatusIdle") || !g_strcmp0(sig, "VideoCallStatusIdle") ) {
290                 TelCallStatusIdleNoti_t data;
291                 int start_time = 0, end_time = 0;
292                 g_variant_get(param, "(iiii)", &data.id, &data.cause, &start_time, &end_time);
293                 dbg("[ check ] %s : call_id(%d), end_cause(0x%x)", "Status Idle noti", data.id, data.cause);
294                 CALLBACK_CALL(&data);
295         }
296         else if (!g_strcmp0(sig, "VoiceCallStatusDialing") || !g_strcmp0(sig, "VideoCallStatusDialing")) {
297                 TelCallStatusDialingNoti_t data;
298                 g_variant_get(param, "(i)", &data.id);
299                 dbg("[ check ] %s : call_id(%d)", "Status Dialing noti", data.id);
300                 CALLBACK_CALL(&data);
301         }
302         else if (!g_strcmp0(sig, "VoiceCallStatusAlert") || !g_strcmp0(sig, "VideoCallStatusAlert")) {
303                 TelCallStatusAlertNoti_t data;
304                 g_variant_get(param, "(i)", &data.id);
305                 dbg("[ check ] %s : call_id(%d)", "Status Alert noti", data.id);
306                 CALLBACK_CALL(&data);
307         }
308         else if (!g_strcmp0(sig, "VoiceCallStatusActive") || !g_strcmp0(sig, "VideoCallStatusActive")) {
309                 TelCallStatusActiveNoti_t data;
310                 g_variant_get(param, "(i)", &data.id);
311                 dbg("[ check ] %s : call_id(%d)", "Status Active noti", data.id);
312                 CALLBACK_CALL(&data);
313         }
314         else if (!g_strcmp0(sig, "VoiceCallStatusHeld") ) {
315                 TelCallStatusHeldNoti_t data;
316                 g_variant_get(param, "(i)", &data.id);
317                 dbg("[ check ] %s : call_id(%d)", "Status Held noti", data.id);
318                 CALLBACK_CALL(&data);
319         }
320         else if (!g_strcmp0(sig, "VoiceCallStatusIncoming") || !g_strcmp0(sig, "VideoCallStatusIncoming")) {
321                 TelCallStatusIncomingNoti_t data;
322                 g_variant_get(param, "(i)", &data.id);
323                 dbg("[ check ] %s : call_id(%d)", "Status Incoming noti", data.id);
324                 CALLBACK_CALL(&data);
325         }
326         else if (!g_strcmp0(sig, "Waiting")) {
327                 TelCallInfoWaitingNoti_t data;
328                 g_variant_get(param, "(i)", &data.id);
329                 dbg("[ check ] %s : data.id(%d)", "Call Info Waiting noti", data.id);
330                 CALLBACK_CALL(&data.id);
331         }
332         else if (!g_strcmp0(sig, "Forwarded")) {
333                 TelCallInfoForwardedNoti_t data;
334                 g_variant_get(param, "(i)", &data.id);
335                 dbg("[ check ] %s : data.id(%d)", "Call Info Forwarded noti", data.id);
336                 CALLBACK_CALL(&data.id);
337         }
338         else if (!g_strcmp0(sig, "BarredIncoming")) {
339                 TelCallInfoBarredIncomingNoti_t data;
340                 g_variant_get(param, "(i)", &data.id);
341                 dbg("[ check ] %s : data.id(%d)", "Call Info Barred Incoming noti", data.id);
342                 CALLBACK_CALL(&data.id);
343         }
344         else if (!g_strcmp0(sig, "BarredOutgoing")) {
345                 TelCallInfoBarredOutgoingNoti_t data;
346                 g_variant_get(param, "(i)", &data.id);
347                 dbg("[ check ] %s : data.id(%d)", "Call Info Barred Outgoing noti", data.id);
348                 CALLBACK_CALL(&data.id);
349         }
350         else if (!g_strcmp0(sig, "ForwardConditional")) {
351                 TelCallInfoForwardConditionalNoti_t data;
352                 g_variant_get(param, "(i)", &data.id);
353                 dbg("[ check ] %s : data.id(%d)", "Call Info Forward Conditional noti", data.id);
354                 CALLBACK_CALL(&data.id);
355         }
356         else if (!g_strcmp0(sig, "ForwardUnconditional")) {
357                 TelCallInfoForwardUnconditionalNoti_t data;
358                 g_variant_get(param, "(i)", &data.id);
359                 dbg("[ check ] %s : data.id(%d)", "Call Info Forward Unconditional noti", data.id);
360                 CALLBACK_CALL(&data.id);
361         }
362         else if (!g_strcmp0(sig, "CallActive")) {
363                 TelCallInfoActiveNoti_t data;
364                 g_variant_get(param, "(i)", &data.id);
365                 dbg("[ check ] %s : data.id(%d)", "Call Info Call Active noti", data.id);
366                 CALLBACK_CALL(&data.id);
367         }
368         else if (!g_strcmp0(sig, "CallHeld")) {
369                 TelCallInfoHeldNoti_t data;
370                 g_variant_get(param, "(i)", &data.id);
371                 dbg("[ check ] %s : data.id(%d)", "Call Info Call Held noti", data.id);
372                 CALLBACK_CALL(&data.id);
373         }
374         else if (!g_strcmp0(sig, "CallJoined")) {
375                 TelCallInfoJoinedNoti_t data;
376                 g_variant_get(param, "(i)", &data.id);
377                 dbg("[ check ] %s : data.id(%d)", "Call Info Call Joined noti", data.id);
378                 CALLBACK_CALL(&data.id);
379         }
380         else if (!g_strcmp0(sig, "CallSoundPath")) {
381                 TelCallSoundPathNoti_t data;
382                 g_variant_get(param, "(i)", &data.path);
383                 dbg("[ check ] %s : path(%d)", "Call Sound Path noti", data.path);
384                 CALLBACK_CALL(&data);
385         }
386         else if (!g_strcmp0(sig, "CallSoundRingbackTone")) {
387                 TelCallSoundRingbackToneNoti_t status;
388                 g_variant_get(param, "(i)", &status);
389                 dbg("[ check ] %s : status(%d)", "Call Sound Ringbacktone noti", status);
390                 CALLBACK_CALL(&status);
391         }
392         else if (!g_strcmp0(sig, "CallSoundWbamr")) {
393                 TelCallSoundWbamrNoti_t status;
394                 g_variant_get(param, "(i)", &status);
395                 dbg("[ check ] %s : status(%d)", "Call Sound Wbamr noti", status);
396                 CALLBACK_CALL(&status);
397         }
398         else if (!g_strcmp0(sig, "CallSoundEqualization")) {
399                 TelCallSoundEqualizationNoti_t data;
400                 g_variant_get(param, "(ii)", &data.mode, &data.direction);
401                 dbg("[ check ] %s : mode(%d), direction(%d)", "Call Sound Equalization noti", data.mode, data.direction);
402                 CALLBACK_CALL(&data);
403         }
404         else if (!g_strcmp0(sig, "CallSoundNoiseReduction")) {
405                 TelCallSoundNoiseReductionNoti_t data;
406                 g_variant_get(param, "(i)", &data.status);
407                 dbg("[ check ] %s : status(%d)", "Call Sound Noise Reduction noti", data.status);
408                 CALLBACK_CALL(&data);
409         }
410         else if (!g_strcmp0(sig, "CallSoundClockStatus")) {
411                 gboolean data;
412                 g_variant_get(param, "(b)", &data);
413                 dbg("[ check ] %s : status(%d)", "Call Sound Clock Status noti", data);
414                 CALLBACK_CALL(&data);
415         }
416         else {
417                 dbg("not handled Call noti[%s]", sig);
418         }
419 }
420
421 static void _process_sat_event(const gchar *sig, GVariant *param,
422         TapiHandle *handle, char *noti_id, struct tapi_evt_cb *evt_cb_data)
423 {
424         if (!evt_cb_data)
425                 return;
426
427         if (!g_strcmp0(sig, "SetupMenu")) {
428                 TelSatSetupMenuInfo_t setup_menu;
429
430                 gchar *title;
431                 gint command_id, item_cnt;
432                 gboolean b_present, b_helpinfo, b_updated;
433                 GVariant *items = NULL;
434
435                 memset(&setup_menu, 0, sizeof(TelSatSetupMenuInfo_t));
436
437                 g_variant_get(param, "(ibs@vibb)", &command_id, &b_present, &title, &items, &item_cnt,
438                                         &b_helpinfo, &b_updated);
439
440                 setup_menu.commandId = command_id;
441                 setup_menu.bIsMainMenuPresent = (b_present ? 1 : 0);
442                 memcpy(setup_menu.satMainTitle, title, TAPI_SAT_DEF_TITLE_LEN_MAX+1);
443                 setup_menu.satMainMenuNum = item_cnt;
444                 if(items && item_cnt > 0){
445                         int index = 0;
446                         GVariant *unbox;
447                         GVariantIter *iter;
448
449                         gchar *item_str;
450                         gint item_id;
451                         unbox = g_variant_get_variant(items);
452                         dbg("items(%p) items type_format(%s)", items, g_variant_get_type_string(unbox));
453
454                         g_variant_get(unbox, "a(si)", &iter);
455                         while(g_variant_iter_loop(iter,"(si)",&item_str, &item_id)){
456                                 setup_menu.satMainMenuItem[index].itemId = item_id;
457                                 memcpy(setup_menu.satMainMenuItem[index].itemString, item_str, TAPI_SAT_DEF_ITEM_STR_LEN_MAX + 6);
458                                 dbg("item index(%d) id(%d) str(%s)",index, setup_menu.satMainMenuItem[index].itemId, setup_menu.satMainMenuItem[index].itemString);
459                                 //dbg("item index(%d) id(%d) str(%s)",index, item_id, item_str);
460                                 index++;
461                         }
462                         g_variant_iter_free(iter);
463                 }
464                 setup_menu.bIsSatMainMenuHelpInfo = (b_helpinfo ? 1 : 0);
465                 setup_menu.bIsUpdatedSatMainMenu = (b_updated ? 1 : 0);
466
467                 dbg("command id (%d)", setup_menu.commandId);
468                 dbg("menu present (%d)", setup_menu.bIsMainMenuPresent);
469                 dbg("menu title (%s)", setup_menu.satMainTitle);
470                 dbg("item cnt (%d)", setup_menu.satMainMenuNum);
471                 dbg("menu help info (%d)", setup_menu.bIsSatMainMenuHelpInfo);
472                 dbg("menu updated (%d)", setup_menu.bIsUpdatedSatMainMenu);
473
474                 CALLBACK_CALL(&setup_menu);
475         }
476         else if (!g_strcmp0(sig, "DisplayText")) {
477                 TelSatDisplayTextInd_t display_text;
478
479                 gchar* text;
480                 gint command_id, text_len, duration;
481                 gboolean high_priority, user_rsp_required, immediately_rsp;
482
483                 memset(&display_text, 0, sizeof(TelSatDisplayTextInd_t));
484
485                 g_variant_get(param, "(isiibbb)", &command_id, &text, &text_len, &duration,
486                         &high_priority, &user_rsp_required, &immediately_rsp);
487
488                 display_text.commandId = command_id;
489                 memcpy(display_text.text.string, text, TAPI_SAT_DEF_TEXT_STRING_LEN_MAX+1);
490                 display_text.text.stringLen = text_len;
491                 display_text.duration = duration;
492                 display_text.bIsPriorityHigh = (high_priority ? 1 : 0);
493                 display_text.bIsUserRespRequired = (user_rsp_required ? 1 : 0);
494                 display_text.b_immediately_resp = (immediately_rsp ? 1 : 0);
495
496                 dbg("command id (%d)", display_text.commandId);
497                 dbg("display text (%s)", display_text.text.string);
498                 dbg("string len(%d)", display_text.text.stringLen);
499                 dbg("duration (%d)", display_text.duration);
500                 dbg("high priority (%d)", display_text.bIsPriorityHigh);
501                 dbg("user response required(%d)", display_text.bIsUserRespRequired);
502                 dbg("immediately response (%d)", display_text.b_immediately_resp);
503
504                 CALLBACK_CALL(&display_text);
505         }
506         else if (!g_strcmp0(sig, "SelectItem")) {
507                 TelSatSelectItemInd_t select_item;
508
509                 gboolean help_info ;
510                 gchar *selected_text;
511                 gint command_id, default_item_id, menu_cnt, text_len =0;
512                 GVariant *menu_items;
513
514                 memset(&select_item, 0, sizeof(TelSatSelectItemInd_t));
515
516                 g_variant_get(param, "(ibsiii@v)", &command_id, &help_info, &selected_text,
517                         &text_len, &default_item_id, &menu_cnt, &menu_items);
518
519                 select_item.commandId = command_id;
520                 select_item.bIsHelpInfoAvailable = (help_info ? 1 : 0);
521                 memcpy(select_item.text.string, selected_text, TAPI_SAT_DEF_TITLE_LEN_MAX+1);
522                 select_item.text.stringLen = text_len;
523                 select_item.defaultItemIndex = default_item_id;
524                 select_item.menuItemCount = menu_cnt;
525                 if(menu_items && menu_cnt > 0){
526                         int index = 0;
527                         GVariant *unbox;
528                         GVariantIter *iter;
529
530                         gchar *item_str;
531                         gint item_id, item_len;
532                         unbox = g_variant_get_variant(menu_items);
533                         dbg("items(%p) items type_format(%s)", menu_items, g_variant_get_type_string(unbox));
534
535                         g_variant_get(unbox, "a(iis)", &iter);
536                         while(g_variant_iter_loop(iter,"(iis)",&item_id, &item_len, &item_str)){
537                                 select_item.menuItem[index].itemId = item_id;
538                                 select_item.menuItem[index].textLen = item_len;
539                                 memcpy(select_item.menuItem[index].text, item_str, TAPI_SAT_ITEM_TEXT_LEN_MAX + 1);
540                                 dbg("item index(%d) id(%d) len(%d) str(%s)", index,
541                                                 select_item.menuItem[index].itemId, select_item.menuItem[index].textLen, select_item.menuItem[index].text);
542                                 index++;
543                         }
544                         g_variant_iter_free(iter);
545                 }
546
547                 dbg("command id (%d)", select_item.commandId);
548                 dbg("help info(%d)", select_item.bIsHelpInfoAvailable);
549                 dbg("selected item string(%s)", select_item.text.string);
550                 dbg("string len(%d)", select_item.text.stringLen);
551                 dbg("default item index(%d)", select_item.defaultItemIndex);
552                 dbg("item count(%d)", select_item.menuItemCount);
553
554                 CALLBACK_CALL(&select_item);
555         }
556         else if (!g_strcmp0(sig, "GetInkey")) {
557                 TelSatGetInkeyInd_t get_inkey;
558
559                 gint command_id, key_type, input_character_mode;
560                 gint text_len, duration;
561                 gboolean b_numeric, b_help_info;
562                 gchar *text;
563
564                 memset(&get_inkey, 0, sizeof(TelSatGetInkeyInd_t));
565
566                 g_variant_get(param, "(iiibbsii)", &command_id, &key_type, &input_character_mode,
567                         &b_numeric,&b_help_info, &text, &text_len, &duration);
568
569                 get_inkey.commandId = command_id;
570                 get_inkey.keyType = key_type;
571                 get_inkey.inputCharMode = input_character_mode;
572                 get_inkey.bIsNumeric = (b_numeric ? 1 : 0);
573                 get_inkey.bIsHelpInfoAvailable = (b_help_info ? 1 : 0);
574                 memcpy(get_inkey.text.string, text, TAPI_SAT_DEF_TEXT_STRING_LEN_MAX+1);
575                 get_inkey.text.stringLen = text_len;
576                 get_inkey.duration = duration;
577
578                 dbg("command id(%d)", get_inkey.commandId);
579                 dbg("key type(%d)", get_inkey.keyType);
580                 dbg("input character mode(%d)", get_inkey.inputCharMode);
581                 dbg("numeric(%d)", get_inkey.bIsNumeric);
582                 dbg("help info available(%d)", get_inkey.bIsHelpInfoAvailable);
583                 dbg("text (%s)", get_inkey.text.string);
584                 dbg("text length", get_inkey.text.stringLen);
585                 dbg("duration", get_inkey.duration);
586
587                 CALLBACK_CALL(&get_inkey);
588         }
589         else if (!g_strcmp0(sig, "GetInput")) {
590                 TelSatGetInputInd_t get_input;
591
592                 gint command_id, input_character_mode;
593                 gint text_len, def_text_len, rsp_len_min, rsp_len_max;
594                 gboolean b_numeric, b_help_info, b_echo_input;
595                 gchar *text, *def_text;
596
597                 memset(&get_input, 0, sizeof(TelSatGetInputInd_t));
598
599                 g_variant_get(param, "(iibbbsiiisi)", &command_id, &input_character_mode, &b_numeric, &b_help_info, &b_echo_input,
600                         &text, &text_len, &rsp_len_max, &rsp_len_min, &def_text, &def_text_len);
601
602                 get_input.commandId = command_id;
603                 get_input.inputCharMode = input_character_mode;
604                 get_input.bIsNumeric = (b_numeric ? 1 : 0);
605                 get_input.bIsHelpInfoAvailable = (b_help_info ? 1 : 0);
606                 get_input.bIsEchoInput = (b_echo_input ? 1 : 0);
607                 memcpy(get_input.text.string, text, TAPI_SAT_DEF_TEXT_STRING_LEN_MAX+1);
608                 get_input.text.stringLen = text_len;
609                 get_input.respLen.max = rsp_len_max;
610                 get_input.respLen.min = rsp_len_min;
611                 memcpy(get_input.defaultText.string, def_text, TAPI_SAT_DEF_TEXT_STRING_LEN_MAX+1);
612                 get_input.defaultText.stringLen = def_text_len;
613
614                 dbg("command id(%d)", get_input.commandId);
615                 dbg("input character mode(%d)", get_input.inputCharMode);
616                 dbg("numeric(%d)", get_input.bIsNumeric);
617                 dbg("help info avaiable(%d)", get_input.bIsHelpInfoAvailable);
618                 dbg("echo input(%d)", get_input.bIsEchoInput);
619                 dbg("text(%s)", get_input.text.string);
620                 dbg("text length(%d)", get_input.text.stringLen);
621                 dbg("response length max(%d)", get_input.respLen.max);
622                 dbg("response length min(%d)", get_input.respLen.min);
623                 dbg("default text(%s)", get_input.defaultText.string);
624                 dbg("default text length(%d)", get_input.defaultText.stringLen);
625
626                 CALLBACK_CALL(&get_input);
627         }
628         else if (!g_strcmp0(sig, "SendSMS")) {
629                 TelSatSendSmsIndSmsData_t send_sms;
630
631                 gint command_id, ton, npi, tpdu_type;
632                 gboolean b_packing_required;
633                 gint text_len, number_len, tpdu_data_len, tmp_data_len;
634                 gchar* text, *dialling_number;
635                 GVariant *tpdu_data;
636
637                 memset(&send_sms, 0, sizeof(TelSatSendSmsIndSmsData_t));
638
639                 g_variant_get(param, "(isibiisii@vi)", &command_id, &text, &text_len, &b_packing_required, &ton, &npi,
640                                         &dialling_number, &number_len, &tpdu_type, &tpdu_data, &tpdu_data_len);
641
642                 send_sms.commandId = command_id;
643                 send_sms.bIsPackingRequired = (b_packing_required ? 1 : 0);
644
645                 send_sms.address.ton = ton;
646                 send_sms.address.npi = npi;
647                 send_sms.address.diallingNumberLen = number_len;
648                 memcpy(send_sms.address.diallingNumber, dialling_number, TAPI_SAT_DIALLING_NUMBER_LEN_MAX);
649
650                 send_sms.smsTpdu.tpduType = tpdu_type;
651                 send_sms.smsTpdu.dataLen = tpdu_data_len;
652
653                 if(tpdu_data){
654                         int index = 0;
655                         guchar data;
656                         GVariantIter *iter = NULL;
657                         GVariant *inner_gv = NULL;
658
659                         inner_gv = g_variant_get_variant(tpdu_data);
660                         dbg("tpdu data exist type_format(%s)", g_variant_get_type_string(inner_gv));
661
662                         g_variant_get(inner_gv, "ay", &iter);
663                         while( g_variant_iter_loop (iter, "y", &data)){
664                                 dbg("index(%d) data(%c)", index, data);
665                                 send_sms.smsTpdu.data[index] = data;
666                                 index++;
667                         }
668                         g_variant_iter_free(iter);
669                         g_variant_unref(inner_gv);
670                         tmp_data_len = index - 1;
671                 }
672
673                 dbg("command id(%d)", send_sms.commandId);
674                 dbg("packing required(%d)", send_sms.bIsPackingRequired);
675                 dbg("address ton(%d)", send_sms.address.ton);
676                 dbg("address npi(%d)", send_sms.address.npi);
677                 dbg("address dialing number (%s)", send_sms.address.diallingNumber);
678                 dbg("address number length (%d)", send_sms.address.diallingNumberLen);
679                 dbg("tpdu type (%d)", send_sms.smsTpdu.tpduType);
680                 dbg("tpdu length (%d)", send_sms.smsTpdu.dataLen);
681
682                 CALLBACK_CALL(&send_sms);
683         }
684         else if (!g_strcmp0(sig, "SetupEventList")) {
685                 int g_index = 0;
686                 gint event_cnt;
687                 GVariant *evt_list;
688                 TelSatEventListData_t event_list;
689
690                 memset(&event_list, 0, sizeof(TelSatEventListData_t));
691
692                 g_variant_get(param, "(i@v)", &event_cnt, &evt_list);
693
694                 if(evt_list){
695                         guchar data;
696                         GVariantIter *iter = NULL;
697                         GVariant *inner_gv = NULL;
698
699                         inner_gv = g_variant_get_variant(evt_list);
700                         dbg("event list exist type_format(%s)", g_variant_get_type_string(inner_gv));
701
702                         g_variant_get(inner_gv, "ai", &iter);
703                         while( g_variant_iter_loop (iter, "i", &data)){
704                                 dbg("g_index(%d) event(%d)", g_index, data);
705                                 g_event_list[g_index] = data;
706
707                                 if(data == TAPI_EVENT_SAT_DW_TYPE_IDLE_SCREEN_AVAILABLE){
708                                         event_list.bIsIdleScreenAvailable = 1;
709                                 }
710                                 else if(data == TAPI_EVENT_SAT_DW_TYPE_LANGUAGE_SELECTION){
711                                         event_list.bIsLanguageSelection = 1;
712                                 }
713                                 else if(data == TAPI_EVENT_SAT_DW_TYPE_BROWSER_TERMINATION){
714                                         event_list.bIsBrowserTermination = 1;
715                                 }
716                                 else if(data == TAPI_EVENT_SAT_DW_TYPE_DATA_AVAILABLE){
717                                         event_list.bIsDataAvailable = 1;
718                                 }
719                                 else if(data == TAPI_EVENT_SAT_DW_TYPE_CHANNEL_STATUS){
720                                         event_list.bIsChannelStatus = 1;
721                                 }
722
723                                 g_index++;
724                         }
725                         //while end
726                         g_variant_iter_free(iter);
727                         g_variant_unref(inner_gv);
728                 }
729
730                 dbg("event list cnt(%d)", event_cnt);
731
732                 CALLBACK_CALL(&event_list);
733         }
734         else if (!g_strcmp0(sig, "Refresh")) {
735                 TelSatRefreshInd_t refresh_info;
736
737                 gint command_id = 0;
738                 gint refresh_type =0;
739                 gint file_cnt = 0;
740                 GVariant *file_list = NULL;
741
742                 memset(&refresh_info, 0, sizeof(TelSatRefreshInd_t));
743
744                 g_variant_get(param, "(ii@v)", &command_id, &refresh_type, &file_list);
745
746                 refresh_info.commandId = command_id;
747                 refresh_info.refreshMode = refresh_type;
748
749                 if(file_list){
750                         int g_index = 0;
751                         guchar data;
752                         GVariantIter *iter = NULL;
753                         GVariant *inner_gv = NULL;
754
755                         inner_gv = g_variant_get_variant(file_list);
756                         dbg("file list exist type_format(%s)", g_variant_get_type_string(inner_gv));
757
758                         g_variant_get(inner_gv, "ai", &iter);
759                         while( g_variant_iter_loop (iter, "i", &data)){
760                                 dbg("g_index(%d) file id(%d)", g_index, data);
761                                 refresh_info.fileId[g_index] = data;
762                                 g_index++;
763                         }
764                         file_cnt = g_index;
765
766                         //while end
767                         g_variant_iter_free(iter);
768                         g_variant_unref(inner_gv);
769                 }
770                 refresh_info.fileCount = file_cnt;
771
772                 dbg("refresh event/file cnt(%d)", refresh_info.fileCount);
773
774                 CALLBACK_CALL(&refresh_info);
775         }
776         else if (!g_strcmp0(sig, "SendDtmf")) {
777                 TelSatSendDtmfIndDtmfData_t send_dtmf;
778
779                 gint command_id = 0;
780                 gint text_len = 0, dtmf_str_len = 0;
781                 gchar *text = NULL;
782                 gchar *dtmf_str = NULL;
783
784                 memset(&send_dtmf, 0, sizeof(TelSatSendDtmfIndDtmfData_t));
785
786                 g_variant_get(param, "(isisi)", &command_id, &text, &text_len, &dtmf_str, &dtmf_str_len);
787
788                 send_dtmf.commandId = command_id;
789                 if(text_len <= 0 || g_strcmp0(text,"") == 0 ){
790                         send_dtmf.bIsHiddenMode = 1;
791                 }
792
793                 send_dtmf.dtmfString.stringLen = dtmf_str_len;
794                 memcpy(send_dtmf.dtmfString.string, dtmf_str, TAPI_SAT_DEF_TEXT_STRING_LEN_MAX+1);
795
796                 dbg("dtmf event command id(%d)", send_dtmf.commandId);
797                 dbg("dtmf event dtmf(%s)", send_dtmf.dtmfString.string);
798
799                 CALLBACK_CALL(&send_dtmf);
800         }
801         else if (!g_strcmp0(sig, "EndProactiveSession")) {
802                 int command_type = 0;
803
804                 g_variant_get(param, "(i)", &command_type);
805                 dbg("end session evt : command type(%d)", command_type);
806                 CALLBACK_CALL(&command_type);
807         }
808         else {
809                 dbg("not handled Sat noti[%s]", sig);
810         }
811 }
812
813 static void _process_sim_event(const gchar *sig, GVariant *param,
814         TapiHandle *handle, char *noti_id, struct tapi_evt_cb *evt_cb_data)
815 {
816         if (!evt_cb_data)
817                 return;
818
819         if (!g_strcmp0(sig, "Status")) {
820                 int noti = 0;
821                 g_variant_get(param, "(i)", &noti);
822                 CALLBACK_CALL(&noti);
823         } else {
824                 dbg("not handled SIM noti[%s]",sig );
825         }
826 }
827
828 static void _process_pb_event(const gchar *sig, GVariant *param,
829         TapiHandle *handle, char *noti_id, struct tapi_evt_cb *evt_cb_data)
830 {
831         if (!evt_cb_data)
832                 return;
833
834         if (!g_strcmp0(sig, "Status")) {
835                 int noti = 0;
836                 g_variant_get(param, "(i)", &noti);
837                 CALLBACK_CALL(&noti);
838         } else {
839                 dbg("not handled Phonebook noti[%s]",sig );
840         }
841 }
842
843 static void _process_sap_event(const gchar *sig, GVariant *param,
844         TapiHandle *handle, char *noti_id, struct tapi_evt_cb *evt_cb_data)
845 {
846         if (!evt_cb_data)
847                 return;
848
849         if (!g_strcmp0(sig, "Status")) {
850                 int noti = 0;
851                 g_variant_get(param, "(i)", &noti);
852                 CALLBACK_CALL(&noti);
853         } else if (!g_strcmp0(sig, "Disconnect")) {
854                 int disconnect = 0;
855                 g_variant_get(param, "(i)", &disconnect);
856                 CALLBACK_CALL(&disconnect);
857         } else {
858                 dbg("not handled SAP noti[%s]",sig );
859         }
860 }
861
862 static void _process_modem_event(const gchar *sig, GVariant *param,
863         TapiHandle *handle, char *noti_id, struct tapi_evt_cb *evt_cb_data)
864 {
865         if (!evt_cb_data)
866                 return;
867
868         if (!g_strcmp0(sig, "Power")) {
869                 int noti = 0;
870
871                 g_variant_get(param, "(i)", &noti);
872
873                 CALLBACK_CALL(&noti);
874         }
875         else if (!g_strcmp0(sig, "DunPinCtrl")) {
876                 tapi_ps_btdun_pincontrol pin_ctrl;
877                 int signal = 0, status = 0;
878
879                 memset(&pin_ctrl, 0, sizeof(tapi_ps_btdun_pincontrol));
880                 g_variant_get(param, "(ib)", &signal, &status);
881                 pin_ctrl.signal = signal;
882                 pin_ctrl.status = status;
883
884                 CALLBACK_CALL(&pin_ctrl);
885         }
886         else if (!g_strcmp0(sig, "DunExternalCall")) {
887                 gboolean external_call;
888
889                 g_variant_get(param, "(b)", &external_call);
890
891                 CALLBACK_CALL(&external_call);
892         }
893         else {
894                 dbg("not handled Modem noti[%s]", sig);
895         }
896
897         return;
898 }
899
900 static void _process_ss_event(const gchar *sig, GVariant *param,
901         TapiHandle *handle, char *noti_id, struct tapi_evt_cb *evt_cb_data)
902 {
903         if (!evt_cb_data)
904                 return;
905
906         if (!g_strcmp0(sig, "NotifyUSSD")) {
907                 TelSsUssdMsgInfo_t noti;
908                 char *str = NULL;
909                 memset(&noti, 0, sizeof(TelSsUssdMsgInfo_t));
910
911                 g_variant_get(param, "(iis)", &noti.Type, &noti.Length, &str);
912
913                 if (str) {
914                         g_strlcpy(noti.szString, str, TAPI_SS_USSD_DATA_SIZE_MAX);
915                         g_free(str);
916                 }
917
918                 CALLBACK_CALL(&noti);
919
920         } else if (!g_strcmp0(sig, "ReleaseComplete")) {
921                 TelSsRelCompMsgInfo_t noti;
922                 GVariant *msg = 0;
923                 int len = 0;
924
925                 memset ( &noti, 0, sizeof( TelSsRelCompMsgInfo_t ));
926
927                 g_variant_get(param, "(i@v)", &len, &msg);
928
929                 noti.RelCompMsgLen = (unsigned char)len;
930
931                 if(msg){
932                         int i = 0;
933                         guchar data;
934                         GVariantIter *iter = NULL;
935                         GVariant *inner_gv = NULL;
936
937                         inner_gv = g_variant_get_variant(msg);
938                         dbg("[ check ] data exist type_format(%s)", g_variant_get_type_string(inner_gv));
939
940                         g_variant_get(inner_gv, "ay", &iter);
941                         while( g_variant_iter_loop (iter, "y", &data)){
942                                 dbg("index(%d) data(%c)", index, data);
943                                 noti.szRelCompMsg[i] = data;
944                                 i++;
945                         }
946                         g_variant_iter_free(iter);
947                         g_variant_unref(inner_gv);
948                 }
949
950                 CALLBACK_CALL(&noti);
951
952         } else {
953                 dbg("not handled SS noti[%s]", sig);
954         }
955
956         return;
957 }
958
959 static void _process_gps_event(const gchar *sig, GVariant *param,
960                 TapiHandle *handle, char *noti_id, struct tapi_evt_cb *evt_cb_data)
961 {
962         gboolean b_decode_data = FALSE;
963         guchar *decoded_data = NULL;
964
965         if (!evt_cb_data)
966                 return;
967
968         if (!g_strcmp0(sig, "AssistData")) {
969                 b_decode_data = TRUE;
970         }
971         else if (!g_strcmp0(sig, "MeasurePosition")) {
972                 b_decode_data = TRUE;
973         }
974         else if (!g_strcmp0(sig, "ResetAssistData")) {
975                 /* noting to decode */
976         }
977         else if (!g_strcmp0(sig, "FrequencyAiding")) {
978                 b_decode_data = TRUE;
979         }
980         else {
981                 dbg("not handled Gps noti[%s]", sig);
982         }
983
984         /* decoding data */
985         if(b_decode_data) {
986                 gchar *data = NULL;
987                 gsize length;
988                 dbg("[%s] decoding start", sig);
989
990                 g_variant_get(param, "(s)", &data);
991                 decoded_data = g_base64_decode((const gchar *)data, &length);
992                 if (decoded_data) {
993                         CALLBACK_CALL(decoded_data);
994                 }
995
996                 dbg("length=%d", length);
997         }
998
999         if (decoded_data)
1000                 g_free(decoded_data);
1001 }
1002
1003 static void on_signal_callback(GDBusConnection *conn, const gchar *name, const gchar *path, const gchar *interface,
1004                 const gchar *sig, GVariant *param, gpointer user_data)
1005 {
1006         TapiHandle *handle = user_data;
1007         struct tapi_evt_cb *evt_cb_data = NULL;
1008         char *noti_id;
1009
1010         if (!handle) {
1011                 dbg("handle is NULL");
1012                 return;
1013         }
1014
1015         noti_id = g_strdup_printf("%s:%s", interface, sig);
1016
1017         evt_cb_data = g_hash_table_lookup(handle->evt_list, noti_id);
1018         if (!evt_cb_data) {
1019                 dbg("can't find noti_id(%s) callback info", noti_id);
1020                 g_free(noti_id);
1021                 return;
1022         }
1023
1024         if (!g_strcmp0(interface, DBUS_TELEPHONY_NETWORK_INTERFACE)) {
1025                 _process_network_event(sig, param, handle, noti_id, evt_cb_data);
1026         }
1027         else if (!g_strcmp0(interface, DBUS_TELEPHONY_SMS_INTERFACE)) {
1028                 _process_sms_event(sig, param, handle, noti_id, evt_cb_data);
1029         }
1030         else if (!g_strcmp0(interface, DBUS_TELEPHONY_CALL_INTERFACE)) {
1031                 _process_call_event(sig, param, handle, noti_id, evt_cb_data);
1032         }
1033         else if (!g_strcmp0(interface, DBUS_TELEPHONY_SAT_INTERFACE)) {
1034                 _process_sat_event(sig, param, handle, noti_id, evt_cb_data);
1035         }
1036         else if (!g_strcmp0(interface, DBUS_TELEPHONY_SIM_INTERFACE)) {
1037                 _process_sim_event(sig, param, handle, noti_id, evt_cb_data);
1038         }
1039         else if (!g_strcmp0(interface, DBUS_TELEPHONY_PB_INTERFACE)) {
1040                 _process_pb_event(sig, param, handle, noti_id, evt_cb_data);
1041         }
1042         else if (!g_strcmp0(interface, DBUS_TELEPHONY_SAP_INTERFACE)) {
1043                 _process_sap_event(sig, param, handle, noti_id, evt_cb_data);
1044         }
1045         else if (!g_strcmp0(interface, DBUS_TELEPHONY_MODEM_INTERFACE)) {
1046                 _process_modem_event(sig, param, handle, noti_id, evt_cb_data);
1047         }
1048         else if (!g_strcmp0(interface, DBUS_TELEPHONY_SS_INTERFACE)) {
1049                 _process_ss_event(sig, param, handle, noti_id, evt_cb_data);
1050         }
1051         else if (!g_strcmp0(interface, DBUS_TELEPHONY_GPS_INTERFACE)) {
1052                 _process_gps_event(sig, param, handle, noti_id, evt_cb_data);
1053         }
1054         else {
1055                 dbg("can't find interface(%s)", interface);
1056         }
1057         g_free(noti_id);
1058 }
1059
1060 EXPORT_API char** tel_get_cp_name_list(void)
1061 {
1062         GError *error = NULL;
1063         GDBusProxy *proxy = NULL;
1064
1065         GVariantIter *iter = NULL;
1066         GVariant *rst = NULL;
1067
1068         gchar *modem_path = NULL;
1069         GSList *list = NULL;
1070         GSList *l = NULL;
1071
1072         int i = 0, element_cnt = 0;
1073         gchar **cp_list = NULL;
1074
1075         proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
1076                                 "org.tizen.telephony", "/org/tizen/telephony", "org.tizen.telephony.Manager",
1077                                 NULL, &error);
1078         if (!proxy) {
1079                 dbg("g_dbus_proxy_new_for_bus_sycn() failed. (%s)", error->message);
1080                 g_error_free (error);
1081                 return NULL;
1082         }
1083
1084         rst = g_dbus_proxy_call_sync (proxy, "GetModems", NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1085         if (!rst) {
1086                 dbg( "GetModems() failed. (%s)", error->message);
1087                 g_error_free (error);
1088                 goto OUT;
1089         }
1090
1091         g_variant_get (rst, "(as)", &iter);
1092         while (g_variant_iter_next(iter, "s", &modem_path))
1093                 list = g_slist_append(list, modem_path);
1094
1095         g_variant_iter_free(iter);
1096         g_variant_unref(rst);
1097
1098         if (!list) {
1099                 dbg( "No CP name");
1100                 goto OUT;
1101         }
1102
1103         element_cnt = g_slist_length(list);
1104         cp_list = g_new0(char*, element_cnt + 1);
1105         if (!cp_list) {
1106                 goto OUT;
1107         }
1108
1109         for (l = list; l; l = l->next, i++) {
1110                 cp_list[i] = g_strdup(l->data);
1111                 dbg("cp name[%d] = %s", i, cp_list[i]);
1112         }
1113         cp_list[element_cnt] = NULL;
1114
1115 OUT:
1116         g_slist_free_full(list, g_free);
1117         g_object_unref(proxy);
1118
1119         return cp_list;
1120 }
1121
1122 EXPORT_API int tel_get_property_int(TapiHandle *handle, const char *property, int *result)
1123 {
1124         char **dbus_info;
1125         GDBusProxy *proxy = NULL;
1126         GVariant *value = NULL;
1127         GError *error = NULL;
1128         int ret = TAPI_API_SUCCESS;
1129
1130         if (!handle || !property)
1131                 return TAPI_API_INVALID_INPUT;
1132
1133         dbus_info = g_strsplit(property, ":", 2);
1134         if (!dbus_info) {
1135                 dbg("invalid property");
1136                 return TAPI_API_INVALID_INPUT;
1137         }
1138
1139         if (!dbus_info[0] || !dbus_info[1]) {
1140                 dbg("invalid property");
1141                 ret = TAPI_API_INVALID_INPUT;
1142                 goto OUT;
1143         }
1144
1145         proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, NULL,
1146                                 DBUS_TELEPHONY_SERVICE, handle->path, dbus_info[0],
1147                                 NULL, &error);
1148         if (!proxy) {
1149                 dbg("g_dbus_proxy_new_for_bus_sycn() failed. (%s)", error->message);
1150                 g_error_free (error);
1151                 ret = TAPI_API_OPERATION_FAILED;
1152                 goto OUT;
1153         }
1154
1155         value = g_dbus_proxy_get_cached_property(proxy, dbus_info[1]);
1156         if (!value) {
1157                 dbg("g_dbus_proxy_get_cached_property('power') failed.");
1158                 ret = TAPI_API_OPERATION_FAILED;
1159                 goto OUT;
1160         }
1161
1162         g_variant_get(value, "i", result);
1163         g_variant_unref(value);
1164
1165 OUT:
1166         if (proxy)
1167                 g_object_unref(proxy);
1168
1169         g_strfreev(dbus_info);
1170
1171         return ret;
1172 }
1173
1174 EXPORT_API TapiHandle* tel_init(const char *cp_name)
1175 {
1176         GError *error = NULL;
1177         struct tapi_handle *handle = NULL;
1178         gchar *addr;
1179 #if !GLIB_CHECK_VERSION (2, 35, 3)
1180         g_type_init();
1181 #endif
1182
1183         dbg("process info: env: [%s] invocation: [%s]",
1184                                                 getenv("_"), program_invocation_name);
1185
1186         /* Get System BUS */
1187         addr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM,
1188                                                                                 NULL,
1189                                                                                 &error);
1190         if (addr == NULL) {
1191                 err("Error get dbus: [%s]", error->message);
1192                 goto ERROR;
1193         }
1194
1195         /* Allocate new Handle */
1196         handle = g_try_new0(struct tapi_handle, 1);
1197         if (handle == NULL) {
1198                 err("Failed to allocate handle");
1199                 goto ERROR;
1200         }
1201
1202         /* Create DBUS connection */
1203         handle->dbus_connection = g_dbus_connection_new_for_address_sync(addr,
1204                                                         (G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
1205                                                         G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION),
1206                                                         NULL,
1207                                                         NULL,
1208                                                         &error);
1209         if (handle->dbus_connection == NULL) {
1210                 err("Error creating DBUS connection: [%s]", error->message);
1211                 goto ERROR;
1212         }
1213
1214         handle->ca = g_cancellable_new();
1215         if (handle->ca == NULL) {
1216                 err("Error allocating cancellable object");
1217                 goto ERROR;
1218         }
1219
1220         /* Get CP name */
1221         if (cp_name != NULL)
1222                 handle->cp_name = g_strdup(cp_name);
1223         else {
1224                 char **list = NULL;
1225                 int i = 0;
1226
1227                 /* Get CP Name list */
1228                 list = tel_get_cp_name_list();
1229                 if ((list == NULL) || (list[0] == NULL)) {
1230                         err("Get CP name list failed");
1231
1232                         g_free(list);
1233                         goto ERROR;
1234                 }
1235
1236                 /*
1237                  * Presently, we would only be providing the 'default' CP name,
1238                  * it is the first CP name in the obtained list of CP names.
1239                  *
1240                  * We would evolve this logic in due course.
1241                  */
1242                 dbg("Use 'default' Plug-in: [%s]", list[0]);
1243                 handle->cp_name = g_strdup(list[0]);
1244
1245                 /* Free CP name list */
1246                 while (list[i] != NULL)
1247                         g_free(list[i++]);
1248
1249                 g_free(list);
1250         }
1251
1252         /* Create Event list Hash Table */
1253         handle->evt_list = g_hash_table_new_full(g_str_hash,
1254                                                                                 g_str_equal,
1255                                                                                 g_free,
1256                                                                                 g_free);
1257
1258         /* Set Path */
1259         handle->path = g_strdup_printf("%s/%s",
1260                                         DBUS_TELEPHONY_DEFAULT_PATH, handle->cp_name);
1261         dbg("handle: [0x%x] Path: [%s]", handle, handle->path);
1262
1263         g_free(addr);
1264         return handle;
1265
1266 ERROR:
1267         /* Free all resources */
1268         g_free(addr);
1269         g_error_free (error);
1270
1271         if (handle != NULL) {
1272                 g_cancellable_cancel(handle->ca);
1273                 g_object_unref(handle->ca);
1274
1275                 if (g_dbus_connection_close_sync(handle->dbus_connection,
1276                                                                         NULL,
1277                                                                         NULL) == FALSE) {
1278                         err("Failed to close connection");
1279                 }
1280                 g_object_unref(handle->dbus_connection);
1281                 g_free(handle);
1282         }
1283
1284         return NULL;
1285 }
1286
1287 static gboolean _unregister_noti(gpointer key, gpointer value, gpointer user_data)
1288 {
1289         struct tapi_evt_cb *evt_cb_data = value;
1290         TapiHandle *handle = user_data;
1291
1292         if (handle == NULL)
1293                 return TRUE;
1294
1295         g_dbus_connection_signal_unsubscribe(handle->dbus_connection, evt_cb_data->evt_id);
1296
1297         return TRUE;
1298 }
1299
1300 EXPORT_API int tel_deinit(TapiHandle *handle)
1301 {
1302         if (handle == NULL)
1303                 return TAPI_API_INVALID_INPUT;
1304
1305         g_free(handle->cp_name);
1306         g_free(handle->path);
1307
1308         dbg("Remove all signals");
1309         g_hash_table_foreach_remove(handle->evt_list, _unregister_noti, handle);
1310         g_hash_table_destroy(handle->evt_list);
1311
1312         g_cancellable_cancel(handle->ca);
1313         g_object_unref(handle->ca);
1314
1315         dbg("Close DBUS connection");
1316         if (g_dbus_connection_close_sync(handle->dbus_connection,
1317                                                                         NULL, NULL) == FALSE) {
1318                 err("Failed to close DBUS connection");
1319         }
1320         g_object_unref(handle->dbus_connection);
1321         g_free(handle);
1322
1323         dbg("deinit complete");
1324         return TAPI_API_SUCCESS;
1325 }
1326
1327 EXPORT_API int tel_register_noti_event(TapiHandle *handle, const char *noti_id,
1328                                                                 tapi_notification_cb callback, void *user_data)
1329 {
1330         gchar **dbus_str = NULL;
1331         gpointer tmp = NULL;
1332         struct tapi_evt_cb *evt_cb_data = NULL;
1333
1334         if ((handle == NULL) || (handle->dbus_connection == NULL)
1335                         || (callback == NULL) || (noti_id == NULL)) {
1336                 err("Invalid input parameters");
1337                 return TAPI_API_INVALID_INPUT;
1338         }
1339
1340         tmp = g_hash_table_lookup(handle->evt_list, noti_id);
1341         if (tmp != NULL) {
1342                 dbg("noti_id(%s) is already registered", noti_id);
1343                 return TAPI_API_INVALID_INPUT;
1344         }
1345
1346         dbus_str = g_strsplit(noti_id, ":", 2);
1347         if (!dbus_str) {
1348                 dbg("invalid noti_id");
1349                 return TAPI_API_INVALID_INPUT;
1350         }
1351
1352         if (!dbus_str[0] || !dbus_str[1]) {
1353                 g_strfreev(dbus_str);
1354                 dbg("invalid noti_id");
1355                 return TAPI_API_INVALID_INPUT;
1356         }
1357
1358         evt_cb_data = g_try_new0(struct tapi_evt_cb, 1);
1359         if (evt_cb_data == NULL) {
1360                 err("Failed to allocate memory");
1361                 g_strfreev(dbus_str);
1362                 return TAPI_API_SYSTEM_OUT_OF_MEM;
1363         }
1364
1365         /* Assign callback and user_data */
1366         evt_cb_data->cb_fn = callback;
1367         evt_cb_data->user_data = user_data;
1368         evt_cb_data->handle = handle;
1369
1370         dbg("path(%s) interface (%s) signal (%s)", handle->path, dbus_str[0], dbus_str[1]);
1371
1372         evt_cb_data->evt_id = g_dbus_connection_signal_subscribe(handle->dbus_connection,
1373                         DBUS_TELEPHONY_SERVICE, /* Sender */
1374                         dbus_str[0], /* Interface */
1375                         dbus_str[1], /* Member */
1376                         handle->path, /* Object path */
1377                         NULL, /* arg0 */
1378                         G_DBUS_SIGNAL_FLAGS_NONE,
1379                         on_signal_callback, handle,
1380                         NULL);
1381
1382         g_hash_table_insert(handle->evt_list, g_strdup(noti_id), evt_cb_data);
1383
1384         g_strfreev(dbus_str);
1385
1386         return TAPI_API_SUCCESS;
1387 }
1388
1389 EXPORT_API int tel_deregister_noti_event(TapiHandle *handle, const char *noti_id)
1390 {
1391         struct tapi_evt_cb *evt_cb_data = NULL;
1392         gboolean rv = FALSE;
1393
1394         if (!handle || !handle->dbus_connection){
1395                 dbg("invalid parameter");
1396                 return TAPI_API_INVALID_INPUT;
1397         }
1398
1399         evt_cb_data = g_hash_table_lookup(handle->evt_list, noti_id);
1400
1401         if(!evt_cb_data){
1402                 dbg("event does not registered");
1403                 return TAPI_API_INVALID_INPUT;
1404         }
1405
1406         g_dbus_connection_signal_unsubscribe(handle->dbus_connection, evt_cb_data->evt_id);
1407
1408         rv = g_hash_table_remove(handle->evt_list, noti_id);
1409         if(!rv){
1410                 dbg("fail to deregister noti event(%s)", noti_id);
1411                 return TAPI_API_OPERATION_FAILED;
1412         }
1413
1414         return TAPI_API_SUCCESS;
1415 }
1416 /*      EOF     */