From 4f475f70084d784740c5de573c178ba13200d10d Mon Sep 17 00:00:00 2001 From: taks <857tn859@gmail.com> Date: Mon, 15 Apr 2024 12:47:59 +0900 Subject: [PATCH] Use heapless --- Cargo.toml | 1 + src/server/ble_server.rs | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c7b458a..f3eae8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ embedded-svc = { version = "0.27", default-features = false } bitflags = { version = "2.4.1" } bstr = { version = "1.8.0", default-features = false } embassy-sync = { version = "0.5.0" } +heapless = "0.8.0" num_enum = { version = "0.7", default-features = false } once_cell = { version = "1.19.0", default-features = false } uuid = { version = "1", default-features = false, features = ["macro-diagnostics"] } diff --git a/src/server/ble_server.rs b/src/server/ble_server.rs index 39e4c78..0df65d9 100644 --- a/src/server/ble_server.rs +++ b/src/server/ble_server.rs @@ -7,6 +7,7 @@ use alloc::{boxed::Box, sync::Arc, vec::Vec}; use core::{cell::UnsafeCell, ffi::c_void}; const BLE_HS_CONN_HANDLE_NONE: u16 = esp_idf_sys::BLE_HS_CONN_HANDLE_NONE as _; +const MAX_CONNECTIONS: usize = esp_idf_sys::CONFIG_BT_NIMBLE_MAX_CONNECTIONS as _; #[allow(clippy::type_complexity)] pub struct BLEServer { @@ -14,8 +15,8 @@ pub struct BLEServer { advertise_on_disconnect: bool, services: Vec>>, notify_characteristic: Vec<&'static mut BLECharacteristic>, - connections: Vec, - indicate_wait: [u16; esp_idf_sys::CONFIG_BT_NIMBLE_MAX_CONNECTIONS as _], + connections: heapless::Vec, + indicate_wait: [u16; MAX_CONNECTIONS], on_connect: Option>, on_disconnect: Option) + Send + Sync>>, @@ -31,8 +32,8 @@ impl BLEServer { advertise_on_disconnect: true, services: Vec::new(), notify_characteristic: Vec::new(), - connections: Vec::new(), - indicate_wait: [BLE_HS_CONN_HANDLE_NONE; esp_idf_sys::CONFIG_BT_NIMBLE_MAX_CONNECTIONS as _], + connections: heapless::Vec::new(), + indicate_wait: [BLE_HS_CONN_HANDLE_NONE; MAX_CONNECTIONS], on_connect: None, on_disconnect: None, on_passkey_request: None, @@ -251,7 +252,7 @@ impl BLEServer { esp_idf_sys::BLE_GAP_EVENT_CONNECT => { let connect = unsafe { &event.__bindgen_anon_1.connect }; if connect.status == 0 { - server.connections.push(connect.conn_handle); + server.connections.push(connect.conn_handle).unwrap(); if let Ok(desc) = ble_gap_conn_find(connect.conn_handle) { let server = UnsafeCell::new(server);