Fix coverity issue 1149129
[apps/native/volume-app.git] / src / bt.c
1 /*
2  * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <appcore-common.h>
19 #include <vconf.h>
20 #include <vconf-keys.h>
21 #include <feedback.h>
22 #include <bluetooth.h>
23 #include <bluetooth_internal.h>
24 #include <bluetooth_extension.h>
25
26 #include "main.h"
27 #include "_util_log.h"
28 #include "view.h"
29 #include "control.h"
30 #include "sound.h"
31 #include "timer.h"
32
33 static void _bt_display_bt_volume_view(sound_type_e sound_type, int sound, int vibration, bool bt_opened);
34 static void _bt_volume_changed_cb(int volume, void *user_data);
35 static void _bt_state_changed_cb(int result, bool opened, void *user_data);
36 static int _bt_register_changed_cb(void);
37 static int _bt_unregister_changed_cb(void);
38
39 int bt_get_bt_volume(void)
40 {
41         int ret = BT_ERROR_NONE;
42         int bt_vol = 0;
43
44         ret = bt_ag_get_speaker_gain(&bt_vol);
45
46         if (ret != BT_ERROR_NONE) {
47                 _E("bt_ag_get_speaker_gain Failed");
48                 return -1;
49         }
50         _D("bt vol: %d", bt_vol);
51         return bt_vol;
52 }
53
54 void bt_init_sco(void)
55 {
56         _D("SCO volume initialize");
57         if (BT_ERROR_NONE != bt_initialize())
58                 _E("BT initialize failed");
59
60         if (_bt_register_changed_cb() != BT_ERROR_NONE)
61                 _E("volume bt register changed cb failed");
62 }
63
64 void bt_fini_sco(void)
65 {
66         _D("SCO volume Deinitialize");
67         if (_bt_unregister_changed_cb() != BT_ERROR_NONE)
68                 _E("volume bt Unregister changed cb failed");
69
70         if (BT_ERROR_NONE != bt_deinitialize())
71                 _E("BT Deinitialize failed");
72 }
73
74 static void _bt_display_bt_volume_view(sound_type_e sound_type, int sound, int vibration, bool bt_opened)
75 {
76         if (VOLUME_ERROR_OK != volume_view_window_show(sound_type))
77                 _E("Failed to show volume window");
78
79         volume_view_volume_icon_set(sound_type, sound, vibration, bt_opened);
80 }
81
82 static void _bt_volume_changed_cb(int volume, void *user_data)
83 {
84         bool bt_opened = false;
85         int error = 0;
86         int status = 0;
87         int sound = 0;
88         int lock = 1;
89         int vibration = 0;
90         sound_type_e sound_type = 0;
91         _D("BT VOLUME is changed");
92
93         status = volume_control_check_status(&lock, &sound_type);
94         _D("status: %d, lock: %d, sound type : %d", status, lock, sound_type);
95
96         sound = volume_sound_vconf_status_get(TYPE_VCONF_SOUND_STATUS);
97         _D("sound status : %d", sound);
98
99         error = bt_ag_is_sco_opened(&bt_opened);
100         if (error != BT_ERROR_NONE)
101                 _E("bt_ag_is_sco_opened return [%d]", error);
102         _D("BT state %d", bt_opened);
103
104         if (bt_opened == true && sound_type == SOUND_TYPE_CALL) {
105                 _bt_display_bt_volume_view(sound_type, sound, vibration, bt_opened);
106                 _D("BT SCO volume level is : %d", volume);
107                 if (VOLUME_ERROR_OK != volume_view_slider_value_set(volume))
108                         _E("Failed to set slider value");
109
110                 volume_timer_add(3.0, TYPE_TIMER_BT);
111         }
112 }
113
114 static void _bt_state_changed_cb(int result, bool opened, void *user_data)
115 {
116         _D("SCO opened [%s]", opened ? "YES" : "NO");
117 }
118
119 static int _bt_register_changed_cb(void)
120 {
121         int ret = 0;
122
123         ret = bt_audio_initialize();
124         if (ret != BT_ERROR_NONE) {
125                 _E("bt audio initialize failed");
126                 return -1;
127         }
128
129         ret = bt_ag_set_speaker_gain_changed_cb(_bt_volume_changed_cb, NULL);
130         if (ret != BT_ERROR_NONE) {
131                 _E("register bt volume changed callback failed");
132                 return -1;
133         }
134
135         ret = bt_ag_set_sco_state_changed_cb(_bt_state_changed_cb, NULL);
136         if (ret != BT_ERROR_NONE)
137                 _E("register bt state changed callback failed");
138
139         return 0;
140 }
141
142 static int _bt_unregister_changed_cb(void)
143 {
144         int ret = 0;
145
146         ret = bt_audio_deinitialize();
147         if (ret != BT_ERROR_NONE) {
148                 _E("bt audio initialize failed");
149                 return -1;
150         }
151
152         ret = bt_ag_unset_speaker_gain_changed_cb();
153         if (ret != BT_ERROR_NONE) {
154                 _E("register bt volume changed callback failed");
155                 return -1;
156         }
157
158         ret = bt_ag_unset_sco_state_changed_cb();
159         if (ret != BT_ERROR_NONE)
160                 _E("register bt state changed callback failed");
161
162         return 0;
163 }
164