Modify the bt-service label from system to user
[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-glib.h>
25 #include <dbus/dbus.h>
26 #include <dlog.h>
27 #include <glib.h>
28 #include <gio/gio.h>
29
30 #include "bluetooth-api.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35
36 #undef LOG_TAG
37 #define LOG_TAG "BLUETOOTH_FRWK_API"
38
39 #ifndef BT_EXPORT_API
40 #define BT_EXPORT_API __attribute__((visibility("default")))
41 #endif
42
43 #define LOG_COLOR_RESET    "\033[0m"
44 #define LOG_COLOR_RED      "\033[31m"
45 #define LOG_COLOR_YELLOW   "\033[33m"
46 #define LOG_COLOR_GREEN         "\033[32m"
47 #define LOG_COLOR_BLUE          "\033[36m"
48 #define LOG_COLOR_PURPLE   "\033[35m"
49
50 #define BT_DBG(fmt, args...) \
51         SLOGD(fmt, ##args)
52 #define BT_INFO(fmt, args...) \
53         SLOGI(fmt, ##args)
54 #define BT_ERR(fmt, args...) \
55         SLOGE(fmt, ##args)
56
57 #define BT_DBG_UUID(uuids, len, i) \
58         BT_DBG("***UUIDs***"); \
59         for (i = 0; i < len; i++) { \
60                 BT_DBG("%s", uuids[i]); \
61         }
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                         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                         BT_ERR("BT BREDR is not enabled"); \
154                         func BLUETOOTH_ERROR_DEVICE_NOT_ENABLED; \
155                 } \
156         } while (0)
157
158 #define BT_CHECK_ENABLED_LE(func) \
159         do { \
160                 if (bluetooth_check_adapter_le() == BLUETOOTH_ADAPTER_LE_DISABLED) { \
161                         BT_ERR("BT LE is not enabled"); \
162                         func BLUETOOTH_ERROR_DEVICE_NOT_ENABLED; \
163                 } \
164         } while (0)
165
166 #define BT_CHECK_ENABLED_ANY(func) \
167         do { \
168                 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_DISABLED && \
169                         bluetooth_check_adapter_le() == BLUETOOTH_ADAPTER_LE_DISABLED) { \
170                         BT_ERR("BT is not enabled"); \
171                         func BLUETOOTH_ERROR_DEVICE_NOT_ENABLED; \
172                 } \
173         } while (0)
174
175 #define BT_ADDRESS_LENGTH_MAX 6
176 #define BT_ADDRESS_STRING_SIZE 18
177 #define BT_ADAPTER_OBJECT_PATH_MAX 50
178 #define BT_RFCOMM_BUFFER_LEN 1024
179
180 #define BT_ACCESS_DENIED_MSG "Rejected send message"
181
182 #define BT_EVENT_FREEDESKTOP "org.freedesktop.DBus"
183 #define BT_FREEDESKTOP_PATH "/org/freedesktop/DBus"
184
185 #define BT_EVENT_MANAGER "org.bluez.Manager"
186 #define BT_MANAGER_PATH "/"
187
188
189 #define BT_MANAGER_INTERFACE "org.freedesktop.DBus.ObjectManager"
190 #define BT_ADAPTER_INTERFACE "org.bluez.Adapter1"
191 #define BT_DEVICE_INTERFACE "org.bluez.Device1"
192 #define BT_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
193
194
195 #define BT_SERIAL_INTERFACE "org.bluez.Serial"
196
197 #define BT_BLUEZ_NAME "org.bluez"
198 #define BT_DBUS_NAME "org.projectx.bt"
199 #define BT_SERVICE_PATH "/org/projectx/bt_service"
200
201 #define BT_AGENT_NAME "org.bluez.frwk_agent"
202 #define BT_AGENT_PATH "/org/bluez/agent/frwk_agent"
203
204 #define BT_AGENT_INTERFACE "org.bluez.Agent1"
205
206
207 #define BT_MAX_USER_INFO 5
208 #define RFKILL_EVENT_SIZE 8
209 #define RFKILL_NODE "/dev/rfkill"
210
211 typedef enum {
212         RFKILL_TYPE_ALL = 0,
213         RFKILL_TYPE_WLAN,
214         RFKILL_TYPE_BLUETOOTH,
215         RFKILL_TYPE_UWB,
216         RFKILL_TYPE_WIMAX,
217         RFKILL_TYPE_WWAN,
218         RFKILL_TYPE_GPS,
219         RFKILL_TYPE_FM,
220         NUM_RFKILL_TYPES,
221 } rfkill_type;
222
223 typedef struct {
224         unsigned int idx;
225         unsigned char type;
226         unsigned char op;
227         unsigned char soft;
228         unsigned char hard;
229 } rfkill_event;
230
231 typedef enum {
232         BT_COMMON = 0x00,
233         BT_HID,
234         BT_AUDIO,
235         BT_AVRCP,
236         BT_HF,
237 } bt_user_info_type_t;
238
239 typedef struct {
240         void *cb;
241         void *user_data;
242 } bt_user_info_t;
243
244 void _bt_set_user_data(int type, void *callback, void *user_data);
245
246 void _bt_print_device_address_t(const bluetooth_device_address_t *addr);
247
248 bt_user_info_t* _bt_get_user_data(int type);
249
250 void _bt_common_event_cb(int event, int result, void *param,
251                                 void *callback, void *user_data);
252
253 void _bt_input_event_cb(int event, int result, void *param,
254                                         void *callback, void *user_data);
255
256 void _bt_headset_event_cb(int event, int result, void *param,
257                                         void *callback, void *user_data);
258
259 void _bt_a2dp_source_event_cb(int event, int result, void *param,
260                                         void *callback, void *user_data);
261
262 void _bt_hf_event_cb(int event, int result, void *param,
263                                         void *callback, void *user_data);
264
265 void _bt_avrcp_event_cb(int event, int result, void *param,
266                                         void *callback, void *user_data);
267
268 void _bt_opp_client_event_cb(int event, int result, void *param,
269                                         void *callback, void *user_data);
270
271 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
272                                 unsigned int cod);
273
274 void _bt_convert_addr_string_to_type(unsigned char *addr,
275                                         const char *address);
276
277 void _bt_convert_addr_type_to_string(char *address,
278                                 unsigned char *addr);
279
280 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length);
281
282 gboolean _bt_utf8_validate(char *name);
283
284 typedef struct {
285         char *obj_path;
286         char *uuid;
287         gboolean authentication;
288         gboolean authorization;
289         char *role;
290
291         char *service;
292
293 } bt_register_profile_info_t;
294
295 int _bt_get_adapter_path(GDBusConnection *conn, char *path);
296 char *_bt_get_device_object_path(char *address);
297 int _bt_connect_profile(char *address, char *uuid, void *cb,
298                                                         gpointer func_data);
299 int _bt_disconnect_profile(char *address, char *uuid, void *cb,
300                                                         gpointer func_data);
301
302 int _bt_cancel_discovers(char *address);
303 int _bt_discover_services(char *address, char *uuid, void *cb,
304                 gpointer func_data);
305 int _bt_discover_service_uuids(char *address, char *remote_uuid);
306 int _bt_get_cod_by_address(char *address, bluetooth_device_class_t *dev_class);
307
308 void _bt_set_le_scan_status(gboolean mode);
309
310 int _bt_register_profile(bt_register_profile_info_t *info, gboolean use_default_rfcomm);
311 int _bt_register_profile_platform(bt_register_profile_info_t *info, gboolean use_default_rfcomm);
312 int _bt_register_profile_ex(bt_register_profile_info_t *info, gboolean use_default_rfcomm, const char *name, const char *path);
313
314 void _bt_unregister_profile(char *path);
315 GDBusNodeInfo * _bt_get_gdbus_node(const gchar *xml_data);
316 GDBusNodeInfo * _bt_get_gdbus_node_ex(const gchar *xml_data, const char *bus_name);
317 int __rfcomm_assign_id(void);
318 void __rfcomm_delete_id(int id);
319 void _bt_unregister_gdbus(int object_id);
320 typedef int (*bt_new_connection_cb) (const char *path, int fd,
321                                         bluetooth_device_address_t *address);
322 int _bt_register_new_conn(const char *path, bt_new_connection_cb cb);
323 int _bt_register_new_conn_ex(const char *path, const char *bus_name, bt_new_connection_cb cb);
324 void _bt_swap_addr(unsigned char *dst, const unsigned char *src);
325
326 void _bt_device_path_to_address(const char *device_path, char *device_address);
327
328 GDBusConnection *g_bus_get_private_conn(void);
329
330 DBusGConnection *_bt_get_system_gconn(void);
331
332 DBusConnection *_bt_get_system_conn(void);
333
334 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd);
335 int _bt_unregister_osp_server_in_agent(int type, char *uuid);
336
337
338 int _bt_check_privilege(int service_type, int service_function);
339
340 GDBusConnection *_bt_gdbus_init_system_gconn(void);
341
342 GDBusConnection *_bt_gdbus_get_system_gconn(void);
343
344 GDBusConnection *_bt_gdbus_init_session_gconn(void);
345
346 GDBusConnection *_bt_gdbus_get_session_gconn(void);
347
348
349 GVariant *_bt_get_managed_objects(void);
350
351 void _bt_convert_device_path_to_address(const char *device_path,
352                                 char *device_address);
353
354 #ifdef RFCOMM_DIRECT
355 void _bt_rfcomm_server_free_all();
356
357 gboolean _check_uuid_path(char *path, char *uuid);
358 #endif
359
360 #ifdef __cplusplus
361 }
362 #endif /* __cplusplus */
363 #endif /*_BT_COMMON_H_*/
364