implement card model
This commit is contained in:
+30
-7
@@ -1,5 +1,7 @@
|
||||
use alloc::string::{String, ToString};
|
||||
use core::fmt::Display;
|
||||
|
||||
use crate::card::model::NfcPayload;
|
||||
use alloc::string::{String, ToString};
|
||||
|
||||
pub fn split_nfc_hex(payload: &[u8]) -> Option<NfcPayload<'_>> {
|
||||
if payload.len() < 0x35A {
|
||||
@@ -18,13 +20,34 @@ pub fn split_nfc_hex(payload: &[u8]) -> Option<NfcPayload<'_>> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn decode_known_event(event_encoding: &[u8]) -> Option<&'static str> {
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Events {
|
||||
CCC39,
|
||||
CCC40,
|
||||
GPN24,
|
||||
GPN25,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl Display for Events {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
match *self {
|
||||
Events::CCC39 => write!(f, "39C3"),
|
||||
Events::CCC40 => write!(f, "40C3"),
|
||||
Events::GPN24 => write!(f, "GPN24"),
|
||||
Events::GPN25 => write!(f, "GPN25"),
|
||||
Events::Unknown => write!(f, "Unknown"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decode_known_event(event_encoding: &[u8]) -> Events {
|
||||
match event_encoding {
|
||||
[0x39, 0xC3] => Some("39C3"),
|
||||
[0x40, 0xC3] => Some("40C3"), // future proofing :)
|
||||
[0xE9, 0x24] => Some("GPN 24"),
|
||||
[0xE9, 0x25] => Some("GPN 25"), // future proofing :)
|
||||
_ => None,
|
||||
[0x39, 0xC3] => Events::CCC39,
|
||||
[0x40, 0xC3] => Events::CCC40, // future proofing :)
|
||||
[0xE9, 0x24] => Events::GPN24,
|
||||
[0xE9, 0x25] => Events::GPN25, // future proofing :)
|
||||
_ => Events::Unknown,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+42
-10
@@ -1,4 +1,7 @@
|
||||
use alloc::{string::String, vec::Vec};
|
||||
use num_enum::FromPrimitive;
|
||||
|
||||
use crate::card::decoder::{Events, decode_known_event, decode_packed_card_text, decode_secret};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct NfcPayload<'a> {
|
||||
@@ -12,16 +15,18 @@ pub struct NfcPayload<'a> {
|
||||
pub _opaque_trailer: &'a [u8],
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, Eq, PartialEq, FromPrimitive)]
|
||||
#[repr(u8)]
|
||||
pub enum CardType {
|
||||
Default,
|
||||
Blueprint,
|
||||
CYBER,
|
||||
Nature,
|
||||
Fossil,
|
||||
Ghost,
|
||||
Daemon,
|
||||
Fairy,
|
||||
#[num_enum(default)]
|
||||
Default = 0,
|
||||
Blueprint = 1,
|
||||
CYBER = 2,
|
||||
Nature = 3,
|
||||
Fossil = 4,
|
||||
Ghost = 5,
|
||||
Daemon = 6,
|
||||
Fairy = 7,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -32,8 +37,9 @@ pub struct Sprite {
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Card {
|
||||
pub uuid: String,
|
||||
pub name: String,
|
||||
pub event: Events,
|
||||
pub cardtype: CardType,
|
||||
pub name: String,
|
||||
|
||||
pub trait1: String,
|
||||
pub trait2: String,
|
||||
@@ -42,3 +48,29 @@ pub struct Card {
|
||||
|
||||
pub sprite: Sprite,
|
||||
}
|
||||
|
||||
impl TryFrom<NfcPayload<'static>> for Card {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(payload: NfcPayload) -> Result<Self, Self::Error> {
|
||||
let uuid: String = str::from_utf8(payload.card_uuid).unwrap().into();
|
||||
let event = decode_known_event(payload.event_encoding);
|
||||
let cardtype = CardType::from(payload.card_type);
|
||||
let (name, trait1, trait2, trait3) = decode_packed_card_text(payload.packed_card_text);
|
||||
let secret = decode_secret(payload.secret);
|
||||
let sprite = Sprite {
|
||||
data: payload.sprite.to_vec(),
|
||||
};
|
||||
Ok(Self {
|
||||
uuid,
|
||||
event,
|
||||
cardtype,
|
||||
name,
|
||||
trait1,
|
||||
trait2,
|
||||
trait3,
|
||||
secret,
|
||||
sprite,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user