Skip to content

Commit

Permalink
Changed set_value to deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
taks committed Dec 11, 2024
1 parent ba8c2df commit 622fce3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
7 changes: 6 additions & 1 deletion examples/ble_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use esp32_nimble::{
BLEHIDDevice, BLEServer,
};
use std::sync::Arc;
use zerocopy::IntoBytes;
use zerocopy_derive::{Immutable, IntoBytes};

const KEYBOARD_ID: u8 = 0x01;
Expand Down Expand Up @@ -298,7 +299,11 @@ impl Keyboard {
}

fn send_report(&self, keys: &KeyReport) {
self.input_keyboard.lock().set_from(keys).notify();
self
.input_keyboard
.lock()
.set_value(keys.as_bytes())
.notify();
esp_idf_svc::hal::delay::Ets::delay_ms(7);
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/server/att_value.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use alloc::vec::Vec;
use zerocopy::{Immutable, IntoBytes};

pub struct AttValue {
value: Vec<u8>,
Expand Down Expand Up @@ -54,9 +53,13 @@ impl AttValue {
self.value.extend_from_slice(value);
}

#[deprecated(note = "Please use `set_value` + zerocopy::IntoBytes")]
#[inline]
pub fn set_from<T: IntoBytes + Immutable>(&mut self, value: &T) {
self.set_value(value.as_bytes());
pub fn set_from<T: Sized>(&mut self, p: &T) {
let slice = unsafe {
::core::slice::from_raw_parts((p as *const T) as *const u8, ::core::mem::size_of::<T>())
};
self.set_value(slice);
}

#[inline]
Expand Down
18 changes: 11 additions & 7 deletions src/server/ble_2904.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use alloc::sync::Arc;
use zerocopy::IntoBytes;
use zerocopy_derive::{Immutable, IntoBytes};

use crate::{utilities::mutex::Mutex, BLEDescriptor};
Expand Down Expand Up @@ -51,13 +52,16 @@ pub struct BLE2904 {

impl BLE2904 {
pub(super) fn new(descriptor: Arc<Mutex<BLEDescriptor>>) -> Self {
descriptor.lock().set_from(&Data {
format: BLE2904Format::OPAQUE,
exponent: 0,
unit: 0,
namespace: 1,
description: 0,
});
descriptor.lock().set_value(
Data {
format: BLE2904Format::OPAQUE,
exponent: 0,
unit: 0,
namespace: 1,
description: 0,
}
.as_bytes(),
);

Self { descriptor }
}
Expand Down
5 changes: 3 additions & 2 deletions src/server/ble_characteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use alloc::{boxed::Box, sync::Arc, vec::Vec};
use bitflags::bitflags;
use core::{cell::UnsafeCell, ffi::c_void};
use esp_idf_svc::sys;
use zerocopy::{Immutable, IntoBytes};

#[cfg(cpfd)]
use crate::cpfd::Cpfd;
Expand Down Expand Up @@ -160,7 +159,9 @@ impl BLECharacteristic {
self
}

pub fn set_from<T: IntoBytes + Immutable>(&mut self, value: &T) -> &mut Self {
#[deprecated(note = "Please use `set_value` + zerocopy::IntoBytes")]
pub fn set_from<T: Sized>(&mut self, value: &T) -> &mut Self {
#[allow(deprecated)]
self.value.set_from(value);
self
}
Expand Down
5 changes: 3 additions & 2 deletions src/server/ble_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use bitflags::bitflags;
use core::{cell::UnsafeCell, ffi::c_void};
use esp_idf_svc::sys as esp_idf_sys;
use esp_idf_sys::{ble_uuid_any_t, ble_uuid_cmp};
use zerocopy::{Immutable, IntoBytes};

use crate::{
utilities::{
Expand Down Expand Up @@ -53,7 +52,9 @@ impl BLEDescriptor {
self
}

pub fn set_from<T: IntoBytes + Immutable>(&mut self, value: &T) -> &mut Self {
#[deprecated(note = "Please use `set_value` + zerocopy::IntoBytes")]
pub fn set_from<T: Sized>(&mut self, value: &T) -> &mut Self {
#[allow(deprecated)]
self.value.set_from(value);
self
}
Expand Down

0 comments on commit 622fce3

Please sign in to comment.