Skip to content

Commit

Permalink
Relay works on latest switch firmware
Browse files Browse the repository at this point in the history
Mac address needs to be changed to start with 94:58:CB: ? maybe ?
Poohl/joycontrol#4

Probably will move back away from async. Not sure why I was doing it, but this is some old code
  • Loading branch information
JuanPotato committed May 5, 2022
1 parent 2a81790 commit f5b542e
Show file tree
Hide file tree
Showing 8 changed files with 487 additions and 123 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ smol = "0.1"
futures = "0.3.4"
# For generic nums
num-traits = { version = "0.2", default-features = false }
# For raw hci
hciraw = "1.0"
139 changes: 139 additions & 0 deletions src/hci_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use libc::EIO;
use std::ffi::c_void;
use std::io::{Result, Error};
// use libbluetooth::hci::{
// OGF_LINK_POLICY,
// sniff_mode_cp, OCF_SNIFF_MODE, SNIFF_MODE_CP_SIZE,
// exit_sniff_mode_cp, OCF_EXIT_SNIFF_MODE, EXIT_SNIFF_MODE_CP_SIZE,
// evt_mode_change, EVT_MODE_CHANGE, EVT_MODE_CHANGE_SIZE,
// };

use libbluetooth::hci_lib::hci_request;

fn mut_void_ptr<T>(t: &mut T) -> *mut c_void {
t as *mut T as *mut c_void
}

fn void_ptr<T>(t: &mut T) -> *const c_void {
t as *const T as *const c_void
}

pub unsafe fn hci_send_req(dd: i32, req: &mut hci_request, timeout: i32) -> std::io::Result<()> {
let res = libbluetooth::hci_lib::hci_send_req(dd, req, timeout);

if res < 0 {
Err(std::io::Error::last_os_error())
} else {
Ok(())
}
}

pub fn hci_sniff_mode(
dd: i32, handle: u16, max_interval: u16, min_interval: u16,
sniff_attempt: u16, sniff_timeout: u16, timeout: i32
) -> Result<()> {
use libbluetooth::hci::{
OGF_LINK_POLICY,
sniff_mode_cp, OCF_SNIFF_MODE, SNIFF_MODE_CP_SIZE,
// evt_mode_change, EVT_MODE_CHANGE, EVT_MODE_CHANGE_SIZE,
evt_cmd_status, EVT_CMD_STATUS, EVT_CMD_STATUS_SIZE,
};


let mut cp = sniff_mode_cp {
handle: handle,
max_interval: max_interval,
min_interval: min_interval,
attempt: sniff_attempt,
timeout: sniff_timeout
};

// let mut rp = evt_mode_change {
// status: 0,
// handle: 0,
// mode: 0,
// interval: 0
// };

let mut rp = evt_cmd_status {
status: 0,
ncmd: 0,
opcode: 0
};

let mut rq = hci_request {
ogf: OGF_LINK_POLICY as u16,
ocf: OCF_SNIFF_MODE as u16,
// event: EVT_MODE_CHANGE,
event: EVT_CMD_STATUS,
cparam: mut_void_ptr(&mut cp),
clen: SNIFF_MODE_CP_SIZE as i32,
rparam: mut_void_ptr(&mut rp),
// rlen: EVT_MODE_CHANGE_SIZE as i32
rlen: EVT_CMD_STATUS_SIZE as i32
};

unsafe {
if libbluetooth::hci_lib::hci_send_req(dd, &mut rq, timeout) < 0 {
return Err(Error::last_os_error());
}
}

if rp.status != 0 {
return Err(Error::from_raw_os_error(EIO));
}

Ok(())
}

pub fn hci_exit_sniff_mode(dd: i32, handle: u16, timeout: i32) -> Result<()> {
use libbluetooth::hci::{
OGF_LINK_POLICY,
exit_sniff_mode_cp, OCF_EXIT_SNIFF_MODE, EXIT_SNIFF_MODE_CP_SIZE,
// evt_mode_change, EVT_MODE_CHANGE, EVT_MODE_CHANGE_SIZE,
evt_cmd_status, EVT_CMD_STATUS, EVT_CMD_STATUS_SIZE,
};

let mut cp = exit_sniff_mode_cp {
handle: handle,
};

// let mut rp = evt_mode_change {
// status: 0,
// handle: 0,
// mode: 0,
// interval: 0
// };

let mut rp = evt_cmd_status {
status: 0,
ncmd: 0,
opcode: 0
};

let mut rq = hci_request {
ogf: OGF_LINK_POLICY as u16,
ocf: OCF_EXIT_SNIFF_MODE as u16,
// event: EVT_MODE_CHANGE,
event: EVT_CMD_STATUS,
cparam: mut_void_ptr(&mut cp),
clen: EXIT_SNIFF_MODE_CP_SIZE as i32,
rparam: mut_void_ptr(&mut rp),
// rlen: EVT_MODE_CHANGE_SIZE as i32
rlen: EVT_CMD_STATUS_SIZE as i32
};

unsafe {
if libbluetooth::hci_lib::hci_send_req(dd, &mut rq, timeout) < 0 {
return Err(Error::last_os_error());
}
}

if rp.status != 0 {
return Err(Error::from_raw_os_error(EIO));
}

Ok(())
}


84 changes: 84 additions & 0 deletions src/hcidev.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use std::ffi::c_void;
use std::os::raw::{c_char, c_int};
use std::io::Result;
use libbluetooth::bluetooth::SOL_HCI;
use libbluetooth::hci::{HCI_EVENT_PKT, hci_filter, HCI_FILTER};
use crate::libc_helpe::libc_check_error;
use libbluetooth::hci_lib::{self, hci_open_dev, hci_close_dev, hci_read_class_of_dev, hci_write_class_of_dev, hci_filter_clear_opcode};
use libc::setsockopt;

pub struct HciDev {
hdev: c_int,
socket: c_int,
}

impl HciDev {
pub unsafe fn new(hdev: c_int) -> Result<HciDev> {
let socket = hci_open_dev(hdev);

Ok(HciDev {
hdev: hdev,
socket: libc_check_error(socket)?,
})
}

pub unsafe fn write_class(&mut self, cod: u32) -> Result<()> {
let write_res = hci_write_class_of_dev(self.socket, cod, 2000);
libc_check_error(write_res)?;
Ok(())
}

pub unsafe fn read_class(&mut self) -> Result<u32> {
let mut class = [0u8; 4];
let read_res = hci_read_class_of_dev(self.socket, class[1..].as_mut_ptr() as *mut c_char, 2000);
libc_check_error(read_res)?;

Ok(u32::from_be_bytes(class))
}

pub unsafe fn cmd_cmd(&mut self, ocf: u16, ogf: u16, cmd: &mut [u8]) -> std::io::Result<Vec<u8>> {
let mut flt = hci_filter {
type_mask: 0,
event_mask: [0, 0],
opcode: 0
};

/* Setup filter */
hci_lib::hci_filter_set_ptype(HCI_EVENT_PKT, &mut flt);
hci_lib::hci_filter_all_events(&mut flt);

libc_check_error(setsockopt(
self.socket,
SOL_HCI,
HCI_FILTER,
&flt as *const hci_filter as *const c_void,
std::mem::size_of::<hci_filter>() as libc::socklen_t
))?;

libc_check_error(hci_lib::hci_send_cmd(
self.socket,
ogf,
ocf,
cmd.len() as c_char,
cmd.as_mut_ptr() as *mut c_void
))?;

let mut buf = vec![0u8; libbluetooth::hci::HCI_MAX_EVENT_SIZE];
let len = libc::read(self.socket, buf.as_mut_ptr() as *mut c_void, buf.len());
libc_check_error(len)?;

buf.resize(len as usize, 0);
Ok(buf)
}
// pub unsafe fn write_cmd(&mut self) -> Result<()> {
// hci_write_
// }
}

impl Drop for HciDev {
fn drop(&mut self) {
unsafe {
hci_close_dev(self.socket);
}
}
}
46 changes: 45 additions & 1 deletion src/l2cap.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::smol_fd::{libc_check_error, SmolFd};
use std::borrow::Borrow;
use crate::smol_fd::SmolFd;
use crate::libc_helpe::libc_check_error;
use libbluetooth::bluetooth::bdaddr_t;
use std::io::{Read, Result, Write};
use std::mem::{size_of, MaybeUninit};
Expand All @@ -22,6 +24,27 @@ impl L2CAPListener {
)
})?;

unsafe {
let opt: libc::c_int = 0;

libc_check_error(libc::setsockopt(
socket,
libc::SOL_SOCKET,
libc::SO_SNDBUF,
&opt as *const i32 as *const libc::c_void,
std::mem::size_of::<libc::c_int>() as libc::socklen_t
))?;

const BT_POWER: libc::c_int = 9;
libc_check_error(libc::setsockopt(
socket,
libc::SOL_BLUETOOTH,
BT_POWER,
&opt as *const i32 as *const libc::c_void,
std::mem::size_of::<libc::c_int>() as libc::socklen_t
))?;
}

Ok(L2CAPListener {
fd: SmolFd::new(socket),
})
Expand Down Expand Up @@ -124,6 +147,27 @@ impl L2CAPStream {
)
})?;

unsafe {
let opt: libc::c_int = 0;

libc_check_error(libc::setsockopt(
socket,
libc::SOL_SOCKET,
libc::SO_SNDBUF,
&opt as *const i32 as *const libc::c_void,
std::mem::size_of::<libc::c_int>() as libc::socklen_t
))?;

const BT_POWER: libc::c_int = 9;
libc_check_error(libc::setsockopt(
socket,
libc::SOL_BLUETOOTH,
BT_POWER,
&opt as *const i32 as *const libc::c_void,
std::mem::size_of::<libc::c_int>() as libc::socklen_t
))?;
}

Ok(L2CAPStream {
fd: SmolFd::new(socket),
})
Expand Down
10 changes: 10 additions & 0 deletions src/libc_helpe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use std::io::{Error, Result};
use num_traits::{PrimInt, Signed, Zero};

pub fn libc_check_error<T: Signed + PrimInt + Zero>(val: T) -> Result<T> {
if val < T::zero() {
Err(Error::last_os_error())
} else {
Ok(val)
}
}
Loading

0 comments on commit f5b542e

Please sign in to comment.