Skip to content

Commit

Permalink
fix: forgot one slash and pcie was borked (also error logging)
Browse files Browse the repository at this point in the history
  • Loading branch information
zleyyij committed Feb 17, 2024
1 parent c2a4515 commit c04856b
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 16 deletions.
28 changes: 26 additions & 2 deletions src/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};

use levenshtein::levenshtein;
use log::{debug, trace};
Expand All @@ -18,6 +18,21 @@ pub struct Cpu {
pub attributes: HashMap<String, String>,
}

struct IndexEntry {
/// The primary identifier for a processor, like:
/// - `14900` in `i9-14900k`
model: String,
/// A list of modifiers applied directly to the processor number, like:
/// - `F` in `i5-11400F`
/// - `i7-` in `i7-7600`
prefixes: String,
/// Similar to modifiers, but they're not directly a part of the processor, like:
/// `PRO` in `Ryzen 5 PRO 5600`
tags: HashSet<String>,
/// The index of the corresponding cpu in the "full" vec
index: usize,
}

#[derive(Clone)]
pub struct CpuCache {
intel_cpus: Vec<Cpu>,
Expand Down Expand Up @@ -95,8 +110,17 @@ impl CpuCache {
}
}

///
fn generate_index_entry(name: &str) -> IndexEntry {
let model_token = find_model(name);

todo!()
}

/// Search the input string for the section that refers to the model of a CPU.
/// For example, given an input string of "AMD Ryzen 5 3600", it would try to return "3600"
/// For example, given an input string of "AMD Ryzen 5 3600", it would try to return "3600".
/// This function does return the whole token associated with a model, so prefixes and suffixes
/// are included
fn find_model(input: &str) -> String {
let mut best_fit = "";
let mut high_score: isize = -10;
Expand Down
16 changes: 11 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use chrono::Local;
use clap::Parser;
use colored::*;
use cpu::{Cpu, CpuCache};
use log::info;
use log::{error, info};
use log::{Level, LevelFilter, Metadata, Record};
use pcie::PcieCache;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -77,7 +77,7 @@ struct UsbResponse {
pub device: Option<String>,
}

/// This handler accepts a `GET` request to `/api/usbs/?identifier.
/// This handler accepts a `GET` request to `/api/usbs/?identifier`.
/// It relies on a globally shared [AppState] to re-use the usb cache.
async fn get_usb_handler(
State(state): State<AppState>,
Expand All @@ -90,7 +90,10 @@ async fn get_usb_handler(
vendor: r.0.map(|v| v.name),
device: r.1.map(|d| d.name),
})),
Err(_) => Err(StatusCode::NOT_FOUND),
Err(e) => {
error!("usb error: {:?} caused by query: {:?}", e, query);
Err(StatusCode::NOT_FOUND)
},
}
}

Expand Down Expand Up @@ -119,7 +122,10 @@ async fn get_pcie_handler(
device: r.1.map(|d| d.name),
subsystem: r.2.map(|s| s.name),
})),
Err(_) => Err(StatusCode::NOT_FOUND),
Err(e) => {
error!("pcie error: {:?} caused by query: {:?}", e, query);
Err(StatusCode::NOT_FOUND)
}
}
}

Expand Down Expand Up @@ -151,7 +157,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let app = Router::new()
.route("/api/cpus/", get(get_cpu_handler))
.route("/api/usbs/", get(get_usb_handler))
.route("/api/pcie", get(get_pcie_handler))
.route("/api/pcie/", get(get_pcie_handler))
.layer(CorsLayer::new().allow_origin("*".parse::<HeaderValue>().unwrap()))
.with_state(AppState {
cpu_cache: CpuCache::new(),
Expand Down
45 changes: 36 additions & 9 deletions src/pcie/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::NomError;
use nom::bytes::complete::{tag, take, take_until};
use nom::character::complete::char;
use nom::sequence::{delimited, preceded, terminated};
use nom::IResult;
use crate::NomError;

// the input file was obtained from https://pci-ids.ucw.cz/
const FILE_INPUT: &str = include_str!("./pci.ids.txt");
Expand Down Expand Up @@ -224,9 +224,12 @@ fn read_subsystem_line(input: &str) -> IResult<&str, Subsystem> {

#[cfg(test)]
mod tests {
use crate::pcie::{parse_device_identifier, read_device, read_subsystem_line, read_vendor, Device, Subsystem, Vendor};
use crate::pcie::{
parse_device_identifier, read_device, read_subsystem_line, read_vendor, Device, Subsystem,
Vendor,
};

use super::{parse_pcie_db, read_header};
use super::{parse_pcie_db, read_header, PcieCache};

#[test]
fn basic_read_header() {
Expand Down Expand Up @@ -329,11 +332,35 @@ mod tests {
#[test]
fn basic_parse_device_identifier() {
// https://learn.microsoft.com/en-us/windows-hardware/drivers/install/identifiers-for-pci-devices
assert_eq!(parse_device_identifier("PCI\\VEN_1234&DEV_5678&SUBSYS_91230000&REV_00"), Ok(("1234", "5678", Some("9123"))));
assert_eq!(parse_device_identifier("PCI\\VEN_1234&DEV_5678&SUBSYS_91230000"), Ok(("1234", "5678", Some("9123"))));
assert_eq!(parse_device_identifier("PCI\\VEN_1234&DEV_5678&REV_00"), Ok(("1234", "5678", None)));
assert_eq!(parse_device_identifier("PCI\\VEN_1234&DEV_5678"), Ok(("1234", "5678", None)));
assert_eq!(parse_device_identifier("PCI\\VEN_1234&DEV_5678&CC_112200"), Ok(("1234", "5678", None)));
assert_eq!(parse_device_identifier("PCI\\VEN_1234&DEV_5678&CC_1122"), Ok(("1234", "5678", None)));
assert_eq!(
parse_device_identifier("PCI\\VEN_1234&DEV_5678&SUBSYS_91230000&REV_00"),
Ok(("1234", "5678", Some("9123")))
);
assert_eq!(
parse_device_identifier("PCI\\VEN_1234&DEV_5678&SUBSYS_91230000"),
Ok(("1234", "5678", Some("9123")))
);
assert_eq!(
parse_device_identifier("PCI\\VEN_1234&DEV_5678&REV_00"),
Ok(("1234", "5678", None))
);
assert_eq!(
parse_device_identifier("PCI\\VEN_1234&DEV_5678"),
Ok(("1234", "5678", None))
);
assert_eq!(
parse_device_identifier("PCI\\VEN_1234&DEV_5678&CC_112200"),
Ok(("1234", "5678", None))
);
assert_eq!(
parse_device_identifier("PCI\\VEN_1234&DEV_5678&CC_1122"),
Ok(("1234", "5678", None))
);
}

// #[test]
// fn basic_find_device() {
// let cache = PcieCache::new();
// // println!("{:#?}", cache.find("PCI\\VEN_1022&DEV_1633&SUBSYS_14531022&REV_00\\3&2411E6FE&1&09").map(|t| t.1));
// }
}

0 comments on commit c04856b

Please sign in to comment.