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