Skip to content

Commit

Permalink
✨ feat(Test): Add BPiF3-Armbian
Browse files Browse the repository at this point in the history
- Finish fixes and changes for real use
- Finish test on BPiF3 - Armbian comb
  • Loading branch information
wychlw committed Sep 12, 2024
1 parent c580abb commit 20452d4
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/target
*.log
*.cast

# Byte-compiled / optimized / DLL files

Expand Down
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- [ ] **修 shell……**
- [ ] **想办法把那个 PyTty 缩小掉……**

- [ ] 更多的连接方式
- [x] 更完善的 SSH
Expand Down
8 changes: 5 additions & 3 deletions apps/boards/bpif3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from time import sleep
from tester import PyTty, PySerial, PySdWirec, PyExec
from tester import PyTty, PySerial, PySdWirec, PyExec, info


class BPiF3:
Expand All @@ -23,7 +23,8 @@ def flash(self, shell: PyExec, img: str, dsk="/dev/sda"):
self.sdwirec.to_ts()
sleep(0.5)
shell.assert_script_sudo(
f"dd if={img} of={dsk} bs=1M status=progress", 600)
f"dd if={img} of={dsk} status=progress", 600)
shell.assert_script_sudo("sync")
sleep(0.5)
self.sdwirec.to_dut()
sleep(0.5)
Expand All @@ -35,7 +36,8 @@ def power_cycle(self):
Maybe some relay or something?
But for now, manually power cycle the board.
"""
input("Please power cycle the board and press enter to continue.")
info("Please power cycle the board and continue. You have arount 10s.")
sleep(10)

def get_console(self) -> PyTty:
"""
Expand Down
21 changes: 17 additions & 4 deletions apps/bpif3_armbian.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def default_proc():

board = BPiF3("id = 0\n", "/dev/ttyUSB0", 115200)

url = "/AArmbian-bpi-SpacemiT_24.5.0-trunk_Bananapif3_mantic_legacy_6.1.15_xfce_desktop.img.xz" # 度盘,dummy
# url = "/Armbian-bpi-SpacemiT_24.5.0-trunk_Bananapif3_mantic_legacy_6.1.15_xfce_desktop.img.xz" # 度盘,dummy
url = "https://mirrors.tuna.tsinghua.edu.cn/armbian-releases/bananapif3/archive/Armbian_24.8.1_Bananapif3_noble_legacy_6.1.15_minimal.img.xz"
work_dir = "/home/lw/Work/plct/boards/bpif3/armbian"
img = wget_image(url, work_dir)
if img is None:
Expand All @@ -43,11 +44,12 @@ def default_proc():

info("Begin flashing board...")

board.flash(e, img)
board.flash(e, img, "/dev/mmcblk0")

info("Flash board ended...")

console = board.get_console()
console = PyTee(console, "con.log")

asciicast = e.exit()
local_shell = swap_tty(asciicast, console)
Expand All @@ -61,13 +63,24 @@ def default_proc():

system.loggin()

asciicast = e.exit()
logger = PyTty("wrap=true\nsimple_recorder=true\n", asciicast)
logger.begin()
e = PyExec(logger)
system.tty = e

system.get_info()

asciicast = e.exit()
logger = e.exit()
info_log = logger.end()
asciicast = logger.exit()
res = asciicast.end()

with open("res.cast") as f:
with open("res.cast", "w") as f:
f.write(res)

with open("info.log", "w") as f:
f.write(info_log)

if __name__ == "__main__":
default_proc()
41 changes: 23 additions & 18 deletions apps/system/armbian.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
from time import sleep
from tester import PyExec

def slp(t = 0.5):

def slp(t=0.5):
"""
Sleep for t second.
"""
sleep(t)


class Armbian:
"""
System class for Armbian.
"""

def __init__(self, tty: PyExec):
self.tty = tty

Expand All @@ -38,32 +41,34 @@ def loggin(self):
"""

# init settings
self.wit("IP address:")
self.wln("")
self.wit("Create root password:")
self.wit("Create root password")
self.wln("autotest_123")
self.wit("Repeat root password:")
self.wit("Repeat root password")
self.wln("autotest_123")
# Choose default system command shell:
slp(2)
self.wln("1")
# slp(2)
# self.wln("1")

# register new user
self.wit("(eg. your first name):")
self.wit("(eg. your first name)")
self.wln("plct")
self.wit("password:")
self.wit("password")
self.wln("plct_123")
self.wit("password:")
self.wit("password")
self.wln("plct_123")
self.wit("real name:")
self.wln("Plct")
self.wit("real name")
self.wln("")

# Set user language based on your location? [Y/n]
self.wit("[Y/n]")
self.wln("y")
# Set time zone
self.wit("choice:")
self.wln("328")
try:
self.wit("wireless", 10)
self.wln("n")
except Exception:
pass

# At your location, more locales are possible:
self.wit("location")
slp(2)
self.wln("332")
# Please select a continent, ocean, "coord", or "TZ".
self.wit("#?")
self.wln("4")
Expand Down
8 changes: 5 additions & 3 deletions src/exec/cli_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

use crate::{
consts::DURATION, err, log, term::tty::{DynTty, InnerTty, Tty, WrapperTty}, util::{anybase::AnyBase, util::rand_string}
consts::DURATION, err, info, log, term::tty::{DynTty, InnerTty, Tty, WrapperTty}, util::{anybase::AnyBase, util::rand_string}
};

use super::cli_api::CliTestApi;
Expand All @@ -23,7 +23,7 @@ impl CliTester {

impl CliTester {
fn run_command(&mut self, command: &String) -> Result<(), Box<dyn Error>> {
log!("Write to shell: {}", command);
info!("Write to shell: {}", command);
let res = self.inner.write(command.as_bytes());
if let Err(e) = res {
return Err(e);
Expand Down Expand Up @@ -78,6 +78,7 @@ impl CliTestApi for CliTester {
fn wait_serial(&mut self, expected: &str, timeout: u32) -> Result<(), Box<dyn Error>> {
let begin = Instant::now();
let mut buf = Vec::new();
info!("Waiting for string {{{}}}", expected);
loop {
sleep(Duration::from_millis(DURATION));
let res = self.inner.read();
Expand All @@ -86,8 +87,9 @@ impl CliTestApi for CliTester {
}
let line = res.unwrap();
buf.extend_from_slice(&line);
let content = String::from_utf8(buf.clone()).unwrap();
let content = String::from_utf8(buf.clone()).unwrap_or_default();
if content.contains(expected) {
info!("Matched string {{{}}}", expected);
break;
}
if begin.elapsed().as_secs() > timeout as u64 {
Expand Down
58 changes: 30 additions & 28 deletions src/term/asciicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl Asciicast {
logged.push(Entry {
time: timestamp,
event_type: EventType::Output,
event_data: String::from_utf8(new_data.clone()).unwrap(),
event_data: String::from_utf8(new_data.clone()).unwrap_or_default(),
});
}

Expand Down Expand Up @@ -144,29 +144,30 @@ impl Tty for Asciicast {
return Ok(res);
}
fn write(&mut self, data: &[u8]) -> Result<(), Box<dyn Error>> {
let begin = self.begin.lock();
if let Err(_) = begin {
return Err(Box::<dyn Error>::from("Recorder not started."));
}
let begin = begin.unwrap();
if *begin {
let begin_time = self.begin_time.lock().unwrap();
let time = begin_time.elapsed().unwrap();
let timestamp = time.as_millis();
let timestamp = timestamp as f64 / 1000.0;
let mut logged = self.logged.lock().unwrap();
let line = String::from_utf8(data.to_vec()).unwrap();
let line = SHELL_PROMPT.to_string() + &line;
let line = line.replace("\\n", "\\r\\n");
logged.push(Entry {
time: timestamp,
// event_type: EventType::Input,
event_type: EventType::Output,
event_data: line,
});
}

let mut inner = self.inner.lock().unwrap();
// let begin = self.begin.lock();
// if let Err(_) = begin {
// return Err(Box::<dyn Error>::from("Recorder not started."));
// }
// let begin = begin.unwrap();
// if *begin {
// let begin_time = self.begin_time.lock().unwrap();
// let time = begin_time.elapsed().unwrap();
// let timestamp = time.as_millis();
// let timestamp = timestamp as f64 / 1000.0;
// let mut logged = self.logged.lock().unwrap();
// let line = String::from_utf8(data.to_vec()).unwrap();
// let line = SHELL_PROMPT.to_string() + &line;
// let line = line.replace("\\n", "\\r\\n");
// logged.push(Entry {
// time: timestamp,
// // event_type: EventType::Input,
// event_type: EventType::Output,
// event_data: line,
// });
// }

let inner = self.inner.clone();
let mut inner = inner.lock().unwrap();
if inner.is_none() {
return Err(Box::<dyn Error>::from("You've already exited."));
}
Expand All @@ -179,7 +180,8 @@ impl Tty for Asciicast {

impl WrapperTty for Asciicast {
fn exit(self) -> DynTty {
let mut inner = self.inner.lock().unwrap();
let inner = self.inner.clone();
let mut inner = inner.lock().unwrap();
let inner = inner.take().unwrap();
inner
}
Expand Down Expand Up @@ -279,14 +281,14 @@ impl Recorder for Asciicast {
}

fn swap(&mut self, target: DynTty) -> Result<DynTty, Box<dyn Error>> {
sleep(Duration::from_micros(100));
let mut inner = self.inner.lock().unwrap();
sleep(Duration::from_micros(DURATION));
let inner = self.inner.clone();
let mut inner = inner.lock().unwrap();
if inner.is_none() {
return Err(Box::<dyn Error>::from("You've already exited."));
}
let res = inner.take().unwrap();
*inner = Some(target);
sleep(Duration::from_micros(100));
Ok(res)
}
}
26 changes: 19 additions & 7 deletions src/term/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ use std::time::Duration;
use serialport::{self, SerialPort};

use crate::consts::SHELL_DURATION;
use crate::{err, info};
use crate::term::tty::Tty;
use crate::util::anybase::AnyBase;
use crate::{err, info};

pub struct Serial {
inner: Box<dyn SerialPort>,
}

impl Serial {
pub fn build(port: &str, baud: u32) -> Result<Serial, Box<dyn Error>> {
let inner = serialport::new(port, baud).open();
let inner = serialport::new(port, baud)
.timeout(Duration::from_millis(50))
.open();

if let Err(e) = inner {
err!("Open serial port failed! Reason: {}", e);
Expand Down Expand Up @@ -47,15 +49,18 @@ impl AnyBase for Serial {
impl Tty for Serial {
fn read(&mut self) -> Result<Vec<u8>, Box<dyn Error>> {
let mut buf = Vec::new();
sleep(Duration::from_millis(SHELL_DURATION));
loop {
sleep(Duration::from_millis(SHELL_DURATION));
let mut buff = [0u8];
match self.inner.read(&mut buff) {
Ok(_) => {
if buff[0] == 0x0 {
return Ok(buf);
}
buf.extend_from_slice(&buff);
return Ok(buf);
}
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
Err(e) if e.kind() == ErrorKind::TimedOut => return Ok(buf),
Err(e) => {
err!("Read from serial port failed. Reason: {}", e);
return Err(Box::new(e));
Expand All @@ -65,8 +70,8 @@ impl Tty for Serial {
}
fn read_line(&mut self) -> Result<Vec<u8>, Box<dyn Error>> {
let mut buf = Vec::new();
sleep(Duration::from_millis(SHELL_DURATION));
loop {
sleep(Duration::from_millis(SHELL_DURATION));
let mut buff = [0u8];
match self.inner.read(&mut buff) {
Ok(_) => {
Expand All @@ -87,9 +92,16 @@ impl Tty for Serial {
fn write(&mut self, data: &[u8]) -> Result<(), Box<dyn Error>> {
loop {
sleep(Duration::from_millis(SHELL_DURATION));
self.inner.flush()?;
match self.inner.write_all(data) {
Ok(_) => break,
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
Ok(_) => {
self.inner.flush()?;
break;
},
Err(e) if e.kind() == ErrorKind::Interrupted => {
info!("Write being Interrupted!");
continue;
},
Err(e) => {
err!("Write to serial port failed. Reason: {}", e);
return Err(Box::new(e));
Expand Down
2 changes: 1 addition & 1 deletion src/term/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
collections::HashMap,
env,
error::Error,
io::{BufReader, ErrorKind, Read, Write},
io::{ErrorKind, Read, Write},
process::{ChildStdin, Command, Stdio},
sync::{Arc, Mutex},
thread::{sleep, spawn, JoinHandle},
Expand Down

0 comments on commit 20452d4

Please sign in to comment.