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