Correct the privilege check for BT_UPDATE_LE_CONNECTION_MODE
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-hid.c
1 /*
2  * Copyright (c) 2011 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 "bluetooth-api.h"
19 #include "bluetooth-hid-api.h"
20 #include "bt-internal-types.h"
21
22 #include "bt-common.h"
23 #include "bt-request-sender.h"
24 #include "bt-event-handler.h"
25 #include "bt-dpm.h"
26
27 BT_EXPORT_API int bluetooth_hid_init(hid_cb_func_ptr callback_ptr, void *user_data)
28 {
29         int ret;
30
31         /* Register AVRCP events */
32         ret = _bt_register_event(BT_HID_EVENT , (void *)callback_ptr, user_data);
33
34         if (ret != BLUETOOTH_ERROR_NONE &&
35              ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
36                 BT_ERR("Fail to init the event handler");
37                 return ret;
38         }
39
40         _bt_set_user_data(BT_HID, (void *)callback_ptr, user_data);
41
42         return BLUETOOTH_ERROR_NONE;
43 }
44
45 BT_EXPORT_API int bluetooth_hid_deinit(void)
46 {
47         int ret;
48
49         ret = _bt_unregister_event(BT_HID_EVENT);
50
51         if (ret != BLUETOOTH_ERROR_NONE) {
52                 BT_ERR("Fail to deinit the event handler");
53                 return ret;
54         }
55
56         _bt_set_user_data(BT_HID, NULL, NULL);
57
58         return BLUETOOTH_ERROR_NONE;
59 }
60
61 BT_EXPORT_API int bluetooth_hid_connect(hid_device_address_t *device_address)
62 {
63         int result;
64         bt_user_info_t *user_info;
65
66         BT_CHECK_PARAMETER(device_address, return);
67         BT_CHECK_ENABLED(return);
68
69         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_CONNECT)
70              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
71                 BT_ERR("Don't have a privilege to use this API");
72                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
73         }
74
75         if (_bt_check_dpm(BT_DPM_ADDRESS, device_address) == BT_DPM_RESTRICTED) {
76                 BT_ERR("Blacklist device");
77                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
78         }
79
80         if (_bt_check_dpm(BT_DPM_UUID, BT_HID_UUID) == BT_DPM_RESTRICTED) {
81                 BT_ERR("Blacklist uuid");
82                 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
83         }
84
85         user_info = _bt_get_user_data(BT_HID);
86         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
87
88         BT_INIT_PARAMS();
89         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
90
91         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
92
93         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_HID_CONNECT,
94                 in_param1, in_param2, in_param3, in_param4,
95                 user_info->cb, user_info->user_data);
96
97         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
98
99         return result;
100 }
101
102 BT_EXPORT_API int bluetooth_hid_disconnect(hid_device_address_t *device_address)
103 {
104         int result;
105         bt_user_info_t *user_info;
106
107         BT_CHECK_PARAMETER(device_address, return);
108         BT_CHECK_ENABLED(return);
109
110         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DISCONNECT)
111              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
112                 BT_ERR("Don't have a privilege to use this API");
113                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
114         }
115
116         user_info = _bt_get_user_data(BT_HID);
117         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
118
119         BT_INIT_PARAMS();
120         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
121
122         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
123
124         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_HID_DISCONNECT,
125                 in_param1, in_param2, in_param3, in_param4,
126                 user_info->cb, user_info->user_data);
127
128         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
129
130         return result;
131 }
132
133 BT_EXPORT_API int bluetooth_hid_enable_barcode_feature(void)
134 {
135         int result;
136
137         BT_INIT_PARAMS();
138         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
139
140         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_HID_ENABLE_BARCODE_FEATURE,
141                 in_param1, in_param2, in_param3, in_param4, &out_param);
142
143         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
144
145         return result;
146 }
147