Apply tizen 3.0 based product patchsets
[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(NULL == buf, BLUETOOTH_ERROR_INVALID_PARAM);
144         retv_if(length == 0 || length > BLUETOOTH_TDS_DATA_LENGTH_MAX,
145                         BLUETOOTH_ERROR_INVALID_PARAM);
146         tds_data.length = length;
147         memcpy(tds_data.data, buf, length);
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, &tds_handle, sizeof(unsigned int));
153         g_array_append_vals(in_param2, &state, sizeof(int));
154         g_array_append_vals(in_param3, &tds_data, sizeof(bluetooth_tds_data_t));
155
156         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_TDS_PROVIDER_SET_TRANSPORT_DATA,
157                         in_param1, in_param2, in_param3, in_param4, &out_param);
158
159         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
160
161         return result;
162 }
163
164 BT_EXPORT_API int bluetooth_send_tds_activation_response(bluetooth_device_address_t *device_address,
165         unsigned int tds_handle, int response, unsigned char *buf, unsigned int length)
166 {
167         int result;
168         bluetooth_tds_data_t tds_data;
169
170         BT_CHECK_PARAMETER(device_address, return);
171         BT_CHECK_ENABLED_LE(return);
172
173         retv_if(length > 0 && NULL == buf, BLUETOOTH_ERROR_INVALID_PARAM);
174         tds_data.length = length;
175         memcpy(tds_data.data, buf, length);
176
177         BT_INIT_PARAMS();
178         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
179
180         g_array_append_vals(in_param1, &tds_handle, sizeof(unsigned int));
181         g_array_append_vals(in_param2, &response, sizeof(int));
182         g_array_append_vals(in_param3, device_address, sizeof(bluetooth_device_address_t));
183         g_array_append_vals(in_param4, &tds_data, sizeof(bluetooth_tds_data_t));
184
185         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_TDS_SEND_ACTIVATION_RESPONSE,
186                         in_param1, in_param2, in_param3, in_param4, &out_param);
187
188         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
189
190         return result;
191 }
192
193 BT_EXPORT_API int bluetooth_tds_read_transport_data(const bluetooth_device_address_t *device_address,
194                                                         const char *handle)
195 {
196         char *path;
197         int path_len = 0;
198         bt_user_info_t *user_info;
199         int result = BLUETOOTH_ERROR_INTERNAL;
200         BT_DBG("+");
201
202         BT_CHECK_PARAMETER(device_address, return);
203         BT_CHECK_PARAMETER(handle, return);
204         BT_CHECK_ENABLED_LE(return);
205
206         user_info = _bt_get_user_data(BT_COMMON);
207         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
208
209         BT_INIT_PARAMS();
210         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
211
212         path = g_strdup(handle);
213         path_len = _bluetooth_handle_get_len(path);
214         g_array_append_vals(in_param1, path, path_len);
215         g_free(path);
216
217         g_array_append_vals(in_param2, device_address,
218                         sizeof(bluetooth_device_address_t));
219
220         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_TDS_READ_TRANSPORT_DATA,
221                         in_param1, in_param2, in_param3, in_param4,
222                         user_info->cb, user_info->user_data);
223
224         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
225
226         BT_DBG("-");
227         return result;
228 }
229
230 BT_EXPORT_API int bluetooth_tds_enable_control_point(const bluetooth_device_address_t *device_address,
231                                                         const char *handle)
232 {
233         char *path;
234         int path_len = 0;
235         bt_user_info_t *user_info;
236         int result = BLUETOOTH_ERROR_INTERNAL;
237         BT_DBG("+");
238
239         BT_CHECK_PARAMETER(device_address, return);
240         BT_CHECK_PARAMETER(handle, return);
241         BT_CHECK_ENABLED_LE(return);
242
243         user_info = _bt_get_user_data(BT_COMMON);
244         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
245
246         BT_INIT_PARAMS();
247         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
248
249         path = g_strdup(handle);
250         path_len = _bluetooth_handle_get_len(path);
251         g_array_append_vals(in_param1, path, path_len);
252         g_free(path);
253
254         g_array_append_vals(in_param2, device_address,
255                         sizeof(bluetooth_device_address_t));
256
257         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_TDS_ENABLE_CONTROL_POINT,
258                         in_param1, in_param2, in_param3, in_param4,
259                         user_info->cb, user_info->user_data);
260
261         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
262
263         BT_DBG("-");
264         return result;
265 }
266
267 BT_EXPORT_API int bluetooth_tds_activate_control_point(const bluetooth_device_address_t *device_address,
268                                         const char *handle, unsigned char *buf,
269                                         int length)
270 {
271         char *path;
272         int path_len = 0;
273         bt_user_info_t *user_info;
274         int result = BLUETOOTH_ERROR_INTERNAL;
275         bluetooth_control_point_data_t data;
276         BT_DBG("+");
277
278         BT_CHECK_PARAMETER(device_address, return);
279         BT_CHECK_PARAMETER(handle, return);
280         BT_CHECK_PARAMETER(buf, return);
281         BT_CHECK_ENABLED_LE(return);
282
283         user_info = _bt_get_user_data(BT_COMMON);
284         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
285
286         BT_INIT_PARAMS();
287         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
288
289         memset(&data, 0x00, sizeof(bluetooth_control_point_data_t));
290
291         data.length = length;
292         if (length > 0)
293                 memcpy(data.data, buf, length);
294
295         path = g_strdup(handle);
296         path_len = _bluetooth_handle_get_len(path);
297
298         /*Fill parameters*/
299         g_array_append_vals(in_param1, path, path_len);
300
301         g_array_append_vals(in_param2, device_address,
302                         sizeof(bluetooth_device_address_t));
303
304         g_array_append_vals(in_param3, &data, sizeof(bluetooth_control_point_data_t));
305
306         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_TDS_ACTIVATE_CONTROL_POINT,
307                         in_param1, in_param2, in_param3, in_param4,
308                         user_info->cb, user_info->user_data);
309
310         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
311
312         g_free(path);
313         BT_DBG("-");
314         return result;
315 }