5ba4477af8e2e3afca3236276f094d9d39903c90
[platform/core/connectivity/bluetooth-agent.git] / ag-agent / bluetooth-ag-manager.c
1 /*
2  * bluetooth-ag-manager.c
3  *
4  * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:     Hocheol Seo <hocheol.seo@samsung.com>
7  *              Chethan TN <chethan.tn@samsung.com>
8  *              Chanyeol Park <chanyeol.park@samsung.com>
9  *              Rakesh MK <rakesh.mk@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 <string.h>
26 #include "bluetooth-ag-agent.h"
27 #include "bluetooth-ag-handler.h"
28 #include <dbus/dbus.h>
29
30 #define PHONEBOOK_AGENT_BUS_NAME "org.bluez.pb_agent"
31 #define PHONEBOOK_AGENT_PATH     "/org/bluez/pb_agent"
32 #define PHONEBOOK_AGENT_INTERFACE "org.bluez.PbAgent.At"
33
34 struct telephony_call {
35         char *call_path;
36         int call_status;
37         gboolean call_originating;
38         gboolean call_emergency;
39         gboolean call_on_hold;
40         gboolean call_conference;
41         char *call_number;
42         gboolean call_setup;
43         uint32_t call_id;
44         char *call_sender;
45 };
46
47 #define HFP_AGENT_ACTIVITY_STATUS_READY 0
48 #define HFP_AGENT_ACTIVITY_STATUS_UNAVAILABLE 1
49 #define HFP_AGENT_ACTIVITY_STATUS_UNKNOWN 2
50 #define HFP_AGENT_ACTIVITY_STATUS_RINGING 3
51 #define HFP_AGENT_ACTIVITY_STATUS_CALL_IN_PROGRESS 4
52
53 #define HFP_AGENT_BATTERY_INDICATOR "battchg"
54 #define HFP_AGENT_CALL_INDICATOR                "call"
55 #define HFP_AGENT_CALLHELD_INDICATOR    "callheld"
56 #define HFP_AGENT_CALLSETUP_INDICATOR   "callsetup"
57 #define HFP_AGENT_ROAMING_INDICATOR "roam"
58 #define HFP_AGENT_SERVICE_INDICATOR  "service"
59 #define HFP_AGENT_SIGNAL_INDICATOR      "signal"
60
61 #define HFP_AGENT_CALL_IDLE  0
62 #define HFP_AGENT_CALL_ACTIVE 1
63
64 #define HFP_INCOMING_CALLSETUP  1
65 #define HFP_OUTGOING_CALLSETUP  2
66 #define RESTRAIN_CALL_FLAG 0x01
67 #define ALLOW_CALL_FLAG  0x02
68
69 #define HFP_CALL_STATUS_IDLE    0
70 #define HFP_CALL_STATUS_CREATE          1
71 #define HFP_CALL_STATUS_COMING          2
72 #define HFP_CALL_STATUS_PROCEEDING      3
73 #define HFP_CALL_STATUS_MO_ALERTING     4
74 #define HFP_CALL_STATUS_MT_ALERTING             5
75 #define HFP_CALL_STATUS_WAITING         6
76 #define HFP_CALL_STATUS_ANSWERED        7
77 #define HFP_CALL_STATUS_ACTIVE          8
78 #define HFP_CALL_STATUS_MO_RELEASE      9
79 #define HFP_CALL_STATUS_MT_RELEASE      10
80 #define HFP_CALL_STATUS_HOLD_INITIATED          11
81 #define HFP_CALL_STATUS_HOLD            12
82 #define HFP_CALL_STATUS_RETRIEVE_INITIATED      13
83 #define HFP_CALL_STATUS_RECONNECT_PENDING       14
84 #define HFP_CALL_STATUS_TERMINATED              15
85 #define HFP_CALL_STATUS_SWAP_INITIATED          16
86
87 #define AGENT_MAX_PB_COUNT              1000
88 #define AGENT_PB_NAME_MAX_LENGTH                20
89 #define AGENT_PB_NUMBER_MAX_LENGTH      20
90 #define AGENT_MAX_CALLLOG_COUNT         30
91 #define ERR_NOT_FOUND -1
92 #define AG_MAX_LENGTH 16
93
94 static gboolean update_events = FALSE;
95 static int caller_id = 0;
96
97 static GSList *call_senders_paths = NULL;
98 static GSList *existing_call_list = NULL;
99 static GSList *agent_active_call_list = NULL;
100 static char *ag_subscriber_num = NULL;
101
102 static guint call_on_hold_timer = 0;
103
104 typedef struct {
105         gchar *sender_path;
106         gchar *sender_name;
107 } sender_info_t;
108
109 static struct {
110         char *network_operator_name;
111         uint8_t network_status;
112         int32_t signal_strength;
113 } network_info = {
114         .network_operator_name = NULL,
115         .network_status = BT_AGENT_NETWORK_REG_STATUS_UNKOWN,
116         .signal_strength = 0,
117 };
118
119 static const char *agent_pb_store_list[] =  {
120         "\"ME\"", "\"DC\"", "\"MC\"", "\"RC\""
121 };
122
123 static const char *agent_supported_character_set[] = {
124         "\"UTF-8\"", "\"IRA\""
125 };
126
127 #if defined(TIZEN_PROFILE_WEARABLE) && defined(TIZEN_FEATURE_BT_HFP_AG)
128 static const char *ag_chld_str = "0,1,2";
129 #else
130 static const char *ag_chld_str = "0,1,2,3";
131 #endif
132
133 #define AGENT_PB_STORE_LIST_SIZE (sizeof(agent_pb_store_list) \
134                                 /sizeof(const char *))
135 #define AGENT_SUPPORTED_CHARACTER_SET_SIZE ( \
136                 sizeof(agent_supported_character_set)/sizeof(const char *))
137
138 static bt_ag_indicators_t hfp_ag_ind[] = {
139         { "call", "0,1", 0, TRUE, TRUE },
140         { "callsetup", "0-3", 0 , TRUE, TRUE },
141         { "battchg", "0-5", 5 , TRUE, TRUE },
142         { "callheld", "0-2", 0 , FALSE, TRUE },
143         { "roam", "0,1", 0 , TRUE, TRUE },
144         { "signal", "0-5", 0 , TRUE, TRUE },
145         { "service",    "0,1", 0, TRUE, TRUE },
146         { NULL }
147 };
148
149 static struct {
150         int32_t path_id;
151         int32_t charset_id;
152 } ag_pb_info = {
153         .path_id = 0,
154         .charset_id = 0
155 };
156
157 static gboolean __bt_hfp_check_for_callpath(const char *call_path,
158                                         const char *call_sender)
159 {
160         GSList *sender_list = call_senders_paths;
161         sender_info_t *sender;
162
163         DBG("call path is  = %s\n", call_path);
164         DBG("sender is  = %s\n", call_sender);
165
166         if (call_path == NULL || call_sender == NULL) {
167
168                 ERR("Invalid Parameters");
169                 return FALSE;
170         }
171
172         /*check if the call is already registered*/
173         DBG("Checking if the call is already registered");
174         while (sender_list != NULL) {
175                 sender = sender_list->data;
176
177                 if (sender == NULL)
178                         break;
179
180                 if (g_strcmp0(sender->sender_path, call_path) == 0) {
181                         DBG("sender path and call path match... so return true");
182                         return TRUE;
183                 }
184
185                 sender_list = sender_list->next;
186         }
187
188         ERR("Call path is not already registered");
189         return FALSE;
190 }
191
192 static void __bt_hfp_clear_sender_path(sender_info_t *s_path)
193 {
194         if (s_path == NULL)
195                 return;
196
197         g_free(s_path->sender_name);
198         g_free(s_path->sender_path);
199         g_free(s_path);
200
201         if (g_slist_length(call_senders_paths) == 0) {
202                 g_slist_free(call_senders_paths);
203                 call_senders_paths = NULL;
204         }
205 }
206
207 static void __bt_hfp_free_call(struct telephony_call *t_call)
208 {
209         if (t_call == NULL)
210                 return;
211
212         g_free(t_call->call_number);
213         g_free(t_call->call_path);
214         g_free(t_call->call_sender);
215         g_free(t_call);
216 }
217
218 static void __bt_hfp_reset_indicators(void)
219 {
220         int i;
221
222         for (i = 0; hfp_ag_ind[i].indicator_desc != NULL; i++)
223                 hfp_ag_ind[i].is_activated = TRUE;
224 }
225
226 void _bt_hfp_device_disconnected(void *t_device)
227 {
228         DBG("hfp_agent: device %p disconnected", t_device);
229         update_events = FALSE;
230         __bt_hfp_reset_indicators();
231 }
232
233 void _bt_hfp_initialize_telephony_manager(uint32_t ag_features)
234 {
235         int index;
236         int value;
237         int ret;
238
239         /* Reset the indicator values */
240         for (index = 0; hfp_ag_ind[index].indicator_desc != NULL; index++) {
241                 if (g_str_equal(hfp_ag_ind[index].indicator_desc, "battchg")) {
242                         ret = vconf_get_int(VCONFKEY_SYSMAN_BATTERY_CAPACITY,
243                                                                         &value);
244                         if (ret != 0) {
245                                 ERR("Get battery status failed : %d\n", ret);
246                         } else {
247                                 /* Send battery status ranging from 0-5 */
248                                 if (value < 5)
249                                         hfp_ag_ind[index].hfp_value = 0;
250                                 else if (value >= 100)
251                                         hfp_ag_ind[index].hfp_value = 5;
252                                 else
253                                         hfp_ag_ind[index].hfp_value = value / 20 + 1;
254                         }
255                 } else if (g_str_equal(hfp_ag_ind[index].indicator_desc, "signal")) {
256                         ret = vconf_get_int(VCONFKEY_TELEPHONY_RSSI, &value);
257                         if (ret != 0) {
258                                 ERR("Get signal status failed err = %d\n", ret);
259                         } else {
260                                 BT_CHECK_SIGNAL_STRENGTH(value);
261                                 hfp_ag_ind[index].hfp_value = value;
262                         }
263                 } else if (g_str_equal(hfp_ag_ind[index].indicator_desc, "roam")) {
264                         ret = vconf_get_int(VCONFKEY_TELEPHONY_SVC_ROAM, &value);
265                         if (ret != 0)
266                                 ERR("Get roaming status failed err = %d\n", ret);
267                         else
268                                 hfp_ag_ind[index].hfp_value = value;
269                 } else if (g_str_equal(hfp_ag_ind[index].indicator_desc, "service")) {
270                         ret = vconf_get_int(VCONFKEY_TELEPHONY_SVCTYPE, &value);
271                         if (ret != 0) {
272                                 ERR("Get Service status failed : %d\n", ret);
273                         } else {
274                                 switch (value) {
275                                 case VCONFKEY_TELEPHONY_SVCTYPE_NONE:
276                                 case VCONFKEY_TELEPHONY_SVCTYPE_NOSVC:
277                                 case VCONFKEY_TELEPHONY_SVCTYPE_SEARCH:
278                                         hfp_ag_ind[index].hfp_value =
279                                                 INDICATOR_EVENT_SERVICE_NONE;
280                                         break;
281                                 default:
282                                         hfp_ag_ind[index].hfp_value =
283                                                 INDICATOR_EVENT_SERVICE_PRESENT;
284                                         break;
285                                 }
286                         }
287                 } else {
288                         hfp_ag_ind[index].hfp_value = 0;
289                 }
290         }
291
292         /*Initializatoin of the indicators*/
293         _bt_hfp_set_ag_indicator(ag_features, hfp_ag_ind,
294                                         BT_RSP_HOLD_NOT_SUPPORTED,
295                                         ag_chld_str);
296 }
297
298 void _bt_hfp_deinitialize_telephony_manager(void)
299 {
300         GSList *list = call_senders_paths;
301
302         g_free(ag_subscriber_num);
303         ag_subscriber_num = NULL;
304
305         g_free(network_info.network_operator_name);
306         network_info.network_operator_name = NULL;
307
308         network_info.network_status = BT_AGENT_NETWORK_REG_STATUS_UNKOWN;
309         network_info.signal_strength = 0;
310
311         g_slist_free(agent_active_call_list);
312         agent_active_call_list = NULL;
313
314         g_slist_foreach(existing_call_list, (GFunc) __bt_hfp_free_call, NULL);
315         g_slist_free(existing_call_list);
316         existing_call_list = NULL;
317
318         while (list != NULL) {
319                 __bt_hfp_clear_sender_path(list->data);
320                 list = list->next;
321         }
322
323         g_slist_free(call_senders_paths);
324         call_senders_paths = NULL;
325
326         _bt_hfp_deinitialize();
327 }
328
329 bt_hfp_agent_error_t _bt_hfp_register_telephony_agent(gboolean register_flag,
330                 const char *path_to_register,
331                 const char *sender)
332 {
333         sender_info_t *sender_info;
334
335         if (sender == NULL || path_to_register == NULL)
336                 return BT_HFP_AGENT_ERROR_INVALID_PARAM;
337
338         DBG(" register_flag = %d", register_flag);
339         DBG(" path_to_register = %s", path_to_register);
340         DBG(" sender = %s", sender);
341
342         if (register_flag) {
343                 if (__bt_hfp_check_for_callpath(path_to_register, sender))
344                         return BT_HFP_AGENT_ERROR_ALREADY_EXSIST;
345
346                 /* add call path to the senders list*/
347                 DBG("Call path doesn't exist. Add path %s to global path",
348                                                 path_to_register);
349                 sender_info = g_new0(sender_info_t, 1);
350                 sender_info->sender_path = g_strdup(path_to_register);
351                 sender_info->sender_name = g_strdup(sender);
352                 call_senders_paths = g_slist_append(call_senders_paths,
353                                                                 sender_info);
354
355                 return BT_HFP_AGENT_ERROR_NONE;
356         } else {
357                 /*remove the call from senders list */
358                 GSList *s_list = call_senders_paths;
359
360                 while (s_list != NULL) {
361                         sender_info = s_list->data;
362
363                         if (sender_info == NULL)
364                                 return BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
365
366                         if (g_strcmp0(sender_info->sender_path,
367                                         path_to_register) == 0) {
368                                 call_senders_paths = g_slist_remove(
369                                                         call_senders_paths,
370                                                         sender_info);
371                                 __bt_hfp_clear_sender_path(sender_info);
372                                 return BT_HFP_AGENT_ERROR_NONE;
373                         }
374                         s_list = s_list->next;
375                 }
376
377                 return BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
378         }
379 }
380
381 static gboolean __bt_hfp_is_call_allowed(const char *call_path)
382 {
383         GSList *call_list = existing_call_list;
384
385         /*if prior call list doesn't exisit, allow the call as it can be a new-call*/
386         if (!existing_call_list) {
387                 DBG(" This must be a new call... Allow it!");
388                 return TRUE;
389         }
390
391         while (call_list != NULL) {
392
393                 struct telephony_call *t_call = call_list->data;
394
395                 if (g_strcmp0(t_call->call_path, call_path) == 0)
396                         return TRUE;
397
398                 call_list = call_list->next;
399         }
400
401         ERR("call is not allowed");
402         return FALSE;
403 }
404
405 static struct telephony_call *__bt_hfp_create_new_call(
406                                         const char *incoming_path,
407                                         uint32_t incoming_call_id,
408                                         const char *incoming_number,
409                                         const char *sender)
410 {
411         struct telephony_call *t_call = NULL;
412         GSList *call_list = existing_call_list;
413
414         while (call_list != NULL) {
415                 t_call = call_list->data;
416
417                 if (t_call->call_id == incoming_call_id)
418                         break;
419                 else
420                         t_call = NULL;
421
422                 call_list = call_list->next;
423         }
424
425         DBG("Create a new call");
426
427         if (t_call == NULL) {
428                 t_call = g_new0(struct telephony_call, 1);
429                 t_call->call_id = incoming_call_id;
430                 t_call->call_path = g_strdup(incoming_path);
431                 t_call->call_sender = g_strdup(sender);
432                 t_call->call_number = g_strdup(incoming_number);
433
434                 existing_call_list = g_slist_append(existing_call_list,
435                                                         t_call);
436         }
437         return t_call;
438 }
439
440 gboolean _bt_hfp_is_call_exist(void)
441 {
442         DBG("_bt_hfp_is_call_exist [%x]", existing_call_list);
443         if (existing_call_list)
444                 return TRUE;
445         else
446                 return FALSE;
447 }
448
449 static struct telephony_call *__bt_hfp_get_call_with_status(int call_status)
450 {
451         DBG("Get Call with status %d", call_status);
452
453         GSList *temp_list = existing_call_list;
454
455         if (existing_call_list != NULL) {
456                 while (temp_list != NULL) {
457                         struct telephony_call *t_call = temp_list->data;
458                         if (t_call->call_status == call_status)
459                                 return t_call;
460                         temp_list = temp_list->next;
461                 }
462         }
463
464         DBG("Existing call list is NULL. So return NULL");
465         return NULL;
466 }
467
468 static bt_hfp_agent_error_t __bt_hfp_modify_indicator(
469                         const char *indicator_name,
470                         int update_value)
471 {
472         bt_ag_indicators_t *hf_ind = NULL;
473         int i;
474 #ifdef TIZEN_FEATURE_BT_MEDIA_ENHANCE
475         if (g_strcmp0(indicator_name,
476                 HFP_AGENT_CALLSETUP_INDICATOR) == 0)
477                 _bt_ag_agent_check_transport_state();
478 #endif
479         for (i = 0; hfp_ag_ind[i].indicator_desc != NULL; i++) {
480                 if (g_str_equal(hfp_ag_ind[i].indicator_desc,
481                                                 indicator_name)) {
482                         hf_ind = &hfp_ag_ind[i];
483                         break;
484                 }
485         }
486
487         if (hf_ind == NULL)
488                 return BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
489
490         if (hf_ind->hfp_value == update_value && hf_ind->ignore)
491                 return BT_HFP_AGENT_ERROR_NONE;
492
493         if (hf_ind->is_activated ==  FALSE)
494                 return BT_HFP_AGENT_ERROR_NONE;
495
496         hf_ind->hfp_value = update_value;
497
498         DBG("updating hfp event indicator [%s] with value [%d]",
499                                 indicator_name, hf_ind->hfp_value);
500
501         return _bt_hfp_event_indicator(i);
502 }
503
504 static int __bt_hfp_get_indicator_value(
505                         const bt_ag_indicators_t *ag_indicators,
506                         const char *hf_desc)
507 {
508         int x;
509         for (x = 0; ag_indicators[x].indicator_desc != NULL; x++) {
510                 if (g_str_equal(ag_indicators[x].indicator_desc, hf_desc))
511                         return ag_indicators[x].hfp_value;
512         }
513
514         return ERR_NOT_FOUND;
515 }
516
517 static void __bt_hfp_handle_call_conference(void)
518 {
519         GSList *t_call_list;
520         struct telephony_call *t_active_call = NULL;
521         int t_active_call_count = 0;
522
523         struct telephony_call *t_held_call = NULL;
524         int t_held_call_count = 0;
525
526         for (t_call_list = existing_call_list; t_call_list != NULL;
527                 t_call_list = t_call_list->next) {
528
529                 struct telephony_call *t_call = t_call_list->data;
530
531                 if (t_call == NULL) {
532                         ERR("t_call is NULL");
533                         break;
534                 }
535
536                 if (t_call->call_status == HFP_CALL_STATUS_ACTIVE) {
537                         if (t_active_call == NULL)
538                                 t_active_call = t_call;
539
540                         t_active_call_count++;
541
542                         if (t_active_call_count >= 2) {
543                                 if (t_active_call->call_conference == FALSE)
544                                         t_active_call->call_conference = TRUE;
545                                 t_call->call_conference = TRUE;
546                         }
547
548                 } else if (t_call->call_status == HFP_CALL_STATUS_HOLD) {
549                         if (t_held_call == NULL)
550                                 t_held_call = t_call;
551
552                         t_held_call_count++;
553
554                         if (t_held_call_count >= 2) {
555                                 if (t_held_call->call_conference == FALSE)
556                                         t_held_call->call_conference = TRUE;
557                                 t_call->call_conference = TRUE;
558                         }
559                 }
560         }
561
562         if (t_held_call != NULL && t_held_call_count == 1)
563                 t_held_call->call_conference = FALSE;
564
565         if (t_active_call != NULL && t_active_call_count == 1)
566                 t_active_call->call_conference = FALSE;
567 }
568
569 static gboolean __bt_hfp_on_call_hold_timeout(gpointer t_data)
570 {
571         int status;
572
573         if (__bt_hfp_get_call_with_status(HFP_CALL_STATUS_HOLD)) {
574                 if (__bt_hfp_get_call_with_status(HFP_CALL_STATUS_ACTIVE))
575                         status = INDICATOR_EVENT_CALLHELD_MULTIPLE;
576                 else
577                         status = INDICATOR_EVENT_CALLHELD_ON_HOLD;
578         } else {
579                 status = INDICATOR_EVENT_CALLHELD_NONE;
580         }
581
582         __bt_hfp_modify_indicator("callheld", status);
583
584         call_on_hold_timer = 0;
585         return FALSE;
586 }
587
588 static void __bt_hfp_handle_call_on_hold_request(void)
589 {
590         DBG(" Starting the timer for call on hold");
591         if (call_on_hold_timer)
592                 g_source_remove(call_on_hold_timer);
593
594         call_on_hold_timer = g_timeout_add(250, __bt_hfp_on_call_hold_timeout,
595                                                         NULL);
596         DBG(" returning from the timer call");
597 }
598
599 static void __bt_hfp_set_call_status(struct telephony_call *t_call,
600                                 int call_status)
601 {
602         int call_held = 0;
603         int org_status = t_call->call_status;
604
605         call_held = __bt_hfp_get_indicator_value(hfp_ag_ind, "callheld");
606
607         if (org_status == call_status) {
608                 DBG("Ignore the CSD Call state change to existing state");
609                 return;
610         }
611
612         t_call->call_status = call_status;
613
614         DBG(" call status is   %d", call_status);
615
616         switch (call_status) {
617         case HFP_CALL_STATUS_IDLE:
618                 if (t_call->call_setup) {
619                         __bt_hfp_modify_indicator("callsetup",
620                                 INDICATOR_EVENT_CALLSETUP_INACTIVE);
621                         if (!t_call->call_originating)
622                                 _bt_calling_stopped_indicator();
623                 }
624
625                 g_free(t_call->call_number);
626                 t_call->call_number = NULL;
627                 t_call->call_originating = FALSE;
628                 t_call->call_emergency = FALSE;
629                 t_call->call_on_hold = FALSE;
630                 t_call->call_conference = FALSE;
631                 t_call->call_setup = FALSE;
632                 break;
633
634         case HFP_CALL_STATUS_COMING:
635                 t_call->call_originating = FALSE;
636                 t_call->call_setup = TRUE;
637                 __bt_hfp_modify_indicator("callsetup",
638                                         INDICATOR_EVENT_CALLSETUP_INCOMING);
639                 break;
640
641         case HFP_CALL_STATUS_CREATE:
642                 t_call->call_originating = TRUE;
643                 t_call->call_setup = TRUE;
644                 break;
645
646         case HFP_CALL_STATUS_MO_ALERTING:
647                 __bt_hfp_modify_indicator("callsetup",
648                                         INDICATOR_EVENT_CALLSETUP_ALERTING);
649                 break;
650
651         case HFP_CALL_STATUS_MT_ALERTING: {
652                 int  t_number = AGENT_NUMBER_TYPE_TELEPHONY;
653
654                 if (t_call->call_number == NULL) {
655                         t_number = AGENT_NUMBER_TYPE_TELEPHONY;
656                 } else {
657                         if (t_call->call_number[0] == '+' ||
658                                 strncmp(t_call->call_number, "00", 2) == 0)
659                                 t_number = AGENT_NUMBER_TYPE_INTERNATIONAL;
660                 }
661
662                 if (org_status == HFP_CALL_STATUS_WAITING)
663                         _bt_incoming_call_indicator(t_call->call_number,
664                                                 t_number);
665         }
666                 break;
667
668         case HFP_CALL_STATUS_ACTIVE:
669                 DBG(" This is an Active call");
670                 if (t_call->call_on_hold) {
671                         t_call->call_on_hold = FALSE;
672                         __bt_hfp_handle_call_on_hold_request();
673                 } else {
674                         if (!g_slist_find(agent_active_call_list, t_call)) {
675                                 DBG(" This call is not in the active call list. So Add it to the list.\n");
676                                 agent_active_call_list =
677                                         g_slist_prepend(agent_active_call_list,
678                                                         t_call);
679                         }
680                         if (g_slist_length(agent_active_call_list) == 1) {
681                                 DBG(" Update indicator to show the call presence.\n");
682                                 __bt_hfp_modify_indicator("call",
683                                                 INDICATOR_EVENT_CALL_ACTIVE);
684                         }
685
686                         __bt_hfp_modify_indicator("callsetup",
687                                         INDICATOR_EVENT_CALLSETUP_INACTIVE);
688                         __bt_hfp_handle_call_on_hold_request();
689
690                         if (!t_call->call_originating)
691                                 _bt_calling_stopped_indicator();
692
693                         t_call->call_setup = FALSE;
694                 }
695                 break;
696
697         case HFP_CALL_STATUS_MO_RELEASE:
698         case HFP_CALL_STATUS_MT_RELEASE:
699                 agent_active_call_list = g_slist_remove(agent_active_call_list,
700                                                         t_call);
701                 if (g_slist_length(agent_active_call_list) == 0)
702                         __bt_hfp_modify_indicator("call",
703                                         INDICATOR_EVENT_CALL_INACTIVE);
704
705                 if (org_status == HFP_CALL_STATUS_HOLD)
706                         __bt_hfp_modify_indicator("callheld", INDICATOR_EVENT_CALLHELD_NONE);
707
708                 if ((org_status == HFP_CALL_STATUS_MO_ALERTING) ||
709                         (org_status == HFP_CALL_STATUS_COMING) ||
710                         (org_status == HFP_CALL_STATUS_CREATE) ||
711                         (org_status == HFP_CALL_STATUS_WAITING)) {
712                                 __bt_hfp_modify_indicator("callsetup",
713                                         INDICATOR_EVENT_CALLSETUP_INACTIVE);
714                 }
715
716                 if (org_status == HFP_CALL_STATUS_COMING) {
717                         if (!t_call->call_originating)
718                                 _bt_calling_stopped_indicator();
719                 }
720                 existing_call_list = g_slist_remove(existing_call_list, t_call);
721                 __bt_hfp_free_call(t_call);
722                 break;
723
724         case HFP_CALL_STATUS_HOLD:
725                 t_call->call_on_hold = TRUE;
726                 __bt_hfp_handle_call_on_hold_request();
727                 break;
728
729         case HFP_CALL_STATUS_TERMINATED:
730                 if (t_call->call_on_hold &&
731                 !__bt_hfp_get_call_with_status(HFP_CALL_STATUS_HOLD)) {
732                         __bt_hfp_modify_indicator("callheld",
733                                         INDICATOR_EVENT_CALLHELD_NONE);
734                         return;
735                 }
736
737                 if (call_held == INDICATOR_EVENT_CALLHELD_MULTIPLE &&
738                 __bt_hfp_get_call_with_status(HFP_CALL_STATUS_HOLD) &&
739                 !__bt_hfp_get_call_with_status(HFP_CALL_STATUS_ACTIVE))
740                         __bt_hfp_modify_indicator("callheld",
741                                 INDICATOR_EVENT_CALLHELD_ON_HOLD);
742                 break;
743
744         case HFP_CALL_STATUS_PROCEEDING:
745         case HFP_CALL_STATUS_SWAP_INITIATED:
746         case HFP_CALL_STATUS_RETRIEVE_INITIATED:
747         case HFP_CALL_STATUS_RECONNECT_PENDING:
748         case HFP_CALL_STATUS_HOLD_INITIATED:
749         case HFP_CALL_STATUS_WAITING:
750         case HFP_CALL_STATUS_ANSWERED:
751                 break;
752
753         default:
754                 break;
755         }
756
757         /* Update the call conference status for each of the call */
758         __bt_hfp_handle_call_conference();
759 }
760
761 bt_hfp_agent_error_t _bt_hfp_incoming_call(const char *call_path,
762                 const char *incoming_number,
763                 uint32_t incoming_call_id,
764                 const char *sender)
765 {
766         struct telephony_call *t_call = NULL;
767         bt_hfp_agent_error_t hfp_err = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
768         int t_number = AGENT_NUMBER_TYPE_TELEPHONY;
769         int error;
770
771         if (sender == NULL || call_path == NULL)
772                 return BT_HFP_AGENT_ERROR_INVALID_PARAM;
773
774         if (!__bt_hfp_check_for_callpath(call_path, sender))
775                 return BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
776
777         if (!__bt_hfp_is_call_allowed(call_path))
778                 return BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
779
780         /* Its a new call, so create a list for it*/
781         t_call = __bt_hfp_create_new_call(call_path, incoming_call_id,
782                                                 incoming_number,
783                                                 sender);
784
785         /*get the type of the incoming number*/
786         if (t_call->call_number == NULL) {
787                 t_number = AGENT_NUMBER_TYPE_TELEPHONY;
788                 ERR("call_number is NULL");
789         } else {
790                 if (t_call->call_number[0] == '+' || strncmp(
791                                         t_call->call_number, "00", 2) == 0)
792                         t_number = AGENT_NUMBER_TYPE_INTERNATIONAL;
793
794                 if (__bt_hfp_get_call_with_status(HFP_CALL_STATUS_ACTIVE) ||
795                                 __bt_hfp_get_call_with_status(HFP_CALL_STATUS_HOLD)) {
796                         error = _bt_call_waiting_indicator(t_call->call_number,
797                                                                 t_number);
798                         if (error != 0)
799                                 ERR(" Fail to update CCWA information");
800
801                         if (update_events) {
802                                 hfp_err = __bt_hfp_modify_indicator(
803                                         HFP_AGENT_CALLSETUP_INDICATOR,
804                                         HFP_INCOMING_CALLSETUP);
805                                 if (hfp_err  != BT_HFP_AGENT_ERROR_NONE)
806                                         ERR("Failed to update the indicators");
807                         }
808                         __bt_hfp_set_call_status(t_call, HFP_CALL_STATUS_WAITING);
809                 } else {
810                         DBG(" It is an incoming call");
811
812                         if (update_events) {
813                                 hfp_err = __bt_hfp_modify_indicator(
814                                         HFP_AGENT_CALLSETUP_INDICATOR,
815                                         HFP_INCOMING_CALLSETUP);
816                                 if (hfp_err  != BT_HFP_AGENT_ERROR_NONE)
817                                         ERR("Failed to update the indicators");
818                         }
819
820                         error = _bt_incoming_call_indicator(t_call->call_number,
821                                                                 t_number);
822
823                         __bt_hfp_set_call_status(t_call, HFP_CALL_STATUS_COMING);
824
825                         if (error == -ENODEV)
826                                 return BT_HFP_AGENT_ERROR_NOT_CONNECTED;
827                         else if (error == -EBUSY)
828                                 return BT_HFP_AGENT_ERROR_BUSY;
829                 }
830         }
831
832         return hfp_err;
833 }
834
835 bt_hfp_agent_error_t _bt_hfp_outgoing_call(const char *call_path,
836                                 const char *number,
837                                 uint32_t call_id, const char *sender)
838 {
839         struct telephony_call *t_call = NULL;
840         bt_hfp_agent_error_t ret =  BT_HFP_AGENT_ERROR_NONE;
841         gboolean err = FALSE;
842
843
844         err = __bt_hfp_check_for_callpath(call_path, sender);
845         if (!err)
846                 return BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
847
848         /*check if the call_path exisits in the active call list, if not
849         don't allow as the call may be initated by some other application*/
850
851         err = __bt_hfp_is_call_allowed(call_path);
852         if (!err)
853                 return BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
854
855         /* create a new call for the call_path */
856         t_call = __bt_hfp_create_new_call(call_path, call_id, number, sender);
857
858         __bt_hfp_set_call_status(t_call, HFP_CALL_STATUS_CREATE);
859
860         ret = __bt_hfp_modify_indicator(HFP_AGENT_CALLSETUP_INDICATOR,
861                                         HFP_OUTGOING_CALLSETUP);
862         if (ret != BT_HFP_AGENT_ERROR_NONE)
863                 DBG("Error in updating indicator");
864
865         return ret;
866 }
867
868 bt_hfp_agent_error_t _bt_hfp_change_call_status(const char *call_path,
869                 const char *number, uint32_t call_status,
870                 uint32_t call_id, const char *sender)
871 {
872         GSList *call_list = existing_call_list;
873         struct telephony_call *t_call = NULL;
874         gboolean ret = FALSE;
875
876         if (call_status > AG_MAX_LENGTH)
877                 return BT_HFP_AGENT_ERROR_INVALID_PARAM;
878
879         ret = __bt_hfp_check_for_callpath(call_path, sender);
880
881         if (!ret)
882                 return BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
883
884         ret = __bt_hfp_is_call_allowed(call_path);
885         if (!ret)
886                 return BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
887
888         /* find call with the given call_id*/
889         DBG(" Find call with the given call Id from the list");
890         while (call_list != NULL) {
891                 t_call = call_list->data;
892
893                 if (t_call->call_id == call_id) {
894                         DBG("Call Id Match");
895                         break;
896                 } else {
897                         t_call = NULL;
898                 }
899
900                 call_list = call_list->next;
901         }
902
903         if (t_call == NULL) {
904                 DBG("t_call is NULL. So create new call");
905                 t_call = __bt_hfp_create_new_call(call_path,
906                                         call_id, number, sender);
907         }
908
909         __bt_hfp_set_call_status(t_call, call_status);
910
911         return BT_HFP_AGENT_ERROR_NONE;
912 }
913
914 static int __bt_hfp_update_battery_strength(int32_t battery_strength)
915 {
916         int bat_strength = 0;
917         int x, change_value;
918
919         DBG(" Battery strength is.... %d", battery_strength);
920
921         /* get the current battery level */
922         for (x = 0; hfp_ag_ind[x].indicator_desc != NULL; x++) {
923                 if (g_str_equal(hfp_ag_ind[x].indicator_desc, "battchg"))
924                         bat_strength = hfp_ag_ind[x].hfp_value;
925         }
926
927         /* We need to send battery status ranging from 0-5 */
928         if (battery_strength < 5)
929                  change_value = 0;
930         else if (battery_strength >= 100)
931                 change_value = 5;
932         else
933                 change_value = battery_strength / 20 + 1;
934
935         if (bat_strength == change_value) {
936                 DBG("no change in battery strength");
937                 return 0;
938         }
939
940         if (__bt_hfp_modify_indicator("battchg",
941                         change_value) == BT_HFP_AGENT_ERROR_NONE)
942                 return 1;
943
944         return 0;
945 }
946
947 static int __bt_hfp_update_signal_strength(int32_t signal_strength_bars)
948 {
949         if (signal_strength_bars < 0)
950                 signal_strength_bars = 0;
951         else if (signal_strength_bars > 5)
952                 signal_strength_bars = 5;
953
954         if (network_info.signal_strength == signal_strength_bars) {
955                 DBG("no change in signal strength");
956                 return 0;
957         }
958
959         network_info.signal_strength = signal_strength_bars;
960
961         if (__bt_hfp_modify_indicator("signal",
962                         signal_strength_bars) == BT_HFP_AGENT_ERROR_NONE)
963                 return 1;
964
965         return 0;
966 }
967
968 static int __bt_hfp_update_registration_status(uint8_t register_status)
969 {
970         bt_hfp_agent_error_t reg_ret = BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
971
972         DBG("Updating registration status to.... %d", register_status);
973
974         if (network_info.network_status == register_status) {
975                 DBG("No change in registration status");
976                 return 0;
977         }
978
979         if (register_status == BT_AGENT_NETWORK_REG_STATUS_ROAMING) {
980                 reg_ret = __bt_hfp_modify_indicator("roam",
981                                         INDICATOR_EVENT_ROAM_ACTIVE);
982
983                 if (network_info.network_status >
984                                         BT_AGENT_NETWORK_REG_STATUS_ROAMING)
985                         reg_ret = __bt_hfp_modify_indicator("service",
986                                         INDICATOR_EVENT_SERVICE_PRESENT);
987         } else if (register_status == BT_AGENT_NETWORK_REG_STATUS_HOME) {
988                 reg_ret = __bt_hfp_modify_indicator("roam",
989                                         INDICATOR_EVENT_ROAM_INACTIVE);
990
991                 if (network_info.network_status >
992                                         BT_AGENT_NETWORK_REG_STATUS_ROAMING)
993                         reg_ret = __bt_hfp_modify_indicator("service",
994                                         INDICATOR_EVENT_SERVICE_PRESENT);
995         } else if (register_status == BT_AGENT_NETWORK_REG_STATUS_OFFLINE ||
996                 register_status == BT_AGENT_NETWORK_REG_STATUS_SEARCHING ||
997                 register_status == BT_AGENT_NETWORK_REG_STATUS_NO_SIM ||
998                 register_status == BT_AGENT_NETWORK_REG_STATUS_POWEROFF ||
999                 register_status == BT_AGENT_NETWORK_REG_STATUS_POWERSAFE ||
1000                 register_status == BT_AGENT_NETWORK_REG_STATUS_NO_COVERAGE ||
1001                 register_status == BT_AGENT_NETWORK_REG_STATUS_REJECTED ||
1002                 register_status == BT_AGENT_NETWORK_REG_STATUS_UNKOWN) {
1003                 if (network_info.network_status <
1004                                         BT_AGENT_NETWORK_REG_STATUS_OFFLINE)
1005                         reg_ret = __bt_hfp_modify_indicator("service",
1006                                                 INDICATOR_EVENT_SERVICE_NONE);
1007         }
1008
1009         network_info.network_status = register_status;
1010         if (reg_ret == BT_HFP_AGENT_ERROR_NONE)
1011                 return 1;
1012
1013         return 0;
1014 }
1015
1016 int _bt_hfp_set_property_value(const char *property, int value)
1017 {
1018         int ret = 0;
1019
1020         DBG("Property is %s", property);
1021
1022         if (g_str_equal("RegistrationChanged", property))
1023                 ret = __bt_hfp_update_registration_status(value);
1024
1025         else if (g_str_equal("SignalBarsChanged", property))
1026                 ret = __bt_hfp_update_signal_strength(value);
1027
1028         else if (g_str_equal("BatteryBarsChanged", property))
1029                 ret = __bt_hfp_update_battery_strength(value);
1030
1031         return ret;
1032 }
1033
1034 int _bt_hfp_set_property_name(const char *property, const char *operator_name)
1035 {
1036         int ret = 0;
1037
1038         if (operator_name == NULL)
1039                 return 0;
1040
1041         if (g_str_equal("OperatorNameChanged", property)) {
1042                 g_free(network_info.network_operator_name);
1043                 network_info.network_operator_name =
1044                                 g_strndup(operator_name, 16);
1045                 ret = 1;
1046         }
1047
1048         if (g_str_equal("SubscriberNumberChanged", property)) {
1049                 g_free(ag_subscriber_num);
1050                 ag_subscriber_num = g_strdup(operator_name);
1051                 DBG("HFP: subscriber_number updated: %s", ag_subscriber_num);
1052                 ret = 1;
1053         }
1054         return ret;
1055 }
1056
1057 static int __bt_hfp_answer_call(struct telephony_call *t_call)
1058 {
1059         if (t_call->call_id != 0 && t_call->call_path != NULL &&
1060                 t_call->call_sender != NULL) {
1061                 _bt_ag_agent_answer_call(t_call->call_id,
1062                                                 t_call->call_path,
1063                                                 t_call->call_sender);
1064                 return 0;
1065         }
1066         return -1;
1067 }
1068
1069 void _bt_hfp_answer_call_request(void *t_device)
1070 {
1071         struct telephony_call *t_call;
1072
1073         t_call = __bt_hfp_get_call_with_status(HFP_CALL_STATUS_COMING);
1074
1075         if (t_call == NULL)
1076                 t_call = __bt_hfp_get_call_with_status(
1077                                 HFP_CALL_STATUS_MT_ALERTING);
1078
1079         if (t_call == NULL)
1080                 t_call = __bt_hfp_get_call_with_status(
1081                         HFP_CALL_STATUS_PROCEEDING);
1082
1083         if (t_call == NULL)
1084                 t_call = __bt_hfp_get_call_with_status(
1085                                         HFP_CALL_STATUS_WAITING);
1086
1087         if (t_call == NULL) {
1088                 _bt_answer_call_response(t_device,
1089                                         HFP_STATE_MNGR_ERR_NOT_ALLOWED);
1090                 return;
1091         }
1092
1093         if (__bt_hfp_answer_call(t_call) <  0)
1094                 _bt_answer_call_response(t_device,
1095                                 HFP_STATE_MNGR_ERR_AG_FAILURE);
1096         else
1097                 _bt_answer_call_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1098 }
1099
1100
1101 void _bt_hfp_dial_number_request(const char *dial_number, void *t_device)
1102 {
1103         int call_flag = caller_id;
1104         bt_hfp_agent_error_t error_code = 0;
1105
1106         if (strncmp(dial_number, "#31#", 4) == 0) {
1107                 dial_number = dial_number + 4;
1108                 call_flag = ALLOW_CALL_FLAG;
1109         } else if (strncmp(dial_number, "*31#", 4) == 0) {
1110                 dial_number = dial_number + 4;
1111                 call_flag = RESTRAIN_CALL_FLAG;
1112         } else if (dial_number[0] == '>') {
1113                 int dial_location = strtol(&dial_number[1], NULL, 0);
1114
1115                 error_code = _bt_ag_agent_dial_memory(dial_location);
1116
1117                 if (error_code ==  BT_HFP_AGENT_ERROR_NONE)
1118                         _bt_dial_number_response(t_device,
1119                                         HFP_STATE_MNGR_ERR_NONE);
1120                 else
1121                         _bt_dial_number_response(t_device,
1122                                 HFP_STATE_MNGR_ERR_AG_FAILURE);
1123                 return;
1124         }
1125
1126         error_code = _bt_ag_agent_dial_num(dial_number, call_flag);
1127
1128         if (error_code == BT_HFP_AGENT_ERROR_NONE) {
1129                 _bt_dial_number_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1130                 return;
1131         }
1132
1133         _bt_dial_number_response(t_device, HFP_STATE_MNGR_ERR_AG_FAILURE);
1134
1135 }
1136
1137 void _bt_hfp_update_event_request(int indicator, void *t_device)
1138 {
1139         if (indicator == 1)
1140                 update_events = TRUE;
1141         else
1142                 update_events = FALSE;
1143
1144         _bt_event_reporting_response(t_device,
1145                                         HFP_STATE_MNGR_ERR_NONE);
1146 }
1147
1148 static int __bt_bt_hfp_reject_call(struct telephony_call *t_call)
1149 {
1150         gboolean ret;
1151
1152         if (t_call != NULL) {
1153                 DBG(" rejecting call from sender %s with call path %s and call id %d",
1154                                 t_call->call_sender,
1155                                 t_call->call_path,
1156                                 t_call->call_id);
1157
1158                 ret = _bt_ag_agent_reject_call(t_call->call_id,
1159                                                 t_call->call_path,
1160                                                 t_call->call_sender);
1161                 if (ret)
1162                         return 0;
1163         }
1164
1165         return -1;
1166 }
1167
1168 static int __bt_hfp_release_call(struct telephony_call *t_call)
1169 {
1170         gboolean ret = _bt_ag_agent_release_call(t_call->call_id,
1171                                         t_call->call_path,
1172                                         t_call->call_sender);
1173         if (!ret)
1174                 return -1;
1175
1176         return 0;
1177 }
1178
1179 static int __bt_hfp_release_conference(void)
1180 {
1181         GSList *temp_list = existing_call_list;
1182
1183         while (temp_list != NULL) {
1184                 struct telephony_call *t_call = temp_list->data;
1185
1186                 if (t_call->call_conference)
1187                         __bt_hfp_release_call(t_call);
1188
1189                 temp_list = temp_list->next;
1190         }
1191         return 0;
1192 }
1193
1194 void _bt_hfp_terminate_call_request(void *t_device)
1195 {
1196         struct telephony_call *t_call;
1197         struct telephony_call *t_alert = NULL;
1198         int t_error = 0;
1199
1200         t_call = __bt_hfp_get_call_with_status(HFP_CALL_STATUS_ACTIVE);
1201
1202         if (t_call == NULL) {
1203                 DBG("Find non-idle call");
1204                 GSList *temp_call_list = existing_call_list;
1205                 while (temp_call_list != NULL) {
1206                         t_call = temp_call_list->data;
1207
1208                         if (t_call->call_status == HFP_AGENT_CALL_IDLE)
1209                                 temp_call_list = temp_call_list->next;
1210                         else
1211                                 break;
1212                 }
1213         }
1214
1215         if (t_call == NULL) {
1216                 DBG("Seems like there are no active calls. So do not allow the call");
1217                 _bt_terminate_call_response(t_device,
1218                                         HFP_STATE_MNGR_ERR_NOT_ALLOWED);
1219                 return;
1220         }
1221
1222         if (__bt_hfp_get_call_with_status(HFP_CALL_STATUS_WAITING) != NULL) {
1223                 int value = 1;
1224                 t_error = _bt_ag_agent_threeway_call(value, t_call->call_path,
1225                                 t_call->call_sender);
1226         } else if ((t_alert = __bt_hfp_get_call_with_status(
1227                                 HFP_CALL_STATUS_CREATE))
1228                 != NULL) {
1229                 t_error = __bt_hfp_release_call(t_alert);
1230         } else if ((t_alert = __bt_hfp_get_call_with_status(
1231                                 HFP_CALL_STATUS_MO_ALERTING))
1232                 != NULL) {
1233                 t_error = __bt_hfp_release_call(t_alert);
1234         } else if       ((t_alert =  __bt_hfp_get_call_with_status(
1235                         HFP_CALL_STATUS_COMING)) != NULL) {
1236                 t_error = __bt_bt_hfp_reject_call(t_alert);
1237         } else if (t_call->call_conference)
1238                 t_error = __bt_hfp_release_conference();
1239         else
1240                 t_error = __bt_hfp_release_call(t_call);
1241
1242         if (t_error < 0)
1243                 _bt_terminate_call_response(t_device,
1244                                 HFP_STATE_MNGR_ERR_AG_FAILURE);
1245         else
1246                 _bt_terminate_call_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1247 }
1248
1249 void _bt_hfp_call_hold_request(const char *t_cmd, void *t_device)
1250 {
1251
1252         struct telephony_call *t_call = NULL;
1253         GSList *t_sender_list = call_senders_paths;
1254         sender_info_t *sender_info = NULL;
1255         uint32_t t_chld_value;
1256
1257         t_call = __bt_hfp_get_call_with_status(HFP_CALL_STATUS_ACTIVE);
1258         if (t_call == NULL) {
1259                 if ((t_call =
1260                         __bt_hfp_get_call_with_status(HFP_CALL_STATUS_HOLD))
1261                         == NULL) {
1262                         if ((t_call = __bt_hfp_get_call_with_status(
1263                                 HFP_CALL_STATUS_WAITING)) == NULL) {
1264                                 /* means there is no outgoing call*/
1265                                 _bt_call_hold_response(t_device,
1266                                         HFP_STATE_MNGR_ERR_AG_FAILURE);
1267                                 return;
1268                         }
1269                 }
1270         }
1271
1272         while (t_sender_list != NULL) {
1273                 sender_info = t_sender_list->data;
1274                 if (sender_info == NULL) {
1275                         _bt_call_hold_response(t_device,
1276                                 HFP_STATE_MNGR_ERR_AG_FAILURE);
1277                         return;
1278                 }
1279                 if (g_strcmp0(t_call->call_path, sender_info->sender_path)
1280                                 == 0)
1281                         break;
1282
1283                 t_sender_list = t_sender_list->next;
1284         }
1285
1286         t_chld_value = strtoul(&t_cmd[0], NULL, 0);
1287         gboolean ret = _bt_ag_agent_threeway_call(t_chld_value,
1288                         t_call->call_path,
1289                         t_call->call_sender);
1290
1291         if (ret == TRUE)
1292                 _bt_call_hold_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1293         else
1294                 _bt_call_hold_response(t_device, HFP_STATE_MNGR_ERR_AG_FAILURE);
1295 }
1296
1297 void _bt_hfp_key_press_request(const char *t_key_press, void *t_device)
1298 {
1299         struct telephony_call *t_active_call;
1300         struct telephony_call *t_waiting_call;
1301         int t_error = 0;
1302
1303         t_waiting_call = __bt_hfp_get_call_with_status(HFP_CALL_STATUS_COMING);
1304
1305         if (t_waiting_call == NULL)
1306                 t_waiting_call = __bt_hfp_get_call_with_status(
1307                                 HFP_CALL_STATUS_MT_ALERTING);
1308
1309         if (t_waiting_call == NULL)
1310                 t_waiting_call = __bt_hfp_get_call_with_status(
1311                                 HFP_CALL_STATUS_PROCEEDING);
1312
1313         t_active_call = __bt_hfp_get_call_with_status(HFP_CALL_STATUS_ACTIVE);
1314
1315
1316         if (t_waiting_call != NULL)
1317                 t_error = __bt_hfp_answer_call(t_waiting_call);
1318         else if (t_active_call != NULL)
1319                 t_error = __bt_hfp_release_call(t_active_call);
1320         else {
1321                 if (_bt_ag_agent_dial_last_num(t_device) !=
1322                                 BT_HFP_AGENT_ERROR_NONE)
1323                         _bt_dial_number_response(t_device,
1324                                                 HFP_STATE_MNGR_ERR_AG_FAILURE);
1325                 else
1326                         _bt_dial_number_response(t_device,
1327                                                 HFP_STATE_MNGR_ERR_NONE);
1328                 return;
1329         }
1330
1331         if (t_error < 0)
1332                 _bt_key_press_response(t_device,
1333                                         HFP_STATE_MNGR_ERR_AG_FAILURE);
1334         else
1335                 _bt_key_press_response(t_device,
1336                                                 HFP_STATE_MNGR_ERR_NONE);
1337 }
1338
1339 void _bt_hfp_last_dialed_number_request(void *t_device)
1340 {
1341         bt_hfp_agent_error_t error = _bt_ag_agent_dial_last_num(t_device);
1342
1343         if (error != BT_HFP_AGENT_ERROR_NONE)
1344                 _bt_dial_number_response(t_device,
1345                                         HFP_STATE_MNGR_ERR_AG_FAILURE);
1346         else
1347                 _bt_dial_number_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1348 }
1349
1350 void _bt_hfp_channel_dtmf_request(char t_tone, void *t_device)
1351 {
1352         char buf[2] = { t_tone, '\0' };
1353         char *tone_buffer = buf;
1354
1355         struct telephony_call *t_call = __bt_hfp_get_call_with_status(
1356                                         HFP_CALL_STATUS_ACTIVE);
1357         if (t_call == NULL) {
1358                 t_call = __bt_hfp_get_call_with_status(HFP_CALL_STATUS_HOLD);
1359                 if (t_call == NULL) {
1360                         t_call = __bt_hfp_get_call_with_status(
1361                                         HFP_CALL_STATUS_WAITING);
1362                         if (t_call == NULL) {
1363                                 /* if this point is reached,
1364                                 it means there is no ongoing call */
1365                                 _bt_transmit_dtmf_response(t_device,
1366                                                 HFP_STATE_MNGR_ERR_AG_FAILURE);
1367                                 return;
1368                         }
1369                 }
1370         }
1371
1372         if (_bt_ag_agent_send_dtmf(tone_buffer, t_call->call_path,
1373                 t_call->call_sender) != BT_HFP_AGENT_ERROR_NONE) {
1374                 _bt_transmit_dtmf_response(t_device,
1375                                         HFP_STATE_MNGR_ERR_AG_FAILURE);
1376                         return;
1377                 }
1378
1379         _bt_transmit_dtmf_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1380 }
1381
1382 void _bt_hfp_vendor_cmd_request(const char *cmd,
1383                                                 void *t_device)
1384 {
1385         GSList *t_sender_list = call_senders_paths;
1386         sender_info_t *sender_info = NULL;
1387         GSList *l;
1388         bt_hfp_agent_error_t error = BT_HFP_AGENT_ERROR_NONE;
1389
1390         if (NULL != t_sender_list) {
1391                 for (l = t_sender_list; l != NULL; l = l->next) {
1392                         sender_info = l->data;
1393                         error = _bt_ag_agent_vendor_cmd(cmd,
1394                                 sender_info->sender_path,
1395                                 sender_info->sender_name);
1396                         if (error != BT_HFP_AGENT_ERROR_NONE)
1397                                 break;
1398                 }
1399         }
1400
1401         if (error != BT_HFP_AGENT_ERROR_NONE)
1402                 _bt_vendor_cmd_response(t_device,
1403                                         HFP_STATE_MNGR_ERR_AG_FAILURE);
1404         else
1405                 _bt_vendor_cmd_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1406 }
1407
1408 void _bt_hfp_subscriber_number_request(void *t_device)
1409 {
1410         if (ag_subscriber_num != NULL) {
1411
1412                 int  t_number =  AGENT_NUMBER_TYPE_TELEPHONY;
1413
1414                 if (ag_subscriber_num[0] == '+' || strncmp(
1415                                         ag_subscriber_num, "00", 2) == 0)
1416                         t_number = AGENT_NUMBER_TYPE_INTERNATIONAL;
1417
1418                 _bt_subscriber_number_indicator(ag_subscriber_num,
1419                         t_number, AGENT_SUBSCRIBER_SERVICE_VOICE);
1420         }
1421
1422         _bt_subscriber_number_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1423 }
1424
1425 static int __bt_hfp_get_call_status(struct telephony_call *t_call)
1426 {
1427         switch (t_call->call_status) {
1428         case HFP_CALL_STATUS_IDLE:
1429         case HFP_CALL_STATUS_MO_RELEASE:
1430         case HFP_CALL_STATUS_MT_RELEASE:
1431         case HFP_CALL_STATUS_TERMINATED:
1432                 return -1;
1433
1434         case HFP_CALL_STATUS_ANSWERED:
1435         case HFP_CALL_STATUS_ACTIVE:
1436         case HFP_CALL_STATUS_RECONNECT_PENDING:
1437         case HFP_CALL_STATUS_SWAP_INITIATED:
1438         case HFP_CALL_STATUS_HOLD_INITIATED:
1439                 return AGENT_CALL_STATUS_ACTIVE;
1440
1441         case HFP_CALL_STATUS_RETRIEVE_INITIATED:
1442         case HFP_CALL_STATUS_HOLD:
1443                 return AGENT_CALL_STATUS_HELD;
1444
1445         case  HFP_CALL_STATUS_WAITING:
1446                 return AGENT_CALL_STATUS_WAITING;
1447
1448         case HFP_CALL_STATUS_CREATE:
1449                 return AGENT_CALL_STATUS_DIALING;
1450
1451         case HFP_CALL_STATUS_PROCEEDING:
1452                 if (t_call->call_originating)
1453                         return AGENT_CALL_STATUS_DIALING;
1454                 if (g_slist_length(agent_active_call_list) > 0)
1455                         return AGENT_CALL_STATUS_WAITING;
1456                 else
1457                         return AGENT_CALL_STATUS_INCOMING;
1458
1459         case HFP_CALL_STATUS_COMING:
1460                 if (g_slist_length(agent_active_call_list) > 0)
1461                         return AGENT_CALL_STATUS_WAITING;
1462                 else
1463                         return AGENT_CALL_STATUS_INCOMING;
1464
1465         case HFP_CALL_STATUS_MO_ALERTING:
1466                 return AGENT_CALL_STATUS_ALERTING;
1467
1468         case HFP_CALL_STATUS_MT_ALERTING:
1469                 return AGENT_CALL_STATUS_INCOMING;
1470
1471         default:
1472                 return -1;
1473         }
1474 }
1475
1476 void _bt_list_current_calls(void *t_device)
1477 {
1478         GSList *t_call_list =  existing_call_list;
1479         int t_status;
1480         int t_number = AGENT_NUMBER_TYPE_TELEPHONY;
1481         int t_direction, t_call_conference;
1482         int index;
1483
1484         while (t_call_list != NULL) {
1485                 struct telephony_call *t_call  = t_call_list->data;
1486                 t_status = __bt_hfp_get_call_status(t_call);
1487                 if (t_status >= 0) {
1488                         if (t_call->call_originating != TRUE)
1489                                 t_direction = AGENT_CALL_DIRECTION_INCOMING;
1490                         else
1491                                 t_direction = AGENT_CALL_DIRECTION_OUTGOING;
1492
1493                         if (t_call->call_conference != TRUE)
1494                                 t_call_conference = AGENT_CALL_MULTIPARTY_NO;
1495                         else
1496                                 t_call_conference = AGENT_CALL_MULTIPARTY_YES;
1497
1498                         if (t_call->call_number == NULL) {
1499                                 t_number = AGENT_NUMBER_TYPE_TELEPHONY;
1500                         } else {
1501                                 if (t_call->call_number[0] == '+' || strncmp(
1502                                                 t_call->call_number, "00", 2) == 0)
1503                                         t_number = AGENT_NUMBER_TYPE_INTERNATIONAL;
1504                         }
1505
1506                         index = t_call->call_id;
1507                         _bt_list_current_call_indicator(t_device, index, t_direction,
1508                                         AGENT_CALL_MODE_VOICE,
1509                                         t_status,
1510                                         t_call->call_number,
1511                                         t_call_conference,
1512                                         t_number);
1513                 }
1514                 t_call_list = t_call_list->next;
1515         }
1516         _bt_list_current_calls_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1517 }
1518
1519 void _bt_hfp_release_all_calls_by_sender(const char *sender)
1520 {
1521         GSList *temp_list = existing_call_list;
1522         GSList *next_list;
1523
1524         if (!sender)
1525                 return;
1526
1527         DBG("sender [%s]", sender);
1528
1529         while (temp_list != NULL) {
1530                 struct telephony_call *t_call = temp_list->data;
1531
1532                 if (g_strcmp0(t_call->call_sender, sender) == 0) {
1533                         INFO("terminate call[%d]", t_call->call_id);
1534                         next_list = temp_list->next;
1535                         __bt_hfp_set_call_status(t_call, HFP_CALL_STATUS_MT_RELEASE);
1536                         temp_list = next_list;
1537                 } else
1538                         temp_list = temp_list->next;
1539         }
1540 }
1541
1542 void _bt_hfp_noise_red_and_echo_cancel_request(gboolean t_enable,
1543                         void *t_device)
1544 {
1545         if (_bt_hfp_agent_nrec_status(t_enable, t_device) == TRUE)
1546                 _bt_nr_and_ec_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1547         else
1548                 _bt_nr_and_ec_response(t_device, HFP_STATE_MNGR_ERR_AG_FAILURE);
1549
1550         return;
1551 }
1552
1553 void _bt_hfp_voice_dial_request(gboolean t_enable, void *t_device)
1554 {
1555         gboolean ret = FALSE;
1556         int call_state = 0;
1557
1558         if (vconf_get_int(VCONFKEY_CALL_STATE, &call_state) < 0)
1559                 ERR("vconf_get_int is failed");
1560
1561         if ((t_enable == TRUE && call_state == 0) || t_enable == FALSE)
1562                 ret = _bt_ag_agent_voice_dial(t_enable);
1563
1564         if (ret == TRUE)
1565                 _bt_voice_dial_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1566         else
1567                 _bt_voice_dial_response(t_device,
1568                                 HFP_STATE_MNGR_ERR_AG_FAILURE);
1569
1570         return;
1571 }
1572
1573 void _bt_hfp_set_indicators(const char *t_command, void *t_device)
1574 {
1575         const char delims = ',';
1576         char *str = NULL;
1577         int i = 0;
1578         if (t_command == NULL)
1579                 goto fail;
1580
1581         str = strchr(t_command, '=');
1582         while (hfp_ag_ind[i].indicator_desc != NULL && str != NULL) {
1583                 str++;
1584
1585                 if ((g_strcmp0(hfp_ag_ind[i].indicator_desc, "call") != 0) &&
1586                 (g_strcmp0(hfp_ag_ind[i].indicator_desc, "callheld") != 0) &&
1587                 (g_strcmp0(hfp_ag_ind[i].indicator_desc, "callsetup") != 0)) {
1588
1589                         if (*str == '0') {
1590                                 hfp_ag_ind[i].is_activated = FALSE;
1591                         } else if (*str == '1') {
1592                                 hfp_ag_ind[i].is_activated = TRUE;
1593                         } else {
1594                                 DBG(" no change in is_activated for[%s]\n",
1595                                 hfp_ag_ind[i].indicator_desc);
1596                         }
1597                 }
1598                 str = strchr(str, delims);
1599                 i++;
1600         }
1601
1602         _bt_indicators_activation_response(t_device, HFP_STATE_MNGR_ERR_NONE);
1603         return;
1604
1605 fail:
1606         _bt_indicators_activation_response(t_device,
1607                         HFP_STATE_MNGR_ERR_INVALID_CHAR_IN_STRING);
1608         return;
1609 }
1610
1611 static int __bt_hfp_get_phonebook_count(const char *path, uint32_t *max_size,
1612                                 uint32_t *used)
1613 {
1614 #ifndef TIZEN_PROFILE_WEARABLE
1615         GDBusConnection *g_conn;
1616         GDBusProxy *g_proxy;
1617         GError *err = NULL;
1618         GVariant *ret = NULL;
1619         uint32_t max = 0;
1620         uint32_t size = 0;
1621
1622         g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1623
1624         if (!g_conn) {
1625                 if (err) {
1626                         ERR("Unable to connect to gdbus: %s", err->message);
1627                         g_clear_error(&err);
1628                 }
1629                 return -1;
1630         }
1631
1632         g_proxy =  g_dbus_proxy_new_sync(g_conn,
1633                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1634                         PHONEBOOK_AGENT_BUS_NAME, PHONEBOOK_AGENT_PATH,
1635                         PHONEBOOK_AGENT_INTERFACE, NULL, &err);
1636
1637         if (!g_proxy) {
1638                 if (err) {
1639                         ERR("Unable to connect to gdbus: %s", err->message);
1640                         g_clear_error(&err);
1641                 }
1642                 return -1;
1643         }
1644
1645         ret = g_dbus_proxy_call_sync(g_proxy, "GetPhonebookSizeAt",
1646                         g_variant_new("(s)",
1647                         path),
1648                         G_DBUS_CALL_FLAGS_NONE, -1,
1649                         NULL, &err);
1650
1651         if (ret == NULL) {
1652                 ERR("dbus call failed");
1653                 if (err != NULL) {
1654                         ERR("D-Bus API failure: errCode[%x], message[%s]",
1655                                err->code, err->message);
1656
1657                         g_clear_error(&err);
1658                 }
1659         }
1660         if (ret != NULL) {
1661                 g_variant_get(ret, "(u)", &size);
1662                 g_variant_unref(ret);
1663         }
1664         DBG("Size returned %d", size);
1665         if ((g_strcmp0(path, "\"SM\"") == 0) ||
1666                         (g_strcmp0(path, "\"ME\"") == 0)) {
1667                 max = AGENT_MAX_PB_COUNT;
1668         } else if ((g_strcmp0(path, "\"DC\"") == 0) ||
1669                         (g_strcmp0(path, "\"MC\"") == 0) ||
1670                         (g_strcmp0(path, "\"RC\"") == 0)) {
1671                 max = AGENT_MAX_CALLLOG_COUNT;
1672         }
1673
1674         if (max_size)
1675                 *max_size = max;
1676         if (used) {
1677                 if (size > max)
1678                         *used = max;
1679                 else
1680                         *used = size;
1681         }
1682
1683         if (g_conn)
1684                 g_object_unref(g_conn);
1685         if (g_proxy)
1686                 g_object_unref(g_proxy);
1687 #endif
1688         return 0;
1689 }
1690
1691 void _bt_hfp_select_phonebook_memory_status(void *t_device)
1692 {
1693         int32_t path_id = ag_pb_info.path_id;
1694         uint32_t used = 0;
1695         uint32_t max_size = 0;
1696
1697         hfp_state_manager_err_t err = HFP_STATE_MNGR_ERR_NONE;
1698
1699         if (path_id < 0 || path_id >= AGENT_PB_STORE_LIST_SIZE)
1700                 path_id = 0;
1701
1702         if (__bt_hfp_get_phonebook_count(agent_pb_store_list[path_id],
1703                         &max_size, &used))
1704                 err = HFP_STATE_MNGR_ERR_AG_FAILURE;
1705
1706         _bt_select_phonebook_memory_status_response(t_device,
1707                         agent_pb_store_list[path_id],
1708                         max_size, used,
1709                         err);
1710 }
1711
1712 static char *__bt_hfp_get_supported_list(const char *char_list[],
1713                                         unsigned int size)
1714 {
1715         GString *strng;
1716
1717         int index = 0;
1718
1719         if (char_list == NULL || size == 0)
1720                 return NULL;
1721
1722         strng = g_string_new("(");
1723
1724         while (index < size) {
1725                 if (index > 0)
1726                         g_string_append(strng, ",");
1727
1728                 g_string_append(strng, char_list[index]);
1729                 index++;
1730         }
1731
1732         g_string_append(strng, ")");
1733
1734         return g_string_free(strng, FALSE);
1735 }
1736
1737 void _bt_hfp_select_phonebook_memory_list(void *t_device)
1738 {
1739         char *str;
1740
1741         str = __bt_hfp_get_supported_list(agent_pb_store_list,
1742                         AGENT_PB_STORE_LIST_SIZE);
1743
1744         _bt_select_phonebook_memory_list_response(t_device,
1745                         str, HFP_STATE_MNGR_ERR_NONE);
1746
1747         g_free(str);
1748 }
1749
1750 void _bt_hfp_select_phonebook_memory(void *t_device, const gchar *pb_path)
1751 {
1752         int i = 0;
1753         hfp_state_manager_err_t err;
1754
1755         while (i < AGENT_PB_STORE_LIST_SIZE) {
1756                 if (strcmp(agent_pb_store_list[i], pb_path) == 0)
1757                         break;
1758                 i++;
1759         }
1760
1761         if      (i >= 0 && i < AGENT_PB_STORE_LIST_SIZE) {
1762                 err = HFP_STATE_MNGR_ERR_NONE;
1763                 ag_pb_info.path_id = i;
1764         } else {
1765                 err = HFP_STATE_MNGR_ERR_INVALID_CHAR_IN_STRING;
1766         }
1767         _bt_select_phonebook_memory_response(t_device, err);
1768 }
1769
1770 void _bt_hfp_read_phonebook_entries_list(void *t_device)
1771 {
1772         hfp_state_manager_err_t err = HFP_STATE_MNGR_ERR_NONE;
1773
1774         int32_t path_id = ag_pb_info.path_id;
1775         uint32_t used = 0;
1776
1777         if (path_id < 0 || path_id >= AGENT_PB_STORE_LIST_SIZE)
1778                 err = HFP_STATE_MNGR_ERR_INVALID_INDEX;
1779         else {
1780                 if (__bt_hfp_get_phonebook_count(agent_pb_store_list[path_id],
1781                                         NULL, &used) != 0) {
1782                         err = HFP_STATE_MNGR_ERR_NOT_ALLOWED;
1783                 }
1784         }
1785
1786         _bt_read_phonebook_entries_list_response(t_device, used,
1787                 AGENT_PB_NUMBER_MAX_LENGTH, AGENT_PB_NAME_MAX_LENGTH,
1788                         err);
1789 }
1790
1791 static int __bt_hfp_get_phonebook_entries(int start_index, int end_index)
1792 {
1793 #ifdef TIZEN_PROFILE_WEARABLE
1794         int count = 0;
1795 #else
1796         GDBusConnection *g_conn;
1797         GDBusProxy *g_proxy;
1798         GError *err = NULL;
1799         GVariant *ret = NULL;
1800         int count = 0;
1801         GVariant *value = NULL;
1802         GVariant *tuple = NULL;
1803         const char *name = NULL;
1804         const char *number = NULL;
1805
1806         char *uni_name;
1807         char *uni_number;
1808
1809         uint32_t handle = 0;
1810
1811         g_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
1812
1813         if (!g_conn) {
1814                 if (err) {
1815                         ERR("Unable to connect to gdbus: %s", err->message);
1816                         g_clear_error(&err);
1817                 }
1818                 return -1;
1819         }
1820
1821         g_proxy =  g_dbus_proxy_new_sync(g_conn,
1822                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1823                         PHONEBOOK_AGENT_BUS_NAME, PHONEBOOK_AGENT_PATH,
1824                         PHONEBOOK_AGENT_INTERFACE, NULL, &err);
1825         if (!g_proxy) {
1826                 if (err) {
1827                         ERR("Unable to connect to gdbus: %s", err->message);
1828                         g_clear_error(&err);
1829                 }
1830                 return -1;
1831         }
1832
1833         ret = g_dbus_proxy_call_sync(g_proxy, "GetPhonebookEntriesAt",
1834                         g_variant_new("(sii)",
1835                         agent_pb_store_list[ag_pb_info.path_id],
1836                         start_index, end_index),
1837                         G_DBUS_CALL_FLAGS_NONE, -1,
1838                         NULL, &err);
1839
1840         if (ret == NULL) {
1841                 ERR("Dbus call failed");
1842                 if (err != NULL) {
1843                         ERR("D-Bus API failure: errCode[%x], message[%s]",
1844                                    err->code, err->message);
1845
1846                         g_clear_error(&err);
1847                 } else {
1848                         ERR("error returned was NULL");
1849                 }
1850                 return -1;
1851         } else {
1852                 tuple = g_variant_get_child_value(ret, 0);
1853                 if (tuple != NULL) {
1854                         int number_contacts = g_variant_n_children(tuple);
1855                         DBG("number of contacts %d", number_contacts);
1856                         while (count < number_contacts) {
1857                                 value = NULL;
1858
1859                                 value = g_variant_get_child_value(tuple, count);
1860
1861                                 g_variant_get(value, "(ssu)", &name, &number, &handle);
1862                                 count++;
1863                                 uni_name = g_strndup(name, AGENT_PB_NAME_MAX_LENGTH);
1864                                 uni_number = g_strndup(number, AGENT_PB_NAME_MAX_LENGTH);
1865
1866                                 _bt_read_phonebook_entries_indicator(uni_name,
1867                                                 uni_number, handle);
1868                                 g_variant_unref(value);
1869                                 g_free(uni_name);
1870                                 g_free(uni_number);
1871                                 g_free((gpointer)name);
1872                                 g_free((gpointer)number);
1873                         }
1874                         g_variant_unref(tuple);
1875                 }
1876         }
1877         g_variant_unref(ret);
1878         if (g_conn)
1879                 g_object_unref(g_conn);
1880         if (g_proxy)
1881                 g_object_unref(g_proxy);
1882 #endif
1883         return count;
1884 }
1885
1886 void _bt_hfp_read_phonebook_entries(void *t_device, const char *cmd)
1887 {
1888         int start_index = 0;
1889         int end_index = 0;
1890
1891         int count = 0;
1892
1893         char *str = NULL;
1894         char *next = NULL;
1895
1896         hfp_state_manager_err_t err;
1897
1898         if (cmd == NULL)
1899                 return;
1900
1901         str = g_strdup(cmd);
1902         next = strchr(str, ',');
1903
1904         if (next) {
1905                 *next = '\0';
1906                 next++;
1907
1908                 end_index = strtol(next, NULL, 10);
1909         }
1910
1911         start_index = strtol(str, NULL, 10);
1912
1913         g_free(str);
1914
1915         count = __bt_hfp_get_phonebook_entries(start_index, end_index);
1916
1917         if (count < 0)
1918                 err = HFP_STATE_MNGR_ERR_AG_FAILURE;
1919         else if (count == 0)
1920                 err = HFP_STATE_MNGR_ERR_INVALID_INDEX;
1921         else
1922                 err = HFP_STATE_MNGR_ERR_NONE;
1923
1924         _bt_read_phonebook_entries_response(t_device, err);
1925 }
1926
1927 void _bt_hfp_find_phonebook_entries_status(void *t_device)
1928 {
1929         _bt_find_phonebook_entries_status_indicator(
1930                         AGENT_PB_NUMBER_MAX_LENGTH,
1931                         AGENT_PB_NAME_MAX_LENGTH);
1932
1933         _bt_find_phonebook_entries_status_response(t_device,
1934                                                 HFP_STATE_MNGR_ERR_NONE);
1935 }
1936
1937 static int __bt_hfp_find_pb_entries(const char *str)
1938 {
1939         return 0;
1940 }
1941
1942 void _bt_hfp_find_phonebook_entries(void *t_device, const char *cmd)
1943 {
1944         gchar *st = NULL;
1945         gchar *unquoted = NULL;
1946
1947         hfp_state_manager_err_t err = HFP_STATE_MNGR_ERR_NONE;
1948
1949         /* remove quote and compress */
1950         st = strchr(cmd, '"');
1951         if (st == NULL)
1952                 unquoted = g_strdup(cmd);
1953         else {
1954                 gchar *end = NULL;
1955
1956                 end = strrchr(cmd, '"');
1957                 if (end == NULL)
1958                         unquoted = g_strdup(cmd);
1959                 else
1960                         unquoted = g_strndup(st + 1, end - st - 1);
1961         }
1962
1963         if (__bt_hfp_find_pb_entries(unquoted))
1964                 err = HFP_STATE_MNGR_ERR_AG_FAILURE;
1965
1966         _bt_find_phonebook_entries_response(t_device, err);
1967
1968         g_free(unquoted);
1969 }
1970
1971 void _bt_hfp_get_character_set(void *t_device)
1972 {
1973         _bt_supported_character_generic_response(t_device,
1974                 (char *)agent_supported_character_set[ag_pb_info.charset_id],
1975                 HFP_STATE_MNGR_ERR_NONE);
1976 }
1977
1978 void _bt_hfp_list_supported_character(void *t_device)
1979 {
1980         char *str;
1981
1982         str = __bt_hfp_get_supported_list(agent_supported_character_set,
1983                         AGENT_SUPPORTED_CHARACTER_SET_SIZE);
1984
1985         _bt_supported_character_generic_response(t_device,
1986                         str, HFP_STATE_MNGR_ERR_NONE);
1987
1988         g_free(str);
1989 }
1990
1991 void _bt_hfp_set_character_set(void *t_device, const char *cmd)
1992 {
1993         int index = 0;
1994
1995         while (index < AGENT_SUPPORTED_CHARACTER_SET_SIZE) {
1996                 if (strcmp(agent_supported_character_set[index], cmd) == 0) {
1997                         _bt_set_characterset_generic_response(t_device,
1998                                         HFP_STATE_MNGR_ERR_NONE);
1999
2000                         ag_pb_info.charset_id = index;
2001                         return;
2002                 }
2003                 index++;
2004         }
2005
2006         _bt_set_characterset_generic_response(t_device,
2007                         HFP_STATE_MNGR_ERR_NOT_SUPPORTED);
2008         return;
2009 }
2010
2011 void _bt_hfp_signal_quality_reply(int32_t rssi, int32_t ber,
2012         void *t_device)
2013 {
2014         DBG("signal_quality_reply");
2015
2016         if (rssi == -1 && ber == -1) {
2017                 _bt_signal_quality_response(t_device, rssi, ber,
2018                 HFP_STATE_MNGR_ERR_AG_FAILURE);
2019         } else {
2020                 _bt_signal_quality_response(t_device, rssi, ber,
2021                 HFP_STATE_MNGR_ERR_NONE);
2022         }
2023 }
2024
2025 void _bt_hfp_battery_property_reply(void *data, int32_t bcs,
2026                         int32_t bcl)
2027 {
2028         if (bcs == -1 || bcl == -1) {
2029                 _bt_battery_charge_status_response(data, bcs,
2030                         bcl, HFP_STATE_MNGR_ERR_AG_FAILURE);
2031         } else {
2032                 _bt_battery_charge_status_response(data, bcs,
2033                         bcl, HFP_STATE_MNGR_ERR_NONE);
2034         }
2035
2036         return;
2037 }
2038
2039 void _bt_hfp_get_battery_property(void *t_device)
2040 {
2041         _bt_ag_agent_get_battery_status(t_device);
2042 }
2043
2044 void _bt_hfp_operator_reply(char *operator_name,  void *t_device)
2045 {
2046         if (operator_name == NULL)
2047                 goto failed;
2048
2049         network_info.network_operator_name = g_strndup(operator_name, 16);
2050
2051         _bt_operator_selection_indicator(AGENT_OPERATOR_MODE_AUTO,
2052                                 operator_name);
2053         _bt_operator_selection_response(t_device, HFP_STATE_MNGR_ERR_NONE);
2054         return;
2055
2056 failed:
2057         _bt_operator_selection_indicator(AGENT_OPERATOR_MODE_AUTO, "UNKNOWN");
2058         _bt_operator_selection_response(t_device,
2059                                 HFP_STATE_MNGR_ERR_AG_FAILURE);
2060 }
2061
2062 void _bt_hfp_get_operator_selection_request(void *t_device)
2063 {
2064         _bt_ag_agent_get_operator_name(t_device);
2065 }
2066
2067 void _bt_hfp_response_and_hold_request(void *t_device)
2068 {
2069         _bt_response_and_hold_response(t_device,
2070                         HFP_STATE_MNGR_ERR_NOT_SUPPORTED);
2071 }
2072
2073 void _bt_get_activity_status(void *t_device)
2074 {
2075         DBG("telephony-tizen: telephony_get_activity_status");
2076
2077         if (NULL != (__bt_hfp_get_call_with_status(
2078                                 HFP_CALL_STATUS_MT_ALERTING)) ||
2079                 NULL != (__bt_hfp_get_call_with_status(
2080                                 HFP_CALL_STATUS_MO_ALERTING)) ||
2081                 NULL != (__bt_hfp_get_call_with_status(
2082                                 HFP_CALL_STATUS_COMING)) ||
2083                 NULL != (__bt_hfp_get_call_with_status(
2084                                 HFP_CALL_STATUS_CREATE)))
2085                 _bt_hfp_get_activity_status_rsp(t_device,
2086                                 HFP_AGENT_ACTIVITY_STATUS_RINGING,
2087                                 HFP_STATE_MNGR_ERR_NONE);
2088         else if (NULL != (__bt_hfp_get_call_with_status(
2089                                         HFP_CALL_STATUS_WAITING)) ||
2090                 NULL != (__bt_hfp_get_call_with_status(
2091                                                 HFP_CALL_STATUS_ACTIVE)))
2092                 _bt_hfp_get_activity_status_rsp(t_device,
2093                                 HFP_AGENT_ACTIVITY_STATUS_CALL_IN_PROGRESS,
2094                                 HFP_STATE_MNGR_ERR_NONE);
2095         else
2096                 _bt_hfp_get_activity_status_rsp(t_device,
2097                                 HFP_AGENT_ACTIVITY_STATUS_READY,
2098                                 HFP_STATE_MNGR_ERR_NONE);
2099 }
2100
2101 void _bt_hfp_get_imei_number_reply(char *imei_number,  void *t_device)
2102 {
2103         _bt_hfp_get_equipment_identity_rsp(t_device, imei_number,
2104                                         HFP_STATE_MNGR_ERR_NONE);
2105 }
2106
2107 void _bt_hfp_get_imsi_reply(char *mcc, char *mnc, char *msin, void *t_device)
2108 {
2109         if (mcc != NULL && mnc != NULL && msin != NULL)
2110                 _bt_hfp_get_imsi_rsp(t_device, mcc, mnc, msin,
2111                                 HFP_STATE_MNGR_ERR_NONE);
2112         else
2113                 _bt_hfp_get_imsi_rsp(t_device, NULL, NULL, NULL,
2114                                 HFP_STATE_MNGR_ERR_NOT_ALLOWED);
2115 }
2116
2117 void _bt_hfp_get_creg_status_reply(int n, int status, void *t_device)
2118 {
2119         _bt_hfp_get_creg_status_rsp(t_device, n, status,
2120                                 HFP_STATE_MNGR_ERR_NONE);
2121 }
2122
2123 void _bt_hfp_get_equipment_identity_req(void *t_device)
2124 {
2125         _bt_ag_agent_get_imei_number(t_device);
2126 }
2127
2128 void _bt_hfp_get_model_info_reply(char *model,  void *t_device)
2129 {
2130         _bt_hfp_get_model_info_rsp(t_device, model,
2131                                         HFP_STATE_MNGR_ERR_NONE);
2132 }
2133
2134 void _bt_hfp_get_model_info_req(void *t_device)
2135 {
2136         _bt_ag_agent_get_model_name(t_device);
2137 }
2138
2139 void _bt_hfp_get_device_manufacturer_reply(char *manufacturer,  void *t_device)
2140 {
2141         _bt_hfp_get_device_manufacturer_rsp(t_device, manufacturer,
2142                                         HFP_STATE_MNGR_ERR_NONE);
2143 }
2144
2145 void _bt_hfp_get_device_manufacturer_req(void *t_device)
2146 {
2147         _bt_ag_agent_get_manufacturer_name(t_device);
2148 }
2149
2150 void _bt_hfp_get_imsi_req(void *t_device)
2151 {
2152         _bt_ag_agent_get_imsi(t_device);
2153 }
2154
2155 void _bt_hfp_get_creg_status_req(void *t_device)
2156 {
2157         _bt_ag_agent_get_creg_status(t_device);
2158 }
2159
2160 void _bt_hfp_get_revision_info_reply(char *revision,  void *t_device)
2161 {
2162         _bt_hfp_get_revision_info_rsp(t_device, revision,
2163                                         HFP_STATE_MNGR_ERR_NONE);
2164 }
2165
2166 void _bt_hfp_get_revision_info_req(void *t_device)
2167 {
2168         _bt_ag_agent_get_revision_information(t_device);
2169 }
2170