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