Skip to content

Commit 197a8de

Browse files
committed
feat: add all service advertisement variants
1 parent b935bcf commit 197a8de

File tree

6 files changed

+82
-18
lines changed

6 files changed

+82
-18
lines changed

examples/apps/src/ble_bas_peripheral.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ async fn advertise<'values, 'server, C: Controller>(
145145
let len = AdStructure::encode_slice(
146146
&[
147147
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
148-
AdStructure::ServiceUuids16(&[[0x0f, 0x18]]),
148+
AdStructure::IncompleteServiceUuids16(&[[0x0f, 0x18]]),
149149
AdStructure::CompleteLocalName(name.as_bytes()),
150150
],
151151
&mut advertiser_data[..],

examples/apps/src/ble_bas_peripheral_auth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ async fn advertise<'values, 'server, C: Controller>(
197197
let len = AdStructure::encode_slice(
198198
&[
199199
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
200-
AdStructure::ServiceUuids16(&[[0x0f, 0x18]]),
200+
AdStructure::IncompleteServiceUuids16(&[[0x0f, 0x18]]),
201201
AdStructure::CompleteLocalName(name.as_bytes()),
202202
],
203203
&mut advertiser_data[..],

examples/apps/src/ble_bas_peripheral_bonding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ async fn advertise<'values, 'server, C: Controller>(
337337
let len = AdStructure::encode_slice(
338338
&[
339339
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
340-
AdStructure::ServiceUuids16(&[
340+
AdStructure::IncompleteServiceUuids16(&[
341341
service::BATTERY.to_le_bytes(),
342342
service::HUMAN_INTERFACE_DEVICE.to_le_bytes(),
343343
]),

examples/apps/src/ble_bas_peripheral_pass_key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ async fn advertise<'values, 'server, C: Controller>(
181181
let len = AdStructure::encode_slice(
182182
&[
183183
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
184-
AdStructure::ServiceUuids16(&[[0x0f, 0x18]]),
184+
AdStructure::IncompleteServiceUuids16(&[[0x0f, 0x18]]),
185185
AdStructure::CompleteLocalName(name.as_bytes()),
186186
],
187187
&mut advertiser_data[..],

examples/apps/src/ble_bas_peripheral_sec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ async fn advertise<'values, 'server, C: Controller>(
177177
let len = AdStructure::encode_slice(
178178
&[
179179
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
180-
AdStructure::ServiceUuids16(&[[0x0f, 0x18]]),
180+
AdStructure::IncompleteServiceUuids16(&[[0x0f, 0x18]]),
181181
AdStructure::CompleteLocalName(name.as_bytes()),
182182
],
183183
&mut advertiser_data[..],

host/src/advertise.rs

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,29 @@ pub enum AdStructure<'a> {
360360
/// Must not be used in scan response data.
361361
Flags(u8),
362362

363-
/// List of 16-bit service UUIDs.
363+
/// Incomplete List of 16-bit service UUIDs.
364364
/// The UUID data matches the ble network's endian order (should be little endian).
365-
ServiceUuids16(&'a [[u8; 2]]),
365+
IncompleteServiceUuids16(&'a [[u8; 2]]),
366366

367-
/// List of 128-bit service UUIDs.
367+
/// Complete List of 16-bit service UUIDs.
368368
/// The UUID data matches the ble network's endian order (should be little endian).
369-
ServiceUuids128(&'a [[u8; 16]]),
369+
CompleteServiceUuids16(&'a [[u8; 2]]),
370+
371+
/// Incomplete List of 32-bit service UUIDs.
372+
/// The UUID data matches the ble network's endian order (should be little endian).
373+
IncompleteServiceUuids32(&'a [[u8; 4]]),
374+
375+
/// Complete List of 32-bit service UUIDs.
376+
/// The UUID data matches the ble network's endian order (should be little endian).
377+
CompleteServiceUuids32(&'a [[u8; 4]]),
378+
379+
/// Incomplete List of 128-bit service UUIDs.
380+
/// The UUID data matches the ble network's endian order (should be little endian).
381+
IncompleteServiceUuids128(&'a [[u8; 16]]),
382+
383+
/// Complete List of 128-bit service UUIDs.
384+
/// The UUID data matches the ble network's endian order (should be little endian).
385+
CompleteServiceUuids128(&'a [[u8; 16]]),
370386

371387
/// Service data with 16-bit service UUID.
372388
/// The UUID data matches the ble network's endian order (should be little endian).
@@ -417,13 +433,37 @@ impl AdStructure<'_> {
417433
AdStructure::Flags(flags) => {
418434
w.append(&[0x02, 0x01, *flags])?;
419435
}
420-
AdStructure::ServiceUuids16(uuids) => {
436+
AdStructure::IncompleteServiceUuids16(uuids) => {
421437
w.append(&[(uuids.len() * 2 + 1) as u8, 0x02])?;
422438
for uuid in uuids.iter() {
423439
w.write_ref(&Uuid::Uuid16(*uuid))?;
424440
}
425441
}
426-
AdStructure::ServiceUuids128(uuids) => {
442+
AdStructure::CompleteServiceUuids16(uuids) => {
443+
w.append(&[(uuids.len() * 2 + 1) as u8, 0x03])?;
444+
for uuid in uuids.iter() {
445+
w.write_ref(&Uuid::Uuid16(*uuid))?;
446+
}
447+
}
448+
AdStructure::IncompleteServiceUuids32(uuids) => {
449+
w.append(&[(uuids.len() * 4 + 1) as u8, 0x04])?;
450+
for uuid in uuids.iter() {
451+
w.write_ref(&Uuid::Uuid32(*uuid))?;
452+
}
453+
}
454+
AdStructure::CompleteServiceUuids32(uuids) => {
455+
w.append(&[(uuids.len() * 4 + 1) as u8, 0x05])?;
456+
for uuid in uuids.iter() {
457+
w.write_ref(&Uuid::Uuid32(*uuid))?;
458+
}
459+
}
460+
AdStructure::IncompleteServiceUuids128(uuids) => {
461+
w.append(&[(uuids.len() * 16 + 1) as u8, 0x06])?;
462+
for uuid in uuids.iter() {
463+
w.write_ref(&Uuid::Uuid128(*uuid))?;
464+
}
465+
}
466+
AdStructure::CompleteServiceUuids128(uuids) => {
427467
w.append(&[(uuids.len() * 16 + 1) as u8, 0x07])?;
428468
for uuid in uuids.iter() {
429469
w.write_ref(&Uuid::Uuid128(*uuid))?;
@@ -485,24 +525,48 @@ impl<'d> AdStructureIter<'d> {
485525
// Flags
486526
0x01 => Ok(AdStructure::Flags(data[0])),
487527
// Incomplete List of 16-bit Service or Service Class UUIDs
488-
// 0x02 =>
528+
0x02 => match zerocopy::FromBytes::ref_from_bytes(data) {
529+
Ok(x) => Ok(AdStructure::IncompleteServiceUuids16(x)),
530+
Err(e) => {
531+
let _ = zerocopy::SizeError::from(e);
532+
Err(codec::Error::InvalidValue)
533+
}
534+
},
489535
// Complete List of 16-bit Service or Service Class UUIDs
490536
0x03 => match zerocopy::FromBytes::ref_from_bytes(data) {
491-
Ok(x) => Ok(AdStructure::ServiceUuids16(x)),
537+
Ok(x) => Ok(AdStructure::CompleteServiceUuids16(x)),
492538
Err(e) => {
493539
let _ = zerocopy::SizeError::from(e);
494540
Err(codec::Error::InvalidValue)
495541
}
496542
},
497543
// Incomplete List of 32-bit Service or Service Class UUIDs
498-
// 0x04 =>
544+
0x04 => match zerocopy::FromBytes::ref_from_bytes(data) {
545+
Ok(x) => Ok(AdStructure::IncompleteServiceUuids32(x)),
546+
Err(e) => {
547+
let _ = zerocopy::SizeError::from(e);
548+
Err(codec::Error::InvalidValue)
549+
}
550+
},
499551
// Complete List of 32-bit Service or Service Class UUIDs
500-
// 0x05
552+
0x05 => match zerocopy::FromBytes::ref_from_bytes(data) {
553+
Ok(x) => Ok(AdStructure::CompleteServiceUuids32(x)),
554+
Err(e) => {
555+
let _ = zerocopy::SizeError::from(e);
556+
Err(codec::Error::InvalidValue)
557+
}
558+
},
501559
// Incomplete List of 128-bit Service or Service Class UUIDs
502-
// 0x06
560+
0x06 => match zerocopy::FromBytes::ref_from_bytes(data) {
561+
Ok(x) => Ok(AdStructure::IncompleteServiceUuids128(x)),
562+
Err(e) => {
563+
let _ = zerocopy::SizeError::from(e);
564+
Err(codec::Error::InvalidValue)
565+
}
566+
},
503567
// Complete List of 128-bit Service or Service Class UUIDs
504568
0x07 => match zerocopy::FromBytes::ref_from_bytes(data) {
505-
Ok(x) => Ok(AdStructure::ServiceUuids128(x)),
569+
Ok(x) => Ok(AdStructure::CompleteServiceUuids128(x)),
506570
Err(e) => {
507571
let _ = zerocopy::SizeError::from(e);
508572
Err(codec::Error::InvalidValue)
@@ -594,7 +658,7 @@ mod tests {
594658
assert!(AdStructure::encode_slice(
595659
&[
596660
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
597-
AdStructure::ServiceUuids16(&[[0x0f, 0x18]]),
661+
AdStructure::IncompleteServiceUuids16(&[[0x0f, 0x18]]),
598662
AdStructure::CompleteLocalName(b"12345678901234567890123"),
599663
],
600664
&mut adv_data[..],

0 commit comments

Comments
 (0)