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