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 PBAP_SIM_ENABLE
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 PBAP_SIM_ENABLE
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 PBAP_SIM_ENABLE
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);
941 err = contacts_svc_disconnect();
942 ERR("contacts_svc_disconnect fucntion call [error] = %d \n", err);
944 if (NULL != stream) {
953 /* Create GError from error code and error message*/
954 static GError *__bt_pb_error(gint error_code, const gchar *error_message)
956 return g_error_new(g_quark_from_string("PB Agent"),
957 error_code, "PB Agent Error: %s", error_message);
960 static PhoneBookType __bluetooth_pb_get_pb_type(const char *name)
963 gchar *suffix = ".vcf";
973 if (g_str_has_suffix(name, suffix))
974 len -= strlen(suffix);
976 size = G_N_ELEMENTS(bluetooth_pb_agent_folder_list) - 1;
977 for (i = 0; i < size; i++) {
978 if (strncmp(name, bluetooth_pb_agent_folder_list[i], len) == 0)
986 static PhoneBookType __bluetooth_pb_get_storage_pb_type(const char *name)
992 if (g_strcmp0(name, "\"ME\"") == 0)
995 if (g_strcmp0(name, "\"RC\"") == 0)
998 if (g_strcmp0(name, "\"DC\"") == 0)
1001 if (g_strcmp0(name, "\"MC\"") == 0)
1003 #ifdef PBAP_SIM_ENABLE
1004 if (g_strcmp0(name, "\"SM\"") == 0)
1008 return TELECOM_NONE;
1011 static gint __bluetooth_pb_phone_log_filter_append(contacts_filter_h filter,
1012 gint *match, gint size)
1018 for (i = 0; i < size; i++) {
1021 status = contacts_filter_add_operator(filter,
1022 CONTACTS_FILTER_OPERATOR_OR);
1024 if (status != CONTACTS_ERROR_NONE)
1028 status = contacts_filter_add_int(filter,
1029 _contacts_phone_log.log_type,
1030 CONTACTS_MATCH_EQUAL, match[i]);
1032 if (status != CONTACTS_ERROR_NONE)
1037 return CONTACTS_ERROR_NONE;
1040 static contacts_query_h __bluetooth_pb_query_phone_log(gint *match, gint size)
1043 contacts_query_h query = NULL;
1044 contacts_filter_h filter = NULL;
1047 status = contacts_query_create(_contacts_phone_log._uri, &query);
1049 if (status != CONTACTS_ERROR_NONE)
1052 status = contacts_filter_create(_contacts_phone_log._uri, &filter);
1054 if (status != CONTACTS_ERROR_NONE) {
1055 contacts_query_destroy(query);
1059 status = __bluetooth_pb_phone_log_filter_append(filter, match, size);
1061 if (status != CONTACTS_ERROR_NONE) {
1062 contacts_filter_destroy(filter);
1063 contacts_query_destroy(query);
1067 status = contacts_query_set_filter(query, filter);
1069 if (status != CONTACTS_ERROR_NONE) {
1070 contacts_filter_destroy(filter);
1071 contacts_query_destroy(query);
1075 status = contacts_query_set_sort(query, _contacts_phone_log.log_time,
1078 if (status != CONTACTS_ERROR_NONE) {
1079 contacts_filter_destroy(filter);
1080 contacts_query_destroy(query);
1084 contacts_filter_destroy(filter);
1090 bool __bt_is_matching_addressbook(const char *addressbook_name, int addressbook)
1092 bool is_sim_addressbook = _bt_is_sim_addressbook(addressbook_name);
1094 if ((is_sim_addressbook == false
1095 && addressbook == PBAP_ADDRESSBOOK_PHONE) ||
1096 (is_sim_addressbook == true
1097 && addressbook == PBAP_ADDRESSBOOK_SIM))
1103 static contacts_query_h __bluetooth_pb_query_person(int addressbook)
1106 contacts_query_h query = NULL;
1107 contacts_filter_h filter = NULL;
1108 contacts_list_h recordList = NULL;
1109 contacts_record_h record = NULL;
1110 char *addressbook_name = NULL;
1111 int address_book_id = 0;
1115 bool is_first_condition = true;
1117 DBG("Addressbook [%d]", addressbook);
1119 status = contacts_query_create(_contacts_person_contact._uri, &query);
1121 ERR("Could not create query");
1125 /* Create addressbook Filter*/
1126 status = contacts_db_get_all_records(_contacts_address_book._uri, 0, 0,
1128 if (status != CONTACTS_ERROR_NONE)
1129 ERR("Contact list get api failed %d", status);
1131 contacts_filter_create(_contacts_person_contact._uri, &filter);
1132 contacts_list_get_count(recordList, &count);
1134 for (i = 0; i < count; i++) {
1135 status = contacts_list_get_current_record_p(recordList, &record);
1136 if (status != CONTACTS_ERROR_NONE) {
1137 ERR("Contact list get api failed %d", status);
1140 status = contacts_record_get_str_p(record,
1141 _contacts_address_book.name,
1143 if (status != CONTACTS_ERROR_NONE) {
1144 ERR("Contact record get api failed %d", status);
1147 status = contacts_record_get_int(record,
1148 _contacts_address_book.id,
1150 if (status != CONTACTS_ERROR_NONE) {
1151 ERR("contacts record get int api failed %d", status);
1155 DBG("Addressbook ID: [%d] Addressbook Name: [%s]",
1156 address_book_id, addressbook_name);
1158 if (__bt_is_matching_addressbook(addressbook_name,
1160 if (is_first_condition)
1161 is_first_condition = false;
1163 contacts_filter_add_operator(filter,
1164 CONTACTS_FILTER_OPERATOR_OR);
1165 DBG("SELECTED Addressbook ID: [%d] Addressbook Name: [%s]",
1166 address_book_id, addressbook_name);
1167 status = contacts_filter_add_int(filter,
1168 _contacts_person_contact.address_book_id,
1169 CONTACTS_MATCH_EQUAL, address_book_id);
1170 if (status != CONTACTS_ERROR_NONE)
1171 ERR("Contact filter add failed %d", status);
1174 if (contacts_list_next(recordList) != CONTACTS_ERROR_NONE)
1178 contacts_list_destroy(recordList, true);
1180 status = contacts_query_set_filter(query, filter);
1181 if (status != CONTACTS_ERROR_NONE)
1182 ERR("Could not Apply Filter");
1184 contacts_filter_destroy(filter);
1189 static contacts_query_h __bluetooth_pb_query_person_number(void)
1192 contacts_query_h query = NULL;
1195 status = contacts_query_create(_contacts_person_number._uri, &query);
1197 if (status != CONTACTS_ERROR_NONE)
1204 static contacts_query_h __bluetooth_pb_query_phone_log_incoming(void)
1209 CONTACTS_PLOG_TYPE_VOICE_INCOMING,
1210 CONTACTS_PLOG_TYPE_VIDEO_INCOMING,
1211 CONTACTS_PLOG_TYPE_VOICE_REJECT,
1212 CONTACTS_PLOG_TYPE_VIDEO_REJECT
1216 return __bluetooth_pb_query_phone_log(match, size);
1219 static contacts_query_h __bluetooth_pb_query_phone_log_outgoing(void)
1224 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1225 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING
1229 return __bluetooth_pb_query_phone_log(match, size);
1232 static contacts_query_h __bluetooth_pb_query_phone_log_missed(void)
1237 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1238 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN,
1239 CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN,
1240 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN
1244 return __bluetooth_pb_query_phone_log(match, size);
1247 static contacts_query_h __bluetooth_pb_query_phone_log_combined(void)
1252 CONTACTS_PLOG_TYPE_VOICE_INCOMING,
1253 CONTACTS_PLOG_TYPE_VIDEO_INCOMING,
1254 CONTACTS_PLOG_TYPE_VOICE_OUTGOING,
1255 CONTACTS_PLOG_TYPE_VIDEO_OUTGOING,
1256 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1257 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN,
1258 CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN,
1259 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN,
1260 CONTACTS_PLOG_TYPE_VOICE_REJECT,
1261 CONTACTS_PLOG_TYPE_VIDEO_REJECT
1265 return __bluetooth_pb_query_phone_log(match, size);
1268 static gboolean __bluetooth_pb_get_count(PhoneBookType pb_type,
1272 contacts_query_h query = NULL;
1278 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1281 query = __bluetooth_pb_query_phone_log_incoming();
1284 query = __bluetooth_pb_query_phone_log_outgoing();
1287 query = __bluetooth_pb_query_phone_log_missed();
1290 query = __bluetooth_pb_query_phone_log_combined();
1292 #ifdef PBAP_SIM_ENABLE
1294 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1304 status = contacts_db_get_count_with_query(query, &signed_count);
1306 if (status != CONTACTS_ERROR_NONE) {
1307 contacts_query_destroy(query);
1311 contacts_query_destroy(query);
1313 if (signed_count < 0)
1316 *count = (gint) signed_count;
1322 static gboolean __bluetooth_pb_get_count_new_missed_call(guint *count)
1325 contacts_query_h query = NULL;
1330 CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN,
1331 CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN
1334 query = __bluetooth_pb_query_phone_log(match, size);
1339 status = contacts_db_get_count_with_query(query, &signed_count);
1341 if (status != CONTACTS_ERROR_NONE) {
1342 contacts_query_destroy(query);
1346 contacts_query_destroy(query);
1348 if (signed_count < 0)
1351 *count = (guint)signed_count;
1357 static const char *__bluetooth_pb_phone_log_get_log_type(contacts_record_h record)
1363 status = contacts_record_get_int(record, _contacts_phone_log.log_type,
1366 if (status != CONTACTS_ERROR_NONE)
1370 case CONTACTS_PLOG_TYPE_VOICE_INCOMING:
1371 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING:
1372 case CONTACTS_PLOG_TYPE_VOICE_REJECT:
1373 case CONTACTS_PLOG_TYPE_VIDEO_REJECT:
1375 case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
1376 case CONTACTS_PLOG_TYPE_VIDEO_OUTGOING:
1378 case CONTACTS_PLOG_TYPE_VOICE_INCOMING_UNSEEN:
1379 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING_UNSEEN:
1380 case CONTACTS_PLOG_TYPE_VOICE_INCOMING_SEEN:
1381 case CONTACTS_PLOG_TYPE_VIDEO_INCOMING_SEEN:
1389 static void __bluetooth_pb_get_vcards(PbAgentData *agent, PhoneBookType pb_type,
1390 guint64 filter, guint8 format, guint16 max_list_count,
1391 guint16 list_start_offset, GVariantBuilder *vcards)
1394 contacts_list_h record_list = NULL;
1395 contacts_query_h query = NULL;
1399 guint property_id = 0;
1400 const char *attr = NULL;
1401 gboolean get_log = FALSE;
1403 /* contact offset is n - 1 of PBAP */
1404 offset = (gint)list_start_offset - 1;
1406 if (max_list_count >= 65535)
1407 limit = -1; /* contact limit -1 means unrestricted */
1409 limit = (gint)max_list_count;
1413 #ifdef PBAP_SIM_ENABLE
1417 if (list_start_offset == 0) {
1420 vcard = _bluetooth_pb_vcard_contact_owner(agent->tel_number,
1423 g_variant_builder_add(vcards, "s", vcard);
1433 if (pb_type == TELECOM_PB)
1434 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1435 #ifdef PBAP_SIM_ENABLE
1436 else if (pb_type == SIM_PB)
1437 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1440 property_id = _contacts_person.id;
1443 query = __bluetooth_pb_query_phone_log_incoming();
1444 property_id = _contacts_phone_log.id;
1448 query = __bluetooth_pb_query_phone_log_outgoing();
1449 property_id = _contacts_phone_log.id;
1453 query = __bluetooth_pb_query_phone_log_missed();
1454 property_id = _contacts_phone_log.id;
1458 query = __bluetooth_pb_query_phone_log_combined();
1459 property_id = _contacts_phone_log.id;
1465 INFO("Limit is = %d and offset is =%d\n", limit, offset);
1467 /* When limit is passed as ZERO to contacts_db_get_records_with_query
1468 * API then this API will provide all available contacts in its database
1469 * (unrestricted). Now consider a case when client requests for
1470 * maxlistcount of 1 and start offset as 0 then we have already read the
1471 * owner card in above switch case and when it reads owner card it
1472 * decrements the limit by 1.
1475 status = contacts_db_get_records_with_query(query, offset,
1476 limit, &record_list);
1478 if (status != CONTACTS_ERROR_NONE) {
1479 contacts_list_destroy(record_list, TRUE);
1480 contacts_query_destroy(query);
1484 status = contacts_list_first(record_list);
1486 if (status != CONTACTS_ERROR_NONE) {
1487 contacts_list_destroy(record_list, TRUE);
1488 contacts_query_destroy(query);
1493 contacts_record_h record;
1495 gchar *vcard = NULL;
1497 status = contacts_list_get_current_record_p(record_list,
1500 if (status != CONTACTS_ERROR_NONE)
1502 status = contacts_record_get_int(record, property_id,
1505 if (status != CONTACTS_ERROR_NONE)
1508 if (property_id == _contacts_person.id)
1509 vcard = _bluetooth_pb_vcard_contact(id, filter,
1513 attr = __bluetooth_pb_phone_log_get_log_type(record);
1515 vcard = _bluetooth_pb_vcard_call(id, filter,
1520 g_variant_builder_add(vcards, "s", vcard);
1522 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1523 contacts_list_destroy(record_list, TRUE);
1526 contacts_query_destroy(query);
1531 static void __bluetooth_pb_get_contact_list(PbAgentData *agent,
1532 contacts_query_h query, GVariantBuilder *builder)
1535 contacts_list_h record_list = NULL;
1543 tmp = _bluetooth_pb_owner_name();
1544 name = g_strdup_printf("%s;;;;", tmp);
1547 __bluetooth_pb_list_ptr_array_add(builder, name,
1548 agent->tel_number, 0);
1553 status = contacts_db_get_records_with_query(query, -1, -1,
1556 if (status != CONTACTS_ERROR_NONE) {
1557 contacts_list_destroy(record_list, TRUE);
1561 status = contacts_list_first(record_list);
1563 if (status != CONTACTS_ERROR_NONE) {
1564 contacts_list_destroy(record_list, TRUE);
1569 contacts_record_h record;
1572 status = contacts_list_get_current_record_p(record_list,
1575 if (status != CONTACTS_ERROR_NONE)
1578 status = contacts_record_get_int(record,
1579 _contacts_person_contact.person_id, &id);
1581 if (status != CONTACTS_ERROR_NONE)
1589 name = _bluetooth_pb_name_from_person_id(id);
1590 number = _bluetooth_pb_number_from_person_id(id);
1592 __bluetooth_pb_list_ptr_array_add(builder, name,
1599 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1601 contacts_list_destroy(record_list, TRUE);
1605 static void __bluetooth_pb_get_phone_log_list(PbAgentData *agent,
1606 contacts_query_h query, GVariantBuilder *builder)
1609 contacts_list_h record_list = NULL;
1612 status = contacts_db_get_records_with_query(query, -1, -1,
1615 if (status != CONTACTS_ERROR_NONE)
1618 status = contacts_list_first(record_list);
1620 if (status != CONTACTS_ERROR_NONE) {
1621 contacts_list_destroy(record_list, TRUE);
1626 contacts_record_h record;
1629 status = contacts_list_get_current_record_p(record_list,
1632 if (status != CONTACTS_ERROR_NONE)
1635 status = contacts_record_get_int(record,
1636 _contacts_phone_log.id, &id);
1638 if (status != CONTACTS_ERROR_NONE)
1646 name = _bluetooth_pb_name_from_phonelog_id(id);
1648 contacts_record_get_str_p(record,
1649 _contacts_phone_log.address, &number);
1651 __bluetooth_pb_list_ptr_array_add(builder, name,
1657 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1659 contacts_list_destroy(record_list, TRUE);
1664 static void __bluetooth_pb_get_list(PbAgentData *agent, PhoneBookType pb_type,
1665 GVariantBuilder *builder)
1668 contacts_query_h query;
1670 /* no requires refresh cache */
1671 if (builder == NULL && agent->pb_type == pb_type)
1676 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_PHONE);
1677 __bluetooth_pb_get_contact_list(agent, query, builder);
1680 query = __bluetooth_pb_query_phone_log_incoming();
1681 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1684 query = __bluetooth_pb_query_phone_log_outgoing();
1685 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1688 query = __bluetooth_pb_query_phone_log_missed();
1689 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1692 query = __bluetooth_pb_query_phone_log_combined();
1693 __bluetooth_pb_get_phone_log_list(agent, query, builder);
1695 #ifdef PBAP_SIM_ENABLE
1697 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1698 __bluetooth_pb_get_contact_list(agent, query, builder);
1705 agent->pb_type = pb_type;
1708 contacts_query_destroy(query);
1712 static void __bluetooth_pb_get_contact_list_number(PbAgentData *agent,
1713 contacts_query_h query, gint start_index,
1714 gint end_index, GVariantBuilder *builder)
1717 contacts_list_h record_list = NULL;
1733 offset = to - from + 1;
1739 status = contacts_db_get_records_with_query(query, from - 1 , offset,
1742 if (status != CONTACTS_ERROR_NONE) {
1743 contacts_list_destroy(record_list, TRUE);
1747 status = contacts_list_first(record_list);
1749 if (status != CONTACTS_ERROR_NONE) {
1750 contacts_list_destroy(record_list, TRUE);
1755 contacts_record_h record;
1756 gchar *display_name;
1759 status = contacts_list_get_current_record_p(record_list,
1762 if (status != CONTACTS_ERROR_NONE)
1765 contacts_record_get_str_p(record,
1766 _contacts_person_number.display_name,
1768 contacts_record_get_str_p(record,
1769 _contacts_person_number.number, &number);
1771 __bluetooth_pb_list_ptr_array_add(builder, display_name,
1775 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1777 contacts_list_destroy(record_list, TRUE);
1781 static void __bluetooth_pb_get_phone_log_list_number(PbAgentData *agent,
1782 contacts_query_h query, gint start_index,
1783 gint end_index, GVariantBuilder *builder)
1786 contacts_list_h record_list = NULL;
1802 offset = to - from + 1;
1808 status = contacts_db_get_records_with_query(query, from - 1 , offset,
1811 if (status != CONTACTS_ERROR_NONE) {
1812 contacts_list_destroy(record_list, TRUE);
1816 status = contacts_list_first(record_list);
1817 if (status != CONTACTS_ERROR_NONE) {
1818 contacts_list_destroy(record_list, TRUE);
1823 contacts_record_h record;
1825 gchar *display_name;
1828 status = contacts_list_get_current_record_p(record_list,
1831 if (status != CONTACTS_ERROR_NONE)
1834 status = contacts_record_get_int(record,
1835 _contacts_phone_log.id, &id);
1836 if (status != CONTACTS_ERROR_NONE) {
1837 ERR("contact_record_get_int api failed %d", status);
1841 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
1843 contacts_record_get_str_p(record, _contacts_phone_log.address,
1846 __bluetooth_pb_list_ptr_array_add(builder, display_name,
1851 g_free(display_name);
1853 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1855 contacts_list_destroy(record_list, TRUE);
1859 static void __bluetooth_pb_get_list_number(PbAgentData *agent,
1860 PhoneBookType pb_type, gint start_index,
1861 gint end_index, GVariantBuilder *builder)
1864 contacts_query_h query;
1866 DBG("type = %d", pb_type);
1869 query = __bluetooth_pb_query_person_number();
1870 __bluetooth_pb_get_contact_list_number(agent, query,
1871 start_index, end_index, builder);
1874 query = __bluetooth_pb_query_phone_log_incoming();
1875 __bluetooth_pb_get_phone_log_list_number(agent, query,
1876 start_index, end_index, builder);
1879 query = __bluetooth_pb_query_phone_log_outgoing();
1880 __bluetooth_pb_get_phone_log_list_number(agent, query,
1881 start_index, end_index, builder);
1884 query = __bluetooth_pb_query_phone_log_missed();
1885 __bluetooth_pb_get_phone_log_list_number(agent, query,
1886 start_index, end_index, builder);
1889 query = __bluetooth_pb_query_phone_log_combined();
1890 __bluetooth_pb_get_phone_log_list_number(agent, query,
1891 start_index, end_index, builder);
1893 #ifdef PBAP_SIM_ENABLE
1895 query = __bluetooth_pb_query_person(PBAP_ADDRESSBOOK_SIM);
1896 __bluetooth_pb_get_contact_list_number(agent, query,
1897 start_index, end_index, builder);
1906 contacts_query_destroy(query);
1910 static void __bluetooth_pb_get_contact_list_name(PbAgentData *agent,
1911 contacts_query_h query, const gchar *find_text,
1912 GVariantBuilder *builder)
1915 contacts_list_h record_list = NULL;
1919 status = contacts_db_get_records_with_query(query,
1920 -1, -1, &record_list);
1922 if (status != CONTACTS_ERROR_NONE) {
1923 contacts_list_destroy(record_list, TRUE);
1927 status = contacts_list_first(record_list);
1929 if (status != CONTACTS_ERROR_NONE) {
1930 contacts_list_destroy(record_list, TRUE);
1935 contacts_record_h record;
1936 gchar *display_name;
1938 status = contacts_list_get_current_record_p(record_list,
1941 if (status != CONTACTS_ERROR_NONE)
1944 contacts_record_get_str_p(record,
1945 _contacts_person_number.display_name, &display_name);
1947 if (g_str_has_prefix(display_name, find_text)) {
1950 contacts_record_get_str_p(record,
1951 _contacts_person_number.number, &number);
1953 __bluetooth_pb_list_ptr_array_add(builder, display_name,
1958 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
1959 contacts_list_destroy(record_list, TRUE);
1963 static void __bluetooth_pb_get_phone_log_list_name(PbAgentData *agent,
1964 contacts_query_h query, const gchar *find_text,
1965 GVariantBuilder *builder)
1968 contacts_list_h record_list = NULL;
1972 status = contacts_db_get_records_with_query(query, -1, -1,
1975 if (status != CONTACTS_ERROR_NONE) {
1976 contacts_list_destroy(record_list, TRUE);
1980 status = contacts_list_first(record_list);
1982 if (status != CONTACTS_ERROR_NONE) {
1983 contacts_list_destroy(record_list, TRUE);
1988 contacts_record_h record;
1990 gchar *display_name;
1992 status = contacts_list_get_current_record_p(record_list,
1995 if (status != CONTACTS_ERROR_NONE)
1998 status = contacts_record_get_int(record,
1999 _contacts_phone_log.id, &id);
2000 if (status != CONTACTS_ERROR_NONE) {
2001 ERR("contacts_record_get_int failed %d", status);
2005 display_name = _bluetooth_pb_fn_from_phonelog_id(id);
2007 if (g_str_has_prefix(display_name, find_text)) {
2010 contacts_record_get_str_p(record,
2011 _contacts_phone_log.address, &number);
2013 __bluetooth_pb_list_ptr_array_add(builder, display_name,
2019 g_free(display_name);
2021 } while (contacts_list_next(record_list) == CONTACTS_ERROR_NONE);
2023 contacts_list_destroy(record_list, TRUE);
2027 static void __bluetooth_pb_get_list_name(PbAgentData *agent,
2028 PhoneBookType pb_type, const gchar *find_text,
2029 GVariantBuilder *builder)
2032 contacts_query_h query;
2036 query = __bluetooth_pb_query_person_number();
2037 __bluetooth_pb_get_contact_list_name(agent, query,
2038 find_text, builder);
2041 query = __bluetooth_pb_query_phone_log_incoming();
2042 __bluetooth_pb_get_phone_log_list_name(agent, query,
2043 find_text, builder);
2046 query = __bluetooth_pb_query_phone_log_outgoing();
2047 __bluetooth_pb_get_phone_log_list_name(agent, query,
2048 find_text, builder);
2051 query = __bluetooth_pb_query_phone_log_missed();
2052 __bluetooth_pb_get_phone_log_list_name(agent, query,
2053 find_text, builder);
2056 query = __bluetooth_pb_query_phone_log_combined();
2057 __bluetooth_pb_get_phone_log_list_name(agent, query,
2058 find_text, builder);
2065 contacts_query_destroy(query);
2069 static void __bluetooth_pb_list_ptr_array_add(GVariantBuilder *builder,
2070 const gchar *name, const gchar *number, gint handle)
2073 gchar *temp_name = g_strdup(name);
2074 gchar *temp_number = g_strdup(number);
2076 g_variant_builder_add(builder, "(ssu)", temp_name, temp_number, handle);
2083 static void __bluetooth_pb_agent_signal_handler(int signum)
2087 g_main_loop_quit(g_mainloop);
2089 DBG("Terminate Bluetooth PBAP agent");
2094 static void __bluetooth_pb_contact_changed(const gchar *view_uri,
2098 guint new_missed_call;
2099 PbAgentData *agent = (PbAgentData *)user_data;
2100 GDBusConnection *conn = __bt_pb_get_gdbus_connection();
2102 DBG("Received contact changed cb");
2104 g_dbus_connection_emit_signal(conn, "org.bluez.pb_agent",
2105 "/org/bluez/pb_agent", "org.bluez.PbAgent",
2106 "clear", NULL, NULL);
2108 bluetooth_pb_agent_clear(agent);
2110 __bluetooth_pb_get_count_new_missed_call(&new_missed_call);
2112 if (new_missed_call > total_missed_call_count)
2113 unnotified_missed_call_count += new_missed_call -
2114 total_missed_call_count;
2116 INFO("Missed call count : #prev[%d], #current[%d], #unnotified[%d]",
2117 total_missed_call_count, new_missed_call,
2118 unnotified_missed_call_count);
2120 total_missed_call_count = new_missed_call;
2124 static void __bluetooth_pb_agent_timeout_add_seconds(PbAgentData *agent)
2128 if (agent->timeout_id)
2129 g_source_remove(agent->timeout_id);
2131 agent->timeout_id = g_timeout_add_seconds(BLUETOOTH_PB_AGENT_TIMEOUT,
2132 __bluetooth_pb_agent_timeout_calback, agent);
2136 static gboolean __bluetooth_pb_agent_timeout_calback(gpointer user_data)
2139 PbAgentData *agent = (PbAgentData *)user_data;
2141 agent->timeout_id = 0;
2144 g_main_loop_quit(g_mainloop);
2150 static void __bluetooth_pb_tel_callback(TapiHandle *handle, int result,
2151 void *data, void *user_data)
2154 PbAgentData *agent = (PbAgentData *)user_data;
2155 TelSimMsisdnList_t *number;
2157 __bt_pb_dbus_init(agent);
2160 number = (TelSimMsisdnList_t *)data;
2161 agent->tel_number = g_strdup(number->list[0].num);
2164 tel_deinit(agent->tapi_handle);
2165 agent->tapi_handle = NULL;
2169 static gboolean __bt_pb_destroy_agent()
2172 g_main_loop_quit(g_mainloop);
2177 static GDBusNodeInfo *__bt_pb_create_method_node_info
2178 (const gchar *introspection_data)
2181 GDBusNodeInfo *node_info = NULL;
2183 if (introspection_data == NULL)
2186 node_info = g_dbus_node_info_new_for_xml(introspection_data, &err);
2189 ERR("Unable to create node: %s", err->message);
2190 g_clear_error(&err);
2195 static gboolean __bt_pb_dbus_init(PbAgentData *agent)
2199 GDBusNodeInfo *node_info = NULL;
2200 GError *error = NULL;
2201 GDBusConnection *conn = __bt_pb_get_gdbus_connection();
2204 ERR("Error in creating the gdbus connection");
2208 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, BT_PB_SERVICE_NAME,
2209 G_BUS_NAME_OWNER_FLAGS_NONE, NULL,
2210 NULL, NULL, NULL, NULL);
2214 node_info = __bt_pb_create_method_node_info(pb_agent_introspection_xml);
2215 if (node_info == NULL)
2218 pb_id = g_dbus_connection_register_object(conn, BT_PB_SERVICE_OBJECT_PATH,
2219 node_info->interfaces[0], &method_table,
2220 agent, NULL, &error);
2223 ERR("Failed to register: %s", error->message);
2224 g_clear_error(&error);
2228 agent->pbagent_interface_id = pb_id;
2230 pb_id = g_dbus_connection_register_object(conn, BT_PB_SERVICE_OBJECT_PATH,
2231 node_info->interfaces[1], &method_table,
2232 agent, NULL, &error);
2235 ERR("Failed to register: %s", error->message);
2236 g_clear_error(&error);
2240 g_dbus_node_info_unref(node_info);
2242 agent->pbagent_at_interface_id = pb_id;
2248 g_object_unref(pb_dbus_conn);
2249 pb_dbus_conn = NULL;
2253 g_object_unref(conn);
2256 g_dbus_node_info_unref(node_info);
2259 g_bus_unown_name(owner_id);
2264 static gboolean __bt_pb_dbus_deinit(PbAgentData *agent)
2267 if (agent->pbagent_interface_id != 0) {
2268 g_dbus_connection_unregister_object(pb_dbus_conn,
2269 agent->pbagent_interface_id);
2270 agent->pbagent_interface_id = 0;
2272 if (agent->pbagent_at_interface_id != 0) {
2273 g_dbus_connection_unregister_object(pb_dbus_conn,
2274 agent->pbagent_at_interface_id);
2275 agent->pbagent_at_interface_id = 0;
2278 g_object_unref(pb_dbus_conn);
2279 pb_dbus_conn = NULL;
2290 gint ret = EXIT_SUCCESS;
2292 struct sigaction sa;
2294 DBG("Starting Bluetooth PBAP agent");
2296 g_mainloop = g_main_loop_new(NULL, FALSE);
2297 if (g_mainloop == NULL) {
2298 ERR("Couldn't create GMainLoop\n");
2299 return EXIT_FAILURE;
2302 agent = (PbAgentData *)g_malloc(sizeof(PbAgentData));
2305 return EXIT_FAILURE;
2307 agent->pbagent_interface_id = 0;
2308 agent->pbagent_at_interface_id = 0;
2309 agent->tel_number = NULL;
2311 /* connect contact */
2312 if (contacts_connect() != CONTACTS_ERROR_NONE) {
2313 ERR("Can not connect contacts server\n");
2315 return EXIT_FAILURE;
2318 __bluetooth_pb_get_count_new_missed_call(&total_missed_call_count);
2320 if (contacts_db_add_changed_cb(_contacts_contact._uri,
2321 __bluetooth_pb_contact_changed,
2322 (void *)agent) != CONTACTS_ERROR_NONE) {
2323 ERR("Can not add changed callback");
2326 if (contacts_db_add_changed_cb(_contacts_phone_log._uri,
2327 __bluetooth_pb_contact_changed,
2328 (void *)agent) != CONTACTS_ERROR_NONE) {
2329 ERR("Can not add changed callback");
2333 memset(&sa, 0, sizeof(sa));
2334 sa.sa_handler = __bluetooth_pb_agent_signal_handler;
2335 sigaction(SIGTERM, &sa, NULL);
2336 sigaction(SIGINT, &sa, NULL);
2339 agent->tapi_handle = tel_init(NULL);
2340 tapi_result = tel_get_sim_msisdn(agent->tapi_handle,
2341 __bluetooth_pb_tel_callback, agent);
2343 if (tapi_result != TAPI_API_SUCCESS) {
2344 __bt_pb_dbus_init(agent);
2347 __bluetooth_pb_agent_timeout_add_seconds(agent);
2349 g_main_loop_run(g_mainloop);
2351 if (contacts_db_remove_changed_cb(_contacts_phone_log._uri,
2352 __bluetooth_pb_contact_changed,
2353 (void *)agent) != CONTACTS_ERROR_NONE) {
2354 ERR("Cannot remove changed callback");
2357 if (contacts_db_remove_changed_cb(_contacts_contact._uri,
2358 __bluetooth_pb_contact_changed,
2359 (void *)agent) != CONTACTS_ERROR_NONE) {
2360 ERR("Cannot remove changed callback");
2363 if (contacts_disconnect() != CONTACTS_ERROR_NONE)
2364 ERR("contacts_disconnect failed \n");
2366 g_dbus_connection_emit_signal(pb_dbus_conn, "org.bluez.pb_agent",
2367 "/org/bluez/pb_agent", "org.bluez.PbAgent",
2368 "clear", NULL, NULL);
2370 bluetooth_pb_agent_clear(agent);
2372 if (agent->tapi_handle)
2373 tel_deinit(agent->tapi_handle);
2374 if (agent->tel_number)
2375 g_free(agent->tel_number);
2377 __bt_pb_dbus_deinit(agent);
2381 DBG("Terminate Bluetooth PBAP agent");