b03143b9347a2cd8b9abd12c9d8f1d40e9134ba0
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-hid.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include "bluetooth-api.h"
25 #include "bluetooth-hid-api.h"
26 #include "bt-internal-types.h"
27
28 #include "bt-common.h"
29 #include "bt-request-sender.h"
30 #include "bt-event-handler.h"
31
32 BT_EXPORT_API int bluetooth_hid_init(hid_cb_func_ptr callback_ptr, void *user_data)
33 {
34         int ret;
35
36         /* Register AVRCP events */
37         ret = _bt_register_event(BT_HID_EVENT , (void *)callback_ptr, user_data);
38
39         if (ret != BLUETOOTH_ERROR_NONE &&
40              ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
41                 BT_ERR("Fail to init the event handler");
42                 return ret;
43         }
44
45         _bt_set_user_data(BT_HID, (void *)callback_ptr, user_data);
46
47         return BLUETOOTH_ERROR_NONE;
48 }
49
50 BT_EXPORT_API int bluetooth_hid_deinit(void)
51 {
52         int ret;
53
54         ret = _bt_unregister_event(BT_HID_EVENT);
55
56         if (ret != BLUETOOTH_ERROR_NONE) {
57                 BT_ERR("Fail to deinit the event handler");
58                 return ret;
59         }
60
61         _bt_set_user_data(BT_HID, NULL, NULL);
62
63         return BLUETOOTH_ERROR_NONE;
64 }
65
66 BT_EXPORT_API int bluetooth_hid_connect(hid_device_address_t *device_address)
67 {
68         int result;
69         bt_user_info_t *user_info;
70
71         BT_CHECK_PARAMETER(device_address, return);
72         BT_CHECK_ENABLED(return);
73
74         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_CONNECT)
75              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
76                 BT_ERR("Don't have a privilege to use this API");
77                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
78         }
79
80         user_info = _bt_get_user_data(BT_HID);
81         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
82
83         BT_INIT_PARAMS();
84         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
85
86         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
87
88         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_HID_CONNECT,
89                 in_param1, in_param2, in_param3, in_param4,
90                 user_info->cb, user_info->user_data);
91
92         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
93
94         return result;
95 }
96
97 BT_EXPORT_API int bluetooth_hid_disconnect(hid_device_address_t *device_address)
98 {
99         int result;
100         bt_user_info_t *user_info;
101
102         BT_CHECK_PARAMETER(device_address, return);
103         BT_CHECK_ENABLED(return);
104
105         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_HID_DISCONNECT)
106              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
107                 BT_ERR("Don't have a privilege to use this API");
108                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
109         }
110
111         user_info = _bt_get_user_data(BT_HID);
112         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
113
114         BT_INIT_PARAMS();
115         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
116
117         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
118
119         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_HID_DISCONNECT,
120                 in_param1, in_param2, in_param3, in_param4,
121                 user_info->cb, user_info->user_data);
122
123         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
124
125         return result;
126 }
127