75a7a13963d2711752d974907aabffa9fc55b499
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / include / bt-common.h
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
19 #ifndef _BT_COMMON_H_
20 #define _BT_COMMON_H_
21
22 #include <sys/types.h>
23 #include <libintl.h>
24 #include <dlog.h>
25 #include <glib.h>
26 #include <gio/gio.h>
27
28 #include "bluetooth-api.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif /* __cplusplus */
33
34 #undef LOG_TAG
35 #define LOG_TAG "BLUETOOTH_FRWK_API"
36
37 #ifndef BT_EXPORT_API
38 #define BT_EXPORT_API __attribute__((visibility("default")))
39 #endif
40
41 #define LOG_COLOR_RESET    "\033[0m"
42 #define LOG_COLOR_RED      "\033[31m"
43 #define LOG_COLOR_YELLOW   "\033[33m"
44 #define LOG_COLOR_GREEN         "\033[32m"
45 #define LOG_COLOR_BLUE          "\033[36m"
46 #define LOG_COLOR_PURPLE   "\033[35m"
47
48 #define BT_DBG(fmt, args...) \
49         SLOGD(fmt, ##args)
50 #define BT_INFO(fmt, args...) \
51         SLOGI(fmt, ##args)
52 #define BT_ERR(fmt, args...) \
53         SLOGE(fmt, ##args)
54
55 #define BT_DBG_UUID(uuids, len, i) \
56         BT_DBG("***UUIDs***"); \
57         for (i = 0; i < len; i++) { \
58                 BT_DBG("%s", uuids[i]); \
59         }
60
61 #define BT_INFO_C(fmt, arg...) \
62         SLOGI_IF(TRUE,  LOG_COLOR_GREEN" "fmt" "LOG_COLOR_RESET, ##arg)
63 #define BT_ERR_C(fmt, arg...) \
64         SLOGI_IF(TRUE,  LOG_COLOR_RED" "fmt" "LOG_COLOR_RESET, ##arg)
65
66 #define DBG_SECURE(fmt, args...) SECURE_SLOGD(fmt, ##args)
67 #define ERR_SECURE(fmt, args...) SECURE_SLOGE(fmt, ##args)
68
69 //#define FUNCTION_TRACE
70 #ifdef FUNCTION_TRACE
71 #define FN_START BT_DBG("[ENTER FUNC]")
72 #define FN_END BT_DBG("[EXIT FUNC]")
73 #else
74 #define FN_START
75 #define FN_END
76 #endif
77
78 #define ret_if(expr) \
79         do { \
80                 if (expr) { \
81                         BT_ERR("(%s) return", #expr); \
82                         return; \
83                 } \
84         } while (0)
85
86 #define retv_if(expr, val) \
87         do { \
88                 if (expr) { \
89                         BT_ERR("(%s) return", #expr); \
90                         return (val); \
91                 } \
92         } while (0)
93
94 #define BT_INIT_PARAMS() \
95         GArray *in_param1 = NULL; \
96         GArray *in_param2 = NULL; \
97         GArray *in_param3 = NULL; \
98         GArray *in_param4 = NULL; \
99         GArray *out_param = NULL;
100
101 #define BT_FREE_PARAMS(IP1, IP2, IP3, IP4, OP) \
102         do { \
103                 if (IP1) \
104                         g_array_free(IP1, TRUE); \
105                 if (IP2) \
106                         g_array_free(IP2, TRUE); \
107                 if (IP3) \
108                         g_array_free(IP3, TRUE); \
109                 if (IP4) \
110                         g_array_free(IP4, TRUE); \
111                 if (OP) \
112                         g_array_free(OP, TRUE); \
113         } while (0)
114
115 #define BT_ALLOC_PARAMS(IP1, IP2, IP3, IP4, OP) \
116         do { \
117                 IP1 = g_array_new(TRUE, TRUE, sizeof(gchar));   \
118                 IP2 = g_array_new(TRUE, TRUE, sizeof(gchar));   \
119                 IP3 = g_array_new(TRUE, TRUE, sizeof(gchar));   \
120                 IP4 = g_array_new(TRUE, TRUE, sizeof(gchar)); \
121         } while (0)
122
123 #define BT_INIT_AGENT_PARAMS() \
124         GArray *in_param = NULL; \
125         GArray *out_param = NULL;
126
127 #define BT_FREE_AGENT_PARAMS(IP, OP) \
128         do { \
129                 if (IP) \
130                         g_array_free(IP, TRUE); \
131                 if (OP) \
132                         g_array_free(OP, TRUE); \
133         } while (0)
134
135 #define BT_ALLOC_AGENT_PARAMS(IP, OP) \
136         do { \
137                 IP = g_array_new(FALSE, FALSE, sizeof(gchar));  \
138         } while (0)
139
140 #define BT_CHECK_PARAMETER(arg, func) \
141         do { \
142                 if (arg == NULL) { \
143                         BT_ERR("%s is NULL", #arg); \
144                         func BLUETOOTH_ERROR_INVALID_PARAM; \
145                 } \
146         } while (0)
147
148 #define BT_CHECK_ENABLED(func) \
149         do { \
150                 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED) { \
151                         BT_ERR("BT BREDR is not enabled"); \
152                         func BLUETOOTH_ERROR_DEVICE_NOT_ENABLED; \
153                 } \
154         } while (0)
155
156 #define BT_CHECK_ENABLED_LE(func) \
157         do { \
158                 if (bluetooth_check_adapter_le() == BLUETOOTH_ADAPTER_LE_DISABLED) { \
159                         BT_ERR("BT LE is not enabled"); \
160                         func BLUETOOTH_ERROR_DEVICE_NOT_ENABLED; \
161                 } \
162         } while (0)
163
164 #define BT_CHECK_ENABLED_ANY(func) \
165         do { \
166                 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED && \
167                         bluetooth_check_adapter_le() == BLUETOOTH_ADAPTER_LE_DISABLED) { \
168                         BT_ERR("BT is not enabled"); \
169                         func BLUETOOTH_ERROR_DEVICE_NOT_ENABLED; \
170                 } \
171         } while (0)
172
173 #define BT_CHECK_ENABLED_INTERNAL(func) \
174         do { \
175                 if (_bt_check_enabled_internal() == FALSE) { \
176                         BT_ERR("BT BREDR is not enabled"); \
177                         func BLUETOOTH_ERROR_DEVICE_NOT_ENABLED; \
178                 } \
179         } while (0)
180
181 #define BT_ADDRESS_LENGTH_MAX 6
182 #define BT_ADDRESS_STRING_SIZE 18
183 #define BT_ADAPTER_OBJECT_PATH_MAX 50
184 #define BT_RFCOMM_BUFFER_LEN 1024
185
186 #define BT_ACCESS_DENIED_MSG "Rejected send message"
187
188 #define BT_EVENT_FREEDESKTOP "org.freedesktop.DBus"
189 #define BT_FREEDESKTOP_PATH "/org/freedesktop/DBus"
190
191 #define BT_EVENT_MANAGER "org.bluez.Manager"
192 #define BT_MANAGER_PATH "/"
193
194
195 #define BT_MANAGER_INTERFACE "org.freedesktop.DBus.ObjectManager"
196 #define BT_ADAPTER_INTERFACE "org.bluez.Adapter1"
197 #define BT_DEVICE_INTERFACE "org.bluez.Device1"
198 #define BT_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
199
200
201 #define BT_SERIAL_INTERFACE "org.bluez.Serial"
202
203 #define BT_BLUEZ_NAME "org.bluez"
204 #define BT_DBUS_NAME "org.projectx.bt"
205 #define BT_SERVICE_PATH "/org/projectx/bt_service"
206
207 #define BT_AGENT_NAME "org.bluez.frwk_agent"
208 #define BT_AGENT_PATH "/org/bluez/agent/frwk_agent"
209
210 #define BT_AGENT_INTERFACE "org.bluez.Agent1"
211
212 #define BT_ERROR_INTERNAL "InternalError"
213 #define BT_ERROR_NOT_AVAILABLE "NotAvailable"
214 #define BT_ERROR_NOT_CONNECTED "NotConnected"
215 #define BT_ERROR_NOT_CONNECTION_FAILED "ConnectionFailed"
216 #define BT_ERROR_BUSY "InProgress"
217 #define BT_ERROR_INVALID_PARAM "InvalidArguments"
218 #define BT_ERROR_ALREADY_EXIST "AlreadyExists"
219 #define BT_ERROR_DOES_NOT_EXIST "DoesNotExist"
220 #define BT_ERROR_ALREADY_CONNECTED "Already Connected"
221 #define BT_ERROR_NO_MEMORY "No memory"
222 #define BT_ERROR_NO_DATA "No data"
223 #define BT_ERROR_I_O_ERROR "I/O error"
224 #define BT_ERROR_OPERATION_NOT_AVAILABLE "Operation currently not available"
225 #define BT_ERROR_OPERATION_NOT_ALLOWED "Operation not allowed"
226 #define BT_ERROR_OPERATION_NOT_SUPPORTED "Operation not supported"
227 #define BT_ERROR_INVALID_FILE_DESCRIPTOR "Invalid File Descriptor"
228 #define BT_ERROR_ACCESS_DENIED "Rejected send message"
229
230 #define RFKILL_EVENT_SIZE 8
231 #define RFKILL_NODE "/dev/rfkill"
232
233 typedef enum {
234         RFKILL_TYPE_ALL = 0,
235         RFKILL_TYPE_WLAN,
236         RFKILL_TYPE_BLUETOOTH,
237         RFKILL_TYPE_UWB,
238         RFKILL_TYPE_WIMAX,
239         RFKILL_TYPE_WWAN,
240         RFKILL_TYPE_GPS,
241         RFKILL_TYPE_FM,
242         NUM_RFKILL_TYPES,
243 } rfkill_type;
244
245 typedef struct {
246         unsigned int idx;
247         unsigned char type;
248         unsigned char op;
249         unsigned char soft;
250         unsigned char hard;
251 } rfkill_event;
252
253 typedef enum {
254         BT_COMMON = 0x00,
255         BT_HID,
256         BT_AUDIO,
257         BT_AVRCP,
258         BT_HF,
259 #ifndef GATT_DIRECT
260         BT_GATT_SERVER,
261 #endif
262
263 #ifdef TIZEN_GATT_CLIENT
264         BT_GATT_CLIENT,
265 #endif
266         BT_MAX_USER_INFO
267 } bt_user_info_type_t;
268
269 typedef struct {
270         void *cb;
271         void *user_data;
272 } bt_user_info_t;
273
274 void _bt_set_user_data(int type, void *callback, void *user_data);
275
276 void _bt_print_device_address_t(const bluetooth_device_address_t *addr);
277
278 bt_user_info_t* _bt_get_user_data(int type);
279
280 void _bt_common_event_cb(int event, int result, void *param,
281                                 void *callback, void *user_data);
282
283 void _bt_input_event_cb(int event, int result, void *param,
284                                         void *callback, void *user_data);
285
286 void _bt_headset_event_cb(int event, int result, void *param,
287                                         void *callback, void *user_data);
288
289 void _bt_a2dp_source_event_cb(int event, int result, void *param,
290                                         void *callback, void *user_data);
291
292 void _bt_hf_event_cb(int event, int result, void *param,
293                                         void *callback, void *user_data);
294
295 void _bt_avrcp_event_cb(int event, int result, void *param,
296                                         void *callback, void *user_data);
297
298 void _bt_opp_client_event_cb(int event, int result, void *param,
299                                         void *callback, void *user_data);
300
301 void _bt_map_client_event_cb(int event, int result, void *param,
302                                         void *callback, void *user_data);
303
304 #ifndef GATT_DIRECT
305 void _bt_gatt_server_event_cb(int event, int result, void *param,
306                 void *callback, void *user_data);
307 #endif
308
309 #ifdef TIZEN_GATT_CLIENT
310 void _bt_gatt_client_event_cb(int event, int result, void *param,
311                 void *callback, void *user_data);
312 #endif
313
314 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
315                                 unsigned int cod);
316
317 void _bt_convert_addr_string_to_type(unsigned char *addr, const char *address);
318
319 void _bt_convert_addr_string_to_secure_string(char *addr, const char *address);
320
321 void _bt_convert_addr_type_to_string(char *address, unsigned char *addr);
322
323 void _bt_convert_addr_type_to_secure_string(char *address, unsigned char *addr);
324
325 const char *_bt_convert_error_to_string(int error);
326
327 const char *_bt_convert_service_function_to_string(int function);
328
329 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length);
330
331 gboolean _bt_utf8_validate(char *name);
332
333 void  _bt_convert_uuid_type_to_string(char *str, const unsigned char *uuid);
334
335 typedef struct {
336         char *obj_path;
337         char *uuid;
338         gboolean authentication;
339         gboolean authorization;
340         char *role;
341
342         char *service;
343
344 } bt_register_profile_info_t;
345
346 int _bt_get_adapter_path(GDBusConnection *conn, char *path);
347 char *_bt_get_device_object_path(char *address);
348 int _bt_connect_profile(char *address, char *uuid, void *cb,
349                                                         gpointer func_data);
350 int _bt_disconnect_profile(char *address, char *uuid, void *cb,
351                                                         gpointer func_data);
352 int _bt_disconnect_ext_profile(char *address, char *path);
353
354 int _bt_cancel_discovers(char *address);
355 int _bt_discover_services(char *address, char *uuid, void *cb,
356                 gpointer func_data);
357 int _bt_discover_service_uuids(char *address, char *remote_uuid);
358 int _bt_get_cod_by_address(char *address, bluetooth_device_class_t *dev_class);
359
360 void _bt_set_le_scan_status(gboolean mode);
361
362 int _bt_register_profile(bt_register_profile_info_t *info, gboolean use_default_rfcomm);
363 int _bt_register_profile_platform(bt_register_profile_info_t *info, gboolean use_default_rfcomm);
364 int _bt_register_profile_ex(bt_register_profile_info_t *info, gboolean use_default_rfcomm, const char *name, const char *path);
365
366 void _bt_unregister_profile(char *path);
367 int __rfcomm_assign_id(void);
368 void __rfcomm_delete_id(int id);
369 void _bt_unregister_gdbus(int object_id);
370 typedef int (*bt_new_connection_cb) (const char *path, int fd,
371                                         bluetooth_device_address_t *address);
372 int _bt_register_new_conn(const char *path, bt_new_connection_cb cb);
373 int _bt_register_new_conn_ex(const char *path, const char *bus_name, bt_new_connection_cb cb);
374 void _bt_swap_addr(unsigned char *dst, const unsigned char *src);
375
376 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd);
377 int _bt_unregister_osp_server_in_agent(int type, char *uuid);
378
379 int _bt_check_privilege(int service_type, int service_function);
380
381 GDBusConnection *_bt_get_system_shared_conn(void);
382 GDBusConnection *_bt_get_system_private_conn(void);
383
384 GVariant *_bt_get_managed_objects(void);
385
386 gboolean _bt_check_enabled_internal(void);
387
388 void _bt_set_adapter_internal_status(gboolean enabled);
389
390 void _bt_convert_device_path_to_addr_type(const char *device_path,
391                                           unsigned char *addr);
392
393 void _bt_convert_device_path_to_address(const char *device_path,
394                                         char *device_address);
395
396 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
397 void _bt_rfcomm_server_free_all(void);
398
399 void _bt_rfcomm_server_disconnect_all(void);
400
401 void _bt_rfcomm_client_disconnect_all(void);
402
403 gboolean _check_uuid_path(char *path, char *uuid);
404 #endif
405
406 int _bt_hid_device_get_fd(const char *address, int *ctrl, int *intr);
407
408 int _bt_hid_new_connection(bluetooth_device_address_t *addr,
409                                 int ctrl_fd, int intr_fd);
410
411 void _bt_hid_free_hid_info(void);
412
413 int _bt_get_error_value_from_message(const char *error_message);
414
415 void _bt_hdp_app_remove_obj_info(unsigned int channe_id);
416 int _bt_hdp_app_acquire_fd(bt_hdp_connected_t *conn_info);
417
418 #ifdef __cplusplus
419 }
420 #endif /* __cplusplus */
421 #endif /*_BT_COMMON_H_*/
422