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