2018-01-05 03:48:29 -05:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "btc_spp.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "btc/btc_manage.h"
|
|
|
|
#include "btc/btc_task.h"
|
|
|
|
#include "bta/bta_jv_api.h"
|
|
|
|
#include "common/bt_trace.h"
|
|
|
|
#include "osi/allocator.h"
|
2018-01-05 03:48:29 -05:00
|
|
|
#include "esp_spp_api.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "osi/list.h"
|
2018-04-27 23:36:22 -04:00
|
|
|
#include "freertos/ringbuf.h"
|
2018-04-08 00:10:50 -04:00
|
|
|
#include "osi/mutex.h"
|
2018-03-02 05:11:47 -05:00
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/fcntl.h>
|
2018-04-27 23:36:22 -04:00
|
|
|
#include "esp_vfs.h"
|
|
|
|
#include "esp_vfs_dev.h"
|
2018-03-02 05:11:47 -05:00
|
|
|
|
2018-01-05 03:48:29 -05:00
|
|
|
#if (defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE)
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t serial;
|
|
|
|
bool connected;
|
|
|
|
uint8_t scn;
|
|
|
|
uint8_t max_session;
|
2018-04-27 23:36:22 -04:00
|
|
|
RingbufHandle_t ringbuf_read;
|
|
|
|
RingbufHandle_t ringbuf_write;
|
2018-01-05 03:48:29 -05:00
|
|
|
uint32_t id;
|
|
|
|
uint32_t mtu;//unused
|
|
|
|
uint32_t sdp_handle;
|
|
|
|
uint32_t rfc_handle;
|
|
|
|
uint32_t rfc_port_handle;
|
2018-04-27 23:36:22 -04:00
|
|
|
int fd;
|
|
|
|
uint8_t *write_data;
|
2018-01-05 03:48:29 -05:00
|
|
|
esp_spp_role_t role;
|
|
|
|
esp_spp_sec_t security;
|
|
|
|
esp_bd_addr_t addr;
|
|
|
|
list_t *list;
|
2018-04-27 23:36:22 -04:00
|
|
|
list_t *incoming_list;
|
2018-01-05 03:48:29 -05:00
|
|
|
uint8_t service_uuid[16];
|
2018-10-30 22:48:47 -04:00
|
|
|
char service_name[ESP_SPP_SERVER_NAME_MAX + 1];
|
2018-01-05 03:48:29 -05:00
|
|
|
} spp_slot_t;
|
2018-04-27 23:36:22 -04:00
|
|
|
|
|
|
|
static struct spp_local_param_t {
|
|
|
|
spp_slot_t *spp_slots[BTA_JV_MAX_RFC_SR_SESSION + 1];
|
|
|
|
uint32_t spp_slot_id;
|
|
|
|
esp_spp_mode_t spp_mode;
|
|
|
|
osi_mutex_t spp_slot_mutex;
|
2018-05-11 09:56:53 -04:00
|
|
|
esp_vfs_id_t spp_vfs_id;
|
2018-04-27 23:36:22 -04:00
|
|
|
} spp_local_param;
|
2018-01-05 03:48:29 -05:00
|
|
|
|
|
|
|
static void spp_osi_free(void *p)
|
|
|
|
{
|
|
|
|
osi_free(p);
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
|
2018-05-11 09:56:53 -04:00
|
|
|
static spp_slot_t *spp_malloc_slot(void)
|
2018-01-05 03:48:29 -05:00
|
|
|
{
|
2018-04-27 23:36:22 -04:00
|
|
|
if (++spp_local_param.spp_slot_id == 0) {
|
|
|
|
spp_local_param.spp_slot_id = 1;
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
for (size_t i = 1; i <= BTA_JV_MAX_RFC_SR_SESSION; i++) {
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_slots[i] == NULL) {
|
|
|
|
spp_local_param.spp_slots[i] = (spp_slot_t *)osi_malloc(sizeof(spp_slot_t));
|
|
|
|
if (!spp_local_param.spp_slots[i]) {
|
2018-01-05 03:48:29 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
spp_local_param.spp_slots[i]->id = spp_local_param.spp_slot_id;
|
|
|
|
spp_local_param.spp_slots[i]->serial = i;
|
|
|
|
spp_local_param.spp_slots[i]->connected = FALSE;
|
|
|
|
spp_local_param.spp_slots[i]->write_data = NULL;
|
|
|
|
spp_local_param.spp_slots[i]->list = list_new(spp_osi_free);
|
|
|
|
if (spp_local_param.spp_mode == ESP_SPP_MODE_VFS) {
|
|
|
|
spp_local_param.spp_slots[i]->incoming_list = list_new(spp_osi_free);
|
2018-05-11 09:56:53 -04:00
|
|
|
if (esp_vfs_register_fd(spp_local_param.spp_vfs_id, &spp_local_param.spp_slots[i]->fd) != ESP_OK) {
|
|
|
|
osi_free(spp_local_param.spp_slots[i]);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
spp_local_param.spp_slots[i]->ringbuf_read = xRingbufferCreate(ESP_SPP_RINGBUF_SIZE, RINGBUF_TYPE_BYTEBUF);
|
|
|
|
spp_local_param.spp_slots[i]->ringbuf_write = xRingbufferCreate(ESP_SPP_RINGBUF_SIZE, RINGBUF_TYPE_BYTEBUF);
|
|
|
|
}
|
|
|
|
return spp_local_param.spp_slots[i];
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
|
2018-05-11 09:56:53 -04:00
|
|
|
static spp_slot_t *spp_find_slot_by_id(uint32_t id)
|
2018-01-05 03:48:29 -05:00
|
|
|
{
|
|
|
|
for (size_t i = 1; i <= BTA_JV_MAX_RFC_SR_SESSION; i++) {
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_slots[i] != NULL && spp_local_param.spp_slots[i]->id == id) {
|
|
|
|
return spp_local_param.spp_slots[i];
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
|
2018-05-11 09:56:53 -04:00
|
|
|
static spp_slot_t *spp_find_slot_by_handle(uint32_t handle)
|
2018-01-05 03:48:29 -05:00
|
|
|
{
|
|
|
|
for (size_t i = 1; i <= BTA_JV_MAX_RFC_SR_SESSION; i++) {
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_slots[i] != NULL && spp_local_param.spp_slots[i]->rfc_handle == handle) {
|
|
|
|
return spp_local_param.spp_slots[i];
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
|
2018-05-11 09:56:53 -04:00
|
|
|
static spp_slot_t *spp_find_slot_by_fd(int fd)
|
2018-04-27 23:36:22 -04:00
|
|
|
{
|
|
|
|
for (size_t i = 1; i <= BTA_JV_MAX_RFC_SR_SESSION; i++) {
|
|
|
|
if (spp_local_param.spp_slots[i] != NULL && spp_local_param.spp_slots[i]->fd == fd) {
|
|
|
|
return spp_local_param.spp_slots[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-11 09:56:53 -04:00
|
|
|
static void spp_free_slot(spp_slot_t *slot)
|
2018-01-05 03:48:29 -05:00
|
|
|
{
|
|
|
|
if (!slot) {
|
|
|
|
return;
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
spp_local_param.spp_slots[slot->serial] = NULL;
|
2018-01-05 03:48:29 -05:00
|
|
|
list_free(slot->list);
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_mode == ESP_SPP_MODE_VFS) {
|
2018-05-11 09:56:53 -04:00
|
|
|
(void) esp_vfs_unregister_fd(spp_local_param.spp_vfs_id, slot->fd);
|
2018-04-27 23:36:22 -04:00
|
|
|
list_free(slot->incoming_list);
|
|
|
|
vRingbufferDelete(slot->ringbuf_read);
|
|
|
|
vRingbufferDelete(slot->ringbuf_write);
|
|
|
|
}
|
2018-01-05 03:48:29 -05:00
|
|
|
osi_free(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void btc_spp_cb_to_app(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
|
|
|
|
{
|
|
|
|
esp_spp_cb_t *btc_spp_cb = (esp_spp_cb_t *)btc_profile_cb_get(BTC_PID_SPP);
|
|
|
|
if (btc_spp_cb) {
|
|
|
|
btc_spp_cb(event, param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void btc_create_server_fail_cb(void)
|
|
|
|
{
|
|
|
|
esp_spp_cb_param_t param;
|
|
|
|
param.start.status = ESP_SPP_FAILURE;
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_START_EVT, ¶m);
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
|
2018-01-05 03:48:29 -05:00
|
|
|
static void btc_disconnect_cb(uint32_t handle)
|
|
|
|
{
|
|
|
|
esp_spp_cb_param_t param;
|
|
|
|
param.close.status = ESP_SPP_SUCCESS;
|
|
|
|
param.close.handle = handle;
|
|
|
|
param.close.async = FALSE;
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_CLOSE_EVT, ¶m);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *btc_spp_rfcomm_inter_cb(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data)
|
|
|
|
{
|
|
|
|
bt_status_t status;
|
|
|
|
btc_msg_t msg;
|
|
|
|
void *new_user_data = NULL;
|
|
|
|
|
|
|
|
uint32_t id = (uintptr_t)user_data;
|
|
|
|
spp_slot_t *slot, *slot_new;
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-01-05 03:48:29 -05:00
|
|
|
switch (event) {
|
|
|
|
case BTA_JV_RFCOMM_SRV_OPEN_EVT:
|
2018-05-11 09:56:53 -04:00
|
|
|
slot = spp_find_slot_by_id(id);
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
}
|
2018-05-11 09:56:53 -04:00
|
|
|
slot_new = spp_malloc_slot();
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot_new) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to malloc RFCOMM slot!", __func__);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
new_user_data = (void *)(uintptr_t)slot_new->id;
|
|
|
|
slot_new->security = slot->security;
|
|
|
|
slot_new->role = slot->role;
|
|
|
|
slot_new->scn = slot->scn;;
|
|
|
|
slot_new->max_session = slot->max_session;
|
|
|
|
strcpy(slot_new->service_name, slot->service_name);
|
|
|
|
slot_new->sdp_handle = slot->sdp_handle;
|
|
|
|
slot_new->rfc_handle = p_data->rfc_srv_open.new_listen_handle;
|
|
|
|
slot_new->rfc_port_handle = BTA_JvRfcommGetPortHdl(p_data->rfc_srv_open.new_listen_handle);
|
|
|
|
|
|
|
|
memcpy(slot->addr, p_data->rfc_srv_open.rem_bda, ESP_BD_ADDR_LEN);
|
|
|
|
slot->connected = TRUE;
|
|
|
|
slot->rfc_handle = p_data->rfc_srv_open.handle;
|
|
|
|
slot->rfc_port_handle = BTA_JvRfcommGetPortHdl(p_data->rfc_srv_open.handle);
|
2018-10-11 23:12:19 -04:00
|
|
|
BTA_JvSetPmProfile(p_data->rfc_srv_open.handle, BTA_JV_PM_ALL, BTA_JV_CONN_OPEN);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
case BTA_JV_RFCOMM_OPEN_EVT:
|
2018-05-11 09:56:53 -04:00
|
|
|
slot = spp_find_slot_by_id(id);
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
slot->connected = TRUE;
|
|
|
|
slot->rfc_handle = p_data->rfc_open.handle;
|
|
|
|
slot->rfc_port_handle = BTA_JvRfcommGetPortHdl(p_data->rfc_open.handle);
|
2018-10-11 23:12:19 -04:00
|
|
|
BTA_JvSetPmProfile(p_data->rfc_open.handle, BTA_JV_PM_ID_1, BTA_JV_CONN_OPEN);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
case BTA_JV_RFCOMM_CLOSE_EVT:
|
2018-05-11 09:56:53 -04:00
|
|
|
slot = spp_find_slot_by_id(id);
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (slot->connected) {
|
|
|
|
BTA_JvRfcommClose(slot->rfc_handle, (void *)slot->id);
|
|
|
|
}
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_free_slot(slot);
|
2018-03-08 22:46:30 -05:00
|
|
|
p_data->rfc_close.status = BTA_JV_SUCCESS;
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
case BTA_JV_RFCOMM_DATA_IND_EVT:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_SPP;
|
|
|
|
msg.act = event;
|
|
|
|
|
|
|
|
|
|
|
|
status = btc_transfer_context(&msg, p_data,
|
|
|
|
sizeof(tBTA_JV), NULL);
|
|
|
|
|
|
|
|
if (status != BT_STATUS_SUCCESS) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s btc_transfer_context failed", __func__);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
return new_user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void btc_spp_dm_inter_cb(tBTA_JV_EVT event, tBTA_JV *p_data, void *user_data)
|
|
|
|
{
|
|
|
|
bt_status_t status;
|
|
|
|
btc_msg_t msg;
|
|
|
|
|
|
|
|
uint32_t id = (uintptr_t)user_data;
|
|
|
|
spp_slot_t *slot;
|
|
|
|
switch (event) {
|
|
|
|
case BTA_JV_GET_SCN_EVT:
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
slot = spp_find_slot_by_id(id);
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p_data->scn == 0) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to get scn, start server fail!", __func__);
|
2018-01-05 03:48:29 -05:00
|
|
|
btc_create_server_fail_cb();
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_free_slot(slot);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
slot->scn = p_data->scn;
|
|
|
|
BTA_JvCreateRecordByUser(slot->service_name, slot->scn, (void *)slot->id);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
case BTA_JV_CREATE_RECORD_EVT:
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
slot = spp_find_slot_by_id(id);
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p_data->create_rec.status == BTA_JV_SUCCESS) {
|
|
|
|
slot->sdp_handle = p_data->create_rec.handle;
|
|
|
|
BTA_JvRfcommStartServer(slot->security, slot->role, slot->scn,
|
|
|
|
slot->max_session, (tBTA_JV_RFCOMM_CBACK *)btc_spp_rfcomm_inter_cb, (void *)slot->id);
|
|
|
|
} else {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to create record, start server fail!", __func__);
|
2018-01-05 03:48:29 -05:00
|
|
|
btc_create_server_fail_cb();
|
|
|
|
BTA_JvFreeChannel(slot->scn, BTA_JV_CONN_TYPE_RFCOMM);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_free_slot(slot);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_SPP;
|
|
|
|
msg.act = event;
|
|
|
|
|
|
|
|
status = btc_transfer_context(&msg, p_data, sizeof(tBTA_JV), NULL);
|
|
|
|
|
|
|
|
if (status != BT_STATUS_SUCCESS) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:36:22 -04:00
|
|
|
static void btc_spp_init(btc_spp_args_t *arg)
|
2018-01-05 03:48:29 -05:00
|
|
|
{
|
2018-04-27 23:36:22 -04:00
|
|
|
if (osi_mutex_new(&spp_local_param.spp_slot_mutex) != 0) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s osi_mutex_new failed\n", __func__);
|
2018-03-02 05:11:47 -05:00
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
spp_local_param.spp_mode = arg->init.mode;
|
|
|
|
spp_local_param.spp_slot_id = 0;
|
2018-03-06 01:57:08 -05:00
|
|
|
BTA_JvEnable((tBTA_JV_DM_CBACK *)btc_spp_dm_inter_cb);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
|
2018-01-05 03:48:29 -05:00
|
|
|
static void btc_spp_uninit(void)
|
|
|
|
{
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-01-05 03:48:29 -05:00
|
|
|
for (size_t i = 1; i <= BTA_JV_MAX_RFC_SR_SESSION; i++) {
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_slots[i] != NULL && spp_local_param.spp_slots[i]->connected) {
|
|
|
|
BTA_JvRfcommClose(spp_local_param.spp_slots[i]->rfc_handle, (void *)spp_local_param.spp_slots[i]->id);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_free_slot(spp_local_param.spp_slots[i]);
|
2018-04-27 23:36:22 -04:00
|
|
|
spp_local_param.spp_slots[i] = NULL;
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (size_t i = 1; i <= BTA_JV_MAX_RFC_SR_SESSION; i++) {
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_slots[i] != NULL && !(spp_local_param.spp_slots[i]->connected)) {
|
2018-05-10 04:16:25 -04:00
|
|
|
BTA_JvRfcommStopServer(spp_local_param.spp_slots[i]->sdp_handle, (void *)spp_local_param.spp_slots[i]->id);
|
2018-04-27 23:36:22 -04:00
|
|
|
BTA_JvDeleteRecord(spp_local_param.spp_slots[i]->sdp_handle);
|
|
|
|
BTA_JvFreeChannel(spp_local_param.spp_slots[i]->scn, BTA_JV_CONN_TYPE_RFCOMM);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_free_slot(spp_local_param.spp_slots[i]);
|
2018-04-27 23:36:22 -04:00
|
|
|
spp_local_param.spp_slots[i] = NULL;
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BTA_JvDisable();
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
osi_mutex_free(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void btc_spp_start_discovery(btc_spp_args_t *arg)
|
|
|
|
{
|
|
|
|
BTA_JvStartDiscovery(arg->start_discovery.bd_addr, arg->start_discovery.num_uuid, arg->start_discovery.p_uuid_list, NULL);
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
|
2018-01-05 03:48:29 -05:00
|
|
|
static void btc_spp_connect(btc_spp_args_t *arg)
|
|
|
|
{
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_slot_t *slot = spp_malloc_slot();
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to malloc RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
slot->security = arg->connect.sec_mask;
|
|
|
|
slot->role = arg->connect.role;
|
|
|
|
slot->scn = arg->connect.remote_scn;;
|
|
|
|
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,
|
|
|
|
arg->connect.peer_bd_addr, (tBTA_JV_RFCOMM_CBACK *)btc_spp_rfcomm_inter_cb, (void *)slot->id);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
|
2018-01-05 03:48:29 -05:00
|
|
|
static void btc_spp_disconnect(btc_spp_args_t *arg)
|
|
|
|
{
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_slot_t *slot = spp_find_slot_by_handle(arg->disconnect.handle);
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot! disconnect fail!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
BTA_JvRfcommClose(arg->disconnect.handle, (void *)slot->id);
|
|
|
|
btc_disconnect_cb(slot->rfc_handle);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_free_slot(slot);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
|
2018-01-05 03:48:29 -05:00
|
|
|
static void btc_spp_start_srv(btc_spp_args_t *arg)
|
|
|
|
{
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_slot_t *slot = spp_malloc_slot();
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to malloc RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
slot->security = arg->start_srv.sec_mask;
|
|
|
|
slot->role = arg->start_srv.role;
|
|
|
|
slot->scn = arg->start_srv.local_scn;;
|
|
|
|
slot->max_session = arg->start_srv.max_session;
|
|
|
|
strcpy(slot->service_name, arg->start_srv.name);
|
|
|
|
|
|
|
|
BTA_JvGetChannelId(BTA_JV_CONN_TYPE_RFCOMM, (void *)slot->id, arg->start_srv.local_scn);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void btc_spp_write(btc_spp_args_t *arg)
|
|
|
|
{
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_slot_t *slot = spp_find_slot_by_handle(arg->write.handle);
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
return;
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_mode == ESP_SPP_MODE_VFS) {
|
|
|
|
size_t item_size = 0;
|
|
|
|
if (slot->write_data != NULL) {
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint8_t *data = xRingbufferReceiveUpTo(slot->ringbuf_write, &item_size, 0,990);
|
|
|
|
if (item_size != 0){
|
|
|
|
slot->write_data = data;
|
|
|
|
BTA_JvRfcommWrite(arg->write.handle, slot->id, item_size, data);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
list_append(slot->list, arg->write.p_data);
|
|
|
|
BTA_JvRfcommWrite(arg->write.handle, slot->id, arg->write.len, arg->write.p_data);
|
|
|
|
}
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void btc_spp_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
|
|
|
|
{
|
|
|
|
btc_spp_args_t *dst = (btc_spp_args_t *) p_dest;
|
|
|
|
btc_spp_args_t *src = (btc_spp_args_t *) p_src;
|
|
|
|
|
|
|
|
switch (msg->act) {
|
|
|
|
case BTC_SPP_ACT_START_DISCOVERY:
|
|
|
|
dst->start_discovery.p_uuid_list = (tSDP_UUID *)osi_malloc(src->start_discovery.num_uuid * sizeof(tSDP_UUID));
|
|
|
|
if (dst->start_discovery.p_uuid_list) {
|
|
|
|
memcpy(dst->start_discovery.p_uuid_list, src->start_discovery.p_uuid_list, src->start_discovery.num_uuid * sizeof(tSDP_UUID));
|
|
|
|
} else if (src->start_discovery.num_uuid == 0) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s %d no mem\n", __func__, msg->act);
|
2018-01-05 03:48:29 -05:00
|
|
|
} else {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s %d osi_malloc failed\n", __func__, msg->act);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BTC_SPP_ACT_WRITE:
|
|
|
|
dst->write.p_data = (uint8_t *)osi_malloc(src->write.len);
|
|
|
|
if (dst->write.p_data) {
|
|
|
|
memcpy(dst->write.p_data, src->write.p_data, src->write.len);
|
|
|
|
} else if (src->write.len == 0) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_DEBUG("%s %d no mem\n", __func__, msg->act);
|
2018-01-05 03:48:29 -05:00
|
|
|
} else {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s %d osi_malloc failed\n", __func__, msg->act);
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void btc_spp_arg_deep_free(btc_msg_t *msg)
|
|
|
|
{
|
|
|
|
btc_spp_args_t *arg = (btc_spp_args_t *)msg->arg;
|
|
|
|
|
|
|
|
switch (msg->act) {
|
|
|
|
case BTC_SPP_ACT_START_DISCOVERY:
|
|
|
|
if (arg->start_discovery.p_uuid_list) {
|
|
|
|
osi_free(arg->start_discovery.p_uuid_list);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void btc_spp_call_handler(btc_msg_t *msg)
|
|
|
|
{
|
|
|
|
btc_spp_args_t *arg = (btc_spp_args_t *)(msg->arg);
|
|
|
|
switch (msg->act) {
|
|
|
|
case BTC_SPP_ACT_INIT:
|
2018-04-27 23:36:22 -04:00
|
|
|
btc_spp_init(arg);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
case BTC_SPP_ACT_UNINIT:
|
|
|
|
btc_spp_uninit();
|
|
|
|
break;
|
|
|
|
case BTC_SPP_ACT_START_DISCOVERY:
|
|
|
|
btc_spp_start_discovery(arg);
|
|
|
|
break;
|
|
|
|
case BTC_SPP_ACT_CONNECT:
|
|
|
|
btc_spp_connect(arg);
|
|
|
|
break;
|
|
|
|
case BTC_SPP_ACT_DISCONNECT:
|
|
|
|
btc_spp_disconnect(arg);
|
|
|
|
break;
|
|
|
|
case BTC_SPP_ACT_START_SRV:
|
|
|
|
btc_spp_start_srv(arg);
|
|
|
|
break;
|
|
|
|
case BTC_SPP_ACT_WRITE:
|
|
|
|
btc_spp_write(arg);
|
|
|
|
break;
|
|
|
|
default:
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s: Unhandled event (%d)!\n", __FUNCTION__, msg->act);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
btc_spp_arg_deep_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void btc_spp_cb_handler(btc_msg_t *msg)
|
|
|
|
{
|
|
|
|
esp_spp_cb_param_t param;
|
|
|
|
tBTA_JV *p_data = (tBTA_JV *)msg->arg;
|
2018-04-27 23:36:22 -04:00
|
|
|
spp_slot_t *slot;
|
2018-01-05 03:48:29 -05:00
|
|
|
switch (msg->act) {
|
|
|
|
case BTA_JV_ENABLE_EVT:
|
|
|
|
param.init.status = p_data->status;
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_INIT_EVT, ¶m);
|
|
|
|
break;
|
|
|
|
case BTA_JV_DISCOVERY_COMP_EVT:
|
|
|
|
param.disc_comp.status = p_data->disc_comp.status;
|
|
|
|
param.disc_comp.scn_num = p_data->disc_comp.scn_num;
|
|
|
|
memcpy(param.disc_comp.scn, p_data->disc_comp.scn, p_data->disc_comp.scn_num);
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_DISCOVERY_COMP_EVT, ¶m);
|
|
|
|
break;
|
|
|
|
case BTA_JV_RFCOMM_CL_INIT_EVT:
|
|
|
|
param.cl_init.status = p_data->rfc_cl_init.status;
|
|
|
|
param.cl_init.handle = p_data->rfc_cl_init.handle;
|
|
|
|
param.cl_init.sec_id = p_data->rfc_cl_init.sec_id;
|
|
|
|
param.cl_init.use_co = p_data->rfc_cl_init.use_co;
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_CL_INIT_EVT, ¶m);
|
|
|
|
break;
|
|
|
|
case BTA_JV_RFCOMM_OPEN_EVT:
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_mode == ESP_SPP_MODE_VFS) {
|
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
slot = spp_find_slot_by_handle(p_data->rfc_open.handle);
|
2018-04-27 23:36:22 -04:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
param.open.fd = slot->fd;
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
}
|
2018-01-05 03:48:29 -05:00
|
|
|
param.open.status = p_data->rfc_open.status;
|
|
|
|
param.open.handle = p_data->rfc_open.handle;
|
|
|
|
memcpy(param.open.rem_bda, p_data->rfc_open.rem_bda, ESP_BD_ADDR_LEN);
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_OPEN_EVT, ¶m);
|
|
|
|
break;
|
|
|
|
case BTA_JV_RFCOMM_START_EVT:
|
|
|
|
param.start.status = p_data->rfc_start.status;
|
|
|
|
param.start.handle = p_data->rfc_start.handle;
|
|
|
|
param.start.sec_id = p_data->rfc_start.sec_id;
|
|
|
|
param.start.use_co = p_data->rfc_start.use_co;
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_START_EVT, ¶m);
|
|
|
|
break;
|
|
|
|
case BTA_JV_RFCOMM_SRV_OPEN_EVT:
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_mode == ESP_SPP_MODE_VFS) {
|
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
slot = spp_find_slot_by_handle(p_data->rfc_srv_open.handle);
|
2018-04-27 23:36:22 -04:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
param.srv_open.fd = slot->fd;
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
}
|
2018-01-05 03:48:29 -05:00
|
|
|
param.srv_open.status = p_data->rfc_srv_open.status;
|
|
|
|
param.srv_open.handle = p_data->rfc_srv_open.handle;
|
|
|
|
param.srv_open.new_listen_handle = p_data->rfc_srv_open.new_listen_handle;
|
|
|
|
memcpy(param.srv_open.rem_bda, p_data->rfc_srv_open.rem_bda, ESP_BD_ADDR_LEN);
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_SRV_OPEN_EVT, ¶m);
|
|
|
|
break;
|
|
|
|
case BTA_JV_RFCOMM_WRITE_EVT:
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
slot = spp_find_slot_by_handle(p_data->rfc_write.handle);
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_mode == ESP_SPP_MODE_CB){
|
|
|
|
param.write.status = p_data->rfc_write.status;
|
|
|
|
param.write.handle = p_data->rfc_write.handle;
|
|
|
|
param.write.len = p_data->rfc_write.len;
|
|
|
|
param.write.cong = p_data->rfc_write.cong;
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_WRITE_EVT, ¶m);
|
|
|
|
list_remove(slot->list, list_front(slot->list));
|
|
|
|
} else {
|
|
|
|
if (p_data->rfc_write.status != BTA_JV_SUCCESS) {
|
|
|
|
if (slot->write_data != NULL){
|
|
|
|
vRingbufferReturnItem(slot->ringbuf_write,slot->write_data);
|
|
|
|
slot->write_data = NULL;
|
|
|
|
}
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p_data->rfc_write.cong == 0) {
|
|
|
|
if (slot->write_data != NULL){
|
|
|
|
vRingbufferReturnItem(slot->ringbuf_write,slot->write_data);
|
|
|
|
slot->write_data = NULL;
|
|
|
|
}
|
|
|
|
size_t item_size = 0;
|
|
|
|
uint8_t *data = xRingbufferReceiveUpTo(slot->ringbuf_write, &item_size, 0,990);
|
|
|
|
if (item_size != 0){
|
|
|
|
slot->write_data = data;
|
|
|
|
BTA_JvRfcommWrite(slot->rfc_handle, slot->id, item_size, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
case BTA_JV_RFCOMM_CLOSE_EVT:
|
|
|
|
param.close.status = p_data->rfc_close.status;
|
|
|
|
param.close.port_status = p_data->rfc_close.port_status;
|
|
|
|
param.close.handle = p_data->rfc_close.handle;
|
|
|
|
param.close.async = p_data->rfc_close.async;
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_CLOSE_EVT, ¶m);
|
|
|
|
break;
|
|
|
|
case BTA_JV_RFCOMM_CONG_EVT:
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_mode == ESP_SPP_MODE_CB) {
|
|
|
|
param.cong.status = p_data->rfc_cong.status;
|
|
|
|
param.cong.handle = p_data->rfc_cong.handle;
|
|
|
|
param.cong.cong = p_data->rfc_cong.cong;
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_CONG_EVT, ¶m);
|
|
|
|
} else {
|
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
slot = spp_find_slot_by_handle(p_data->rfc_cong.handle);
|
2018-04-27 23:36:22 -04:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p_data->rfc_cong.cong == 0) {
|
|
|
|
if (slot->write_data != NULL){
|
|
|
|
vRingbufferReturnItem(slot->ringbuf_write,slot->write_data);
|
|
|
|
slot->write_data = NULL;
|
|
|
|
}
|
|
|
|
size_t item_size = 0;
|
|
|
|
uint8_t *data = xRingbufferReceiveUpTo(slot->ringbuf_write, &item_size, 0,990);
|
|
|
|
if (item_size != 0){
|
|
|
|
slot->write_data = data;
|
|
|
|
BTA_JvRfcommWrite(slot->rfc_handle, slot->id, item_size, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
}
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
case BTA_JV_RFCOMM_DATA_IND_EVT:
|
|
|
|
param.data_ind.status = ESP_SPP_SUCCESS;
|
|
|
|
param.data_ind.handle = p_data->data_ind.handle;
|
|
|
|
if (p_data->data_ind.p_buf) {
|
|
|
|
param.data_ind.len = p_data->data_ind.p_buf->len;
|
|
|
|
param.data_ind.data = p_data->data_ind.p_buf->data + p_data->data_ind.p_buf->offset;
|
|
|
|
} else {
|
|
|
|
param.data_ind.len = 0;
|
|
|
|
param.data_ind.data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
btc_spp_cb_to_app(ESP_SPP_DATA_IND_EVT, ¶m);
|
|
|
|
osi_free (p_data->data_ind.p_buf);
|
|
|
|
break;
|
|
|
|
default:
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_DEBUG("%s: Unhandled event (%d)!", __FUNCTION__, msg->act);
|
2018-01-05 03:48:29 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:36:22 -04:00
|
|
|
/**
|
|
|
|
* If there is too much data not accepted, wait until accepted,or discard it when timeout!
|
|
|
|
* @param list list
|
|
|
|
* @param p_buf incoming data
|
|
|
|
*/
|
|
|
|
static void spp_delay_append(list_t *list, BT_HDR *p_buf){
|
|
|
|
uint8_t count = 0;
|
|
|
|
while (count++ < 100) {
|
|
|
|
if (list_length(list) < 200){
|
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
|
|
|
list_append(list, p_buf);
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
osi_free(p_buf);
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_WARNING("%s There is too much data not accepted, discard it!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
}
|
2018-01-05 03:48:29 -05:00
|
|
|
|
|
|
|
int bta_co_rfc_data_incoming(void *user_data, BT_HDR *p_buf)
|
|
|
|
{
|
|
|
|
bt_status_t status;
|
|
|
|
tBTA_JV p_data;
|
|
|
|
btc_msg_t msg;
|
|
|
|
msg.sig = BTC_SIG_API_CB;
|
|
|
|
msg.pid = BTC_PID_SPP;
|
|
|
|
msg.act = BTA_JV_RFCOMM_DATA_IND_EVT;
|
|
|
|
|
|
|
|
uint32_t id = (uintptr_t)user_data;
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_slot_t *slot = spp_find_slot_by_id(id);
|
2018-01-05 03:48:29 -05:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
p_data.data_ind.handle = slot->rfc_handle;
|
|
|
|
|
2018-04-27 23:36:22 -04:00
|
|
|
if (spp_local_param.spp_mode == ESP_SPP_MODE_CB) {
|
|
|
|
p_data.data_ind.p_buf = p_buf;
|
|
|
|
status = btc_transfer_context(&msg, &p_data,
|
|
|
|
sizeof(tBTA_JV), NULL);
|
|
|
|
if (status != BT_STATUS_SUCCESS) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s btc_transfer_context failed\n", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (list_is_empty(slot->incoming_list)) {
|
|
|
|
BaseType_t done = xRingbufferSend(slot->ringbuf_read, p_buf->data + p_buf->offset, p_buf->len, 0);
|
|
|
|
if (done) {
|
|
|
|
osi_free (p_buf);
|
|
|
|
} else {
|
|
|
|
list_append(slot->incoming_list, p_buf);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
list_t *list = slot->incoming_list;
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
spp_delay_append(list,p_buf);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-01-05 03:48:29 -05:00
|
|
|
}
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
2018-01-05 03:48:29 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int bta_co_rfc_data_outgoing_size(void *user_data, int *size)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
int bta_co_rfc_data_outgoing(void *user_data, uint8_t *buf, uint16_t size)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:36:22 -04:00
|
|
|
|
|
|
|
static ssize_t spp_vfs_write(int fd, const void * data, size_t size)
|
|
|
|
{
|
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_slot_t *slot = spp_find_slot_by_fd(fd);
|
2018-04-27 23:36:22 -04:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
BaseType_t done = xRingbufferSend(slot->ringbuf_write, (void *)data, size, 0);
|
|
|
|
esp_spp_write(slot->rfc_handle, 0, NULL);
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
if (done){
|
|
|
|
return size;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
static int spp_vfs_close(int fd)
|
|
|
|
{
|
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_slot_t *slot = spp_find_slot_by_fd(fd);
|
2018-04-27 23:36:22 -04:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
esp_spp_disconnect(slot->rfc_handle);
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool incoming_list_2_ringbuf_read(spp_slot_t *slot)
|
|
|
|
{
|
|
|
|
while (!list_is_empty(slot->incoming_list)) {
|
|
|
|
BT_HDR *p_buf = list_front(slot->incoming_list);
|
|
|
|
BaseType_t done = xRingbufferSend(slot->ringbuf_read, p_buf->data + p_buf->offset, p_buf->len, 0);
|
|
|
|
if (done) {
|
|
|
|
list_remove(slot->incoming_list, p_buf);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t spp_vfs_read(int fd, void * dst, size_t size)
|
|
|
|
{
|
|
|
|
osi_mutex_lock(&spp_local_param.spp_slot_mutex, OSI_MUTEX_MAX_TIMEOUT);
|
2018-05-11 09:56:53 -04:00
|
|
|
spp_slot_t *slot = spp_find_slot_by_fd(fd);
|
2018-04-27 23:36:22 -04:00
|
|
|
if (!slot) {
|
2018-04-24 22:11:06 -04:00
|
|
|
BTC_TRACE_ERROR("%s unable to find RFCOMM slot!", __func__);
|
2018-04-27 23:36:22 -04:00
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
incoming_list_2_ringbuf_read(slot);
|
|
|
|
size_t item_size = 0;
|
|
|
|
uint8_t *data = xRingbufferReceiveUpTo(slot->ringbuf_read, &item_size, 0, size);
|
|
|
|
if (item_size > 0){
|
|
|
|
memcpy(dst, data, item_size);
|
|
|
|
vRingbufferReturnItem(slot->ringbuf_read, data);
|
|
|
|
}
|
|
|
|
osi_mutex_unlock(&spp_local_param.spp_slot_mutex);
|
|
|
|
return item_size;
|
|
|
|
}
|
|
|
|
|
2018-05-11 09:56:53 -04:00
|
|
|
esp_err_t btc_spp_vfs_register()
|
2018-04-27 23:36:22 -04:00
|
|
|
{
|
|
|
|
esp_vfs_t vfs = {
|
2018-05-07 03:01:56 -04:00
|
|
|
.flags = ESP_VFS_FLAG_DEFAULT,
|
2018-04-27 23:36:22 -04:00
|
|
|
.write = spp_vfs_write,
|
|
|
|
.open = NULL,
|
|
|
|
.fstat = NULL,
|
|
|
|
.close = spp_vfs_close,
|
|
|
|
.read = spp_vfs_read,
|
|
|
|
.fcntl = NULL
|
|
|
|
};
|
2018-05-11 09:56:53 -04:00
|
|
|
|
|
|
|
// No FD range is registered here: spp_vfs_id is used to register/unregister
|
|
|
|
// file descriptors
|
|
|
|
if (esp_vfs_register_with_id(&vfs, NULL, &spp_local_param.spp_vfs_id) != ESP_OK) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ESP_OK;
|
2018-04-27 23:36:22 -04:00
|
|
|
}
|
|
|
|
|
2018-05-07 03:01:56 -04:00
|
|
|
#endif ///defined BTC_SPP_INCLUDED && BTC_SPP_INCLUDED == TRUE
|