bjoernQ / bleps

A toy-level BLE peripheral stack
MIT License
55 stars 18 forks source link

Uuid128 off by 1 error in advertising data? #2

Closed brandonros closed 1 year ago

brandonros commented 1 year ago
 let connector = esp_wifi::ble::controller::BleConnector {};
        let hci = HciConnector::new(connector, esp_wifi::current_millis);
        let service_uuid = Uuid::Uuid128([0x00, 0x00, 0xab, 0xf0, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb]);
        let mut ble = Ble::new(&hci);
        ble.init().unwrap();
        ble.cmd_set_le_advertising_parameters().unwrap();
        let advertising_data = create_advertising_data(&[
            AdStructure::Flags(LE_GENERAL_DISCOVERABLE | BR_EDR_NOT_SUPPORTED),
            AdStructure::ServiceUuids128(&[service_uuid]),
            AdStructure::CompleteLocalName("BLE_TO_ISOTP"),
        ]);
        ble.cmd_set_le_advertising_data(advertising_data).unwrap();
        ble.cmd_set_le_advertise_enable(true).unwrap();
Brandons-MacBook-Air:esp32-isotp-ble-bridge-rs brandonros 2022-10-26 11:14:41 $ cargo run --release
warning: Patch `embassy-embedded-hal v0.1.0 (https://github.com/embassy-rs/embassy#ce1cba76)` was not used in the crate graph.
Check that the patched package version and available features are compatible
with the dependency requirements. If the patch has a different version from
what is locked in the Cargo.lock file, run `cargo update` to use the new
version. This may also occur with an optional dependency that is not enabled.
   Compiling esp-wifi v0.1.0 (https://github.com/esp-rs/esp-wifi?branch=main#7adb1658)
   Compiling esp32_isotp_ble_bridge_rs v0.1.0 (/Users/brandonros/Desktop/esp32-isotp-ble-bridge-rs)
warning: unused variable: `wdt`
   --> src/main.rs:146:13
    |
146 |     let mut wdt = timer_group0.wdt;
    |             ^^^ help: if this is intentional, prefix it with an underscore: `_wdt`
    |
    = note: `#[warn(unused_variables)]` on by default

warning: variable does not need to be mutable
   --> src/main.rs:146:9
    |
146 |     let mut wdt = timer_group0.wdt;
    |         ----^^^
    |         |
    |         help: remove this `mut`
    |
    = note: `#[warn(unused_mut)]` on by default

warning: static `CHANNEL` is never used
  --> src/main.rs:34:8
   |
34 | static CHANNEL: StaticCell<Channel<NoopRawMutex, u32, 4>> = StaticCell::new();
   |        ^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: static `TIMER0` is never used
  --> src/main.rs:36:8
   |
36 | static TIMER0: Mutex<RefCell<Option<Timer<Timer0<TIMG0>>>>> = Mutex::new(RefCell::new(None));
   |        ^^^^^^

warning: static `TIMER1` is never used
  --> src/main.rs:37:8
   |
37 | static TIMER1: Mutex<RefCell<Option<Timer<Timer1<TIMG0>>>>> = Mutex::new(RefCell::new(None));
   |        ^^^^^^

warning: static `SERIAL0` is never used
  --> src/main.rs:38:8
   |
38 | static SERIAL0: Mutex<RefCell<Option<Serial<UART0>>>> = Mutex::new(RefCell::new(None));
   |        ^^^^^^^

warning: function `receiver_task` is never used
  --> src/main.rs:40:10
   |
40 | async fn receiver_task(receiver: Receiver<'static, NoopRawMutex, u32, 4>) {
   |          ^^^^^^^^^^^^^

warning: function `sender_task` is never used
  --> src/main.rs:59:10
   |
59 | async fn sender_task(sender: Sender<'static, NoopRawMutex, u32, 4>) {
   |          ^^^^^^^^^^^

warning: `esp32_isotp_ble_bridge_rs` (bin "esp32_isotp_ble_bridge_rs") generated 8 warnings
    Finished release [optimized] target(s) in 1.44s
     Running `espflash --monitor /dev/tty.usbserial-02728E37 --monitor-speed 115200 target/xtensa-esp32-none-elf/release/esp32_isotp_ble_bridge_rs`
Serial port: /dev/tty.usbserial-02728E37
Connecting...

Chip type:         ESP32 (revision 3)
Crystal frequency: 40MHz
Flash size:        16MB
Features:          WiFi, BT, Dual Core, 240MHz, Coding Scheme None
MAC address:       94:b9:7e:57:4b:18
App/part. size:    295984/16711680 bytes, 1.77%
[00:00:01] ########################################      16/16      segment 0x1000                                                                                                                                                                                                                                                                                                                                                                 
[00:00:00] ########################################       1/1       segment 0x8000                                                                                                                                                                                                                                                                                                                                                                 
[00:00:16] ########################################     156/156     segment 0x10000                                                                                                                                                                                                                                                                                                                                                                
Flashing has completed!
Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit

ets Jul 29 2019 12:21:46
rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0048,len:12
ho 0 tail 12 room 4
load:0x3fff0054,len:4800
load:0x40078000,len:17448
0x40078000 - r_rwip_reset
    at ??:??
load:0x4007c428,len:4840
0x4007c428 - r_rwip_reset
    at ??:??
entry 0x4007c6a0
0x4007c6a0 - r_rwip_reset
    at ??:??
WARN - coex_register_bt_cb 0x40081698
0x40081698 - coex_bt_callback
    at ??:??
WARN - coex_schm_register_btdm_callback 0x400df7dc
0x400df7dc - coex_schm_btdm_callback
    at ??:??
WARN - coex_wifi_channel_get

!! A panic occured in '/Users/brandonros/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/slice/index.rs', at line 73, column 5

PanicInfo {
    payload: Any { .. },
    message: Some(
        range end index 129 out of range for slice of length 128,
    ),
    location: Location {
        file: "/Users/brandonros/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/slice/index.rs",
        line: 73,
        col: 5,
    },
    can_unwind: true,
}

Backtrace:

0x400dc009
0x400dc009 - _ZN4core3ops8function6FnOnce9call_once17hf83c65e1c5683b9fE
    at ??:??
0x400dc039
0x400dc039 - _ZN4core10intrinsics17const_eval_select17h2e98052629cb9109E
    at ??:??
0x400dc095
0x400dc095 - _ZN4core5slice5index24slice_end_index_len_fail17h1a7d586e621fc8b7E
    at ??:??
0x400d7fca
0x400d7fca - _ZN5bleps12ad_structure23create_advertising_data17ha24b1c7a30ebd68cE
    at ??:??
0x400d14ae
0x400d14ae - _ZN97_$LT$core..future..from_generator..GenFuture$LT$T$GT$$u20$as$u20$core..future..future..Future$GT$4poll17h4c7b5d09e6303bf8E
    at ??:??
0x400d1c1c
0x400d1c1c - _ZN16embassy_executor3raw20TaskStorage$LT$F$GT$4poll17h57ceb5a97861a96dE.llvm.11765450987655502707
    at ??:??
0x400f7e27
0x400f7e27 - _ZN16embassy_executor3raw8Executor4poll17hb27cb61edfd0c27cE
    at ??:??
0x400d1c88
0x400d1c88 - _ZN16embassy_executor4arch8Executor3run17h50320a162ae4a970E
    at ??:??
0x400d1e45
0x400d1e45 - main
    at ??:??
0x400d947a
0x400d947a - Reset
    at ??:??
bjoernQ commented 1 year ago

Thanks for reporting this. I'll have a look

bjoernQ commented 1 year ago

What happens here is that the generated advertising data exceeds the limit of 32 bytes - e.g. if you remove the CompleteLocalName it should work

However I agree that the generated error is very misleading so the solution would be to check the length and create a suitable error if the maximum length is exceeded

MatMaul commented 1 year ago

FTR since I hit the same limit, you can now use cmd_set_le_scan_rsp_data to store the CompleteLocalName independently.