@@ -3919,6 +3919,10 @@ static void hfp_hf_disconnected(struct bt_rfcomm_dlc *dlc)
3919
3919
if (bt_hf -> disconnected ) {
3920
3920
bt_hf -> disconnected (hf );
3921
3921
}
3922
+
3923
+ k_work_cancel (& hf -> work );
3924
+ k_work_cancel_delayable (& hf -> deferred_work );
3925
+ hf -> acl = NULL ;
3922
3926
}
3923
3927
3924
3928
static void hfp_hf_recv (struct bt_rfcomm_dlc * dlc , struct net_buf * buf )
@@ -3945,60 +3949,68 @@ static void bt_hf_work(struct k_work *work)
3945
3949
hfp_hf_send_data (hf );
3946
3950
}
3947
3951
3948
- static int hfp_hf_accept (struct bt_conn * conn , struct bt_rfcomm_server * server ,
3949
- struct bt_rfcomm_dlc * * dlc )
3952
+ static struct bt_hfp_hf * hfp_hf_create (struct bt_conn * conn )
3950
3953
{
3951
- int i ;
3954
+ size_t index ;
3952
3955
static struct bt_rfcomm_dlc_ops ops = {
3953
3956
.connected = hfp_hf_connected ,
3954
3957
.disconnected = hfp_hf_disconnected ,
3955
3958
.recv = hfp_hf_recv ,
3956
3959
.sent = hfp_hf_sent ,
3957
3960
};
3961
+ struct bt_hfp_hf * hf ;
3958
3962
3959
3963
LOG_DBG ("conn %p" , conn );
3960
3964
3961
- for (i = 0 ; i < ARRAY_SIZE (bt_hfp_hf_pool ); i ++ ) {
3962
- struct bt_hfp_hf * hf = & bt_hfp_hf_pool [i ];
3963
- int j ;
3965
+ index = (size_t )bt_conn_index (conn );
3966
+ hf = & bt_hfp_hf_pool [index ];
3967
+ if (hf -> acl ) {
3968
+ LOG_ERR ("HF connection (%p) is established" , conn );
3969
+ return NULL ;
3970
+ }
3964
3971
3965
- if (hf -> rfcomm_dlc .session ) {
3966
- continue ;
3967
- }
3972
+ memset (hf , 0 , sizeof (* hf ));
3973
+
3974
+ hf -> acl = conn ;
3975
+ hf -> at .buf = hf -> hf_buffer ;
3976
+ hf -> at .buf_max_len = HF_MAX_BUF_LEN ;
3968
3977
3969
- memset (hf , 0 , sizeof (* hf ));
3978
+ hf -> rfcomm_dlc .ops = & ops ;
3979
+ hf -> rfcomm_dlc .mtu = BT_HFP_MAX_MTU ;
3970
3980
3971
- hf -> acl = conn ;
3972
- hf -> at .buf = hf -> hf_buffer ;
3973
- hf -> at .buf_max_len = HF_MAX_BUF_LEN ;
3981
+ /* Set the supported features*/
3982
+ hf -> hf_features = BT_HFP_HF_SUPPORTED_FEATURES ;
3974
3983
3975
- hf -> rfcomm_dlc . ops = & ops ;
3976
- hf -> rfcomm_dlc . mtu = BT_HFP_MAX_MTU ;
3984
+ /* Set supported codec ids */
3985
+ hf -> hf_codec_ids = BT_HFP_HF_SUPPORTED_CODEC_IDS ;
3977
3986
3978
- * dlc = & hf -> rfcomm_dlc ;
3987
+ k_fifo_init ( & hf -> tx_pending ) ;
3979
3988
3980
- /* Set the supported features*/
3981
- hf -> hf_features = BT_HFP_HF_SUPPORTED_FEATURES ;
3989
+ k_work_init (& hf -> work , bt_hf_work );
3982
3990
3983
- /* Set supported codec ids */
3984
- hf -> hf_codec_ids = BT_HFP_HF_SUPPORTED_CODEC_IDS ;
3991
+ k_work_init_delayable (& hf -> deferred_work , bt_hf_deferred_work );
3985
3992
3986
- k_fifo_init (& hf -> tx_pending );
3993
+ for (index = 0 ; index < ARRAY_SIZE (hf -> ind_table ); index ++ ) {
3994
+ hf -> ind_table [index ] = -1 ;
3995
+ }
3987
3996
3988
- k_work_init (& hf -> work , bt_hf_work );
3997
+ return hf ;
3998
+ }
3989
3999
3990
- k_work_init_delayable (& hf -> deferred_work , bt_hf_deferred_work );
4000
+ static int hfp_hf_accept (struct bt_conn * conn , struct bt_rfcomm_server * server ,
4001
+ struct bt_rfcomm_dlc * * dlc )
4002
+ {
4003
+ struct bt_hfp_hf * hf ;
3991
4004
3992
- for (j = 0 ; j < HF_MAX_AG_INDICATORS ; j ++ ) {
3993
- hf -> ind_table [j ] = -1 ;
3994
- }
4005
+ hf = hfp_hf_create (conn );
3995
4006
3996
- return 0 ;
4007
+ if (!hf ) {
4008
+ return - ECONNREFUSED ;
3997
4009
}
3998
4010
3999
- LOG_ERR ( "Unable to establish HF connection (%p)" , conn ) ;
4011
+ * dlc = & hf -> rfcomm_dlc ;
4000
4012
4001
- return - ENOMEM ;
4013
+ return 0 ;
4002
4014
}
4003
4015
4004
4016
static void hfp_hf_sco_connected (struct bt_sco_chan * chan )
@@ -4086,3 +4098,43 @@ int bt_hfp_hf_register(struct bt_hfp_hf_cb *cb)
4086
4098
4087
4099
return 0 ;
4088
4100
}
4101
+
4102
+ int bt_hfp_hf_connect (struct bt_conn * conn , struct bt_hfp_hf * * hf , uint8_t channel )
4103
+ {
4104
+ struct bt_hfp_hf * new_hf ;
4105
+ int err ;
4106
+
4107
+ if (!conn || !hf || !channel ) {
4108
+ return - EINVAL ;
4109
+ }
4110
+
4111
+ if (!bt_hf ) {
4112
+ return - EFAULT ;
4113
+ }
4114
+
4115
+ new_hf = hfp_hf_create (conn );
4116
+ if (!new_hf ) {
4117
+ return - ECONNREFUSED ;
4118
+ }
4119
+
4120
+ err = bt_rfcomm_dlc_connect (conn , & new_hf -> rfcomm_dlc , channel );
4121
+ if (err != 0 ) {
4122
+ (void )memset (new_hf , 0 , sizeof (* new_hf ));
4123
+ * hf = NULL ;
4124
+ } else {
4125
+ * hf = new_hf ;
4126
+ }
4127
+
4128
+ return err ;
4129
+ }
4130
+
4131
+ int bt_hfp_hf_disconnect (struct bt_hfp_hf * hf )
4132
+ {
4133
+ LOG_DBG ("" );
4134
+
4135
+ if (!hf ) {
4136
+ return - EINVAL ;
4137
+ }
4138
+
4139
+ return bt_rfcomm_dlc_disconnect (& hf -> rfcomm_dlc );
4140
+ }
0 commit comments