Skip to content

Commit

Permalink
Merge pull request #17 from bjoernQ/housekeeping
Browse files Browse the repository at this point in the history
Housekeeping
  • Loading branch information
bjoernQ committed May 24, 2023
2 parents 0cc1532 + a2f2402 commit b82f1e7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 52 deletions.
20 changes: 16 additions & 4 deletions bleps-macros/tests/macro_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use bleps_macros::gatt;

#[test]
fn test() {
let mut my_read_function = || &b"Hello"[..];
let mut my_read_function = |_offset: usize, data: &mut [u8]| {
data[..5].copy_from_slice(&b"Hola!"[..]);
5
};
let mut my_write_function = |_offset, data: &[u8]| {
println!("{:?}", data);
};
Expand All @@ -21,7 +24,10 @@ fn test() {

#[test]
fn test2() {
let mut my_read_function = || &b"Hello"[..];
let mut my_read_function = |_offset: usize, data: &mut [u8]| {
data[..6].copy_from_slice(&b"Hello!"[..]);
6
};
let mut my_write_function = |_offset, data: &[u8]| {
println!("{:?}", data);
};
Expand All @@ -40,7 +46,10 @@ fn test2() {

#[test]
fn test3() {
let mut my_read_function = || &b"Hello"[..];
let mut my_read_function = |_offset: usize, data: &mut [u8]| {
data[..5].copy_from_slice(&b"Hola!"[..]);
5
};
let mut my_write_function = |_offset, data: &[u8]| {
println!("{:?}", data);
};
Expand All @@ -60,7 +69,10 @@ fn test3() {

#[test]
fn test4() {
let mut my_read_function = || &b"Hello"[..];
let mut my_read_function = |_offset: usize, data: &mut [u8]| {
data[..5].copy_from_slice(&b"Hola!"[..]);
5
};
let mut my_write_function = |_offset, data: &[u8]| {
println!("{:?}", data);
};
Expand Down
13 changes: 11 additions & 2 deletions bleps/src/ad_structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ pub const BR_EDR_NOT_SUPPORTED: u8 = 0b00000100;
pub const SIMUL_LE_BR_CONTROLLER: u8 = 0b00001000;
pub const SIMUL_LE_BR_HOST: u8 = 0b00010000;

#[derive(Debug, Copy, Clone)]
pub enum AdvertisementDataError {
TooLong,
}

#[derive(Debug, Copy, Clone)]
pub enum AdStructure<'a> {
/// Device flags and baseband capabilities.
Expand Down Expand Up @@ -94,7 +99,7 @@ impl Data {
}
}

pub fn create_advertising_data(ad: &[AdStructure]) -> Data {
pub fn create_advertising_data(ad: &[AdStructure]) -> Result<Data, AdvertisementDataError> {
let mut data = Data::default();
data.append(&[0]);

Expand All @@ -105,9 +110,13 @@ pub fn create_advertising_data(ad: &[AdStructure]) -> Data {
let len = data.len - 1;
data.set(0, len as u8);

if len > 31 {
return Err(AdvertisementDataError::TooLong);
}

for _ in 0..(31 - len) {
data.append(&[0]);
}

data
Ok(data)
}
55 changes: 35 additions & 20 deletions bleps/tests/test_ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ use std::{assert_matches::assert_matches, cell::RefCell};

extern crate std;

use bleps::ad_structure::AdvertisementDataError;
use bleps::{
acl::{encode_acl_packet, AclPacket, BoundaryFlag, ControllerBroadcastFlag, HostBroadcastFlag},
acl::{AclPacket, BoundaryFlag, ControllerBroadcastFlag, HostBroadcastFlag},
ad_structure::{
create_advertising_data, AdStructure, BR_EDR_NOT_SUPPORTED, LE_GENERAL_DISCOVERABLE,
},
att::{parse_att, Att, AttErrorCode, Uuid, ATT_READ_BY_GROUP_TYPE_REQUEST_OPCODE},
att::{Att, AttErrorCode, Uuid, ATT_READ_BY_GROUP_TYPE_REQUEST_OPCODE},
attribute::Attribute,
attribute_server::{AttributeServer, CHARACTERISTIC_UUID16, PRIMARY_SERVICE_UUID16},
command::{create_command_data, Command, CommandHeader},
command::{Command, CommandHeader},
event::{ErrorCode, EventType},
l2cap::{encode_l2cap, parse_l2cap},
l2cap::L2capPacket,
Ble, Data, HciConnection, PollResult,
};

Expand Down Expand Up @@ -243,14 +244,14 @@ pub fn command_header_set_adv_param_works() {

#[test]
fn create_reset_command_works() {
let data = create_command_data(Command::Reset);
let data = Command::Reset.encode();
assert_eq!(data.len, 4);
assert_eq!(data.data[0..4], [0x01, 0x03, 0x0c, 0x00]);
}

#[test]
fn create_le_set_advertising_parameters_works() {
let data = create_command_data(Command::LeSetAdvertisingParameters);
let data = Command::LeSetAdvertisingParameters.encode();
assert_eq!(data.len, 19);
assert_eq!(
data.data[..19],
Expand All @@ -272,9 +273,10 @@ fn set_advertising_parameters_works() {

#[test]
fn create_le_set_advertising_data_works() {
let data = create_command_data(Command::LeSetAdvertisingData {
let data = Command::LeSetAdvertisingData {
data: Data::new(&[1, 2, 3, 4, 5]),
});
}
.encode();
assert_eq!(data.len, 9);
assert_eq!(data.data[..9], [0x01, 0x08, 0x20, 0x05, 1, 2, 3, 4, 5]);
}
Expand All @@ -293,7 +295,7 @@ fn le_set_advertising_data_works() {

#[test]
fn create_le_set_advertise_enable_works() {
let data = create_command_data(Command::LeSetAdvertiseEnable(true));
let data = Command::LeSetAdvertiseEnable(true).encode();
assert_eq!(data.len, 5);
assert_eq!(data.data[..5], [0x01, 0x0a, 0x20, 0x01, 0x01]);
}
Expand Down Expand Up @@ -385,7 +387,7 @@ fn receiving_read_by_group_type_works() {
Some(res) => match res {
PollResult::Event(_) => assert!(true, "Expected async data"),
PollResult::AsyncData(res) => {
let res = parse_att(parse_l2cap(res).unwrap().1);
let res = Att::decode(L2capPacket::decode(res).unwrap().1);
assert_matches!(
res,
Ok(Att::ReadByGroupTypeReq {
Expand Down Expand Up @@ -417,8 +419,8 @@ fn create_read_by_group_type_resp_acl_works() {
let mut res = Data::new_att_read_by_group_type_response();
res.append_att_read_by_group_type_response(0x0001, 0x0010, &Uuid::Uuid16(0x1801));
res.append_att_read_by_group_type_response(0x0020, 0x0030, &Uuid::Uuid16(0x1802));
let res = encode_l2cap(res);
let res = encode_acl_packet(
let res = L2capPacket::encode(res);
let res = AclPacket::encode(
0x0000,
BoundaryFlag::FirstAutoFlushable,
HostBroadcastFlag::NoBroadcast,
Expand Down Expand Up @@ -460,7 +462,7 @@ fn receiving_read_by_type_works() {
Some(res) => match res {
PollResult::Event(_) => assert!(true, "Expected async data"),
PollResult::AsyncData(res) => {
let res = parse_att(parse_l2cap(res).unwrap().1);
let res = Att::decode(L2capPacket::decode(res).unwrap().1);
assert_matches!(
res,
Ok(Att::ReadByTypeReq {
Expand Down Expand Up @@ -506,7 +508,7 @@ fn receiving_read_works() {
Some(res) => match res {
PollResult::Event(_) => assert!(true, "Expected async data"),
PollResult::AsyncData(res) => {
let res = parse_att(parse_l2cap(res).unwrap().1);
let res = Att::decode(L2capPacket::decode(res).unwrap().1);
assert_matches!(res, Ok(Att::ReadReq { handle: 0x03 }))
}
},
Expand Down Expand Up @@ -536,7 +538,7 @@ fn receiving_write_works() {
Some(res) => match res {
PollResult::Event(_) => assert!(true, "Expected async data"),
PollResult::AsyncData(res) => {
let res = parse_att(parse_l2cap(res).unwrap().1);
let res = Att::decode(L2capPacket::decode(res).unwrap().1);
assert_matches!(
res,
Ok(Att::WriteReq {
Expand All @@ -562,21 +564,34 @@ fn create_advertising_data_works() {
let res = create_advertising_data(&[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
AdStructure::ServiceUuids16(&[Uuid::Uuid16(0x1809)]),
AdStructure::CompleteLocalName("BL-602 Ble-Example!"),
]);
AdStructure::CompleteLocalName("Ble-Example!"),
])
.unwrap();

println!("{:x?}", res);

assert_matches!(
res.as_slice(),
&[
0x1c, 0x02, 0x01, 0x06, 0x03, 0x02, 0x09, 0x18, 0x14, 0x09, 0x42, 0x4C, 0x2D, 0x36,
0x30, 0x32, 0x20, 0x42, 0x6C, 0x65, 0x2D, 0x45, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65,
0x21, 0x00, 0x00, 0x00
21, 2, 1, 6, 3, 2, 9, 24, 13, 9, 66, 108, 101, 45, 69, 120, 97, 109, 112, 108, 101, 33,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
]
);
}

#[test]
fn create_advertising_data_fails() {
let res = create_advertising_data(&[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
AdStructure::ServiceUuids16(&[Uuid::Uuid16(0x1809)]),
AdStructure::CompleteLocalName(
"Ble-Example!Ble-Example!Ble-Example!Ble-Example!Ble-Example!Ble-Example!Ble-Example!",
),
]);

assert_matches!(res, Err(AdvertisementDataError::TooLong));
}

#[test]
fn attribute_server_discover_two_services() {
let connector = connector();
Expand Down
15 changes: 5 additions & 10 deletions esp32s3-serial-hci/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ license = "MIT OR Apache-2.0"
opt-level = 3

[dependencies]
esp32s3-hal = "0.4.0"
esp-backtrace = { version = "0.4.0", features = ["esp32s3", "panic-handler", "print-uart"] }
esp-println = { version = "0.3.1", features = [ "esp32s3", "log" ] }
xtensa-lx-rt = { version = "0.14.0", features = ["esp32s3"], optional = true }
esp-wifi = { git = "https://github.com/esp-rs/esp-wifi", rev = "dbcd5ea4989ff7c295b799c1beb6ffecd78518e3", features = ["esp32s3", "ble"] }
embedded-io = "0.3.1"
esp32s3-hal = "0.9.0"
esp-backtrace = { version = "0.7.0", features = ["esp32s3", "panic-handler", "print-uart"] }
esp-println = { version = "0.5.0", features = [ "esp32s3", "log" ] }
esp-wifi = { git = "https://github.com/esp-rs/esp-wifi", rev = "dad30df82aabc76e81c341845f260d5049e0f304", features = ["esp32s3", "ble"] }
embedded-io = "0.4.0"
log = "0.4.17"

[features]
default = ["rt"]
rt = ["xtensa-lx-rt"]
36 changes: 25 additions & 11 deletions esp32s3-serial-hci/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,56 @@
use embedded_io::blocking::{Read, Write};
use esp32s3_hal::{
clock::{ClockControl, CpuClock},
pac::Peripherals,
peripherals::Peripherals,
prelude::*,
timer::TimerGroup,
Rng, Rtc, Serial,
Rng, Rtc, Uart,
};
use esp_backtrace as _;
use esp_println::logger::init_logger;
use esp_wifi::ble::controller::BleConnector;

const CNT: usize = 5000;

#[xtensa_lx_rt::entry]
#[entry]
fn main() -> ! {
init_logger(log::LevelFilter::Off);
esp_wifi::init_heap();

let peripherals = Peripherals::take().unwrap();
let system = peripherals.SYSTEM.split();
let peripherals = Peripherals::take();
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::configure(system.clock_control, CpuClock::Clock240MHz).freeze();

// Disable the RTC and TIMG watchdog timers
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
let timer_group0 = TimerGroup::new(peripherals.TIMG0, &clocks);
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(peripherals.TIMG1, &clocks);
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt1 = timer_group1.wdt;

rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();

esp_wifi::initialize(timer_group1.timer0, Rng::new(peripherals.RNG), &clocks).unwrap();
let bt = peripherals.RADIO.split().1;
esp_wifi::initialize(
timer_group1.timer0,
Rng::new(peripherals.RNG),
system.radio_clock_control,
&clocks,
)
.unwrap();

let mut connector = BleConnector {};
let mut connector = BleConnector::new(bt);

let mut serial = Serial::new(peripherals.UART0);
let mut serial = Uart::new(peripherals.UART0, &mut system.peripheral_clock_control);

serial.write(0xff).unwrap();

Expand Down
13 changes: 8 additions & 5 deletions example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ fn main() {
println!("{:?}", ble.cmd_set_le_advertising_parameters());
println!(
"{:?}",
ble.cmd_set_le_advertising_data(create_advertising_data(&[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
AdStructure::ServiceUuids16(&[Uuid::Uuid16(0x1809)]),
AdStructure::CompleteLocalName("BLEPS"),
]))
ble.cmd_set_le_advertising_data(
create_advertising_data(&[
AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
AdStructure::ServiceUuids16(&[Uuid::Uuid16(0x1809)]),
AdStructure::CompleteLocalName("BLEPS"),
])
.unwrap()
)
);
println!("{:?}", ble.cmd_set_le_advertise_enable(true));

Expand Down

0 comments on commit b82f1e7

Please sign in to comment.