-
Notifications
You must be signed in to change notification settings - Fork 109
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
Put shared buffer into UnsafeCell #185
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,12 @@ pub struct AdcBuffer { | |
buffer: [u8; BUFFER_SIZE], | ||
} | ||
|
||
impl AsMut<[u8]> for AdcBuffer { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
&mut self.buffer | ||
} | ||
} | ||
|
||
impl Default for AdcBuffer { | ||
fn default() -> Self { | ||
AdcBuffer { | ||
|
@@ -67,11 +73,11 @@ impl<CB: FnMut(usize, usize)> Consumer<CB> for AdcEventConsumer { | |
} | ||
|
||
impl<'a> Adc<'a> { | ||
pub fn init_buffer(&self, buffer: &'a mut AdcBuffer) -> TockResult<SharedMemory> { | ||
pub fn init_buffer(&self, buffer: &'a mut AdcBuffer) -> TockResult<SharedMemory<'a>> { | ||
syscalls::allow(DRIVER_NUMBER, allow_nr::BUFFER, &mut buffer.buffer).map_err(Into::into) | ||
} | ||
|
||
pub fn init_alt_buffer(&self, alt_buffer: &'a mut AdcBuffer) -> TockResult<SharedMemory> { | ||
pub fn init_alt_buffer(&self, alt_buffer: &'a mut AdcBuffer) -> TockResult<SharedMemory<'a>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you revert this? |
||
syscalls::allow(DRIVER_NUMBER, allow_nr::BUFFER_ALT, &mut alt_buffer.buffer) | ||
.map_err(Into::into) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,10 +58,34 @@ impl Default for HmacKeyBuffer { | |
} | ||
} | ||
|
||
impl AsRef<[u8]> for HmacKeyBuffer { | ||
fn as_ref(&self) -> &[u8] { | ||
&self.buffer | ||
} | ||
} | ||
|
||
impl AsMut<[u8]> for HmacKeyBuffer { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
&mut self.buffer | ||
} | ||
} | ||
|
||
pub struct HmacDataBuffer { | ||
pub buffer: [u8; DATA_BUFFER_SIZE], | ||
} | ||
|
||
impl AsRef<[u8]> for HmacDataBuffer { | ||
fn as_ref(&self) -> &[u8] { | ||
&self.buffer | ||
} | ||
} | ||
|
||
impl AsMut<[u8]> for HmacDataBuffer { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
&mut self.buffer | ||
} | ||
} | ||
|
||
impl Default for HmacDataBuffer { | ||
fn default() -> Self { | ||
HmacDataBuffer { | ||
|
@@ -74,6 +98,18 @@ pub struct HmacDestBuffer { | |
buffer: [u8; DEST_BUFFER_SIZE], | ||
} | ||
|
||
impl AsRef<[u8]> for HmacDestBuffer { | ||
fn as_ref(&self) -> &[u8] { | ||
&self.buffer | ||
} | ||
} | ||
|
||
impl AsMut<[u8]> for HmacDestBuffer { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
&mut self.buffer | ||
} | ||
} | ||
|
||
impl Default for HmacDestBuffer { | ||
fn default() -> Self { | ||
HmacDestBuffer { | ||
|
@@ -87,15 +123,15 @@ pub struct HmacDriver<'a> { | |
} | ||
|
||
impl<'a> HmacDriver<'a> { | ||
pub fn init_key_buffer(&self, buffer: &'a mut HmacKeyBuffer) -> TockResult<SharedMemory> { | ||
pub fn init_key_buffer(&self, buffer: &'a mut HmacKeyBuffer) -> TockResult<SharedMemory<'a>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this. |
||
syscalls::allow(DRIVER_NUMBER, allow_nr::KEY, &mut buffer.buffer).map_err(Into::into) | ||
} | ||
|
||
pub fn init_data_buffer(&self, buffer: &'a mut HmacDataBuffer) -> TockResult<SharedMemory> { | ||
pub fn init_data_buffer(&self, buffer: &'a mut HmacDataBuffer) -> TockResult<SharedMemory<'a>> { | ||
syscalls::allow(DRIVER_NUMBER, allow_nr::DATA, &mut buffer.buffer).map_err(Into::into) | ||
} | ||
|
||
pub fn init_dest_buffer(&self, buffer: &'a mut HmacDestBuffer) -> TockResult<SharedMemory> { | ||
pub fn init_dest_buffer(&self, buffer: &'a mut HmacDestBuffer) -> TockResult<SharedMemory<'a>> { | ||
syscalls::allow(DRIVER_NUMBER, allow_nr::DEST, &mut buffer.buffer).map_err(Into::into) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,20 +53,28 @@ impl BleAdvertisingDriverFactory { | |
#[non_exhaustive] | ||
pub struct BleAdvertisingDriver; | ||
|
||
pub struct BleAdvertisingBuffer([u8; BUFFER_SIZE_ADVERTISE]); | ||
|
||
impl AsMut<[u8]> for BleAdvertisingBuffer { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl BleAdvertisingDriver { | ||
pub fn create_advertising_buffer() -> [u8; BUFFER_SIZE_ADVERTISE] { | ||
[0; BUFFER_SIZE_ADVERTISE] | ||
pub fn create_advertising_buffer() -> BleAdvertisingBuffer { | ||
BleAdvertisingBuffer([0; BUFFER_SIZE_ADVERTISE]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert this? |
||
} | ||
pub fn initialize<'a>( | ||
pub fn initialize<'a, 'b>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a second lifetime here? |
||
&'a mut self, | ||
interval: usize, | ||
service_payload: &BlePayload, | ||
advertising_buffer: &'a mut [u8; BUFFER_SIZE_ADVERTISE], | ||
) -> TockResult<SharedMemory<'a>> { | ||
advertising_buffer: &'b mut BleAdvertisingBuffer, | ||
) -> TockResult<SharedMemory<'b>> { | ||
let mut shared_memory = syscalls::allow( | ||
DRIVER_NUMBER, | ||
allow_nr::ALLOW_ADVERTISMENT_BUFFER, | ||
advertising_buffer, | ||
&mut advertising_buffer.0, | ||
)?; | ||
shared_memory.write_bytes(service_payload); | ||
Self::start_advertising(gap_flags::BLE_DISCOVERABLE, interval)?; | ||
|
@@ -89,9 +97,16 @@ struct BleCallback<'a> { | |
shared_buffer: SharedMemory<'a>, | ||
} | ||
|
||
pub(crate) type ScanBuffer = [u8; BUFFER_SIZE_SCAN]; | ||
#[derive(Clone, Copy)] | ||
pub struct ScanBuffer([u8; BUFFER_SIZE_SCAN]); | ||
|
||
impl AsRef<[u8]> for ScanBuffer { | ||
fn as_ref(&self) -> &[u8] { | ||
&self.0 | ||
} | ||
} | ||
|
||
const EMPTY_SCAN_BUFFER: ScanBuffer = [0; BUFFER_SIZE_SCAN]; | ||
const EMPTY_SCAN_BUFFER: ScanBuffer = ScanBuffer([0; BUFFER_SIZE_SCAN]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert? |
||
|
||
#[non_exhaustive] | ||
pub struct BleScanningDriverFactory; | ||
|
@@ -130,13 +145,19 @@ pub struct BleScanningDriver { | |
read_value: Cell<Option<ScanBuffer>>, | ||
} | ||
|
||
impl AsMut<[u8]> for ScanBuffer { | ||
fn as_mut(&mut self) -> &mut [u8] { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl BleScanningDriver { | ||
/// Prepare Ble Scanning Driver to share memory with the ble capsule | ||
pub fn share_memory(&mut self) -> TockResult<BleScanningDriverShared> { | ||
let shared_buffer: SharedMemory = syscalls::allow( | ||
let shared_buffer = syscalls::allow( | ||
DRIVER_NUMBER, | ||
allow_nr::ALLOW_SCAN_BUFFER, | ||
&mut self.shared_buffer, | ||
&mut self.shared_buffer.0, | ||
) | ||
.map_err(Into::<TockError>::into)?; | ||
Ok(BleScanningDriverShared { | ||
|
@@ -195,7 +216,7 @@ impl<'a> Consumer<Self> for BleCallback<'a> { | |
fn consume(callback: &mut Self, _: usize, _: usize, _: usize) { | ||
let mut temporary_buffer: ScanBuffer = EMPTY_SCAN_BUFFER; | ||
|
||
callback.shared_buffer.read_bytes(&mut temporary_buffer[..]); | ||
callback.shared_buffer.read_bytes(temporary_buffer.as_mut()); | ||
callback.read_value.set(Some(temporary_buffer)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You no longer need this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if all
AsRef
andAsMut
implementations were removed until we see the need for them.