2016-11-15 12:56:15 +00: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 <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "btc_task.h"
|
|
|
|
#include "bt_trace.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "gki.h"
|
|
|
|
#include "bt_defs.h"
|
2016-11-16 08:26:02 +00:00
|
|
|
#include "btc_main.h"
|
2016-11-15 12:56:15 +00:00
|
|
|
#include "btc_gatts.h"
|
|
|
|
#include "btc_gattc.h"
|
|
|
|
#include "btc_gap_ble.h"
|
2016-11-16 14:11:59 +00:00
|
|
|
#include "btc_blufi_prf.h"
|
2016-11-19 13:00:08 +00:00
|
|
|
#include "bta_gatt_api.h"
|
|
|
|
|
2016-11-15 12:56:15 +00:00
|
|
|
|
|
|
|
static xTaskHandle xBtcTaskHandle = NULL;
|
|
|
|
static xQueueHandle xBtcQueue = 0;
|
|
|
|
|
|
|
|
static btc_func_t profile_tab[BTC_PID_NUM] = {
|
2016-11-24 18:10:15 +00:00
|
|
|
[BTC_PID_MAIN_INIT] = {btc_main_call_handler, NULL },
|
|
|
|
[BTC_PID_GATTS] = {btc_gatts_call_handler, btc_gatts_cb_handler },
|
|
|
|
[BTC_PID_GATTC] = {btc_gattc_call_handler, btc_gattc_cb_handler },
|
|
|
|
[BTC_PID_GAP_BLE] = {btc_gap_ble_call_handler, btc_gap_ble_cb_handler },
|
|
|
|
[BTC_PID_GAP_BT] = {NULL, NULL}, // {btc_gap_bt_call_handler, btc_gap_bt_cb_handler },
|
|
|
|
[BTC_PID_SDP] = {NULL, NULL},
|
|
|
|
[BTC_PID_BLE_HID] = {NULL, NULL},
|
|
|
|
[BTC_PID_BT_HID] = {NULL, NULL},
|
|
|
|
[BTC_PID_SPP] = {NULL, NULL},
|
|
|
|
[BTC_PID_SPPLIKE] = {NULL, NULL},
|
|
|
|
[BTC_PID_BLUFI] = {btc_blufi_call_handler, btc_blufi_cb_handler },
|
2016-11-15 12:56:15 +00:00
|
|
|
};
|
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
**
|
|
|
|
** Function btc_task
|
|
|
|
**
|
|
|
|
** Description Process profile Task Thread.
|
|
|
|
******************************************************************************/
|
2016-11-15 12:56:15 +00:00
|
|
|
static void btc_task(void *arg)
|
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
btc_msg_t msg;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (pdTRUE == xQueueReceive(xBtcQueue, &msg, (portTickType)portMAX_DELAY)) {
|
2016-12-07 06:11:40 +00:00
|
|
|
LOG_DEBUG("%s msg %u %u %u %p\n", __func__, msg.sig, msg.pid, msg.act, msg.arg);
|
2016-11-24 18:10:15 +00:00
|
|
|
switch (msg.sig) {
|
|
|
|
case BTC_SIG_API_CALL:
|
|
|
|
profile_tab[msg.pid].btc_call(&msg);
|
|
|
|
break;
|
|
|
|
case BTC_SIG_API_CB:
|
|
|
|
profile_tab[msg.pid].btc_cb(&msg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (msg.arg) {
|
|
|
|
GKI_freebuf(msg.arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-15 12:56:15 +00:00
|
|
|
|
|
|
|
static bt_status_t btc_task_post(btc_msg_t *msg)
|
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
if (msg == NULL) {
|
|
|
|
return BT_STATUS_PARM_INVALID;
|
|
|
|
}
|
2016-11-15 12:56:15 +00:00
|
|
|
|
2016-12-22 01:42:21 +00:00
|
|
|
if (xQueueSend(xBtcQueue, msg, 10 / portTICK_PERIOD_MS) != pdTRUE) {
|
2016-11-24 18:10:15 +00:00
|
|
|
LOG_ERROR("Btc Post failed\n");
|
|
|
|
return BT_STATUS_BUSY;
|
|
|
|
}
|
2016-11-15 12:56:15 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return BT_STATUS_SUCCESS;
|
2016-11-15 12:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg_deep_copy_t copy_func)
|
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
btc_msg_t lmsg;
|
|
|
|
|
|
|
|
if (msg == NULL) {
|
|
|
|
return BT_STATUS_PARM_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, arg);
|
|
|
|
|
|
|
|
memcpy(&lmsg, msg, sizeof(btc_msg_t));
|
|
|
|
if (arg) {
|
|
|
|
lmsg.arg = (void *)GKI_getbuf(arg_len);
|
|
|
|
memset(lmsg.arg, 0x00, arg_len); //important, avoid arg which have no length
|
|
|
|
if (lmsg.arg == NULL) {
|
|
|
|
return BT_STATUS_NOMEM;
|
|
|
|
}
|
|
|
|
memcpy(lmsg.arg, arg, arg_len);
|
|
|
|
if (copy_func) {
|
|
|
|
copy_func(&lmsg, lmsg.arg, arg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lmsg.arg = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return btc_task_post(&lmsg);
|
2016-11-15 12:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int btc_init(void)
|
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
xBtcQueue = xQueueCreate(BTC_TASK_QUEUE_NUM, sizeof(btc_msg_t));
|
|
|
|
xTaskCreate(btc_task, "Btc_task", BTC_TASK_STACK_SIZE, NULL, BTC_TASK_PRIO, &xBtcTaskHandle);
|
2016-11-15 12:56:15 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
/* TODO: initial the profile_tab */
|
2016-11-15 12:56:15 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
return BT_STATUS_SUCCESS;
|
2016-11-15 12:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void btc_deinit(void)
|
|
|
|
{
|
2016-11-24 18:10:15 +00:00
|
|
|
vTaskDelete(xBtcTaskHandle);
|
|
|
|
vQueueDelete(xBtcQueue);
|
2016-11-15 12:56:15 +00:00
|
|
|
|
2016-11-24 18:10:15 +00:00
|
|
|
xBtcTaskHandle = NULL;
|
|
|
|
xBtcQueue = 0;
|
2016-11-15 12:56:15 +00:00
|
|
|
}
|