Merge "DPM: Add basic code for device policy manager for BT." into tizen
[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 BT_EXPORT_API int bluetooth_hid_init(hid_cb_func_ptr callback_ptr, void *user_data)
27 {
28         int ret;
29
30         /* Register AVRCP events */
31         ret = _bt_register_event(BT_HID_EVENT , (void *)callback_ptr, user_data);
32
33         if (ret != BLUETOOTH_ERROR_NONE &&
34              ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
35                 BT_ERR("Fail to init the event handler");
36                 return ret;
37         }
38
39         _bt_set_user_data(BT_HID, (void *)callback_ptr, user_data);
40
41         return BLUETOOTH_ERROR_NONE;
42 }
43
44 BT_EXPORT_API int bluetooth_hid_deinit(void)
45 {
46         int ret;
47
48         ret = _bt_unregister_event(BT_HID_EVENT);
49
50         if (ret != BLUETOOTH_ERROR_NONE) {
51                 BT_ERR("Fail to deinit the event handler");
52                 return ret;
53         }
54
55         _bt_set_user_data(BT_HID, NULL, NULL);
56
57         return BLUETOOTH_ERROR_NONE;
58 }
59
60 BT_EXPORT_API int bluetooth_hid_connect(hid_device_address_t *device_address)
61 {
62         int result;
63         bt_user_info_t *user_info;
64
65         BT_CHECK_PARAMETER(device_address, return);
66         BT_CHECK_ENABLED(return);
67
68         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_CONNECT)
69              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
70                 BT_ERR("Don't have a privilege to use this API");
71                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
72         }
73
74         user_info = _bt_get_user_data(BT_HID);
75         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
76
77         BT_INIT_PARAMS();
78         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
79
80         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
81
82         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_HID_CONNECT,
83                 in_param1, in_param2, in_param3, in_param4,
84                 user_info->cb, user_info->user_data);
85
86         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
87
88         return result;
89 }
90
91 BT_EXPORT_API int bluetooth_hid_disconnect(hid_device_address_t *device_address)
92 {
93         int result;
94         bt_user_info_t *user_info;
95
96         BT_CHECK_PARAMETER(device_address, return);
97         BT_CHECK_ENABLED(return);
98
99         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DISCONNECT)
100              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
101                 BT_ERR("Don't have a privilege to use this API");
102                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
103         }
104
105         user_info = _bt_get_user_data(BT_HID);
106         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
107
108         BT_INIT_PARAMS();
109         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
110
111         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
112
113         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_HID_DISCONNECT,
114                 in_param1, in_param2, in_param3, in_param4,
115                 user_info->cb, user_info->user_data);
116
117         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
118
119         return result;
120 }
121