987b45d9893ddcee8be79be430043387542bd773
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-network.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 "bt-internal-types.h"
26
27 #include "bt-common.h"
28 #include "bt-request-sender.h"
29 #include "bt-event-handler.h"
30
31 BT_EXPORT_API int bluetooth_network_activate_server()
32 {
33         int result;
34
35         BT_CHECK_ENABLED(return);
36
37         BT_INIT_PARAMS();
38         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
39
40         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_NETWORK_ACTIVATE,
41                 in_param1, in_param2, in_param3, in_param4, &out_param);
42
43         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
44
45         return result;
46 }
47
48 BT_EXPORT_API int bluetooth_network_deactivate_server(void)
49 {
50         int result;
51
52         BT_CHECK_ENABLED(return);
53
54         BT_INIT_PARAMS();
55         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
56
57         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_NETWORK_DEACTIVATE,
58                 in_param1, in_param2, in_param3, in_param4, &out_param);
59
60         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
61
62         return result;
63 }
64
65 BT_EXPORT_API int bluetooth_network_connect(const bluetooth_device_address_t *device_address,
66                                                 bluetooth_network_role_t role,
67                                                 char *custom_uuid)
68 {
69         int result;
70         bt_user_info_t *user_info;
71
72         BT_CHECK_PARAMETER(device_address, return);
73         BT_CHECK_ENABLED(return);
74
75         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_NETWORK_CONNECT)
76              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
77                 BT_ERR("Don't have a privilege to use this API");
78                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
79         }
80
81         user_info = _bt_get_user_data(BT_COMMON);
82         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
83
84         BT_INIT_PARAMS();
85         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
86
87         g_array_append_vals(in_param1, device_address,
88                                 sizeof(bluetooth_device_address_t));
89         g_array_append_vals(in_param2, &role, sizeof(int));
90
91         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_NETWORK_CONNECT,
92                 in_param1, in_param2, in_param3, in_param4,
93                 user_info->cb, user_info->user_data);
94
95         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
96
97         return result;
98 }
99
100 BT_EXPORT_API int bluetooth_network_disconnect(const bluetooth_device_address_t *device_address)
101 {
102         int result;
103         bt_user_info_t *user_info;
104
105         BT_CHECK_PARAMETER(device_address, return);
106         BT_CHECK_ENABLED(return);
107
108         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_NETWORK_DISCONNECT)
109              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
110                 BT_ERR("Don't have a privilege to use this API");
111                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
112         }
113
114         user_info = _bt_get_user_data(BT_COMMON);
115         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
116
117         BT_INIT_PARAMS();
118         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
119
120         g_array_append_vals(in_param1, device_address,
121                                         sizeof(bluetooth_device_address_t));
122
123         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_NETWORK_DISCONNECT,
124                 in_param1, in_param2, in_param3, in_param4,
125                 user_info->cb, user_info->user_data);
126
127         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
128
129         return result;
130 }
131
132 BT_EXPORT_API int bluetooth_network_server_disconnect(const bluetooth_device_address_t *device_address)
133 {
134         int result;
135         bt_user_info_t *user_info;
136
137         BT_CHECK_PARAMETER(device_address, return);
138         BT_CHECK_ENABLED(return);
139
140         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_NETWORK_SERVER_DISCONNECT)
141              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
142                 BT_ERR("Don't have a privilege to use this API");
143                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
144         }
145
146         user_info = _bt_get_user_data(BT_COMMON);
147         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
148
149         BT_INIT_PARAMS();
150         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
151
152         g_array_append_vals(in_param1, device_address,
153                                         sizeof(bluetooth_device_address_t));
154
155         result = _bt_send_request_async(BT_BLUEZ_SERVICE,
156                                 BT_NETWORK_SERVER_DISCONNECT,
157                                 in_param1, in_param2, in_param3, in_param4,
158                                 user_info->cb, user_info->user_data);
159
160         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
161
162         return result;
163 }
164