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 /* LCOV_EXCL_START */
143 static void __bt_pb_agent_method(GDBusConnection *connection,
144 const gchar *sender, const gchar *object_path,
145 const gchar *interface_name, const gchar *method_name,
146 GVariant *parameters, GDBusMethodInvocation *invocation,
149 static GVariant *__bt_pb_get_phonebook_folder_list(GError **error);
151 static GVariant *__bt_pb_get_phonebook(PbAgentData *agent, const char *name,
152 guint64 filter, guint8 format, guint16 max_list_count,
153 guint16 list_start_offset, GError **err);
155 static GVariant *__bt_pb_get_phonebook_size(PbAgentData *agent,
156 const char *name, GError **err);
158 static GVariant *__bt_pb_get_phonebook_list(PbAgentData *agent,
159 const char *name, GError **err);
161 static GVariant *__bt_pb_get_phonebook_entry(PbAgentData *agent,
162 const gchar *folder, const gchar *id, guint64 filter,
163 guint8 format, GError **err);
165 static GVariant *__bt_pb_get_phonebook_size_at(PbAgentData *agent,
166 const gchar *command, GError **err);
168 static GVariant *__bt_pb_get_phonebook_entries_at(PbAgentData *agent,
169 const gchar *command, gint32 start_index,
170 gint32 end_index, GError **err);
172 static GVariant *__bt_pb_get_phonebook_entries_find_at(PbAgentData *agent,
173 const gchar *command, const gchar *find_text,
176 static GVariant *__bt_pb_get_total_object_count(PbAgentData *agent,
177 gchar *path, GError **err);
179 static gboolean __bt_pb_add_contact(PbAgentData *agent, const char *filename,
182 static gboolean __bt_pb_destroy_agent();
184 static GError *__bt_pb_error(gint error_code, const gchar *error_message);
186 static PhoneBookType __bluetooth_pb_get_pb_type(const char *name);
188 static PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name);
190 static gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
191 gint *match, gint size);
193 static contacts_query_h __bluetooth_pb_query_phone_log(gint *match, gint size);
195 static contacts_query_h __bluetooth_pb_query_person(int addressbook);
197 static contacts_query_h __bluetooth_pb_query_person_number(void);
199 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void);
201 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void);
203 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void);
205 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void);
207 static gboolean __bluetooth_pb_get_count(PhoneBookType pb_type, guint *count);
209 static gboolean __bluetooth_pb_get_count_new_missed_call(guint *count);
211 static const char *__bluetooth_pb_phone_log_get_log_type(contacts_record_h record);
213 static void __bluetooth_pb_get_vcards(PbAgentData *agent, PhoneBookType pb_type,
214 guint64 filter, guint8 format, guint16 max_list_count,
215 guint16 list_start_offset, GVariantBuilder *vcards);
217 static void __bluetooth_pb_get_contact_list(PbAgentData *agent,
218 contacts_query_h query, GVariantBuilder *builder);
220 static void __bluetooth_pb_get_phone_log_list(PbAgentData *agent,
221 contacts_query_h query, GVariantBuilder *builder);
223 static void __bluetooth_pb_get_list(PbAgentData *agent, PhoneBookType pb_type,
224 GVariantBuilder *builder);
226 static void __bluetooth_pb_get_contact_list_number(PbAgentData *agent,
227 contacts_query_h query, gint start_index,
228 gint end_index, GVariantBuilder *builder);
230 static void __bluetooth_pb_get_phone_log_list_number(PbAgentData *agent,
231 contacts_query_h query, gint start_index,
232 gint end_index, GVariantBuilder *builder);
234 static void __bluetooth_pb_get_list_number(PbAgentData *agent,
235 PhoneBookType pb_type, gint start_index,
236 gint end_index, GVariantBuilder *builder);
238 static void __bluetooth_pb_get_contact_list_name(PbAgentData *agent,
239 contacts_query_h query, const gchar *find_text,
240 GVariantBuilder *builder);
242 static void __bluetooth_pb_get_phone_log_list_name(PbAgentData *agent,
243 contacts_query_h query, const gchar *find_text,
244 GVariantBuilder *builder);
246 static void __bluetooth_pb_get_list_name(PbAgentData *agent,
247 PhoneBookType pb_type, const gchar *find_text,
248 GVariantBuilder *builder);
250 static void __bluetooth_pb_list_ptr_array_add(GVariantBuilder *builder,
251 const gchar *name, const gchar *number, gint handle);
253 static void __bluetooth_pb_agent_signal_handler(int signum);
255 static void __bluetooth_pb_contact_changed(const gchar *view_uri,
258 static void __bluetooth_pb_agent_timeout_add_seconds(PbAgentData *agent);
260 static gboolean __bluetooth_pb_agent_timeout_calback(gpointer user_data);
262 static void __bluetooth_pb_tel_callback(TapiHandle *handle, int result,
263 void *data, void *user_data);
265 static gboolean __bt_pb_dbus_init(PbAgentData *agent);
267 static gboolean __bt_pb_dbus_deinit(PbAgentData *agent);
269 static const GDBusInterfaceVTable method_table = {
270 __bt_pb_agent_method,
275 static void __bt_pb_agent_method(GDBusConnection *connection,
276 const gchar *sender, const gchar *object_path,
277 const gchar *interface_name, const gchar *method_name,
278 GVariant *parameters, GDBusMethodInvocation *invocation,
282 INFO("method: %s; object_path: %s", method_name, object_path);
283 PbAgentData *agent = (PbAgentData *)user_data;
285 if (g_strcmp0(interface_name, "org.bluez.PbAgent") == 0) {
286 if (g_strcmp0(method_name, "GetPhonebookFolderList") == 0) {
287 GVariant *folder_list = NULL;
289 folder_list = __bt_pb_get_phonebook_folder_list(&err);
292 g_dbus_method_invocation_return_value(invocation,
294 } else if (g_strcmp0(method_name, "GetPhonebook") == 0) {
295 GVariant *phonebook = NULL;
299 guint16 max_list_count;
300 guint16 list_start_offset;
302 g_variant_get(parameters, "(&styqq)", &name, &filter,
303 &format, &max_list_count,
305 phonebook = __bt_pb_get_phonebook(agent, name, filter,
306 format, max_list_count,
307 list_start_offset, &err);
310 g_dbus_method_invocation_return_value(invocation,
312 } else if (g_strcmp0(method_name, "GetPhonebookSize") == 0) {
313 GVariant *phonebook_size = NULL;
316 g_variant_get(parameters, "(&s)", &name);
317 phonebook_size = __bt_pb_get_phonebook_size(agent, name,
321 g_dbus_method_invocation_return_value(invocation,
323 } else if (g_strcmp0(method_name, "GetPhonebookList") == 0) {
324 GVariant *phonebook_list = NULL;
327 g_variant_get(parameters, "(&s)", &name);
328 phonebook_list = __bt_pb_get_phonebook_list(agent, name,
332 g_dbus_method_invocation_return_value(invocation,
334 } else if (g_strcmp0(method_name, "GetPhonebookEntry") == 0) {
335 GVariant *phonebook_entry = NULL;
341 g_variant_get(parameters, "(&s&sty)", &folder, &id,
343 phonebook_entry = __bt_pb_get_phonebook_entry(agent,
344 folder, id, filter, format, &err);
347 g_dbus_method_invocation_return_value(invocation,
349 } else if (g_strcmp0(method_name, "GetTotalObjectCount") == 0) {
350 GVariant *phonebook_size = NULL;
353 g_variant_get(parameters, "(&s)", &path);
354 phonebook_size = __bt_pb_get_total_object_count(agent,
358 g_dbus_method_invocation_return_value(invocation,
360 } else if (g_strcmp0(method_name, "AddContact") == 0) {
361 const char *filename;
363 g_variant_get(parameters, "(&s)", &filename);
364 __bt_pb_add_contact(agent, filename, &err);
367 g_dbus_method_invocation_return_value(invocation, NULL);
368 } else if (g_strcmp0(method_name, "DestroyAgent") == 0) {
369 g_dbus_method_invocation_return_value(invocation, NULL);
370 __bt_pb_destroy_agent();
372 } else if (g_strcmp0(interface_name, "org.bluez.PbAgent.At") == 0) {
373 if (g_strcmp0(method_name, "GetPhonebookSizeAt") == 0) {
374 GVariant *phonebook_size = NULL;
375 const gchar *command;
377 g_variant_get(parameters, "(&s)", &command);
378 phonebook_size = __bt_pb_get_phonebook_size_at(agent,
382 g_dbus_method_invocation_return_value(invocation,
384 } else if (g_strcmp0(method_name,
385 "GetPhonebookEntriesAt") == 0) {
386 GVariant *phonebook_entries = NULL;
387 const gchar *command;
391 g_variant_get(parameters, "(&sii)",
392 &command, &start_index, &end_index);
393 phonebook_entries = __bt_pb_get_phonebook_entries_at(agent,
394 command, start_index,
398 g_dbus_method_invocation_return_value(invocation,
400 } else if (g_strcmp0(method_name,
401 "GetPhonebookEntriesFindAt") == 0) {
402 GVariant *phonebook_entries = NULL;
403 const gchar *command;
404 const gchar *find_text;
406 g_variant_get(parameters, "(&s&s)", &command, &find_text);
407 phonebook_entries = __bt_pb_get_phonebook_entries_find_at(agent,
408 command, find_text, &err);
411 g_dbus_method_invocation_return_value(invocation,
420 g_dbus_method_invocation_return_gerror(invocation, err);
426 static void bluetooth_pb_agent_clear(PbAgentData *agent)
429 agent->pb_type = TELECOM_NONE;
433 static GDBusConnection *__bt_pb_get_gdbus_connection(void)
441 pb_dbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
444 ERR("Unable to connect to dbus: %s", err->message);
453 static GVariant *__bt_pb_get_phonebook_folder_list(GError **error)
456 GVariant *folder_list;
459 GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
461 size = G_N_ELEMENTS(bluetooth_pb_agent_folder_list);
463 for (i = 0; i < size; i++)
464 g_variant_builder_add(builder, "s",
465 bluetooth_pb_agent_folder_list[i]);
467 folder_list = g_variant_new("(as)", builder);
468 g_variant_builder_unref(builder);
475 static GVariant *__bt_pb_get_phonebook(PbAgentData *agent, const char *name,
476 guint64 filter, guint8 format, guint16 max_list_count,
477 guint16 list_start_offset, GError **err)
481 PhoneBookType pb_type = TELECOM_NONE;
482 GVariantBuilder *vcards;
484 INFO("name: %s filter: %lld format: %d max_list_count: %d list_start_offset: %d\n",
485 name, (unsigned long long int)filter, format, max_list_count, list_start_offset);
487 __bluetooth_pb_agent_timeout_add_seconds(agent);
489 pb_type = __bluetooth_pb_get_pb_type(name);
491 if (pb_type == TELECOM_NONE) {
492 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
493 "unsupported name defined");
497 vcards = g_variant_builder_new(G_VARIANT_TYPE("as"));
499 if (max_list_count > 0) {
500 __bluetooth_pb_get_vcards(agent, pb_type, filter, format,
501 max_list_count, list_start_offset, vcards);
504 if (pb_type == TELECOM_MCH) {
505 phonebook = g_variant_new("(asu)", vcards,
506 unnotified_missed_call_count);
507 INFO("Notified [%d] missed call count",
508 unnotified_missed_call_count);
509 unnotified_missed_call_count = 0;
511 phonebook = g_variant_new("(asu)", vcards, 0);
514 g_variant_builder_unref(vcards);
520 static GVariant *__bt_pb_get_phonebook_size(PbAgentData *agent,
521 const char *name, GError **err)
524 GVariant *phonebook_size;
525 PhoneBookType pb_type = TELECOM_NONE;
528 DBG_SECURE("name: %s\n", name);
530 __bluetooth_pb_agent_timeout_add_seconds(agent);
532 pb_type = __bluetooth_pb_get_pb_type(name);
534 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
535 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
536 "unsupported name defined");
541 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
542 if (pb_type == TELECOM_PB || pb_type == SIM_PB)
545 if (pb_type == TELECOM_PB)
548 if (pb_type == TELECOM_MCH) {
549 phonebook_size = g_variant_new("(uu)", count,
550 unnotified_missed_call_count);
551 INFO("Notified [%d] missed call count",
552 unnotified_missed_call_count);
553 unnotified_missed_call_count = 0;
555 phonebook_size = g_variant_new("(uu)", count, 0);
559 return phonebook_size;
563 static GVariant *__bt_pb_get_phonebook_list(PbAgentData *agent,
564 const char *name, GError **err)
567 GVariant *phonebook_list;
568 PhoneBookType pb_type = TELECOM_NONE;
570 GVariantBuilder *builder;
572 DBG_SECURE("name: %s\n", name);
574 __bluetooth_pb_agent_timeout_add_seconds(agent);
576 pb_type = __bluetooth_pb_get_pb_type(name);
578 if (pb_type == TELECOM_NONE) {
579 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
580 "unsupported name defined");
584 builder = g_variant_builder_new(G_VARIANT_TYPE("a(ssu)"));
586 __bluetooth_pb_get_list(agent, pb_type, builder);
588 INFO("pb_type[%d] / number of missed_call[%d]", pb_type,
589 unnotified_missed_call_count);
591 if (pb_type == TELECOM_MCH) {
592 phonebook_list = g_variant_new("(a(ssu)u)", builder,
593 unnotified_missed_call_count);
594 INFO("Notified [%d] missed call count",
595 unnotified_missed_call_count);
596 unnotified_missed_call_count = 0;
598 phonebook_list = g_variant_new("(a(ssu)u)", builder, 0);
601 g_variant_builder_unref(builder);
604 return phonebook_list;
607 static GVariant *__bt_pb_get_phonebook_entry(PbAgentData *agent,
608 const gchar *folder, const gchar *id, guint64 filter,
609 guint8 format, GError **err)
612 GVariant *phonebook_entry;
613 PhoneBookType pb_type = TELECOM_NONE;
616 const gchar *attr = NULL;
618 DBG_SECURE("folder: %s id: %s filter: %llu format: %d\n",
619 folder, id, (unsigned long long int)filter, format);
621 __bluetooth_pb_agent_timeout_add_seconds(agent);
623 if (!g_str_has_suffix(id, ".vcf")) {
624 *err = __bt_pb_error(G_FILE_ERROR_INVAL, "invalid vcf file");
628 handle = (gint)g_ascii_strtoll(id, NULL, 10);
630 pb_type = __bluetooth_pb_get_pb_type(folder);
632 if (pb_type == TELECOM_NONE) {
633 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
634 "unsupported name defined");
638 /* create index cache */
639 __bluetooth_pb_get_list(agent, pb_type, NULL);
644 str = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
647 if (_bluetooth_get_contact_addressbook(handle) == PBAP_ADDRESSBOOK_PHONE)
648 str = _bluetooth_pb_vcard_contact(handle,
654 str = _bluetooth_pb_vcard_call(handle, filter, format,
658 str = _bluetooth_pb_vcard_call(handle, filter, format, "DIALED");
661 str = _bluetooth_pb_vcard_call(handle, filter, format, "MISSED");
664 contacts_record_h record = NULL;
668 status = contacts_db_get_record(_contacts_phone_log._uri,
671 if (status != CONTACTS_ERROR_NONE)
674 attr = __bluetooth_pb_phone_log_get_log_type(record);
675 str = _bluetooth_pb_vcard_call(handle, filter, format, attr);
677 contacts_record_destroy(record, TRUE);
680 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
683 str = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
686 if (_bluetooth_get_contact_addressbook(handle) == PBAP_ADDRESSBOOK_SIM)
687 str = _bluetooth_pb_vcard_contact(handle,
693 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
694 "unsupported name defined");
698 phonebook_entry = g_variant_new("(s)", str);
703 return phonebook_entry;
706 static GVariant *__bt_pb_get_phonebook_size_at(PbAgentData *agent,
707 const gchar *command, GError **err)
710 GVariant *phonebook_size;
711 PhoneBookType pb_type = TELECOM_NONE;
714 DBG("command: %s\n", command);
716 __bluetooth_pb_agent_timeout_add_seconds(agent);
718 pb_type = __bluetooth_pb_get_storage_pb_type(command);
720 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
721 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
722 "unsupported name defined");
726 phonebook_size = g_variant_new("(u)", count);
729 return phonebook_size;
732 static GVariant *__bt_pb_get_phonebook_entries_at(PbAgentData *agent,
733 const gchar *command, gint32 start_index,
734 gint32 end_index, GError **err)
737 GVariant *phonebook_entries;
738 PhoneBookType pb_type = TELECOM_NONE;
739 GVariantBuilder *builder;
741 DBG("command: %s, start_index: %d, end_index: %d\n",
742 command, start_index, end_index);
744 __bluetooth_pb_agent_timeout_add_seconds(agent);
746 pb_type = __bluetooth_pb_get_storage_pb_type(command);
748 if (pb_type == TELECOM_NONE || pb_type == TELECOM_CCH) {
749 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
750 "unsupported name defined");
754 builder = g_variant_builder_new(G_VARIANT_TYPE("a(ssu)"));
756 __bluetooth_pb_get_list_number(agent, pb_type,
757 start_index, end_index, builder);
759 phonebook_entries = g_variant_new("(a(ssu))", builder);
760 g_variant_builder_unref(builder);
763 return phonebook_entries;
766 static GVariant *__bt_pb_get_phonebook_entries_find_at(PbAgentData *agent,
767 const gchar *command, const gchar *find_text,
771 GVariant *phonebook_entries;
772 PhoneBookType pb_type = TELECOM_NONE;
773 GVariantBuilder *builder;
775 DBG("command: %s, find text: %s\n", command, find_text);
777 __bluetooth_pb_agent_timeout_add_seconds(agent);
779 pb_type = __bluetooth_pb_get_storage_pb_type(command);
781 if (pb_type == TELECOM_NONE || pb_type == TELECOM_CCH) {
782 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
783 "unsupported name defined");
787 builder = g_variant_builder_new(G_VARIANT_TYPE("a(ssu)"));
789 __bluetooth_pb_get_list_name(agent, pb_type, find_text, builder);
791 phonebook_entries = g_variant_new("(a(ssu))", builder);
793 g_variant_builder_unref(builder);
796 return phonebook_entries;
799 static GVariant *__bt_pb_get_total_object_count(PbAgentData *agent,
800 gchar *path, GError **err)
803 GVariant *phonebook_size;
805 PhoneBookType pb_type = TELECOM_NONE;
807 __bluetooth_pb_agent_timeout_add_seconds(agent);
809 pb_type = __bluetooth_pb_get_storage_pb_type(path);
811 if (__bluetooth_pb_get_count(pb_type, &count) == FALSE) {
812 *err = __bt_pb_error(G_FILE_ERROR_INVAL,
813 "unsupported name defined");
817 phonebook_size = g_variant_new("(u)", count);
820 return phonebook_size;
824 static int __bluetooth_pb_agent_read_file(const char *file_path, char **stream)
829 int received_file_size = 0;
830 struct stat file_attr;
832 if (file_path == NULL || stream == NULL) {
833 ERR("Invalid data \n");
837 DBG_SECURE("file_path = %s\n", file_path);
839 if ((fp = fopen(file_path, "r+")) == NULL) {
840 ERR_SECURE("Cannot open %s\n", file_path);
844 if (fstat(fileno(fp), &file_attr) == 0) {
845 received_file_size = file_attr.st_size;
846 DBG("file_attr.st_size = %d, size = %d\n", file_attr.st_size,
849 if (received_file_size <= 0) {
850 ERR_SECURE("Some problem in the file size [%s] \n",
857 *stream = (char *)malloc(sizeof(char) *received_file_size);
858 if (NULL == *stream) {
864 ERR_SECURE("Some problem in the file [%s] \n", file_path);
870 read_len = fread(*stream, 1, received_file_size, fp);
877 DBG_SECURE("Cannot open %s\n", file_path);
890 static gboolean __bt_pb_add_contact(PbAgentData *agent, const char *filename,
894 /* Contact API is changed, Temporary blocked */
896 CTSstruct *contact_record = NULL;
897 GSList *numbers_list = NULL, *cursor;
899 int is_duplicated = 0;
903 DBG_SECURE("file_path = %s\n", filename);
905 err = contacts_svc_connect();
906 ERR("contact_db_service_connect fucntion call [error] = %d \n", err);
908 err = __bluetooth_pb_agent_read_file(filename, &stream);
911 contacts_svc_disconnect();
912 ERR("contacts_svc_disconnect fucntion call [error] = %d \n", err);
914 if (NULL != stream) {
921 is_success = contacts_svc_get_contact_from_vcard((const void *)stream,
924 DBG("contacts_svc_get_contact_from_vcard fucntion call [is_success] = %d \n", is_success);
926 if (0 == is_success) {
927 contacts_svc_struct_get_list(contact_record, CTS_CF_NUMBER_LIST,
929 cursor = numbers_list;
931 for (; cursor; cursor = g_slist_next(cursor)) {
932 if (contacts_svc_find_contact_by(CTS_FIND_BY_NUMBER,
933 contacts_svc_value_get_str(cursor->data,
934 CTS_NUM_VAL_NUMBER_STR)) > 0) {
935 DBG("is_duplicated\n");
936 is_duplicated = TRUE;
940 if (is_duplicated == FALSE)
941 contacts_svc_insert_contact(0, contact_record);
946 err = contacts_svc_disconnect();
947 ERR("contacts_svc_disconnect fucntion call [error] = %d \n", err);
949 if (NULL != stream) {
958 /* Create GError from error code and error message*/
959 static GError *__bt_pb_error(gint error_code, const gchar *error_message)
961 return g_error_new(g_quark_from_string("PB Agent"),
962 error_code, "PB Agent Error: %s", error_message);
965 static PhoneBookType __bluetooth_pb_get_pb_type(const char *name)
968 gchar *suffix = ".vcf";
978 if (g_str_has_suffix(name, suffix))
979 len -= strlen(suffix);
981 size = G_N_ELEMENTS(bluetooth_pb_agent_folder_list) - 1;
982 for (i = 0; i < size; i++) {
983 if (strncmp(name, bluetooth_pb_agent_folder_list[i], len) == 0)
991 static PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name)
997 if (g_strcmp0(name, "\"ME\"") == 0)
1000 if (g_strcmp0(name, "\"RC\"") == 0)
1003 if (g_strcmp0(name, "\"DC\"") == 0)
1006 if (g_strcmp0(name, "\"MC\"") == 0)
1008 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1009 if (g_strcmp0(name, "\"SM\"") == 0)
1013 return TELECOM_NONE;
1016 static gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
1017 gint *match, gint size)
1023 for (i = 0; i < size; i++) {
1026 status = contacts_filter_add_operator(filter,
1027 CONTACTS_FILTER_OPERATOR_OR);
1029 if (status != CONTACTS_ERROR_NONE)
1033 status = contacts_filter_add_int(filter,
1034 _contacts_phone_log.log_type,
1035 CONTACTS_MATCH_EQUAL, match[i]);
1037 if (status != CONTACTS_ERROR_NONE)
1042 return CONTACTS_ERROR_NONE;
1045 static contacts_query_h __bluetooth_pb_query_phone_log(gint *match, gint size)
1048 contacts_query_h query = NULL;
1049 contacts_filter_h filter = NULL;
1052 status = contacts_query_create(_contacts_phone_log._uri, &query);
1054 if (status != CONTACTS_ERROR_NONE)
1057 status = contacts_filter_create(_contacts_phone_log._uri, &filter);
1059 if (status != CONTACTS_ERROR_NONE) {
1060 contacts_query_destroy(query);
1064 status = __bluetooth_pb_phone_log_filter_append(filter, match, size);
1066 if (status != CONTACTS_ERROR_NONE) {
1067 contacts_filter_destroy(filter);
1068 contacts_query_destroy(query);
1072 status = contacts_query_set_filter(query, filter);
1074 if (status != CONTACTS_ERROR_NONE) {
1075 contacts_filter_destroy(filter);
1076 contacts_query_destroy(query);
1080 status = contacts_query_set_sort(query, _contacts_phone_log.log_time,
1083 if (status != CONTACTS_ERROR_NONE) {
1084 contacts_filter_destroy(filter);
1085 contacts_query_destroy(query);
1089 contacts_filter_destroy(filter);
1095 bool __bt_is_matching_addressbook(const char *addressbook_name, int addressbook)
1097 bool is_sim_addressbook = _bt_is_sim_addressbook(addressbook_name);
1099 if ((is_sim_addressbook == false
1100 && addressbook == PBAP_ADDRESSBOOK_PHONE) ||
1101 (is_sim_addressbook == true
1102 && addressbook == PBAP_ADDRESSBOOK_SIM))
1108 static contacts_query_h __bluetooth_pb_query_person(int addressbook)
1111 contacts_query_h query = NULL;
1112 contacts_filter_h filter = NULL;
1113 contacts_list_h recordList = NULL;
1114 contacts_record_h record = NULL;
1115 char *addressbook_name = NULL;
1116 int address_book_id = 0;
1120 bool is_first_condition = true;
1122 DBG("Addressbook [%d]", addressbook);
1124 status = contacts_query_create(_contacts_person_contact._uri, &query);
1126 ERR("Could not create query");
1130 /* Create addressbook Filter*/
1131 status = contacts_db_get_all_records(_contacts_address_book._uri, 0, 0,
1133 if (status != CONTACTS_ERROR_NONE)
1134 ERR("Contact list get api failed %d", status);
1136 contacts_filter_create(_contacts_person_contact._uri, &filter);
1137 contacts_list_get_count(recordList, &count);
1139 for (i = 0; i < count; i++) {
1140 status = contacts_list_get_current_record_p(recordList, &record);
1141 if (status != CONTACTS_ERROR_NONE) {
1142 ERR("Contact list get api failed %d", status);
1145 status = contacts_record_get_str_p(record,
1146 _contacts_address_book.name,
1148 if (status != CONTACTS_ERROR_NONE) {
1149 ERR("Contact record get api failed %d", status);
1152 status = contacts_record_get_int(record,
1153 _contacts_address_book.id,
1155 if (status != CONTACTS_ERROR_NONE) {
1156 ERR("contacts record get int api failed %d", status);
1160 DBG("Addressbook ID: [%d] Addressbook Name: [%s]",
1161 address_book_id, addressbook_name);
1163 if (__bt_is_matching_addressbook(addressbook_name,
1165 if (is_first_condition)
1166 is_first_condition = false;
1168 contacts_filter_add_operator(filter,
1169 CONTACTS_FILTER_OPERATOR_OR);
1170 DBG("SELECTED Addressbook ID: [%d] Addressbook Name: [%s]",
1171 address_book_id, addressbook_name);
1172 status = contacts_filter_add_int(filter,
1173 _contacts_person_contact.address_book_id,
1174 CONTACTS_MATCH_EQUAL, address_book_id);
1175 if (status != CONTACTS_ERROR_NONE)
1176 ERR("Contact filter add failed %d", status);
1179 if (contacts_list_next(recordList) != CONTACTS_ERROR_NONE)
1183 contacts_list_destroy(recordList, true);
1185 if (is_first_condition) {
1186 ERR("ADDRESSBOOK NOT FOUND");
1187 contacts_filter_destroy(filter);
1188 contacts_query_destroy(query);
1192 status = contacts_query_set_filter(query, filter);
1193 if (status != CONTACTS_ERROR_NONE)
1194 ERR("Could not Apply Filter");
1196 contacts_filter_destroy(filter);
1201 static contacts_query_h __bluetooth_pb_query_person_number(void)
1204 contacts_query_h query = NULL;
1207 status = contacts_query_create(_contacts_person_number._uri, &query);
1209 if (status != CONTACTS_ERROR_NONE)
1216 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void)
1221 CONTACTS_PLOG_TYPE_VOICE_INCOMING,
1222 CONTACTS_PLOG_TYPE_VIDEO_INCOMING,
1223 CONTACTS_PLOG_TYPE_VOICE_REJECT,
1224 CONTACTS_PLOG_TYPE_VIDEO_REJECT
1228 return __bluetooth_pb_query_phone_log(match, size);
1231 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void)
1236 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1237 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING
1241 return __bluetooth_pb_query_phone_log(match, size);
1244 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void)
1249 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1250 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN,
1251 CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN,
1252 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN
1256 return __bluetooth_pb_query_phone_log(match, size);
1259 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void)
1264 CONTACTS_PLOG_TYPE_VOICE_INCOMING,
1265 CONTACTS_PLOG_TYPE_VIDEO_INCOMING,
1266 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1267 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING,
1268 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1269 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN,
1270 CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN,
1271 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN,
1272 CONTACTS_PLOG_TYPE_VOICE_REJECT,
1273 CONTACTS_PLOG_TYPE_VIDEO_REJECT
1277 return __bluetooth_pb_query_phone_log(match, size);
1280 static gboolean __bluetooth_pb_get_count(PhoneBookType pb_type,
1284 contacts_query_h query = NULL;
1290 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1293 query = __bluetooth_pb_query_phone_log_incoming();
1296 query = __bluetooth_pb_query_phone_log_outgoing();
1299 query = __bluetooth_pb_query_phone_log_missed();
1302 query = __bluetooth_pb_query_phone_log_combined();
1304 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1306 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1316 status = contacts_db_get_count_with_query(query, &signed_count);
1318 if (status != CONTACTS_ERROR_NONE) {
1319 contacts_query_destroy(query);
1323 contacts_query_destroy(query);
1325 if (signed_count < 0)
1328 *count = (gint) signed_count;
1334 static gboolean __bluetooth_pb_get_count_new_missed_call(guint *count)
1337 contacts_query_h query = NULL;
1342 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1343 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN
1346 query = __bluetooth_pb_query_phone_log(match, size);
1351 status = contacts_db_get_count_with_query(query, &signed_count);
1353 if (status != CONTACTS_ERROR_NONE) {
1354 contacts_query_destroy(query);
1358 contacts_query_destroy(query);
1360 if (signed_count < 0)
1363 *count = (guint)signed_count;
1369 static const char *__bluetooth_pb_phone_log_get_log_type(contacts_record_h record)
1375 status = contacts_record_get_int(record, _contacts_phone_log.log_type,
1378 if (status != CONTACTS_ERROR_NONE)
1382 case CONTACTS_PLOG_TYPE_VOICE_INCOMING:
1383 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING:
1384 case CONTACTS_PLOG_TYPE_VOICE_REJECT:
1385 case CONTACTS_PLOG_TYPE_VIDEO_REJECT:
1387 case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
1388 case CONTACTS_PLOG_TYPE_VIDEO_OUTGOING:
1390 case CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN:
1391 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN:
1392 case CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN:
1393 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN:
1401 static void __bluetooth_pb_get_vcards(PbAgentData *agent, PhoneBookType pb_type,
1402 guint64 filter, guint8 format, guint16 max_list_count,
1403 guint16 list_start_offset, GVariantBuilder *vcards)
1406 contacts_list_h record_list = NULL;
1407 contacts_query_h query = NULL;
1411 guint property_id = 0;
1412 const char *attr = NULL;
1413 gboolean get_log = FALSE;
1415 /* contact offset is n - 1 of PBAP */
1416 offset = (gint)list_start_offset - 1;
1418 if (max_list_count >= 65535)
1419 limit = -1; /* contact limit -1 means unrestricted */
1421 limit = (gint)max_list_count;
1425 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1429 if (list_start_offset == 0) {
1432 vcard = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
1435 g_variant_builder_add(vcards, "s", vcard);
1445 if (pb_type == TELECOM_PB)
1446 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1447 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1448 else if (pb_type == SIM_PB)
1449 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1452 property_id = _contacts_person.id;
1455 query = __bluetooth_pb_query_phone_log_incoming();
1456 property_id = _contacts_phone_log.id;
1460 query = __bluetooth_pb_query_phone_log_outgoing();
1461 property_id = _contacts_phone_log.id;
1465 query = __bluetooth_pb_query_phone_log_missed();
1466 property_id = _contacts_phone_log.id;
1470 query = __bluetooth_pb_query_phone_log_combined();
1471 property_id = _contacts_phone_log.id;
1477 INFO("Limit is = %d and offset is =%d\n", limit, offset);
1479 if (query == NULL) {
1480 ERR("Query is NULL");
1483 /* When limit is passed as ZERO to contacts_db_get_records_with_query
1484 * API then this API will provide all available contacts in its database
1485 * (unrestricted). Now consider a case when client requests for
1486 * maxlistcount of 1 and start offset as 0 then we have already read the
1487 * owner card in above switch case and when it reads owner card it
1488 * decrements the limit by 1.
1491 status = contacts_db_get_records_with_query(query, offset,
1492 limit, &record_list);
1494 if (status != CONTACTS_ERROR_NONE) {
1495 contacts_list_destroy(record_list, TRUE);
1496 contacts_query_destroy(query);
1500 status = contacts_list_first(record_list);
1502 if (status != CONTACTS_ERROR_NONE) {
1503 contacts_list_destroy(record_list, TRUE);
1504 contacts_query_destroy(query);
1509 contacts_record_h record;
1511 gchar *vcard = NULL;
1513 status = contacts_list_get_current_record_p(record_list,
1516 if (status != CONTACTS_ERROR_NONE)
1518 status = contacts_record_get_int(record, property_id,
1521 if (status != CONTACTS_ERROR_NONE)
1524 if (property_id == _contacts_person.id)
1525 vcard = _bluetooth_pb_vcard_contact(id, filter,
1529 attr = __bluetooth_pb_phone_log_get_log_type(record);
1531 vcard = _bluetooth_pb_vcard_call(id, filter,
1536 g_variant_builder_add(vcards, "s", vcard);
1538 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1539 contacts_list_destroy(record_list, TRUE);
1542 contacts_query_destroy(query);
1547 static void __bluetooth_pb_get_contact_list(PbAgentData *agent,
1548 contacts_query_h query, GVariantBuilder *builder)
1551 contacts_list_h record_list = NULL;
1559 tmp = _bluetooth_pb_owner_name();
1560 name = g_strdup_printf("%s;;;;", tmp);
1563 __bluetooth_pb_list_ptr_array_add(builder, name,
1564 agent->tel_number, 0);
1569 if (query == NULL) {
1570 ERR("Query is NULL");
1574 status = contacts_db_get_records_with_query(query, -1, -1,
1577 if (status != CONTACTS_ERROR_NONE) {
1578 contacts_list_destroy(record_list, TRUE);
1582 status = contacts_list_first(record_list);
1584 if (status != CONTACTS_ERROR_NONE) {
1585 contacts_list_destroy(record_list, TRUE);
1590 contacts_record_h record;
1593 status = contacts_list_get_current_record_p(record_list,
1596 if (status != CONTACTS_ERROR_NONE)
1599 status = contacts_record_get_int(record,
1600 _contacts_person_contact.person_id, &id);
1602 if (status != CONTACTS_ERROR_NONE)
1610 name = _bluetooth_pb_name_from_person_id(id);
1611 number = _bluetooth_pb_number_from_person_id(id);
1613 __bluetooth_pb_list_ptr_array_add(builder, name,
1620 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1622 contacts_list_destroy(record_list, TRUE);
1626 static void __bluetooth_pb_get_phone_log_list(PbAgentData *agent,
1627 contacts_query_h query, GVariantBuilder *builder)
1630 contacts_list_h record_list = NULL;
1633 status = contacts_db_get_records_with_query(query, -1, -1,
1636 if (status != CONTACTS_ERROR_NONE)
1639 status = contacts_list_first(record_list);
1641 if (status != CONTACTS_ERROR_NONE) {
1642 contacts_list_destroy(record_list, TRUE);
1647 contacts_record_h record;
1650 status = contacts_list_get_current_record_p(record_list,
1653 if (status != CONTACTS_ERROR_NONE)
1656 status = contacts_record_get_int(record,
1657 _contacts_phone_log.id, &id);
1659 if (status != CONTACTS_ERROR_NONE)
1667 name = _bluetooth_pb_name_from_phonelog_id(id);
1669 contacts_record_get_str_p(record,
1670 _contacts_phone_log.address, &number);
1672 __bluetooth_pb_list_ptr_array_add(builder, name,
1678 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1680 contacts_list_destroy(record_list, TRUE);
1685 static void __bluetooth_pb_get_list(PbAgentData *agent, PhoneBookType pb_type,
1686 GVariantBuilder *builder)
1689 contacts_query_h query;
1691 /* no requires refresh cache */
1692 if (builder == NULL && agent->pb_type == pb_type)
1697 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1698 __bluetooth_pb_get_contact_list(agent, query, builder);
1701 query = __bluetooth_pb_query_phone_log_incoming();
1702 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1705 query = __bluetooth_pb_query_phone_log_outgoing();
1706 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1709 query = __bluetooth_pb_query_phone_log_missed();
1710 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1713 query = __bluetooth_pb_query_phone_log_combined();
1714 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1716 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1718 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1719 __bluetooth_pb_get_contact_list(agent, query, builder);
1726 agent->pb_type = pb_type;
1729 contacts_query_destroy(query);
1733 static void __bluetooth_pb_get_contact_list_number(PbAgentData *agent,
1734 contacts_query_h query, gint start_index,
1735 gint end_index, GVariantBuilder *builder)
1738 contacts_list_h record_list = NULL;
1754 offset = to - from + 1;
1760 status = contacts_db_get_records_with_query(query, from - 1 , offset,
1763 if (status != CONTACTS_ERROR_NONE) {
1764 contacts_list_destroy(record_list, TRUE);
1768 status = contacts_list_first(record_list);
1770 if (status != CONTACTS_ERROR_NONE) {
1771 contacts_list_destroy(record_list, TRUE);
1776 contacts_record_h record;
1777 gchar *display_name;
1780 status = contacts_list_get_current_record_p(record_list,
1783 if (status != CONTACTS_ERROR_NONE)
1786 contacts_record_get_str_p(record,
1787 _contacts_person_number.display_name,
1789 contacts_record_get_str_p(record,
1790 _contacts_person_number.number, &number);
1792 __bluetooth_pb_list_ptr_array_add(builder, display_name,
1796 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1798 contacts_list_destroy(record_list, TRUE);
1802 static void __bluetooth_pb_get_phone_log_list_number(PbAgentData *agent,
1803 contacts_query_h query, gint start_index,
1804 gint end_index, GVariantBuilder *builder)
1807 contacts_list_h record_list = NULL;
1823 offset = to - from + 1;
1829 status = contacts_db_get_records_with_query(query, from - 1 , offset,
1832 if (status != CONTACTS_ERROR_NONE) {
1833 contacts_list_destroy(record_list, TRUE);
1837 status = contacts_list_first(record_list);
1838 if (status != CONTACTS_ERROR_NONE) {
1839 contacts_list_destroy(record_list, TRUE);
1844 contacts_record_h record;
1846 gchar *display_name;
1849 status = contacts_list_get_current_record_p(record_list,
1852 if (status != CONTACTS_ERROR_NONE)
1855 status = contacts_record_get_int(record,
1856 _contacts_phone_log.id, &id);
1857 if (status != CONTACTS_ERROR_NONE) {
1858 ERR("contact_record_get_int api failed %d", status);
1862 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
1864 contacts_record_get_str_p(record, _contacts_phone_log.address,
1867 __bluetooth_pb_list_ptr_array_add(builder, display_name,
1872 g_free(display_name);
1874 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1876 contacts_list_destroy(record_list, TRUE);
1880 static void __bluetooth_pb_get_list_number(PbAgentData *agent,
1881 PhoneBookType pb_type, gint start_index,
1882 gint end_index, GVariantBuilder *builder)
1885 contacts_query_h query;
1887 DBG("type = %d", pb_type);
1890 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1891 __bluetooth_pb_get_contact_list_number(agent, query,
1892 start_index, end_index, builder);
1895 query = __bluetooth_pb_query_phone_log_incoming();
1896 __bluetooth_pb_get_phone_log_list_number(agent, query,
1897 start_index, end_index, builder);
1900 query = __bluetooth_pb_query_phone_log_outgoing();
1901 __bluetooth_pb_get_phone_log_list_number(agent, query,
1902 start_index, end_index, builder);
1905 query = __bluetooth_pb_query_phone_log_missed();
1906 __bluetooth_pb_get_phone_log_list_number(agent, query,
1907 start_index, end_index, builder);
1910 query = __bluetooth_pb_query_phone_log_combined();
1911 __bluetooth_pb_get_phone_log_list_number(agent, query,
1912 start_index, end_index, builder);
1914 #ifdef TIZEN_FEATURE_BT_PBAP_SIM
1916 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1917 __bluetooth_pb_get_contact_list_number(agent, query,
1918 start_index, end_index, builder);
1927 contacts_query_destroy(query);
1931 static void __bluetooth_pb_get_contact_list_name(PbAgentData *agent,
1932 contacts_query_h query, const gchar *find_text,
1933 GVariantBuilder *builder)
1936 contacts_list_h record_list = NULL;
1940 status = contacts_db_get_records_with_query(query,
1941 -1, -1, &record_list);
1943 if (status != CONTACTS_ERROR_NONE) {
1944 contacts_list_destroy(record_list, TRUE);
1948 status = contacts_list_first(record_list);
1950 if (status != CONTACTS_ERROR_NONE) {
1951 contacts_list_destroy(record_list, TRUE);
1956 contacts_record_h record;
1957 gchar *display_name;
1959 status = contacts_list_get_current_record_p(record_list,
1962 if (status != CONTACTS_ERROR_NONE)
1965 contacts_record_get_str_p(record,
1966 _contacts_person_number.display_name, &display_name);
1968 if (g_str_has_prefix(display_name, find_text)) {
1971 contacts_record_get_str_p(record,
1972 _contacts_person_number.number, &number);
1974 __bluetooth_pb_list_ptr_array_add(builder, display_name,
1979 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1980 contacts_list_destroy(record_list, TRUE);
1984 static void __bluetooth_pb_get_phone_log_list_name(PbAgentData *agent,
1985 contacts_query_h query, const gchar *find_text,
1986 GVariantBuilder *builder)
1989 contacts_list_h record_list = NULL;
1993 status = contacts_db_get_records_with_query(query, -1, -1,
1996 if (status != CONTACTS_ERROR_NONE) {
1997 contacts_list_destroy(record_list, TRUE);
2001 status = contacts_list_first(record_list);
2003 if (status != CONTACTS_ERROR_NONE) {
2004 contacts_list_destroy(record_list, TRUE);
2009 contacts_record_h record;
2011 gchar *display_name;
2013 status = contacts_list_get_current_record_p(record_list,
2016 if (status != CONTACTS_ERROR_NONE)
2019 status = contacts_record_get_int(record,
2020 _contacts_phone_log.id, &id);
2021 if (status != CONTACTS_ERROR_NONE) {
2022 ERR("contacts_record_get_int failed %d", status);
2026 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
2028 if (g_str_has_prefix(display_name, find_text)) {
2031 contacts_record_get_str_p(record,
2032 _contacts_phone_log.address, &number);
2034 __bluetooth_pb_list_ptr_array_add(builder, display_name,
2040 g_free(display_name);
2042 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
2044 contacts_list_destroy(record_list, TRUE);
2048 static void __bluetooth_pb_get_list_name(PbAgentData *agent,
2049 PhoneBookType pb_type, const gchar *find_text,
2050 GVariantBuilder *builder)
2053 contacts_query_h query;
2057 query = __bluetooth_pb_query_person_number();
2058 __bluetooth_pb_get_contact_list_name(agent, query,
2059 find_text, builder);
2062 query = __bluetooth_pb_query_phone_log_incoming();
2063 __bluetooth_pb_get_phone_log_list_name(agent, query,
2064 find_text, builder);
2067 query = __bluetooth_pb_query_phone_log_outgoing();
2068 __bluetooth_pb_get_phone_log_list_name(agent, query,
2069 find_text, builder);
2072 query = __bluetooth_pb_query_phone_log_missed();
2073 __bluetooth_pb_get_phone_log_list_name(agent, query,
2074 find_text, builder);
2077 query = __bluetooth_pb_query_phone_log_combined();
2078 __bluetooth_pb_get_phone_log_list_name(agent, query,
2079 find_text, builder);
2086 contacts_query_destroy(query);
2090 static void __bluetooth_pb_list_ptr_array_add(GVariantBuilder *builder,
2091 const gchar *name, const gchar *number, gint handle)
2094 gchar *temp_name = g_strdup(name);
2095 gchar *temp_number = g_strdup(number);
2097 g_variant_builder_add(builder, "(ssu)", temp_name, temp_number, handle);
2104 static void __bluetooth_pb_agent_signal_handler(int signum)
2108 g_main_loop_quit(g_mainloop);
2110 DBG("Terminate Bluetooth PBAP agent");
2115 static void __bluetooth_pb_contact_changed(const gchar *view_uri,
2119 guint new_missed_call;
2120 PbAgentData *agent = (PbAgentData *)user_data;
2121 GDBusConnection *conn = __bt_pb_get_gdbus_connection();
2123 DBG("Received contact changed cb");
2125 g_dbus_connection_emit_signal(conn, NULL,
2126 "/org/bluez/pb_agent", "org.bluez.PbAgent",
2127 "clear", NULL, NULL);
2129 bluetooth_pb_agent_clear(agent);
2131 __bluetooth_pb_get_count_new_missed_call(&new_missed_call);
2133 if (new_missed_call > total_missed_call_count)
2134 unnotified_missed_call_count += new_missed_call -
2135 total_missed_call_count;
2137 INFO("Missed call count : #prev[%d], #current[%d], #unnotified[%d]",
2138 total_missed_call_count, new_missed_call,
2139 unnotified_missed_call_count);
2141 total_missed_call_count = new_missed_call;
2145 static void __bluetooth_pb_agent_timeout_add_seconds(PbAgentData *agent)
2149 if (agent->timeout_id)
2150 g_source_remove(agent->timeout_id);
2152 agent->timeout_id = g_timeout_add_seconds(BLUETOOTH_PB_AGENT_TIMEOUT,
2153 __bluetooth_pb_agent_timeout_calback, agent);
2157 static gboolean __bluetooth_pb_agent_timeout_calback(gpointer user_data)
2160 PbAgentData *agent = (PbAgentData *)user_data;
2162 agent->timeout_id = 0;
2165 g_main_loop_quit(g_mainloop);
2171 static void __bluetooth_pb_tel_callback(TapiHandle *handle, int result,
2172 void *data, void *user_data)
2175 PbAgentData *agent = (PbAgentData *)user_data;
2176 TelSimMsisdnList_t *number;
2178 __bt_pb_dbus_init(agent);
2181 number = (TelSimMsisdnList_t *)data;
2182 agent->tel_number = g_strdup(number->list[0].num);
2185 tel_deinit(agent->tapi_handle);
2186 agent->tapi_handle = NULL;
2190 static gboolean __bt_pb_destroy_agent()
2193 g_main_loop_quit(g_mainloop);
2198 static GDBusNodeInfo *__bt_pb_create_method_node_info
2199 (const gchar *introspection_data)
2202 GDBusNodeInfo *node_info = NULL;
2204 if (introspection_data == NULL)
2207 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
2210 ERR("Unable to create node: %s", err->message);
2211 g_clear_error(&err);
2216 static gboolean __bt_pb_dbus_init(PbAgentData *agent)
2220 GDBusNodeInfo *node_info = NULL;
2221 GError *error = NULL;
2222 GDBusConnection *conn = __bt_pb_get_gdbus_connection();
2225 ERR("Error in creating the gdbus connection");
2229 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, BT_PB_SERVICE_NAME,
2230 G_BUS_NAME_OWNER_FLAGS_NONE, NULL,
2231 NULL, NULL, NULL, NULL);
2235 node_info = __bt_pb_create_method_node_info(pb_agent_introspection_xml);
2236 if (node_info == NULL)
2239 pb_id = g_dbus_connection_register_object(conn, BT_PB_SERVICE_OBJECT_PATH,
2240 node_info->interfaces[0], &method_table,
2241 agent, NULL, &error);
2244 ERR("Failed to register: %s", error->message);
2245 g_clear_error(&error);
2249 agent->pbagent_interface_id = pb_id;
2251 pb_id = g_dbus_connection_register_object(conn, BT_PB_SERVICE_OBJECT_PATH,
2252 node_info->interfaces[1], &method_table,
2253 agent, NULL, &error);
2256 ERR("Failed to register: %s", error->message);
2257 g_clear_error(&error);
2261 g_dbus_node_info_unref(node_info);
2263 agent->pbagent_at_interface_id = pb_id;
2269 g_object_unref(pb_dbus_conn);
2270 pb_dbus_conn = NULL;
2274 g_object_unref(conn);
2277 g_dbus_node_info_unref(node_info);
2280 g_bus_unown_name(owner_id);
2285 static gboolean __bt_pb_dbus_deinit(PbAgentData *agent)
2288 if (agent->pbagent_interface_id != 0) {
2289 g_dbus_connection_unregister_object(pb_dbus_conn,
2290 agent->pbagent_interface_id);
2291 agent->pbagent_interface_id = 0;
2293 if (agent->pbagent_at_interface_id != 0) {
2294 g_dbus_connection_unregister_object(pb_dbus_conn,
2295 agent->pbagent_at_interface_id);
2296 agent->pbagent_at_interface_id = 0;
2299 g_object_unref(pb_dbus_conn);
2300 pb_dbus_conn = NULL;
2311 gint ret = EXIT_SUCCESS;
2313 struct sigaction sa;
2315 DBG("Starting Bluetooth PBAP agent");
2317 g_mainloop = g_main_loop_new(NULL, FALSE);
2318 if (g_mainloop == NULL) {
2319 ERR("Couldn't create GMainLoop\n");
2320 return EXIT_FAILURE;
2323 agent = (PbAgentData *)g_malloc(sizeof(PbAgentData));
2326 return EXIT_FAILURE;
2328 agent->pbagent_interface_id = 0;
2329 agent->pbagent_at_interface_id = 0;
2330 agent->tel_number = NULL;
2332 /* connect contact */
2333 if (contacts_connect() != CONTACTS_ERROR_NONE) {
2334 ERR("Can not connect contacts server\n");
2336 return EXIT_FAILURE;
2339 __bluetooth_pb_get_count_new_missed_call(&total_missed_call_count);
2341 if (contacts_db_add_changed_cb(_contacts_contact._uri,
2342 __bluetooth_pb_contact_changed,
2343 (void *)agent) != CONTACTS_ERROR_NONE) {
2344 ERR("Can not add changed callback");
2347 if (contacts_db_add_changed_cb(_contacts_phone_log._uri,
2348 __bluetooth_pb_contact_changed,
2349 (void *)agent) != CONTACTS_ERROR_NONE) {
2350 ERR("Can not add changed callback");
2354 memset(&sa, 0, sizeof(sa));
2355 sa.sa_handler = __bluetooth_pb_agent_signal_handler;
2356 sigaction(SIGTERM, &sa, NULL);
2357 sigaction(SIGINT, &sa, NULL);
2360 agent->tapi_handle = tel_init(NULL);
2361 tapi_result = tel_get_sim_msisdn(agent->tapi_handle,
2362 __bluetooth_pb_tel_callback, agent);
2364 if (tapi_result != TAPI_API_SUCCESS)
2365 __bt_pb_dbus_init(agent);
2367 __bluetooth_pb_agent_timeout_add_seconds(agent);
2369 g_main_loop_run(g_mainloop);
2371 if (contacts_db_remove_changed_cb(_contacts_phone_log._uri,
2372 __bluetooth_pb_contact_changed,
2373 (void *)agent) != CONTACTS_ERROR_NONE) {
2374 ERR("Cannot remove changed callback");
2377 if (contacts_db_remove_changed_cb(_contacts_contact._uri,
2378 __bluetooth_pb_contact_changed,
2379 (void *)agent) != CONTACTS_ERROR_NONE) {
2380 ERR("Cannot remove changed callback");
2383 if (contacts_disconnect() != CONTACTS_ERROR_NONE)
2384 ERR("contacts_disconnect failed \n");
2386 g_dbus_connection_emit_signal(pb_dbus_conn, NULL,
2387 "/org/bluez/pb_agent", "org.bluez.PbAgent",
2388 "clear", NULL, NULL);
2390 bluetooth_pb_agent_clear(agent);
2392 if (agent->tapi_handle)
2393 tel_deinit(agent->tapi_handle);
2394 if (agent->tel_number)
2395 g_free(agent->tel_number);
2397 __bt_pb_dbus_deinit(agent);
2401 DBG("Terminate Bluetooth PBAP agent");
2405 /* LCOV_EXCL_STOP */