Skip to content

Commit

Permalink
Added logs sumbcommand to edit PrincessLog config remotely
Browse files Browse the repository at this point in the history
  • Loading branch information
nikarh committed Nov 1, 2023
1 parent 82d8413 commit 94fda1a
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 24 deletions.
69 changes: 62 additions & 7 deletions Cargo.lock

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

12 changes: 1 addition & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,4 @@ suppaftp = { version = "5.2.1", features = ["no-log"] }
tee = "0.1.0"
tempfile = "3.8.0"
walkdir = "2.4.0"

[package.metadata.vita]
title_id = "VITASHELL"
title_name = "Test app"
assets = "static"
# You can choose a subset of std or use panic_abort if you don't need unwinding
build_std = "std,panic_unwind"
# You can provide a custom JSON file spec
vita_strip_flags = ["-g"]
vita_make_fself_flags = ["-s"]
vita_mksfoex_flags = ["-d", "ATTRIBUTE2=12"]
local-ip-address = "0.5.6"
122 changes: 116 additions & 6 deletions src/commands/logs.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,120 @@
use std::{io::Read, net::TcpListener};
use std::{
io::{Cursor, Read},
net::{Ipv4Addr, TcpListener},
str::FromStr,
};

use anyhow::Context;
use clap::Args;
use anyhow::{bail, Context};
use clap::{Args, Subcommand};
use colored::Colorize;

use super::Executor;
use crate::ftp;

use super::{ConnectionArgs, Executor};

#[derive(Args, Debug)]
pub struct Logs {
#[command(subcommand)]
cmd: Option<LogsCmd>,

#[arg(long, short = 'p', env = "VITA_LOG_PORT", default_value_t = 8888)]
port: u16,
}

impl Executor for Logs {
fn execute(&self, verbose: u8) -> anyhow::Result<()> {
#[derive(Subcommand, Debug)]
pub enum LogsCmd {
/// Start a TCP server on 0.0.0.0 and print to stdout all bytes read from the socket
Listen,
/// Reconfigures PrincessLog via vita-companion.
/// This will upload the configuration file with the ip address of your host and a port to your Vita.
Configure(Configure),
}

#[derive(Args, Debug)]
pub struct Configure {
#[command(flatten)]
pub connection: ConnectionArgs,

#[arg(long)]
host_ip_address: Option<String>,

#[arg(long, default_value = "false")]
kernel_debug: bool,
}

static MAGIC: &[u8] = b"NLM\0";
static NLM_CONFIG_FLAGS_BIT_QAF_DEBUG_PRINTF: u32 = 1 << 0;

impl Logs {
fn configure(&self, configure: &Configure, verbose: u8) -> anyhow::Result<()> {
let filename = "ur0:/data/NetLoggingMgrConfig.bin";
println!(
"{} {filename}",
"Downloading the existing config from".blue()
);
let mut ftp = ftp::connect(&configure.connection, verbose)?;

let file = ftp.retr_as_buffer(filename);
if let Ok(mut config) = file {
println!("{}", "Found existing config".blue());
let mut buffer = [0; 4];
config.read_exact(&mut buffer)?;
if buffer != MAGIC {
println!("{}", "Existing config is invalid".red());
}

config.read_exact(&mut buffer)?;
let ip = Ipv4Addr::from(buffer);
config.read_exact(&mut buffer)?;
let flags = u32::from_le_bytes(buffer);

let mut buffer = [0; 2];
config.read_exact(&mut buffer)?;
let port = u16::from_le_bytes(buffer);

println!("{}: {ip}:{port}", "Current log address".yellow());
let kdbg = (flags & NLM_CONFIG_FLAGS_BIT_QAF_DEBUG_PRINTF) != 0;
println!("{}: {kdbg}", "Kernel debug print".yellow());
}

let ip = match &configure.host_ip_address {
Some(ip) => Ipv4Addr::from_str(ip)?,
None => match local_ip_address::local_ip()? {
std::net::IpAddr::V4(ip) => ip,
std::net::IpAddr::V6(_) => bail!("Unable to guess host ip address"),
},
};

println!(
"{} {ip}:{port} {} {kdbg}",
"Setting log address to".blue(),
"and kernel debug print to".blue(),
port = self.port,
kdbg = configure.kernel_debug,
);

let mut config = Vec::new();
config.extend_from_slice(MAGIC);
config.extend_from_slice(&ip.octets());

let flags = match configure.kernel_debug {
true => NLM_CONFIG_FLAGS_BIT_QAF_DEBUG_PRINTF,
false => 0,
};

config.extend_from_slice(&flags.to_le_bytes());
config.extend_from_slice(&self.port.to_le_bytes());
config.extend_from_slice(&[0, 0]);

print!("{} {}", "Saving config to".blue(), filename);
let _ = ftp.mkdir("ur0:/data/");
ftp.put_file(filename, &mut Cursor::new(config))?;

println!("Config will take effect after Vita is rebooted");

Ok(())
}
fn listen(&self, verbose: u8) -> anyhow::Result<()> {
if verbose > 0 {
println!("{} {}", "Starting TCP server on port".blue(), self.port);
}
Expand Down Expand Up @@ -62,3 +163,12 @@ impl Executor for Logs {
Ok(())
}
}

impl Executor for Logs {
fn execute(&self, verbose: u8) -> anyhow::Result<()> {
match self.cmd.as_ref() {
Some(LogsCmd::Configure(cmd)) => self.configure(cmd, verbose),
Some(LogsCmd::Listen) | None => self.listen(verbose),
}
}
}

0 comments on commit 94fda1a

Please sign in to comment.