Fix osp_server unregister issue
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / include / bt-service-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_SERVICE_COMMON_H_
20 #define _BT_SERVICE_COMMON_H_
21
22 #include <sys/types.h>
23 #include <dlog.h>
24 #include <glib.h>
25 #include <gio/gio.h>
26
27 #include "bluetooth-api.h"
28 #include "bt-request-handler.h"
29 #include <oal-manager.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 #undef LOG_TAG
36 #define LOG_TAG "BLUETOOTH_FRWK_SERVICE"
37
38 #ifdef FUNCTION_TRACE
39 #define FN_START BT_DBG("[ENTER FUNC]")
40 #define FN_END BT_DBG("[EXIT FUNC]")
41 #else
42 #define FN_START
43 #define FN_END
44 #endif
45
46 #define LOG_COLOR_RESET    "\033[0m"
47 #define LOG_COLOR_RED      "\033[31m"
48 #define LOG_COLOR_YELLOW   "\033[33m"
49 #define LOG_COLOR_GREEN         "\033[32m"
50 #define LOG_COLOR_BLUE          "\033[36m"
51 #define LOG_COLOR_PURPLE   "\033[35m"
52
53 #define BT_PERMANENT_LOG(fmt, args...) _bt_permanent_log(fmt, ##args)
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_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 #ifdef TIZEN_FEATURE_BT_OBEX
69 #define INFO_SECURE(fmt, args...) SECURE_SLOGI(fmt, ##args)
70 #endif
71 #define ERR_SECURE(fmt, args...) SECURE_SLOGE(fmt, ##args)
72
73 #define ret_if(expr) \
74         do { \
75                 if (expr) { \
76                         BT_ERR("(%s) return", #expr); \
77                         return; \
78                 } \
79         } while (0)
80
81 #define retv_if(expr, val) \
82         do { \
83                 if (expr) { \
84                         BT_ERR("(%s) return", #expr); \
85                         return (val); \
86                 } \
87         } while (0)
88
89 #define BT_CHECK_PARAMETER(arg, func) \
90         do { \
91                 if (arg == NULL) { \
92                         BT_ERR("%s is NULL", #arg); \
93                         func BLUETOOTH_ERROR_INVALID_PARAM; \
94                 } \
95         } while (0)
96
97
98 #define BT_ADDRESS_LENGTH_MAX 6
99 #define BT_ADDRESS_STRING_SIZE 18
100 #define BT_RFCOMM_BUFFER_MAX 1024
101 #define BT_LOWER_ADDRESS_LENGTH 9
102 #define BT_UUID_LEN 16
103
104 #define BT_AGENT_AUTO_PAIR_BLACKLIST_FILE (APP_SYSCONFDIR"/auto-pair-blacklist")
105 #define BT_AGENT_NEW_LINE "\r\n"
106
107 #define BT_MAX_DBUS_TIMEOUT 45000
108 #ifndef TIZEN_TV
109 #define BT_ENABLE_TIMEOUT 20000 /* 20 seconds */
110 #else
111 #define BT_ENABLE_TIMEOUT 5000 /* 5 seconds */
112 #endif
113 #define BT_DISCOVERY_FINISHED_DELAY 200
114
115 #define MANAGER_EVENT_MATCH_RULE \
116                         "type='signal'," \
117                         "interface='%s'," \
118                         "member='%s'"
119
120 #define EVENT_MATCH_RULE \
121                         "type='signal'," \
122                         "interface='%s',"
123
124 #define BT_TEMINATING_WAIT_TIME 200
125
126 #define BT_TIMEOUT_MESSAGE "Did not receive a reply. Possible causes include: " \
127                         "the remote application did not send a reply, " \
128                         "the message bus security policy blocked the reply, " \
129                         "the reply timeout expired, or the network connection " \
130                         "was broken."
131
132 #define BT_BLUEZ_NAME "org.bluez"
133
134 #ifdef TIZEN_FEATURE_BT_IPSP
135 #define BT_IPSP_NAME "org.projectx.bt_ipsp"
136 #define BT_IPSP_INTERFACE "org.projectx.bt_ipsp"
137 #endif
138
139 #define BT_BLUEZ_PATH "/org/bluez"
140 #define BT_BLUEZ_HCI_PATH "/org/bluez/hci0"
141 #define BT_AGENT_NAME "org.bluez.frwk_agent"
142 #define BT_AGENT_PATH "/org/bluez/agent/frwk_agent"
143 #define BT_DEVICE_AGENT_PATH "/org/tizen/device_agent"
144 #define BT_ADAPTER_AGENT_PATH "/org/tizen/adapter_agent"
145 #define BT_MANAGER_PATH "/"
146
147
148 #define BT_MANAGER_INTERFACE "org.freedesktop.DBus.ObjectManager"
149 #define BT_ADAPTER_INTERFACE "org.bluez.Adapter1"
150 #define BT_AGENT_INTERFACE "org.bluez.Agent1"
151 #define BT_AGENT_MANAGER_INTERFACE "org.bluez.AgentManager1"
152 #define BT_DEVICE_INTERFACE "org.bluez.Device1"
153 #define BT_NETWORK_SERVER_INTERFACE "org.bluez.NetworkServer1"
154 #define BT_MEDIA_INTERFACE "org.bluez.Media1"
155 #define BT_MEDIA_PLAYER_INTERFACE "org.mpris.MediaPlayer2.Player"
156 #define BT_MEDIATRANSPORT_INTERFACE "org.bluez.MediaTransport1"
157 #define BT_MEDIA_CONTROL_INTERFACE "org.bluez.MediaControl1"
158 #define BT_PLAYER_CONTROL_INTERFACE "org.bluez.MediaPlayer1"
159 #define BT_GATT_CHAR_INTERFACE "org.bluez.GattCharacteristic1"
160
161 #define BT_INPUT_INTERFACE "org.bluez.Input1"
162 #define BT_NETWORK_INTERFACE "org.bluez.Network"
163 #define BT_NETWORK_CLIENT_INTERFACE "org.bluez.Network1"
164 #define BT_SERIAL_INTERFACE "org.bluez.Serial"
165 #define BT_SERIAL_MANAGER_INTERFACE "org.bluez.SerialProxyManager"
166 #define BT_SERIAL_PROXY_INTERFACE "org.bluez.SerialProxy"
167 #define BT_SINK_INTERFACE "org.bluez.AudioSink"
168 #define BT_AUDIO_INTERFACE "org.bluez.Audio"
169 #define BT_HEADSET_INTERFACE "org.bluez.Headset"
170 #define BT_OOB_INTERFACE "org.bluez.OutOfBand"
171 #define BT_HANDSFREE_GATEWAY_INTERFACE "org.bluez.HandsfreeGateway"
172 #define BT_OBEXD_INTERFACE "org.openobex"
173 #define BT_OBEXD_MANAGER_INTERFACE "org.openobex.Manager"
174 #define BT_OBEXD_TRANSFER_INTERFACE "org.openobex.Transfer"
175 #define BT_A2DP_SOURCE_INTERFACE "org.bluez.AudioSource"
176
177 #define BT_PROPERTIES_INTERFACE "org.freedesktop.DBus.Properties"
178
179
180 #define BT_OBEX_SERVICE_NAME "org.bluez.obex"
181 #define BT_OBEX_CLIENT_PATH "/org/bluez/obex"
182 #define BT_OBEX_CLIENT_INTERFACE "org.bluez.obex.Client1"
183 #ifdef TIZEN_FEATURE_BT_OBEX
184 #define BT_OBEX_MESSAGE_INTERFACE "org.bluez.obex.MessageAccess1"
185 #define BT_EVENT_FREEDESKTOP "org.freedesktop.DBus"
186 #endif
187
188 #define BT_OBEX_TRANSFER_INTERFACE "org.bluez.obex.Transfer1"
189 #define BT_OBEX_AGENT_INTERFACE "org.bluez.obex.Agent1"
190
191
192
193 #define BT_OBEXD_DBUS_NAME "org.bluez.obex"
194 #define BT_OBEX_OBJECT_PUSH_INTERFACE "org.bluez.obex.ObjectPush1"
195
196
197 #define BT_FREEDESKTOP_INTERFACE "org.freedesktop.DBus"
198 #define BT_FREEDESKTOP_SERVICE "org.freedesktop.DBus"
199 #define BT_FREEDESKTOP_PATH "/org/freedesktop/DBus"
200
201 #define BT_DUMP_SERVICE_NAME "org.tizen.system.dumpservice"
202 #define BT_DUMP_SERVICE_PATH "/Org/Tizen/System/DumpService"
203 #define BT_DUMP_SERVICE_INTERFACE BT_DUMP_SERVICE_NAME
204
205 #define BT_DUMP_SERVICE_SIGNAL "Dump"
206 #define BT_DUMP_SERVICE_START_SIGNAL "Start"
207 #define BT_DUMP_SERVICE_FINISH_SIGNAL "Finish"
208
209 #define BT_INTERFACES_ADDED "InterfacesAdded"
210 #define BT_INTERFACES_REMOVED "InterfacesRemoved"
211 #define BT_NAME_OWNER_CHANGED "NameOwnerChanged"
212 #define BT_PROPERTIES_CHANGED "PropertiesChanged"
213 #define BT_IPSP_STATE_CHANGE "IpspStateChanged"
214
215
216 #define BT_SESSION_BASEPATH_SERVER "/org/bluez/obex/server"
217 #define BT_SESSION_BASEPATH_CLIENT "/org/bluez/obex/client"
218
219 #define BT_SERVICE_ERR_MSG_NOT_SUPPORTED "Operation is not supported"
220
221 /* UUID */
222 #define GENERIC_AUDIO_UUID      "00001203-0000-1000-8000-00805f9b34fb"
223
224 #define OBEX_OPP_UUID           "00001105-0000-1000-8000-00805f9b34fb"
225
226 #define HSP_HS_UUID             "00001108-0000-1000-8000-00805f9b34fb"
227 #define HSP_AG_UUID             "00001112-0000-1000-8000-00805f9b34fb"
228
229 #define HFP_HS_UUID             "0000111e-0000-1000-8000-00805f9b34fb"
230 #define HFP_AG_UUID             "0000111f-0000-1000-8000-00805f9b34fb"
231
232 #define ADVANCED_AUDIO_UUID     "0000110d-0000-1000-8000-00805f9b34fb"
233
234 #define A2DP_SOURCE_UUID        "0000110a-0000-1000-8000-00805f9b34fb"
235 #define A2DP_SINK_UUID          "0000110b-0000-1000-8000-00805f9b34fb"
236
237 #define AVRCP_REMOTE_UUID       "0000110e-0000-1000-8000-00805f9b34fb"
238 #define AVRCP_TARGET_UUID       "0000110c-0000-1000-8000-00805f9b34fb"
239
240 #define HID_UUID                "00001124-0000-1000-8000-00805f9b34fb"
241 #define PNP_UUID                "00001200-0000-1000-8000-00805f9b34fb"
242 #define RFCOMM_UUID_STR         "00000003-0000-1000-8000-00805f9b34fb"
243 #define PANU_UUID               "00001115-0000-1000-8000-00805f9b34fb"
244 #define NAP_UUID                "00001116-0000-1000-8000-00805f9b34fb"
245 #define OBEX_PSE_UUID           "0000112f-0000-1000-8000-00805f9b34fb"
246 #define GATT_UUID               "00001801-0000-1000-8000-00805f9b34fb"
247
248 /* Privilege */
249 #define BT_PRIVILEGE_PUBLIC "http://tizen.org/privilege/bluetooth"
250 #define BT_PRIVILEGE_PLATFORM "http://tizen.org/privilege/bluetooth.admin"
251 #define MEDIASTORAGE_PRIVILEGE "http://tizen.org/privilege/mediastorage"
252
253 /* BD Address type */
254 #define BDADDR_BREDR           0x00
255 #define BDADDR_LE_PUBLIC       0x01
256 #define BDADDR_LE_RANDOM       0x02
257
258 /* Advertising report event types */
259 #define BT_LE_ADV_IND           0x00
260 #define BT_LE_ADV_DIRECT_IND    0x01
261 #define BT_LE_ADV_SCAN_IND              0x02
262 #define BT_LE_ADV_NONCONN_IND   0x03
263 #define BT_LE_ADV_SCAN_RSP              0x04
264
265 /* Profile states matched to btd_service_state_t of bluez service.h */
266 typedef enum {
267         BT_PROFILE_STATE_UNAVAILABLE,
268         BT_PROFILE_STATE_DISCONNECTED,
269         BT_PROFILE_STATE_CONNECTING,
270         BT_PROFILE_STATE_CONNECTED,
271         BT_PROFILE_STATE_DISCONNECTING,
272 } bt_profile_state_t;
273
274 typedef enum {
275         BT_ADV_IND_INFO = 0x00,
276         BT_SCAN_RSP_INFO = 0x01,
277 } bt_le_device_info_type_t;
278
279 typedef enum {
280         BT_PROFILE_CONN_RFCOMM = 0x01,
281         BT_PROFILE_CONN_A2DP = 0x02,
282         BT_PROFILE_CONN_HSP = 0x04,
283         BT_PROFILE_CONN_HID = 0x08,
284         BT_PROFILE_CONN_NAP = 0x10,
285         BT_PROFILE_CONN_HFG = 0x20,
286         BT_PROFILE_CONN_GATT = 0x40,
287         BT_PROGILE_CONN_NAP = 0x80,
288         BT_PROFILE_CONN_A2DP_SINK = 0x100,
289         BT_PROFILE_CONN_ALL = 0xffffffff,
290 } bt_profile_type_t;
291
292 typedef struct {
293         char *address;
294         int addr_type;
295         int rssi;
296         int adv_type;
297         bt_le_device_info_type_t dev_type;
298         int adv_data_len;
299         char *adv_data;
300 } bt_remote_le_dev_info_t;
301
302 typedef struct {
303         int rssi;
304         int class;
305         char *address;
306         char *name;
307         char *alias;
308         char **uuids;
309         unsigned int uuid_count;
310         gboolean paired;
311         bluetooth_connected_link_t connected;
312         gboolean trust;
313         gboolean is_alias_set;
314         char *manufacturer_data;
315         int manufacturer_data_len;
316         guchar addr_type;
317 #if 0 /* Should match with bt_dev_info_t in bluetooth-api.h */
318         bt_remote_le_dev_info_t le_dev_info;
319 #endif
320         int vid;
321         int pid;
322         int device_type;
323 } bt_remote_dev_info_t;
324
325 /* RFCOMM client /server will use this structure*/
326 typedef struct {
327         int fd;
328         GIOChannel *io_channel;
329         guint io_event;
330         char *dev_node;
331         char *address;
332         char *uuid;
333 } bt_rfcomm_info_t;
334
335 typedef struct {
336         int req_id;
337         char *address;
338 } bt_function_data_t;
339
340 typedef struct {
341         int count;
342         bluetooth_device_address_t *addr_list;
343         GArray *out_param;
344 } bonded_devices_req_info_t;
345
346 GDBusConnection *_bt_get_system_conn(void);
347
348 GDBusConnection *_bt_get_system_gconn(void);
349
350 GDBusConnection *_bt_get_session_gconn(void);
351
352 void *_bt_get_net_conn(void);
353
354 GDBusProxy *_bt_get_manager_proxy(void);
355
356 GDBusProxy *_bt_get_adapter_proxy(void);
357
358 #ifdef TIZEN_FEATURE_BT_IPSP
359 GDBusProxy *_bt_get_ipsp_proxy(void);
360 #endif
361 GDBusProxy *_bt_get_adapter_properties_proxy(void);
362
363 char *_bt_get_device_object_path(char *address);
364
365 char *_bt_get_profile_uuid128(bt_profile_type_t profile_type);
366
367 const char *_bt_convert_uuid_to_string(const char *uuid);
368
369 const char *_bt_convert_error_to_string(int error);
370
371 const char * _bt_convert_disc_reason_to_string(int reason);
372
373 void _bt_logging_connection(gboolean connect, int addr_type);
374
375 char *_bt_get_adapter_path(void);
376
377 void _bt_deinit_proxys(void);
378
379 void _bt_convert_device_path_to_address(const char *device_path,
380                                                 char *device_address);
381
382 void _bt_convert_addr_string_to_type(unsigned char *addr,
383                                         const char *address);
384
385 void _bt_convert_addr_type_to_string(char *address,
386                                 unsigned char *addr);
387
388 void _bt_swap_byte_ordering(char *data, int data_len);
389
390 int _bt_byte_arr_cmp(const char *data1, const char *data2, int data_len);
391
392 int _bt_byte_arr_cmp_with_mask(const char *data1, const char *data2,
393                                 const char *mask, int data_len);
394
395 gboolean _bt_compare_adddress(const bluetooth_device_address_t *addr1,
396                 const bluetooth_device_address_t *addr2);
397
398 void _bt_print_device_address_t(const bluetooth_device_address_t *addr);
399
400 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
401                                 unsigned int cod);
402
403 void _bt_free_le_device_info(bt_remote_le_dev_info_t *le_dev_info);
404
405 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length);
406
407 gboolean _bt_utf8_validate(char *name);
408
409 int _bt_register_osp_server_in_agent(const char *sender, int type, char *uuid, char *path, int fd);
410
411 int _bt_unregister_osp_server_in_agent(int type, char *uuid);
412
413 int _bt_unregister_osp_server_in_agent_by_sender(const char *sender);
414
415 void _bt_deinit_osp_server();
416
417 int _bt_set_socket_non_blocking(int socket_fd);
418
419 int _bt_set_non_blocking_tty(int sk);
420
421 void _bt_deinit_bluez_proxy(void);
422
423 int _bt_eventsystem_set_value(const char *event, const char *key, const char *value);
424
425 void _bt_copy_remote_dev(bt_remote_dev_info_t * dev_info, remote_device_t * oal_device);
426
427 void _bt_uuid_to_string(service_uuid_t *p_uuid, char *str);
428
429 void _bt_truncate_non_utf8_chars(char * str);
430
431 void _bt_copy_remote_device(bt_remote_dev_info_t *rem_dev, bluetooth_device_info_t *dev);
432
433 void _bt_service_print_dev_info(bluetooth_device_info_t *dev_info);
434
435 void _bt_free_remote_dev(bt_remote_dev_info_t * dev_info);
436
437 void _bt_free_paired_dev(void * paired_info);
438
439 void _bt_set_device_values(gboolean connected, int state);
440
441 #ifdef TIZEN_FEATURE_READOUT_NOTIFICATION
442 void _bt_set_readout_notification_of_tts(void);
443 #endif
444
445 void _bt_string_to_uuid(char *str, service_uuid_t *p_uuid);
446
447 int _bt_get_ad_data_by_type(char *in_data, int in_len,
448                 char in_type, char **data, int *data_len);
449
450 invocation_info_t* _bt_get_request_info_data(int service_function, char *address);
451
452 extern bt_plugin_info_t *headed_plugin_info;
453
454 #if defined(TIZEN_FEATURE_BT_OBEX) || defined(TIZEN_FEATURE_BT_PAN_NAP)
455 GDBusConnection *_bt_gdbus_get_system_gconn(void);
456 #endif
457
458 void _bt_copy_remote_dev_info(bt_remote_dev_info_t *dest, bt_remote_dev_info_t *src);
459
460 invocation_info_t* _bt_get_request_info_data_from_function_name(int service_function);
461
462 void _bt_permanent_log(const char *fmt, ...);
463
464 #ifdef __cplusplus
465 }
466 #endif /* __cplusplus */
467 #endif /*_BT_COMMON_H_*/
468