Add interfaces for BT AVC feature with pulseaudio
[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
24 #include <oal-event.h>
25 #include <oal-hardware.h>
26 #include <oal-audio-src.h>
27
28 #include "bt-service-common.h"
29 #include "bt-service-audio-common.h"
30 #include "bt-service-event.h"
31
32 static unsigned int absolute_volume = 0;
33 static unsigned int bt_volume = 0;
34 static unsigned int avc_mode = BT_AVC_OFF;
35
36 int _bt_audio_set_absolute_volume(unsigned int volume)
37 {
38         int result = BLUETOOTH_ERROR_NONE;
39
40         /* 1. Translate the absolute volume to bt volume */
41
42         /* Convert system gain to BT
43                 BT volume range : 0 ~ 127
44                 system volume range : 0 ~ 150 */
45
46         bt_volume = 127 * volume / 150;
47
48         /* volume table
49                 system volume : 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150
50                 avrcp_ volume : 0  9 17 26 34 43 51 60 68 77  85  94 102 111 119 127 */
51
52         if (bt_volume > 0 && bt_volume < 127)
53                 bt_volume++;
54
55         /* 2. Notify the bt_volume to transport (BT Headset) */
56
57         /* 3. Notify the avc mode to the pulseaudio if it is needed */
58
59         return result;
60 }
61
62 /* Just return the absolute_volume value */
63 int _bt_audio_get_absolute_volume(unsigned int *volume)
64 {
65         int result = BLUETOOTH_ERROR_NONE;
66
67         *volume = absolute_volume;
68
69         return result;
70 }
71
72 int _bt_audio_is_avc_activated(bool *activated)
73 {
74         *activated = (avc_mode == BT_AVC_OFF) ? false : true;
75
76         return BLUETOOTH_ERROR_NONE;
77 }
78
79 int _bt_audio_get_avc_mode(unsigned int *mode)
80 {
81         /* 1. AVC mode in the vconf value */
82
83         /* 2. Get active A2DP headset path */
84
85         /* 3. Get volume info for activae transport for the path */
86
87         *mode = avc_mode;
88
89         return BLUETOOTH_ERROR_NONE;
90 }
91
92 int _bt_audio_notify_avc_mode_changed(unsigned int avc_mode)
93 {
94         GVariant *param = g_variant_new("(u)", avc_mode);
95
96         _bt_send_event(BT_AUDIO_AVC_EVENT,
97                         BLUETOOTH_EVENT_AUDIO_AVC_MODE_CHANGED,
98                         param);
99
100         return BLUETOOTH_ERROR_NONE;
101 }
102
103 /* 1. When a2dp headset is connected, we should set 'AVC' mode into pulseaudio */
104 /* 2. If the avc mode is changed, we should inform 'AVC' mode to pulseaudio */
105 /* 3. If BT is off, we should send 'AVC' off mode event for application */
106 /* 4. If BT A2DP is disconnected, we should send 'AVC' off mode event for application */
107