Skip to content

Commit

Permalink
scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
rojer committed Nov 11, 2021
1 parent 13b21ba commit 836772d
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 109 deletions.
6 changes: 4 additions & 2 deletions include/esp32/esp32_bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

#include <stdlib.h>

#include "esp_bt_defs.h"
#include "esp_gatt_defs.h"
//#include "esp_bt_defs.h"
//#include "esp_gatt_defs.h"

#include "common/mg_str.h"

Expand All @@ -35,9 +35,11 @@ extern "C" {

#define MGOS_BT_ADDR_LEN 6

/*
const char *esp32_bt_addr_to_str(const esp_bd_addr_t addr, char *out);
int esp32_bt_addr_cmp(const esp_bd_addr_t a, const esp_bd_addr_t b);
const char *esp32_bt_uuid_to_str(const esp_bt_uuid_t *uuid, char *out);
*/

void mgos_bt_uuid_to_esp32(const struct mgos_bt_uuid *in, esp_bt_uuid_t *out);
void esp32_bt_uuid_to_mgos(const esp_bt_uuid_t *in, struct mgos_bt_uuid *out);
Expand Down
8 changes: 0 additions & 8 deletions include/esp32/esp32_bt_gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@ extern "C" {
bool mgos_bt_gap_get_pairing_enable(void);
bool mgos_bt_gap_set_pairing_enable(bool pairing_enable);

int mgos_bt_gap_get_num_paired_devices(void);
/*
* These are actually async. TODO(rojer): Add callbacks to the API.
* For now, just allow some time for the calls to complete.
*/
void mgos_bt_gap_remove_paired_device(const esp_bd_addr_t addr);
void mgos_bt_gap_remove_all_paired_devices(void);

#ifdef __cplusplus
}
#endif
8 changes: 4 additions & 4 deletions include/esp32/esp32_bt_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@
extern "C" {
#endif

enum cs_log_level ll_from_status(esp_bt_status_t status);
// enum cs_log_level ll_from_status(esp_bt_status_t status);
const char *esp32_bt_addr_to_str(const uint8_t *addr, char *out);

bool esp32_bt_is_scanning(void);
bool esp32_bt_gattc_init(void);

bool esp32_bt_gap_init(void);
bool esp32_bt_gatts_init(void);
void esp32_bt_gatts_auth_cmpl(const esp_bd_addr_t addr, bool success);
//void esp32_bt_gatts_auth_cmpl(const esp_bd_addr_t addr, bool success);

void esp32_bt_set_is_advertising(bool is_advertising);

/* Workaround for https://github.com/espressif/esp-idf/issues/1406 */
bool esp32_bt_wipe_config(void);

esp_gatt_status_t esp32_bt_gatt_get_status(enum mgos_bt_gatt_status st);
//esp_gatt_status_t esp32_bt_gatt_get_status(enum mgos_bt_gatt_status st);

#ifdef __cplusplus
}
Expand Down
6 changes: 6 additions & 0 deletions include/mgos_bt_gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ bool mgos_bt_gap_set_scan_rsp_data(struct mg_str scan_rsp_data);
bool mgos_bt_gap_get_adv_enable(void);
bool mgos_bt_gap_set_adv_enable(bool adv_enable);

int mgos_bt_gap_get_num_paired_devices(void);

void mgos_bt_gap_remove_paired_device(const struct mgos_bt_addr addr);

void mgos_bt_gap_remove_all_paired_devices(void);

#ifdef __cplusplus
}
#endif
21 changes: 21 additions & 0 deletions mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ config_schema:
- ["bt.gatts.min_sec_level", "i", 0, {title: "0 - no auth required, 1 - encryption reqd, 2 - encryption + MITM reqd"}]
- ["bt.gatts.require_pairing", "b", false, {title: "Require device to be paired before accessing services"}]

conds:
- when: mos.platform == "esp32"
apply:
build_vars:
ESP_IDF_EXTRA_COMPONENTS: "${build_vars.ESP_IDF_EXTRA_COMPONENTS} bt esp_wifi wpa_supplicant"
ESP_IDF_SDKCONFIG_OPTS: >
${build_vars.ESP_IDF_SDKCONFIG_OPTS}
CONFIG_BT_ENABLED=y
CONFIG_BT_NIMBLE_ENABLED=y
CONFIG_BT_NIMBLE_ROLE_BROADCASTER=n
CONFIG_BT_NIMBLE_ROLE_OBSERVER=n
CONFIG_BT_NIMBLE_ROLE_CENTRAL=n
CONFIG_BT_NIMBLE_ROLE_PERIPHERAL=y
CONFIG_BT_NIMBLE_MAX_CONNECTIONS=7
CONFIG_BT_NIMBLE_DEBUG=n
CONFIG_BT_NIMBLE_TASK_STACK_SIZE=6144
CONFIG_BT_NIMBLE_L2CAP_COC_MAX_NUM=0
CONFIG_BT_NIMBLE_GAP_DEVICE_NAME_MAX_LEN=50
# CONFIG_BT_NIMBLE_CRYPTO_STACK_MBEDTLS=y

tags:
- bt
- bluetooth
Expand Down
37 changes: 0 additions & 37 deletions mos_esp32.yml

This file was deleted.

114 changes: 71 additions & 43 deletions src/esp32/esp32_bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,20 @@
#include <stdbool.h>
#include <stdlib.h>

#include "esp_bt.h"
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#include "esp_gatt_common_api.h"
#include "nvs.h"

#include "common/mg_str.h"

#include "mgos_hal.h"
#include "mgos_net.h"
#include "mgos_sys_config.h"

#include "esp_nimble_hci.h"
#include "host/ble_hs.h"
#include "host/util/util.h"
#include "nimble/nimble_port.h"
#include "nimble/nimble_port_freertos.h"
#include "services/gap/ble_svc_gap.h"


const char *esp32_bt_addr_to_str(const esp_bd_addr_t addr, char *out) {
return mgos_bt_addr_to_str((const struct mgos_bt_addr *) &addr[0], 0, out);
}
Expand Down Expand Up @@ -75,38 +76,67 @@ static void mgos_bt_net_ev(int ev, void *evd, void *arg) {
if (ev != MGOS_NET_EV_IP_ACQUIRED) return;
LOG(LL_INFO, ("Network is up, disabling Bluetooth"));
mgos_sys_config_set_bt_enable(false);
#if 0 // TODO
char *msg = NULL;
if (save_cfg(&mgos_sys_config, &msg)) {
esp_bt_controller_disable();
esp_bt_controller_deinit();
esp_bt_controller_mem_release(ESP_BT_MODE_BLE);
}
#endif
(void) arg;
}

int mgos_bt_gap_get_num_paired_devices(void) {
return esp_ble_get_bond_device_num();
int count = 0;
ble_store_util_count(BLE_STORE_OBJ_TYPE_OUR_SEC, &count);
return count;
}

/* Workaround for https://github.com/espressif/esp-idf/issues/1406 */
bool esp32_bt_wipe_config(void) {
bool result = false;
nvs_handle h = 0;
/* CONFIG_FILE_PATH form btc_config.c */
if (nvs_open("bt_config.conf", NVS_READWRITE, &h) != ESP_OK) goto clean;
if (nvs_erase_key(h, "bt_cfg_key") != ESP_OK) goto clean;
result = true;

clean:
if (h != 0) nvs_close(h);
return result;
// TODO
return false;
}

static void _on_reset(int reason) {
LOG(LL_ERROR, ("Resetting state; reason=%d", reason));
}

static void _on_sync(void) {
int rc;

rc = ble_hs_util_ensure_addr(mgos_sys_config_get_bt_random_address());
if (rc != 0) {
LOG(LL_ERROR, ("ble_hs_util_ensure_addr rc=%d", rc));
return;
}

uint8_t own_addr_type;
rc = ble_hs_id_infer_auto(0, &own_addr_type);
if (rc != 0) {
LOG(LL_ERROR, ("error determining address type; rc=%d", rc));
return;
}

uint8_t addr_val[6] = {0};
rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
char addr[18] = {0};
LOG(LL_INFO, ("BLE Device Address: %s", esp32_bt_addr_to_str(addr_val, addr)));

mgos_bt_gap_set_adv_enable(mgos_sys_config_get_bt_adv_enable());
}

static void ble_host_task(void *param) {
nimble_port_run();
nimble_port_freertos_deinit();
}

extern void ble_store_config_init(void);

bool mgos_bt_common_init(void) {
bool ret = false;
if (!mgos_sys_config_get_bt_enable()) {
LOG(LL_INFO, ("Bluetooth is disabled"));
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
return true;
}

Expand All @@ -117,38 +147,32 @@ bool mgos_bt_common_init(void) {
free(dev_name);
}

esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);

esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
esp_err_t err = esp_bt_controller_init(&bt_cfg);
esp_err_t err = esp_nimble_hci_and_controller_init();
if (err) {
LOG(LL_ERROR, ("BT init failed: %d", err));
goto out;
}
err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (err) {
LOG(LL_ERROR, ("BT enable failed: %d", err));
goto out;
}
err = esp_bluedroid_init();
if (err != ESP_OK) {
LOG(LL_ERROR, ("bluedroid init failed: %d", err));
goto out;
}
err = esp_bluedroid_enable();
if (err != ESP_OK) {
LOG(LL_ERROR, ("bluedroid enable failed: %d", err));
goto out;
}

nimble_port_init();

ble_hs_cfg.reset_cb = _on_reset;
ble_hs_cfg.sync_cb = _on_sync;
//ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb;
ble_hs_cfg.store_status_cb = ble_store_util_status_rr;

ble_hs_cfg.sm_sc = true;
ble_hs_cfg.sm_io_cap = BLE_SM_IO_CAP_NO_IO;
ble_hs_cfg.sm_bonding = mgos_sys_config_get_bt_allow_pairing();
ble_hs_cfg.sm_mitm = true;

ble_att_set_preferred_mtu(mgos_sys_config_get_bt_gatt_mtu());

if (!esp32_bt_gap_init()) {
LOG(LL_ERROR, ("GAP init failed"));
ret = false;
goto out;
}

esp_ble_gatt_set_local_mtu(mgos_sys_config_get_bt_gatt_mtu());

#if 0
if (!esp32_bt_gattc_init()) {
LOG(LL_ERROR, ("GATTC init failed"));
ret = false;
Expand All @@ -160,11 +184,15 @@ bool mgos_bt_common_init(void) {
ret = false;
goto out;
}

#endif
if (!mgos_sys_config_get_bt_keep_enabled()) {
mgos_event_add_group_handler(MGOS_EVENT_GRP_NET, mgos_bt_net_ev, NULL);
}

ble_store_config_init();

nimble_port_freertos_init(ble_host_task);

LOG(LL_INFO, ("Bluetooth init ok, MTU %d, pairing %s, %d paired devices",
mgos_sys_config_get_bt_gatt_mtu(),
(mgos_bt_gap_get_pairing_enable() ? "enabled" : "disabled"),
Expand Down
Loading

0 comments on commit 836772d

Please sign in to comment.