Skip to content

Commit ded0b17

Browse files
lylezhu2012kartben
authored andcommitted
Bluetooth: HFP_AG: Optimize feature access
Add `BOTH_SUPT_FEAT(ag, _hf_feature, _ag_feature)` to check if the feature is supported by both side. Add `HF_SUPT_FEAT(ag, _feature)` to check if the feature is supported by HF. Add `AG_SUPT_FEAT(ag, _feature)` to check if the feature is supported by AG. Use `BOTH_SUPT_FEAT/HF_SUPT_FEAT/AG_SUPT_FEAT` to optimize code that checks whether a feature is supported. Signed-off-by: Lyle Zhu <[email protected]>
1 parent 9cda31a commit ded0b17

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

subsys/bluetooth/host/classic/hfp_ag.c

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ static struct bt_hfp_ag bt_hfp_ag_pool[CONFIG_BT_MAX_CONN];
7373

7474
static struct bt_hfp_ag_cb *bt_ag;
7575

76+
#define AG_SUPT_FEAT(ag, _feature) ((ag->ag_features & (_feature)) != 0)
77+
#define HF_SUPT_FEAT(ag, _feature) ((ag->hf_features & (_feature)) != 0)
78+
#define BOTH_SUPT_FEAT(ag, _hf_feature, _ag_feature) \
79+
(HF_SUPT_FEAT(ag, _hf_feature) && AG_SUPT_FEAT(ag, _ag_feature))
80+
7681
/* Sent but not acknowledged TX packets with a callback */
7782
static struct bt_ag_tx ag_tx[CONFIG_BT_HFP_AG_TX_BUF_COUNT * 2];
7883
static K_FIFO_DEFINE(ag_tx_free);
@@ -953,8 +958,7 @@ static int bt_hfp_ag_bac_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
953958
return -ENOTSUP;
954959
}
955960

956-
if (!(ag->hf_features & BT_HFP_HF_FEATURE_CODEC_NEG) ||
957-
!(ag->ag_features & BT_HFP_AG_FEATURE_CODEC_NEG)) {
961+
if (!BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_CODEC_NEG, BT_HFP_AG_FEATURE_CODEC_NEG)) {
958962
return -ENOEXEC;
959963
}
960964

@@ -1057,7 +1061,7 @@ static void bt_hfp_ag_set_in_band_ring(struct bt_hfp_ag *ag, void *user_data)
10571061
{
10581062
bool is_inband_ringtone;
10591063

1060-
is_inband_ringtone = (ag->ag_features & BT_HFP_AG_FEATURE_INBAND_RINGTONE) ? true : false;
1064+
is_inband_ringtone = AG_SUPT_FEAT(ag, BT_HFP_AG_FEATURE_INBAND_RINGTONE) ? true : false;
10611065

10621066
if (is_inband_ringtone && !atomic_test_bit(ag->flags, BT_HFP_AG_INBAND_RING)) {
10631067
int err = hfp_ag_send_data(ag, NULL, NULL, "\r\n+BSIR:1\r\n");
@@ -1108,8 +1112,7 @@ static int bt_hfp_ag_cmer_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
11081112

11091113
if (number == 1) {
11101114
atomic_set_bit(ag->flags, BT_HFP_AG_CMER_ENABLE);
1111-
if ((ag->hf_features & BT_HFP_HF_FEATURE_3WAY_CALL) &&
1112-
(ag->ag_features & BT_HFP_AG_FEATURE_3WAY_CALL)) {
1115+
if (BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_3WAY_CALL, BT_HFP_AG_FEATURE_3WAY_CALL)) {
11131116
LOG_DBG("Waiting for AT+CHLD=?");
11141117
return 0;
11151118
}
@@ -1513,8 +1516,7 @@ static int bt_hfp_ag_chld_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
15131516
int err;
15141517
char *response;
15151518

1516-
if (!((ag->ag_features & BT_HFP_AG_FEATURE_3WAY_CALL) &&
1517-
(ag->hf_features & BT_HFP_HF_FEATURE_3WAY_CALL))) {
1519+
if (!BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_3WAY_CALL, BT_HFP_AG_FEATURE_3WAY_CALL)) {
15181520
return -ENOEXEC;
15191521
}
15201522

@@ -1569,8 +1571,7 @@ static int bt_hfp_ag_bind_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
15691571
char *data;
15701572
uint32_t len;
15711573

1572-
if (!((ag->ag_features & BT_HFP_AG_FEATURE_HF_IND) &&
1573-
(ag->hf_features & BT_HFP_HF_FEATURE_HF_IND))) {
1574+
if (!BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_HF_IND, BT_HFP_AG_FEATURE_HF_IND)) {
15741575
return -ENOEXEC;
15751576
}
15761577

@@ -2527,8 +2528,8 @@ static int bt_hfp_ag_outgoing_call(struct bt_hfp_ag *ag, const char *number, uin
25272528

25282529
if (call_count) {
25292530
#if defined(CONFIG_BT_HFP_AG_3WAY_CALL)
2530-
if (!((ag->ag_features & BT_HFP_AG_FEATURE_3WAY_CALL) &&
2531-
(ag->hf_features & BT_HFP_HF_FEATURE_3WAY_CALL))) {
2531+
if (!BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_3WAY_CALL,
2532+
BT_HFP_AG_FEATURE_3WAY_CALL)) {
25322533
return -ENOEXEC;
25332534
}
25342535

@@ -2723,7 +2724,7 @@ static int bt_hfp_ag_nrec_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
27232724
return -ENOTSUP;
27242725
}
27252726

2726-
if (!(ag->ag_features & BT_HFP_AG_SDP_FEATURE_3WAY_CALL)) {
2727+
if (!AG_SUPT_FEAT(ag, BT_HFP_AG_FEATURE_ECNR)) {
27272728
return -ENOTSUP;
27282729
}
27292730

@@ -2883,8 +2884,7 @@ static int bt_hfp_ag_ccwa_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
28832884
int err;
28842885
uint32_t value;
28852886

2886-
if (!((ag->ag_features & BT_HFP_AG_FEATURE_3WAY_CALL) &&
2887-
(ag->hf_features & BT_HFP_HF_FEATURE_3WAY_CALL))) {
2887+
if (!BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_3WAY_CALL, BT_HFP_AG_FEATURE_3WAY_CALL)) {
28882888
return -ENOEXEC;
28892889
}
28902890

@@ -2913,8 +2913,8 @@ static void bt_hfp_ag_vr_activate(struct bt_hfp_ag *ag, void *user_data)
29132913
{
29142914
bool feature;
29152915

2916-
feature = (ag->ag_features & BT_HFP_AG_FEATURE_ENH_VOICE_RECG) &&
2917-
(ag->hf_features & BT_HFP_HF_FEATURE_ENH_VOICE_RECG);
2916+
feature = BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_ENH_VOICE_RECG,
2917+
BT_HFP_AG_FEATURE_ENH_VOICE_RECG);
29182918

29192919
#if defined(CONFIG_BT_HFP_AG_VOICE_RECG)
29202920
if (bt_ag && bt_ag->voice_recognition) {
@@ -2960,8 +2960,7 @@ static int bt_hfp_ag_bvra_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
29602960
int err;
29612961
uint32_t value;
29622962

2963-
if (!((ag->ag_features & BT_HFP_AG_FEATURE_VOICE_RECG) &&
2964-
(ag->hf_features & BT_HFP_HF_FEATURE_VOICE_RECG))) {
2963+
if (!BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_VOICE_RECG, BT_HFP_AG_FEATURE_VOICE_RECG)) {
29652964
return -ENOEXEC;
29662965
}
29672966

@@ -2995,8 +2994,8 @@ static int bt_hfp_ag_bvra_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
29952994
err = hfp_ag_next_step(ag, bt_hfp_ag_vr_activate, NULL);
29962995
break;
29972996
case BT_HFP_BVRA_READY_TO_ACCEPT:
2998-
if (!((ag->ag_features & BT_HFP_AG_FEATURE_ENH_VOICE_RECG) &&
2999-
(ag->hf_features & BT_HFP_HF_FEATURE_ENH_VOICE_RECG))) {
2997+
if (!BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_ENH_VOICE_RECG,
2998+
BT_HFP_AG_FEATURE_ENH_VOICE_RECG)) {
30002999
LOG_WRN("Enhance voice recognition is not supported");
30013000
return -ENOEXEC;
30023001
}
@@ -3021,7 +3020,7 @@ static int bt_hfp_ag_binp_handler(struct bt_hfp_ag *ag, struct net_buf *buf)
30213020
char *number = NULL;
30223021
#endif /* CONFIG_BT_HFP_AG_VOICE_TAG */
30233022

3024-
if (!(ag->ag_features & BT_HFP_AG_FEATURE_VOICE_TAG)) {
3023+
if (!AG_SUPT_FEAT(ag, BT_HFP_AG_FEATURE_VOICE_TAG)) {
30253024
return -ENOEXEC;
30263025
}
30273026

@@ -3830,8 +3829,8 @@ int bt_hfp_ag_remote_incoming(struct bt_hfp_ag *ag, const char *number)
38303829
call_count = get_none_released_calls(ag);
38313830
if (call_count) {
38323831
#if defined(CONFIG_BT_HFP_AG_3WAY_CALL)
3833-
if (!((ag->ag_features & BT_HFP_AG_FEATURE_3WAY_CALL) &&
3834-
(ag->hf_features & BT_HFP_HF_FEATURE_3WAY_CALL))) {
3832+
if (!BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_3WAY_CALL,
3833+
BT_HFP_AG_FEATURE_3WAY_CALL)) {
38353834
LOG_WRN("3 Way call feature is not supported on both sides");
38363835
return -ENOEXEC;
38373836
}
@@ -3957,7 +3956,7 @@ int bt_hfp_ag_reject(struct bt_hfp_ag_call *call)
39573956
return -EINVAL;
39583957
}
39593958

3960-
if (!(ag->ag_features & BT_HFP_AG_FEATURE_REJECT_CALL)) {
3959+
if (!AG_SUPT_FEAT(ag, BT_HFP_AG_FEATURE_REJECT_CALL)) {
39613960
LOG_ERR("AG has not ability to reject call");
39623961
return -ENOTSUP;
39633962
}
@@ -4564,7 +4563,7 @@ int bt_hfp_ag_vgm(struct bt_hfp_ag *ag, uint8_t vgm)
45644563
}
45654564
hfp_ag_unlock(ag);
45664565

4567-
if (!(ag->hf_features & BT_HFP_HF_FEATURE_VOLUME)) {
4566+
if (!HF_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_VOLUME)) {
45684567
LOG_ERR("Remote Audio Volume Control is unsupported");
45694568
return -ENOTSUP;
45704569
}
@@ -4599,7 +4598,7 @@ int bt_hfp_ag_vgs(struct bt_hfp_ag *ag, uint8_t vgs)
45994598
}
46004599
hfp_ag_unlock(ag);
46014600

4602-
if (!(ag->hf_features & BT_HFP_HF_FEATURE_VOLUME)) {
4601+
if (!HF_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_VOLUME)) {
46034602
LOG_ERR("Remote Audio Volume Control is unsupported");
46044603
return -ENOTSUP;
46054604
}
@@ -4667,8 +4666,8 @@ int bt_hfp_ag_voice_recognition(struct bt_hfp_ag *ag, bool activate)
46674666
return -ENOTSUP;
46684667
}
46694668

4670-
feature = (ag->ag_features & BT_HFP_AG_FEATURE_ENH_VOICE_RECG) &&
4671-
(ag->hf_features & BT_HFP_HF_FEATURE_ENH_VOICE_RECG);
4669+
feature = BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_ENH_VOICE_RECG,
4670+
BT_HFP_AG_FEATURE_ENH_VOICE_RECG);
46724671
if (!feature) {
46734672
bvra = "";
46744673
} else {
@@ -4713,8 +4712,8 @@ int bt_hfp_ag_vre_state(struct bt_hfp_ag *ag, uint8_t state)
47134712
return -EINVAL;
47144713
}
47154714

4716-
feature = (ag->ag_features & BT_HFP_AG_FEATURE_ENH_VOICE_RECG) &&
4717-
(ag->hf_features & BT_HFP_HF_FEATURE_ENH_VOICE_RECG);
4715+
feature = BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_ENH_VOICE_RECG,
4716+
BT_HFP_AG_FEATURE_ENH_VOICE_RECG);
47184717
if (!feature) {
47194718
return -ENOTSUP;
47204719
}
@@ -4762,14 +4761,14 @@ int bt_hfp_ag_vre_textual_representation(struct bt_hfp_ag *ag, uint8_t state, co
47624761
return -EINVAL;
47634762
}
47644763

4765-
feature = (ag->ag_features & BT_HFP_AG_FEATURE_ENH_VOICE_RECG) &&
4766-
(ag->hf_features & BT_HFP_HF_FEATURE_ENH_VOICE_RECG);
4764+
feature = BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_ENH_VOICE_RECG,
4765+
BT_HFP_AG_FEATURE_ENH_VOICE_RECG);
47674766
if (!feature) {
47684767
return -ENOTSUP;
47694768
}
47704769

4771-
feature = (ag->ag_features & BT_HFP_AG_FEATURE_VOICE_RECG_TEXT) &&
4772-
(ag->hf_features & BT_HFP_HF_FEATURE_VOICE_RECG_TEXT);
4770+
feature = BOTH_SUPT_FEAT(ag, BT_HFP_HF_FEATURE_VOICE_RECG_TEXT,
4771+
BT_HFP_AG_FEATURE_VOICE_RECG_TEXT);
47734772
if (!feature) {
47744773
return -ENOTSUP;
47754774
}

0 commit comments

Comments
 (0)