Skip to content

Commit

Permalink
Merge pull request #41 from torfmaster/bugfix/fix-allows
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
Woyten authored Apr 20, 2018
2 parents 1bf4810 + e057773 commit 4da1741
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 190 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "tock"]
path = tock
url = https://github.com/helena-project/tock
[submodule "xargo"]
path = xargo
url = https://github.com/japaric/xargo
21 changes: 9 additions & 12 deletions examples/ble_scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ extern crate serde;
extern crate serde_derive;
extern crate tock;

// Macro usages are not detected
#[allow(unused_imports)]
use alloc::*;
use alloc::Vec;
use tock::ble_parser;
use tock::led;
use tock::simple_ble::BleCallback;
Expand All @@ -26,22 +24,21 @@ struct LedCommand {
// Prevents the compiler from dropping the subscription too early.
#[allow(unreachable_code)]
fn main() {
let mut shared_memory = BleDriver::share_memory().unwrap();
let mut shared_buffer = BleDriver::create_scan_buffer();
let mut my_buffer = BleDriver::create_scan_buffer();
let shared_memory = BleDriver::share_memory(&mut shared_buffer).unwrap();

let mut callback = BleCallback::new(|_: usize, _: usize| {
match ble_parser::find(
shared_memory.to_bytes(),
tock::simple_ble::gap_data::SERVICE_DATA as u8,
) {
shared_memory.read_bytes(&mut my_buffer);
match ble_parser::find(&my_buffer, tock::simple_ble::gap_data::SERVICE_DATA as u8) {
Some(payload) => {
let payload: Vec<u8> = payload.into_iter().map(|x| *x).collect::<Vec<u8>>();
let msg: LedCommand = corepack::from_bytes(payload.as_slice()).unwrap();
let msg_led = led::get(msg.nr as isize);
match msg_led {
Some(msg_led) => if msg.st {
msg_led.on();
} else {
msg_led.off();
Some(msg_led) => match msg.st {
true => msg_led.on(),
false => msg_led.off(),
},
_ => (),
}
Expand Down
9 changes: 6 additions & 3 deletions examples/simple_ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ fn main() {
let led = led::get(0).unwrap();

let name = String::from("Tock!");
let uuid: [u16; 1] = [0x0018];
let mut uuid: [u8; 2] = [0x18, 0x00];

let payload = corepack::to_bytes(LedCommand { nr: 2, st: true }).unwrap();
let mut payload = corepack::to_bytes(LedCommand { nr: 2, st: true }).unwrap();

let handle = BleAdvertisingDriver::initialize(100, name, uuid.to_vec(), true, payload).unwrap();
let mut buffer = BleAdvertisingDriver::create_advertising_buffer();
let handle =
BleAdvertisingDriver::initialize(100, name, &mut uuid, true, &mut payload, &mut buffer)
.unwrap();

loop {
led.on();
Expand Down
29 changes: 5 additions & 24 deletions src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use alloc::String;
use core::cell::Cell;
use core::fmt;
use core::result::Result;
use shared_memory::ShareableMemory;
use syscalls;

const DRIVER_NUMBER: usize = 1;
Expand All @@ -19,24 +18,6 @@ mod allow_nr {
pub const SHARE_BUFFER: usize = 1;
}

struct ShareableString {
string: String,
}

impl ShareableMemory for ShareableString {
fn driver_number(&self) -> usize {
DRIVER_NUMBER
}

fn allow_number(&self) -> usize {
allow_nr::SHARE_BUFFER
}

fn to_bytes(&mut self) -> &mut [u8] {
unsafe { self.string.as_bytes_mut() }
}
}

pub struct Console;

impl Console {
Expand All @@ -45,13 +26,13 @@ impl Console {
}

// TODO: Use &str after relocation is fixed
pub fn write(&mut self, text: String) {
pub fn write(&mut self, mut text: String) {
let num_bytes = text.as_bytes().len();

let text = ShareableString { string: text };

let (result_code, _shared_string) = syscalls::allow_new(text);
if result_code < 0 {
let result = syscalls::allow(DRIVER_NUMBER, allow_nr::SHARE_BUFFER, unsafe {
text.as_bytes_mut()
});
if result.is_err() {
return;
}

Expand Down
8 changes: 5 additions & 3 deletions src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//! Tempoary formatting functions until format! is fixed
use syscalls::{allow, command};
use syscalls;

pub fn output_number(value: u32) {
let mut out: [u8; 11] = [32; 11];
write_u32_into_array(&mut out, value as u32, 0x10_00_00_00, 0x10);

unsafe {
allow(1, 1, &out);
command(1, 1, 10, 0);
let handle = syscalls::allow(1, 1, &mut out);
syscalls::command(1, 1, 10, 0);
handle.unwrap();
}
}
pub fn write_u32_into_array(result: &mut [u8; 11], value: u32, start: u32, base: u32) {
Expand Down
13 changes: 7 additions & 6 deletions src/ipc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ impl Client {

match shared_val {
Ok(v) => {
share(self.pid, v, 1 << len)?;
Ok(RawVec::from_raw_parts(v, 1 << len).into_box())
let mut b = RawVec::from_raw_parts(v, 1 << len).into_box();
share(self.pid, b.as_mut_ptr(), 1 << len)?;
Ok(b)
}
_ => Err(()),
}
Expand Down Expand Up @@ -74,8 +75,9 @@ impl Client {
}
}

unsafe fn discover(pkg_name: String) -> Result<usize, ()> {
let res = syscalls::allow(DRIVER_NUM, 0, pkg_name.as_bytes());
unsafe fn discover(mut pkg_name: String) -> Result<usize, ()> {
let len = pkg_name.as_bytes().len();
let res = syscalls::allow_ptr(DRIVER_NUM, 0, pkg_name.as_bytes_mut().as_mut_ptr(), len);
if res < 0 {
Err(())
} else {
Expand All @@ -84,8 +86,7 @@ unsafe fn discover(pkg_name: String) -> Result<usize, ()> {
}

unsafe fn share(pid: usize, base: *mut u8, len: usize) -> Result<(), ()> {
use core::slice::from_raw_parts;
let res = syscalls::allow(DRIVER_NUM, pid, from_raw_parts(base, len));
let res = syscalls::allow_ptr(DRIVER_NUM, pid, base, len);
if res < 0 {
Err(())
} else {
Expand Down
16 changes: 12 additions & 4 deletions src/ipc_cs/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ impl ServerHandle {
shared_buffer.clone_from_slice(message);

unsafe {
if syscalls::allow(DRIVER_NUMBER, self.pid as usize, &*shared_buffer) < 0 {
if syscalls::allow_ptr(
DRIVER_NUMBER,
self.pid as usize,
shared_buffer.as_mut().as_mut_ptr(),
32,
) < 0
{
panic!()
};
}
Expand All @@ -56,12 +62,14 @@ impl ServerHandle {
unsafe { syscalls::command(DRIVER_NUMBER, self.pid as usize, 0, 0) };
}

pub fn discover_service(name: String) -> Option<ServerHandle> {
pub fn discover_service(mut name: String) -> Option<ServerHandle> {
let len = name.len();
let pid = unsafe {
syscalls::allow(
syscalls::allow_ptr(
DRIVER_NUMBER,
ipc_commands::DISCOVER_SERVICE,
&name.as_bytes(),
name.as_bytes_mut().as_mut_ptr(),
len,
)
};
if pid >= 0 {
Expand Down
37 changes: 26 additions & 11 deletions src/shared_memory.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
pub trait ShareableMemory {
fn driver_number(&self) -> usize;
use core::ptr;
use syscalls;

fn allow_number(&self) -> usize;

fn to_bytes(&mut self) -> &mut [u8];
pub struct SharedMemory<'a> {
pub driver_number: usize,
pub allow_number: usize,
pub buffer_to_share: &'a mut [u8],
}

pub struct SharedMemory<SM: ShareableMemory> {
#[allow(dead_code)] // Used in drop
pub(crate) shareable_memory: SM,
impl<'a> SharedMemory<'a> {
pub fn read_bytes(&self, destination: &mut [u8]) {
safe_copy(self.buffer_to_share, destination);
}

pub fn write_bytes(&mut self, source: &[u8]) {
safe_copy(source, self.buffer_to_share);
}
}

impl<SM: ShareableMemory> SharedMemory<SM> {
pub fn to_bytes(&mut self) -> &mut [u8] {
self.shareable_memory.to_bytes()
impl<'a> Drop for SharedMemory<'a> {
fn drop(&mut self) {
unsafe {
syscalls::allow_ptr(self.driver_number, self.allow_number, ptr::null_mut(), 0);
}
}
}

fn safe_copy(origin: &[u8], destination: &mut [u8]) {
let amount = origin.len().min(destination.len());
let origin = &origin[0..amount];
let destination = &mut destination[0..amount];
destination.clone_from_slice(origin);
}
Loading

0 comments on commit 4da1741

Please sign in to comment.