Skip to content

Commit

Permalink
Nimble: Add LE Power CTRL changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rahult-github committed Dec 6, 2022
1 parent 0e04bff commit e8e79e8
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
11 changes: 11 additions & 0 deletions components/bt/host/nimble/Kconfig.in
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,17 @@ choice BT_NIMBLE_COEX_PHY_CODED_TX_RX_TLIM
Disable the limitation on max tx/rx time for Coded-PHY connection
endchoice

menuconfig BT_NIMBLE_52_FEATURE_SUPPORT
bool "Enable BLE 5.2 Feature"
help
Enable this option to select 5.2 features

config BT_NIMBLE_BLE_POWER_CONTROL
bool "Enable support for BLE Power Control"
depends on BT_NIMBLE_52_FEATURE_SUPPORT && SOC_ESP_NIMBLE_CONTROLLER
help
Set this option to enable the Power Control feature

config BT_NIMBLE_COEX_PHY_CODED_TX_RX_TLIM_EFF
int
default 0 if !(ESP32_WIFI_SW_COEXIST_ENABLE && BT_NIMBLE_ENABLED)
Expand Down
4 changes: 4 additions & 0 deletions components/bt/host/nimble/port/include/esp_nimble_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -1595,4 +1595,8 @@
#endif
#endif

#ifndef MYNEWT_VAL_BLE_POWER_CONTROL
#define MYNEWT_VAL_BLE_POWER_CONTROL CONFIG_BT_NIMBLE_BLE_POWER_CONTROL
#endif

#endif
62 changes: 62 additions & 0 deletions components/bt/porting/nimble/include/nimble/hci_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,48 @@ struct ble_hci_cb_wr_auth_pyld_tmo_rp {
uint16_t conn_handle;
} __attribute__((packed));

#define BLE_HCI_OCF_LE_ENH_READ_TRANSMIT_POWER_LEVEL (0x0076)
struct ble_hci_le_enh_read_transmit_power_level_cp {
uint16_t conn_handle;
uint8_t phy;
} __attribute__((packed));
struct ble_hci_le_enh_read_transmit_power_level_rp {
uint8_t status;
uint16_t conn_handle;
uint8_t phy;
uint8_t curr_tx_power_level;
uint8_t max_tx_power_level;
} __attribute__((packed));

#define BLE_HCI_OCF_LE_READ_REMOTE_TRANSMIT_POWER_LEVEL (0x0077)
struct ble_hci_le_read_remote_transmit_power_level_cp {
uint16_t conn_handle;
uint8_t phy;
} __attribute__((packed));

#define BLE_HCI_OCF_LE_SET_PATH_LOSS_REPORT_PARAM (0x0078)
struct ble_hci_le_set_path_loss_report_param_cp {
uint16_t conn_handle;
uint8_t high_threshold;
uint8_t high_hysteresis;
uint8_t low_threshold;
uint8_t low_hysteresis;
uint16_t min_time_spent;
} __attribute__((packed));

#define BLE_HCI_OCF_LE_SET_PATH_LOSS_REPORT_ENABLE (0x0079)
struct ble_hci_le_set_path_loss_report_enable_cp {
uint16_t conn_handle;
uint8_t enable;
} __attribute__((packed));

#define BLE_HCI_OCF_LE_SET_TRANS_PWR_REPORT_ENABLE (0x007A)
struct ble_hci_le_set_transmit_power_report_enable_cp {
uint16_t conn_handle;
uint8_t local_enable;
uint8_t remote_enable;
} __attribute__((packed));

/* List of OCF for Info Param commands (OGF=0x04) */
#define BLE_HCI_OCF_IP_RD_LOCAL_VER (0x0001)
struct ble_hci_ip_rd_local_ver_rp {
Expand Down Expand Up @@ -1798,6 +1840,26 @@ struct ble_hci_ev_le_subev_peer_sca_complete {
uint8_t sca;
} __attribute__((packed));

#define BLE_HCI_LE_SUBEV_PATH_LOSS_THRESHOLD (0x20)
struct ble_hci_ev_le_subev_path_loss_threshold {
uint8_t subev_code;
uint16_t conn_handle;
uint8_t current_path_loss;
uint8_t zone_entered;
} __attribute__((packed));

#define BLE_HCI_LE_SUBEV_TRANSMIT_POWER_REPORT (0x21)
struct ble_hci_ev_le_subev_transmit_power_report {
uint8_t subev_code;
uint8_t status;
uint16_t conn_handle;
uint8_t reason;
uint8_t phy;
uint8_t transmit_power_level;
uint8_t transmit_power_level_flag;
uint8_t delta;
} __attribute__((packed));

#define BLE_HCI_LE_SUBEV_BIGINFO_ADV_REPORT (0x22)
struct ble_hci_ev_le_subev_biginfo_adv_report {
uint8_t subev_code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration
#
CONFIG_IDF_TARGET="esp32c2"
# CONFIG_EXAMPLE_EXTENDED_ADV is not set
CONFIG_EXAMPLE_EXTENDED_ADV=y
CONFIG_BT_ENABLED=y
CONFIG_BT_NIMBLE_ENABLED=y
# CONFIG_BT_NIMBLE_50_FEATURE_SUPPORT is not set
47 changes: 47 additions & 0 deletions examples/bluetooth/nimble/bleprph/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ static uint8_t own_addr_type;

void ble_store_config_init(void);

#if MYNEWT_VAL(BLE_POWER_CONTROL)
static struct ble_gap_event_listener power_control_event_listener;
#endif

/**
* Logs information about a connection to the console.
*/
Expand Down Expand Up @@ -197,6 +201,20 @@ bleprph_advertise(void)
}
#endif

#if MYNEWT_VAL(BLE_POWER_CONTROL)
static void bleprph_power_control(uint16_t conn_handle)
{
int rc;

rc = ble_gap_read_remote_transmit_power_level(conn_handle, 0x01 ); // Attempting on LE 1M phy
assert (rc == 0);

rc = ble_gap_set_transmit_power_reporting_enable(conn_handle, 0x1, 0x1);
assert (rc == 0);
}
#endif


/**
* The nimble host executes this callback when a GAP event occurs. The
* application associates a GAP event callback with each connection that forms.
Expand Down Expand Up @@ -239,6 +257,13 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
bleprph_advertise();
#endif
}

#if MYNEWT_VAL(BLE_POWER_CONTROL)
bleprph_power_control(event->connect.conn_handle);

ble_gap_event_listener_register(&power_control_event_listener,
bleprph_gap_event, NULL);
#endif
return 0;

case BLE_GAP_EVENT_DISCONNECT:
Expand Down Expand Up @@ -361,6 +386,28 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg)
ESP_LOGI(tag, "ble_sm_inject_io result: %d\n", rc);
}
return 0;

#if MYNEWT_VAL(BLE_POWER_CONTROL)
case BLE_GAP_EVENT_TRANSMIT_POWER:
MODLOG_DFLT(INFO, "Transmit power event : status=%d conn_handle=%d reason=%d "
"phy=%d power_level=%x power_level_flag=%d delta=%d",
event->transmit_power.status,
event->transmit_power.conn_handle,
event->transmit_power.reason,
event->transmit_power.phy,
event->transmit_power.transmit_power_level,
event->transmit_power.transmit_power_level_flag,
event->transmit_power.delta);
return 0;

case BLE_GAP_EVENT_PATHLOSS_THRESHOLD:
MODLOG_DFLT(INFO, "Pathloss threshold event : conn_handle=%d current path loss=%d "
"zone_entered =%d",
event->pathloss_threshold.conn_handle,
event->pathloss_threshold.current_path_loss,
event->pathloss_threshold.zone_entered);
return 0;
#endif
}

return 0;
Expand Down

0 comments on commit e8e79e8

Please sign in to comment.