component/bt: add module btc_gap_bt and port function esp_bt_gap_set_scan_mode

This commit is contained in:
wangmengyang 2017-02-24 17:28:33 +08:00
parent 41070f6768
commit c7dac035ae
7 changed files with 110 additions and 19 deletions

View file

@ -0,0 +1,39 @@
// 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 "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "bt_trace.h"
#include "btc_manage.h"
#include "btc_gap_bt.h"
esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode)
{
btc_msg_t msg;
btc_gap_bt_args_t arg;
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
return ESP_ERR_INVALID_STATE;
}
msg.sig = BTC_SIG_API_CALL;
msg.pid = BTC_PID_GAP_BT;
msg.act = BTC_GAP_BT_ACT_SET_SCAN_MODE;
arg.scan_mode.mode = mode;
return (btc_transfer_context(&msg, &arg, sizeof(btc_gap_bt_args_t), NULL) == BT_STATUS_SUCCESS ? ESP_OK : ESP_FAIL);
}

View file

@ -25,10 +25,10 @@ extern "C" {
/// Discoverability and Connectability mode
typedef enum {
BT_SCAN_MODE_NONE = 0, /*!< Neither discoverable nor connectable */
BT_SCAN_MODE_CONNECTABLE, /*!< Connectable but not discoverable */
BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE /*!< both discoverable and connectaable */
} bt_scan_mode_t;
ESP_BT_SCAN_MODE_NONE = 0, /*!< Neither discoverable nor connectable */
ESP_BT_SCAN_MODE_CONNECTABLE, /*!< Connectable but not discoverable */
ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE /*!< both discoverable and connectaable */
} esp_bt_scan_mode_t;
/**
* @brief Set discoverability and connectability mode for legacy bluetooth
@ -39,7 +39,7 @@ typedef enum {
* - ESP_OK : Succeed
* - ESP_ERR_INVALID_ARG: if argument invalid
*/
esp_err_t esp_bt_gap_set_scan_mode(bt_scan_mode_t mode);
esp_err_t esp_bt_gap_set_scan_mode(esp_bt_scan_mode_t mode);
/**

View file

@ -43,14 +43,14 @@ static void btc_enable_bluetooth(void)
{
if (BTA_EnableBluetooth(btc_sec_callback) != BTA_SUCCESS) {
future_ready(*btc_main_get_future_p(BTC_MAIN_ENABLE_FUTURE), FUTURE_FAIL);
}
}
}
static void btc_disable_bluetooth(void)
{
if (BTA_DisableBluetooth() != BTA_SUCCESS) {
future_ready(*btc_main_get_future_p(BTC_MAIN_DISABLE_FUTURE), FUTURE_FAIL);
}
}
}
void btc_init_callback(void)

View file

@ -23,6 +23,7 @@
#include "btc_gatts.h"
#include "btc_gattc.h"
#include "btc_gap_ble.h"
#include "btc_gap_bt.h"
#include "btc_blufi_prf.h"
#include "bta_gatt_api.h"
@ -35,7 +36,7 @@ static btc_func_t profile_tab[BTC_PID_NUM] = {
[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_GAP_BT] = {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},

View file

@ -13,42 +13,76 @@
// limitations under the License.
#include "esp_gap_bt_api.h"
#include "btc_gap_bt.h"
#include "bta_api.h"
#include "bt_trace.h"
#include <string.h>
esp_err_t esp_bt_gap_set_scan_mode(bt_scan_mode_t mode)
void btc_gap_bt_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
{
switch (msg->act) {
default:
// LOG_ERROR("Unhandled deep copy %d\n", msg->act);
break;
}
}
static void btc_gap_bt_arg_deep_free(btc_msg_t *msg)
{
LOG_DEBUG("%s \n", __func__);
switch (msg->act) {
default:
// LOG_DEBUG("Unhandled deep free %d\n", msg->act);
break;
}
}
static void btc_bt_set_scan_mode(esp_bt_scan_mode_t mode)
{
tBTA_DM_DISC disc_mode;
tBTA_DM_CONN conn_mode;
switch (mode) {
case BT_SCAN_MODE_NONE:
case ESP_BT_SCAN_MODE_NONE:
disc_mode = BTA_DM_NON_DISC;
conn_mode = BTA_DM_NON_CONN;
break;
case BT_SCAN_MODE_CONNECTABLE:
case ESP_BT_SCAN_MODE_CONNECTABLE:
disc_mode = BTA_DM_NON_DISC;
conn_mode = BTA_DM_CONN;
break;
case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
case ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
disc_mode = BTA_DM_GENERAL_DISC;
conn_mode = BTA_DM_CONN;
break;
default:
// BTIF_TRACE_ERROR("invalid scan mode (0x%x)", mode);
return ESP_ERR_INVALID_ARG;
LOG_WARN("invalid scan mode (0x%x)", mode);
return;
}
// BTIF_TRACE_EVENT("set property scan mode : %x", mode);
BTA_DmSetVisibility(disc_mode, conn_mode, BTA_DM_IGNORE, BTA_DM_IGNORE);
return ESP_OK;
return;
}
void btc_gap_bt_call_handler(btc_msg_t *msg)
{
btc_gap_bt_args_t *arg = (btc_gap_bt_args_t *)msg->arg;
LOG_DEBUG("%s act %d\n", __func__, msg->act);
switch (msg->act) {
case BTC_GAP_BT_ACT_SET_SCAN_MODE: {
btc_bt_set_scan_mode(arg->scan_mode.mode);
break;
}
default:
break;
}
btc_gap_bt_arg_deep_free(msg);
}
/* to be fixed */
esp_err_t esp_bt_gap_set_device_name(const char *name)
{
if (name == NULL || *name == '\0') {

View file

@ -15,8 +15,25 @@
#ifndef __BTC_GAP_BT_H__
#define __BTC_GAP_BT_H__
#include "esp_bt_defs.h"
#include "esp_gap_bt_api.h"
#include "btc_task.h"
typedef enum {
BTC_GAP_BT_ACT_SET_SCAN_MODE = 0
} btc_gap_bt_act_t;
/* btc_gap_bt_args_t */
typedef union {
// BTC_GAP_BT_ACT_SET_SCAN_MODE,
struct scan_mode_args {
esp_bt_scan_mode_t mode;
} scan_mode;
} btc_gap_bt_args_t;
void btc_gap_bt_call_handler(btc_msg_t *msg);
void btc_gap_bt_cb_handler(btc_msg_t *msg);
#define /* __BTC_GAP_BT_H__ */
void btc_gap_bt_arg_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
#endif /* __BTC_GAP_BT_H__ */

View file

@ -86,7 +86,7 @@ static void bt_app_handle_evt(uint16_t event, void *p_param)
esp_avrc_ct_init();
esp_avrc_ct_register_callback(bt_app_rc_ct_cb);
esp_bt_gap_set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
break;
}
case ESP_A2D_CONNECTION_STATE_EVT: {