Skip to content

Commit

Permalink
Add Rust dynamic binary
Browse files Browse the repository at this point in the history
  • Loading branch information
Luni-4 committed Feb 27, 2024
1 parent 6cbfd9a commit 6d41953
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 80 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ jobs:
cd libfakedevice-c
./autorun.sh
- name: Build Rust project
- name: Build Rust project (dynamic)
run: |
cd libfakedevice-rust
./autorun.sh
- name: Build Rust project (static)
run: |
cd libfakedevice-rust
./autorun.sh --target=x86_64-unknown-linux-musl
Expand All @@ -47,13 +52,15 @@ jobs:
run: |
mv ./libfakedevice-cpp/build/fake-firmware-cpp ./fake-firmware-cpp-dynamic
mv ./libfakedevice-c/build/fake-firmware-c ./fake-firmware-c-dynamic
mv ./libfakedevice-rust/target/debug/fake-firmware-rust ./fake-firmware-rust-dynamic
mv ./libfakedevice-rust/target/x86_64-unknown-linux-musl/debug/fake-firmware-rust ./fake-firmware-rust-static
mv ./minimal-libfakedevice-c/build/minimal-fake-firmware-c ./minimal-fake-firmware-c-static
- name: Strip binaries
run: |
strip ./fake-firmware-cpp-dynamic -o ./fake-firmware-cpp-dynamic-stripped
strip ./fake-firmware-c-dynamic -o ./fake-firmware-c-dynamic-stripped
strip ./fake-firmware-rust-dynamic -o ./fake-firmware-rust-dynamic-stripped
strip ./fake-firmware-rust-static -o ./fake-firmware-rust-static-stripped
strip ./minimal-fake-firmware-c-static -o ./minimal-fake-firmware-c-static-stripped
Expand All @@ -64,10 +71,12 @@ jobs:
files: |
./fake-firmware-cpp-dynamic
./fake-firmware-c-dynamic
./fake-firmware-rust-dynamic
./fake-firmware-rust-static
./minimal-fake-firmware-c-static
./fake-firmware-cpp-dynamic-stripped
./fake-firmware-c-dynamic-stripped
./fake-firmware-rust-dynamic-stripped
./fake-firmware-rust-static-stripped
./minimal-fake-firmware-c-static-stripped
env:
Expand Down
6 changes: 4 additions & 2 deletions libfakedevice-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
#cpal = "0.15.2"
reqwest = "0.11.23"
rscam = { version = "0.5.5", features = ["no_wrapper"] }
tokio = { version = "1", features = ["full"] }

[target.'cfg(not(target_env = "musl"))'.dependencies]
cpal = "0.15.2"

[target.'cfg(target_env = "musl")'.dependencies]
openssl = {version = "0.10.64", features = ["vendored"] }
openssl = { version = "0.10.64", features = ["vendored"] }

[profile.release]
opt-level = 3
Expand Down
4 changes: 2 additions & 2 deletions libfakedevice-rust/src/bin/fake-firmware-rust.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Features APIs
//use libfakedevice::features::access_audio_driver;
use libfakedevice::features::access_network;
use libfakedevice::features::access_webcam;
use libfakedevice::features::write_on_drive;
Expand Down Expand Up @@ -29,7 +28,8 @@ pub async fn features_apis() {
access_webcam("/dev/video0").unwrap();

// Access audio driver
//access_audio_driver();
#[cfg(not(target_env = "musl"))]
libfakedevice::features::no_musl::access_audio_driver();
}

async fn device_apis() {
Expand Down
154 changes: 79 additions & 75 deletions libfakedevice-rust/src/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,85 +45,89 @@ pub fn access_webcam(webcam_path: &str) -> rscam::Result<()> {
Ok(())
}

/*/// Accesses to the audio driver and outputs a beep sound
pub fn access_audio_driver() {
// Retrieve host
let host = cpal::default_host();
// Retrieve device
let device = host.default_output_device().unwrap();
// Retrieve device configuration
let config = device.default_output_config().unwrap();
// Run beep according to sample format accepted by the retrieved device
match config.sample_format() {
cpal::SampleFormat::I8 => run::<i8>(&device, &config.into()),
cpal::SampleFormat::I16 => run::<i16>(&device, &config.into()),
cpal::SampleFormat::I32 => run::<i32>(&device, &config.into()),
cpal::SampleFormat::I64 => run::<i64>(&device, &config.into()),
cpal::SampleFormat::U8 => run::<u8>(&device, &config.into()),
cpal::SampleFormat::U16 => run::<u16>(&device, &config.into()),
cpal::SampleFormat::U32 => run::<u32>(&device, &config.into()),
cpal::SampleFormat::U64 => run::<u64>(&device, &config.into()),
cpal::SampleFormat::F32 => run::<f32>(&device, &config.into()),
cpal::SampleFormat::F64 => run::<f64>(&device, &config.into()),
sample_format => panic!("Unsupported sample format '{sample_format}'"),
#[cfg(not(target_env = "musl"))]
pub mod no_musl {

/// Accesses to the audio driver and outputs a beep sound
pub fn access_audio_driver() {
// Retrieve host
let host = cpal::default_host();

// Retrieve device
let device = host.default_output_device().unwrap();

// Retrieve device configuration
let config = device.default_output_config().unwrap();

// Run beep according to sample format accepted by the retrieved device
match config.sample_format() {
cpal::SampleFormat::I8 => run::<i8>(&device, &config.into()),
cpal::SampleFormat::I16 => run::<i16>(&device, &config.into()),
cpal::SampleFormat::I32 => run::<i32>(&device, &config.into()),
cpal::SampleFormat::I64 => run::<i64>(&device, &config.into()),
cpal::SampleFormat::U8 => run::<u8>(&device, &config.into()),
cpal::SampleFormat::U16 => run::<u16>(&device, &config.into()),
cpal::SampleFormat::U32 => run::<u32>(&device, &config.into()),
cpal::SampleFormat::U64 => run::<u64>(&device, &config.into()),
cpal::SampleFormat::F32 => run::<f32>(&device, &config.into()),
cpal::SampleFormat::F64 => run::<f64>(&device, &config.into()),
sample_format => panic!("Unsupported sample format '{sample_format}'"),
}
}
}

use cpal::{
traits::{DeviceTrait, HostTrait, StreamTrait},
FromSample, Sample, SizedSample,
};
fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig)
where
T: SizedSample + FromSample<f32>,
{
// Retrieve sample rate
let sample_rate = config.sample_rate.0 as f32;
// Retrieve device channels
let channels = config.channels as usize;
// Produce a sinusoid of maximum amplitude.
let mut sample_clock = 0f32;
let mut next_value = move || {
sample_clock = (sample_clock + 1.0) % sample_rate;
(sample_clock * 440.0 * 2.0 * std::f32::consts::PI / sample_rate).sin()
use cpal::{
traits::{DeviceTrait, HostTrait, StreamTrait},
FromSample, Sample, SizedSample,
};

let err_fn = |err| eprintln!("an error occurred on stream: {}", err);
// Send the sinusoid of maximum amplitude as a stream to the device
let stream = device
.build_output_stream(
config,
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
write_data(data, channels, &mut next_value)
},
err_fn,
None,
)
.unwrap();
// Play the data content present in the buffer
stream.play().unwrap();
// Stop the thread for one second in order to check out the latencies
std::thread::sleep(std::time::Duration::from_millis(1000));
}
fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig)
where
T: SizedSample + FromSample<f32>,
{
// Retrieve sample rate
let sample_rate = config.sample_rate.0 as f32;
// Retrieve device channels
let channels = config.channels as usize;

// Produce a sinusoid of maximum amplitude.
let mut sample_clock = 0f32;
let mut next_value = move || {
sample_clock = (sample_clock + 1.0) % sample_rate;
(sample_clock * 440.0 * 2.0 * std::f32::consts::PI / sample_rate).sin()
};

let err_fn = |err| eprintln!("an error occurred on stream: {}", err);

// Send the sinusoid of maximum amplitude as a stream to the device
let stream = device
.build_output_stream(
config,
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
write_data(data, channels, &mut next_value)
},
err_fn,
None,
)
.unwrap();

// Play the data content present in the buffer
stream.play().unwrap();

// Stop the thread for one second in order to check out the latencies
std::thread::sleep(std::time::Duration::from_millis(1000));
}

fn write_data<T>(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32)
where
T: Sample + FromSample<f32>,
{
// Write each sample on the data frame retrieved dividing the output buffer
// according to the output channels
for frame in output.chunks_mut(channels) {
let value: T = T::from_sample(next_sample());
for sample in frame.iter_mut() {
*sample = value;
fn write_data<T>(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32)
where
T: Sample + FromSample<f32>,
{
// Write each sample on the data frame retrieved dividing the output buffer
// according to the output channels
for frame in output.chunks_mut(channels) {
let value: T = T::from_sample(next_sample());
for sample in frame.iter_mut() {
*sample = value;
}
}
}
}*/
}

0 comments on commit 6d41953

Please sign in to comment.