Watch
0
0
Fork
You've already forked esp32-c_its-companion
0

ble: Set short name in advertisement

This commit is contained in:
Jannik Beyerstedt 2026-09-29 15:25:16 +02:00
commit 9a820356ff
2 changed files with 12 additions and 6 deletions

View file

@ -110,6 +110,7 @@ pub async fn run(
CONTROLLER_SLOTS, CONTROLLER_SLOTS,
>, >,
name: alloc::string::String, name: alloc::string::String,
short_name: alloc::string::String,
) { ) {
let mut resources: HostResources<DefaultPacketPool, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> = let mut resources: HostResources<DefaultPacketPool, CONNECTIONS_MAX, L2CAP_CHANNELS_MAX> =
HostResources::new(); HostResources::new();
@ -120,6 +121,7 @@ pub async fn run(
.. ..
} = stack.build(); } = stack.build();
info!("BLE name: {name} (short: {short_name})");
info!( info!(
"Starting advertising and GATT service for {}", "Starting advertising and GATT service for {}",
crate::util::get_rx_feature_list().join(",") crate::util::get_rx_feature_list().join(",")
@ -132,7 +134,7 @@ pub async fn run(
let _ = embassy_futures::join::join(ble_task(runner), async { let _ = embassy_futures::join::join(ble_task(runner), async {
loop { loop {
match advertise(&mut peripheral, &server).await { match advertise(&mut peripheral, &server, &short_name).await {
Ok(conn) => { Ok(conn) => {
info!("BLE: New client connected"); info!("BLE: New client connected");
@ -176,16 +178,19 @@ pub fn update(payload: BleSendable) {
async fn advertise<'values, 'server, C: Controller>( async fn advertise<'values, 'server, C: Controller>(
peripheral: &mut Peripheral<'values, C, DefaultPacketPool>, peripheral: &mut Peripheral<'values, C, DefaultPacketPool>,
server: &'server Server<'values>, server: &'server Server<'values>,
// name in advertisement, max. 8 characters
short_name: &'values str,
) -> Result<GattConnection<'values, 'server, DefaultPacketPool>, BleHostError<C::Error>> { ) -> Result<GattConnection<'values, 'server, DefaultPacketPool>, BleHostError<C::Error>> {
let mut advertiser_data = [0; 35]; let mut advertiser_data = [0; 31]; // 37 bytes payload - 6 byte address
let service_uuid = Uuid::from(0xc0b7_0000_d4f4_4000_ada8_f99a_02ee_315c_u128) let service_uuid = Uuid::from(0xc0b7_0000_d4f4_4000_ada8_f99a_02ee_315c_u128)
.as_raw() .as_raw()
.try_into() .try_into()
.unwrap(); .unwrap();
let len = AdStructure::encode_slice( let len = AdStructure::encode_slice(
&[ &[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED), AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED), // 2+ 1 byte
AdStructure::ServiceUuids128(&[service_uuid]), AdStructure::ServiceUuids128(&[service_uuid]), // 2+16 byte = 21
AdStructure::ShortenedLocalName(short_name.as_bytes()), // 2+ 8 byte = 31
], ],
&mut advertiser_data[..], &mut advertiser_data[..],
)?; )?;

View file

@ -222,12 +222,13 @@ async fn main(spawner: Spawner) -> ! {
let controller = ble::make_controller(peripherals.BT); let controller = ble::make_controller(peripherals.BT);
let serial = &mac.as_bytes()[3..6]; let serial = &mac.as_bytes()[3..6];
let name = alloc::format!( let name = alloc::format!(
"ESP32 C-ITS {:02x}{:02x}{:02x}", "C-ITS ESP {:02x}:{:02x}:{:02x}",
serial[0], serial[0],
serial[1], serial[1],
serial[2] serial[2]
); );
spawner.spawn(ble::run(controller, name).expect("Failed to spawn BLE task")); let short_name = alloc::format!("V2X {:02x}{:02x}", serial[1], serial[2]); // max 8 chars!
spawner.spawn(ble::run(controller, name, short_name).expect("Failed to spawn BLE task"));
} }
// Setup GNSS: // Setup GNSS: