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