c7b983b853170504dcaa45ea7346f2379f10152e
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-tds.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 "bt-internal-types.h"
20
21 #include "bt-common.h"
22 #include "bt-request-sender.h"
23 #include "bt-event-handler.h"
24
25 static int _bluetooth_handle_get_len(const char *str)
26 {
27         int i;
28         for (i = 0; str && str[i] != '\0'; i++);
29         return i;
30 }
31
32 BT_EXPORT_API int bluetooth_tds_provider_register(void)
33 {
34         int result = BLUETOOTH_ERROR_INTERNAL;
35
36         BT_CHECK_ENABLED_LE(return);
37
38         BT_INIT_PARAMS();
39         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
40         BT_DBG("");
41
42         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_TDS_PROVIDER_REGISTER,
43                 in_param1, in_param2, in_param3, in_param4, &out_param);
44
45         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
46
47         return result;
48 }
49
50 BT_EXPORT_API int bluetooth_tds_provider_unregister(void)
51 {
52         int result = BLUETOOTH_ERROR_INTERNAL;
53
54         BT_CHECK_ENABLED_LE(return);
55
56         BT_INIT_PARAMS();
57         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
58         BT_DBG("");
59
60         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_TDS_PROVIDER_UNREGISTER,
61                 in_param1, in_param2, in_param3, in_param4, &out_param);
62
63         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
64
65         return result;
66 }
67
68 BT_EXPORT_API int bluetooth_set_tds_provider_manuf_data(unsigned char *buf, unsigned int length)
69 {
70         int result;
71         bluetooth_advertising_data_t manuf_data;
72
73         BT_CHECK_ENABLED_LE(return);
74
75         retv_if(length > 0 && NULL == buf, BLUETOOTH_ERROR_INVALID_PARAM);
76         retv_if(length > BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX, BLUETOOTH_ERROR_INVALID_PARAM);
77
78         memcpy(manuf_data.data, buf, length);
79
80         BT_INIT_PARAMS();
81         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
82
83         g_array_append_vals(in_param1, &length, sizeof(unsigned int));
84         g_array_append_vals(in_param2, &manuf_data, sizeof(bluetooth_advertising_data_t));
85
86         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_TDS_PROVIDER_SET_MANUF_DATA,
87                         in_param1, in_param2, in_param3, in_param4, &out_param);
88
89         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
90
91         return result;
92 }
93
94 BT_EXPORT_API int bluetooth_tds_provider_create(int transport, unsigned int handle)
95 {
96         int result = BLUETOOTH_ERROR_INTERNAL;
97
98         BT_CHECK_ENABLED_LE(return);
99
100         BT_INIT_PARAMS();
101         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
102         BT_DBG("");
103
104         g_array_append_vals(in_param1, &handle, sizeof(unsigned int));
105         g_array_append_vals(in_param2, &transport, sizeof(unsigned int));
106
107         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_TDS_PROVIDER_CREATE,
108                 in_param1, in_param2, in_param3, in_param4, &out_param);
109
110         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
111
112         return result;
113 }
114
115 BT_EXPORT_API int bluetooth_tds_provider_destroy(unsigned int handle)
116 {
117         int result = BLUETOOTH_ERROR_INTERNAL;
118
119         BT_CHECK_ENABLED_LE(return);
120
121         BT_INIT_PARAMS();
122         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
123         BT_DBG("");
124
125         g_array_append_vals(in_param1, &handle, sizeof(unsigned int));
126
127         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_TDS_PROVIDER_DESTROY,
128                 in_param1, in_param2, in_param3, in_param4, &out_param);
129
130         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
131
132         return result;
133 }
134
135 BT_EXPORT_API int bluetooth_set_tds_provider_transport_data(unsigned int tds_handle,
136                 bluetooth_tds_transport_state_t state, unsigned char *buf, unsigned int length)
137 {
138         int result;
139         bluetooth_tds_data_t tds_data;
140
141         BT_CHECK_ENABLED_LE(return);
142
143         retv_if(length > 0 && NULL == buf, BLUETOOTH_ERROR_INVALID_PARAM);
144         tds_data.length = length;
145         memcpy(tds_data.data, buf, length);
146
147         BT_INIT_PARAMS();
148         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
149
150         g_array_append_vals(in_param1, &tds_handle, sizeof(unsigned int));
151         g_array_append_vals(in_param2, &state, sizeof(int));
152         g_array_append_vals(in_param3, &tds_data, sizeof(bluetooth_tds_data_t));
153
154         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_TDS_PROVIDER_SET_TRANSPORT_DATA,
155                         in_param1, in_param2, in_param3, in_param4, &out_param);
156
157         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
158
159         return result;
160 }
161
162 BT_EXPORT_API int bluetooth_send_tds_activation_response(bluetooth_device_address_t *device_address,
163         unsigned int tds_handle, int response, unsigned char *buf, unsigned int length)
164 {
165         int result;
166         bluetooth_tds_data_t tds_data;
167
168         BT_CHECK_PARAMETER(device_address, return);
169         BT_CHECK_ENABLED_LE(return);
170
171         retv_if(length > 0 && NULL == buf, BLUETOOTH_ERROR_INVALID_PARAM);
172         tds_data.length = length;
173         memcpy(tds_data.data, buf, length);
174
175         BT_INIT_PARAMS();
176         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
177
178         g_array_append_vals(in_param1, &tds_handle, sizeof(unsigned int));
179         g_array_append_vals(in_param2, &response, sizeof(int));
180         g_array_append_vals(in_param3, device_address, sizeof(bluetooth_device_address_t));
181         g_array_append_vals(in_param4, &tds_data, sizeof(bluetooth_tds_data_t));
182
183         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_TDS_SEND_ACTIVATION_RESPONSE,
184                         in_param1, in_param2, in_param3, in_param4, &out_param);
185
186         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
187
188         return result;
189 }
190
191 BT_EXPORT_API int bluetooth_tds_read_transport_data(const bluetooth_device_address_t *device_address,
192                                                         const char *handle)
193 {
194         char *path;
195         int path_len = 0;
196         bt_user_info_t *user_info;
197         int result = BLUETOOTH_ERROR_INTERNAL;
198         BT_DBG("+");
199
200         BT_CHECK_PARAMETER(device_address, return);
201         BT_CHECK_PARAMETER(handle, return);
202         BT_CHECK_ENABLED_LE(return);
203
204         user_info = _bt_get_user_data(BT_COMMON);
205         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
206
207         BT_INIT_PARAMS();
208         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
209
210         path = g_strdup(handle);
211         path_len = _bluetooth_handle_get_len(path);
212         g_array_append_vals(in_param1, path, path_len);
213         g_free(path);
214
215         g_array_append_vals(in_param2, device_address,
216                         sizeof(bluetooth_device_address_t));
217
218         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_TDS_READ_TRANSPORT_DATA,
219                         in_param1, in_param2, in_param3, in_param4,
220                         user_info->cb, user_info->user_data);
221
222         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
223
224         BT_DBG("-");
225         return result;
226 }
227
228 BT_EXPORT_API int bluetooth_tds_enable_control_point(const bluetooth_device_address_t *device_address,
229                                                         const char *handle)
230 {
231         char *path;
232         int path_len = 0;
233         bt_user_info_t *user_info;
234         int result = BLUETOOTH_ERROR_INTERNAL;
235         BT_DBG("+");
236
237         BT_CHECK_PARAMETER(device_address, return);
238         BT_CHECK_PARAMETER(handle, return);
239         BT_CHECK_ENABLED_LE(return);
240
241         user_info = _bt_get_user_data(BT_COMMON);
242         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
243
244         BT_INIT_PARAMS();
245         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
246
247         path = g_strdup(handle);
248         path_len = _bluetooth_handle_get_len(path);
249         g_array_append_vals(in_param1, path, path_len);
250         g_free(path);
251
252         g_array_append_vals(in_param2, device_address,
253                         sizeof(bluetooth_device_address_t));
254
255         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_TDS_ENABLE_CONTROL_POINT,
256                         in_param1, in_param2, in_param3, in_param4,
257                         user_info->cb, user_info->user_data);
258
259         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
260
261         BT_DBG("-");
262         return result;
263 }
264
265 BT_EXPORT_API int bluetooth_tds_activate_control_point(const bluetooth_device_address_t *device_address,
266                                         const char *handle, unsigned char *buf,
267                                         int length)
268 {
269         char *path;
270         int path_len = 0;
271         bt_user_info_t *user_info;
272         int result = BLUETOOTH_ERROR_INTERNAL;
273         bluetooth_control_point_data_t data;
274         BT_DBG("+");
275
276         BT_CHECK_PARAMETER(device_address, return);
277         BT_CHECK_PARAMETER(handle, return);
278         BT_CHECK_PARAMETER(buf, return);
279         BT_CHECK_ENABLED_LE(return);
280
281         user_info = _bt_get_user_data(BT_COMMON);
282         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
283
284         BT_INIT_PARAMS();
285         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
286
287         memset(&data, 0x00, sizeof(bluetooth_control_point_data_t));
288
289         data.length = length;
290         if (length > 0)
291                 memcpy(data.data, buf, length);
292
293         path = g_strdup(handle);
294         path_len = _bluetooth_handle_get_len(path);
295
296         /*Fill parameters*/
297         g_array_append_vals(in_param1, path, path_len);
298
299         g_array_append_vals(in_param2, device_address,
300                         sizeof(bluetooth_device_address_t));
301
302         g_array_append_vals(in_param3, &data, sizeof(bluetooth_control_point_data_t));
303
304         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_TDS_ACTIVATE_CONTROL_POINT,
305                         in_param1, in_param2, in_param3, in_param4,
306                         user_info->cb, user_info->user_data);
307
308         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
309
310         g_free(path);
311         BT_DBG("-");
312         return result;
313 }