Fix memory leak
[platform/core/connectivity/bluetooth-agent.git] / hf-agent / bluetooth-hf-agent.h
1 /*
2  * Bluetooth-hf-agent
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 #ifndef __DEF_BT_HF_AGENT_H_
25 #define __DEF_BT_HF_AGENT_H_
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 #include <unistd.h>
31 #include <dlog.h>
32 #include <stdio.h>
33 #include <signal.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <inttypes.h>
38 #include <sys/socket.h>
39
40 #undef LOG_TAG
41 #define LOG_TAG "BLUETOOTH_HF_AGENT"
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 DBG(fmt, args...) SLOGD(fmt, ##args)
51 #define INFO(fmt, args...) SLOGI(fmt, ##args)
52 #define ERR(fmt, args...) SLOGE(fmt, ##args)
53 #define DBG_SECURE(fmt, args...) SECURE_SLOGD(fmt, ##args)
54 #define INFO_SECURE(fmt, args...) SECURE_SLOGI(fmt, ##args)
55 #define ERR_SECURE(fmt, args...) SECURE_SLOGE(fmt, ##args)
56 #define DBG_SECURE(fmt, args...) SECURE_SLOGD(fmt, ##args)
57 #define INFO_C(fmt, arg...) \
58         SLOGI_IF(TRUE,  LOG_COLOR_BLUE" "fmt" "LOG_COLOR_RESET, ##arg)
59 #define ERR_C(fmt, arg...) \
60         SLOGI_IF(TRUE,  LOG_COLOR_RED" "fmt" "LOG_COLOR_RESET, ##arg)
61
62 #define retv_if(expr, val) \
63         do { \
64                 if (expr) { \
65                         ERR("(%s) return", #expr); \
66                         return (val); \
67                 } \
68         } while (0)
69
70 #define BTPROTO_SCO     2
71 #define BT_HF_DATA_BUF_SIZE 1024
72 #define BT_HF_CMD_BUF_SIZE 32
73 #define BT_HF_INDICATOR_DESCR_SIZE 20
74 #define BT_HF_CALLER_NUM_SIZE 64        /* size of number + type */
75 #define BT_HF_FMT_STR_SIZE 32
76 #define BT_ADDRESS_LENGTH_MAX 6
77 #define BT_ADAPTER_OBJECT_PATH_MAX 50
78 #define AT_CMD_BUFF_SIZE 500
79 #define BT_MAX_TEL_NUM_STR 100
80
81 #define BT_HF_AGENT_ERROR (__bt_hf_agent_error_quark())
82
83 #define BT_ERROR_INTERNAL "InternalError"
84 #define BT_ERROR_NOT_AVAILABLE "NotAvailable"
85 #define BT_ERROR_NOT_CONNECTED "NotConnected"
86 #define BT_ERROR_NOT_CONNECTION_FAILED "ConnectionFailed"
87 #define BT_ERROR_BUSY "InProgress"
88 #define BT_ERROR_INVALID_PARAM "InvalidArguments"
89 #define BT_ERROR_ALREADY_EXIST "AlreadyExists"
90 #define BT_ERROR_ALREADY_CONNECTED "Already Connected"
91 #define BT_ERROR_NO_MEMORY "No memory"
92 #define BT_ERROR_NO_DATA "No data"
93 #define BT_ERROR_I_O_ERROR "I/O error"
94 #define BT_ERROR_OPERATION_NOT_AVAILABLE "Operation currently not available"
95 #define BT_ERROR_OPERATION_NOT_ALLOWED "Operation not allowed"
96 #define BT_ERROR_OPERATION_NOT_SUPPORTED "Operation not supported"
97 #define BT_ERROR_INVALID_FILE_DESCRIPTOR "Invalid File Descriptor"
98
99 #define BT_HF_OK_RESPONSE "\r\nOK\r\n"
100 #define BT_HF_ERROR_RESPONSE "ERROR"
101 #define BT_HF_SEC_ERROR_RESPONSE "SERR"
102
103 /* dbus interfaces */
104 #define BT_HF_SERVICE_NAME "org.bluez.hf_agent"
105 #define BT_HF_AGENT_OBJECT_PATH "/org/bluez/handsfree_agent"
106 #define BT_HF_SERVICE_INTERFACE "org.tizen.HfApp"
107 #define BT_HF_BLUEZ_OBJECT_PATH "/org/tizen/handsfree"
108 #define BT_HF_BLUEZ_INTERFACE   "org.bluez.HandsfreeAgent"
109 #define BT_PBAP_CLIENT_OBJECT_PATH "/org/tizen/pbap_client"
110
111 #define BLUEZ_SERVICE_NAME "org.bluez"
112 #define BLUEZ_HF_INTERFACE_NAME "org.bluez.HandsfreeGateway"
113 #define BLUEZ_PROFILE_MGMT_INTERFACE "org.bluez.ProfileManager1"
114 #define BT_MANAGER_INTERFACE "org.freedesktop.DBus.ObjectManager"
115 #define BT_ADAPTER_INTERFACE    "org.bluez.Adapter1"
116
117 #define BT_INTERFACES_ADDED "InterfacesAdded"
118 #define BT_INTERFACES_REMOVED "InterfacesRemoved"
119
120 /* Handsfree features */
121 #define BT_HF_FEATURE_EC_ANDOR_NR                       0x0001
122 #define BT_HF_FEATURE_CALL_WAITING_AND_3WAY     0x0002
123 #define BT_HF_FEATURE_CLI_PRESENTATION          0x0004
124 #define BT_HF_FEATURE_VOICE_RECOGNITION         0x0008
125 #define BT_HF_FEATURE_REMOTE_VOLUME_CONTROL     0x0010
126 #define BT_HF_FEATURE_ENHANCED_CALL_STATUS      0x0020
127 #define BT_HF_FEATURE_ENHANCED_CALL_CONTROL     0x0040
128 #define BT_HF_FEATURE_CODEC_NEGOTIATION         0x0080
129 #define BT_HF_FEATURE_HF_INDICATORS                     0x0100
130 #define BT_HF_FEATURE_ESCO_S4                           0x0200
131
132 /* Audio Gateway features */
133 #define BT_AG_FEATURE_3WAY      0x1
134 #define BT_AG_FEATURE_NREC      0x0002
135 #define BT_AG_FEATURE_VOICE_RECOGNITION 0x0004
136 #define BT_AG_FEATURE_INBAND_RINGTONE   0x0008
137 #define BT_AG_FEATURE_VOICE_TAG 0x0010
138 #define BT_AG_FEATURE_REJECT_CALL       0x0020
139 #define BT_AG_FEATURE_ENHANCED_CALL_STATUS      0x0040
140 #define BT_AG_FEATURE_ENHANCED_CALL_CONTROL     0x0080
141 #define BT_AG_FEATURE_EXTENDED_RES_CODE 0x100
142 #define BT_AG_FEATURE_CODEC_NEGOTIATION 0x0200
143
144 /* SCO Codec types */
145 #define BT_HF_CODEC_ID_CVSD 1
146 #define BT_HF_CODEC_ID_MSBC 2
147
148 /* AT commands */
149 #define BT_HF_FEATURES "AT+BRSF=%d\r"     /* = 0x7F = All features supported */
150 #define BT_HF_INDICATORS_SUPP "AT+CIND=?\r"
151 #define BT_HF_INDICATORS_VAL "AT+CIND?\r"
152 #define BT_HF_INDICATORS_ENABLE "AT+CMER=3,0,0,1\r"
153 #define BT_HF_HOLD_MPTY_SUPP "AT+CHLD=?\r"
154 #define BT_HF_CALLER_IDENT_ENABLE "AT+CLIP=1\r"
155 #define BT_HF_CARRIER_FORMAT "AT+COPS=3,0\r"
156 #define BT_HF_EXTENDED_RESULT_CODE "AT+CMEE=1\r"
157 #define BT_HF_INDICATORS_ACTIVATION "AT+BIA="
158 #define BT_HF_ANSWER_CALL "ATA\r"
159 #define BT_HF_END_CALL "AT+CHUP\r"
160 #define BT_HF_REDIAL "AT+BLDN\r"
161 #define BT_HF_DIAL_NO "ATD%.100s;\r"
162 #define BT_HF_VOICE_RECOGNITION "AT+BVRA=%d\r"
163 #define BT_HF_XSAT "AT+XSAT=00,TY,WA\r"
164 #define BT_HF_BSSF "AT+BSSF=8\r"
165 #define BT_HF_CALLLIST "AT+CLCC\r"
166 #define BT_HF_AVAILABLE_CODEC "AT+BAC=%d,%d\r"
167 #define BT_HF_CODEC_SELECT "AT+BCS=%d\r"
168 #define BT_HF_SPEAKER_GAIN "AT+VGS=%d\r"
169 #define BT_HF_DTMF "AT+VTS=%s\r"
170 #define BT_HF_NREC "AT+NREC=0\r"
171 #define BT_HF_CALLWAIT_NOTI_ENABLE "AT+CCWA=1\r"
172 #define BT_HF_RELEASE_ALL "AT+CHLD=0\r"
173 #define BT_HF_RELEASE_AND_ACCEPT "AT+CHLD=1\r"
174 #define BT_HF_ACCEPT_AND_HOLD "AT+CHLD=2\r"
175 #define BT_HF_JOIN_CALL "AT+CHLD=3\r"
176
177 #define BT_HF_MAX_SPEAKER_GAIN 15
178 #define BT_HF_MIN_SPEAKER_GAIN 0
179
180 /* Hold and multipary services on AG */
181 #define BT_HF_CHLD_0    0x01 /* Releases all held calls or sets User Busy for a waiting call */
182 #define BT_HF_CHLD_1    0x02 /* Releases all active calls and accept others */
183 #define BT_HF_CHLD_1x   0x04 /* Releases specified active call only (<idx>) */
184 #define BT_HF_CHLD_2    0x08 /* Places all active calls on hold and accepts others */
185 #define BT_HF_CHLD_2x   0x10 /* Place all calls on hold EXCEPT the call indicated by <idx> */
186 #define BT_HF_CHLD_3    0x20 /* Adds a held call to the conversation */
187 #define BT_HF_CHLD_4    0x40 /* Explicit Call Transfer */
188
189 enum hfp_version {
190         HFP_VERSION_1_5 = 0x0105,
191         HFP_VERSION_1_6 = 0x0106,
192         HFP_VERSION_1_7 = 0x0107,
193         HFP_VERSION_LATEST = HFP_VERSION_1_7,
194 };
195
196 typedef enum {
197         BT_HF_AGENT_ERROR_NONE,
198         BT_HF_AGENT_ERROR_INTERNAL,
199         BT_HF_AGENT_ERROR_NOT_AVAILABLE,
200         BT_HF_AGENT_ERROR_NOT_CONNECTED,
201         BT_HF_AGENT_ERROR_CONNECTION_FAILED,
202         BT_HF_AGENT_ERROR_BUSY,
203         BT_HF_AGENT_ERROR_INVALID_PARAM,
204         BT_HF_AGENT_ERROR_ALREADY_EXIST,
205         BT_HF_AGENT_ERROR_ALREADY_CONNECTED,
206         BT_HF_AGENT_ERROR_NO_MEMORY,
207         BT_HF_AGENT_ERROR_NO_DATA,
208         BT_HF_AGENT_ERROR_I_O_ERROR,
209         BT_HF_AGENT_ERROR_APPLICATION,
210         BT_HF_AGENT_ERROR_NOT_ALLOWED,
211         BT_HF_AGENT_ERROR_NOT_SUPPORTED,
212         BT_HF_AGENT_ERROR_INVALID_FILE_DESCRIPTOR,
213 } bt_hf_agent_error_t;
214
215 /* Extended Audio Gateway Error Result Codes */
216 typedef enum {
217         BT_AG_CME_ERROR_NONE                    = -1,
218         BT_AG_CME_ERROR_AG_FAILURE              = 0,
219         BT_AG_CME_ERROR_NO_PHONE_CONNECTION     = 1,
220         BT_AG_CME_ERROR_NOT_ALLOWED             = 3,
221         BT_AG_CME_ERROR_NOT_SUPPORTED           = 4,
222         BT_AG_CME_ERROR_PH_SIM_PIN_REQUIRED     = 5,
223         BT_AG_CME_ERROR_SIM_NOT_INSERTED        = 10,
224         BT_AG_CME_ERROR_SIM_PIN_REQUIRED        = 11,
225         BT_AG_CME_ERROR_SIM_PUK_REQUIRED        = 12,
226         BT_AG_CME_ERROR_SIM_FAILURE             = 13,
227         BT_AG_CME_ERROR_SIM_BUSY                = 14,
228         BT_AG_CME_ERROR_INCORRECT_PASSWORD      = 16,
229         BT_AG_CME_ERROR_SIM_PIN2_REQUIRED       = 17,
230         BT_AG_CME_ERROR_SIM_PUK2_REQUIRED       = 18,
231         BT_AG_CME_ERROR_MEMORY_FULL             = 20,
232         BT_AG_CME_ERROR_INVALID_INDEX           = 21,
233         BT_AG_CME_ERROR_MEMORY_FAILURE          = 23,
234         BT_AG_CME_ERROR_TEXT_STRING_TOO_LONG    = 24,
235         BT_AG_CME_ERROR_INVALID_TEXT_STRING     = 25,
236         BT_AG_CME_ERROR_DIAL_STRING_TOO_LONG    = 26,
237         BT_AG_CME_ERROR_INVALID_DIAL_STRING     = 27,
238         BT_AG_CME_ERROR_NO_NETWORK_SERVICE      = 30,
239         BT_AG_CME_ERROR_NETWORK_TIMEOUT         = 31,
240         BT_AG_CME_ERROR_NETWORK_NOT_ALLOWED     = 32,
241 } bt_ag_cme_error_t;
242
243 typedef enum {
244         BT_HF_STATE_DISCONNECTED,
245         BT_HF_STATE_CONNECTED
246 } bt_hf_state_t;
247
248 typedef enum {
249         BT_HF_AUDIO_DISCONNECTED,
250         BT_HF_AUDIO_CONNECTED
251 } bt_hf_sco_state_t;
252
253 typedef enum {
254         BT_HF_CALL_DIR_OUTGOING,
255         BT_HF_CALL_DIR_INCOMING,
256 } bt_hf_call_direction_t;
257
258 typedef enum {
259         BT_HF_CALL_STAT_ACTIVE,
260         BT_HF_CALL_STAT_HELD,
261         BT_HF_CALL_STAT_DIALING,
262         BT_HF_CALL_STAT_ALERTING,
263         BT_HF_CALL_STAT_INCOMING,
264         BT_HF_CALL_STAT_WAITING,
265 } bt_hf_call_status_t;
266
267 typedef enum {
268         BT_HF_DA_CALL_STAT_CALL_STARTED,
269         BT_HF_DA_CALL_STAT_CALL_ENDED,
270         BT_HF_DA_CALL_STAT_CALL_IDLE,
271         BT_HF_DA_CALL_STAT_CALL_TERMINATED,
272         BT_HF_DA_CALL_STAT_CALL_WAITING,
273         BT_HF_DA_CALL_STAT_CALL_SETUP_INCOMING,
274         BT_HF_DA_CALL_STAT_CALL_SETUP_DIALING,
275         BT_HF_DA_CALL_STAT_CALL_SETUP_ALERTING,
276 } bt_hf_da_call_status_t;
277
278 typedef struct {
279         guint32 fd;
280         gint sco_fd;
281         gint cli_sco_fd;
282
283         GIOChannel *io_chan;
284         GIOChannel *sco_io_chan;
285         bt_hf_state_t state;
286
287         guint watch_id;
288         guint sco_watch_id;
289         guint cli_sco_watch_id;
290
291         guint ag_features;
292         guint hold_multiparty_features;
293         GSList *indies;
294
295         gboolean is_dialing;
296         gboolean call_active;
297         gboolean inband_ringtone_support;
298         gboolean is_companion_dev;
299
300         guint ciev_call_status;
301         guint ciev_call_setup_status;
302
303         guint32 feature;
304
305         char *remote_addr;
306         int slc;
307         GSList *cmd_list;
308         GSList *cmd_send_queue;
309
310         GDBusMethodInvocation *context;
311         char *path;
312
313         void *audio_output;
314         void *audio_input;
315         bt_hf_da_call_status_t call_state;
316         gboolean is_first_audio_out;
317         gboolean is_first_audio_in;
318         gboolean is_audio_input_mute;
319 } bt_hf_agent_info_t;
320
321 typedef struct {
322         int id;
323         char at_cmd[AT_CMD_BUFF_SIZE];
324         int count;
325         GDBusMethodInvocation *context;
326         int pending;
327         int timer_id;
328 } bt_hf_agent_send_at_info;
329
330 typedef struct  {
331         const char *cmd;
332         int (*callback) (bt_hf_agent_info_t *bt_hf_info, const char *buf);
333 } bt_hf_event;
334
335 typedef struct {
336         unsigned char b[6];
337 } __attribute__((packed)) bdaddr_t;
338
339 /* Remote socket address */
340 struct sockaddr_remote {
341         sa_family_t     family;
342         bdaddr_t        remote_bdaddr;
343         uint8_t         channel;
344 };
345
346 /* SCO socket address */
347 struct sockaddr_sco {
348         sa_family_t     sco_family;
349         bdaddr_t        sco_bdaddr;
350 };
351
352 #ifdef __cplusplus
353 }
354 #endif
355 #endif /* __DEF_BT_HF_AGENT_H_ */