fhub: Apply lastest FHUB4.0 bluetooth-agent
[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
130 /* Audio Gateway features */
131 #define BT_AG_FEATURE_3WAY      0x1
132 #define BT_AG_FEATURE_NREC      0x0002
133 #define BT_AG_FEATURE_VOICE_RECOGNITION 0x0004
134 #define BT_AG_FEATURE_INBAND_RINGTONE   0x0008
135 #define BT_AG_FEATURE_VOICE_TAG 0x0010
136 #define BT_AG_FEATURE_REJECT_CALL       0x0020
137 #define BT_AG_FEATURE_ENHANCED_CALL_STATUS      0x0040
138 #define BT_AG_FEATURE_ENHANCED_CALL_CONTROL     0x0080
139 #define BT_AG_FEATURE_EXTENDED_RES_CODE 0x100
140 #define BT_AG_FEATURE_CODEC_NEGOTIATION 0x0200
141
142 /* SCO Codec types */
143 #define BT_HF_CODEC_ID_CVSD 1
144 #define BT_HF_CODEC_ID_MSBC 2
145
146 /* AT commands */
147 #define BT_HF_FEATURES "AT+BRSF=%d\r"     /* = 0x7F = All features supported */
148 #define BT_HF_INDICATORS_SUPP "AT+CIND=?\r"
149 #define BT_HF_INDICATORS_VAL "AT+CIND?\r"
150 #define BT_HF_INDICATORS_ENABLE "AT+CMER=3,0,0,1\r"
151 #define BT_HF_HOLD_MPTY_SUPP "AT+CHLD=?\r"
152 #define BT_HF_CALLER_IDENT_ENABLE "AT+CLIP=1\r"
153 #define BT_HF_CARRIER_FORMAT "AT+COPS=3,0\r"
154 #define BT_HF_EXTENDED_RESULT_CODE "AT+CMEE=1\r"
155 #define BT_HF_INDICATORS_ACTIVATION "AT+BIA="
156 #define BT_HF_ANSWER_CALL "ATA\r"
157 #define BT_HF_END_CALL "AT+CHUP\r"
158 #define BT_HF_REDIAL "AT+BLDN\r"
159 #define BT_HF_DIAL_NO "ATD%.100s;\r"
160 #define BT_HF_VOICE_RECOGNITION "AT+BVRA=%d\r"
161 #define BT_HF_XSAT "AT+XSAT=00,TY,WA\r"
162 #define BT_HF_BSSF "AT+BSSF=8\r"
163 #define BT_HF_CALLLIST "AT+CLCC\r"
164 #define BT_HF_AVAILABLE_CODEC "AT+BAC=%d,%d\r"
165 #define BT_HF_CODEC_SELECT "AT+BCS=%d\r"
166 #define BT_HF_SPEAKER_GAIN "AT+VGS=%d\r"
167 #define BT_HF_DTMF "AT+VTS=%s\r"
168 #define BT_HF_NREC "AT+NREC=0\r"
169 #define BT_HF_CALLWAIT_NOTI_ENABLE "AT+CCWA=1\r"
170 #define BT_HF_RELEASE_ALL "AT+CHLD=0\r"
171 #define BT_HF_RELEASE_AND_ACCEPT "AT+CHLD=1\r"
172 #define BT_HF_ACCEPT_AND_HOLD "AT+CHLD=2\r"
173 #define BT_HF_JOIN_CALL "AT+CHLD=3\r"
174
175 #define BT_HF_MAX_SPEAKER_GAIN 15
176 #define BT_HF_MIN_SPEAKER_GAIN 0
177
178 /* Hold and multipary services on AG */
179 #define BT_HF_CHLD_0    0x01 /* Releases all held calls or sets User Busy for a waiting call */
180 #define BT_HF_CHLD_1    0x02 /* Releases all active calls and accept others */
181 #define BT_HF_CHLD_1x   0x04 /* Releases specified active call only (<idx>) */
182 #define BT_HF_CHLD_2    0x08 /* Places all active calls on hold and accepts others */
183 #define BT_HF_CHLD_2x   0x10 /* Place all calls on hold EXCEPT the call indicated by <idx> */
184 #define BT_HF_CHLD_3    0x20 /* Adds a held call to the conversation */
185 #define BT_HF_CHLD_4    0x40 /* Explicit Call Transfer */
186
187 enum hfp_version {
188         HFP_VERSION_1_5 = 0x0105,
189         HFP_VERSION_1_6 = 0x0106,
190         HFP_VERSION_LATEST = HFP_VERSION_1_6,
191 };
192
193 typedef enum {
194         BT_HF_AGENT_ERROR_NONE,
195         BT_HF_AGENT_ERROR_INTERNAL,
196         BT_HF_AGENT_ERROR_NOT_AVAILABLE,
197         BT_HF_AGENT_ERROR_NOT_CONNECTED,
198         BT_HF_AGENT_ERROR_CONNECTION_FAILED,
199         BT_HF_AGENT_ERROR_BUSY,
200         BT_HF_AGENT_ERROR_INVALID_PARAM,
201         BT_HF_AGENT_ERROR_ALREADY_EXIST,
202         BT_HF_AGENT_ERROR_ALREADY_CONNECTED,
203         BT_HF_AGENT_ERROR_NO_MEMORY,
204         BT_HF_AGENT_ERROR_NO_DATA,
205         BT_HF_AGENT_ERROR_I_O_ERROR,
206         BT_HF_AGENT_ERROR_APPLICATION,
207         BT_HF_AGENT_ERROR_NOT_ALLOWED,
208         BT_HF_AGENT_ERROR_NOT_SUPPORTED,
209         BT_HF_AGENT_ERROR_INVALID_FILE_DESCRIPTOR,
210 } bt_hf_agent_error_t;
211
212 /* Extended Audio Gateway Error Result Codes */
213 typedef enum {
214         BT_AG_CME_ERROR_NONE                    = -1,
215         BT_AG_CME_ERROR_AG_FAILURE              = 0,
216         BT_AG_CME_ERROR_NO_PHONE_CONNECTION     = 1,
217         BT_AG_CME_ERROR_NOT_ALLOWED             = 3,
218         BT_AG_CME_ERROR_NOT_SUPPORTED           = 4,
219         BT_AG_CME_ERROR_PH_SIM_PIN_REQUIRED     = 5,
220         BT_AG_CME_ERROR_SIM_NOT_INSERTED        = 10,
221         BT_AG_CME_ERROR_SIM_PIN_REQUIRED        = 11,
222         BT_AG_CME_ERROR_SIM_PUK_REQUIRED        = 12,
223         BT_AG_CME_ERROR_SIM_FAILURE             = 13,
224         BT_AG_CME_ERROR_SIM_BUSY                = 14,
225         BT_AG_CME_ERROR_INCORRECT_PASSWORD      = 16,
226         BT_AG_CME_ERROR_SIM_PIN2_REQUIRED       = 17,
227         BT_AG_CME_ERROR_SIM_PUK2_REQUIRED       = 18,
228         BT_AG_CME_ERROR_MEMORY_FULL             = 20,
229         BT_AG_CME_ERROR_INVALID_INDEX           = 21,
230         BT_AG_CME_ERROR_MEMORY_FAILURE          = 23,
231         BT_AG_CME_ERROR_TEXT_STRING_TOO_LONG    = 24,
232         BT_AG_CME_ERROR_INVALID_TEXT_STRING     = 25,
233         BT_AG_CME_ERROR_DIAL_STRING_TOO_LONG    = 26,
234         BT_AG_CME_ERROR_INVALID_DIAL_STRING     = 27,
235         BT_AG_CME_ERROR_NO_NETWORK_SERVICE      = 30,
236         BT_AG_CME_ERROR_NETWORK_TIMEOUT         = 31,
237         BT_AG_CME_ERROR_NETWORK_NOT_ALLOWED     = 32,
238 } bt_ag_cme_error_t;
239
240 typedef enum {
241         BT_HF_STATE_DISCONNECTED,
242         BT_HF_STATE_CONNECTED
243 } bt_hf_state_t;
244
245 typedef enum {
246         BT_HF_AUDIO_DISCONNECTED,
247         BT_HF_AUDIO_CONNECTED
248 } bt_hf_sco_state_t;
249
250 typedef enum {
251         BT_HF_CALL_DIR_OUTGOING,
252         BT_HF_CALL_DIR_INCOMING,
253 } bt_hf_call_direction_t;
254
255 typedef enum {
256         BT_HF_CALL_STAT_ACTIVE,
257         BT_HF_CALL_STAT_HELD,
258         BT_HF_CALL_STAT_DIALING,
259         BT_HF_CALL_STAT_ALERTING,
260         BT_HF_CALL_STAT_INCOMING,
261         BT_HF_CALL_STAT_WAITING,
262 } bt_hf_call_status_t;
263
264 typedef enum {
265         BT_HF_DA_CALL_STAT_CALL_STARTED,
266         BT_HF_DA_CALL_STAT_CALL_ENDED,
267         BT_HF_DA_CALL_STAT_CALL_IDLE,
268         BT_HF_DA_CALL_STAT_CALL_TERMINATED,
269         BT_HF_DA_CALL_STAT_CALL_WAITING,
270         BT_HF_DA_CALL_STAT_CALL_SETUP_INCOMING,
271         BT_HF_DA_CALL_STAT_CALL_SETUP_DIALING,
272         BT_HF_DA_CALL_STAT_CALL_SETUP_ALERTING,
273 } bt_hf_da_call_status_t;
274
275 typedef struct {
276         guint32 fd;
277         gint sco_fd;
278         gint cli_sco_fd;
279
280         GIOChannel *io_chan;
281         GIOChannel *sco_io_chan;
282         bt_hf_state_t state;
283
284         guint watch_id;
285         guint sco_watch_id;
286         guint cli_sco_watch_id;
287
288         guint ag_features;
289         guint hold_multiparty_features;
290         GSList *indies;
291
292         gboolean is_dialing;
293         gboolean call_active;
294         gboolean inband_ringtone_support;
295         gboolean is_companion_dev;
296
297         guint ciev_call_status;
298         guint ciev_call_setup_status;
299
300         guint32 feature;
301
302         char *remote_addr;
303         int slc;
304         GSList *cmd_list;
305         GSList *cmd_send_queue;
306
307         GDBusMethodInvocation *context;
308         char *path;
309
310         void *audio_output;
311         void *audio_input;
312         bt_hf_da_call_status_t call_state;
313         gboolean is_first_audio_out;
314         gboolean is_first_audio_in;
315         gboolean is_audio_input_mute;
316 } bt_hf_agent_info_t;
317
318 typedef struct {
319         int id;
320         char at_cmd[AT_CMD_BUFF_SIZE];
321         int count;
322         GDBusMethodInvocation *context;
323         int pending;
324         int timer_id;
325 } bt_hf_agent_send_at_info;
326
327 typedef struct  {
328         const char *cmd;
329         int (*callback) (bt_hf_agent_info_t *bt_hf_info, const char *buf);
330 } bt_hf_event;
331
332 typedef struct {
333         unsigned char b[6];
334 } __attribute__((packed)) bdaddr_t;
335
336 /* Remote socket address */
337 struct sockaddr_remote {
338         sa_family_t     family;
339         bdaddr_t        remote_bdaddr;
340         uint8_t         channel;
341 };
342
343 /* SCO socket address */
344 struct sockaddr_sco {
345         sa_family_t     sco_family;
346         bdaddr_t        sco_bdaddr;
347 };
348
349 #ifdef __cplusplus
350 }
351 #endif
352 #endif /* __DEF_BT_HF_AGENT_H_ */