pinebuds/services/tota/app_spp_tota.cpp

352 lines
11 KiB
C++
Raw Permalink Normal View History

2022-08-15 04:20:27 -05:00
/***************************************************************************
*
* Copyright 2015-2019 BES.
* All rights reserved. All unpublished rights reserved.
*
* No part of this work may be used or reproduced in any form or by any
* means, or stored in a database or retrieval system, without prior written
* permission of BES.
*
* Use of this work is governed by a license granted by BES.
* This work contains confidential and proprietary information of
* BES. which is protected by copyright, trade secret,
* trademark and other intellectual property rights.
*
****************************************************************************/
#include "analog.h"
#include "app_audio.h"
#include "app_bt_stream.h"
#include "app_status_ind.h"
#include "audioflinger.h"
#include "cmsis_os.h"
#include "hal_chipid.h"
#include "hal_cmu.h"
#include "hal_timer.h"
#include "hal_trace.h"
#include "hal_uart.h"
#include "lockcqueue.h"
2022-08-15 04:20:27 -05:00
#include "nvrecord.h"
#include "nvrecord_env.h"
#include <stdio.h>
2022-08-15 04:20:27 -05:00
//#include "nvrecord_dev.h"
#include "app_spp.h"
2022-08-15 04:20:27 -05:00
#include "app_spp_tota.h"
#include "app_tota.h"
#include "app_tota_cmd_code.h"
2022-08-15 04:20:27 -05:00
#include "app_tota_cmd_handler.h"
#include "app_tota_data_handler.h"
#include "bluetooth.h"
#include "cqueue.h"
2022-08-15 04:20:27 -05:00
#include "plat_types.h"
#include "resources.h"
2022-08-15 04:20:27 -05:00
#include "sdp_api.h"
#include "spp_api.h"
2022-08-15 04:20:27 -05:00
//#include "app_bt_conn_mgr.h"
#include "tota_stream_data_transfer.h"
static bool isTotaSppConnected = false;
static struct spp_device *tota_spp_dev = NULL;
static struct spp_service *totaSppService = NULL;
2022-08-15 04:20:27 -05:00
osMutexDef(tota_spp_mutex);
osMutexDef(tota_credit_mutex);
static btif_sdp_record_t *tota_sdp_record = NULL;
static uint8_t totaSppTxBuf[TOTA_SPP_TX_BUF_SIZE];
static uint8_t spp_rx_buf[SPP_RECV_BUFFER_SIZE];
static uint32_t occupiedTotaSppTxBufSize;
static uint32_t offsetToFillTotaSppTxData;
static uint8_t *ptrTotaSppTxBuf;
2022-08-15 04:20:27 -05:00
#if (TOTA_SHARE_TX_RX_BUF == 1)
2022-08-15 04:20:27 -05:00
static const bool spp_share_application_rx_tx_buf = true;
#else
static const bool spp_share_application_rx_tx_buf = false;
#endif
/**/
static map<APP_TOTA_MODULE_E, tota_callback_func_t> s_module_map;
static tota_callback_func_t s_module_func;
static APP_TOTA_MODULE_E s_module;
2022-08-15 04:20:27 -05:00
/* register callback module */
void tota_callback_module_register(APP_TOTA_MODULE_E module,
tota_callback_func_t tota_callback_func) {
map<APP_TOTA_MODULE_E, tota_callback_func_t>::iterator it =
s_module_map.find(module);
if (it == s_module_map.end()) {
TOTA_LOG_DBG(0, "add to map");
s_module_map.insert(make_pair(module, tota_callback_func));
} else {
TOTA_LOG_DBG(0, "already exist, not add");
}
2022-08-15 04:20:27 -05:00
}
/* set current module */
void tota_callback_module_set(APP_TOTA_MODULE_E module) {
map<APP_TOTA_MODULE_E, tota_callback_func_t>::iterator it =
s_module_map.find(module);
if (it != s_module_map.end()) {
s_module = module;
s_module_func = it->second;
TOTA_LOG_DBG(1, "set %d module success", module);
} else {
TOTA_LOG_DBG(0, "not find callback func by module");
}
2022-08-15 04:20:27 -05:00
}
/* get current module */
APP_TOTA_MODULE_E tota_callback_module_get() { return s_module; }
2022-08-15 04:20:27 -05:00
/* is tota busy, use to handle sniff */
bool spp_tota_in_progress(void) {
TOTA_LOG_DBG(2, "[%s] isTotaSppConnected:%d", __func__, isTotaSppConnected);
if (isTotaSppConnected == true)
return true;
else
return false;
2022-08-15 04:20:27 -05:00
}
void app_spp_tota_share_buf_create(uint8_t **tota_tx_buf,
uint8_t **tota_rx_buf) {
*tota_tx_buf = NULL;
*tota_rx_buf = NULL;
2022-08-15 04:20:27 -05:00
if (spp_share_application_rx_tx_buf == true) {
*tota_tx_buf = totaSppTxBuf;
*tota_rx_buf = spp_rx_buf;
}
2022-08-15 04:20:27 -05:00
}
bool app_spp_tota_is_to_share_buf(void) {
return spp_share_application_rx_tx_buf;
2022-08-15 04:20:27 -05:00
}
uint8_t *app_spp_tota_share_tx_buf_get(void) {
return (spp_share_application_rx_tx_buf == true) ? (totaSppTxBuf) : (NULL);
2022-08-15 04:20:27 -05:00
}
uint8_t *app_spp_tota_share_rx_buf_get(void) {
return (spp_share_application_rx_tx_buf == true) ? (spp_rx_buf) : (NULL);
2022-08-15 04:20:27 -05:00
}
uint16_t app_spp_tota_tx_buf_size(void) { return TOTA_SPP_TX_BUF_SIZE; }
2022-08-15 04:20:27 -05:00
void app_spp_tota_init_tx_buf(uint8_t *ptr) {
ptrTotaSppTxBuf = ptr;
occupiedTotaSppTxBufSize = 0;
offsetToFillTotaSppTxData = 0;
2022-08-15 04:20:27 -05:00
}
extern "C" APP_TOTA_CMD_RET_STATUS_E
app_tota_data_received(uint8_t *ptrData, uint32_t dataLength);
extern "C" APP_TOTA_CMD_RET_STATUS_E app_tota_cmd_received(uint8_t *ptrData,
uint32_t dataLength);
2022-08-15 04:20:27 -05:00
/****************************************************************************
* TOTA SPP SDP Entries
****************************************************************************/
/*---------------------------------------------------------------------------
*
* ServiceClassIDList
2022-08-15 04:20:27 -05:00
*/
static const U8 TotaSppClassId[] = {
SDP_ATTRIB_HEADER_8BIT(3), /* Data Element Sequence, 6 bytes */
SDP_UUID_16BIT(SC_SERIAL_PORT), /* Hands-Free UUID in Big Endian */
2022-08-15 04:20:27 -05:00
};
static const U8 TotaSppProtoDescList[] = {
SDP_ATTRIB_HEADER_8BIT(12), /* Data element sequence, 12 bytes */
2022-08-15 04:20:27 -05:00
/* Each element of the list is a Protocol descriptor which is a
* data element sequence. The first element is L2CAP which only
* has a UUID element.
*/
SDP_ATTRIB_HEADER_8BIT(3), /* Data element sequence for L2CAP, 3
* bytes
*/
2022-08-15 04:20:27 -05:00
SDP_UUID_16BIT(PROT_L2CAP), /* Uuid16 L2CAP */
2022-08-15 04:20:27 -05:00
/* Next protocol descriptor in the list is RFCOMM. It contains two
* elements which are the UUID and the channel. Ultimately this
* channel will need to filled in with value returned by RFCOMM.
*/
2022-08-15 04:20:27 -05:00
/* Data element sequence for RFCOMM, 5 bytes */
SDP_ATTRIB_HEADER_8BIT(5),
SDP_UUID_16BIT(PROT_RFCOMM), /* Uuid16 RFCOMM */
/* Uint8 RFCOMM channel number - value can vary */
SDP_UINT_8BIT(RFCOMM_CHANNEL_TOTA)};
2022-08-15 04:20:27 -05:00
/*
* BluetoothProfileDescriptorList
*/
static const U8 TotaSppProfileDescList[] = {
SDP_ATTRIB_HEADER_8BIT(8), /* Data element sequence, 8 bytes */
2022-08-15 04:20:27 -05:00
/* Data element sequence for ProfileDescriptor, 6 bytes */
SDP_ATTRIB_HEADER_8BIT(6),
SDP_UUID_16BIT(SC_SERIAL_PORT), /* Uuid16 SPP */
SDP_UINT_16BIT(0x0102) /* As per errata 2239 */
2022-08-15 04:20:27 -05:00
};
/*
* * OPTIONAL * ServiceName
*/
static const U8 TotaSppServiceName1[] = {
SDP_TEXT_8BIT(5), /* Null terminated text string */
'S',
'p',
'p',
'1',
'\0'};
2022-08-15 04:20:27 -05:00
static const U8 TotaSppServiceName2[] = {
SDP_TEXT_8BIT(5), /* Null terminated text string */
'S',
'p',
'p',
'2',
'\0'};
2022-08-15 04:20:27 -05:00
/* SPP attributes.
*
* This is a ROM template for the RAM structure used to register the
* SPP SDP record.
*/
// static const SdpAttribute TotaSppSdpAttributes1[] = {
2022-08-15 04:20:27 -05:00
static sdp_attribute_t TotaSppSdpAttributes1[] = {
SDP_ATTRIBUTE(AID_SERVICE_CLASS_ID_LIST, TotaSppClassId),
2022-08-15 04:20:27 -05:00
SDP_ATTRIBUTE(AID_PROTOCOL_DESC_LIST, TotaSppProtoDescList),
SDP_ATTRIBUTE(AID_BT_PROFILE_DESC_LIST, TotaSppProfileDescList),
/* SPP service name*/
SDP_ATTRIBUTE((AID_SERVICE_NAME + 0x0100), TotaSppServiceName1),
};
/*
static sdp_attribute_t TotaSppSdpAttributes2[] = {
SDP_ATTRIBUTE(AID_SERVICE_CLASS_ID_LIST, TotaSppClassId),
2022-08-15 04:20:27 -05:00
SDP_ATTRIBUTE(AID_PROTOCOL_DESC_LIST, TotaSppProtoDescList),
SDP_ATTRIBUTE(AID_BT_PROFILE_DESC_LIST, TotaSppProfileDescList),
2022-08-15 04:20:27 -05:00
SDP_ATTRIBUTE((AID_SERVICE_NAME + 0x0100), TotaSppServiceName2),
};
*/
int tota_spp_handle_data_event_func(void *pDev, uint8_t process, uint8_t *pData,
uint16_t dataLen) {
TOTA_LOG_DBG(2, "[%s]data receive length = %d", __func__, dataLen);
TOTA_LOG_DUMP("[0x%x]", pData, dataLen);
2022-08-15 04:20:27 -05:00
if (s_module_func.tota_spp_data_receive_hanle != NULL)
s_module_func.tota_spp_data_receive_hanle(pData, (uint32_t)dataLen);
2022-08-15 04:20:27 -05:00
// the first two bytes of the data packet is the fixed value 0xFFFF
app_tota_handle_received_data(pData, dataLen);
return 0;
2022-08-15 04:20:27 -05:00
}
static void spp_tota_callback(struct spp_device *locDev,
struct spp_callback_parms *Info) {
if (BTIF_SPP_EVENT_REMDEV_CONNECTED == Info->event) {
TOTA_LOG_DBG(1, "::BTIF_SPP_EVENT_REMDEV_CONNECTED %d\n", Info->event);
isTotaSppConnected = true;
app_tota_connected(APP_TOTA_CONNECTED);
app_tota_update_datapath(APP_TOTA_VIA_SPP);
// conn_stop_connecting_mobile_supervising();
if (s_module_func.tota_spp_connected != NULL)
s_module_func.tota_spp_connected();
} else if (BTIF_SPP_EVENT_REMDEV_DISCONNECTED == Info->event) {
TOTA_LOG_DBG(1, "::BTIF_SPP_EVENT_REMDEV_DISCONNECTED %d\n", Info->event);
isTotaSppConnected = false;
app_tota_disconnected(APP_TOTA_DISCONNECTED);
app_tota_update_datapath(APP_TOTA_PATH_IDLE);
if (s_module_func.tota_spp_disconnected != NULL)
s_module_func.tota_spp_disconnected();
} else if (BTIF_SPP_EVENT_DATA_SENT == Info->event) {
app_tota_tx_done_callback();
if (s_module_func.tota_spp_tx_done != NULL)
s_module_func.tota_spp_tx_done();
} else {
TOTA_LOG_DBG(1, "::unknown event %d\n", Info->event);
}
2022-08-15 04:20:27 -05:00
}
bool app_spp_tota_send_data(uint8_t *ptrData, uint16_t length) {
if (!isTotaSppConnected) {
return false;
}
btif_spp_write(tota_spp_dev, (char *)ptrData, &length);
return true;
2022-08-15 04:20:27 -05:00
}
void app_spp_tota_init(void) {
uint8_t *rx_buf;
uint8_t *tx_buf;
osMutexId mid;
btif_sdp_record_param_t param;
2022-08-15 04:20:27 -05:00
if (tota_spp_dev == NULL)
tota_spp_dev = btif_create_spp_device();
2022-08-15 04:20:27 -05:00
if (app_spp_tota_is_to_share_buf() == true) {
app_spp_tota_share_buf_create(&tx_buf, &rx_buf);
} else {
rx_buf = &spp_rx_buf[0];
tx_buf = &totaSppTxBuf[0];
}
2022-08-15 04:20:27 -05:00
tota_spp_dev->rx_buffer = rx_buf;
2022-08-15 04:20:27 -05:00
app_spp_tota_init_tx_buf(tx_buf);
btif_spp_init_rx_buf(tota_spp_dev, rx_buf, SPP_RECV_BUFFER_SIZE);
2022-08-15 04:20:27 -05:00
mid = osMutexCreate(osMutex(tota_spp_mutex));
if (!mid) {
ASSERT(0, "cannot create mutex");
}
2022-08-15 04:20:27 -05:00
osMutexId creditMutex = osMutexCreate(osMutex(tota_credit_mutex));
2022-08-15 04:20:27 -05:00
tota_spp_dev->creditMutex = creditMutex;
2022-08-15 04:20:27 -05:00
if (tota_sdp_record == NULL)
tota_sdp_record = btif_sdp_create_record();
2022-08-15 04:20:27 -05:00
param.attrs = &TotaSppSdpAttributes1[0],
param.attr_count = ARRAY_SIZE(TotaSppSdpAttributes1);
param.COD = BTIF_COD_MAJOR_PERIPHERAL;
btif_sdp_record_setup(tota_sdp_record, &param);
2022-08-15 04:20:27 -05:00
if (totaSppService == NULL)
totaSppService = btif_create_spp_service();
2022-08-15 04:20:27 -05:00
totaSppService->rf_service.serviceId = RFCOMM_CHANNEL_TOTA;
totaSppService->numPorts = 0;
btif_spp_service_setup(tota_spp_dev, totaSppService, tota_sdp_record);
2022-08-15 04:20:27 -05:00
tota_spp_dev->portType = BTIF_SPP_SERVER_PORT;
tota_spp_dev->app_id = BTIF_APP_SPP_SERVER_TOTA_ID;
tota_spp_dev->spp_handle_data_event_func = tota_spp_handle_data_event_func;
btif_spp_init_device(tota_spp_dev, 5, mid);
2022-08-15 04:20:27 -05:00
btif_spp_open(tota_spp_dev, NULL, spp_tota_callback);
2022-08-15 04:20:27 -05:00
}