adf3102aaf111c78c6d6641060b5d751ae94ece1
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-pbap.c
1 /*
2
3  * Bluetooth-frwk
4  *
5  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6  *
7  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
8  *               Girishashok Joshi <girish.joshi@samsung.com>
9  *               Chanyeol Park <chanyeol.park@samsung.com>
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *              http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  */
24
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <malloc.h>
29 #include <stacktrim.h>
30 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
31 #include <syspopup_caller.h>
32 #endif
33 #include <vconf.h>
34
35 #include "bt-internal-types.h"
36 #include "bt-service-common.h"
37 //#include "bt-service-agent.h"
38 //#include "bt-service-gap-agent.h"
39 //#include "bt-service-adapter.h"
40 #include "bt-service-event.h"
41 //#include "bt-service-rfcomm-server.h"
42 //#include "bt-service-device.h"
43 //#include "bt-service-audio.h"
44 #include "bt-service-pbap.h"
45 #include <glib.h>
46 #include <gio/gio.h>
47
48 #define  PBAP_UUID "0000112f-0000-1000-8000-00805f9b34fb"
49 #define  PBAP_OBEX_CLIENT_SERVICE "org.bluez.obex"
50 #define  PBAP_OBEX_CLIENT_PATH "/org/bluez/obex"
51 #define  PBAP_OBEX_CLIENT_INTERFACE "org.bluez.obex.Client1"
52
53 #define  PBAP_SESSION_SERVICE   "org.bluez.obex"
54 #define  PBAP_SESSION_INTERFACE "org.bluez.obex.PhonebookAccess1"
55 #define PBAP_VCARDLIST_MAXLENGTH 256
56
57 typedef enum {
58 PBAP_FIELD_ALL,
59 PBAP_FIELD_VERSION,
60 PBAP_FIELD_FN,
61 PBAP_FIELD_N,
62 PBAP_FIELD_PHOTO,
63 PBAP_FIELD_BDAY,
64 PBAP_FIELD_ADR,
65 PBAP_FIELD_LABEL,
66 PBAP_FIELD_TEL,
67 PBAP_FIELD_EMAIL,
68 PBAP_FIELD_MAILER,
69 PBAP_FIELD_TZ,
70 PBAP_FIELD_GEO,
71 PBAP_FIELD_TITLE,
72 PBAP_FIELD_ROLE,
73 PBAP_FIELD_LOGO,
74 PBAP_FIELD_AGENT,
75 PBAP_FIELD_ORG,
76 PBAP_FIELD_NOTE,
77 PBAP_FIELD_REV,
78 PBAP_FIELD_SOUND,
79 PBAP_FIELD_URL,
80 PBAP_FIELD_UID,
81 PBAP_FIELD_KEY,
82 PBAP_FIELD_NICKNAME,
83 PBAP_FIELD_CATEGORIES,
84 PBAP_FIELD_PROID,
85 PBAP_FIELD_CLASS,
86 PBAP_FIELD_SORT_STRING,
87 PBAP_FIELD_X_IRMC_CALL_DATETIME,
88 } bt_pbap_field_e;
89
90 char *SOURCE[] = {
91                 "int",  //Phone memory
92                 "sim"   // SIM memory
93 };
94
95 char *TYPE[] = {
96                 "pb",   //Phonebook for the saved contacts
97                 "ich",  //Incoming call history
98                 "och",  //Outgoing call history
99                 "mch",  //Missed call history
100                 "cch",  //Combined Call History cch = ich + och + mch
101 };
102
103 char *FORMAT[] = {
104                 "vcard21",      // vCard Format 2.1 (Default)
105                 "vcard30",      // vCard Format 3.0
106 };
107
108 char *ORDER[] = {
109                 "indexed",              // Index (default)
110                 "alphanumeric", // Alphanumeric
111                 "phonetic",             // Phonetic
112 };
113
114 char *SEARCH_FIELD[] = {
115                 "name",         // Search by Name(default)
116                 "number",       // Search by Phone Number
117                 "sound",        // Search by phonetic sound
118 };
119
120 static char *g_pbap_session_path = NULL;
121 static DBusGConnection *dbus_connection = NULL;
122 static DBusGProxy *g_pbap_proxy = NULL;
123
124 static struct {
125         int type;
126         int folder;
127 } selected_path = { -1, -1};
128
129 typedef enum  {
130         PBAP_NONE,
131         GET_SIZE,
132         PULL_ALL,
133         GET_LIST,
134         GET_VCARD,
135         PB_SEARCH,
136 } bt_pbap_operation_e;
137
138 typedef struct  {
139         bt_pbap_operation_e operation;
140         void *data;
141         void *app_param;
142 } bt_pbap_data_t;
143
144 typedef struct {
145         char *path;
146         char *filename;
147         char *remote_device;
148         bt_pbap_operation_e operation;
149 } bt_pbap_transfer_info_t;
150
151 static GSList *transfers;
152
153 int __bt_pbap_call_get_phonebook_size(DBusGProxy *proxy, bt_pbap_data_t *pbap_data);
154 int __bt_pbap_call_get_phonebook(DBusGProxy *proxy, bt_pbap_data_t *pbap_data);
155 int __bt_pbap_call_get_vcards_list(DBusGProxy *proxy, bt_pbap_data_t *pbap_data);
156 int __bt_pbap_call_get_vcard(DBusGProxy *proxy, bt_pbap_data_t *pbap_data);
157 int __bt_pbap_call_search_phonebook(DBusGProxy *proxy, bt_pbap_data_t *pbap_data);
158
159 static void __bt_pbap_free_data(bt_pbap_data_t *pbap_data)
160 {
161         g_free(pbap_data->app_param);
162         g_free(pbap_data->data);
163         g_free(pbap_data);
164 }
165
166 static bt_pbap_transfer_info_t *__bt_find_transfer_by_path(const char *transfer_path)
167 {
168         GSList *l;
169         bt_pbap_transfer_info_t *transfer;
170
171         retv_if(transfer_path == NULL, NULL);
172
173         for (l = transfers; l != NULL; l = l->next) {
174                 transfer = l->data;
175
176                 if (transfer == NULL)
177                         continue;
178
179                 if (g_strcmp0(transfer->path, transfer_path) == 0)
180                         return transfer;
181         }
182
183         return NULL;
184 }
185
186 static void __bt_free_transfer_info(bt_pbap_transfer_info_t *transfer_info)
187 {
188         ret_if(transfer_info == NULL);
189
190         g_free(transfer_info->path);
191         g_free(transfer_info->filename);
192         g_free(transfer_info->remote_device);
193         g_free(transfer_info);
194 }
195
196 void _bt_pbap_obex_transfer_completed(const char *transfer_path, gboolean transfer_status)
197 {
198         bt_pbap_transfer_info_t *transfer_info;
199         int result = 0;
200         int success = transfer_status;
201         BT_DBG("Transfer [%s] Success [%d] \n", transfer_path, success);
202
203         result = (success == TRUE) ? BLUETOOTH_ERROR_NONE
204                                 : BLUETOOTH_ERROR_CANCEL;
205
206         transfer_info = __bt_find_transfer_by_path(transfer_path);
207         ret_if(transfer_info == NULL);
208
209         BT_DBG("Remote Device [%s] FileName: [%s] Operation[%d]",
210                         transfer_info->remote_device, transfer_info->filename,
211                         transfer_info->operation);
212
213         switch(transfer_info->operation) {
214         case PULL_ALL: {
215                 _bt_send_event(BT_PBAP_CLIENT_EVENT,
216                                         BLUETOOTH_PBAP_PHONEBOOK_PULL,
217                                         DBUS_TYPE_INT32, &result,
218                                         DBUS_TYPE_STRING, &transfer_info->remote_device,
219                                         DBUS_TYPE_STRING, &transfer_info->filename,
220                                         DBUS_TYPE_INT32, &success,
221                                         DBUS_TYPE_INVALID);
222                 break;
223                 }
224         case GET_VCARD: {
225                 _bt_send_event(BT_PBAP_CLIENT_EVENT,
226                                         BLUETOOTH_PBAP_VCARD_PULL,
227                                         DBUS_TYPE_INT32, &result,
228                                         DBUS_TYPE_STRING, &transfer_info->remote_device,
229                                         DBUS_TYPE_STRING, &transfer_info->filename,
230                                         DBUS_TYPE_INT32, &success,
231                                         DBUS_TYPE_INVALID);
232                 break;
233                 }
234         default:
235                 BT_INFO("Case not handled");
236                 break;
237
238         }
239         transfers = g_slist_remove(transfers, transfer_info);
240         __bt_free_transfer_info(transfer_info);
241 }
242
243 void __bt_pbap_connect_cb(DBusGProxy *proxy,
244                 DBusGProxyCall *call, void *user_data)
245 {
246         char *session_path = NULL;
247         char *address_string = user_data;
248         GError *g_error = NULL;
249         int connected = -1;
250         int result = BLUETOOTH_ERROR_CANCEL;
251
252         BT_DBG("Address = %s", address_string);
253         if (!dbus_g_proxy_end_call(proxy, call, &g_error,
254                                         DBUS_TYPE_G_OBJECT_PATH, &session_path,
255                                         G_TYPE_INVALID)) {
256                 BT_ERR("Error Code[%d]: Message %s \n", g_error->code, g_error->message);
257                 g_error_free(g_error);
258         } else {
259                 g_pbap_session_path = g_strdup(session_path);
260                 BT_DBG("Session Path = %s\n", g_pbap_session_path);
261                 result = BLUETOOTH_ERROR_NONE;
262                 connected = 1;
263         }
264
265         _bt_send_event(BT_PBAP_CLIENT_EVENT,
266                                 BLUETOOTH_PBAP_CONNECTED,
267                                 DBUS_TYPE_INT32, &result,
268                                 DBUS_TYPE_STRING, &address_string,
269                                 DBUS_TYPE_INT32, &connected,
270                                 DBUS_TYPE_INVALID);
271
272         g_free(address_string);
273         g_free(session_path);
274         BT_DBG("-");
275 }
276
277 int _bt_pbap_connect(const bluetooth_device_address_t *address)
278 {
279         BT_DBG("+");
280         GHashTable *hash;
281         GValue *tgt_value;
282         GError *error = NULL;
283         char address_string[18] = { 0, };
284         char *ptr = NULL;
285
286         BT_CHECK_PARAMETER(address, return);
287
288         /* check if already connected */
289         if (g_pbap_session_path)
290                 return BLUETOOTH_ERROR_ALREADY_CONNECT;
291
292         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X]",
293                         address->addr[0], address->addr[1],
294                         address->addr[2], address->addr[3],
295                         address->addr[4], address->addr[5]);
296
297         _bt_convert_addr_type_to_string(address_string, (unsigned char *)address->addr);
298         BT_DBG("Address String: %s", address_string);
299         dbus_connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
300         if (error != NULL) {
301                         BT_ERR("Couldn't connect to system bus[%s]\n", error->message);
302                         g_error_free(error);
303                         return EXIT_FAILURE;
304         }
305         BT_DBG("#2");
306         g_pbap_proxy = dbus_g_proxy_new_for_name(dbus_connection,
307                         PBAP_OBEX_CLIENT_SERVICE,
308                         PBAP_OBEX_CLIENT_PATH,
309                         PBAP_OBEX_CLIENT_INTERFACE);
310         if (!g_pbap_proxy) {
311                 BT_ERR("Failed to get a proxy for D-Bus\n");
312                 return -1;
313         }
314         BT_DBG("#3");
315         hash = g_hash_table_new_full(g_str_hash, g_str_equal,
316                         NULL, (GDestroyNotify)g_free);
317         BT_DBG("#4");
318         tgt_value = g_new0(GValue, 1);
319         g_value_init(tgt_value, G_TYPE_STRING);
320         g_value_set_string(tgt_value, "pbap");
321         g_hash_table_insert(hash, "Target", tgt_value);
322         BT_DBG("#5");
323
324         ptr = g_strdup(address_string);
325         if (!dbus_g_proxy_begin_call(g_pbap_proxy, "CreateSession",
326                         (DBusGProxyCallNotify)__bt_pbap_connect_cb,
327                         ptr, NULL,
328                         G_TYPE_STRING, ptr,
329                         dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
330                         hash, G_TYPE_INVALID)) {
331                 BT_ERR("Connect Dbus Call Error");
332                 g_free(ptr);
333                 g_object_unref(g_pbap_proxy);
334                 g_hash_table_destroy(hash);
335                 return BLUETOOTH_ERROR_INTERNAL;
336         }
337
338         g_hash_table_destroy(hash);
339
340         BT_DBG("-");
341         return 0;
342 }
343
344 void __bt_pbap_disconnect_cb(DBusGProxy *proxy,
345                 DBusGProxyCall *call, void *user_data)
346 {
347         char *address_string = user_data;
348         GError *g_error = NULL;
349         int connected = -1;
350         int result = BLUETOOTH_ERROR_INTERNAL ;
351
352         BT_DBG("Address = %s", address_string);
353         if (!dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID)) {
354                 BT_ERR("Error Code[%d]: Message %s \n", g_error->code, g_error->message);
355                 g_error_free(g_error);
356         } else {
357                 g_free(g_pbap_session_path);
358                 g_pbap_session_path = NULL;
359                 result = BLUETOOTH_ERROR_NONE;
360                 selected_path.folder = -1;
361                 selected_path.type = -1;
362                 connected = 0;
363         }
364
365         g_object_unref(proxy);
366         _bt_send_event(BT_PBAP_CLIENT_EVENT,
367                                 BLUETOOTH_PBAP_CONNECTED,
368                                 DBUS_TYPE_INT32, &result,
369                                 DBUS_TYPE_STRING, &address_string,
370                                 DBUS_TYPE_INT32, &connected,
371                                 DBUS_TYPE_INVALID);
372
373         g_free(address_string);
374         BT_DBG("-");
375 }
376
377 int _bt_pbap_disconnect(const bluetooth_device_address_t *address)
378 {
379         BT_DBG("+");
380         char address_string[18] = { 0, };
381         char *ptr = NULL;
382         BT_CHECK_PARAMETER(address, return);
383
384         /* check if connected */
385         if (g_pbap_session_path == NULL)
386                 return BLUETOOTH_ERROR_NOT_CONNECTED;
387
388         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X]",
389                         address->addr[0], address->addr[1],
390                         address->addr[2], address->addr[3],
391                         address->addr[4], address->addr[5]);
392
393         _bt_convert_addr_type_to_string(address_string, (unsigned char *)address->addr);
394         BT_DBG("Address String: %s", address_string);
395         BT_DBG("Session Path: %s", g_pbap_session_path);
396
397         ptr = g_strdup(address_string);
398         if (!dbus_g_proxy_begin_call(g_pbap_proxy, "RemoveSession",
399                         (DBusGProxyCallNotify)__bt_pbap_disconnect_cb,
400                         ptr, NULL,
401                         DBUS_TYPE_G_OBJECT_PATH, g_pbap_session_path,
402                         G_TYPE_INVALID)) {
403                 g_free(ptr);
404                 BT_ERR("Disconnect Dbus Call Error");
405                 return BLUETOOTH_ERROR_INTERNAL;
406         }
407
408         return 0;
409 }
410
411 void __bt_pbap_select_cb(DBusGProxy *proxy,
412                 DBusGProxyCall *call, void *user_data)
413 {
414         BT_DBG("+");
415         GError *g_error = NULL;
416         bt_pbap_data_t *pbap_data = user_data;
417         char *address_string = pbap_data->data;
418
419         BT_DBG("Address = %s", address_string);
420         if (dbus_g_proxy_end_call(proxy, call, &g_error, G_TYPE_INVALID)) {
421                 switch (pbap_data->operation) {
422                 case GET_SIZE: {
423                         __bt_pbap_call_get_phonebook_size(proxy, pbap_data);
424                         break;
425                 }
426                 case PULL_ALL: {
427                         __bt_pbap_call_get_phonebook(proxy, pbap_data);
428                         break;
429                 }
430                 case GET_LIST: {
431                         __bt_pbap_call_get_vcards_list(proxy, pbap_data);
432                         break;
433                 }
434                 case GET_VCARD: {
435                         __bt_pbap_call_get_vcard(proxy, pbap_data);
436                         break;
437                 }
438                 case PB_SEARCH: {
439                         __bt_pbap_call_search_phonebook(proxy, pbap_data);
440                         break;
441                 }
442                 default: {
443                         g_object_unref(proxy);
444                         __bt_pbap_free_data(pbap_data);
445                 }
446                 } // End of Case
447         } else {
448                 g_object_unref(proxy);
449                 __bt_pbap_free_data(pbap_data);
450         }
451
452         BT_DBG("-");
453 }
454
455
456 void __bt_pbap_get_phonebook_size_cb(DBusGProxy *proxy,
457                 DBusGProxyCall *call, void *user_data)
458 {
459         BT_DBG("+");
460         GError *g_error = NULL;
461         int result = BLUETOOTH_ERROR_INTERNAL;
462         bt_pbap_data_t *pbap_data = user_data;
463         char *address_string = pbap_data->data;
464         unsigned int size = 0;
465
466         BT_DBG("Address = %s", address_string);
467         if (!dbus_g_proxy_end_call(proxy, call, &g_error,
468                         G_TYPE_UINT, &size,
469                         G_TYPE_INVALID)) {
470                 BT_ERR("Error Code[%d]: Message %s \n", g_error->code, g_error->message);
471                 g_error_free(g_error);
472         } else {
473                 BT_ERR("Success");
474                 result = BLUETOOTH_ERROR_NONE;
475         }
476         BT_DBG("Size of Phonebook: %d", size);
477         _bt_send_event(BT_PBAP_CLIENT_EVENT,
478                                 BLUETOOTH_PBAP_PHONEBOOK_SIZE,
479                                 DBUS_TYPE_INT32, &result,
480                                 DBUS_TYPE_STRING, &address_string,
481                                 DBUS_TYPE_INT32, &size,
482                                 DBUS_TYPE_INVALID);
483         g_object_unref(proxy);
484         __bt_pbap_free_data(pbap_data);
485         BT_DBG("-");
486 }
487
488 void __bt_pbap_get_phonebook_cb(DBusGProxy *proxy,
489                 DBusGProxyCall *call, void *user_data)
490 {
491         BT_DBG("+");
492         GError *g_error = NULL;
493         bt_pbap_data_t *pbap_data = user_data;
494         char *address_string = pbap_data->data;
495         GHashTable *properties;
496         GValue *value = { 0 };
497         bt_pbap_transfer_info_t *transfer_info;
498         char *transfer = NULL;
499         const gchar *filename =  NULL;
500
501         BT_DBG("Address = %s", address_string);
502         if (!dbus_g_proxy_end_call(proxy, call, &g_error,
503                         DBUS_TYPE_G_OBJECT_PATH, &transfer,
504                         dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
505                         &properties,
506                         G_TYPE_INVALID)) {
507                 BT_ERR("Error Code[%d]: Message %s \n", g_error->code, g_error->message);
508                 g_error_free(g_error);
509         } else {
510                 if (properties != NULL) {
511                         value = g_hash_table_lookup(properties, "Filename");
512                         filename = value ? g_value_get_string(value) : NULL;
513                 }
514
515                 BT_DBG("Transfer Path: %s", transfer);
516                 BT_DBG("File Name: %s", filename);
517                 transfer_info = g_new0(bt_pbap_transfer_info_t, 1);
518                 transfer_info->path = transfer;
519                 transfer_info->remote_device = g_strdup(address_string);
520                 transfer_info->filename = (char *)filename;
521                 transfer_info->operation = PULL_ALL;
522                 transfers = g_slist_append(transfers, transfer_info);
523         }
524
525         g_object_unref(proxy);
526         __bt_pbap_free_data(pbap_data);
527         BT_DBG("-");
528 }
529
530 void __bt_pbap_get_vcard_list_cb(DBusGProxy *proxy,
531                 DBusGProxyCall *call, void *user_data)
532 {
533         BT_DBG("+");
534         GError *g_error = NULL;
535         int i;
536         int result = BLUETOOTH_ERROR_INTERNAL;
537         GPtrArray *vcardlist = NULL;
538         bt_pbap_data_t *pbap_data = user_data;
539         char *address_string = pbap_data->data;
540         char **vcard_list = NULL;
541         char list_entry[PBAP_VCARDLIST_MAXLENGTH] = { 0, };
542         int length = 0;
543
544         BT_DBG("Address = %s", address_string);
545         if (!dbus_g_proxy_end_call(proxy, call, &g_error,
546                         dbus_g_type_get_collection("GPtrArray", dbus_g_type_get_struct("GValueArray",
547                                         G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID)),
548                                 &vcardlist, G_TYPE_INVALID)) {
549                 BT_ERR("Error Code[%d]: Message %s \n", g_error->code, g_error->message);
550                 g_error_free(g_error);
551                 vcard_list = g_new0(char *, length + 1);
552         } else {
553                 BT_DBG("vcardlist len %d", vcardlist->len);
554                 length = vcardlist->len;
555                 result = BLUETOOTH_ERROR_NONE;
556
557                 vcard_list = g_new0(char *, length + 1);
558
559                 GValue *v = g_new0(GValue, 1);//g_ptr_array_index(vcardlist, 0);
560                 gchar *elname, *elval;
561                 g_value_init(v, dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID));
562                 for (i = 0; i < length; i++) {
563                         g_value_set_boxed(v, g_ptr_array_index(vcardlist, i));
564                         if (dbus_g_type_struct_get (v, 0, &elname, 1, &elval, G_MAXUINT)) {
565                                 memset(list_entry, 0, PBAP_VCARDLIST_MAXLENGTH);
566                                 g_snprintf (list_entry, PBAP_VCARDLIST_MAXLENGTH - 1,
567                                                 "<card handle = \"%s\" name = \"%s\"/>", elname, elval);
568                                 //If possible send as Array of <STRING, STRING>
569                                 BT_DBG("%s", list_entry);
570                                 vcard_list[i] = g_strdup(list_entry);
571                         }
572                 }
573         }
574
575         _bt_send_event(BT_PBAP_CLIENT_EVENT,
576                                 BLUETOOTH_PBAP_VCARD_LIST,
577                                 DBUS_TYPE_INT32, &result,
578                                 DBUS_TYPE_STRING, &address_string,
579                                 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
580                                 &vcard_list, length,
581                                 DBUS_TYPE_INVALID);
582
583         g_object_unref(proxy);
584         __bt_pbap_free_data(pbap_data);
585         BT_DBG("-");
586 }
587
588 void __bt_pbap_get_vcard_cb(DBusGProxy *proxy,
589                 DBusGProxyCall *call, void *user_data)
590 {
591         BT_DBG("+");
592         GError *g_error = NULL;
593         bt_pbap_data_t *pbap_data = user_data;
594         char *address_string = pbap_data->data;
595         GHashTable *properties;
596         GValue *value = { 0 };
597         bt_pbap_transfer_info_t *transfer_info;
598         char *transfer = NULL;
599         const gchar *filename =  NULL;
600
601         BT_DBG("Address = %s", address_string);
602         if (!dbus_g_proxy_end_call(proxy, call, &g_error,
603                         DBUS_TYPE_G_OBJECT_PATH, &transfer,
604                         dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
605                         &properties,
606                         G_TYPE_INVALID)) {
607                 BT_ERR("Error Code[%d]: Message %s \n", g_error->code, g_error->message);
608                 g_error_free(g_error);
609         } else {
610                 if (properties != NULL) {
611                         value = g_hash_table_lookup(properties, "Filename");
612                         filename = value ? g_value_get_string(value) : NULL;
613                 }
614
615                 BT_DBG("Transfer Path: %s", transfer);
616                 BT_DBG("File Name: %s", filename);
617                 transfer_info = g_new0(bt_pbap_transfer_info_t, 1);
618                 transfer_info->path = transfer;
619                 transfer_info->remote_device = g_strdup(address_string);
620                 transfer_info->filename = (char *)filename;
621                 transfer_info->operation = GET_VCARD;
622                 transfers = g_slist_append(transfers, transfer_info);
623         }
624
625         g_object_unref(proxy);
626         __bt_pbap_free_data(pbap_data);
627         BT_DBG("-");
628 }
629
630 void __bt_pbap_search_phonebook_cb(DBusGProxy *proxy,
631                 DBusGProxyCall *call, void *user_data)
632 {
633         BT_DBG("+");
634         GError *g_error = NULL;
635         int i;
636         GPtrArray *vcardlist = NULL;
637         bt_pbap_data_t *pbap_data = user_data;
638         char *address_string = pbap_data->data;
639         char **vcard_list = NULL;
640         char list_entry[PBAP_VCARDLIST_MAXLENGTH] = { 0, };
641         int length = 0;
642         int result = BLUETOOTH_ERROR_INTERNAL;
643
644         BT_DBG("Address = %s", address_string);
645         if (!dbus_g_proxy_end_call(proxy, call, &g_error,
646                         dbus_g_type_get_collection("GPtrArray", dbus_g_type_get_struct("GValueArray",
647                                         G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID)),
648                                 &vcardlist, G_TYPE_INVALID)) {
649                 BT_ERR("Error Code[%d]: Message %s \n", g_error->code, g_error->message);
650                 g_error_free(g_error);
651         } else {
652                 BT_DBG("vcardlist len %d", vcardlist->len);
653                 length = vcardlist->len;
654                 result = BLUETOOTH_ERROR_NONE;
655
656                 vcard_list = g_new0(char *, length + 1);
657
658                 GValue *v = g_new0(GValue, 1);//g_ptr_array_index(vcardlist, 0);
659                 gchar *elname, *elval;
660                 g_value_init(v, dbus_g_type_get_struct ("GValueArray", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID));
661                 for (i = 0; i < length; i++) {
662                         g_value_set_boxed(v, g_ptr_array_index(vcardlist, i));
663                         if (dbus_g_type_struct_get (v, 0, &elname, 1, &elval, G_MAXUINT)) {
664                                 memset(list_entry, 0, PBAP_VCARDLIST_MAXLENGTH);
665                                 g_snprintf (list_entry, PBAP_VCARDLIST_MAXLENGTH - 1,
666                                                 "<card handle = \"%s\" name = \"%s\"/>", elname, elval);
667                                 //If possible send as Array of <STRING, STRING>
668                                 BT_DBG("%s", list_entry);
669                                 vcard_list[i] = g_strdup(list_entry);
670                         }
671                 }
672         }
673
674         _bt_send_event(BT_PBAP_CLIENT_EVENT,
675                                 BLUETOOTH_PBAP_PHONEBOOK_SEARCH,
676                                 DBUS_TYPE_INT32, &result,
677                                 DBUS_TYPE_STRING, &address_string,
678                                 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
679                                 &vcard_list, length,
680                                 DBUS_TYPE_INVALID);
681         g_object_unref(proxy);
682         __bt_pbap_free_data(pbap_data);
683         BT_DBG("-");
684 }
685
686 int __bt_pbap_call_get_phonebook_size(DBusGProxy *proxy, bt_pbap_data_t *pbap_data)
687 {
688         BT_DBG("+");
689         if (!dbus_g_proxy_begin_call(proxy, "GetSize",
690                         (DBusGProxyCallNotify)__bt_pbap_get_phonebook_size_cb,
691                         pbap_data, NULL,
692                         G_TYPE_INVALID)) {
693                 BT_ERR("GetSize Dbus Call Error");
694                 __bt_pbap_free_data(pbap_data);
695                 return BLUETOOTH_ERROR_INTERNAL;
696         }
697
698         return BLUETOOTH_ERROR_NONE;
699         BT_DBG("-");
700 }
701
702 int __bt_pbap_call_get_phonebook(DBusGProxy *proxy, bt_pbap_data_t *pbap_data)
703 {
704         BT_DBG("+");
705         GHashTable *filters;
706         GValue *max_count;
707         GValue *format;
708         GValue *order;
709         GValue *offset;
710         char *format_str = NULL;
711         char *order_str = NULL;
712         char *target_file = "/opt/usr/media/Downloads/pb.vcf";
713         bt_pbap_pull_parameters_t *app_param = pbap_data->app_param;
714
715         filters = g_hash_table_new_full(g_str_hash, g_str_equal,
716                                 NULL, (GDestroyNotify)g_free);
717
718         /* Add Format Filter only if other than vCard 2.1 (default)*/
719         if (app_param->format > 0) {
720                 format_str = g_strdup(FORMAT[app_param->format]);
721                 format = g_new0(GValue, 1);
722                 g_value_init(format, G_TYPE_STRING);
723                 g_value_set_string(format, format_str);
724                 g_hash_table_insert(filters, "Format", format);
725                 g_free(format_str);
726         }
727
728         /* Add Order Filter only if other than Indexed (default)*/
729         if (app_param->order > 0) {
730                 order_str = g_strdup(ORDER[app_param->order]);
731                 order = g_new0(GValue, 1);
732                 g_value_init(order, G_TYPE_STRING);
733                 g_value_set_string(order, order_str);
734                 g_hash_table_insert(filters, "Order", order);
735                 g_free(order_str);
736         }
737
738         max_count = g_new0(GValue, 1);
739         g_value_init(max_count, G_TYPE_UINT);
740         g_value_set_uint(max_count, app_param->maxlist);
741         g_hash_table_insert(filters, "MaxCount", max_count);
742
743         /* Add Offset Filter only if other than 0 (default)*/
744         if (app_param->offset > 0) {
745                 offset = g_new0(GValue, 1);
746                 g_value_init(offset, G_TYPE_UINT);
747                 g_value_set_uint(offset, app_param->offset);
748                 g_hash_table_insert(filters, "Offset", offset);
749         }
750
751 //****************************
752 // Add code for Fields
753 //
754 //****************************
755
756         if (!dbus_g_proxy_begin_call(proxy, "PullAll",
757                         (DBusGProxyCallNotify)__bt_pbap_get_phonebook_cb,
758                         pbap_data, NULL,
759                         G_TYPE_STRING, target_file,
760                         dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
761                         filters,
762                         G_TYPE_INVALID)) {
763                 BT_ERR("GetSize Dbus Call Error");
764                 g_hash_table_destroy(filters);
765                 return BLUETOOTH_ERROR_INTERNAL;
766         }
767         g_hash_table_destroy(filters);
768
769         return BLUETOOTH_ERROR_NONE;
770         BT_DBG("-");
771 }
772
773 int __bt_pbap_call_get_vcards_list(DBusGProxy *proxy, bt_pbap_data_t *pbap_data)
774 {
775         BT_DBG("+");
776         GHashTable *filters;
777         GValue *max_count;
778         GValue *order;
779         GValue *offset;
780         char *order_str = NULL;
781         bt_pbap_list_parameters_t *app_param = pbap_data->app_param;
782
783         filters = g_hash_table_new_full(g_str_hash, g_str_equal,
784                                 NULL, (GDestroyNotify)g_free);
785
786         /* Add Order Filter only if other than Indexed (default)*/
787         if (app_param->order > 0) {
788                 order_str = g_strdup(ORDER[app_param->order]);
789                 order = g_new0(GValue, 1);
790                 g_value_init(order, G_TYPE_STRING);
791                 g_value_set_string(order, order_str);
792                 g_hash_table_insert(filters, "Order", order);
793                 g_free(order_str);
794         }
795
796         max_count = g_new0(GValue, 1);
797         g_value_init(max_count, G_TYPE_UINT);
798         g_value_set_uint(max_count, app_param->maxlist);
799         g_hash_table_insert(filters, "MaxCount", max_count);
800
801         /* Add Offset Filter only if other than 0 (default)*/
802         if (app_param->offset > 0) {
803                 offset = g_new0(GValue, 1);
804                 g_value_init(offset, G_TYPE_UINT);
805                 g_value_set_uint(offset, app_param->offset);
806                 g_hash_table_insert(filters, "Offset", offset);
807         }
808
809         if (!dbus_g_proxy_begin_call(proxy, "List",
810                         (DBusGProxyCallNotify)__bt_pbap_get_vcard_list_cb,
811                         pbap_data, NULL,
812                         dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
813                         filters,
814                         G_TYPE_INVALID)) {
815                 BT_ERR("List Dbus Call Error");
816                 g_hash_table_destroy(filters);
817                 return BLUETOOTH_ERROR_INTERNAL;
818         }
819
820         g_hash_table_destroy(filters);
821
822         return BLUETOOTH_ERROR_NONE;
823         BT_DBG("-");
824 }
825
826 int __bt_pbap_call_get_vcard(DBusGProxy *proxy, bt_pbap_data_t *pbap_data)
827 {
828         BT_DBG("+");
829         GHashTable *filters;
830         GValue *format;
831         char *format_str = NULL;
832         char *target_file = "/opt/usr/media/Downloads/pb.vcf";
833         char *vcard_handle = NULL;
834         char vcard[10] = { 0, };
835
836         bt_pbap_pull_vcard_parameters_t *app_param = pbap_data->app_param;
837
838         filters = g_hash_table_new_full(g_str_hash, g_str_equal,
839                                 NULL, (GDestroyNotify)g_free);
840
841         /* Add Format Filter only if other than vCard 2.1 (default)*/
842         if (app_param->format > 0) {
843                 format_str = g_strdup(FORMAT[app_param->format]);
844                 format = g_new0(GValue, 1);
845                 g_value_init(format, G_TYPE_STRING);
846                 g_value_set_string(format, format_str);
847                 g_hash_table_insert(filters, "Format", format);
848                 g_free(format_str);
849         }
850
851
852 //****************************
853 // Add code for Fields
854 //
855 //****************************
856
857         sprintf(vcard, "%d.vcf", app_param->index);
858         BT_DBG("Handle: %s", vcard);
859         vcard_handle = g_strdup(vcard);
860
861         if (!dbus_g_proxy_begin_call(proxy, "Pull",
862                         (DBusGProxyCallNotify)__bt_pbap_get_vcard_cb,
863                         pbap_data, NULL,
864                         G_TYPE_STRING, vcard_handle,
865                         G_TYPE_STRING, target_file,
866                         dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
867                         filters,
868                         G_TYPE_INVALID)) {
869                 BT_ERR("GetSize Dbus Call Error");
870                 g_hash_table_destroy(filters);
871                 g_free(vcard_handle);
872                 return BLUETOOTH_ERROR_INTERNAL;
873         }
874
875         g_hash_table_destroy(filters);
876         g_free(vcard_handle);
877
878         return BLUETOOTH_ERROR_NONE;
879         BT_DBG("-");
880
881 }
882
883 int __bt_pbap_call_search_phonebook(DBusGProxy *proxy, bt_pbap_data_t *pbap_data)
884 {
885         BT_DBG("+");
886         GHashTable *filters;
887         GValue *max_count;
888         GValue *order;
889         GValue *offset;
890         char *order_str = NULL;
891         char *field = NULL;
892         char *value = NULL;
893         bt_pbap_search_parameters_t *app_param = pbap_data->app_param;
894
895         filters = g_hash_table_new_full(g_str_hash, g_str_equal,
896                                 NULL, (GDestroyNotify)g_free);
897
898         /* Add Order Filter only if other than Indexed (default)*/
899         if (app_param->order > 0) {
900                 order_str = g_strdup(ORDER[app_param->order]);
901                 order = g_new0(GValue, 1);
902                 g_value_init(order, G_TYPE_STRING);
903                 g_value_set_string(order, order_str);
904                 g_hash_table_insert(filters, "Order", order);
905                 g_free(order_str);
906         }
907
908         max_count = g_new0(GValue, 1);
909         g_value_init(max_count, G_TYPE_UINT);
910         g_value_set_uint(max_count, app_param->maxlist);
911         g_hash_table_insert(filters, "MaxCount", max_count);
912
913         /* Add Offset Filter only if other than 0 (default)*/
914         if (app_param->offset > 0) {
915                 offset = g_new0(GValue, 1);
916                 g_value_init(offset, G_TYPE_UINT);
917                 g_value_set_uint(offset, app_param->offset);
918                 g_hash_table_insert(filters, "Offset", offset);
919         }
920
921         field = g_strdup(SEARCH_FIELD[app_param->search_attribute]);
922         value = g_strdup(app_param->search_value);
923         if (!dbus_g_proxy_begin_call(proxy, "Search",
924                         (DBusGProxyCallNotify)__bt_pbap_search_phonebook_cb,
925                         pbap_data, NULL,
926                         G_TYPE_STRING, field,
927                         G_TYPE_STRING, value,
928                         dbus_g_type_get_map("GHashTable", G_TYPE_STRING, G_TYPE_VALUE),
929                         filters,
930                         G_TYPE_INVALID)) {
931                 BT_ERR("List Dbus Call Error");
932                 g_hash_table_destroy(filters);
933                 g_free(field);
934                 g_free(value);
935                 return BLUETOOTH_ERROR_INTERNAL;
936         }
937
938         g_hash_table_destroy(filters);
939         g_free(field);
940         g_free(value);
941
942         return BLUETOOTH_ERROR_NONE;
943         BT_DBG("-");
944 }
945
946 int _bt_pbap_get_phonebook_size(const bluetooth_device_address_t *address,
947                 int source, int type)
948 {
949         BT_DBG("+");
950         DBusGProxy *g_pbap_session_proxy = NULL;
951         char address_string[18] = { 0, };
952         char *source_string = NULL;
953         char *type_string = NULL;
954         bt_pbap_data_t *pbap_data = NULL;
955
956         BT_CHECK_PARAMETER(address, return);
957
958         /* check if connected */
959         if (g_pbap_session_path == NULL) {
960                 BT_ERR("NOT CONNECTED");
961                 return BLUETOOTH_ERROR_NOT_CONNECTED;
962         }
963
964         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X]",
965                         address->addr[0], address->addr[1],
966                         address->addr[2], address->addr[3],
967                         address->addr[4], address->addr[5]);
968
969         _bt_convert_addr_type_to_string(address_string, (unsigned char *)address->addr);
970                         BT_DBG("Address String: %s", address_string);
971         BT_DBG("Session Path = %s\n", g_pbap_session_path);
972         g_pbap_session_proxy = dbus_g_proxy_new_for_name(dbus_connection,
973                         PBAP_SESSION_SERVICE,
974                         g_pbap_session_path,
975                         PBAP_SESSION_INTERFACE);
976         if (!g_pbap_session_proxy) {
977                 BT_ERR("Failed to get a proxy for D-Bus\n");
978                 return -1;
979         }
980
981         pbap_data = g_new0(bt_pbap_data_t, 1);
982         pbap_data->operation = GET_SIZE;
983         pbap_data->data = g_strdup(address_string);
984
985         if (source ==  selected_path.folder && type == selected_path.type) {
986                 return __bt_pbap_call_get_phonebook_size(g_pbap_session_proxy, pbap_data);
987         }
988
989         source_string = g_strdup(SOURCE[source]);
990         type_string = g_strdup(TYPE[type]);
991
992         BT_DBG("Address[%s] Source[%s] Type[%s]",
993                 address_string, source_string, type_string);
994
995         if (!dbus_g_proxy_begin_call(g_pbap_session_proxy, "Select",
996                         (DBusGProxyCallNotify)__bt_pbap_select_cb,
997                         pbap_data, NULL,
998                         G_TYPE_STRING, source_string,
999                         G_TYPE_STRING, type_string,
1000                         G_TYPE_INVALID)) {
1001                 BT_ERR("Select Dbus Call Error");
1002                 g_free(source_string);
1003                 g_free(type_string);
1004                 g_object_unref(g_pbap_session_proxy);
1005                 return BLUETOOTH_ERROR_INTERNAL;
1006         }
1007
1008         selected_path.folder = source;
1009         selected_path.type = type;
1010
1011         g_free(source_string);
1012         g_free(type_string);
1013
1014         return 0;
1015 }
1016
1017 int _bt_pbap_get_phonebook(const bluetooth_device_address_t *address,
1018                 int source, int type, bt_pbap_pull_parameters_t *app_param)
1019 {
1020         BT_DBG("+");
1021         DBusGProxy *g_pbap_session_proxy = NULL;
1022         char address_string[18] = { 0, };
1023         char *source_string = NULL;
1024         char *type_string = NULL;
1025
1026         bt_pbap_data_t *pbap_data = NULL;
1027         bt_pbap_pull_parameters_t *param = NULL;
1028
1029         BT_CHECK_PARAMETER(address, return);
1030
1031         /* check if connected */
1032         if (g_pbap_session_path == NULL) {
1033                 BT_ERR("NOT CONNECTED");
1034                 return BLUETOOTH_ERROR_NOT_CONNECTED;
1035         }
1036
1037         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X]",
1038                         address->addr[0], address->addr[1],
1039                         address->addr[2], address->addr[3],
1040                         address->addr[4], address->addr[5]);
1041
1042         _bt_convert_addr_type_to_string(address_string, (unsigned char *)address->addr);
1043                 BT_DBG("Address String: %s", address_string);
1044
1045         BT_DBG("Session Path = %s\n", g_pbap_session_path);
1046         g_pbap_session_proxy = dbus_g_proxy_new_for_name(dbus_connection,
1047                         PBAP_SESSION_SERVICE,
1048                         g_pbap_session_path,
1049                         PBAP_SESSION_INTERFACE);
1050         if (!g_pbap_session_proxy) {
1051                 BT_ERR("Failed to get a proxy for D-Bus\n");
1052                 return -1;
1053         }
1054
1055         pbap_data = g_new0(bt_pbap_data_t, 1);
1056         pbap_data->operation = PULL_ALL;
1057         pbap_data->data = g_strdup(address_string);
1058         param = g_new0(bt_pbap_pull_parameters_t, 1);
1059         memcpy(param, app_param, sizeof(bt_pbap_pull_parameters_t));
1060         pbap_data->app_param = param;
1061
1062         if (source ==  selected_path.folder && type == selected_path.type) {
1063                 return __bt_pbap_call_get_phonebook(g_pbap_session_proxy, pbap_data);
1064         }
1065
1066         source_string = g_strdup(SOURCE[source]);
1067         type_string = g_strdup(TYPE[type]);
1068
1069         BT_DBG("Address[%s] Source[%s] Type[%s]",
1070                         address_string, source_string, type_string);
1071
1072         if (!dbus_g_proxy_begin_call(g_pbap_session_proxy, "Select",
1073                         (DBusGProxyCallNotify)__bt_pbap_select_cb,
1074                         pbap_data, NULL,
1075                         G_TYPE_STRING, source_string,
1076                         G_TYPE_STRING, type_string,
1077                         G_TYPE_INVALID)) {
1078                 BT_ERR("Select Dbus Call Error");
1079                 g_free(source_string);
1080                 g_free(type_string);
1081                 g_object_unref(g_pbap_session_proxy);
1082                 return BLUETOOTH_ERROR_INTERNAL;
1083         }
1084
1085         selected_path.folder = source;
1086         selected_path.type = type;
1087
1088         g_free(source_string);
1089         g_free(type_string);
1090
1091         return 0;
1092 }
1093
1094 int _bt_pbap_get_list(const bluetooth_device_address_t *address, int source,
1095                 int type,  bt_pbap_list_parameters_t *app_param)
1096 {
1097         BT_DBG("+");
1098         DBusGProxy *g_pbap_session_proxy = NULL;
1099         char address_string[18] = { 0, };
1100         char *source_string = NULL;
1101         char *type_string = NULL;
1102
1103         bt_pbap_data_t *pbap_data = NULL;
1104         bt_pbap_list_parameters_t *param = NULL;
1105
1106         BT_CHECK_PARAMETER(address, return);
1107
1108         /* check if connected */
1109         if (g_pbap_session_path == NULL) {
1110                 BT_ERR("NOT CONNECTED");
1111                 return BLUETOOTH_ERROR_NOT_CONNECTED;
1112         }
1113
1114         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X]",
1115                         address->addr[0], address->addr[1],
1116                         address->addr[2], address->addr[3],
1117                         address->addr[4], address->addr[5]);
1118
1119         _bt_convert_addr_type_to_string(address_string, (unsigned char *)address->addr);
1120                 BT_DBG("Address String: %s", address_string);
1121
1122         BT_DBG("Session Path = %s\n", g_pbap_session_path);
1123         g_pbap_session_proxy = dbus_g_proxy_new_for_name(dbus_connection,
1124                         PBAP_SESSION_SERVICE,
1125                         g_pbap_session_path,
1126                         PBAP_SESSION_INTERFACE);
1127         if (!g_pbap_session_proxy) {
1128                 BT_ERR("Failed to get a proxy for D-Bus\n");
1129                 return -1;
1130         }
1131
1132         pbap_data = g_new0(bt_pbap_data_t, 1);
1133         pbap_data->operation = GET_LIST;
1134         pbap_data->data = g_strdup(address_string);
1135         param = g_new0(bt_pbap_list_parameters_t, 1);
1136         memcpy(param, app_param, sizeof(bt_pbap_list_parameters_t));
1137         pbap_data->app_param = param;
1138
1139         if (source ==  selected_path.folder && type == selected_path.type) {
1140                 return __bt_pbap_call_get_vcards_list(g_pbap_session_proxy, pbap_data);
1141         }
1142
1143         source_string = g_strdup(SOURCE[source]);
1144         type_string = g_strdup(TYPE[type]);
1145
1146         BT_DBG("Address[%s] Source[%s] Type[%s]",
1147                         address_string, source_string, type_string);
1148
1149         if (!dbus_g_proxy_begin_call(g_pbap_session_proxy, "Select",
1150                         (DBusGProxyCallNotify)__bt_pbap_select_cb,
1151                         pbap_data, NULL,
1152                         G_TYPE_STRING, source_string,
1153                         G_TYPE_STRING, type_string,
1154                         G_TYPE_INVALID)) {
1155                 BT_ERR("Select Dbus Call Error");
1156                 g_free(source_string);
1157                 g_free(type_string);
1158                 g_object_unref(g_pbap_session_proxy);
1159                 return BLUETOOTH_ERROR_INTERNAL;
1160         }
1161
1162         selected_path.folder = source;
1163         selected_path.type = type;
1164
1165         g_free(source_string);
1166         g_free(type_string);
1167
1168         return 0;
1169 }
1170
1171
1172 int _bt_pbap_pull_vcard(const bluetooth_device_address_t *address,
1173                 int source, int type, bt_pbap_pull_vcard_parameters_t *app_param)
1174 {
1175         BT_DBG("+");
1176         DBusGProxy *g_pbap_session_proxy = NULL;
1177         char address_string[18] = { 0, };
1178         char *source_string = NULL;
1179         char *type_string = NULL;
1180         bt_pbap_data_t *pbap_data = NULL;
1181         bt_pbap_pull_vcard_parameters_t *param = NULL;
1182
1183         BT_CHECK_PARAMETER(address, return);
1184
1185         /* check if connected */
1186         if (g_pbap_session_path == NULL) {
1187                 BT_ERR("NOT CONNECTED");
1188                 return BLUETOOTH_ERROR_NOT_CONNECTED;
1189         }
1190
1191         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X]",
1192                         address->addr[0], address->addr[1],
1193                         address->addr[2], address->addr[3],
1194                         address->addr[4], address->addr[5]);
1195
1196         _bt_convert_addr_type_to_string(address_string, (unsigned char *)address->addr);
1197                 BT_DBG("Address String: %s", address_string);
1198
1199         BT_DBG("Session Path = %s\n", g_pbap_session_path);
1200         g_pbap_session_proxy = dbus_g_proxy_new_for_name(dbus_connection,
1201                         PBAP_SESSION_SERVICE,
1202                         g_pbap_session_path,
1203                         PBAP_SESSION_INTERFACE);
1204         if (!g_pbap_session_proxy) {
1205                 BT_ERR("Failed to get a proxy for D-Bus\n");
1206                 return -1;
1207         }
1208
1209         pbap_data = g_new0(bt_pbap_data_t, 1);
1210         pbap_data->operation = GET_VCARD;
1211         pbap_data->data = g_strdup(address_string);
1212         param = g_new0(bt_pbap_pull_vcard_parameters_t, 1);
1213         memcpy(param, app_param, sizeof(bt_pbap_pull_vcard_parameters_t));
1214         pbap_data->app_param = param;
1215
1216         if (source ==  selected_path.folder && type == selected_path.type) {
1217                 return __bt_pbap_call_get_vcard(g_pbap_session_proxy, pbap_data);
1218         }
1219
1220         source_string = g_strdup(SOURCE[source]);
1221         type_string = g_strdup(TYPE[type]);
1222
1223         BT_DBG("Address[%s] Source[%s] Type[%s]",
1224                         address_string, source_string, type_string);
1225
1226         if (!dbus_g_proxy_begin_call(g_pbap_session_proxy, "Select",
1227                         (DBusGProxyCallNotify)__bt_pbap_select_cb,
1228                         pbap_data, NULL,
1229                         G_TYPE_STRING, source_string,
1230                         G_TYPE_STRING, type_string,
1231                         G_TYPE_INVALID)) {
1232                 BT_ERR("Select Dbus Call Error");
1233                 g_free(source_string);
1234                 g_free(type_string);
1235                 g_object_unref(g_pbap_session_proxy);
1236                 return BLUETOOTH_ERROR_INTERNAL;
1237         }
1238
1239         selected_path.folder = source;
1240         selected_path.type = type;
1241
1242         g_free(source_string);
1243         g_free(type_string);
1244
1245         return 0;
1246 }
1247
1248 int _bt_pbap_phonebook_search(const bluetooth_device_address_t *address,
1249                 int source, int type, bt_pbap_search_parameters_t *app_param)
1250 {
1251         BT_DBG("+");
1252         DBusGProxy *g_pbap_session_proxy = NULL;
1253         char address_string[18] = { 0, };
1254         char *source_string = NULL;
1255         char *type_string = NULL;
1256         bt_pbap_data_t *pbap_data = NULL;
1257         bt_pbap_search_parameters_t *param = NULL;
1258
1259         BT_CHECK_PARAMETER(address, return);
1260
1261         /* check if connected */
1262         if (g_pbap_session_path == NULL) {
1263                 BT_ERR("NOT CONNECTED");
1264                 return BLUETOOTH_ERROR_NOT_CONNECTED;
1265         }
1266
1267         BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X]",
1268                         address->addr[0], address->addr[1],
1269                         address->addr[2], address->addr[3],
1270                         address->addr[4], address->addr[5]);
1271
1272         _bt_convert_addr_type_to_string(address_string, (unsigned char *)address->addr);
1273                 BT_DBG("Address String: %s", address_string);
1274
1275         BT_DBG("Session Path = %s\n", g_pbap_session_path);
1276         g_pbap_session_proxy = dbus_g_proxy_new_for_name(dbus_connection,
1277                         PBAP_SESSION_SERVICE,
1278                         g_pbap_session_path,
1279                         PBAP_SESSION_INTERFACE);
1280         if (!g_pbap_session_proxy) {
1281                 BT_ERR("Failed to get a proxy for D-Bus\n");
1282                 return -1;
1283         }
1284
1285         pbap_data = g_new0(bt_pbap_data_t, 1);
1286         pbap_data->operation = PB_SEARCH;
1287         pbap_data->data = g_strdup(address_string);
1288         param = g_new0(bt_pbap_search_parameters_t, 1);
1289         memcpy(param, app_param, sizeof(bt_pbap_search_parameters_t));
1290         pbap_data->app_param = param;
1291
1292         if (source ==  selected_path.folder && type == selected_path.type) {
1293                 return __bt_pbap_call_search_phonebook(g_pbap_session_proxy, pbap_data);
1294         }
1295
1296         source_string = g_strdup(SOURCE[source]);
1297         type_string = g_strdup(TYPE[type]);
1298
1299         BT_DBG("Address[%s] Source[%s] Type[%s]",
1300                         address_string, source_string, type_string);
1301
1302         if (!dbus_g_proxy_begin_call(g_pbap_session_proxy, "Select",
1303                         (DBusGProxyCallNotify)__bt_pbap_select_cb,
1304                         pbap_data, NULL,
1305                         G_TYPE_STRING, source_string,
1306                         G_TYPE_STRING, type_string,
1307                         G_TYPE_INVALID)) {
1308                 BT_ERR("Select Dbus Call Error");
1309                 g_object_unref(g_pbap_session_proxy);
1310                 g_free(source_string);
1311                 g_free(type_string);
1312                 return BLUETOOTH_ERROR_INTERNAL;
1313         }
1314
1315         selected_path.folder = source;
1316         selected_path.type = type;
1317
1318         g_free(source_string);
1319         g_free(type_string);
1320
1321         return 0;
1322 }
1323