Exclude enhance audio feature
[platform/core/connectivity/bluetooth-agent.git] / pb-agent / bluetooth_pb_agent.c
1 /*
2  * Bluetooth-agent
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *               Jaekyun Lee <jkyun.leek@samsung.com>
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *              http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <signal.h>
30 #include <glib.h>
31 #include <gio/gio.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <contacts.h>
36 #include <TapiUtility.h>
37 #include <ITapiSim.h>
38
39 #include "bluetooth_pb_agent.h"
40 #include "bluetooth_pb_vcard.h"
41
42 #define BLUETOOTH_PB_AGENT_TIMEOUT 600
43
44 typedef struct {
45         TapiHandle *tapi_handle;
46         gchar *tel_number;
47         guint timeout_id;
48         PhoneBookType pb_type;
49         guint pbagent_interface_id;
50         guint pbagent_at_interface_id;
51 } PbAgentData;
52
53 static gchar *bluetooth_pb_agent_folder_list[] = {
54         "/telecom/pb",
55         "/telecom/ich",
56         "/telecom/och",
57         "/telecom/mch",
58         "/telecom/cch",
59 #ifdef PBAP_SIM_ENABLE
60         "/SIM1/telecom/pb",
61 #endif
62         NULL
63 };
64
65 static guint total_missed_call_count = 0;
66 static guint unnotified_missed_call_count = 0;
67
68 GMainLoop *g_mainloop;
69 static GDBusConnection *pb_dbus_conn = NULL;
70
71 static const gchar pb_agent_introspection_xml[] =
72 "<node name='/'>"
73         "<interface name='org.bluez.PbAgent'>"
74                 "<method name='GetPhonebookFolderList'>"
75                         "<arg type='as' name='folder_list' direction='out'/>"
76                 "</method>"
77
78                 "<method name='GetPhonebook'>"
79                         "<arg type='s' name='name'/>"
80                         "<arg type='t' name='filter'/>"
81                         "<arg type='y' name='format'/>"
82                         "<arg type='q' name='max_list_count'/>"
83                         "<arg type='q' name='list_start_offset'/>"
84                         "<arg type='as' name='phonebook' direction='out'/>"
85                         "<arg type='u' name='new_missed_call' direction='out'/>"
86                 "</method>"
87
88                 "<method name='GetPhonebookSize'>"
89                         "<arg type='s' name='name'/>"
90                         "<arg type='u' name='phonebook_size' direction='out'/>"
91                         "<arg type='u' name='new_missed_call' direction='out'/>"
92                 "</method>"
93
94                 "<method name='GetPhonebookList'>"
95                         "<arg type='s' name='name'/>"
96                         "<arg type='a(ssu)' name='phonebook_list' direction='out'/>"
97                         "<arg type='u' name='new_missed_call' direction='out'/>"
98                 "</method>"
99
100                 "<method name='GetPhonebookEntry'>"
101                         "<arg type='s' name='folder'/>"
102                         "<arg type='s' name='id'/>"
103                         "<arg type='t' name='filter'/>"
104                         "<arg type='y' name='format'/>"
105                         "<arg type='s' name='phonebook_entry' direction='out'/>"
106                 "</method>"
107
108                 "<method name='GetTotalObjectCount'>"
109                         "<arg type='s' name='path'/>"
110                         "<arg type='u' name='phonebook_size' direction='out'/>"
111                 "</method>"
112
113                 "<method name='AddContact'>"
114                         "<arg type='s' name='filename'/>"
115                 "</method>"
116
117                 "<method name='DestroyAgent'>"
118                 "</method>"
119         "</interface>"
120
121         "<interface name='org.bluez.PbAgent.At'>"
122                 "<method name='GetPhonebookSizeAt'>"
123                         "<arg type='s' name='command'/>"
124                         "<arg type='u' name='phonebook_size' direction='out'/>"
125                 "</method>"
126
127                 "<method name='GetPhonebookEntriesAt'>"
128                         "<arg type='s' name='command'/>"
129                         "<arg type='i' name='start_index'/>"
130                         "<arg type='i' name='end_index'/>"
131                         "<arg type='a(ssu)' name='phonebook_entries' direction='out'/>"
132                 "</method>"
133
134                 "<method name='GetPhonebookEntriesFindAt'>"
135                         "<arg type='s' name='command'/>"
136                         "<arg type='s' name='find_text' />"
137                         "<arg type='a(ssu)' name='phonebook_entries' direction='out'/>"
138                 "</method>"
139         "</interface>"
140 "</node>";
141
142 static void __bt_pb_agent_method(GDBusConnection *connection,
143                         const gchar *sender, const gchar *object_path,
144                         const gchar *interface_name, const gchar *method_name,
145                         GVariant *parameters, GDBusMethodInvocation *invocation,
146                         gpointer user_data);
147
148 static GVariant *__bt_pb_get_phonebook_folder_list(GError **error);
149
150 static GVariant *__bt_pb_get_phonebook(PbAgentData *agent, const char *name,
151                         guint64 filter, guint8 format, guint16 max_list_count,
152                         guint16 list_start_offset, GError **err);
153
154 static GVariant *__bt_pb_get_phonebook_size(PbAgentData *agent,
155                                         const char *name, GError **err);
156
157 static GVariant *__bt_pb_get_phonebook_list(PbAgentData *agent,
158                                         const char *name, GError **err);
159
160 static GVariant *__bt_pb_get_phonebook_entry(PbAgentData *agent,
161                         const gchar *folder, const gchar *id, guint64 filter,
162                         guint8 format, GError **err);
163
164 static GVariant *__bt_pb_get_phonebook_size_at(PbAgentData *agent,
165                                         const gchar *command, GError **err);
166
167 static GVariant *__bt_pb_get_phonebook_entries_at(PbAgentData *agent,
168                         const gchar *command, gint32 start_index,
169                         gint32 end_index, GError **err);
170
171 static GVariant *__bt_pb_get_phonebook_entries_find_at(PbAgentData *agent,
172                                 const gchar *command, const gchar *find_text,
173                                 GError **err);
174
175 static GVariant *__bt_pb_get_total_object_count(PbAgentData *agent,
176                                         gchar *path, GError **err);
177
178 static gboolean __bt_pb_add_contact(PbAgentData *agent, const char *filename,
179                                         GError **error);
180
181 static gboolean __bt_pb_destroy_agent();
182
183 static GError *__bt_pb_error(gint error_code, const gchar *error_message);
184
185 static PhoneBookType __bluetooth_pb_get_pb_type(const char *name);
186
187 static PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name);
188
189 static gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
190                                                 gint *match, gint size);
191
192 static contacts_query_h __bluetooth_pb_query_phone_log(gint *match, gint size);
193
194 static contacts_query_h __bluetooth_pb_query_person(int addressbook);
195
196 static contacts_query_h __bluetooth_pb_query_person_number(void);
197
198 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void);
199
200 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void);
201
202 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void);
203
204 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void);
205
206 static gboolean __bluetooth_pb_get_count(PhoneBookType pb_type, guint *count);
207
208 static gboolean __bluetooth_pb_get_count_new_missed_call(guint *count);
209
210 static const char *__bluetooth_pb_phone_log_get_log_type(contacts_record_h record);
211
212 static void __bluetooth_pb_get_vcards(PbAgentData *agent, PhoneBookType pb_type,
213                         guint64 filter, guint8 format, guint16 max_list_count,
214                         guint16 list_start_offset, GVariantBuilder *vcards);
215
216 static void __bluetooth_pb_get_contact_list(PbAgentData *agent,
217                         contacts_query_h query, GVariantBuilder *builder);
218
219 static void __bluetooth_pb_get_phone_log_list(PbAgentData *agent,
220                         contacts_query_h query, GVariantBuilder *builder);
221
222 static void __bluetooth_pb_get_list(PbAgentData *agent, PhoneBookType pb_type,
223                                 GVariantBuilder *builder);
224
225 static void __bluetooth_pb_get_contact_list_number(PbAgentData *agent,
226                                 contacts_query_h query, gint start_index,
227                                 gint end_index, GVariantBuilder *builder);
228
229 static void __bluetooth_pb_get_phone_log_list_number(PbAgentData *agent,
230                                 contacts_query_h query, gint start_index,
231                                 gint end_index, GVariantBuilder *builder);
232
233 static void __bluetooth_pb_get_list_number(PbAgentData *agent,
234                                 PhoneBookType pb_type, gint start_index,
235                                 gint end_index, GVariantBuilder *builder);
236
237 static void __bluetooth_pb_get_contact_list_name(PbAgentData *agent,
238                                 contacts_query_h query, const gchar *find_text,
239                                 GVariantBuilder *builder);
240
241 static void __bluetooth_pb_get_phone_log_list_name(PbAgentData *agent,
242                                 contacts_query_h query, const gchar *find_text,
243                                 GVariantBuilder *builder);
244
245 static void __bluetooth_pb_get_list_name(PbAgentData *agent,
246                                 PhoneBookType pb_type, const gchar *find_text,
247                                 GVariantBuilder *builder);
248
249 static void __bluetooth_pb_list_ptr_array_add(GVariantBuilder *builder,
250                         const gchar *name, const gchar *number, gint handle);
251
252 static void __bluetooth_pb_agent_signal_handler(int signum);
253
254 static void __bluetooth_pb_contact_changed(const gchar *view_uri,
255                                         void *user_data);
256
257 static void __bluetooth_pb_agent_timeout_add_seconds(PbAgentData *agent);
258
259 static gboolean __bluetooth_pb_agent_timeout_calback(gpointer user_data);
260
261 static void __bluetooth_pb_tel_callback(TapiHandle *handle, int result,
262                                         void *data, void *user_data);
263
264 static gboolean __bt_pb_dbus_init(PbAgentData *agent);
265
266 static gboolean __bt_pb_dbus_deinit(PbAgentData *agent);
267
268 static const GDBusInterfaceVTable method_table = {
269         __bt_pb_agent_method,
270         NULL,
271         NULL,
272 };
273
274 static void __bt_pb_agent_method(GDBusConnection *connection,
275                         const gchar *sender, const gchar *object_path,
276                         const gchar *interface_name, const gchar *method_name,
277                         GVariant *parameters, GDBusMethodInvocation *invocation,
278                         gpointer user_data)
279 {
280         FN_START;
281         INFO("method: %s; object_path: %s", method_name, object_path);
282         PbAgentData *agent = (PbAgentData *)user_data;
283         GError *err = NULL;
284         if (g_strcmp0(interface_name, "org.bluez.PbAgent") == 0) {
285                 if (g_strcmp0(method_name, "GetPhonebookFolderList") == 0) {
286                         GVariant *folder_list = NULL;
287
288                         folder_list = __bt_pb_get_phonebook_folder_list(&err);
289                         if (err)
290                                 goto fail;
291                         g_dbus_method_invocation_return_value(invocation,
292                                                                 folder_list);
293                 } else if (g_strcmp0(method_name, "GetPhonebook") == 0) {
294                         GVariant *phonebook = NULL;
295                         const char *name;
296                         guint64 filter;
297                         guint8 format;
298                         guint16 max_list_count;
299                         guint16 list_start_offset;
300
301                         g_variant_get(parameters, "(&styqq)", &name, &filter,
302                                                 &format, &max_list_count,
303                                                 &list_start_offset);
304                         phonebook = __bt_pb_get_phonebook(agent, name, filter,
305                                                 format, max_list_count,
306                                                 list_start_offset, &err);
307                         if (err)
308                                 goto fail;
309                         g_dbus_method_invocation_return_value(invocation,
310                                                                 phonebook);
311                 } else if (g_strcmp0(method_name, "GetPhonebookSize") == 0) {
312                         GVariant *phonebook_size = NULL;
313                         const char *name;
314
315                         g_variant_get(parameters, "(&s)", &name);
316                         phonebook_size = __bt_pb_get_phonebook_size(agent, name,
317                                                                         &err);
318                         if (err)
319                                 goto fail;
320                         g_dbus_method_invocation_return_value(invocation,
321                                                                 phonebook_size);
322                 } else if (g_strcmp0(method_name, "GetPhonebookList") == 0) {
323                         GVariant *phonebook_list = NULL;
324                         const char *name;
325
326                         g_variant_get(parameters, "(&s)", &name);
327                         phonebook_list = __bt_pb_get_phonebook_list(agent, name,
328                                                                         &err);
329                         if (err)
330                                 goto fail;
331                         g_dbus_method_invocation_return_value(invocation,
332                                                                 phonebook_list);
333                 } else if (g_strcmp0(method_name, "GetPhonebookEntry") == 0) {
334                         GVariant *phonebook_entry = NULL;
335                         const gchar *folder;
336                         const gchar *id;
337                         guint64 filter;
338                         guint8 format;
339
340                         g_variant_get(parameters, "(&s&sty)", &folder, &id,
341                                                         &filter, &format);
342                         phonebook_entry = __bt_pb_get_phonebook_entry(agent,
343                                                 folder, id, filter, format, &err);
344                         if (err)
345                                 goto fail;
346                         g_dbus_method_invocation_return_value(invocation,
347                                                         phonebook_entry);
348                 } else if (g_strcmp0(method_name, "GetTotalObjectCount") == 0) {
349                         GVariant *phonebook_size = NULL;
350                         gchar *path;
351
352                         g_variant_get(parameters, "(&s)", &path);
353                         phonebook_size = __bt_pb_get_total_object_count(agent,
354                                                                 path, &err);
355                         if (err)
356                                 goto fail;
357                         g_dbus_method_invocation_return_value(invocation,
358                                                                 phonebook_size);
359                 } else if (g_strcmp0(method_name, "AddContact") == 0) {
360                         const char *filename;
361
362                         g_variant_get(parameters, "(&s)", &filename);
363                         __bt_pb_add_contact(agent, filename, &err);
364                         if (err)
365                                 goto fail;
366                         g_dbus_method_invocation_return_value(invocation, NULL);
367                 } else if (g_strcmp0(method_name, "DestroyAgent") == 0) {
368                         g_dbus_method_invocation_return_value(invocation, NULL);
369                         __bt_pb_destroy_agent();
370                 }
371         } else if (g_strcmp0(interface_name, "org.bluez.PbAgent.At") == 0) {
372                 if (g_strcmp0(method_name, "GetPhonebookSizeAt") == 0) {
373                         GVariant *phonebook_size = NULL;
374                         const gchar *command;
375
376                         g_variant_get(parameters, "(&s)", &command);
377                         phonebook_size = __bt_pb_get_phonebook_size_at(agent,
378                                                                 command, &err);
379                         if (err)
380                                 goto fail;
381                         g_dbus_method_invocation_return_value(invocation,
382                                                                 phonebook_size);
383                 } else if (g_strcmp0(method_name,
384                                         "GetPhonebookEntriesAt") == 0) {
385                         GVariant *phonebook_entries = NULL;
386                         const gchar *command;
387                         gint32 start_index;
388                         gint32 end_index;
389
390                         g_variant_get(parameters, "(&sii)",
391                                         &command, &start_index, &end_index);
392                         phonebook_entries = __bt_pb_get_phonebook_entries_at(agent,
393                                                         command, start_index,
394                                                         end_index, &err);
395                         if (err)
396                                 goto fail;
397                         g_dbus_method_invocation_return_value(invocation,
398                                                         phonebook_entries);
399                 } else if (g_strcmp0(method_name,
400                                         "GetPhonebookEntriesFindAt") == 0) {
401                         GVariant *phonebook_entries = NULL;
402                         const gchar *command;
403                         const gchar *find_text;
404
405                         g_variant_get(parameters, "(&s&s)", &command, &find_text);
406                         phonebook_entries = __bt_pb_get_phonebook_entries_find_at(agent,
407                                                         command, find_text, &err);
408                         if (err)
409                                 goto fail;
410                         g_dbus_method_invocation_return_value(invocation,
411                                                         phonebook_entries);
412                 }
413         }
414
415         FN_END;
416         return;
417
418 fail:
419         g_dbus_method_invocation_return_gerror(invocation, err);
420         g_clear_error(&err);
421         FN_END;
422         return;
423 }
424
425 static void bluetooth_pb_agent_clear(PbAgentData *agent)
426 {
427         FN_START;
428         agent->pb_type = TELECOM_NONE;
429         FN_END;
430 }
431
432 static GDBusConnection *__bt_pb_get_gdbus_connection(void)
433 {
434         FN_START;
435         GError *err = NULL;
436
437         if (pb_dbus_conn)
438                 return pb_dbus_conn;
439
440         pb_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
441         if (!pb_dbus_conn) {
442                 if (err) {
443                         ERR("Unable to connect to dbus: %s", err->message);
444                         g_clear_error(&err);
445                 }
446                 return NULL;
447         }
448         FN_END;
449         return pb_dbus_conn;
450 }
451
452 static GVariant *__bt_pb_get_phonebook_folder_list(GError **error)
453 {
454         FN_START;
455         GVariant *folder_list;
456         gint size;
457         gint i;
458         GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
459
460         size = G_N_ELEMENTS(bluetooth_pb_agent_folder_list);
461
462         for (i = 0; i < size; i++)
463                 g_variant_builder_add(builder, "s",
464                                         bluetooth_pb_agent_folder_list[i]);
465
466         folder_list = g_variant_new("(as)", builder);
467         g_variant_builder_unref(builder);
468
469         FN_END;
470         return folder_list;
471 }
472
473
474 static GVariant *__bt_pb_get_phonebook(PbAgentData *agent, const char *name,
475                         guint64 filter, guint8 format, guint16 max_list_count,
476                         guint16 list_start_offset, GError **err)
477 {
478         FN_START;
479         GVariant *phonebook;
480         PhoneBookType pb_type = TELECOM_NONE;
481         GVariantBuilder *vcards = g_variant_builder_new(G_VARIANT_TYPE("as"));
482
483         INFO("name: %s filter: %lld format: %d max_list_count: %d list_start_offset: %d\n",
484                         name, filter, format, max_list_count, list_start_offset);
485
486         __bluetooth_pb_agent_timeout_add_seconds(agent);
487
488         pb_type = __bluetooth_pb_get_pb_type(name);
489
490         if (pb_type == TELECOM_NONE) {
491                 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
492                                                 "unsupported name defined");
493                 return NULL;
494         }
495
496         if (max_list_count > 0) {
497                 __bluetooth_pb_get_vcards(agent, pb_type, filter, format,
498                                 max_list_count, list_start_offset, vcards);
499         }
500
501         if (pb_type == TELECOM_MCH) {
502                 phonebook = g_variant_new("(asu)", vcards,
503                                                 unnotified_missed_call_count);
504                 INFO("Notified [%d] missed call count",
505                                                 unnotified_missed_call_count);
506                 unnotified_missed_call_count = 0;
507         } else {
508                 phonebook = g_variant_new("(asu)", vcards, 0);
509         }
510
511         g_variant_builder_unref(vcards);
512
513         FN_END;
514         return phonebook;
515 }
516
517 static GVariant *__bt_pb_get_phonebook_size(PbAgentData *agent,
518                                                 const char *name, GError **err)
519 {
520         FN_START;
521         GVariant *phonebook_size;
522         PhoneBookType pb_type = TELECOM_NONE;
523         guint count = 0;
524
525         DBG_SECURE("name: %s\n", name);
526
527         __bluetooth_pb_agent_timeout_add_seconds(agent);
528
529         pb_type = __bluetooth_pb_get_pb_type(name);
530
531         if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
532                 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
533                                                 "unsupported name defined");
534                 return NULL;
535         }
536
537         /* for owner */
538 #ifdef PBAP_SIM_ENABLE
539         if (pb_type == TELECOM_PB || pb_type == SIM_PB)
540                 count++;
541 #else
542         if (pb_type == TELECOM_PB)
543                 count++;
544 #endif
545         if (pb_type == TELECOM_MCH) {
546                 phonebook_size = g_variant_new("(uu)", count,
547                                                 unnotified_missed_call_count);
548                 INFO("Notified [%d] missed call count",
549                                                 unnotified_missed_call_count);
550                 unnotified_missed_call_count = 0;
551         } else {
552                 phonebook_size = g_variant_new("(uu)", count, 0);
553         }
554
555         FN_END;
556         return phonebook_size;
557 }
558
559
560 static GVariant *__bt_pb_get_phonebook_list(PbAgentData *agent,
561                                                 const char *name, GError **err)
562 {
563         FN_START;
564         GVariant *phonebook_list;
565         PhoneBookType pb_type = TELECOM_NONE;
566
567         GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("a(ssu)"));
568
569         DBG_SECURE("name: %s\n", name);
570
571         __bluetooth_pb_agent_timeout_add_seconds(agent);
572
573         pb_type = __bluetooth_pb_get_pb_type(name);
574
575         if (pb_type == TELECOM_NONE) {
576                 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
577                                                 "unsupported name defined");
578                 return NULL;
579         }
580
581         __bluetooth_pb_get_list(agent, pb_type, builder);
582
583         INFO("pb_type[%d] / number of missed_call[%d]", pb_type,
584                                                 unnotified_missed_call_count);
585
586         if (pb_type == TELECOM_MCH) {
587                 phonebook_list = g_variant_new("(a(ssu)u)", builder,
588                                                 unnotified_missed_call_count);
589                 INFO("Notified [%d] missed call count",
590                                                 unnotified_missed_call_count);
591                 unnotified_missed_call_count = 0;
592         } else {
593                 phonebook_list = g_variant_new("(a(ssu)u)", builder, 0);
594         }
595
596         if (builder)
597                 g_variant_builder_unref(builder);
598
599         FN_END;
600         return phonebook_list;
601 }
602
603 static GVariant *__bt_pb_get_phonebook_entry(PbAgentData *agent,
604                         const gchar *folder, const gchar *id, guint64 filter,
605                         guint8 format, GError **err)
606 {
607         FN_START;
608         GVariant *phonebook_entry;
609         PhoneBookType pb_type = TELECOM_NONE;
610         gint handle = 0;
611         gchar *str = NULL;
612         const gchar *attr = NULL;
613
614         DBG_SECURE("folder: %s id: %s filter: %ld format: %d\n",
615                         folder, id, filter, format);
616
617         __bluetooth_pb_agent_timeout_add_seconds(agent);
618
619         if (!g_str_has_suffix(id, ".vcf")) {
620                 *err = __bt_pb_error(G_FILE_ERROR_INVAL, "invalid vcf file");
621                 return NULL;
622         }
623
624         handle = (gint)g_ascii_strtoll(id, NULL, 10);
625
626         pb_type = __bluetooth_pb_get_pb_type(folder);
627
628         if (pb_type == TELECOM_NONE) {
629                 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
630                                                 "unsupported name defined");
631                 return NULL;
632         }
633
634         /* create index cache */
635         __bluetooth_pb_get_list(agent, pb_type, NULL);
636
637         switch (pb_type) {
638         case TELECOM_PB:
639                 if (handle == 0) {
640                         str = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
641                                                                 filter, format);
642                 } else {
643                         if (_bluetooth_get_contact_addressbook(handle) == PBAP_ADDRESSBOOK_PHONE)
644                                 str = _bluetooth_pb_vcard_contact(handle,
645                                                                 filter, format);
646                 }
647                 break;
648
649         case TELECOM_ICH:
650                 str = _bluetooth_pb_vcard_call(handle, filter, format,
651                                                                 "RECEIVED");
652                 break;
653         case TELECOM_OCH:
654                 str = _bluetooth_pb_vcard_call(handle, filter, format, "DIALED");
655                 break;
656         case TELECOM_MCH:
657                 str = _bluetooth_pb_vcard_call(handle, filter, format, "MISSED");
658                 break;
659         case TELECOM_CCH: {
660                 contacts_record_h record = NULL;
661
662                 gint status;
663
664                 status = contacts_db_get_record(_contacts_phone_log._uri,
665                                 handle, &record);
666
667                 if (status != CONTACTS_ERROR_NONE)
668                         break;
669
670                 attr = __bluetooth_pb_phone_log_get_log_type(record);
671                 str = _bluetooth_pb_vcard_call(handle, filter, format, attr);
672
673                 contacts_record_destroy(record, TRUE);
674                 break;
675         }
676 #ifdef PBAP_SIM_ENABLE
677         case SIM_PB:
678                 if (handle == 0) {
679                         str = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
680                                                                 filter, format);
681                 } else {
682                         if (_bluetooth_get_contact_addressbook(handle) == PBAP_ADDRESSBOOK_SIM)
683                                 str = _bluetooth_pb_vcard_contact(handle,
684                                                                 filter, format);
685                 }
686                 break;
687 #endif
688         default:
689                 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
690                                                 "unsupported name defined");
691                 return NULL;
692         }
693
694         phonebook_entry = g_variant_new("(s)", str);
695
696         g_free(str);
697
698         FN_END;
699         return phonebook_entry;
700 }
701
702 static GVariant *__bt_pb_get_phonebook_size_at(PbAgentData *agent,
703                                         const gchar *command, GError **err)
704 {
705         FN_START;
706         GVariant *phonebook_size;
707         PhoneBookType pb_type = TELECOM_NONE;
708         guint count = 0;
709
710         DBG("command: %s\n", command);
711
712         __bluetooth_pb_agent_timeout_add_seconds(agent);
713
714         pb_type = __bluetooth_pb_get_storage_pb_type(command);
715
716         if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
717                 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
718                                                 "unsupported name defined");
719                 return NULL;
720         }
721
722         phonebook_size = g_variant_new("(u)", count);
723
724         FN_END;
725         return phonebook_size;
726 }
727
728 static GVariant *__bt_pb_get_phonebook_entries_at(PbAgentData *agent,
729                                 const gchar *command, gint32 start_index,
730                                 gint32 end_index, GError **err)
731 {
732         FN_START;
733         GVariant *phonebook_entries;
734         PhoneBookType pb_type = TELECOM_NONE;
735         GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("a(ssu)"));
736
737         DBG("command: %s, start_index: %d, end_index: %d\n",
738                         command, start_index, end_index);
739
740         __bluetooth_pb_agent_timeout_add_seconds(agent);
741
742         pb_type = __bluetooth_pb_get_storage_pb_type(command);
743
744         if (pb_type == TELECOM_NONE || pb_type == TELECOM_CCH) {
745                 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
746                                                 "unsupported name defined");
747                 return NULL;
748         }
749
750         __bluetooth_pb_get_list_number(agent, pb_type,
751                         start_index, end_index, builder);
752
753         phonebook_entries = g_variant_new("(a(ssu))", builder);
754         if (builder)
755                 g_variant_builder_unref(builder);
756
757         FN_END;
758         return phonebook_entries;
759 }
760
761 static GVariant *__bt_pb_get_phonebook_entries_find_at(PbAgentData *agent,
762                                 const gchar *command, const gchar *find_text,
763                                 GError **err)
764 {
765         FN_START;
766         GVariant *phonebook_entries;
767         PhoneBookType pb_type = TELECOM_NONE;
768         GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("a(ssu)"));
769
770         DBG("command: %s, find text: %s\n", command, find_text);
771
772         __bluetooth_pb_agent_timeout_add_seconds(agent);
773
774         pb_type = __bluetooth_pb_get_storage_pb_type(command);
775
776         if (pb_type == TELECOM_NONE || pb_type == TELECOM_CCH) {
777                 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
778                                                 "unsupported name defined");
779                 return NULL;
780         }
781
782         __bluetooth_pb_get_list_name(agent, pb_type, find_text, builder);
783
784         phonebook_entries = g_variant_new("(a(ssu))", builder);
785
786         if (builder)
787                 g_variant_builder_unref(builder);
788
789         FN_END;
790         return phonebook_entries;
791 }
792
793 static GVariant *__bt_pb_get_total_object_count(PbAgentData *agent,
794                                                 gchar *path, GError **err)
795 {
796         FN_START;
797         GVariant *phonebook_size;
798         guint count = 0;
799         PhoneBookType pb_type = TELECOM_NONE;
800
801         __bluetooth_pb_agent_timeout_add_seconds(agent);
802
803         pb_type = __bluetooth_pb_get_storage_pb_type(path);
804
805         if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
806                 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
807                                                 "unsupported name defined");
808                 return NULL;
809         }
810
811         phonebook_size = g_variant_new("(u)", count);
812
813         FN_END;
814         return phonebook_size;
815 }
816
817 #if 0
818 static int __bluetooth_pb_agent_read_file(const char *file_path, char **stream)
819 {
820         FN_START;
821         FILE *fp = NULL;
822         int read_len = -1;
823         int received_file_size = 0;
824         struct stat file_attr;
825
826         if (file_path == NULL || stream == NULL) {
827                 ERR("Invalid data \n");
828                 return -1;
829         }
830
831         DBG_SECURE("file_path = %s\n", file_path);
832
833         if ((fp = fopen(file_path, "r+")) == NULL) {
834                 ERR_SECURE("Cannot open %s\n", file_path);
835                 return -1;
836         }
837
838         if (fstat(fileno(fp), &file_attr) == 0) {
839                 received_file_size = file_attr.st_size;
840                 DBG("file_attr.st_size = %d, size = %d\n", file_attr.st_size,
841                                                         received_file_size);
842
843                 if (received_file_size <= 0) {
844                         ERR_SECURE("Some problem in the file size [%s]  \n",
845                                                                 file_path);
846                         fclose(fp);
847                         fp = NULL;
848                         return -1;
849                 }
850
851                 *stream = (char *)malloc(sizeof(char) *received_file_size);
852                 if (NULL == *stream) {
853                         fclose(fp);
854                         fp = NULL;
855                         return -1;
856                 }
857         } else {
858                 ERR_SECURE("Some problem in the file [%s]  \n", file_path);
859                 fclose(fp);
860                 fp = NULL;
861                 return -1;
862         }
863
864         read_len = fread(*stream, 1, received_file_size, fp);
865
866         if (read_len == 0) {
867                 if (fp != NULL) {
868                         fclose(fp);
869                         fp = NULL;
870                 }
871                 DBG_SECURE("Cannot open %s\n", file_path);
872                 return -1;
873         }
874
875         if (fp != NULL) {
876                 fclose(fp);
877                 fp = NULL;
878         }
879         FN_END;
880         return 0;
881 }
882 #endif
883
884 static gboolean __bt_pb_add_contact(PbAgentData *agent, const char *filename,
885                                          GError **error)
886 {
887         FN_START;
888         /* Contact API is changed, Temporary blocked */
889 #if 0
890         CTSstruct *contact_record = NULL;
891         GSList *numbers_list = NULL, *cursor;
892         int is_success = 0;
893         int is_duplicated = 0;
894         int err = 0;
895         char *stream = NULL;
896
897         DBG_SECURE("file_path = %s\n", filename);
898
899         err = contacts_svc_connect();
900         ERR("contact_db_service_connect fucntion call [error] = %d \n", err);
901
902         err = __bluetooth_pb_agent_read_file(filename, &stream);
903
904         if (err != 0) {
905                 contacts_svc_disconnect();
906                 ERR("contacts_svc_disconnect fucntion call [error] = %d \n", err);
907
908                 if (NULL != stream) {
909                         free(stream);
910                         stream = NULL;
911                 }
912                 return FALSE;
913         }
914
915         is_success = contacts_svc_get_contact_from_vcard((const void *)stream,
916                                                         &contact_record);
917
918         DBG("contacts_svc_get_contact_from_vcard fucntion call [is_success] = %d \n", is_success);
919
920         if (0 == is_success) {
921                 contacts_svc_struct_get_list(contact_record, CTS_CF_NUMBER_LIST,
922                                                                 &numbers_list);
923                 cursor = numbers_list;
924
925                 for (; cursor; cursor = g_slist_next(cursor)) {
926                         if (contacts_svc_find_contact_by(CTS_FIND_BY_NUMBER,
927                                         contacts_svc_value_get_str(cursor->data,
928                                         CTS_NUM_VAL_NUMBER_STR)) > 0) {
929                                 DBG("is_duplicated\n");
930                                 is_duplicated = TRUE;
931                         }
932                 }
933
934                 if (is_duplicated == FALSE) {
935                         contacts_svc_insert_contact(0, contact_record);
936                 }
937         } else {
938                 ERR("Fail \n");
939         }
940
941         err = contacts_svc_disconnect();
942         ERR("contacts_svc_disconnect fucntion call [error] = %d \n", err);
943
944         if (NULL != stream) {
945                 free(stream);
946                 stream = NULL;
947         }
948 #endif
949         FN_END;
950         return TRUE;
951 }
952
953 /* Create GError from error code and error message*/
954 static GError *__bt_pb_error(gint error_code, const gchar *error_message)
955 {
956         return g_error_new(g_quark_from_string("PB Agent"),
957                         error_code, "PB Agent Error: %s", error_message);
958 }
959
960 static PhoneBookType __bluetooth_pb_get_pb_type(const char *name)
961 {
962         FN_START;
963         gchar *suffix = ".vcf";
964         gint len;
965         gint size;
966         gint i;
967
968         if (name == NULL)
969                 return TELECOM_NONE;
970
971         len = strlen(name);
972
973         if (g_str_has_suffix(name, suffix))
974                 len -= strlen(suffix);
975
976         size = G_N_ELEMENTS(bluetooth_pb_agent_folder_list) - 1;
977         for (i = 0; i < size; i++) {
978                 if (strncmp(name, bluetooth_pb_agent_folder_list[i], len) == 0)
979                         return i;
980         }
981
982         FN_END;
983         return TELECOM_NONE;
984 }
985
986 static PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name)
987 {
988         FN_START;
989         if (name == NULL)
990                 return TELECOM_NONE;
991
992         if (g_strcmp0(name, "\"ME\"") == 0)
993                 return TELECOM_PB;
994
995         if (g_strcmp0(name, "\"RC\"") == 0)
996                 return TELECOM_ICH;
997
998         if (g_strcmp0(name, "\"DC\"") == 0)
999                 return TELECOM_OCH;
1000
1001         if (g_strcmp0(name, "\"MC\"") == 0)
1002                 return TELECOM_MCH;
1003 #ifdef PBAP_SIM_ENABLE
1004         if (g_strcmp0(name, "\"SM\"") == 0)
1005                 return SIM_PB;
1006 #endif
1007         FN_END;
1008         return TELECOM_NONE;
1009 }
1010
1011 static gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
1012                                                 gint *match, gint size)
1013 {
1014         FN_START;
1015         gint i;
1016         gint status;
1017
1018         for (i = 0; i < size; i++) {
1019
1020                 if (i > 0) {
1021                         status = contacts_filter_add_operator(filter,
1022                                         CONTACTS_FILTER_OPERATOR_OR);
1023
1024                         if (status != CONTACTS_ERROR_NONE)
1025                                 return status;
1026                 }
1027
1028                 status = contacts_filter_add_int(filter,
1029                                         _contacts_phone_log.log_type,
1030                                         CONTACTS_MATCH_EQUAL, match[i]);
1031
1032                 if (status != CONTACTS_ERROR_NONE)
1033                         return status;
1034         }
1035
1036         FN_END;
1037         return CONTACTS_ERROR_NONE;
1038 }
1039
1040 static contacts_query_h __bluetooth_pb_query_phone_log(gint *match, gint size)
1041 {
1042         FN_START;
1043         contacts_query_h query = NULL;
1044         contacts_filter_h filter = NULL;
1045         gint status;
1046
1047         status = contacts_query_create(_contacts_phone_log._uri, &query);
1048
1049         if (status != CONTACTS_ERROR_NONE)
1050                 return NULL;
1051
1052         status = contacts_filter_create(_contacts_phone_log._uri, &filter);
1053
1054         if (status != CONTACTS_ERROR_NONE) {
1055                 contacts_query_destroy(query);
1056                 return NULL;
1057         }
1058
1059         status = __bluetooth_pb_phone_log_filter_append(filter, match, size);
1060
1061         if (status != CONTACTS_ERROR_NONE) {
1062                 contacts_filter_destroy(filter);
1063                 contacts_query_destroy(query);
1064                 return NULL;
1065         }
1066
1067         status = contacts_query_set_filter(query, filter);
1068
1069         if (status != CONTACTS_ERROR_NONE) {
1070                 contacts_filter_destroy(filter);
1071                 contacts_query_destroy(query);
1072                 return NULL;
1073         }
1074
1075         status = contacts_query_set_sort(query, _contacts_phone_log.log_time,
1076                                                                         false);
1077
1078         if (status != CONTACTS_ERROR_NONE) {
1079                 contacts_filter_destroy(filter);
1080                 contacts_query_destroy(query);
1081                 return NULL;
1082         }
1083
1084         contacts_filter_destroy(filter);
1085
1086         FN_END;
1087         return query;
1088 }
1089
1090 bool __bt_is_matching_addressbook(const char *addressbook_name, int addressbook)
1091 {
1092         bool is_sim_addressbook = _bt_is_sim_addressbook(addressbook_name);
1093
1094         if ((is_sim_addressbook == false
1095                         && addressbook == PBAP_ADDRESSBOOK_PHONE) ||
1096                 (is_sim_addressbook == true
1097                         && addressbook == PBAP_ADDRESSBOOK_SIM))
1098                 return true;
1099
1100         return false;
1101 }
1102
1103 static contacts_query_h __bluetooth_pb_query_person(int addressbook)
1104 {
1105         FN_START;
1106         contacts_query_h query = NULL;
1107         contacts_filter_h filter = NULL;
1108         contacts_list_h recordList = NULL;
1109         contacts_record_h record = NULL;
1110         char *addressbook_name = NULL;
1111         int address_book_id = 0;
1112         int count = 0;
1113         unsigned int i = 0;
1114         gint status;
1115         bool is_first_condition = true;
1116
1117         DBG("Addressbook [%d]", addressbook);
1118         /* Create query*/
1119         status = contacts_query_create(_contacts_person_contact._uri, &query);
1120         if (status != 0) {
1121                 ERR("Could not create query");
1122                 return NULL;
1123         }
1124
1125         /* Create addressbook Filter*/
1126         status = contacts_db_get_all_records(_contacts_address_book._uri, 0, 0,
1127                                         &recordList);
1128         if (status != CONTACTS_ERROR_NONE)
1129                 ERR("Contact list get api failed %d", status);
1130
1131         contacts_filter_create(_contacts_person_contact._uri, &filter);
1132         contacts_list_get_count(recordList, &count);
1133
1134         for (i = 0; i < count; i++) {
1135                 status = contacts_list_get_current_record_p(recordList, &record);
1136                 if (status != CONTACTS_ERROR_NONE) {
1137                         ERR("Contact list get api failed %d", status);
1138                         goto next;
1139                 }
1140                 status = contacts_record_get_str_p(record,
1141                                                 _contacts_address_book.name,
1142                                                 &addressbook_name);
1143                 if (status != CONTACTS_ERROR_NONE) {
1144                         ERR("Contact record get api failed %d", status);
1145                         goto next;
1146                 }
1147                 status = contacts_record_get_int(record,
1148                                                 _contacts_address_book.id,
1149                                                 &address_book_id);
1150                 if (status != CONTACTS_ERROR_NONE) {
1151                         ERR("contacts record get int api failed %d", status);
1152                         goto next;
1153                 }
1154
1155                 DBG("Addressbook ID: [%d] Addressbook Name: [%s]",
1156                                 address_book_id, addressbook_name);
1157
1158                 if (__bt_is_matching_addressbook(addressbook_name,
1159                                                         addressbook)) {
1160                         if (is_first_condition)
1161                                 is_first_condition = false;
1162                         else
1163                                 contacts_filter_add_operator(filter,
1164                                                 CONTACTS_FILTER_OPERATOR_OR);
1165                         DBG("SELECTED Addressbook ID: [%d] Addressbook Name: [%s]",
1166                                         address_book_id, addressbook_name);
1167                         status = contacts_filter_add_int(filter,
1168                                         _contacts_person_contact.address_book_id,
1169                                         CONTACTS_MATCH_EQUAL, address_book_id);
1170                         if (status != CONTACTS_ERROR_NONE)
1171                                 ERR("Contact filter add failed %d", status);
1172                 }
1173 next:
1174                 if (contacts_list_next(recordList) != CONTACTS_ERROR_NONE)
1175                         break;
1176         }
1177
1178         contacts_list_destroy(recordList, true);
1179
1180         status = contacts_query_set_filter(query, filter);
1181         if (status != CONTACTS_ERROR_NONE)
1182                 ERR("Could not Apply Filter");
1183
1184         contacts_filter_destroy(filter);
1185         FN_END;
1186         return query;
1187 }
1188
1189 static contacts_query_h __bluetooth_pb_query_person_number(void)
1190 {
1191         FN_START;
1192         contacts_query_h query = NULL;
1193         gint status;
1194
1195         status = contacts_query_create(_contacts_person_number._uri, &query);
1196
1197         if (status != CONTACTS_ERROR_NONE)
1198                 return NULL;
1199
1200         FN_END;
1201         return query;
1202 }
1203
1204 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void)
1205 {
1206         FN_START;
1207         gint size = 4;
1208         gint match[] = {
1209                 CONTACTS_PLOG_TYPE_VOICE_INCOMING,
1210                 CONTACTS_PLOG_TYPE_VIDEO_INCOMING,
1211                 CONTACTS_PLOG_TYPE_VOICE_REJECT,
1212                 CONTACTS_PLOG_TYPE_VIDEO_REJECT
1213         };
1214
1215         FN_END;
1216         return __bluetooth_pb_query_phone_log(match, size);
1217 }
1218
1219 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void)
1220 {
1221         FN_START;
1222         gint size = 2;
1223         gint match[] = {
1224                 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1225                 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING
1226         };
1227
1228         FN_END;
1229         return __bluetooth_pb_query_phone_log(match, size);
1230 }
1231
1232 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void)
1233 {
1234         FN_START;
1235         gint size = 4;
1236         gint match[] = {
1237                 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1238                 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN,
1239                 CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN,
1240                 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN
1241         };
1242
1243         FN_END;
1244         return __bluetooth_pb_query_phone_log(match, size);
1245 }
1246
1247 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void)
1248 {
1249         FN_START;
1250         gint size = 10;
1251         gint match[] = {
1252                 CONTACTS_PLOG_TYPE_VOICE_INCOMING,
1253                 CONTACTS_PLOG_TYPE_VIDEO_INCOMING,
1254                 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1255                 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING,
1256                 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1257                 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN,
1258                 CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN,
1259                 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN,
1260                 CONTACTS_PLOG_TYPE_VOICE_REJECT,
1261                 CONTACTS_PLOG_TYPE_VIDEO_REJECT
1262         };
1263
1264         FN_END;
1265         return __bluetooth_pb_query_phone_log(match, size);
1266 }
1267
1268 static gboolean __bluetooth_pb_get_count(PhoneBookType pb_type,
1269                                 guint *count)
1270 {
1271         FN_START;
1272         contacts_query_h query = NULL;
1273         gint status;
1274         gint signed_count;
1275
1276         switch (pb_type) {
1277         case TELECOM_PB:
1278                 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1279                 break;
1280         case TELECOM_ICH:
1281                 query = __bluetooth_pb_query_phone_log_incoming();
1282                 break;
1283         case TELECOM_OCH:
1284                 query = __bluetooth_pb_query_phone_log_outgoing();
1285                 break;
1286         case TELECOM_MCH:
1287                 query = __bluetooth_pb_query_phone_log_missed();
1288                 break;
1289         case TELECOM_CCH:
1290                 query = __bluetooth_pb_query_phone_log_combined();
1291                 break;
1292 #ifdef PBAP_SIM_ENABLE
1293         case SIM_PB:
1294                 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1295                 break;
1296 #endif
1297         default:
1298                 return FALSE;
1299         }
1300
1301         if (query == NULL)
1302                 return FALSE;
1303
1304         status = contacts_db_get_count_with_query(query, &signed_count);
1305
1306         if (status != CONTACTS_ERROR_NONE) {
1307                 contacts_query_destroy(query);
1308                 return FALSE;
1309         }
1310
1311         contacts_query_destroy(query);
1312
1313         if (signed_count < 0)
1314                 signed_count = 0;
1315
1316         *count = (gint) signed_count;
1317
1318         FN_END;
1319         return TRUE;
1320 }
1321
1322 static gboolean __bluetooth_pb_get_count_new_missed_call(guint *count)
1323 {
1324         FN_START;
1325         contacts_query_h query = NULL;
1326         gint status;
1327         gint signed_count;
1328         gint size = 2;
1329         gint match[] = {
1330                 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1331                 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN
1332         };
1333
1334         query = __bluetooth_pb_query_phone_log(match, size);
1335
1336         if (query == NULL)
1337                 return FALSE;
1338
1339         status = contacts_db_get_count_with_query(query, &signed_count);
1340
1341         if (status != CONTACTS_ERROR_NONE) {
1342                 contacts_query_destroy(query);
1343                 return FALSE;
1344         }
1345
1346         contacts_query_destroy(query);
1347
1348         if (signed_count < 0)
1349                 signed_count = 0;
1350
1351         *count = (guint)signed_count;
1352
1353         FN_END;
1354         return TRUE;
1355 }
1356
1357 static const char *__bluetooth_pb_phone_log_get_log_type(contacts_record_h record)
1358 {
1359         FN_START;
1360         gint status;
1361         gint log_type;
1362
1363         status = contacts_record_get_int(record, _contacts_phone_log.log_type,
1364                                                                 &log_type);
1365
1366         if (status != CONTACTS_ERROR_NONE)
1367                 return NULL;
1368
1369         switch (log_type) {
1370         case CONTACTS_PLOG_TYPE_VOICE_INCOMING:
1371         case CONTACTS_PLOG_TYPE_VIDEO_INCOMING:
1372         case CONTACTS_PLOG_TYPE_VOICE_REJECT:
1373         case CONTACTS_PLOG_TYPE_VIDEO_REJECT:
1374                 return "RECEIVED";
1375         case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
1376         case CONTACTS_PLOG_TYPE_VIDEO_OUTGOING:
1377                 return "DIALED";
1378         case CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN:
1379         case CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN:
1380         case CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN:
1381         case CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN:
1382                 return "MISSED";
1383         default:
1384                 return NULL;
1385         }
1386         FN_END;
1387 }
1388
1389 static void __bluetooth_pb_get_vcards(PbAgentData *agent, PhoneBookType pb_type,
1390                         guint64 filter, guint8 format, guint16 max_list_count,
1391                         guint16 list_start_offset, GVariantBuilder *vcards)
1392 {
1393         FN_START;
1394         contacts_list_h record_list = NULL;
1395         contacts_query_h query = NULL;
1396         gint status;
1397         gint limit;
1398         gint offset;
1399         guint property_id = 0;
1400         const char *attr = NULL;
1401         gboolean get_log = FALSE;
1402
1403         /* contact offset is n - 1 of PBAP */
1404         offset = (gint)list_start_offset - 1;
1405
1406         if (max_list_count >= 65535)
1407                 limit = -1;     /* contact limit -1 means unrestricted */
1408         else
1409                 limit = (gint)max_list_count;
1410
1411         switch (pb_type) {
1412         case TELECOM_PB:
1413 #ifdef PBAP_SIM_ENABLE
1414         case SIM_PB:
1415 #endif
1416                 /* for owner */
1417                 if (list_start_offset == 0) {
1418                         char *vcard;
1419
1420                         vcard = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
1421                                                                 filter, format);
1422                         if (vcard)
1423                                 g_variant_builder_add(vcards, "s", vcard);
1424
1425                         offset = 0;
1426
1427                         if (limit == 1)
1428                                 return;
1429                         else if (limit > 1)
1430                                 limit--;
1431                 }
1432
1433                 if (pb_type == TELECOM_PB)
1434                         query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1435 #ifdef PBAP_SIM_ENABLE
1436                 else if (pb_type == SIM_PB)
1437                         query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1438 #endif
1439
1440                 property_id = _contacts_person.id;
1441                 break;
1442         case TELECOM_ICH:
1443                 query = __bluetooth_pb_query_phone_log_incoming();
1444                 property_id = _contacts_phone_log.id;
1445                 attr = "RECEIVED";
1446                 break;
1447         case TELECOM_OCH:
1448                 query = __bluetooth_pb_query_phone_log_outgoing();
1449                 property_id = _contacts_phone_log.id;
1450                 attr = "DIALED";
1451                 break;
1452         case TELECOM_MCH:
1453                 query = __bluetooth_pb_query_phone_log_missed();
1454                 property_id = _contacts_phone_log.id;
1455                 attr = "MISSED";
1456                 break;
1457         case TELECOM_CCH:
1458                 query = __bluetooth_pb_query_phone_log_combined();
1459                 property_id = _contacts_phone_log.id;
1460                 get_log = TRUE;
1461                 break;
1462         default:
1463                 return;
1464         }
1465         INFO("Limit is = %d and offset is =%d\n", limit, offset);
1466
1467         /* When limit is passed as ZERO to contacts_db_get_records_with_query
1468          * API then this API will provide all available contacts in its database
1469          * (unrestricted). Now consider a case when client requests for
1470          * maxlistcount of 1 and start offset as 0 then we have already read the
1471          * owner card in above switch case and when it reads owner card it
1472          * decrements the limit by 1.
1473          */
1474         if (limit != 0) {
1475                 status = contacts_db_get_records_with_query(query, offset,
1476                                                         limit, &record_list);
1477
1478                 if (status != CONTACTS_ERROR_NONE) {
1479                         contacts_list_destroy(record_list, TRUE);
1480                         contacts_query_destroy(query);
1481                         return;
1482                 }
1483
1484                 status = contacts_list_first(record_list);
1485
1486                 if (status != CONTACTS_ERROR_NONE) {
1487                         contacts_list_destroy(record_list, TRUE);
1488                         contacts_query_destroy(query);
1489                         return;
1490                 }
1491
1492                 do {
1493                         contacts_record_h record;
1494                         gint id;
1495                         gchar *vcard = NULL;
1496
1497                         status = contacts_list_get_current_record_p(record_list,
1498                                                                 &record);
1499
1500                         if (status != CONTACTS_ERROR_NONE)
1501                                 continue;
1502                         status = contacts_record_get_int(record, property_id,
1503                                                                         &id);
1504
1505                         if (status != CONTACTS_ERROR_NONE)
1506                                 continue;
1507
1508                         if (property_id == _contacts_person.id)
1509                                 vcard = _bluetooth_pb_vcard_contact(id, filter,
1510                                                                         format);
1511                         else {
1512                                 if (get_log)
1513                                         attr = __bluetooth_pb_phone_log_get_log_type(record);
1514
1515                                 vcard = _bluetooth_pb_vcard_call(id, filter,
1516                                                                 format, attr);
1517                         }
1518
1519                         if (vcard)
1520                                 g_variant_builder_add(vcards, "s", vcard);
1521
1522                 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1523                 contacts_list_destroy(record_list, TRUE);
1524         }
1525
1526         contacts_query_destroy(query);
1527
1528         FN_END;
1529 }
1530
1531 static void __bluetooth_pb_get_contact_list(PbAgentData *agent,
1532                         contacts_query_h query, GVariantBuilder *builder)
1533 {
1534         FN_START;
1535         contacts_list_h record_list = NULL;
1536         gint status;
1537
1538         /* Add owner */
1539         if (builder) {
1540                 gchar *tmp;
1541                 gchar *name;
1542
1543                 tmp = _bluetooth_pb_owner_name();
1544                 name = g_strdup_printf("%s;;;;", tmp);
1545                 g_free(tmp);
1546
1547                 __bluetooth_pb_list_ptr_array_add(builder, name,
1548                                                         agent->tel_number, 0);
1549
1550                 g_free(name);
1551         }
1552
1553         status = contacts_db_get_records_with_query(query, -1, -1,
1554                                                                 &record_list);
1555
1556         if (status != CONTACTS_ERROR_NONE) {
1557                 contacts_list_destroy(record_list, TRUE);
1558                 return;
1559         }
1560
1561         status = contacts_list_first(record_list);
1562
1563         if (status != CONTACTS_ERROR_NONE) {
1564                 contacts_list_destroy(record_list, TRUE);
1565                 return;
1566         }
1567
1568         do {
1569                 contacts_record_h record;
1570                 gint id;
1571
1572                 status = contacts_list_get_current_record_p(record_list,
1573                                                                 &record);
1574
1575                 if (status != CONTACTS_ERROR_NONE)
1576                         continue;
1577
1578                 status = contacts_record_get_int(record,
1579                                 _contacts_person_contact.person_id, &id);
1580
1581                 if (status != CONTACTS_ERROR_NONE)
1582                         continue;
1583
1584                 /* create list */
1585                 if (builder) {
1586                         gchar *name;
1587                         gchar *number;
1588
1589                         name = _bluetooth_pb_name_from_person_id(id);
1590                         number = _bluetooth_pb_number_from_person_id(id);
1591
1592                         __bluetooth_pb_list_ptr_array_add(builder, name,
1593                                                                 number, id);
1594
1595                         g_free(name);
1596                         g_free(number);
1597                 }
1598
1599         } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1600
1601         contacts_list_destroy(record_list, TRUE);
1602         FN_END;
1603 }
1604
1605 static void __bluetooth_pb_get_phone_log_list(PbAgentData *agent,
1606                         contacts_query_h query, GVariantBuilder *builder)
1607 {
1608         FN_START;
1609         contacts_list_h record_list = NULL;
1610         gint status;
1611
1612         status = contacts_db_get_records_with_query(query, -1, -1,
1613                                                                 &record_list);
1614
1615         if (status != CONTACTS_ERROR_NONE)
1616                 return;
1617
1618         status = contacts_list_first(record_list);
1619
1620         if (status != CONTACTS_ERROR_NONE) {
1621                 contacts_list_destroy(record_list, TRUE);
1622                 return;
1623         }
1624
1625         do {
1626                 contacts_record_h record;
1627                 gint id;
1628
1629                 status = contacts_list_get_current_record_p(record_list,
1630                                                                 &record);
1631
1632                 if (status != CONTACTS_ERROR_NONE)
1633                         continue;
1634
1635                 status = contacts_record_get_int(record,
1636                                                 _contacts_phone_log.id, &id);
1637
1638                 if (status != CONTACTS_ERROR_NONE)
1639                         continue;
1640
1641                 /* create list */
1642                 if (builder) {
1643                         gchar *name;
1644                         gchar *number;
1645
1646                         name = _bluetooth_pb_name_from_phonelog_id(id);
1647
1648                         contacts_record_get_str_p(record,
1649                                         _contacts_phone_log.address, &number);
1650
1651                         __bluetooth_pb_list_ptr_array_add(builder, name,
1652                                                                 number, id);
1653
1654                         g_free(name);
1655                 }
1656
1657         } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1658
1659         contacts_list_destroy(record_list, TRUE);
1660         FN_END;
1661 }
1662
1663
1664 static void __bluetooth_pb_get_list(PbAgentData *agent, PhoneBookType pb_type,
1665                                 GVariantBuilder *builder)
1666 {
1667         FN_START;
1668         contacts_query_h query;
1669
1670         /* no requires refresh cache */
1671         if (builder == NULL && agent->pb_type == pb_type)
1672                 return;
1673
1674         switch (pb_type) {
1675         case TELECOM_PB:
1676                 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1677                 __bluetooth_pb_get_contact_list(agent, query, builder);
1678                 break;
1679         case TELECOM_ICH:
1680                 query = __bluetooth_pb_query_phone_log_incoming();
1681                 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1682                 break;
1683         case TELECOM_OCH:
1684                 query = __bluetooth_pb_query_phone_log_outgoing();
1685                 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1686                 break;
1687         case TELECOM_MCH:
1688                 query = __bluetooth_pb_query_phone_log_missed();
1689                 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1690                 break;
1691         case TELECOM_CCH:
1692                 query = __bluetooth_pb_query_phone_log_combined();
1693                 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1694                 break;
1695 #ifdef PBAP_SIM_ENABLE
1696         case SIM_PB:
1697                 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1698                 __bluetooth_pb_get_contact_list(agent, query, builder);
1699                 break;
1700 #endif
1701         default:
1702                 return;
1703         }
1704
1705         agent->pb_type = pb_type;
1706
1707         if (query)
1708                 contacts_query_destroy(query);
1709         FN_END;
1710 }
1711
1712 static void __bluetooth_pb_get_contact_list_number(PbAgentData *agent,
1713                                 contacts_query_h query, gint start_index,
1714                                 gint end_index, GVariantBuilder *builder)
1715 {
1716         FN_START;
1717         contacts_list_h record_list = NULL;
1718         gint status;
1719         gint i;
1720         gint from;
1721         gint to;
1722         gint offset;
1723
1724         from = start_index;
1725         to = end_index;
1726
1727         if (from < 1)
1728                 from = 1;
1729
1730         if (to < 1)
1731                 to = 1;
1732
1733         offset = to - from + 1;
1734         if (offset <= 0)
1735                 return;
1736
1737         i = from;
1738
1739         status = contacts_db_get_records_with_query(query, from - 1 , offset,
1740                         &record_list);
1741
1742         if (status != CONTACTS_ERROR_NONE) {
1743                 contacts_list_destroy(record_list, TRUE);
1744                 return;
1745         }
1746
1747         status = contacts_list_first(record_list);
1748
1749         if (status != CONTACTS_ERROR_NONE) {
1750                 contacts_list_destroy(record_list, TRUE);
1751                 return;
1752         }
1753
1754         do {
1755                 contacts_record_h record;
1756                 gchar *display_name;
1757                 gchar *number;
1758
1759                 status = contacts_list_get_current_record_p(record_list,
1760                                                                 &record);
1761
1762                 if (status != CONTACTS_ERROR_NONE)
1763                         continue;
1764
1765                 contacts_record_get_str_p(record,
1766                                         _contacts_person_number.display_name,
1767                                         &display_name);
1768                 contacts_record_get_str_p(record,
1769                                 _contacts_person_number.number, &number);
1770
1771                 __bluetooth_pb_list_ptr_array_add(builder, display_name,
1772                                                                 number, i);
1773
1774                 i++;
1775         } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1776
1777         contacts_list_destroy(record_list, TRUE);
1778         FN_END;
1779 }
1780
1781 static void __bluetooth_pb_get_phone_log_list_number(PbAgentData *agent,
1782                                 contacts_query_h query, gint start_index,
1783                                 gint end_index, GVariantBuilder *builder)
1784 {
1785         FN_START;
1786         contacts_list_h record_list = NULL;
1787         gint status;
1788         gint i;
1789         gint from;
1790         gint to;
1791         gint offset;
1792
1793         from = start_index;
1794         to = end_index;
1795
1796         if (from < 1)
1797                 from = 1;
1798
1799         if (to < 1)
1800                 to = 1;
1801
1802         offset = to - from + 1;
1803         if (offset <= 0)
1804                 return;
1805
1806         i = from;
1807
1808         status = contacts_db_get_records_with_query(query, from - 1 , offset,
1809                         &record_list);
1810
1811         if (status != CONTACTS_ERROR_NONE) {
1812                 contacts_list_destroy(record_list, TRUE);
1813                 return;
1814         }
1815
1816         status = contacts_list_first(record_list);
1817         if (status != CONTACTS_ERROR_NONE) {
1818                 contacts_list_destroy(record_list, TRUE);
1819                 return;
1820         }
1821
1822         do {
1823                 contacts_record_h record;
1824                 gint id;
1825                 gchar *display_name;
1826                 gchar *number;
1827
1828                 status = contacts_list_get_current_record_p(record_list,
1829                                                                 &record);
1830
1831                 if (status != CONTACTS_ERROR_NONE)
1832                         continue;
1833
1834                 status = contacts_record_get_int(record,
1835                                                 _contacts_phone_log.id, &id);
1836                 if (status != CONTACTS_ERROR_NONE) {
1837                         ERR("contact_record_get_int api failed %d", status);
1838                         continue;
1839                 }
1840
1841                 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
1842
1843                 contacts_record_get_str_p(record, _contacts_phone_log.address,
1844                                                                 &number);
1845
1846                 __bluetooth_pb_list_ptr_array_add(builder, display_name,
1847                                                                 number, i);
1848
1849                 i++;
1850
1851                 g_free(display_name);
1852
1853         } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1854
1855         contacts_list_destroy(record_list, TRUE);
1856         FN_END;
1857 }
1858
1859 static void __bluetooth_pb_get_list_number(PbAgentData *agent,
1860                                 PhoneBookType pb_type, gint start_index,
1861                                 gint end_index, GVariantBuilder *builder)
1862 {
1863         FN_START;
1864         contacts_query_h query;
1865
1866         DBG("type = %d", pb_type);
1867         switch (pb_type) {
1868         case TELECOM_PB:
1869                 query = __bluetooth_pb_query_person_number();
1870                 __bluetooth_pb_get_contact_list_number(agent, query,
1871                                 start_index, end_index, builder);
1872                 break;
1873         case TELECOM_ICH:
1874                 query = __bluetooth_pb_query_phone_log_incoming();
1875                 __bluetooth_pb_get_phone_log_list_number(agent, query,
1876                                 start_index, end_index, builder);
1877                 break;
1878         case TELECOM_OCH:
1879                 query = __bluetooth_pb_query_phone_log_outgoing();
1880                 __bluetooth_pb_get_phone_log_list_number(agent, query,
1881                                 start_index, end_index, builder);
1882                 break;
1883         case TELECOM_MCH:
1884                 query = __bluetooth_pb_query_phone_log_missed();
1885                 __bluetooth_pb_get_phone_log_list_number(agent, query,
1886                                 start_index, end_index, builder);
1887                 break;
1888         case TELECOM_CCH:
1889                 query = __bluetooth_pb_query_phone_log_combined();
1890                 __bluetooth_pb_get_phone_log_list_number(agent, query,
1891                                 start_index, end_index, builder);
1892                 break;
1893 #ifdef PBAP_SIM_ENABLE
1894         case SIM_PB:
1895                 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1896                 __bluetooth_pb_get_contact_list_number(agent, query,
1897                                 start_index, end_index, builder);
1898                 break;
1899 #endif
1900
1901         default:
1902                 return;
1903         }
1904
1905         if (query)
1906                 contacts_query_destroy(query);
1907         FN_END;
1908 }
1909
1910 static void __bluetooth_pb_get_contact_list_name(PbAgentData *agent,
1911                                 contacts_query_h query, const gchar *find_text,
1912                                 GVariantBuilder *builder)
1913 {
1914         FN_START;
1915         contacts_list_h record_list = NULL;
1916         gint status;
1917         gint i = 1;
1918
1919         status = contacts_db_get_records_with_query(query,
1920                         -1, -1, &record_list);
1921
1922         if (status != CONTACTS_ERROR_NONE) {
1923                 contacts_list_destroy(record_list, TRUE);
1924                 return;
1925         }
1926
1927         status = contacts_list_first(record_list);
1928
1929         if (status != CONTACTS_ERROR_NONE) {
1930                 contacts_list_destroy(record_list, TRUE);
1931                 return;
1932         }
1933
1934         do {
1935                 contacts_record_h record;
1936                 gchar *display_name;
1937
1938                 status = contacts_list_get_current_record_p(record_list,
1939                                                                 &record);
1940
1941                 if (status != CONTACTS_ERROR_NONE)
1942                         continue;
1943
1944                 contacts_record_get_str_p(record,
1945                         _contacts_person_number.display_name, &display_name);
1946
1947                 if (g_str_has_prefix(display_name, find_text)) {
1948                         gchar *number;
1949
1950                         contacts_record_get_str_p(record,
1951                                 _contacts_person_number.number, &number);
1952
1953                         __bluetooth_pb_list_ptr_array_add(builder, display_name,
1954                                                                 number, i);
1955                 }
1956
1957                 i++;
1958         } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1959         contacts_list_destroy(record_list, TRUE);
1960         FN_END;
1961 }
1962
1963 static void __bluetooth_pb_get_phone_log_list_name(PbAgentData *agent,
1964                                 contacts_query_h query, const gchar *find_text,
1965                                 GVariantBuilder *builder)
1966 {
1967         FN_START;
1968         contacts_list_h record_list = NULL;
1969         gint status;
1970         gint i = 1;
1971
1972         status = contacts_db_get_records_with_query(query, -1, -1,
1973                                                                 &record_list);
1974
1975         if (status != CONTACTS_ERROR_NONE) {
1976                 contacts_list_destroy(record_list, TRUE);
1977                 return;
1978         }
1979
1980         status = contacts_list_first(record_list);
1981
1982         if (status != CONTACTS_ERROR_NONE) {
1983                 contacts_list_destroy(record_list, TRUE);
1984                 return;
1985         }
1986
1987         do {
1988                 contacts_record_h record;
1989                 gint id;
1990                 gchar *display_name;
1991
1992                 status = contacts_list_get_current_record_p(record_list,
1993                                 &record);
1994
1995                 if (status != CONTACTS_ERROR_NONE)
1996                         continue;
1997
1998                 status = contacts_record_get_int(record,
1999                                                 _contacts_phone_log.id, &id);
2000                 if (status != CONTACTS_ERROR_NONE) {
2001                         ERR("contacts_record_get_int failed %d", status);
2002                         continue;
2003                 }
2004
2005                 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
2006
2007                 if (g_str_has_prefix(display_name, find_text)) {
2008                         gchar *number;
2009
2010                         contacts_record_get_str_p(record,
2011                                         _contacts_phone_log.address, &number);
2012
2013                         __bluetooth_pb_list_ptr_array_add(builder, display_name,
2014                                                                 number, i);
2015                 }
2016
2017                 i++;
2018
2019                 g_free(display_name);
2020
2021         } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
2022
2023         contacts_list_destroy(record_list, TRUE);
2024         FN_END;
2025 }
2026
2027 static void __bluetooth_pb_get_list_name(PbAgentData *agent,
2028                                 PhoneBookType pb_type, const gchar *find_text,
2029                                 GVariantBuilder *builder)
2030 {
2031         FN_START;
2032         contacts_query_h query;
2033
2034         switch (pb_type) {
2035         case TELECOM_PB:
2036                 query = __bluetooth_pb_query_person_number();
2037                 __bluetooth_pb_get_contact_list_name(agent, query,
2038                                 find_text, builder);
2039                 break;
2040         case TELECOM_ICH:
2041                 query = __bluetooth_pb_query_phone_log_incoming();
2042                 __bluetooth_pb_get_phone_log_list_name(agent, query,
2043                                 find_text, builder);
2044                 break;
2045         case TELECOM_OCH:
2046                 query = __bluetooth_pb_query_phone_log_outgoing();
2047                 __bluetooth_pb_get_phone_log_list_name(agent, query,
2048                                 find_text, builder);
2049                 break;
2050         case TELECOM_MCH:
2051                 query = __bluetooth_pb_query_phone_log_missed();
2052                 __bluetooth_pb_get_phone_log_list_name(agent, query,
2053                                 find_text, builder);
2054                 break;
2055         case TELECOM_CCH:
2056                 query = __bluetooth_pb_query_phone_log_combined();
2057                 __bluetooth_pb_get_phone_log_list_name(agent, query,
2058                                 find_text, builder);
2059                 break;
2060         default:
2061                 return;
2062         }
2063
2064         if (query)
2065                 contacts_query_destroy(query);
2066         FN_END;
2067 }
2068
2069 static void __bluetooth_pb_list_ptr_array_add(GVariantBuilder *builder,
2070                         const gchar *name, const gchar *number, gint handle)
2071 {
2072         FN_START;
2073         gchar *temp_name = g_strdup(name);
2074         gchar *temp_number = g_strdup(number);
2075
2076         g_variant_builder_add(builder, "(ssu)", temp_name, temp_number, handle);
2077
2078         g_free(temp_name);
2079         g_free(temp_number)
2080         FN_END;
2081 }
2082
2083 static void __bluetooth_pb_agent_signal_handler(int signum)
2084 {
2085         FN_START;
2086         if (g_mainloop) {
2087                 g_main_loop_quit(g_mainloop);
2088         } else {
2089                 DBG("Terminate Bluetooth PBAP agent");
2090                 exit(0);
2091         }
2092 }
2093
2094 static void __bluetooth_pb_contact_changed(const gchar *view_uri,
2095                                         void *user_data)
2096 {
2097         FN_START;
2098         guint new_missed_call;
2099         PbAgentData *agent = (PbAgentData *)user_data;
2100         GDBusConnection *conn = __bt_pb_get_gdbus_connection();
2101
2102         DBG("Received contact changed cb");
2103
2104         g_dbus_connection_emit_signal(conn, "org.bluez.pb_agent",
2105                                 "/org/bluez/pb_agent", "org.bluez.PbAgent",
2106                                 "clear", NULL, NULL);
2107
2108         bluetooth_pb_agent_clear(agent);
2109
2110         __bluetooth_pb_get_count_new_missed_call(&new_missed_call);
2111
2112         if (new_missed_call > total_missed_call_count)
2113                 unnotified_missed_call_count += new_missed_call -
2114                                                         total_missed_call_count;
2115
2116         INFO("Missed call count : #prev[%d], #current[%d], #unnotified[%d]",
2117                 total_missed_call_count, new_missed_call,
2118                                                 unnotified_missed_call_count);
2119
2120         total_missed_call_count = new_missed_call;
2121         FN_END;
2122 }
2123
2124 static void __bluetooth_pb_agent_timeout_add_seconds(PbAgentData *agent)
2125 {
2126         FN_START;
2127
2128         if (agent->timeout_id)
2129                 g_source_remove(agent->timeout_id);
2130
2131         agent->timeout_id = g_timeout_add_seconds(BLUETOOTH_PB_AGENT_TIMEOUT,
2132                                 __bluetooth_pb_agent_timeout_calback, agent);
2133         FN_END;
2134 }
2135
2136 static gboolean __bluetooth_pb_agent_timeout_calback(gpointer user_data)
2137 {
2138         FN_START;
2139         PbAgentData *agent = (PbAgentData *)user_data;
2140
2141         agent->timeout_id = 0;
2142
2143         if (g_mainloop)
2144                 g_main_loop_quit(g_mainloop);
2145
2146         FN_END;
2147         return FALSE;
2148 }
2149
2150 static void __bluetooth_pb_tel_callback(TapiHandle *handle, int result,
2151                                         void *data, void *user_data)
2152 {
2153         FN_START;
2154         PbAgentData *agent = (PbAgentData *)user_data;
2155         TelSimMsisdnList_t *number;
2156
2157         __bt_pb_dbus_init(agent);
2158
2159         if (data != NULL) {
2160                 number = (TelSimMsisdnList_t *)data;
2161                 agent->tel_number = g_strdup(number->list[0].num);
2162         }
2163
2164         tel_deinit(agent->tapi_handle);
2165         agent->tapi_handle = NULL;
2166         FN_END;
2167 }
2168
2169 static gboolean __bt_pb_destroy_agent()
2170 {
2171         FN_START;
2172         g_main_loop_quit(g_mainloop);
2173         FN_END;
2174         return TRUE;
2175 }
2176
2177 static GDBusNodeInfo *__bt_pb_create_method_node_info
2178                                         (const gchar *introspection_data)
2179 {
2180         GError *err = NULL;
2181         GDBusNodeInfo *node_info = NULL;
2182
2183         if (introspection_data == NULL)
2184                 return NULL;
2185
2186         node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
2187
2188         if (err) {
2189                 ERR("Unable to create node: %s", err->message);
2190                 g_clear_error(&err);
2191         }
2192         return node_info;
2193 }
2194
2195 static gboolean __bt_pb_dbus_init(PbAgentData *agent)
2196 {
2197         guint owner_id = 0;
2198         guint pb_id;
2199         GDBusNodeInfo *node_info = NULL;
2200         GError *error = NULL;
2201         GDBusConnection *conn = __bt_pb_get_gdbus_connection();
2202
2203         if (conn == NULL) {
2204                 ERR("Error in creating the gdbus connection");
2205                 goto fail;
2206         }
2207
2208         owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, BT_PB_SERVICE_NAME,
2209                                         G_BUS_NAME_OWNER_FLAGS_NONE, NULL,
2210                                         NULL, NULL, NULL, NULL);
2211         if (owner_id == 0)
2212                 goto fail;
2213
2214         node_info = __bt_pb_create_method_node_info(pb_agent_introspection_xml);
2215         if (node_info == NULL)
2216                 goto fail;
2217
2218         pb_id = g_dbus_connection_register_object(conn, BT_PB_SERVICE_OBJECT_PATH,
2219                                         node_info->interfaces[0], &method_table,
2220                                         agent, NULL, &error);
2221
2222         if (pb_id == 0) {
2223                 ERR("Failed to register: %s", error->message);
2224                 g_clear_error(&error);
2225                 goto fail;
2226         }
2227
2228         agent->pbagent_interface_id = pb_id;
2229
2230         pb_id = g_dbus_connection_register_object(conn, BT_PB_SERVICE_OBJECT_PATH,
2231                                         node_info->interfaces[1], &method_table,
2232                                         agent, NULL, &error);
2233
2234         if (pb_id == 0) {
2235                 ERR("Failed to register: %s", error->message);
2236                 g_clear_error(&error);
2237                 goto fail;
2238         }
2239
2240         g_dbus_node_info_unref(node_info);
2241
2242         agent->pbagent_at_interface_id = pb_id;
2243
2244         return TRUE;
2245
2246 fail:
2247         if (pb_dbus_conn) {
2248                 g_object_unref(pb_dbus_conn);
2249                 pb_dbus_conn = NULL;
2250         }
2251
2252         if (conn)
2253                 g_object_unref(conn);
2254
2255         if (node_info)
2256                 g_dbus_node_info_unref(node_info);
2257
2258         if (owner_id > 0)
2259                 g_bus_unown_name(owner_id);
2260
2261         return FALSE;
2262 }
2263
2264 static gboolean __bt_pb_dbus_deinit(PbAgentData *agent)
2265 {
2266         if (pb_dbus_conn) {
2267                 if (agent->pbagent_interface_id != 0) {
2268                         g_dbus_connection_unregister_object(pb_dbus_conn,
2269                                                 agent->pbagent_interface_id);
2270                         agent->pbagent_interface_id = 0;
2271                 }
2272                 if (agent->pbagent_at_interface_id != 0) {
2273                         g_dbus_connection_unregister_object(pb_dbus_conn,
2274                                                 agent->pbagent_at_interface_id);
2275                         agent->pbagent_at_interface_id = 0;
2276                 }
2277                 if (pb_dbus_conn) {
2278                         g_object_unref(pb_dbus_conn);
2279                         pb_dbus_conn = NULL;
2280                 }
2281                 return TRUE;
2282         }
2283         return FALSE;
2284 }
2285
2286 int main(void)
2287 {
2288         FN_START;
2289         PbAgentData *agent;
2290         gint ret = EXIT_SUCCESS;
2291         gint tapi_result;
2292         struct sigaction sa;
2293
2294         DBG("Starting Bluetooth PBAP agent");
2295
2296         g_mainloop = g_main_loop_new(NULL, FALSE);
2297         if (g_mainloop == NULL) {
2298                 ERR("Couldn't create GMainLoop\n");
2299                 return EXIT_FAILURE;
2300         }
2301
2302         agent = (PbAgentData *)g_malloc(sizeof(PbAgentData));
2303
2304         if (agent == NULL)
2305                 return EXIT_FAILURE;
2306
2307         agent->pbagent_interface_id = 0;
2308         agent->pbagent_at_interface_id = 0;
2309         agent->tel_number = NULL;
2310
2311         /* connect contact */
2312         if (contacts_connect() != CONTACTS_ERROR_NONE) {
2313                 ERR("Can not connect contacts server\n");
2314                 g_free(agent);
2315                 return EXIT_FAILURE;
2316         }
2317
2318         __bluetooth_pb_get_count_new_missed_call(&total_missed_call_count);
2319
2320         if (contacts_db_add_changed_cb(_contacts_contact._uri,
2321                         __bluetooth_pb_contact_changed,
2322                         (void *)agent) != CONTACTS_ERROR_NONE) {
2323                 ERR("Can not add changed callback");
2324         }
2325
2326         if (contacts_db_add_changed_cb(_contacts_phone_log._uri,
2327                         __bluetooth_pb_contact_changed,
2328                         (void *)agent) != CONTACTS_ERROR_NONE) {
2329                 ERR("Can not add changed callback");
2330         }
2331
2332         /* set signal */
2333         memset(&sa, 0, sizeof(sa));
2334         sa.sa_handler = __bluetooth_pb_agent_signal_handler;
2335         sigaction(SIGTERM, &sa, NULL);
2336         sigaction(SIGINT, &sa, NULL);
2337
2338         /* init tapi */
2339         agent->tapi_handle = tel_init(NULL);
2340         tapi_result = tel_get_sim_msisdn(agent->tapi_handle,
2341                         __bluetooth_pb_tel_callback, agent);
2342
2343         if (tapi_result != TAPI_API_SUCCESS) {
2344                 __bt_pb_dbus_init(agent);
2345         }
2346
2347         __bluetooth_pb_agent_timeout_add_seconds(agent);
2348
2349         g_main_loop_run(g_mainloop);
2350
2351         if (contacts_db_remove_changed_cb(_contacts_phone_log._uri,
2352                         __bluetooth_pb_contact_changed,
2353                         (void *)agent) != CONTACTS_ERROR_NONE) {
2354                 ERR("Cannot remove changed callback");
2355         }
2356
2357         if (contacts_db_remove_changed_cb(_contacts_contact._uri,
2358                         __bluetooth_pb_contact_changed,
2359                         (void *)agent) != CONTACTS_ERROR_NONE) {
2360                 ERR("Cannot remove changed callback");
2361         }
2362
2363         if (contacts_disconnect() != CONTACTS_ERROR_NONE)
2364                 ERR("contacts_disconnect failed \n");
2365
2366         g_dbus_connection_emit_signal(pb_dbus_conn, "org.bluez.pb_agent",
2367                                 "/org/bluez/pb_agent", "org.bluez.PbAgent",
2368                                 "clear", NULL, NULL);
2369
2370         bluetooth_pb_agent_clear(agent);
2371
2372         if (agent->tapi_handle)
2373                 tel_deinit(agent->tapi_handle);
2374         if (agent->tel_number)
2375                 g_free(agent->tel_number);
2376
2377         __bt_pb_dbus_deinit(agent);
2378
2379         g_free(agent);
2380
2381         DBG("Terminate Bluetooth PBAP agent");
2382         FN_END;
2383         return ret;
2384 }