Skip to content

Commit

Permalink
Added basic cards support
Browse files Browse the repository at this point in the history
  • Loading branch information
LumaRay committed Jan 18, 2023
1 parent 6650480 commit 65469dc
Show file tree
Hide file tree
Showing 16 changed files with 1,017 additions and 38 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[package]
name = "uem-reader"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["Yury Laykov"]
description = "Library for MicroEM RFID readers"
documentation = "https://microem.ru/catalog/vstraivaemye_i_gotovye_ustroystva/rfid_schityvateli/"
readme = "README.md"
homepage = "https://github.com/LumaRay/uem-reader-rs"
homepage = "https://microem.ru/catalog/vstraivaemye_i_gotovye_ustroystva/rfid_schityvateli/"
repository = "https://github.com/LumaRay/uem-reader-rs"
license = "MIT OR Apache-2.0"
keywords = ["rfid", "nfc", "mifare", "iso-14443", "iso-15693"]
Expand All @@ -18,7 +17,7 @@ std = []

[dependencies]
rusb = "0.9"
usb-ids = "0.2.5"
usb-ids = "1.2022"
enum-iterator = "1.2.0"
thiserror = "1.0"
rand = "0.8.5"
114 changes: 107 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,25 @@ Note that in order to work with Windows you need to [install libusb driver first

## Usage

```rust
use uem_reader::reader::*;
use uem_reader::commands::*;
use uem_reader::commands::reader::*;
use uem_reader::reader::usb::find_usb_readers;
```
use uem_reader::{
reader::{
UemReaderInternal,
usb::find_usb_readers
},
commands::{
UemCommandsTrait,
reader::UemCommandsReaderTrait,
cards::{
UemCommandsCardsTrait,
UemActivateParameters,
mifare::{
UemCommandsCardsMifareTrait,
classic::UemCommandsCardsMifareClassicTrait
},
},
},
};
// Search system for USB readers
let mut uem_readers = find_usb_readers();
Expand All @@ -41,7 +55,7 @@ Note that in order to work with Windows you need to [install libusb driver first
}
// Beep 1 time using command byte vector
if uem_reader.send(vec![0x05_u8, 0x01_u8]).is_err() {
if uem_reader.send(&vec![0x05_u8, 0x01_u8]).is_err() {
return;
}
Expand All @@ -57,8 +71,94 @@ Note that in order to work with Windows you need to [install libusb driver first
return;
}
// Close USB interface connection
let card = uem_reader.commands().cards().activate_a(&UemActivateParameters{
// switch_to_tcl: true,
..Default::default()
});
if card.is_err() {
uem_reader.close();
return;
}
let card = card.unwrap();
let res = uem_reader.commands().cards().mifare().classic()
.authenticate_key_a(
&card,
&[0xFF; 6],
1
);
if res.is_err() {
uem_reader.close();
return;
}
let res = uem_reader.commands().cards().mifare().classic()
.read(1, 1);
if res.is_err() {
uem_reader.close();
return;
}
let prev_data = res.unwrap();
let mut new_data = prev_data.clone();
new_data[0] = 0xFF;
let res = uem_reader.commands().cards().mifare().classic()
.write(new_data.clone(), 1, 1);
if res.is_err() {
uem_reader.close();
return;
}
let res = uem_reader.commands().cards().mifare().classic()
.read(1, 1);
if res.is_err() {
uem_reader.close();
return;
}
if res.unwrap() != new_data {
println!("error!");
uem_reader.close();
return;
}
let res = uem_reader.commands().cards().mifare().classic()
.write(prev_data.clone(), 1, 1);
if res.is_err() {
uem_reader.close();
return;
}
let res = uem_reader.commands().cards().mifare().classic()
.read(1, 1);
if res.is_err() {
uem_reader.close();
return;
}
if res.unwrap() != prev_data {
println!("error!");
uem_reader.close();
return;
}
if uem_reader.close().is_err() {
return;
};
```

## License

This work is dual-licensed under MIT or Apache 2.0.
You can choose between one of them if you use this work.

`SPDX-License-Identifier: MIT OR Apache-2.0`
70 changes: 70 additions & 0 deletions src/card.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! Crate card related structures
//!

#![allow(dead_code)]

use enum_iterator::Sequence;

// #[repr(u8)]
// #[derive(Debug, Default, PartialEq, Sequence, Clone)]
// pub enum UemCardStandard {
// #[default]
// Iso14443a = 0x00,
// Iso14443b = 0x01,
// Iso15693 = 0x02,
// }

#[repr(u8)]
#[derive(Debug, Default, PartialEq, Sequence, Clone, Copy)]
pub enum UemCardBaudrates {
#[default]
Baud106kbps = 0b00,
Baud212kbps = 0b01,
Baud424kbps = 0b10,
Baud848kbps = 0b11,
}

#[derive(Debug, Clone)]
/// ISO14443A card type object
pub struct UemCardIso14443A {
/// Answer to request - 2 bytes
pub atq: Vec<u8>,
/// Select Acknowledge byte
pub sak: u8,
/// Unique identifier - 4/7/10 bytes
pub uid: Vec<u8>,
/// Answer to select - returned after switching
/// to T=CL mode
pub ats: Vec<u8>,
}

#[derive(Debug, Clone)]
/// ISO14443B card type object
pub struct UemCardIso14443B {
/// Buffer length identifier
pub mbli: u8,
/// Unique identifier - 4 bytes
pub pupi: Vec<u8>,
/// Application data - 4 bytes
pub app_data: Vec<u8>,
/// Protocol information - 3 bytes
pub prot_info: Vec<u8>,
/// Answer to request
pub atq: Vec<u8>,
}

/// General placeholder for a card object
pub enum UemCard {
Iso14443A(UemCardIso14443A),
Iso14443B(UemCardIso14443B),
}

impl UemCard {
/// Returns unique identifier of a card
pub fn uid(&self) -> &Vec<u8> {
match self {
Self::Iso14443A(card) => &card.uid,
Self::Iso14443B(card) => &card.pupi,
}
}
}
9 changes: 8 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Crate commands object type

pub mod reader;
pub mod cards;

use crate::reader::*;
use crate::commands::reader::*;
use crate::commands::{reader::*, cards::*};

/// Structure for grouping commands in general
pub struct UemCommands<'a> {
Expand All @@ -21,6 +22,12 @@ impl<'a> UemCommandsReaderTrait for UemCommands<'a> {
}
}

impl<'a> UemCommandsCardsTrait for UemCommands<'a> {
fn cards(&mut self) -> UemCommandsCards {
UemCommandsCards::new(self.as_reader())
}
}

impl<'a> UemCommands<'a> {
pub(crate) fn new(rd: &'a UemReader) -> Self {
UemCommands {reader: rd}
Expand Down
Loading

0 comments on commit 65469dc

Please sign in to comment.