4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
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>
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
32 #include <sys/types.h>
36 #include <TapiUtility.h>
39 #include "bluetooth_pb_agent.h"
40 #include "bluetooth_pb_vcard.h"
42 #define BLUETOOTH_PB_AGENT_TIMEOUT 600
45 TapiHandle *tapi_handle;
48 PhoneBookType pb_type;
49 guint pbagent_interface_id;
50 guint pbagent_at_interface_id;
53 static gchar *bluetooth_pb_agent_folder_list[] = {
59 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
65 static guint total_missed_call_count = 0;
66 static guint unnotified_missed_call_count = 0;
68 GMainLoop *g_mainloop;
69 static GDBusConnection *pb_dbus_conn = NULL;
71 static const gchar pb_agent_introspection_xml[] =
73 "<interface name='org.bluez.PbAgent'>"
74 "<method name='GetPhonebookFolderList'>"
75 "<arg type='as' name='folder_list' direction='out'/>"
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'/>"
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'/>"
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'/>"
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'/>"
108 "<method name='GetTotalObjectCount'>"
109 "<arg type='s' name='path'/>"
110 "<arg type='u' name='phonebook_size' direction='out'/>"
113 "<method name='AddContact'>"
114 "<arg type='s' name='filename'/>"
117 "<method name='DestroyAgent'>"
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'/>"
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'/>"
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'/>"
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,
148 static GVariant *__bt_pb_get_phonebook_folder_list(GError **error);
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);
154 static GVariant *__bt_pb_get_phonebook_size(PbAgentData *agent,
155 const char *name, GError **err);
157 static GVariant *__bt_pb_get_phonebook_list(PbAgentData *agent,
158 const char *name, GError **err);
160 static GVariant *__bt_pb_get_phonebook_entry(PbAgentData *agent,
161 const gchar *folder, const gchar *id, guint64 filter,
162 guint8 format, GError **err);
164 static GVariant *__bt_pb_get_phonebook_size_at(PbAgentData *agent,
165 const gchar *command, GError **err);
167 static GVariant *__bt_pb_get_phonebook_entries_at(PbAgentData *agent,
168 const gchar *command, gint32 start_index,
169 gint32 end_index, GError **err);
171 static GVariant *__bt_pb_get_phonebook_entries_find_at(PbAgentData *agent,
172 const gchar *command, const gchar *find_text,
175 static GVariant *__bt_pb_get_total_object_count(PbAgentData *agent,
176 gchar *path, GError **err);
178 static gboolean __bt_pb_add_contact(PbAgentData *agent, const char *filename,
181 static gboolean __bt_pb_destroy_agent();
183 static GError *__bt_pb_error(gint error_code, const gchar *error_message);
185 static PhoneBookType __bluetooth_pb_get_pb_type(const char *name);
187 static PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name);
189 static gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
190 gint *match, gint size);
192 static contacts_query_h __bluetooth_pb_query_phone_log(gint *match, gint size);
194 static contacts_query_h __bluetooth_pb_query_person(int addressbook);
196 static contacts_query_h __bluetooth_pb_query_person_number(void);
198 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void);
200 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void);
202 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void);
204 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void);
206 static gboolean __bluetooth_pb_get_count(PhoneBookType pb_type, guint *count);
208 static gboolean __bluetooth_pb_get_count_new_missed_call(guint *count);
210 static const char *__bluetooth_pb_phone_log_get_log_type(contacts_record_h record);
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);
216 static void __bluetooth_pb_get_contact_list(PbAgentData *agent,
217 contacts_query_h query, GVariantBuilder *builder);
219 static void __bluetooth_pb_get_phone_log_list(PbAgentData *agent,
220 contacts_query_h query, GVariantBuilder *builder);
222 static void __bluetooth_pb_get_list(PbAgentData *agent, PhoneBookType pb_type,
223 GVariantBuilder *builder);
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);
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);
233 static void __bluetooth_pb_get_list_number(PbAgentData *agent,
234 PhoneBookType pb_type, gint start_index,
235 gint end_index, GVariantBuilder *builder);
237 static void __bluetooth_pb_get_contact_list_name(PbAgentData *agent,
238 contacts_query_h query, const gchar *find_text,
239 GVariantBuilder *builder);
241 static void __bluetooth_pb_get_phone_log_list_name(PbAgentData *agent,
242 contacts_query_h query, const gchar *find_text,
243 GVariantBuilder *builder);
245 static void __bluetooth_pb_get_list_name(PbAgentData *agent,
246 PhoneBookType pb_type, const gchar *find_text,
247 GVariantBuilder *builder);
249 static void __bluetooth_pb_list_ptr_array_add(GVariantBuilder *builder,
250 const gchar *name, const gchar *number, gint handle);
252 static void __bluetooth_pb_agent_signal_handler(int signum);
254 static void __bluetooth_pb_contact_changed(const gchar *view_uri,
257 static void __bluetooth_pb_agent_timeout_add_seconds(PbAgentData *agent);
259 static gboolean __bluetooth_pb_agent_timeout_calback(gpointer user_data);
261 static void __bluetooth_pb_tel_callback(TapiHandle *handle, int result,
262 void *data, void *user_data);
264 static gboolean __bt_pb_dbus_init(PbAgentData *agent);
266 static gboolean __bt_pb_dbus_deinit(PbAgentData *agent);
268 static const GDBusInterfaceVTable method_table = {
269 __bt_pb_agent_method,
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,
281 INFO("method: %s; object_path: %s", method_name, object_path);
282 PbAgentData *agent = (PbAgentData *)user_data;
284 if (g_strcmp0(interface_name, "org.bluez.PbAgent") == 0) {
285 if (g_strcmp0(method_name, "GetPhonebookFolderList") == 0) {
286 GVariant *folder_list = NULL;
288 folder_list = __bt_pb_get_phonebook_folder_list(&err);
291 g_dbus_method_invocation_return_value(invocation,
293 } else if (g_strcmp0(method_name, "GetPhonebook") == 0) {
294 GVariant *phonebook = NULL;
298 guint16 max_list_count;
299 guint16 list_start_offset;
301 g_variant_get(parameters, "(&styqq)", &name, &filter,
302 &format, &max_list_count,
304 phonebook = __bt_pb_get_phonebook(agent, name, filter,
305 format, max_list_count,
306 list_start_offset, &err);
309 g_dbus_method_invocation_return_value(invocation,
311 } else if (g_strcmp0(method_name, "GetPhonebookSize") == 0) {
312 GVariant *phonebook_size = NULL;
315 g_variant_get(parameters, "(&s)", &name);
316 phonebook_size = __bt_pb_get_phonebook_size(agent, name,
320 g_dbus_method_invocation_return_value(invocation,
322 } else if (g_strcmp0(method_name, "GetPhonebookList") == 0) {
323 GVariant *phonebook_list = NULL;
326 g_variant_get(parameters, "(&s)", &name);
327 phonebook_list = __bt_pb_get_phonebook_list(agent, name,
331 g_dbus_method_invocation_return_value(invocation,
333 } else if (g_strcmp0(method_name, "GetPhonebookEntry") == 0) {
334 GVariant *phonebook_entry = NULL;
340 g_variant_get(parameters, "(&s&sty)", &folder, &id,
342 phonebook_entry = __bt_pb_get_phonebook_entry(agent,
343 folder, id, filter, format, &err);
346 g_dbus_method_invocation_return_value(invocation,
348 } else if (g_strcmp0(method_name, "GetTotalObjectCount") == 0) {
349 GVariant *phonebook_size = NULL;
352 g_variant_get(parameters, "(&s)", &path);
353 phonebook_size = __bt_pb_get_total_object_count(agent,
357 g_dbus_method_invocation_return_value(invocation,
359 } else if (g_strcmp0(method_name, "AddContact") == 0) {
360 const char *filename;
362 g_variant_get(parameters, "(&s)", &filename);
363 __bt_pb_add_contact(agent, filename, &err);
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();
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;
376 g_variant_get(parameters, "(&s)", &command);
377 phonebook_size = __bt_pb_get_phonebook_size_at(agent,
381 g_dbus_method_invocation_return_value(invocation,
383 } else if (g_strcmp0(method_name,
384 "GetPhonebookEntriesAt") == 0) {
385 GVariant *phonebook_entries = NULL;
386 const gchar *command;
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,
397 g_dbus_method_invocation_return_value(invocation,
399 } else if (g_strcmp0(method_name,
400 "GetPhonebookEntriesFindAt") == 0) {
401 GVariant *phonebook_entries = NULL;
402 const gchar *command;
403 const gchar *find_text;
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);
410 g_dbus_method_invocation_return_value(invocation,
419 g_dbus_method_invocation_return_gerror(invocation, err);
425 static void bluetooth_pb_agent_clear(PbAgentData *agent)
428 agent->pb_type = TELECOM_NONE;
432 static GDBusConnection *__bt_pb_get_gdbus_connection(void)
440 pb_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
443 ERR("Unable to connect to dbus: %s", err->message);
452 static GVariant *__bt_pb_get_phonebook_folder_list(GError **error)
455 GVariant *folder_list;
458 GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
460 size = G_N_ELEMENTS(bluetooth_pb_agent_folder_list);
462 for (i = 0; i < size; i++)
463 g_variant_builder_add(builder, "s",
464 bluetooth_pb_agent_folder_list[i]);
466 folder_list = g_variant_new("(as)", builder);
467 g_variant_builder_unref(builder);
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)
480 PhoneBookType pb_type = TELECOM_NONE;
481 GVariantBuilder *vcards = g_variant_builder_new(G_VARIANT_TYPE("as"));
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);
486 __bluetooth_pb_agent_timeout_add_seconds(agent);
488 pb_type = __bluetooth_pb_get_pb_type(name);
490 if (pb_type == TELECOM_NONE) {
491 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
492 "unsupported name defined");
496 if (max_list_count > 0) {
497 __bluetooth_pb_get_vcards(agent, pb_type, filter, format,
498 max_list_count, list_start_offset, vcards);
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;
508 phonebook = g_variant_new("(asu)", vcards, 0);
511 g_variant_builder_unref(vcards);
517 static GVariant *__bt_pb_get_phonebook_size(PbAgentData *agent,
518 const char *name, GError **err)
521 GVariant *phonebook_size;
522 PhoneBookType pb_type = TELECOM_NONE;
525 DBG_SECURE("name: %s\n", name);
527 __bluetooth_pb_agent_timeout_add_seconds(agent);
529 pb_type = __bluetooth_pb_get_pb_type(name);
531 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
532 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
533 "unsupported name defined");
538 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
539 if (pb_type == TELECOM_PB || pb_type == SIM_PB)
542 if (pb_type == TELECOM_PB)
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;
552 phonebook_size = g_variant_new("(uu)", count, 0);
556 return phonebook_size;
560 static GVariant *__bt_pb_get_phonebook_list(PbAgentData *agent,
561 const char *name, GError **err)
564 GVariant *phonebook_list;
565 PhoneBookType pb_type = TELECOM_NONE;
567 GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("a(ssu)"));
569 DBG_SECURE("name: %s\n", name);
571 __bluetooth_pb_agent_timeout_add_seconds(agent);
573 pb_type = __bluetooth_pb_get_pb_type(name);
575 if (pb_type == TELECOM_NONE) {
576 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
577 "unsupported name defined");
581 __bluetooth_pb_get_list(agent, pb_type, builder);
583 INFO("pb_type[%d] / number of missed_call[%d]", pb_type,
584 unnotified_missed_call_count);
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;
593 phonebook_list = g_variant_new("(a(ssu)u)", builder, 0);
597 g_variant_builder_unref(builder);
600 return phonebook_list;
603 static GVariant *__bt_pb_get_phonebook_entry(PbAgentData *agent,
604 const gchar *folder, const gchar *id, guint64 filter,
605 guint8 format, GError **err)
608 GVariant *phonebook_entry;
609 PhoneBookType pb_type = TELECOM_NONE;
612 const gchar *attr = NULL;
614 DBG_SECURE("folder: %s id: %s filter: %ld format: %d\n",
615 folder, id, filter, format);
617 __bluetooth_pb_agent_timeout_add_seconds(agent);
619 if (!g_str_has_suffix(id, ".vcf")) {
620 *err = __bt_pb_error(G_FILE_ERROR_INVAL, "invalid vcf file");
624 handle = (gint)g_ascii_strtoll(id, NULL, 10);
626 pb_type = __bluetooth_pb_get_pb_type(folder);
628 if (pb_type == TELECOM_NONE) {
629 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
630 "unsupported name defined");
634 /* create index cache */
635 __bluetooth_pb_get_list(agent, pb_type, NULL);
640 str = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
643 if (_bluetooth_get_contact_addressbook(handle) == PBAP_ADDRESSBOOK_PHONE)
644 str = _bluetooth_pb_vcard_contact(handle,
650 str = _bluetooth_pb_vcard_call(handle, filter, format,
654 str = _bluetooth_pb_vcard_call(handle, filter, format, "DIALED");
657 str = _bluetooth_pb_vcard_call(handle, filter, format, "MISSED");
660 contacts_record_h record = NULL;
664 status = contacts_db_get_record(_contacts_phone_log._uri,
667 if (status != CONTACTS_ERROR_NONE)
670 attr = __bluetooth_pb_phone_log_get_log_type(record);
671 str = _bluetooth_pb_vcard_call(handle, filter, format, attr);
673 contacts_record_destroy(record, TRUE);
676 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
679 str = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
682 if (_bluetooth_get_contact_addressbook(handle) == PBAP_ADDRESSBOOK_SIM)
683 str = _bluetooth_pb_vcard_contact(handle,
689 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
690 "unsupported name defined");
694 phonebook_entry = g_variant_new("(s)", str);
699 return phonebook_entry;
702 static GVariant *__bt_pb_get_phonebook_size_at(PbAgentData *agent,
703 const gchar *command, GError **err)
706 GVariant *phonebook_size;
707 PhoneBookType pb_type = TELECOM_NONE;
710 DBG("command: %s\n", command);
712 __bluetooth_pb_agent_timeout_add_seconds(agent);
714 pb_type = __bluetooth_pb_get_storage_pb_type(command);
716 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
717 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
718 "unsupported name defined");
722 phonebook_size = g_variant_new("(u)", count);
725 return phonebook_size;
728 static GVariant *__bt_pb_get_phonebook_entries_at(PbAgentData *agent,
729 const gchar *command, gint32 start_index,
730 gint32 end_index, GError **err)
733 GVariant *phonebook_entries;
734 PhoneBookType pb_type = TELECOM_NONE;
735 GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("a(ssu)"));
737 DBG("command: %s, start_index: %d, end_index: %d\n",
738 command, start_index, end_index);
740 __bluetooth_pb_agent_timeout_add_seconds(agent);
742 pb_type = __bluetooth_pb_get_storage_pb_type(command);
744 if (pb_type == TELECOM_NONE || pb_type == TELECOM_CCH) {
745 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
746 "unsupported name defined");
750 __bluetooth_pb_get_list_number(agent, pb_type,
751 start_index, end_index, builder);
753 phonebook_entries = g_variant_new("(a(ssu))", builder);
755 g_variant_builder_unref(builder);
758 return phonebook_entries;
761 static GVariant *__bt_pb_get_phonebook_entries_find_at(PbAgentData *agent,
762 const gchar *command, const gchar *find_text,
766 GVariant *phonebook_entries;
767 PhoneBookType pb_type = TELECOM_NONE;
768 GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("a(ssu)"));
770 DBG("command: %s, find text: %s\n", command, find_text);
772 __bluetooth_pb_agent_timeout_add_seconds(agent);
774 pb_type = __bluetooth_pb_get_storage_pb_type(command);
776 if (pb_type == TELECOM_NONE || pb_type == TELECOM_CCH) {
777 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
778 "unsupported name defined");
782 __bluetooth_pb_get_list_name(agent, pb_type, find_text, builder);
784 phonebook_entries = g_variant_new("(a(ssu))", builder);
787 g_variant_builder_unref(builder);
790 return phonebook_entries;
793 static GVariant *__bt_pb_get_total_object_count(PbAgentData *agent,
794 gchar *path, GError **err)
797 GVariant *phonebook_size;
799 PhoneBookType pb_type = TELECOM_NONE;
801 __bluetooth_pb_agent_timeout_add_seconds(agent);
803 pb_type = __bluetooth_pb_get_storage_pb_type(path);
805 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
806 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
807 "unsupported name defined");
811 phonebook_size = g_variant_new("(u)", count);
814 return phonebook_size;
818 static int __bluetooth_pb_agent_read_file(const char *file_path, char **stream)
823 int received_file_size = 0;
824 struct stat file_attr;
826 if (file_path == NULL || stream == NULL) {
827 ERR("Invalid data \n");
831 DBG_SECURE("file_path = %s\n", file_path);
833 if ((fp = fopen(file_path, "r+")) == NULL) {
834 ERR_SECURE("Cannot open %s\n", file_path);
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,
843 if (received_file_size <= 0) {
844 ERR_SECURE("Some problem in the file size [%s] \n",
851 *stream = (char *)malloc(sizeof(char) *received_file_size);
852 if (NULL == *stream) {
858 ERR_SECURE("Some problem in the file [%s] \n", file_path);
864 read_len = fread(*stream, 1, received_file_size, fp);
871 DBG_SECURE("Cannot open %s\n", file_path);
884 static gboolean __bt_pb_add_contact(PbAgentData *agent, const char *filename,
888 /* Contact API is changed, Temporary blocked */
890 CTSstruct *contact_record = NULL;
891 GSList *numbers_list = NULL, *cursor;
893 int is_duplicated = 0;
897 DBG_SECURE("file_path = %s\n", filename);
899 err = contacts_svc_connect();
900 ERR("contact_db_service_connect fucntion call [error] = %d \n", err);
902 err = __bluetooth_pb_agent_read_file(filename, &stream);
905 contacts_svc_disconnect();
906 ERR("contacts_svc_disconnect fucntion call [error] = %d \n", err);
908 if (NULL != stream) {
915 is_success = contacts_svc_get_contact_from_vcard((const void *)stream,
918 DBG("contacts_svc_get_contact_from_vcard fucntion call [is_success] = %d \n", is_success);
920 if (0 == is_success) {
921 contacts_svc_struct_get_list(contact_record, CTS_CF_NUMBER_LIST,
923 cursor = numbers_list;
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;
934 if (is_duplicated == FALSE)
935 contacts_svc_insert_contact(0, contact_record);
940 err = contacts_svc_disconnect();
941 ERR("contacts_svc_disconnect fucntion call [error] = %d \n", err);
943 if (NULL != stream) {
952 /* Create GError from error code and error message*/
953 static GError *__bt_pb_error(gint error_code, const gchar *error_message)
955 return g_error_new(g_quark_from_string("PB Agent"),
956 error_code, "PB Agent Error: %s", error_message);
959 static PhoneBookType __bluetooth_pb_get_pb_type(const char *name)
962 gchar *suffix = ".vcf";
972 if (g_str_has_suffix(name, suffix))
973 len -= strlen(suffix);
975 size = G_N_ELEMENTS(bluetooth_pb_agent_folder_list) - 1;
976 for (i = 0; i < size; i++) {
977 if (strncmp(name, bluetooth_pb_agent_folder_list[i], len) == 0)
985 static PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name)
991 if (g_strcmp0(name, "\"ME\"") == 0)
994 if (g_strcmp0(name, "\"RC\"") == 0)
997 if (g_strcmp0(name, "\"DC\"") == 0)
1000 if (g_strcmp0(name, "\"MC\"") == 0)
1002 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1003 if (g_strcmp0(name, "\"SM\"") == 0)
1007 return TELECOM_NONE;
1010 static gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
1011 gint *match, gint size)
1017 for (i = 0; i < size; i++) {
1020 status = contacts_filter_add_operator(filter,
1021 CONTACTS_FILTER_OPERATOR_OR);
1023 if (status != CONTACTS_ERROR_NONE)
1027 status = contacts_filter_add_int(filter,
1028 _contacts_phone_log.log_type,
1029 CONTACTS_MATCH_EQUAL, match[i]);
1031 if (status != CONTACTS_ERROR_NONE)
1036 return CONTACTS_ERROR_NONE;
1039 static contacts_query_h __bluetooth_pb_query_phone_log(gint *match, gint size)
1042 contacts_query_h query = NULL;
1043 contacts_filter_h filter = NULL;
1046 status = contacts_query_create(_contacts_phone_log._uri, &query);
1048 if (status != CONTACTS_ERROR_NONE)
1051 status = contacts_filter_create(_contacts_phone_log._uri, &filter);
1053 if (status != CONTACTS_ERROR_NONE) {
1054 contacts_query_destroy(query);
1058 status = __bluetooth_pb_phone_log_filter_append(filter, match, size);
1060 if (status != CONTACTS_ERROR_NONE) {
1061 contacts_filter_destroy(filter);
1062 contacts_query_destroy(query);
1066 status = contacts_query_set_filter(query, filter);
1068 if (status != CONTACTS_ERROR_NONE) {
1069 contacts_filter_destroy(filter);
1070 contacts_query_destroy(query);
1074 status = contacts_query_set_sort(query, _contacts_phone_log.log_time,
1077 if (status != CONTACTS_ERROR_NONE) {
1078 contacts_filter_destroy(filter);
1079 contacts_query_destroy(query);
1083 contacts_filter_destroy(filter);
1089 bool __bt_is_matching_addressbook(const char *addressbook_name, int addressbook)
1091 bool is_sim_addressbook = _bt_is_sim_addressbook(addressbook_name);
1093 if ((is_sim_addressbook == false
1094 && addressbook == PBAP_ADDRESSBOOK_PHONE) ||
1095 (is_sim_addressbook == true
1096 && addressbook == PBAP_ADDRESSBOOK_SIM))
1102 static contacts_query_h __bluetooth_pb_query_person(int addressbook)
1105 contacts_query_h query = NULL;
1106 contacts_filter_h filter = NULL;
1107 contacts_list_h recordList = NULL;
1108 contacts_record_h record = NULL;
1109 char *addressbook_name = NULL;
1110 int address_book_id = 0;
1114 bool is_first_condition = true;
1116 DBG("Addressbook [%d]", addressbook);
1118 status = contacts_query_create(_contacts_person_contact._uri, &query);
1120 ERR("Could not create query");
1124 /* Create addressbook Filter*/
1125 status = contacts_db_get_all_records(_contacts_address_book._uri, 0, 0,
1127 if (status != CONTACTS_ERROR_NONE)
1128 ERR("Contact list get api failed %d", status);
1130 contacts_filter_create(_contacts_person_contact._uri, &filter);
1131 contacts_list_get_count(recordList, &count);
1133 for (i = 0; i < count; i++) {
1134 status = contacts_list_get_current_record_p(recordList, &record);
1135 if (status != CONTACTS_ERROR_NONE) {
1136 ERR("Contact list get api failed %d", status);
1139 status = contacts_record_get_str_p(record,
1140 _contacts_address_book.name,
1142 if (status != CONTACTS_ERROR_NONE) {
1143 ERR("Contact record get api failed %d", status);
1146 status = contacts_record_get_int(record,
1147 _contacts_address_book.id,
1149 if (status != CONTACTS_ERROR_NONE) {
1150 ERR("contacts record get int api failed %d", status);
1154 DBG("Addressbook ID: [%d] Addressbook Name: [%s]",
1155 address_book_id, addressbook_name);
1157 if (__bt_is_matching_addressbook(addressbook_name,
1159 if (is_first_condition)
1160 is_first_condition = false;
1162 contacts_filter_add_operator(filter,
1163 CONTACTS_FILTER_OPERATOR_OR);
1164 DBG("SELECTED Addressbook ID: [%d] Addressbook Name: [%s]",
1165 address_book_id, addressbook_name);
1166 status = contacts_filter_add_int(filter,
1167 _contacts_person_contact.address_book_id,
1168 CONTACTS_MATCH_EQUAL, address_book_id);
1169 if (status != CONTACTS_ERROR_NONE)
1170 ERR("Contact filter add failed %d", status);
1173 if (contacts_list_next(recordList) != CONTACTS_ERROR_NONE)
1177 contacts_list_destroy(recordList, true);
1179 status = contacts_query_set_filter(query, filter);
1180 if (status != CONTACTS_ERROR_NONE)
1181 ERR("Could not Apply Filter");
1183 contacts_filter_destroy(filter);
1188 static contacts_query_h __bluetooth_pb_query_person_number(void)
1191 contacts_query_h query = NULL;
1194 status = contacts_query_create(_contacts_person_number._uri, &query);
1196 if (status != CONTACTS_ERROR_NONE)
1203 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void)
1208 CONTACTS_PLOG_TYPE_VOICE_INCOMING,
1209 CONTACTS_PLOG_TYPE_VIDEO_INCOMING,
1210 CONTACTS_PLOG_TYPE_VOICE_REJECT,
1211 CONTACTS_PLOG_TYPE_VIDEO_REJECT
1215 return __bluetooth_pb_query_phone_log(match, size);
1218 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void)
1223 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1224 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING
1228 return __bluetooth_pb_query_phone_log(match, size);
1231 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void)
1236 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1237 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN,
1238 CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN,
1239 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN
1243 return __bluetooth_pb_query_phone_log(match, size);
1246 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void)
1251 CONTACTS_PLOG_TYPE_VOICE_INCOMING,
1252 CONTACTS_PLOG_TYPE_VIDEO_INCOMING,
1253 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1254 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING,
1255 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1256 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN,
1257 CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN,
1258 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN,
1259 CONTACTS_PLOG_TYPE_VOICE_REJECT,
1260 CONTACTS_PLOG_TYPE_VIDEO_REJECT
1264 return __bluetooth_pb_query_phone_log(match, size);
1267 static gboolean __bluetooth_pb_get_count(PhoneBookType pb_type,
1271 contacts_query_h query = NULL;
1277 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1280 query = __bluetooth_pb_query_phone_log_incoming();
1283 query = __bluetooth_pb_query_phone_log_outgoing();
1286 query = __bluetooth_pb_query_phone_log_missed();
1289 query = __bluetooth_pb_query_phone_log_combined();
1291 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1293 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1303 status = contacts_db_get_count_with_query(query, &signed_count);
1305 if (status != CONTACTS_ERROR_NONE) {
1306 contacts_query_destroy(query);
1310 contacts_query_destroy(query);
1312 if (signed_count < 0)
1315 *count = (gint) signed_count;
1321 static gboolean __bluetooth_pb_get_count_new_missed_call(guint *count)
1324 contacts_query_h query = NULL;
1329 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1330 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN
1333 query = __bluetooth_pb_query_phone_log(match, size);
1338 status = contacts_db_get_count_with_query(query, &signed_count);
1340 if (status != CONTACTS_ERROR_NONE) {
1341 contacts_query_destroy(query);
1345 contacts_query_destroy(query);
1347 if (signed_count < 0)
1350 *count = (guint)signed_count;
1356 static const char *__bluetooth_pb_phone_log_get_log_type(contacts_record_h record)
1362 status = contacts_record_get_int(record, _contacts_phone_log.log_type,
1365 if (status != CONTACTS_ERROR_NONE)
1369 case CONTACTS_PLOG_TYPE_VOICE_INCOMING:
1370 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING:
1371 case CONTACTS_PLOG_TYPE_VOICE_REJECT:
1372 case CONTACTS_PLOG_TYPE_VIDEO_REJECT:
1374 case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
1375 case CONTACTS_PLOG_TYPE_VIDEO_OUTGOING:
1377 case CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN:
1378 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN:
1379 case CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN:
1380 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN:
1388 static void __bluetooth_pb_get_vcards(PbAgentData *agent, PhoneBookType pb_type,
1389 guint64 filter, guint8 format, guint16 max_list_count,
1390 guint16 list_start_offset, GVariantBuilder *vcards)
1393 contacts_list_h record_list = NULL;
1394 contacts_query_h query = NULL;
1398 guint property_id = 0;
1399 const char *attr = NULL;
1400 gboolean get_log = FALSE;
1402 /* contact offset is n - 1 of PBAP */
1403 offset = (gint)list_start_offset - 1;
1405 if (max_list_count >= 65535)
1406 limit = -1; /* contact limit -1 means unrestricted */
1408 limit = (gint)max_list_count;
1412 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1416 if (list_start_offset == 0) {
1419 vcard = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
1422 g_variant_builder_add(vcards, "s", vcard);
1432 if (pb_type == TELECOM_PB)
1433 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1434 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1435 else if (pb_type == SIM_PB)
1436 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1439 property_id = _contacts_person.id;
1442 query = __bluetooth_pb_query_phone_log_incoming();
1443 property_id = _contacts_phone_log.id;
1447 query = __bluetooth_pb_query_phone_log_outgoing();
1448 property_id = _contacts_phone_log.id;
1452 query = __bluetooth_pb_query_phone_log_missed();
1453 property_id = _contacts_phone_log.id;
1457 query = __bluetooth_pb_query_phone_log_combined();
1458 property_id = _contacts_phone_log.id;
1464 INFO("Limit is = %d and offset is =%d\n", limit, offset);
1466 /* When limit is passed as ZERO to contacts_db_get_records_with_query
1467 * API then this API will provide all available contacts in its database
1468 * (unrestricted). Now consider a case when client requests for
1469 * maxlistcount of 1 and start offset as 0 then we have already read the
1470 * owner card in above switch case and when it reads owner card it
1471 * decrements the limit by 1.
1474 status = contacts_db_get_records_with_query(query, offset,
1475 limit, &record_list);
1477 if (status != CONTACTS_ERROR_NONE) {
1478 contacts_list_destroy(record_list, TRUE);
1479 contacts_query_destroy(query);
1483 status = contacts_list_first(record_list);
1485 if (status != CONTACTS_ERROR_NONE) {
1486 contacts_list_destroy(record_list, TRUE);
1487 contacts_query_destroy(query);
1492 contacts_record_h record;
1494 gchar *vcard = NULL;
1496 status = contacts_list_get_current_record_p(record_list,
1499 if (status != CONTACTS_ERROR_NONE)
1501 status = contacts_record_get_int(record, property_id,
1504 if (status != CONTACTS_ERROR_NONE)
1507 if (property_id == _contacts_person.id)
1508 vcard = _bluetooth_pb_vcard_contact(id, filter,
1512 attr = __bluetooth_pb_phone_log_get_log_type(record);
1514 vcard = _bluetooth_pb_vcard_call(id, filter,
1519 g_variant_builder_add(vcards, "s", vcard);
1521 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1522 contacts_list_destroy(record_list, TRUE);
1525 contacts_query_destroy(query);
1530 static void __bluetooth_pb_get_contact_list(PbAgentData *agent,
1531 contacts_query_h query, GVariantBuilder *builder)
1534 contacts_list_h record_list = NULL;
1542 tmp = _bluetooth_pb_owner_name();
1543 name = g_strdup_printf("%s;;;;", tmp);
1546 __bluetooth_pb_list_ptr_array_add(builder, name,
1547 agent->tel_number, 0);
1552 status = contacts_db_get_records_with_query(query, -1, -1,
1555 if (status != CONTACTS_ERROR_NONE) {
1556 contacts_list_destroy(record_list, TRUE);
1560 status = contacts_list_first(record_list);
1562 if (status != CONTACTS_ERROR_NONE) {
1563 contacts_list_destroy(record_list, TRUE);
1568 contacts_record_h record;
1571 status = contacts_list_get_current_record_p(record_list,
1574 if (status != CONTACTS_ERROR_NONE)
1577 status = contacts_record_get_int(record,
1578 _contacts_person_contact.person_id, &id);
1580 if (status != CONTACTS_ERROR_NONE)
1588 name = _bluetooth_pb_name_from_person_id(id);
1589 number = _bluetooth_pb_number_from_person_id(id);
1591 __bluetooth_pb_list_ptr_array_add(builder, name,
1598 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1600 contacts_list_destroy(record_list, TRUE);
1604 static void __bluetooth_pb_get_phone_log_list(PbAgentData *agent,
1605 contacts_query_h query, GVariantBuilder *builder)
1608 contacts_list_h record_list = NULL;
1611 status = contacts_db_get_records_with_query(query, -1, -1,
1614 if (status != CONTACTS_ERROR_NONE)
1617 status = contacts_list_first(record_list);
1619 if (status != CONTACTS_ERROR_NONE) {
1620 contacts_list_destroy(record_list, TRUE);
1625 contacts_record_h record;
1628 status = contacts_list_get_current_record_p(record_list,
1631 if (status != CONTACTS_ERROR_NONE)
1634 status = contacts_record_get_int(record,
1635 _contacts_phone_log.id, &id);
1637 if (status != CONTACTS_ERROR_NONE)
1645 name = _bluetooth_pb_name_from_phonelog_id(id);
1647 contacts_record_get_str_p(record,
1648 _contacts_phone_log.address, &number);
1650 __bluetooth_pb_list_ptr_array_add(builder, name,
1656 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1658 contacts_list_destroy(record_list, TRUE);
1663 static void __bluetooth_pb_get_list(PbAgentData *agent, PhoneBookType pb_type,
1664 GVariantBuilder *builder)
1667 contacts_query_h query;
1669 /* no requires refresh cache */
1670 if (builder == NULL && agent->pb_type == pb_type)
1675 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1676 __bluetooth_pb_get_contact_list(agent, query, builder);
1679 query = __bluetooth_pb_query_phone_log_incoming();
1680 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1683 query = __bluetooth_pb_query_phone_log_outgoing();
1684 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1687 query = __bluetooth_pb_query_phone_log_missed();
1688 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1691 query = __bluetooth_pb_query_phone_log_combined();
1692 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1694 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1696 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1697 __bluetooth_pb_get_contact_list(agent, query, builder);
1704 agent->pb_type = pb_type;
1707 contacts_query_destroy(query);
1711 static void __bluetooth_pb_get_contact_list_number(PbAgentData *agent,
1712 contacts_query_h query, gint start_index,
1713 gint end_index, GVariantBuilder *builder)
1716 contacts_list_h record_list = NULL;
1732 offset = to - from + 1;
1738 status = contacts_db_get_records_with_query(query, from - 1 , offset,
1741 if (status != CONTACTS_ERROR_NONE) {
1742 contacts_list_destroy(record_list, TRUE);
1746 status = contacts_list_first(record_list);
1748 if (status != CONTACTS_ERROR_NONE) {
1749 contacts_list_destroy(record_list, TRUE);
1754 contacts_record_h record;
1755 gchar *display_name;
1758 status = contacts_list_get_current_record_p(record_list,
1761 if (status != CONTACTS_ERROR_NONE)
1764 contacts_record_get_str_p(record,
1765 _contacts_person_number.display_name,
1767 contacts_record_get_str_p(record,
1768 _contacts_person_number.number, &number);
1770 __bluetooth_pb_list_ptr_array_add(builder, display_name,
1774 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1776 contacts_list_destroy(record_list, TRUE);
1780 static void __bluetooth_pb_get_phone_log_list_number(PbAgentData *agent,
1781 contacts_query_h query, gint start_index,
1782 gint end_index, GVariantBuilder *builder)
1785 contacts_list_h record_list = NULL;
1801 offset = to - from + 1;
1807 status = contacts_db_get_records_with_query(query, from - 1 , offset,
1810 if (status != CONTACTS_ERROR_NONE) {
1811 contacts_list_destroy(record_list, TRUE);
1815 status = contacts_list_first(record_list);
1816 if (status != CONTACTS_ERROR_NONE) {
1817 contacts_list_destroy(record_list, TRUE);
1822 contacts_record_h record;
1824 gchar *display_name;
1827 status = contacts_list_get_current_record_p(record_list,
1830 if (status != CONTACTS_ERROR_NONE)
1833 status = contacts_record_get_int(record,
1834 _contacts_phone_log.id, &id);
1835 if (status != CONTACTS_ERROR_NONE) {
1836 ERR("contact_record_get_int api failed %d", status);
1840 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
1842 contacts_record_get_str_p(record, _contacts_phone_log.address,
1845 __bluetooth_pb_list_ptr_array_add(builder, display_name,
1850 g_free(display_name);
1852 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1854 contacts_list_destroy(record_list, TRUE);
1858 static void __bluetooth_pb_get_list_number(PbAgentData *agent,
1859 PhoneBookType pb_type, gint start_index,
1860 gint end_index, GVariantBuilder *builder)
1863 contacts_query_h query;
1865 DBG("type = %d", pb_type);
1868 query = __bluetooth_pb_query_person_number();
1869 __bluetooth_pb_get_contact_list_number(agent, query,
1870 start_index, end_index, builder);
1873 query = __bluetooth_pb_query_phone_log_incoming();
1874 __bluetooth_pb_get_phone_log_list_number(agent, query,
1875 start_index, end_index, builder);
1878 query = __bluetooth_pb_query_phone_log_outgoing();
1879 __bluetooth_pb_get_phone_log_list_number(agent, query,
1880 start_index, end_index, builder);
1883 query = __bluetooth_pb_query_phone_log_missed();
1884 __bluetooth_pb_get_phone_log_list_number(agent, query,
1885 start_index, end_index, builder);
1888 query = __bluetooth_pb_query_phone_log_combined();
1889 __bluetooth_pb_get_phone_log_list_number(agent, query,
1890 start_index, end_index, builder);
1892 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1894 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1895 __bluetooth_pb_get_contact_list_number(agent, query,
1896 start_index, end_index, builder);
1905 contacts_query_destroy(query);
1909 static void __bluetooth_pb_get_contact_list_name(PbAgentData *agent,
1910 contacts_query_h query, const gchar *find_text,
1911 GVariantBuilder *builder)
1914 contacts_list_h record_list = NULL;
1918 status = contacts_db_get_records_with_query(query,
1919 -1, -1, &record_list);
1921 if (status != CONTACTS_ERROR_NONE) {
1922 contacts_list_destroy(record_list, TRUE);
1926 status = contacts_list_first(record_list);
1928 if (status != CONTACTS_ERROR_NONE) {
1929 contacts_list_destroy(record_list, TRUE);
1934 contacts_record_h record;
1935 gchar *display_name;
1937 status = contacts_list_get_current_record_p(record_list,
1940 if (status != CONTACTS_ERROR_NONE)
1943 contacts_record_get_str_p(record,
1944 _contacts_person_number.display_name, &display_name);
1946 if (g_str_has_prefix(display_name, find_text)) {
1949 contacts_record_get_str_p(record,
1950 _contacts_person_number.number, &number);
1952 __bluetooth_pb_list_ptr_array_add(builder, display_name,
1957 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1958 contacts_list_destroy(record_list, TRUE);
1962 static void __bluetooth_pb_get_phone_log_list_name(PbAgentData *agent,
1963 contacts_query_h query, const gchar *find_text,
1964 GVariantBuilder *builder)
1967 contacts_list_h record_list = NULL;
1971 status = contacts_db_get_records_with_query(query, -1, -1,
1974 if (status != CONTACTS_ERROR_NONE) {
1975 contacts_list_destroy(record_list, TRUE);
1979 status = contacts_list_first(record_list);
1981 if (status != CONTACTS_ERROR_NONE) {
1982 contacts_list_destroy(record_list, TRUE);
1987 contacts_record_h record;
1989 gchar *display_name;
1991 status = contacts_list_get_current_record_p(record_list,
1994 if (status != CONTACTS_ERROR_NONE)
1997 status = contacts_record_get_int(record,
1998 _contacts_phone_log.id, &id);
1999 if (status != CONTACTS_ERROR_NONE) {
2000 ERR("contacts_record_get_int failed %d", status);
2004 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
2006 if (g_str_has_prefix(display_name, find_text)) {
2009 contacts_record_get_str_p(record,
2010 _contacts_phone_log.address, &number);
2012 __bluetooth_pb_list_ptr_array_add(builder, display_name,
2018 g_free(display_name);
2020 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
2022 contacts_list_destroy(record_list, TRUE);
2026 static void __bluetooth_pb_get_list_name(PbAgentData *agent,
2027 PhoneBookType pb_type, const gchar *find_text,
2028 GVariantBuilder *builder)
2031 contacts_query_h query;
2035 query = __bluetooth_pb_query_person_number();
2036 __bluetooth_pb_get_contact_list_name(agent, query,
2037 find_text, builder);
2040 query = __bluetooth_pb_query_phone_log_incoming();
2041 __bluetooth_pb_get_phone_log_list_name(agent, query,
2042 find_text, builder);
2045 query = __bluetooth_pb_query_phone_log_outgoing();
2046 __bluetooth_pb_get_phone_log_list_name(agent, query,
2047 find_text, builder);
2050 query = __bluetooth_pb_query_phone_log_missed();
2051 __bluetooth_pb_get_phone_log_list_name(agent, query,
2052 find_text, builder);
2055 query = __bluetooth_pb_query_phone_log_combined();
2056 __bluetooth_pb_get_phone_log_list_name(agent, query,
2057 find_text, builder);
2064 contacts_query_destroy(query);
2068 static void __bluetooth_pb_list_ptr_array_add(GVariantBuilder *builder,
2069 const gchar *name, const gchar *number, gint handle)
2072 gchar *temp_name = g_strdup(name);
2073 gchar *temp_number = g_strdup(number);
2075 g_variant_builder_add(builder, "(ssu)", temp_name, temp_number, handle);
2082 static void __bluetooth_pb_agent_signal_handler(int signum)
2086 g_main_loop_quit(g_mainloop);
2088 DBG("Terminate Bluetooth PBAP agent");
2093 static void __bluetooth_pb_contact_changed(const gchar *view_uri,
2097 guint new_missed_call;
2098 PbAgentData *agent = (PbAgentData *)user_data;
2099 GDBusConnection *conn = __bt_pb_get_gdbus_connection();
2101 DBG("Received contact changed cb");
2103 g_dbus_connection_emit_signal(conn, "org.bluez.pb_agent",
2104 "/org/bluez/pb_agent", "org.bluez.PbAgent",
2105 "clear", NULL, NULL);
2107 bluetooth_pb_agent_clear(agent);
2109 __bluetooth_pb_get_count_new_missed_call(&new_missed_call);
2111 if (new_missed_call > total_missed_call_count)
2112 unnotified_missed_call_count += new_missed_call -
2113 total_missed_call_count;
2115 INFO("Missed call count : #prev[%d], #current[%d], #unnotified[%d]",
2116 total_missed_call_count, new_missed_call,
2117 unnotified_missed_call_count);
2119 total_missed_call_count = new_missed_call;
2123 static void __bluetooth_pb_agent_timeout_add_seconds(PbAgentData *agent)
2127 if (agent->timeout_id)
2128 g_source_remove(agent->timeout_id);
2130 agent->timeout_id = g_timeout_add_seconds(BLUETOOTH_PB_AGENT_TIMEOUT,
2131 __bluetooth_pb_agent_timeout_calback, agent);
2135 static gboolean __bluetooth_pb_agent_timeout_calback(gpointer user_data)
2138 PbAgentData *agent = (PbAgentData *)user_data;
2140 agent->timeout_id = 0;
2143 g_main_loop_quit(g_mainloop);
2149 static void __bluetooth_pb_tel_callback(TapiHandle *handle, int result,
2150 void *data, void *user_data)
2153 PbAgentData *agent = (PbAgentData *)user_data;
2154 TelSimMsisdnList_t *number;
2156 __bt_pb_dbus_init(agent);
2159 number = (TelSimMsisdnList_t *)data;
2160 agent->tel_number = g_strdup(number->list[0].num);
2163 tel_deinit(agent->tapi_handle);
2164 agent->tapi_handle = NULL;
2168 static gboolean __bt_pb_destroy_agent()
2171 g_main_loop_quit(g_mainloop);
2176 static GDBusNodeInfo *__bt_pb_create_method_node_info
2177 (const gchar *introspection_data)
2180 GDBusNodeInfo *node_info = NULL;
2182 if (introspection_data == NULL)
2185 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
2188 ERR("Unable to create node: %s", err->message);
2189 g_clear_error(&err);
2194 static gboolean __bt_pb_dbus_init(PbAgentData *agent)
2198 GDBusNodeInfo *node_info = NULL;
2199 GError *error = NULL;
2200 GDBusConnection *conn = __bt_pb_get_gdbus_connection();
2203 ERR("Error in creating the gdbus connection");
2207 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, BT_PB_SERVICE_NAME,
2208 G_BUS_NAME_OWNER_FLAGS_NONE, NULL,
2209 NULL, NULL, NULL, NULL);
2213 node_info = __bt_pb_create_method_node_info(pb_agent_introspection_xml);
2214 if (node_info == NULL)
2217 pb_id = g_dbus_connection_register_object(conn, BT_PB_SERVICE_OBJECT_PATH,
2218 node_info->interfaces[0], &method_table,
2219 agent, NULL, &error);
2222 ERR("Failed to register: %s", error->message);
2223 g_clear_error(&error);
2227 agent->pbagent_interface_id = pb_id;
2229 pb_id = g_dbus_connection_register_object(conn, BT_PB_SERVICE_OBJECT_PATH,
2230 node_info->interfaces[1], &method_table,
2231 agent, NULL, &error);
2234 ERR("Failed to register: %s", error->message);
2235 g_clear_error(&error);
2239 g_dbus_node_info_unref(node_info);
2241 agent->pbagent_at_interface_id = pb_id;
2247 g_object_unref(pb_dbus_conn);
2248 pb_dbus_conn = NULL;
2252 g_object_unref(conn);
2255 g_dbus_node_info_unref(node_info);
2258 g_bus_unown_name(owner_id);
2263 static gboolean __bt_pb_dbus_deinit(PbAgentData *agent)
2266 if (agent->pbagent_interface_id != 0) {
2267 g_dbus_connection_unregister_object(pb_dbus_conn,
2268 agent->pbagent_interface_id);
2269 agent->pbagent_interface_id = 0;
2271 if (agent->pbagent_at_interface_id != 0) {
2272 g_dbus_connection_unregister_object(pb_dbus_conn,
2273 agent->pbagent_at_interface_id);
2274 agent->pbagent_at_interface_id = 0;
2277 g_object_unref(pb_dbus_conn);
2278 pb_dbus_conn = NULL;
2289 gint ret = EXIT_SUCCESS;
2291 struct sigaction sa;
2293 DBG("Starting Bluetooth PBAP agent");
2295 g_mainloop = g_main_loop_new(NULL, FALSE);
2296 if (g_mainloop == NULL) {
2297 ERR("Couldn't create GMainLoop\n");
2298 return EXIT_FAILURE;
2301 agent = (PbAgentData *)g_malloc(sizeof(PbAgentData));
2304 return EXIT_FAILURE;
2306 agent->pbagent_interface_id = 0;
2307 agent->pbagent_at_interface_id = 0;
2308 agent->tel_number = NULL;
2310 /* connect contact */
2311 if (contacts_connect() != CONTACTS_ERROR_NONE) {
2312 ERR("Can not connect contacts server\n");
2314 return EXIT_FAILURE;
2317 __bluetooth_pb_get_count_new_missed_call(&total_missed_call_count);
2319 if (contacts_db_add_changed_cb(_contacts_contact._uri,
2320 __bluetooth_pb_contact_changed,
2321 (void *)agent) != CONTACTS_ERROR_NONE) {
2322 ERR("Can not add changed callback");
2325 if (contacts_db_add_changed_cb(_contacts_phone_log._uri,
2326 __bluetooth_pb_contact_changed,
2327 (void *)agent) != CONTACTS_ERROR_NONE) {
2328 ERR("Can not add changed callback");
2332 memset(&sa, 0, sizeof(sa));
2333 sa.sa_handler = __bluetooth_pb_agent_signal_handler;
2334 sigaction(SIGTERM, &sa, NULL);
2335 sigaction(SIGINT, &sa, NULL);
2338 agent->tapi_handle = tel_init(NULL);
2339 tapi_result = tel_get_sim_msisdn(agent->tapi_handle,
2340 __bluetooth_pb_tel_callback, agent);
2342 if (tapi_result != TAPI_API_SUCCESS)
2343 __bt_pb_dbus_init(agent);
2345 __bluetooth_pb_agent_timeout_add_seconds(agent);
2347 g_main_loop_run(g_mainloop);
2349 if (contacts_db_remove_changed_cb(_contacts_phone_log._uri,
2350 __bluetooth_pb_contact_changed,
2351 (void *)agent) != CONTACTS_ERROR_NONE) {
2352 ERR("Cannot remove changed callback");
2355 if (contacts_db_remove_changed_cb(_contacts_contact._uri,
2356 __bluetooth_pb_contact_changed,
2357 (void *)agent) != CONTACTS_ERROR_NONE) {
2358 ERR("Cannot remove changed callback");
2361 if (contacts_disconnect() != CONTACTS_ERROR_NONE)
2362 ERR("contacts_disconnect failed \n");
2364 g_dbus_connection_emit_signal(pb_dbus_conn, "org.bluez.pb_agent",
2365 "/org/bluez/pb_agent", "org.bluez.PbAgent",
2366 "clear", NULL, NULL);
2368 bluetooth_pb_agent_clear(agent);
2370 if (agent->tapi_handle)
2371 tel_deinit(agent->tapi_handle);
2372 if (agent->tel_number)
2373 g_free(agent->tel_number);
2375 __bt_pb_dbus_deinit(agent);
2379 DBG("Terminate Bluetooth PBAP agent");