From 56ed9e41ceb1dbf923af784b7ee5be7f4c4ac801 Mon Sep 17 00:00:00 2001 From: taks <857tn859@gmail.com> Date: Tue, 14 Nov 2023 13:04:20 +0900 Subject: [PATCH] Added BLEAdvertising.advertisement_type function. --- src/enums.rs | 11 +++++++++++ src/server/ble_advertising.rs | 19 +++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/enums.rs b/src/enums.rs index 024e05b..90231c5 100644 --- a/src/enums.rs +++ b/src/enums.rs @@ -81,6 +81,17 @@ pub enum PowerType { Default = esp_ble_power_type_t_ESP_BLE_PWR_TYPE_DEFAULT as _, } +#[repr(u8)] +#[derive(Copy, Clone, PartialEq, Debug)] +pub enum ConnMode { + /// non-connectable (3.C.9.3.2) + Non = esp_idf_sys::BLE_GAP_CONN_MODE_NON as _, + /// directed-connectable (3.C.9.3.3) + Dir = esp_idf_sys::BLE_GAP_CONN_MODE_DIR as _, + /// undirected-connectable (3.C.9.3.4) + Und = esp_idf_sys::BLE_GAP_CONN_MODE_UND as _, +} + bitflags! { #[repr(transparent)] #[derive(Debug, Clone, Copy, PartialEq, Eq)] diff --git a/src/server/ble_advertising.rs b/src/server/ble_advertising.rs index 0504959..8917b1b 100644 --- a/src/server/ble_advertising.rs +++ b/src/server/ble_advertising.rs @@ -1,6 +1,6 @@ use core::ffi::c_void; -use crate::{ble, enums::PowerType, utilities::BleUuid, BLEDevice, BLEReturnCode, BLEServer}; +use crate::{ble, enums::*, utilities::BleUuid, BLEDevice, BLEReturnCode, BLEServer}; use alloc::{ffi::CString, vec::Vec}; use once_cell::sync::Lazy; @@ -204,6 +204,12 @@ impl BLEAdvertising { } } + /// Set the type of advertisment to use. + pub fn advertisement_type(&mut self, adv_type: ConnMode) -> &mut Self { + self.adv_params.conn_mode = adv_type as _; + self + } + /// Set if scan response is available. pub fn scan_response(&mut self, value: bool) -> &mut Self { self.scan_response = value; @@ -227,9 +233,14 @@ impl BLEAdvertising { } } - self.adv_params.disc_mode = esp_idf_sys::BLE_GAP_DISC_MODE_GEN as _; - self.adv_data.flags = - (esp_idf_sys::BLE_HS_ADV_F_DISC_GEN | esp_idf_sys::BLE_HS_ADV_F_BREDR_UNSUP) as _; + if self.adv_params.conn_mode == (ConnMode::Non as _) && !self.scan_response { + self.adv_params.disc_mode = esp_idf_sys::BLE_GAP_DISC_MODE_NON as _; + self.adv_data.flags = 0; + } else { + self.adv_params.disc_mode = esp_idf_sys::BLE_GAP_DISC_MODE_GEN as _; + self.adv_data.flags = + (esp_idf_sys::BLE_HS_ADV_F_DISC_GEN | esp_idf_sys::BLE_HS_ADV_F_BREDR_UNSUP) as _; + } if !self.custom_adv_data && !self.adv_data_set { let mut payload_len: u8 = if self.adv_data.flags > 0 { 2 + 1 } else { 0 };