1 /*
2  * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <assert.h>
12 #include "bt_common.h"
13 #include "btc/btc_common.h"
14 #include "btc/btc_dm.h"
15 #include "btc_hf_ag.h"
16 #include "btc/btc_profile_queue.h"
17 #include "btc/btc_manage.h"
18 #include "btc/btc_util.h"
19 #include "bta/bta_ag_api.h"
20 #include "bta/bta_api.h"
21 #include "common/bt_target.h"
22 #include "common/bt_defs.h"
23 #include "device/bdaddr.h"
24 #include "esp_bt.h"
25 #include "esp_hf_ag_api.h"
26 #include "esp_err.h"
27 #include "esp_bt_main.h"
28 #include "osi/allocator.h"
29 
30 #if (BTC_HF_INCLUDED == TRUE)
esp_hf_ag_register_callback(esp_hf_cb_t callback)31 esp_err_t esp_hf_ag_register_callback(esp_hf_cb_t callback)
32 {
33     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
34         return ESP_ERR_INVALID_STATE;
35     }
36     if (callback == NULL) {
37         return ESP_FAIL;
38     }
39     btc_profile_cb_set(BTC_PID_HF, callback);
40     return ESP_OK;
41 }
42 
esp_hf_ag_init(void)43 esp_err_t esp_hf_ag_init(void)
44 {
45     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
46         return ESP_ERR_INVALID_STATE;
47     }
48     btc_msg_t msg;
49     msg.sig = BTC_SIG_API_CALL;
50     msg.pid = BTC_PID_HF;
51     msg.act = BTC_HF_INIT_EVT;
52 
53     /* Switch to BTC context */
54     bt_status_t status = btc_transfer_context(&msg, NULL, 0, NULL, NULL);
55     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
56 }
57 
esp_hf_ag_deinit(void)58 esp_err_t esp_hf_ag_deinit(void)
59 {
60     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
61         return ESP_ERR_INVALID_STATE;
62     }
63     btc_msg_t msg;
64     msg.sig = BTC_SIG_API_CALL;
65     msg.pid = BTC_PID_HF;
66     msg.act = BTC_HF_DEINIT_EVT;
67 
68     /* Switch to BTC context */
69     bt_status_t status = btc_transfer_context(&msg, NULL, 0, NULL, NULL);
70     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
71 }
72 
esp_hf_ag_slc_connect(esp_bd_addr_t remote_addr)73 esp_err_t esp_hf_ag_slc_connect(esp_bd_addr_t remote_addr)
74 {
75     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
76         return ESP_ERR_INVALID_STATE;
77     }
78     btc_msg_t msg;
79     msg.sig = BTC_SIG_API_CALL;
80     msg.pid = BTC_PID_HF;
81     msg.act = BTC_HF_CONNECT_EVT;
82 
83     btc_hf_args_t arg;
84     memset(&arg, 0, sizeof(btc_hf_args_t));
85     memcpy(&(arg.connect), remote_addr, sizeof(esp_bd_addr_t));
86 
87     /* Switch to BTC context */
88     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
89     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
90 }
91 
esp_hf_ag_slc_disconnect(esp_bd_addr_t remote_addr)92 esp_err_t esp_hf_ag_slc_disconnect(esp_bd_addr_t remote_addr)
93 {
94     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
95         return ESP_ERR_INVALID_STATE;
96     }
97     btc_msg_t msg;
98     msg.sig = BTC_SIG_API_CALL;
99     msg.pid = BTC_PID_HF;
100     msg.act = BTC_HF_DISCONNECT_EVT;
101 
102     btc_hf_args_t arg;
103     memset(&arg, 0, sizeof(btc_hf_args_t));
104     memcpy(&(arg.disconnect), remote_addr, sizeof(esp_bd_addr_t));
105 
106     /* Switch to BTC context */
107     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
108     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
109 }
110 
esp_hf_ag_audio_connect(esp_bd_addr_t remote_addr)111 esp_err_t esp_hf_ag_audio_connect(esp_bd_addr_t remote_addr)
112 {
113     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
114         return ESP_ERR_INVALID_STATE;
115     }
116     btc_msg_t msg;
117     msg.sig = BTC_SIG_API_CALL;
118     msg.pid = BTC_PID_HF;
119     msg.act = BTC_HF_CONNECT_AUDIO_EVT;
120 
121     btc_hf_args_t arg;
122     memset(&arg, 0, sizeof(btc_hf_args_t));
123     memcpy(&(arg.connect_audio), remote_addr, sizeof(esp_bd_addr_t));
124 
125     /* Switch to BTC context */
126     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
127     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
128 }
129 
esp_hf_ag_audio_disconnect(esp_bd_addr_t remote_addr)130 esp_err_t esp_hf_ag_audio_disconnect(esp_bd_addr_t remote_addr)
131 {
132     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
133         return ESP_ERR_INVALID_STATE;
134     }
135     btc_msg_t msg;
136     msg.sig = BTC_SIG_API_CALL;
137     msg.pid = BTC_PID_HF;
138     msg.act = BTC_HF_DISCONNECT_AUDIO_EVT;
139 
140     btc_hf_args_t arg;
141     memset(&arg, 0, sizeof(btc_hf_args_t));
142     memcpy(&(arg.disconnect_audio), remote_addr, sizeof(esp_bd_addr_t));
143 
144     /* Switch to BTC context */
145     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
146     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
147 }
148 
esp_hf_ag_vra_control(esp_bd_addr_t remote_addr,esp_hf_vr_state_t value)149 esp_err_t esp_hf_ag_vra_control(esp_bd_addr_t remote_addr, esp_hf_vr_state_t value)
150 {
151     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
152         return ESP_ERR_INVALID_STATE;
153     }
154     btc_msg_t msg;
155     msg.sig = BTC_SIG_API_CALL;
156     msg.pid = BTC_PID_HF;
157     msg.act = BTC_HF_VRA_EVT;
158 
159     btc_hf_args_t arg;
160     memset(&arg, 0, sizeof(btc_hf_args_t));
161     arg.vra_rep.value = value;
162     memcpy(&(arg.volcon.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
163 
164     /* Switch to BTC context */
165     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
166     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
167 }
168 
esp_hf_ag_volume_control(esp_bd_addr_t remote_addr,esp_hf_volume_control_target_t type,int volume)169 esp_err_t esp_hf_ag_volume_control(esp_bd_addr_t remote_addr, esp_hf_volume_control_target_t type, int volume)
170 {
171     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
172         return ESP_ERR_INVALID_STATE;
173     }
174     if (volume < 0 || volume > 15) {
175         return ESP_ERR_INVALID_ARG;
176     }
177     btc_msg_t msg;
178     msg.sig = BTC_SIG_API_CALL;
179     msg.pid = BTC_PID_HF;
180     msg.act = BTC_HF_VOLUME_CONTROL_EVT;
181 
182     btc_hf_args_t arg;
183     memset(&arg, 0, sizeof(btc_hf_args_t));
184     arg.volcon.target_type = type;
185     arg.volcon.volume = volume;
186     memcpy(&(arg.volcon.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
187 
188     /* Switch to BTC context */
189     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
190     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
191 }
192 
esp_hf_ag_unknown_at_send(esp_bd_addr_t remote_addr,char * unat)193 esp_err_t esp_hf_ag_unknown_at_send(esp_bd_addr_t remote_addr, char *unat)
194 {
195     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
196         return ESP_ERR_INVALID_STATE;
197     }
198     btc_msg_t msg;
199     msg.sig = BTC_SIG_API_CALL;
200     msg.pid = BTC_PID_HF;
201     msg.act = BTC_HF_UNAT_RESPONSE_EVT;
202 
203     btc_hf_args_t arg;
204     memset(&arg, 0, sizeof(btc_hf_args_t));
205     arg.unat_rep.unat = unat;
206     memcpy(&(arg.unat_rep.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
207 
208     /* Switch to BTC context */
209     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t),
210                                                 btc_hf_arg_deep_copy, btc_hf_arg_deep_free);
211     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
212 }
213 
esp_hf_ag_cmee_send(esp_bd_addr_t remote_addr,esp_hf_at_response_code_t response_code,esp_hf_cme_err_t error_code)214 esp_err_t esp_hf_ag_cmee_send(esp_bd_addr_t remote_addr, esp_hf_at_response_code_t response_code, esp_hf_cme_err_t error_code)
215 {
216     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
217         return ESP_ERR_INVALID_STATE;
218     }
219     btc_msg_t msg;
220     msg.sig = BTC_SIG_API_CALL;
221     msg.pid = BTC_PID_HF;
222     msg.act = BTC_HF_CME_ERR_EVT;
223 
224     btc_hf_args_t arg;
225     memset(&arg, 0, sizeof(btc_hf_args_t));
226     arg.ext_at.response_code = response_code;
227     arg.ext_at.error_code = error_code;
228     memcpy(&(arg.ext_at.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
229 
230     /* Switch to BTC context */
231     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
232     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
233 }
234 
esp_hf_ag_devices_status_indchange(esp_bd_addr_t remote_addr,esp_hf_call_status_t call_state,esp_hf_call_setup_status_t call_setup_state,esp_hf_network_state_t ntk_state,int signal)235 esp_err_t esp_hf_ag_devices_status_indchange(esp_bd_addr_t remote_addr,
236                                             esp_hf_call_status_t call_state,
237                                             esp_hf_call_setup_status_t call_setup_state,
238                                             esp_hf_network_state_t ntk_state, int signal)
239 {
240     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
241         return ESP_ERR_INVALID_STATE;
242     }
243     if (signal < 0 || signal > 5) {
244         return ESP_ERR_INVALID_ARG;
245     }
246     btc_msg_t msg;
247     msg.sig = BTC_SIG_API_CALL;
248     msg.pid = BTC_PID_HF;
249     msg.act = BTC_HF_IND_NOTIFICATION_EVT;
250 
251     btc_hf_args_t arg;
252     memset(&arg, 0, sizeof(btc_hf_args_t));
253     memcpy(&(arg.ind_change.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
254     arg.ind_change.call_state = call_state;
255     arg.ind_change.call_setup_state = call_setup_state;
256     arg.ind_change.ntk_state = ntk_state;
257     arg.ind_change.signal = signal;
258 
259     /* Switch to BTC context */
260     bt_status_t state = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
261     return (state == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
262 }
263 
esp_hf_ag_ciev_report(esp_bd_addr_t remote_addr,esp_hf_ciev_report_type_t ind_type,int value)264 esp_err_t esp_hf_ag_ciev_report(esp_bd_addr_t remote_addr, esp_hf_ciev_report_type_t ind_type, int value)
265 {
266     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
267         return ESP_ERR_INVALID_STATE;
268     }
269     btc_msg_t msg;
270     msg.sig = BTC_SIG_API_CALL;
271     msg.pid = BTC_PID_HF;
272     msg.act = BTC_HF_CIEV_REPORT_EVT;
273 
274     btc_hf_args_t arg;
275     memset(&arg, 0, sizeof(btc_hf_args_t));
276     memcpy(&(arg.ciev_rep.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
277     arg.ciev_rep.ind.type = ind_type;
278     arg.ciev_rep.ind.value = value;
279 
280     /* Switch to BTC context */
281     bt_status_t state = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
282     return (state == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
283 }
284 
esp_hf_ag_cind_response(esp_bd_addr_t remote_addr,esp_hf_call_status_t call_state,esp_hf_call_setup_status_t call_setup_state,esp_hf_network_state_t ntk_state,int signal,esp_hf_roaming_status_t roam,int batt_lev,esp_hf_call_held_status_t call_held_status)285 esp_err_t esp_hf_ag_cind_response(esp_bd_addr_t remote_addr,
286                                 esp_hf_call_status_t call_state,
287                                 esp_hf_call_setup_status_t call_setup_state,
288                                 esp_hf_network_state_t ntk_state, int signal, esp_hf_roaming_status_t roam, int batt_lev,
289                                 esp_hf_call_held_status_t call_held_status)
290 {
291     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
292         return ESP_ERR_INVALID_STATE;
293     }
294     if (signal < 0 || signal > 5 || batt_lev < 0 || batt_lev > 5) {
295         return ESP_ERR_INVALID_ARG;
296     }
297 
298     btc_msg_t msg;
299     msg.sig = BTC_SIG_API_CALL;
300     msg.pid = BTC_PID_HF;
301     msg.act = BTC_HF_CIND_RESPONSE_EVT;
302 
303     btc_hf_args_t arg;
304     memset(&arg, 0, sizeof(btc_hf_args_t));
305     memcpy(&(arg.cind_rep.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
306     arg.cind_rep.call_state = call_state;
307     arg.cind_rep.call_setup_state = call_setup_state;
308     arg.cind_rep.ntk_state = ntk_state;
309     arg.cind_rep.signal = signal;
310     arg.cind_rep.roam = roam;
311     arg.cind_rep.batt_lev = batt_lev;
312     arg.cind_rep.call_held_state = call_held_status;
313 
314     /* Switch to BTC context */
315     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
316     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
317 }
318 
esp_hf_ag_cops_response(esp_bd_addr_t remote_addr,char * name)319 esp_err_t esp_hf_ag_cops_response(esp_bd_addr_t remote_addr, char *name)
320 {
321     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
322         return ESP_ERR_INVALID_STATE;
323     }
324     btc_msg_t msg;
325     msg.sig = BTC_SIG_API_CALL;
326     msg.pid = BTC_PID_HF;
327     msg.act = BTC_HF_COPS_RESPONSE_EVT;
328 
329     btc_hf_args_t arg;
330     memset(&arg, 0, sizeof(btc_hf_args_t));
331     memcpy(&(arg.cops_rep.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
332     arg.cops_rep.name = name; //deep_copy
333 
334     /* Switch to BTC context */
335     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t),
336                                                 btc_hf_arg_deep_copy, btc_hf_arg_deep_free);
337     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
338 }
339 
esp_hf_ag_clcc_response(esp_bd_addr_t remote_addr,int index,esp_hf_current_call_direction_t dir,esp_hf_current_call_status_t current_call_state,esp_hf_current_call_mode_t mode,esp_hf_current_call_mpty_type_t mpty,char * number,esp_hf_call_addr_type_t type)340 esp_err_t esp_hf_ag_clcc_response(esp_bd_addr_t remote_addr, int index, esp_hf_current_call_direction_t dir,
341                                  esp_hf_current_call_status_t current_call_state, esp_hf_current_call_mode_t mode,
342                                  esp_hf_current_call_mpty_type_t mpty, char *number, esp_hf_call_addr_type_t type)
343 {
344     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
345         return ESP_ERR_INVALID_STATE;
346     }
347     btc_msg_t msg;
348     msg.sig = BTC_SIG_API_CALL;
349     msg.pid = BTC_PID_HF;
350     msg.act = BTC_HF_CLCC_RESPONSE_EVT;
351 
352     btc_hf_args_t arg;
353     memset(&arg, 0, sizeof(btc_hf_args_t));
354     memcpy(&(arg.clcc_rep.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
355     //mandatory args
356     arg.clcc_rep.index = index;
357     arg.clcc_rep.dir = dir;
358     arg.clcc_rep.current_call_state = current_call_state;
359     arg.clcc_rep.mode = mode;
360     arg.clcc_rep.mpty = mpty;
361     // option args
362     arg.clcc_rep.number = number; //deep_copy
363     arg.clcc_rep.type = type;
364 
365     /* Switch to BTC context */
366     bt_status_t stat = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t),
367                                                 btc_hf_arg_deep_copy, btc_hf_arg_deep_free);
368     return (stat == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
369 }
370 
esp_hf_ag_cnum_response(esp_bd_addr_t remote_addr,char * number,int number_type,esp_hf_subscriber_service_type_t service_type)371 esp_err_t esp_hf_ag_cnum_response(esp_bd_addr_t remote_addr, char *number, int number_type, esp_hf_subscriber_service_type_t service_type)
372 {
373     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
374         return ESP_ERR_INVALID_STATE;
375     }
376     if (number == NULL || number_type < 128 || number_type > 175) {
377         return ESP_ERR_INVALID_ARG;
378     }
379     btc_msg_t msg;
380     msg.sig = BTC_SIG_API_CALL;
381     msg.pid = BTC_PID_HF;
382     msg.act = BTC_HF_CNUM_RESPONSE_EVT;
383 
384     btc_hf_args_t arg;
385     memset(&arg, 0, sizeof(btc_hf_args_t));
386     memcpy(&(arg.cnum_rep), remote_addr, sizeof(esp_bd_addr_t));
387     arg.cnum_rep.number = number; //deep_copy
388     arg.cnum_rep.number_type = number_type;
389     arg.cnum_rep.service_type = service_type;
390 
391     /* Switch to BTC context */
392     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t),
393                                                 btc_hf_arg_deep_copy, btc_hf_arg_deep_free);
394     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
395 }
396 
esp_hf_ag_bsir(esp_bd_addr_t remote_addr,esp_hf_in_band_ring_state_t state)397 esp_err_t esp_hf_ag_bsir(esp_bd_addr_t remote_addr, esp_hf_in_band_ring_state_t state)
398 {
399     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
400         return ESP_ERR_INVALID_STATE;
401     }
402     btc_msg_t msg;
403     msg.sig = BTC_SIG_API_CALL;
404     msg.pid = BTC_PID_HF;
405     msg.act = BTC_HF_INBAND_RING_EVT;
406 
407     btc_hf_args_t arg;
408     memset(&arg, 0, sizeof(btc_hf_args_t));
409     memcpy(&(arg.bsir.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
410     arg.bsir.state = state;
411 
412     /* Switch to BTC context */
413     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
414     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
415 }
416 
esp_hf_ag_answer_call(esp_bd_addr_t remote_addr,int num_active,int num_held,esp_hf_call_status_t call_state,esp_hf_call_setup_status_t call_setup_state,char * number,esp_hf_call_addr_type_t call_addr_type)417 esp_err_t esp_hf_ag_answer_call(esp_bd_addr_t remote_addr, int num_active, int num_held,
418                                 esp_hf_call_status_t call_state,  esp_hf_call_setup_status_t call_setup_state,
419                                 char *number, esp_hf_call_addr_type_t call_addr_type)
420 {
421     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
422         return ESP_ERR_INVALID_STATE;
423     }
424     btc_msg_t msg;
425     msg.sig = BTC_SIG_API_CALL;
426     msg.pid = BTC_PID_HF;
427     msg.act = BTC_HF_AC_INCALL_EVT;
428 
429     btc_hf_args_t arg;
430     memset(&arg, 0, sizeof(btc_hf_args_t));
431     memcpy(&(arg.phone.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
432     arg.phone.num_active = num_active;
433     arg.phone.num_held = num_held;
434     arg.phone.call_state = call_state;
435     arg.phone.call_setup_state = call_setup_state;
436     arg.phone.number = number; //deep_copy
437     arg.phone.call_addr_type = call_addr_type;
438 
439     /* Switch to BTC context */
440     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t),
441                                                 btc_hf_arg_deep_copy, btc_hf_arg_deep_free);
442     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
443 }
444 
esp_hf_ag_reject_call(esp_bd_addr_t remote_addr,int num_active,int num_held,esp_hf_call_status_t call_state,esp_hf_call_setup_status_t call_setup_state,char * number,esp_hf_call_addr_type_t call_addr_type)445 esp_err_t esp_hf_ag_reject_call(esp_bd_addr_t remote_addr, int num_active, int num_held,
446                                 esp_hf_call_status_t call_state,  esp_hf_call_setup_status_t call_setup_state,
447                                 char *number, esp_hf_call_addr_type_t call_addr_type)
448 {
449     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
450         return ESP_ERR_INVALID_STATE;
451     }
452     btc_msg_t msg;
453     msg.sig = BTC_SIG_API_CALL;
454     msg.pid = BTC_PID_HF;
455     msg.act = BTC_HF_RJ_INCALL_EVT;
456 
457     btc_hf_args_t arg;
458     memset(&arg, 0, sizeof(btc_hf_args_t));
459     memcpy(&(arg.phone.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
460     arg.phone.num_active = num_active;
461     arg.phone.num_held = num_held;
462     arg.phone.call_state = call_state;
463     arg.phone.call_setup_state = call_setup_state;
464     arg.phone.number = number; //deep_copy
465     arg.phone.call_addr_type = call_addr_type;
466 
467     /* Switch to BTC context */
468     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t),
469                                                 btc_hf_arg_deep_copy, btc_hf_arg_deep_free);
470     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
471 }
472 
esp_hf_ag_end_call(esp_bd_addr_t remote_addr,int num_active,int num_held,esp_hf_call_status_t call_state,esp_hf_call_setup_status_t call_setup_state,char * number,esp_hf_call_addr_type_t call_addr_type)473 esp_err_t esp_hf_ag_end_call(esp_bd_addr_t remote_addr, int num_active, int num_held,
474                             esp_hf_call_status_t call_state,  esp_hf_call_setup_status_t call_setup_state,
475                             char *number, esp_hf_call_addr_type_t call_addr_type)
476 {
477     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
478         return ESP_ERR_INVALID_STATE;
479     }
480     btc_msg_t msg;
481     msg.sig = BTC_SIG_API_CALL;
482     msg.pid = BTC_PID_HF;
483     msg.act = BTC_HF_END_CALL_EVT;
484 
485     btc_hf_args_t arg;
486     memset(&arg, 0, sizeof(btc_hf_args_t));
487     memcpy(&(arg.phone.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
488     arg.phone.num_active = num_active;
489     arg.phone.num_held = num_held;
490     arg.phone.call_state = call_state;
491     arg.phone.call_setup_state = call_setup_state;
492     arg.phone.number = number; //deep_copy
493     arg.phone.call_addr_type = call_addr_type;
494 
495     /* Switch to BTC context */
496     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t),
497                                                 btc_hf_arg_deep_copy, btc_hf_arg_deep_free);
498     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
499 }
500 
esp_hf_ag_out_call(esp_bd_addr_t remote_addr,int num_active,int num_held,esp_hf_call_status_t call_state,esp_hf_call_setup_status_t call_setup_state,char * number,esp_hf_call_addr_type_t call_addr_type)501 esp_err_t esp_hf_ag_out_call(esp_bd_addr_t remote_addr, int num_active, int num_held,
502                             esp_hf_call_status_t call_state,  esp_hf_call_setup_status_t call_setup_state,
503                             char *number, esp_hf_call_addr_type_t call_addr_type)
504 {
505     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
506         return ESP_ERR_INVALID_STATE;
507     }
508     btc_msg_t msg;
509     msg.sig = BTC_SIG_API_CALL;
510     msg.pid = BTC_PID_HF;
511     msg.act = BTC_HF_OUT_CALL_EVT;
512 
513     btc_hf_args_t arg;
514     memset(&arg, 0, sizeof(btc_hf_args_t));
515     memcpy(&(arg.phone.remote_addr), remote_addr, sizeof(esp_bd_addr_t));
516     arg.phone.num_active = num_active;
517     arg.phone.num_held = num_held;
518     arg.phone.call_state = call_state;
519     arg.phone.call_setup_state = call_setup_state;
520     arg.phone.number = number; //deep_copy
521     arg.phone.call_addr_type = call_addr_type;
522 
523     /* Switch to BTC context */
524     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t),
525                                                 btc_hf_arg_deep_copy, btc_hf_arg_deep_free);
526     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
527 }
528 
esp_hf_ag_register_data_callback(esp_hf_incoming_data_cb_t recv,esp_hf_outgoing_data_cb_t send)529 esp_err_t esp_hf_ag_register_data_callback(esp_hf_incoming_data_cb_t recv, esp_hf_outgoing_data_cb_t send)
530 {
531     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
532         return ESP_ERR_INVALID_STATE;
533     }
534     btc_msg_t msg;
535     msg.sig = BTC_SIG_API_CALL;
536     msg.pid = BTC_PID_HF;
537     msg.act = BTC_HF_REGISTER_DATA_CALLBACK_EVT;
538 
539     btc_hf_args_t arg;
540     memset(&arg, 0, sizeof(btc_hf_args_t));
541     arg.reg_data_cb.recv = recv;
542     arg.reg_data_cb.send = send;
543 
544     /* Switch to BTC context */
545     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
546     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
547 }
548 
549 #if (BTM_SCO_HCI_INCLUDED == TRUE)
esp_hf_ag_pkt_stat_nums_get(uint16_t sync_conn_handle)550 esp_err_t esp_hf_ag_pkt_stat_nums_get(uint16_t sync_conn_handle)
551 {
552     if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
553         return ESP_ERR_INVALID_STATE;
554     }
555 
556     btc_msg_t msg;
557     msg.sig = BTC_SIG_API_CALL;
558     msg.pid = BTC_PID_HF;
559     msg.act = BTC_HF_REQUEST_PKT_STAT_EVT;
560 
561     btc_hf_args_t arg;
562     memset(&arg, 0, sizeof(btc_hf_args_t));
563     arg.pkt_sync_hd.sync_conn_handle = sync_conn_handle;
564 
565     /* Switch to BTC context */
566     bt_status_t status = btc_transfer_context(&msg, &arg, sizeof(btc_hf_args_t), NULL, NULL);
567     return (status == BT_STATUS_SUCCESS) ? ESP_OK : ESP_FAIL;
568 }
569 
esp_hf_ag_outgoing_data_ready(void)570 void esp_hf_ag_outgoing_data_ready(void)
571 {
572     btc_hf_ci_sco_data();
573 }
574 #endif /* #if (BTM_SCO_HCI_INCLUDED == TRUE) */
575 
576 #endif // BTC_HF_INCLUDED
577