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