// Copyright 2018 Matthias P. Braendli // SPDX-License-Identifier: GPL-2.0-only extern crate libc; extern crate raspi_rfm95_kiss; use raspi_rfm95_kiss::rfm95::{RF95, Bandwidth, CodingRate, SpreadingFactor}; use std::fs::{OpenOptions, File}; use std::ffi::{CStr, CString}; use std::os::unix::io::AsRawFd; const MAX_MTU: usize = 251; extern { fn kissattach( callsign: * const libc::c_char, speed: libc::int32_t, mtu: libc::int32_t, kttyname: * const libc::c_char, allow_broadcast: libc::int32_t) -> libc::int32_t; } fn create_pts_pair() -> std::io::Result { let master_file = OpenOptions::new() .read(true) .write(true) .open("/dev/ptmx")?; unsafe { let master_fd = master_file.as_raw_fd(); if libc::grantpt(master_fd) == -1 { return Err(std::io::Error::last_os_error()); } if libc::unlockpt(master_fd) == -1 { return Err(std::io::Error::last_os_error()); } } Ok(master_file) } fn main() { eprintln!("Creating PTY pair"); let master_file = match create_pts_pair() { Ok(fd) => fd, Err(e) => panic!("create_pts_pair failed: {}", e) }; eprintln!("PTS master: {:?}", master_file); let slavename; unsafe { slavename = libc::ptsname(master_file.as_raw_fd()); } if slavename.is_null() { panic!("Cannot get PTS slave name"); } unsafe { let slice = CStr::from_ptr(slavename); eprintln!("PTS slave: {:?}", slice); } match RF95::new(Bandwidth::Bw250, CodingRate::Cr8, SpreadingFactor::Sf10) { Ok(mut radio) => { radio.reset().expect("radio reset"); if let Ok(ver) = radio.get_version() { println!("Device version: {:02x}", ver); } else { eprintln!("Cannot read device version"); } }, Err(e) => panic!("Cannot create lora radio object, {}", e) }; let callsign = CString::new("HB9EGM-1").expect("Failed to convert callsign to CString"); let speed : i32 = 9600; let mtu : i32 = 251; let allow_broadcast : i32 = 1; let success = unsafe { kissattach( callsign.as_ptr(), speed, mtu, slavename, allow_broadcast) }; if success == 0 { panic!("kissattach failed"); } let todo = "Read/Write from/to TTY and RFM95"; /* let master_tty = Box::new(master_file); let writer = thread::spawn(|| { loop { let mut buffer = [0; MAX_MTU]; match *master_tty.read(&mut buffer[..]) { Ok(n) => radio.write_buffer(buffer), Err(e) => panic!("TTY Read error {}", e); } } }); writer.join().unwrap(); */ }