-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relay works on latest switch firmware
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
1 parent
2a81790
commit f5b542e
Showing
8 changed files
with
487 additions
and
123 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.