Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge BLE2904 into Cpfd #156

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 17 additions & 3 deletions src/server/cpfd.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
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)]
cfg_if::cfg_if! {
if #[cfg(cpfd)] {
use esp_idf_svc::sys as esp_idf_sys;
} else {
use super::cpfd_constants as esp_idf_sys;
}
}

#[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 _,
Expand Down Expand Up @@ -34,7 +44,9 @@ pub enum ChrFormat {
Medasn1 = 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 +307,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
Loading
Loading