Merge "Invoke HAL_DISCOVERY_STATE_STOPPED event once" into tizen
[platform/core/connectivity/bluetooth-frwk.git] / bt-oal / bluez_hal / src / bt-hal-avrcp-transport-dbus-handler.c
1 /*
2  *
3  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
4  *
5  * Contact: Nilesh Trimbake <t.shripati@samsung.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *              http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <string.h>
25
26 #include <glib.h>
27 #include <gio/gio.h>
28 #include <dlog.h>
29 #include <vconf.h>
30
31 #include "bt-hal-avrcp-transport-dbus-handler.h"
32 #include "bt-hal-dbus-common-utils.h"
33 #include "bt-hal-internal.h"
34
35 static const char *__bt_transport_type_to_str(int type)
36 {
37         switch (type) {
38         case BTRC_TRANSPORT_ATTR_DELAY:
39                 return "Delay";
40         case BTRC_TRANSPORT_ATTR_VOLUME:
41                 return "Volume";
42         default:
43                 return NULL;
44         }
45
46         return NULL;
47 }
48
49 bt_status_t _bt_hal_dbus_handler_avrcp_transport_get_property(bt_bdaddr_t *bd_addr, int type, unsigned int *value)
50 {
51         GDBusProxy *proxy = NULL;
52         GError *err = NULL;
53         GVariant *reply = NULL;
54         GVariant *temp = NULL;
55
56         proxy = _bt_hal_get_avrcp_transport_properties_proxy(bd_addr);
57         if (proxy == NULL) {
58                 ERR("Unable to get proxy \n");
59                 return BT_STATUS_FAIL;
60         }
61
62         reply = g_dbus_proxy_call_sync(proxy,
63                                         "Get", g_variant_new("(ss)", BT_HAL_MEDIATRANSPORT_INTERFACE,
64                                         __bt_transport_type_to_str(type)),
65                                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
66
67         g_object_unref(proxy);
68
69         if (!reply) {
70                 ERR("Can't get managed objects");
71                 if (err) {
72                         ERR("%s", err->message);
73                         g_clear_error(&err);
74                 }
75                 return BT_STATUS_FAIL;
76         }
77
78         g_variant_get(reply, "(v)", &temp);
79         *value = g_variant_get_uint16(temp);
80         g_variant_unref(temp);
81
82         DBG("Type[%s] and Value[%d]", __bt_transport_type_to_str(type), *value);
83
84         g_variant_unref(reply);
85         return BT_STATUS_SUCCESS;
86 }
87
88
89 bt_status_t _bt_hal_dbus_handler_avrcp_transport_set_property(bt_bdaddr_t *bd_addr, int type, unsigned int value)
90 {
91         GVariant *reply, *param = NULL;
92         GError *err = NULL;
93         GDBusProxy *proxy = NULL;
94         uint16_t property_level = (uint16_t)value;
95
96         switch (type) {
97         case BTRC_TRANSPORT_ATTR_DELAY:
98                 param = g_variant_new("q", property_level);
99                 INFO("delay level %d", property_level);
100                 break;
101         case BTRC_TRANSPORT_ATTR_VOLUME:
102                 param = g_variant_new("q", property_level);
103                 INFO("volume level %d", property_level);
104                 break;
105         default:
106                 ERR("Invalid property type: %d", type);
107                 return BT_STATUS_FAIL;
108         }
109
110         proxy = _bt_hal_get_avrcp_transport_properties_proxy(bd_addr);
111         if (proxy == NULL) {
112                 ERR("Unable to get proxy \n");
113                 return BT_STATUS_FAIL;
114         }
115
116         reply = g_dbus_proxy_call_sync(proxy,
117                         "Set", g_variant_new("(ssv)",
118                                 BT_HAL_MEDIATRANSPORT_INTERFACE,
119                                 __bt_transport_type_to_str(type), param),
120                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
121         g_object_unref(proxy);
122         if (!reply) {
123                 ERR("Can't get managed objects");
124                 if (err) {
125                         ERR("SetProperty Fail: %s", err->message);
126                         g_clear_error(&err);
127                         return BT_STATUS_FAIL;
128                 }
129         }
130
131         g_variant_unref(reply);
132
133         return BT_STATUS_SUCCESS;
134 }