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