Implement the event handling logic for AVC
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / audio / bt-service-absolute-volume.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Dohyun Pyun <dh79.pyun@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <vconf.h>
23 #include <vconf-internal-keys.h>
24
25 #include <oal-event.h>
26 #include <oal-hardware.h>
27 #include <oal-audio-src.h>
28
29 #include "bt-service-common.h"
30 #include "bt-service-audio-common.h"
31 #include "bt-service-event.h"
32 #include "bt-service-avrcp-tg.h"
33
34 /* We will remove this flag after stabilizing the functionality */
35 #define AVC_DEBUG 1
36
37 /* Avoid the build error related to vconf.h's dependency */
38 #ifndef VCONFKEY_BT_AVC_MODE
39 #define VCONFKEY_BT_AVC_MODE "db/bluetooth/avc_mode"
40 #endif
41
42 #ifndef VCONFKEY_BT_AVC_OFF
43 #define VCONFKEY_BT_AVC_OFF false
44 #endif
45
46 #define ABSOLUTE_VOLUME_MAX 150
47 #define BT_VOLUME_MAX 127
48
49 static unsigned int absolute_volume = ABSOLUTE_VOLUME_MAX + 1;
50 static unsigned int bt_volume = BT_VOLUME_MAX + 1;
51 static unsigned int avc_mode = BT_AVC_OFF;
52
53 static void __bt_audio_covert_system_to_bt_volume(unsigned int sys_vol, unsigned int *bt_vol)
54 {
55         /* Convert system gain to BT
56                 BT volume range : 0 ~ 127
57                 system volume range : 0 ~ 150 */
58
59         *bt_vol = 127 * ((float)sys_vol / 150);
60
61         /* volume table
62                 system volume : 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
63                 avrcp  volume : 0  9 17 26 34 43 51 60 68 77  85  94 102 111 119 127 */
64
65         if (*bt_vol > 0 && *bt_vol < 127)
66                 *bt_vol = *bt_vol + 1;
67
68 #ifdef AVC_DEBUG
69         BT_DBG("System volume [%d], BT volume [%d]", sys_vol, *bt_vol);
70 #endif
71 }
72
73 static void __bt_audio_covert_bt_to_system_volume(unsigned int bt_vol, unsigned int *sys_vol)
74 {
75         /* Convert BT gain to system
76                 BT volume range : 0 ~ 127
77                 system volume range : 0 ~ 150 */
78
79         *sys_vol = 150 * ((float)bt_vol / 127);
80
81         /* volume table
82                 system volume : 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
83                 avrcp  volume : 0  9 17 26 34 43 51 60 68 77  85  94 102 111 119 127 */
84
85         if (*sys_vol > 0 && *sys_vol < 150)
86                 *sys_vol = *sys_vol + 1;
87
88 #ifdef AVC_DEBUG
89         BT_DBG("System volume [%d], BT volume [%d]", *sys_vol, bt_vol);
90 #endif
91 }
92
93 static int __bt_audio_get_active_headset_volume(void)
94 {
95         int result = BLUETOOTH_ERROR_NONE;
96         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
97         bluetooth_device_address_t device_address;
98         gboolean connected = FALSE;
99         unsigned int volume = BT_VOLUME_MAX + 1;
100
101 #ifdef AVC_DEBUG
102         BT_DBG("+");
103 #endif
104
105         /* 1. Get active A2DP headset path */
106
107         /* We should modify this function to get the active headset in later */
108         connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP, connected_address);
109
110         if (connected == FALSE) {
111                 BT_DBG("There is no active A2DP headset");
112                 return -1;
113         }
114
115         _bt_convert_addr_string_to_type(device_address.addr, connected_address);
116
117         /* 2. Get volume info for active transport for the path */
118         result = _bt_avrcp_target_get_volume(&device_address, &volume);
119         if (result != BLUETOOTH_ERROR_NONE) {
120                 BT_ERR("Fail to notify BT volume to headset");
121                 return -1;
122         }
123
124         if (volume > BT_VOLUME_MAX) {
125                 BT_DBG("Absolute Volume is not set in transport");
126                 return -1;
127         }
128
129         return volume;
130 }
131
132 /* Emit AVC mode changed signal (AVC_OFF/AVC_NULL/AVC_MAX) to application (Pulseaudio) */
133 static void __bt_audio_notify_avc_mode_changed(unsigned int avc_mode)
134 {
135         GVariant *param = g_variant_new("(u)", avc_mode);
136
137         /* AVC Event */
138         _bt_send_event(BT_AUDIO_AVC_EVENT,
139                         BLUETOOTH_EVENT_AUDIO_AVC_MODE_CHANGED,
140                         param);
141 }
142
143 /* Emit On / off state changed signal to application (Setting) */
144 static void __bt_audio_notify_avc_onoff_changed(bool status)
145 {
146         GVariant *param = g_variant_new("(b)", status);
147
148         /* HEADSET Event */
149         _bt_send_event(BT_HEADSET_EVENT,
150                         BLUETOOTH_EVENT_AUDIO_AVC_MODE_CHANGED,
151                         param);
152 }
153
154 static void __bt_audio_set_avc_mode(unsigned int mode)
155 {
156         unsigned int prev_mode = avc_mode;
157         bool onoff_state = false;
158
159         if (prev_mode == mode) {
160                 BT_DBG("No avc mode change");
161                 return;
162         }
163
164         BT_DBG("Previous mode [%d], New mode [%d]", prev_mode, mode);
165
166         /*  Emit AVC mode changed signal (AVC_OFF/AVC_NULL/AVC_MAX) to Pulseaudio  */
167         __bt_audio_notify_avc_mode_changed(mode);
168
169         avc_mode = mode;
170
171         switch (mode) {
172         case BT_AVC_OFF:
173                 onoff_state = false;
174                 break;
175         case BT_AVC_NULL:
176         case BT_AVC_MAX:
177                 if (prev_mode != BT_AVC_OFF) {
178                         BT_DBG("Don't need to send onff state changed");
179                         return;
180                 }
181
182                 onoff_state = true;
183                 break;
184         default:
185                 BT_ERR("Abnormal state!");
186                 return;
187                 break;
188         }
189
190         /* Emit On / off state changed signal to application (Setting) */
191         __bt_audio_notify_avc_onoff_changed(onoff_state);
192 }
193
194 static void __bt_audio_sync_absolute_volume(unsigned int bt_vol)
195 {
196         unsigned int sys_vol = 0;
197
198         BT_DBG("synchronize the system and bt volume");
199
200         __bt_audio_covert_bt_to_system_volume(bt_vol, &sys_vol);
201
202         bt_volume = bt_vol;
203         absolute_volume = sys_vol;
204 }
205
206 static void __bt_audio_set_user_avc_value(bool mode)
207 {
208         int volume = 0;
209
210         BT_DBG("mode: %d", mode);
211
212         if (mode == false) {
213                 __bt_audio_set_avc_mode(BT_AVC_OFF);
214                 absolute_volume = ABSOLUTE_VOLUME_MAX + 1;
215                 bt_volume = BT_VOLUME_MAX + 1;
216                 return;
217         }
218
219         volume = __bt_audio_get_active_headset_volume();
220
221         if (volume == -1) {
222                 __bt_audio_set_avc_mode(BT_AVC_OFF);
223                 return;
224         }
225
226         BT_INFO("The current transport supports AVC : volume [%d]", volume);
227
228         if (volume == 0) {
229                 __bt_audio_set_avc_mode(BT_AVC_NULL);
230         } else {
231                 BT_DBG("BT volume: %d", volume);
232                 __bt_audio_set_avc_mode(BT_AVC_MAX);
233         }
234
235         __bt_audio_sync_absolute_volume(volume);
236 }
237
238 static void __bt_audio_user_avc_mode_cb(keynode_t *node, void *data)
239 {
240         int type;
241         bool mode = VCONFKEY_BT_AVC_OFF;
242
243         DBG_SECURE("key=%s", vconf_keynode_get_name(node));
244         type = vconf_keynode_get_type(node);
245
246         if (type == VCONF_TYPE_BOOL) {
247                 mode = vconf_keynode_get_bool(node);
248                 __bt_audio_set_user_avc_value(mode);
249         } else {
250                 BT_ERR("Invaild vconf key type : %d", type);
251         }
252 }
253
254 int _bt_audio_init_absolute_volume_control(void)
255 {
256 #ifdef AVC_DEBUG
257         BT_DBG("+");
258 #endif
259
260         absolute_volume = ABSOLUTE_VOLUME_MAX + 1;
261         bt_volume = BT_VOLUME_MAX + 1;
262         avc_mode = BT_AVC_OFF;
263
264         if (vconf_notify_key_changed(VCONFKEY_BT_AVC_MODE,
265                 (vconf_callback_fn)__bt_audio_user_avc_mode_cb, NULL) < 0) {
266                         BT_ERR("Unable to register key handler");
267                         return BLUETOOTH_ERROR_INTERNAL;
268         }
269
270         return BLUETOOTH_ERROR_NONE;
271 }
272
273 void _bt_audio_deinit_absolute_volume_control(void)
274 {
275 #ifdef AVC_DEBUG
276         BT_DBG("+");
277 #endif
278
279         absolute_volume = ABSOLUTE_VOLUME_MAX + 1;
280         bt_volume = BT_VOLUME_MAX + 1;
281         avc_mode = BT_AVC_OFF;
282
283         vconf_ignore_key_changed(VCONFKEY_BT_AVC_MODE,
284                         (vconf_callback_fn)__bt_audio_user_avc_mode_cb);
285 }
286
287 int _bt_audio_set_absolute_volume(unsigned int volume)
288 {
289         int result = BLUETOOTH_ERROR_NONE;
290         unsigned int bt_vol = 0;
291         char connected_address[BT_ADDRESS_STRING_SIZE + 1];
292         bluetooth_device_address_t device_address;
293         gboolean connected = FALSE;
294
295         BT_DBG("volume: %d", volume);
296
297         if (volume > 150) {
298                 BT_ERR("The volume exceeded 150");
299                 return BLUETOOTH_ERROR_INVALID_PARAM;
300         }
301
302         if (avc_mode == BT_AVC_OFF) {
303                 BT_ERR("AVC mode is off");
304                 return BLUETOOTH_ERROR_INTERNAL;
305         }
306
307         /* We should modify this function to get the active headset in later */
308         connected = _bt_is_headset_type_connected(BT_AUDIO_A2DP, connected_address);
309
310         if (connected == FALSE) {
311                 BT_DBG("There is no active A2DP headset");
312                 return BLUETOOTH_ERROR_NOT_CONNECTED;
313         }
314
315         _bt_convert_addr_string_to_type(device_address.addr, connected_address);
316
317         /* 1. Translate the absolute volume to bt volume */
318         __bt_audio_covert_system_to_bt_volume(volume, &bt_vol);
319
320         bt_volume = bt_vol;
321         absolute_volume = volume;
322
323         /* 2. Notify the bt_volume to transport (BT Headset) */
324         result = _bt_avrcp_target_notify_volume(&device_address, bt_volume);
325         if (result != BLUETOOTH_ERROR_NONE)
326                 BT_ERR("Fail to notify BT volume to headset");
327
328         /* 3. Notify the avc mode to the pulseaudio if it is needed */
329         if (volume == 0) {
330                 __bt_audio_set_avc_mode(BT_AVC_NULL);
331         } else {
332                 BT_DBG("System volume: %d", volume);
333                 __bt_audio_set_avc_mode(BT_AVC_MAX);
334         }
335
336         return result;
337 }
338
339 /* Just return the absolute_volume value */
340 int _bt_audio_get_absolute_volume(unsigned int *volume)
341 {
342         if (avc_mode == BT_AVC_OFF) {
343                 BT_ERR("AVC mode is off");
344                 return BLUETOOTH_ERROR_INTERNAL;
345         }
346
347         if (absolute_volume > ABSOLUTE_VOLUME_MAX) {
348                 BT_ERR("Absolute Volume level is not set");
349                 return BLUETOOTH_ERROR_INTERNAL;
350         }
351
352         *volume = absolute_volume;
353
354 #ifdef AVC_DEBUG
355         BT_DBG("volume: %d", *volume);
356 #endif
357
358         return BLUETOOTH_ERROR_NONE;
359 }
360
361 int _bt_audio_is_avc_activated(bool *activated)
362 {
363         unsigned int mode = BT_AVC_OFF;
364
365         if (_bt_audio_get_avc_mode(&mode) != BLUETOOTH_ERROR_NONE)
366                 BT_ERR("Fail to get the avc mode");
367
368         *activated = (mode == BT_AVC_OFF) ? false : true;
369
370 #ifdef AVC_DEBUG
371         BT_DBG("avc_mode: %d", avc_mode);
372 #endif
373
374         return BLUETOOTH_ERROR_NONE;
375 }
376
377 int _bt_audio_get_avc_mode(unsigned int *mode)
378 {
379         int state = 0;
380         int volume = 0;
381
382 #ifdef AVC_DEBUG
383         BT_DBG("+");
384 #endif
385
386         /* 1. Get AVC mode in the vconf value */
387         if (vconf_get_bool(VCONFKEY_BT_AVC_MODE, &state) != 0) {
388                 BT_ERR("vconf_get_bool failed");
389                 *mode = BT_AVC_OFF;
390                 return BLUETOOTH_ERROR_INTERNAL;
391         }
392
393         if (state == VCONFKEY_BT_AVC_OFF) {
394                 *mode = BT_AVC_OFF;
395                 return BLUETOOTH_ERROR_NONE;
396         }
397
398         volume = __bt_audio_get_active_headset_volume();
399
400         if (volume == -1) {
401                 *mode = BT_AVC_OFF;
402                 return BLUETOOTH_ERROR_NONE;
403         }
404
405         BT_INFO("The current transport supports AVC : volume [%d]", volume);
406
407         if (volume == 0) {
408                 *mode = BT_AVC_NULL;
409         } else {
410                 BT_DBG("BT volume: %d", volume);
411                 *mode = BT_AVC_MAX;
412         }
413
414         __bt_audio_sync_absolute_volume(volume);
415
416         return BLUETOOTH_ERROR_NONE;
417 }
418
419 void _bt_audio_handle_adapter_disabled(void)
420 {
421         BT_DBG("Reset AVC mode, and volume");
422
423         absolute_volume = ABSOLUTE_VOLUME_MAX + 1;
424         bt_volume = BT_VOLUME_MAX + 1;
425         __bt_audio_set_avc_mode(BT_AVC_OFF);
426         return;
427 }
428
429 void _bt_audio_handle_a2dp_state_changed(const char *address, bool connected)
430 {
431         unsigned int mode = 0;
432
433 #ifdef AVC_DEBUG
434         BT_DBG("+");
435 #endif
436
437         /* In later, we can check the event for the active headset or not in here */
438
439         if (connected == false) {
440                 BT_DBG("Reset AVC mode, and volume");
441
442                 absolute_volume = ABSOLUTE_VOLUME_MAX + 1;
443                 bt_volume = BT_VOLUME_MAX + 1;
444                 __bt_audio_set_avc_mode(BT_AVC_OFF);
445                 return;
446         }
447
448         if (_bt_audio_get_avc_mode(&mode) != BLUETOOTH_ERROR_NONE) {
449                 BT_ERR("Fail to get the avc mode");
450                 return;
451         }
452
453         if (mode != BT_AVC_OFF) {
454                 __bt_audio_set_avc_mode(mode);
455
456                 /* Send the initial volume to application */
457                 _bt_send_event(BT_AVRCP_EVENT,
458                         BLUETOOTH_EVENT_AVRCP_VOLUME_CHANGED,
459                         g_variant_new("(u)", absolute_volume));
460         }
461 }
462
463 void _bt_audio_handle_transport_volume_changed(unsigned int bt_vol)
464 {
465         unsigned int sys_vol = 0;
466         int mode = BT_AVC_OFF;
467         unsigned int prev_vol = absolute_volume;
468
469 #ifdef AVC_DEBUG
470         BT_DBG("+");
471 #endif
472
473         if (bt_vol > BT_VOLUME_MAX) {
474                 /* BT AVC mode off, because bluez transport initiate the volue as MAX */
475                 absolute_volume = ABSOLUTE_VOLUME_MAX + 1;
476                 bt_volume = BT_VOLUME_MAX + 1;
477                 __bt_audio_set_avc_mode(BT_AVC_OFF);
478                 return;
479         }
480
481         __bt_audio_covert_bt_to_system_volume(bt_vol, &sys_vol);
482
483         bt_volume = bt_vol;
484         absolute_volume = sys_vol;
485
486         mode = (absolute_volume == 0) ? BT_AVC_NULL : BT_AVC_MAX;
487
488         __bt_audio_set_avc_mode(mode);
489
490         if (prev_vol == absolute_volume) {
491                 BT_DBG("Same volume level");
492                 return;
493         }
494
495         _bt_send_event(BT_AVRCP_EVENT,
496                 BLUETOOTH_EVENT_AVRCP_VOLUME_CHANGED,
497                 g_variant_new("(u)", absolute_volume));
498 }
499