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