Skip to content

Commit

Permalink
Merge BLE2904 into Cpfd
Browse files Browse the repository at this point in the history
  • Loading branch information
taks committed Dec 13, 2024
1 parent 6124a9e commit e28195c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 163 deletions.
107 changes: 0 additions & 107 deletions src/server/ble_2904.rs

This file was deleted.

27 changes: 14 additions & 13 deletions src/server/ble_characteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ use alloc::{boxed::Box, sync::Arc, vec::Vec};
use bitflags::bitflags;
use core::{cell::UnsafeCell, ffi::c_void};
use esp_idf_svc::sys;

#[cfg(cpfd)]
use crate::cpfd::Cpfd;
#[cfg(not(cpfd))]
use zerocopy::IntoBytes;

use crate::{
ble,
cpfd::Cpfd,
utilities::{
ble_hs_mbuf_from_flat, ble_npl_hw_enter_critical, ble_npl_hw_exit_critical, mutex::Mutex,
os_mbuf_append, voidp_to_ref, BleUuid,
},
AttValue, BLEConnDesc, BLEDescriptor, BLEDevice, BLEError, DescriptorProperties, OnWriteArgs,
BLE2904,
};

cfg_if::cfg_if! {
Expand Down Expand Up @@ -209,15 +208,6 @@ impl BLECharacteristic {
descriptor
}

pub fn create_2904_descriptor(&mut self) -> BLE2904 {
let descriptor = Arc::new(Mutex::new(BLEDescriptor::new(
BleUuid::Uuid16(0x2904),
DescriptorProperties::READ,
)));
self.descriptors.push(descriptor.clone());
BLE2904::new(descriptor)
}

pub(crate) fn construct_svc_def_descriptors(&mut self) -> *mut sys::ble_gatt_dsc_def {
if self.descriptors.is_empty() {
return core::ptr::null_mut();
Expand Down Expand Up @@ -300,6 +290,17 @@ impl BLECharacteristic {
self.cpfd[0].description = cpfd.description;
}

#[cfg(not(cpfd))]
/// Set the Characteristic Presentation Format.
pub fn cpfd(&mut self, cpfd: Cpfd) {
let descriptor = Arc::new(Mutex::new(BLEDescriptor::new(
BleUuid::Uuid16(0x2904),
DescriptorProperties::READ,
)));
descriptor.lock().set_value(cpfd.as_bytes());
self.descriptors.push(descriptor);
}

pub(super) extern "C" fn handle_gap_event(
conn_handle: u16,
_attr_handle: u16,
Expand Down
18 changes: 9 additions & 9 deletions src/server/ble_hid_device.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use alloc::sync::Arc;

use crate::{
cpfd::*,
utilities::{mutex::Mutex, BleUuid},
BLE2904Format, BLECharacteristic, BLEServer, BLEService, DescriptorProperties, NimbleProperties,
BLE2904,
BLECharacteristic, BLEServer, BLEService, DescriptorProperties, NimbleProperties,
};

const BLE_SVC_DIS_CHR_UUID16_MANUFACTURER_NAME: BleUuid = BleUuid::from_uuid16(0x2A29);
Expand All @@ -24,7 +24,6 @@ pub struct BLEHIDDevice {

battery_service: Arc<Mutex<BLEService>>,
battery_level_characteristic: Arc<Mutex<BLECharacteristic>>,
battery_level_descriptor: BLE2904,
}

impl BLEHIDDevice {
Expand Down Expand Up @@ -56,11 +55,13 @@ impl BLEHIDDevice {
BLE_SVC_BAS_CHR_UUID16_BATTERY_LEVEL,
NimbleProperties::READ | NimbleProperties::NOTIFY,
);
let mut battery_level_descriptor = battery_level_characteristic.lock().create_2904_descriptor();
battery_level_descriptor
.format(BLE2904Format::UINT8)
.namespace(1)
.unit(0x27ad);
battery_level_characteristic.lock().cpfd(Cpfd {
format: ChrFormat::Uint8,
exponent: 0,
unit: ChrUnit::Percentage,
name_space: 1,
description: 0,
});

Self {
device_info_service,
Expand All @@ -73,7 +74,6 @@ impl BLEHIDDevice {
protocol_mode_characteristic,
battery_service,
battery_level_characteristic,
battery_level_descriptor,
}
}

Expand Down
67 changes: 37 additions & 30 deletions src/server/cpfd.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
use esp_idf_svc::sys as esp_idf_sys;
use num_enum::IntoPrimitive;
use zerocopy_derive::{Immutable, IntoBytes, KnownLayout, TryFromBytes};

#[derive(Copy, Clone, PartialEq, Eq, Debug, IntoPrimitive)]
#[derive(
Copy, Clone, PartialEq, Eq, Debug, IntoPrimitive, TryFromBytes, KnownLayout, IntoBytes, Immutable,
)]
#[repr(u8)]
pub enum ChrFormat {
Boolean = esp_idf_sys::BLE_GATT_CHR_FORMAT_BOOLEAN as _,
Uint2 = esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT2 as _,
Uint4 = esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT4 as _,
Uint8 = esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT8 as _,
Uint12 = esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT12 as _,
Uint16 = esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT16 as _,
Uint24 = esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT24 as _,
Uint32 = esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT32 as _,
Uint48 = esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT48 as _,
Uint64 = esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT64 as _,
Uint128 = esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT128 as _,
Sint8 = esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT8 as _,
Sint12 = esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT12 as _,
Sint16 = esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT16 as _,
Sint24 = esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT24 as _,
Sint32 = esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT32 as _,
Sint48 = esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT48 as _,
Sint64 = esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT64 as _,
Sint128 = esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT128 as _,
Float32 = esp_idf_sys::BLE_GATT_CHR_FORMAT_FLOAT32 as _,
Float64 = esp_idf_sys::BLE_GATT_CHR_FORMAT_FLOAT64 as _,
Medfloat16 = esp_idf_sys::BLE_GATT_CHR_FORMAT_MEDFLOAT16 as _,
Medfloat32 = esp_idf_sys::BLE_GATT_CHR_FORMAT_MEDFLOAT32 as _,
Uint162 = esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT16_2 as _,
Utf8s = esp_idf_sys::BLE_GATT_CHR_FORMAT_UTF8S as _,
Utf16s = esp_idf_sys::BLE_GATT_CHR_FORMAT_UTF16S as _,
Struct = esp_idf_sys::BLE_GATT_CHR_FORMAT_STRUCT as _,
Medasn1 = esp_idf_sys::BLE_GATT_CHR_FORMAT_MEDASN1 as _,
Boolean = 1, // esp_idf_sys::BLE_GATT_CHR_FORMAT_BOOLEAN as _
Uint2 = 2, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT2 as _,
Uint4 = 3, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT4 as _,
Uint8 = 4, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT8 as _,
Uint12 = 5, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT12 as _,
Uint16 = 6, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT16 as _,
Uint24 = 7, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT24 as _,
Uint32 = 8, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT32 as _,
Uint48 = 9, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT48 as _,
Uint64 = 10, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT64 as _,
Uint128 = 11, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT128 as _,
Sint8 = 12, // esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT8 as _,
Sint12 = 13, // esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT12 as _,
Sint16 = 14, // esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT16 as _,
Sint24 = 15, // esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT24 as _,
Sint32 = 16, // esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT32 as _,
Sint48 = 17, // esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT48 as _,
Sint64 = 18, // esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT64 as _,
Sint128 = 19, // esp_idf_sys::BLE_GATT_CHR_FORMAT_SINT128 as _,
Float32 = 20, // esp_idf_sys::BLE_GATT_CHR_FORMAT_FLOAT32 as _,
Float64 = 21, //esp_idf_sys::BLE_GATT_CHR_FORMAT_FLOAT64 as _,
Medfloat16 = 22, // esp_idf_sys::BLE_GATT_CHR_FORMAT_MEDFLOAT16 as _,
Medfloat32 = 23, // esp_idf_sys::BLE_GATT_CHR_FORMAT_MEDFLOAT32 as _,
Uint162 = 24, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UINT16_2 as _,
Utf8s = 25, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UTF8S as _,
Utf16s = 26, // esp_idf_sys::BLE_GATT_CHR_FORMAT_UTF16S as _,
Struct = 27, // esp_idf_sys::BLE_GATT_CHR_FORMAT_STRUCT as _,
Medasn1 = 28, // esp_idf_sys::BLE_GATT_CHR_FORMAT_MEDASN1 as _,
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, IntoPrimitive)]
#[derive(
Copy, Clone, PartialEq, Eq, Debug, IntoPrimitive, TryFromBytes, KnownLayout, IntoBytes, Immutable,
)]
#[repr(u16)]
pub enum ChrUnit {
/// Unitless
Expand Down Expand Up @@ -295,6 +300,8 @@ pub enum ChrUnit {
VoltAmpere = esp_idf_sys::BLE_GATT_CHR_UNIT_VOLT_AMPERE as _,
}

#[derive(TryFromBytes, KnownLayout, IntoBytes, Immutable)]
#[repr(packed)]
pub struct Cpfd {
/// Format of the value of this characteristic.
pub format: ChrFormat,
Expand Down
4 changes: 0 additions & 4 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
mod att_value;
pub use self::att_value::AttValue;

mod ble_2904;
pub use self::ble_2904::*;

mod ble_advertisement_data;
pub use self::ble_advertisement_data::BLEAdvertisementData;

Expand Down Expand Up @@ -34,7 +31,6 @@ pub use self::ble_server::BLEServer;
mod ble_service;
pub use self::ble_service::BLEService;

#[cfg(cpfd)]
pub mod cpfd;

pub mod hid;
Expand Down

0 comments on commit e28195c

Please sign in to comment.