Skip to content

Commit

Permalink
feat(vendor GAP command): add ADV Set AAdvertising Data command
Browse files Browse the repository at this point in the history
  • Loading branch information
OueslatiGhaith committed Jan 4, 2024
1 parent 5ee3663 commit 3f6116c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/types/extended_advertisement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ pub enum ExtendedAdvertisingIntervalError {
}

/// Advertising PHY
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum AdvertisingPhy {
/// Advertisement PHY is LE 1M
Le1M = 0x01,
Expand Down Expand Up @@ -181,3 +183,19 @@ impl AdvSet {
bytes[3] = self.max_extended_adv_events;
}
}

/// Advertising Operation
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum AdvertisingOperation {
/// Intermediate fragment of fragmented extended advertising data
IntermediateFragment = 0x00,
/// First fragment of fragmented extended advertising data
FirstFragment = 0x01,
/// Last fragment of fragmented extended advertising data
LastFragment = 0x02,
/// Complete extended advertising data
CompleteData = 0x03,
/// Unchanged data (just update the advertising DID)
UnchangedData = 0x04,
}
40 changes: 38 additions & 2 deletions src/vendor/command/gap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate byteorder;

pub use crate::host::{AdvertisingFilterPolicy, AdvertisingType, OwnAddressType};
use crate::types::extended_advertisement::{
AdvSet, AdvertisingEvent, AdvertisingPhy, ExtendedAdvertisingInterval,
AdvSet, AdvertisingEvent, AdvertisingOperation, AdvertisingPhy, ExtendedAdvertisingInterval,
};
pub use crate::types::{ConnectionInterval, ExpectedConnectionLength, ScanWindow};
use crate::{
Expand Down Expand Up @@ -804,12 +804,15 @@ pub trait GapCommands {
/// legacy advertising.
// TODO: add adv_set_scan_response_data
// TODO: add adv_set_advertising_data
// TODO: add adv_set_enable
async fn adv_set_config(&mut self, params: &AdvSetConfig);

/// This command is used to request the Controller to enable or disbale one
/// or more extended advertising sets.
async fn adv_set_enable<'a>(&mut self, params: &AdvSetEnable<'a>);

/// This command is used to set the data used in extended advertising PDUs
/// that have a data field
async fn adv_set_advertising_data(&mut self, params: &AdvSetAdvertisingData);
}

impl<T: Controller> GapCommands for T {
Expand Down Expand Up @@ -1235,6 +1238,12 @@ impl<T: Controller> GapCommands for T {
AdvSetEnable<'a>,
crate::vendor::opcode::GAP_ADV_SET_ENABLE
);

impl_variable_length_params!(
adv_set_advertising_data<'a>,
AdvSetAdvertisingData<'a>,
crate::vendor::opcode::GAP_ADV_SET_ADV_DATA
);
}

/// Potential errors from parameter validation.
Expand Down Expand Up @@ -2510,3 +2519,30 @@ impl<'a> AdvSetEnable<'a> {
}
}
}

/// Params for the [adv_set_advertising_data](GapCommands::adv_set_advertising_data) command
pub struct AdvSetAdvertisingData<'a> {
/// Used to identify an advertising set
pub adv_handle: AdvertisingHandle,
/// Advertising operation
pub operation: AdvertisingOperation,
/// Fragment preference. If set to `true`, the Controller may fragment all data, else
/// the Controller should not fragment or should minimize fragmentation of data
pub fragment: bool,
pub data: &'a [u8],
}

impl<'a> AdvSetAdvertisingData<'a> {
const MAX_LENGTH: usize = 255;

fn copy_into_slice(&self, bytes: &mut [u8]) {
assert!(bytes.len() >= Self::MAX_LENGTH);

bytes[0] = self.adv_handle.0;
bytes[1] = self.operation as u8;
bytes[2] = (!self.fragment) as u8;
let length = self.data.len();
bytes[3] = length as u8;
bytes[4..(4 + length)].copy_from_slice(self.data);
}
}

0 comments on commit 3f6116c

Please sign in to comment.