mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
component/bt: Add lock for SPP slot
This commit is contained in:
parent
6acb38af4c
commit
c4cdb66947
@ -23,6 +23,12 @@
|
|||||||
#include "esp_spp_api.h"
|
#include "esp_spp_api.h"
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
|
|
||||||
|
#include "mutex.h"
|
||||||
|
#include <sys/errno.h>
|
||||||
|
#include <sys/lock.h>
|
||||||
|
#include <sys/fcntl.h>
|
||||||
|
|
||||||
|
|
||||||
#if (defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE)
|
#if (defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -44,6 +50,7 @@ typedef struct {
|
|||||||
} spp_slot_t;
|
} spp_slot_t;
|
||||||
static spp_slot_t *spp_slots[BTA_JV_MAX_RFC_SR_SESSION + 1];
|
static spp_slot_t *spp_slots[BTA_JV_MAX_RFC_SR_SESSION + 1];
|
||||||
static uint32_t spp_slot_id = 0;
|
static uint32_t spp_slot_id = 0;
|
||||||
|
static osi_mutex_t spp_slot_mutex;
|
||||||
|
|
||||||
static void spp_osi_free(void *p)
|
static void spp_osi_free(void *p)
|
||||||
{
|
{
|
||||||
@ -127,6 +134,7 @@ static void *btc_spp_rfcomm_inter_cb(tBTA_JV_EVT event, tBTA_JV *p_data, void *u
|
|||||||
void *new_user_data = NULL;
|
void *new_user_data = NULL;
|
||||||
|
|
||||||
uint32_t id = (uintptr_t)user_data;
|
uint32_t id = (uintptr_t)user_data;
|
||||||
|
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
||||||
spp_slot_t *slot, *slot_new;
|
spp_slot_t *slot, *slot_new;
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case BTA_JV_RFCOMM_SRV_OPEN_EVT:
|
case BTA_JV_RFCOMM_SRV_OPEN_EVT:
|
||||||
@ -181,6 +189,7 @@ static void *btc_spp_rfcomm_inter_cb(tBTA_JV_EVT event, tBTA_JV *p_data, void *u
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
msg.sig = BTC_SIG_API_CB;
|
msg.sig = BTC_SIG_API_CB;
|
||||||
msg.pid = BTC_PID_SPP;
|
msg.pid = BTC_PID_SPP;
|
||||||
msg.act = event;
|
msg.act = event;
|
||||||
@ -202,6 +211,7 @@ static void btc_spp_dm_inter_cb(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_d
|
|||||||
|
|
||||||
uint32_t id = (uintptr_t)user_data;
|
uint32_t id = (uintptr_t)user_data;
|
||||||
spp_slot_t *slot;
|
spp_slot_t *slot;
|
||||||
|
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case BTA_JV_GET_SCN_EVT:
|
case BTA_JV_GET_SCN_EVT:
|
||||||
slot = find_slot_by_id(id);
|
slot = find_slot_by_id(id);
|
||||||
@ -248,15 +258,21 @@ static void btc_spp_dm_inter_cb(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_d
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void btc_spp_init(void)
|
static void btc_spp_init(void)
|
||||||
{
|
{
|
||||||
BTA_JvEnable((tBTA_JV_DM_CBACK *)btc_spp_dm_inter_cb);
|
BTA_JvEnable((tBTA_JV_DM_CBACK *)btc_spp_dm_inter_cb);
|
||||||
|
if (osi_mutex_new(&spp_slot_mutex) != 0) {
|
||||||
|
LOG_ERROR("%s osi_mutex_new failed\n", __func__);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
static void btc_spp_uninit(void)
|
static void btc_spp_uninit(void)
|
||||||
{
|
{
|
||||||
|
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
||||||
for (size_t i = 1; i <= BTA_JV_MAX_RFC_SR_SESSION; i++) {
|
for (size_t i = 1; i <= BTA_JV_MAX_RFC_SR_SESSION; i++) {
|
||||||
if (spp_slots[i] != NULL && spp_slots[i]->connected) {
|
if (spp_slots[i] != NULL && spp_slots[i]->connected) {
|
||||||
BTA_JvRfcommClose(spp_slots[i]->rfc_handle, (void *)spp_slots[i]->id);
|
BTA_JvRfcommClose(spp_slots[i]->rfc_handle, (void *)spp_slots[i]->id);
|
||||||
@ -274,6 +290,8 @@ static void btc_spp_uninit(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
BTA_JvDisable();
|
BTA_JvDisable();
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
|
osi_mutex_free(&spp_slot_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void btc_spp_start_discovery(btc_spp_args_t *arg)
|
static void btc_spp_start_discovery(btc_spp_args_t *arg)
|
||||||
@ -282,9 +300,11 @@ static void btc_spp_start_discovery(btc_spp_args_t *arg)
|
|||||||
}
|
}
|
||||||
static void btc_spp_connect(btc_spp_args_t *arg)
|
static void btc_spp_connect(btc_spp_args_t *arg)
|
||||||
{
|
{
|
||||||
|
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
||||||
spp_slot_t *slot = malloc_spp_slot();
|
spp_slot_t *slot = malloc_spp_slot();
|
||||||
if (!slot) {
|
if (!slot) {
|
||||||
LOG_ERROR("%s unable to malloc RFCOMM slot!", __func__);
|
LOG_ERROR("%s unable to malloc RFCOMM slot!", __func__);
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
slot->security = arg->connect.sec_mask;
|
slot->security = arg->connect.sec_mask;
|
||||||
@ -293,24 +313,29 @@ static void btc_spp_connect(btc_spp_args_t *arg)
|
|||||||
memcpy(slot->addr, arg->connect.peer_bd_addr, ESP_BD_ADDR_LEN);
|
memcpy(slot->addr, arg->connect.peer_bd_addr, ESP_BD_ADDR_LEN);
|
||||||
BTA_JvRfcommConnect(arg->connect.sec_mask, arg->connect.role, arg->connect.remote_scn,
|
BTA_JvRfcommConnect(arg->connect.sec_mask, arg->connect.role, arg->connect.remote_scn,
|
||||||
arg->connect.peer_bd_addr, (tBTA_JV_RFCOMM_CBACK *)btc_spp_rfcomm_inter_cb, (void *)slot->id);
|
arg->connect.peer_bd_addr, (tBTA_JV_RFCOMM_CBACK *)btc_spp_rfcomm_inter_cb, (void *)slot->id);
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
}
|
}
|
||||||
static void btc_spp_disconnect(btc_spp_args_t *arg)
|
static void btc_spp_disconnect(btc_spp_args_t *arg)
|
||||||
{
|
{
|
||||||
|
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
||||||
spp_slot_t *slot = find_slot_by_handle(arg->disconnect.handle);
|
spp_slot_t *slot = find_slot_by_handle(arg->disconnect.handle);
|
||||||
if (!slot) {
|
if (!slot) {
|
||||||
LOG_ERROR("%s unable to find RFCOMM slot! disconnect fail!", __func__);
|
LOG_ERROR("%s unable to find RFCOMM slot! disconnect fail!", __func__);
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
BTA_JvRfcommClose(arg->disconnect.handle, (void *)slot->id);
|
BTA_JvRfcommClose(arg->disconnect.handle, (void *)slot->id);
|
||||||
btc_disconnect_cb(slot->rfc_handle);
|
btc_disconnect_cb(slot->rfc_handle);
|
||||||
free_spp_slot(slot);
|
free_spp_slot(slot);
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
}
|
}
|
||||||
static void btc_spp_start_srv(btc_spp_args_t *arg)
|
static void btc_spp_start_srv(btc_spp_args_t *arg)
|
||||||
{
|
{
|
||||||
|
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
||||||
spp_slot_t *slot = malloc_spp_slot();
|
spp_slot_t *slot = malloc_spp_slot();
|
||||||
if (!slot) {
|
if (!slot) {
|
||||||
LOG_ERROR("%s unable to malloc RFCOMM slot!", __func__);
|
LOG_ERROR("%s unable to malloc RFCOMM slot!", __func__);
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
slot->security = arg->start_srv.sec_mask;
|
slot->security = arg->start_srv.sec_mask;
|
||||||
@ -320,17 +345,21 @@ static void btc_spp_start_srv(btc_spp_args_t *arg)
|
|||||||
strcpy(slot->service_name, arg->start_srv.name);
|
strcpy(slot->service_name, arg->start_srv.name);
|
||||||
|
|
||||||
BTA_JvGetChannelId(BTA_JV_CONN_TYPE_RFCOMM, (void *)slot->id, arg->start_srv.local_scn);
|
BTA_JvGetChannelId(BTA_JV_CONN_TYPE_RFCOMM, (void *)slot->id, arg->start_srv.local_scn);
|
||||||
|
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void btc_spp_write(btc_spp_args_t *arg)
|
static void btc_spp_write(btc_spp_args_t *arg)
|
||||||
{
|
{
|
||||||
|
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
||||||
spp_slot_t *slot = find_slot_by_handle(arg->write.handle);
|
spp_slot_t *slot = find_slot_by_handle(arg->write.handle);
|
||||||
if (!slot) {
|
if (!slot) {
|
||||||
LOG_ERROR("%s unable to find RFCOMM slot! disconnect fail!", __func__);
|
LOG_ERROR("%s unable to find RFCOMM slot! disconnect fail!", __func__);
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
list_append(slot->list, arg->write.p_data);
|
list_append(slot->list, arg->write.p_data);
|
||||||
BTA_JvRfcommWrite(arg->write.handle, slot->id, arg->write.len, arg->write.p_data);
|
BTA_JvRfcommWrite(arg->write.handle, slot->id, arg->write.len, arg->write.p_data);
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -461,12 +490,15 @@ void btc_spp_cb_handler(btc_msg_t *msg)
|
|||||||
param.write.len = p_data->rfc_write.len;
|
param.write.len = p_data->rfc_write.len;
|
||||||
param.write.cong = p_data->rfc_write.cong;
|
param.write.cong = p_data->rfc_write.cong;
|
||||||
btc_spp_cb_to_app(ESP_SPP_WRITE_EVT, ¶m);
|
btc_spp_cb_to_app(ESP_SPP_WRITE_EVT, ¶m);
|
||||||
|
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
||||||
spp_slot_t *slot = find_slot_by_handle(p_data->rfc_write.handle);
|
spp_slot_t *slot = find_slot_by_handle(p_data->rfc_write.handle);
|
||||||
if (!slot) {
|
if (!slot) {
|
||||||
LOG_ERROR("%s unable to find RFCOMM slot! disconnect fail!", __func__);
|
LOG_ERROR("%s unable to find RFCOMM slot! disconnect fail!", __func__);
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
list_remove(slot->list, list_front(slot->list));
|
list_remove(slot->list, list_front(slot->list));
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
break;
|
break;
|
||||||
case BTA_JV_RFCOMM_CLOSE_EVT:
|
case BTA_JV_RFCOMM_CLOSE_EVT:
|
||||||
param.close.status = p_data->rfc_close.status;
|
param.close.status = p_data->rfc_close.status;
|
||||||
@ -516,9 +548,11 @@ int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf)
|
|||||||
msg.act = BTA_JV_RFCOMM_DATA_IND_EVT;
|
msg.act = BTA_JV_RFCOMM_DATA_IND_EVT;
|
||||||
|
|
||||||
uint32_t id = (uintptr_t)user_data;
|
uint32_t id = (uintptr_t)user_data;
|
||||||
|
osi_mutex_lock(&spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
||||||
spp_slot_t *slot = find_slot_by_id(id);
|
spp_slot_t *slot = find_slot_by_id(id);
|
||||||
if (!slot) {
|
if (!slot) {
|
||||||
LOG_ERROR("%s unable to find RFCOMM slot!", __func__);
|
LOG_ERROR("%s unable to find RFCOMM slot!", __func__);
|
||||||
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
p_data.data_ind.handle = slot->rfc_handle;
|
p_data.data_ind.handle = slot->rfc_handle;
|
||||||
@ -529,7 +563,7 @@ int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf)
|
|||||||
if (status != BT_STATUS_SUCCESS) {
|
if (status != BT_STATUS_SUCCESS) {
|
||||||
LOG_ERROR("%s btc_transfer_context failed\n", __func__);
|
LOG_ERROR("%s btc_transfer_context failed\n", __func__);
|
||||||
}
|
}
|
||||||
// osi_free (p_buf);
|
osi_mutex_unlock(&spp_slot_mutex);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int bta_co_rfc_data_outgoing_size(void *user_data, int *size)
|
int bta_co_rfc_data_outgoing_size(void *user_data, int *size)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user