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