Skip to content

Commit

Permalink
Bluedroid BLE support
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Apr 23, 2024
1 parent 91db2b2 commit e8cbb7d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 100 deletions.
1 change: 0 additions & 1 deletion src/bt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ extern crate alloc;
pub mod a2dp;
#[cfg(all(esp32, esp_idf_bt_classic_enabled, esp_idf_bt_a2dp_enable))]
pub mod avrc;
// TODO: Future
pub mod ble;
#[cfg(all(esp32, esp_idf_bt_classic_enabled))]
pub mod gap;
Expand Down
51 changes: 10 additions & 41 deletions src/bt/ble/gatt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use crate::sys::*;

pub mod server;

pub type GattInterface = u8;
pub type Handle = u16;

#[derive(Debug, Copy, Clone, Eq, PartialEq, TryFromPrimitive)]
#[repr(u32)]
pub enum GattStatus {
Expand Down Expand Up @@ -210,75 +213,41 @@ pub enum ServiceUuid {
PublicBroadcastAnnouncement,
}

pub struct AttributeValue<const S: usize> {
len: usize,
value: [u8; S],
}

impl<const S: usize> From<&AttributeValue<S>> for esp_attr_value_t {
fn from(val: &AttributeValue<S>) -> Self {
Self {
attr_max_len: S as _,
attr_len: val.len as _,
attr_value: val.value.as_ptr() as _,
}
}
}

impl<const S: usize> Default for AttributeValue<S> {
fn default() -> Self {
Self {
len: S,
value: [0; S],
}
}
}

impl<const S: usize> AttributeValue<S> {
pub fn new_with_value(value: &[u8]) -> Self {
let actual_len = core::cmp::min(value.len(), S);
let mut val = Self {
len: S,
value: [0; S],
};
val.value[0..actual_len].copy_from_slice(&value[0..actual_len]);
val
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, TryFromPrimitive)]
#[repr(u8)]
pub enum AutoResponse {
ByApp = ESP_GATT_RSP_BY_APP as _,
ByGatt = ESP_GATT_AUTO_RSP as _,
}

pub struct GattCharacteristic<const S: usize> {
#[derive(Debug)]
pub struct GattCharacteristic {
pub(crate) uuid: BtUuid,
pub(crate) permissions: esp_gatt_perm_t,
pub(crate) properties: esp_gatt_char_prop_t,
pub(crate) value: AttributeValue<S>,
pub(crate) max_len: usize,
pub(crate) auto_rsp: AutoResponse,
}

impl<const S: usize> GattCharacteristic<S> {
impl GattCharacteristic {
pub fn new(
uuid: BtUuid,
permissions: esp_gatt_perm_t,
properties: esp_gatt_char_prop_t,
value: AttributeValue<S>,
max_len: usize,
auto_rsp: AutoResponse,
) -> Self {
Self {
uuid,
permissions,
properties,
value,
max_len,
auto_rsp,
}
}
}

#[derive(Debug)]
pub struct GattDescriptor {
pub(crate) uuid: BtUuid,
pub(crate) permissions: esp_gatt_perm_t,
Expand Down
Loading

0 comments on commit e8cbb7d

Please sign in to comment.