pepper-c1 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pepper_c1-0.1.0/PKG-INFO +139 -0
- pepper_c1-0.1.0/README.md +111 -0
- pepper_c1-0.1.0/pepper_c1/__init__.py +25 -0
- pepper_c1-0.1.0/pepper_c1/client.py +306 -0
- pepper_c1-0.1.0/pepper_c1/commands.py +124 -0
- pepper_c1-0.1.0/pepper_c1/exceptions.py +15 -0
- pepper_c1-0.1.0/pepper_c1/protocol.py +148 -0
- pepper_c1-0.1.0/pepper_c1/transport.py +118 -0
- pepper_c1-0.1.0/pepper_c1.egg-info/PKG-INFO +139 -0
- pepper_c1-0.1.0/pepper_c1.egg-info/SOURCES.txt +15 -0
- pepper_c1-0.1.0/pepper_c1.egg-info/dependency_links.txt +1 -0
- pepper_c1-0.1.0/pepper_c1.egg-info/requires.txt +5 -0
- pepper_c1-0.1.0/pepper_c1.egg-info/top_level.txt +1 -0
- pepper_c1-0.1.0/pyproject.toml +41 -0
- pepper_c1-0.1.0/setup.cfg +4 -0
- pepper_c1-0.1.0/tests/test_client.py +145 -0
- pepper_c1-0.1.0/tests/test_protocol.py +143 -0
pepper_c1-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: pepper_c1
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python driver for the Eccel Peeper C1 RFID reader (UART / TCP)
|
|
5
|
+
Author-email: Eccel Technology Ltd <marcin.baliniak@eccel.co.uk>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://www.eccel.co.uk
|
|
8
|
+
Project-URL: Product, https://eccel.co.uk/product/pepper-c1-usb/
|
|
9
|
+
Project-URL: Documentation, https://eccel.co.uk/product/pepper-c1-usb/
|
|
10
|
+
Keywords: rfid,nfc,mifare,desfire,icode,eccel,peeper,iso14443,iso15693
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Topic :: System :: Hardware :: Hardware Drivers
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Classifier: Intended Audience :: Developers
|
|
22
|
+
Requires-Python: >=3.8
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
Requires-Dist: pyserial>=3.5
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=7; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-mock; extra == "dev"
|
|
28
|
+
|
|
29
|
+
# pepper_c1
|
|
30
|
+
|
|
31
|
+
Python driver for the **[Eccel Peeper C1](https://eccel.co.uk/product/pepper-c1-usb/)** RFID reader
|
|
32
|
+
made by [Eccel Technology Ltd](https://www.eccel.co.uk).
|
|
33
|
+
|
|
34
|
+
Supports communication over **UART** and **TCP**, and covers MIFARE Classic,
|
|
35
|
+
MIFARE Ultralight, MIFARE DESFire, and ICODE tag families.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install pepper_c1
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Quick start
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from pepper_c1 import PepperC1, UARTTransport, TCPTransport
|
|
47
|
+
|
|
48
|
+
# UART
|
|
49
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0', baudrate=115200)) as reader:
|
|
50
|
+
print(reader.get_version())
|
|
51
|
+
print(reader.get_uid().hex())
|
|
52
|
+
|
|
53
|
+
# TCP
|
|
54
|
+
with PepperC1(TCPTransport('192.168.1.100', 1234)) as reader:
|
|
55
|
+
count = reader.get_tag_count()
|
|
56
|
+
if count:
|
|
57
|
+
uid = reader.activate_tag()
|
|
58
|
+
print(f"Tag UID: {uid.hex()}")
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## MIFARE Classic example
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from pepper_c1 import PepperC1, UARTTransport
|
|
65
|
+
|
|
66
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0')) as reader:
|
|
67
|
+
reader.activate_tag()
|
|
68
|
+
block = reader.mf_read_block(4)
|
|
69
|
+
print(f"Block 4: {block.hex()}")
|
|
70
|
+
reader.mf_write_block(4, b'\x00' * 16)
|
|
71
|
+
reader.halt()
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## MIFARE Ultralight example
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
from pepper_c1 import PepperC1, UARTTransport
|
|
78
|
+
|
|
79
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0')) as reader:
|
|
80
|
+
reader.activate_tag()
|
|
81
|
+
page = reader.mfu_read_page(4)
|
|
82
|
+
print(f"Page 4: {page.hex()}")
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## MIFARE DESFire example
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from pepper_c1 import PepperC1, UARTTransport
|
|
89
|
+
from pepper_c1.commands import KEY_TYPE_AES128
|
|
90
|
+
|
|
91
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0')) as reader:
|
|
92
|
+
# DESFire does not need activate_tag
|
|
93
|
+
reader.mfdf_select_app(bytes([0x00, 0x00, 0x00])) # PICC master app
|
|
94
|
+
# Load key into reader slot 0, then authenticate (key_no=0 on card, slot=0 in reader)
|
|
95
|
+
reader.set_key(0, KEY_TYPE_AES128, bytes(16)) # all-zero AES key
|
|
96
|
+
reader.mfdf_auth_aes(key_no=0, key_slot=0)
|
|
97
|
+
data = reader.mfdf_read_data(file_no=1, offset=0, length=32)
|
|
98
|
+
print(data.hex())
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Polling / async events
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
from pepper_c1 import PepperC1, UARTTransport
|
|
105
|
+
|
|
106
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0')) as reader:
|
|
107
|
+
reader.set_polling(enable=True, interval_ms=500)
|
|
108
|
+
# Async tag events accumulate in reader.async_events
|
|
109
|
+
import time
|
|
110
|
+
time.sleep(5)
|
|
111
|
+
reader.set_polling(enable=False)
|
|
112
|
+
for event in reader.async_events:
|
|
113
|
+
print(f"Async event: {event.hex()}")
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Error handling
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
from pepper_c1 import PepperC1, UARTTransport, CommandError, TransportError
|
|
120
|
+
|
|
121
|
+
try:
|
|
122
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0')) as reader:
|
|
123
|
+
reader.get_uid()
|
|
124
|
+
except TransportError as e:
|
|
125
|
+
print(f"Connection failed: {e}")
|
|
126
|
+
except CommandError as e:
|
|
127
|
+
print(f"Device error: {e}")
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Protocol
|
|
131
|
+
|
|
132
|
+
The Peeper C1 uses a binary framing protocol over serial or TCP:
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
[0xF5] [LEN_L] [LEN_H] [LEN_L^0xFF] [LEN_H^0xFF] [PAYLOAD...] [CRC_L] [CRC_H]
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
CRC is CCITT-16 computed over the payload only.
|
|
139
|
+
Low-level access is available via `PepperC1.send_command(cmd, data)`.
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# pepper_c1
|
|
2
|
+
|
|
3
|
+
Python driver for the **[Eccel Peeper C1](https://eccel.co.uk/product/pepper-c1-usb/)** RFID reader
|
|
4
|
+
made by [Eccel Technology Ltd](https://www.eccel.co.uk).
|
|
5
|
+
|
|
6
|
+
Supports communication over **UART** and **TCP**, and covers MIFARE Classic,
|
|
7
|
+
MIFARE Ultralight, MIFARE DESFire, and ICODE tag families.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pip install pepper_c1
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from pepper_c1 import PepperC1, UARTTransport, TCPTransport
|
|
19
|
+
|
|
20
|
+
# UART
|
|
21
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0', baudrate=115200)) as reader:
|
|
22
|
+
print(reader.get_version())
|
|
23
|
+
print(reader.get_uid().hex())
|
|
24
|
+
|
|
25
|
+
# TCP
|
|
26
|
+
with PepperC1(TCPTransport('192.168.1.100', 1234)) as reader:
|
|
27
|
+
count = reader.get_tag_count()
|
|
28
|
+
if count:
|
|
29
|
+
uid = reader.activate_tag()
|
|
30
|
+
print(f"Tag UID: {uid.hex()}")
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## MIFARE Classic example
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from pepper_c1 import PepperC1, UARTTransport
|
|
37
|
+
|
|
38
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0')) as reader:
|
|
39
|
+
reader.activate_tag()
|
|
40
|
+
block = reader.mf_read_block(4)
|
|
41
|
+
print(f"Block 4: {block.hex()}")
|
|
42
|
+
reader.mf_write_block(4, b'\x00' * 16)
|
|
43
|
+
reader.halt()
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## MIFARE Ultralight example
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from pepper_c1 import PepperC1, UARTTransport
|
|
50
|
+
|
|
51
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0')) as reader:
|
|
52
|
+
reader.activate_tag()
|
|
53
|
+
page = reader.mfu_read_page(4)
|
|
54
|
+
print(f"Page 4: {page.hex()}")
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## MIFARE DESFire example
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from pepper_c1 import PepperC1, UARTTransport
|
|
61
|
+
from pepper_c1.commands import KEY_TYPE_AES128
|
|
62
|
+
|
|
63
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0')) as reader:
|
|
64
|
+
# DESFire does not need activate_tag
|
|
65
|
+
reader.mfdf_select_app(bytes([0x00, 0x00, 0x00])) # PICC master app
|
|
66
|
+
# Load key into reader slot 0, then authenticate (key_no=0 on card, slot=0 in reader)
|
|
67
|
+
reader.set_key(0, KEY_TYPE_AES128, bytes(16)) # all-zero AES key
|
|
68
|
+
reader.mfdf_auth_aes(key_no=0, key_slot=0)
|
|
69
|
+
data = reader.mfdf_read_data(file_no=1, offset=0, length=32)
|
|
70
|
+
print(data.hex())
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Polling / async events
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
from pepper_c1 import PepperC1, UARTTransport
|
|
77
|
+
|
|
78
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0')) as reader:
|
|
79
|
+
reader.set_polling(enable=True, interval_ms=500)
|
|
80
|
+
# Async tag events accumulate in reader.async_events
|
|
81
|
+
import time
|
|
82
|
+
time.sleep(5)
|
|
83
|
+
reader.set_polling(enable=False)
|
|
84
|
+
for event in reader.async_events:
|
|
85
|
+
print(f"Async event: {event.hex()}")
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Error handling
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
from pepper_c1 import PepperC1, UARTTransport, CommandError, TransportError
|
|
92
|
+
|
|
93
|
+
try:
|
|
94
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0')) as reader:
|
|
95
|
+
reader.get_uid()
|
|
96
|
+
except TransportError as e:
|
|
97
|
+
print(f"Connection failed: {e}")
|
|
98
|
+
except CommandError as e:
|
|
99
|
+
print(f"Device error: {e}")
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Protocol
|
|
103
|
+
|
|
104
|
+
The Peeper C1 uses a binary framing protocol over serial or TCP:
|
|
105
|
+
|
|
106
|
+
```
|
|
107
|
+
[0xF5] [LEN_L] [LEN_H] [LEN_L^0xFF] [LEN_H^0xFF] [PAYLOAD...] [CRC_L] [CRC_H]
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
CRC is CCITT-16 computed over the payload only.
|
|
111
|
+
Low-level access is available via `PepperC1.send_command(cmd, data)`.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""pepper_c1 — Python driver for the Eccel Peeper C1 RFID reader."""
|
|
2
|
+
|
|
3
|
+
from .client import PepperC1
|
|
4
|
+
from .commands import Command, KEY_TYPE_AES128, KEY_TYPE_AES192, KEY_TYPE_AES256
|
|
5
|
+
from .commands import KEY_TYPE_DES, KEY_TYPE_2K3DES, KEY_TYPE_3K3DES, KEY_TYPE_MIFARE
|
|
6
|
+
from .exceptions import CommandError, ProtocolError, TransportError
|
|
7
|
+
from .transport import TCPTransport, UARTTransport
|
|
8
|
+
|
|
9
|
+
__version__ = "0.1.0"
|
|
10
|
+
__all__ = [
|
|
11
|
+
"PepperC1",
|
|
12
|
+
"UARTTransport",
|
|
13
|
+
"TCPTransport",
|
|
14
|
+
"Command",
|
|
15
|
+
"CommandError",
|
|
16
|
+
"ProtocolError",
|
|
17
|
+
"TransportError",
|
|
18
|
+
"KEY_TYPE_AES128",
|
|
19
|
+
"KEY_TYPE_AES192",
|
|
20
|
+
"KEY_TYPE_AES256",
|
|
21
|
+
"KEY_TYPE_DES",
|
|
22
|
+
"KEY_TYPE_2K3DES",
|
|
23
|
+
"KEY_TYPE_3K3DES",
|
|
24
|
+
"KEY_TYPE_MIFARE",
|
|
25
|
+
]
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
"""High-level client for the Eccel Peeper C1 RFID reader."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import List
|
|
6
|
+
|
|
7
|
+
from .commands import Command
|
|
8
|
+
from .exceptions import CommandError, ProtocolError
|
|
9
|
+
from .protocol import build_frame
|
|
10
|
+
from .transport import Transport
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class PepperC1:
|
|
14
|
+
"""Driver for the Eccel Peeper C1 RFID reader.
|
|
15
|
+
|
|
16
|
+
Usage::
|
|
17
|
+
|
|
18
|
+
from pepper_c1 import PepperC1, UARTTransport
|
|
19
|
+
with PepperC1(UARTTransport('/dev/ttyUSB0')) as reader:
|
|
20
|
+
print(reader.get_version())
|
|
21
|
+
print(reader.get_uid().hex())
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, transport: Transport, response_timeout: float = 2.0) -> None:
|
|
25
|
+
self._transport = transport
|
|
26
|
+
self._timeout = response_timeout
|
|
27
|
+
self.async_events: List[bytes] = []
|
|
28
|
+
|
|
29
|
+
# ------------------------------------------------------------------
|
|
30
|
+
# Low-level interface
|
|
31
|
+
# ------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
def send_command(self, cmd: Command, data: bytes = b"") -> bytes:
|
|
34
|
+
"""Send a command frame and return the response payload (without CMD_ACK byte).
|
|
35
|
+
|
|
36
|
+
Raises CommandError if the device responds with CMD_ERROR.
|
|
37
|
+
Async events received while waiting are stored in self.async_events.
|
|
38
|
+
"""
|
|
39
|
+
payload = bytes([cmd]) + data
|
|
40
|
+
self._transport.write(build_frame(payload))
|
|
41
|
+
return self._wait_response(cmd)
|
|
42
|
+
|
|
43
|
+
def _wait_response(self, sent_cmd: Command) -> bytes:
|
|
44
|
+
from .protocol import FrameParser
|
|
45
|
+
parser = FrameParser()
|
|
46
|
+
import time
|
|
47
|
+
deadline = time.monotonic() + self._timeout
|
|
48
|
+
buf = b""
|
|
49
|
+
|
|
50
|
+
while time.monotonic() < deadline:
|
|
51
|
+
chunk = self._transport._read_chunk(256)
|
|
52
|
+
if not chunk:
|
|
53
|
+
continue
|
|
54
|
+
completed = parser.feed(chunk)
|
|
55
|
+
for payload in completed:
|
|
56
|
+
if not payload:
|
|
57
|
+
continue
|
|
58
|
+
resp_byte = payload[0]
|
|
59
|
+
if resp_byte == Command.ASYNC:
|
|
60
|
+
self.async_events.append(payload[1:])
|
|
61
|
+
elif resp_byte == Command.ERROR:
|
|
62
|
+
failed_cmd = payload[1] if len(payload) > 1 else 0xFF
|
|
63
|
+
error_code = 0
|
|
64
|
+
if len(payload) >= 4:
|
|
65
|
+
error_code = payload[2] | (payload[3] << 8)
|
|
66
|
+
raise CommandError(failed_cmd, error_code)
|
|
67
|
+
elif resp_byte == Command.ACK:
|
|
68
|
+
return payload[2:] # skip ACK byte and echoed command byte
|
|
69
|
+
raise TimeoutError(f"No response to command 0x{sent_cmd:02X} within {self._timeout}s")
|
|
70
|
+
|
|
71
|
+
# ------------------------------------------------------------------
|
|
72
|
+
# General commands
|
|
73
|
+
# ------------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
def get_version(self) -> str:
|
|
76
|
+
"""Return firmware version string."""
|
|
77
|
+
data = self.send_command(Command.GET_VERSION)
|
|
78
|
+
return data.decode("ascii", errors="replace").rstrip("\x00")
|
|
79
|
+
|
|
80
|
+
def get_tag_count(self) -> int:
|
|
81
|
+
"""Return number of tags in the RF field."""
|
|
82
|
+
data = self.send_command(Command.GET_TAG_COUNT)
|
|
83
|
+
return data[0] if data else 0
|
|
84
|
+
|
|
85
|
+
def get_uid(self, tag_index: int = 0) -> bytes:
|
|
86
|
+
"""Return UID info for tag at tag_index (0-based).
|
|
87
|
+
|
|
88
|
+
Response payload: [type, param, uid_bytes...]
|
|
89
|
+
"""
|
|
90
|
+
return self.send_command(Command.GET_UID, bytes([tag_index]))
|
|
91
|
+
|
|
92
|
+
def activate_tag(self, tag_index: int = 0) -> bytes:
|
|
93
|
+
"""Activate the tag at tag_index; returns response payload."""
|
|
94
|
+
return self.send_command(Command.ACTIVATE_TAG, bytes([tag_index]))
|
|
95
|
+
|
|
96
|
+
def halt(self) -> None:
|
|
97
|
+
"""Halt the active tag."""
|
|
98
|
+
self.send_command(Command.HALT)
|
|
99
|
+
|
|
100
|
+
def set_polling(self, enable: bool, interval_ms: int = 500) -> None:
|
|
101
|
+
"""Enable or disable automatic tag polling.
|
|
102
|
+
|
|
103
|
+
Args:
|
|
104
|
+
enable: True to start polling, False to stop.
|
|
105
|
+
interval_ms: Polling interval in milliseconds (0–65535).
|
|
106
|
+
"""
|
|
107
|
+
data = bytes([1 if enable else 0, interval_ms & 0xFF, (interval_ms >> 8) & 0xFF])
|
|
108
|
+
self.send_command(Command.SET_POLLING, data)
|
|
109
|
+
|
|
110
|
+
def reboot(self) -> None:
|
|
111
|
+
"""Reboot the reader module."""
|
|
112
|
+
self.send_command(Command.REBOOT)
|
|
113
|
+
|
|
114
|
+
# ------------------------------------------------------------------
|
|
115
|
+
# Key management
|
|
116
|
+
# ------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
def set_key(self, key_slot: int, key_type: int, key_data: bytes) -> None:
|
|
119
|
+
"""Store a key in the reader's key slot.
|
|
120
|
+
|
|
121
|
+
Args:
|
|
122
|
+
key_slot: Key slot index.
|
|
123
|
+
key_type: One of the KEY_TYPE_* constants from pepper_c1.commands.
|
|
124
|
+
key_data: Raw key bytes.
|
|
125
|
+
"""
|
|
126
|
+
data = bytes([key_slot, key_type]) + key_data
|
|
127
|
+
self.send_command(Command.SET_KEY, data)
|
|
128
|
+
|
|
129
|
+
def save_keys(self) -> None:
|
|
130
|
+
"""Persist stored keys to non-volatile memory."""
|
|
131
|
+
self.send_command(Command.SAVE_KEYS)
|
|
132
|
+
|
|
133
|
+
# ------------------------------------------------------------------
|
|
134
|
+
# MIFARE Classic
|
|
135
|
+
# ------------------------------------------------------------------
|
|
136
|
+
|
|
137
|
+
def mf_read_block(self, block: int, count: int = 1,
|
|
138
|
+
auth_param: int = 0x0A, key_no: int = 0) -> bytes:
|
|
139
|
+
"""Read one or more 16-byte MIFARE Classic blocks.
|
|
140
|
+
|
|
141
|
+
Args:
|
|
142
|
+
block: First block number.
|
|
143
|
+
count: Number of consecutive blocks to read.
|
|
144
|
+
auth_param: Firmware authentication parameter (empirically 0x0A).
|
|
145
|
+
key_no: Key slot index stored via set_key().
|
|
146
|
+
"""
|
|
147
|
+
payload = bytes([block, count, auth_param, key_no])
|
|
148
|
+
data = self.send_command(Command.MF_READ_BLOCK, payload)
|
|
149
|
+
expected = 16 * count
|
|
150
|
+
if len(data) != expected:
|
|
151
|
+
raise ProtocolError(f"Expected {expected} bytes for MF block(s), got {len(data)}")
|
|
152
|
+
return data
|
|
153
|
+
|
|
154
|
+
def mf_write_block(self, block: int, data: bytes,
|
|
155
|
+
count: int = 1, auth_param: int = 0x0A, key_no: int = 0) -> None:
|
|
156
|
+
"""Write one or more 16-byte MIFARE Classic blocks.
|
|
157
|
+
|
|
158
|
+
Args:
|
|
159
|
+
block: First block number.
|
|
160
|
+
data: Raw bytes; must be exactly count*16 bytes.
|
|
161
|
+
count: Number of consecutive blocks to write.
|
|
162
|
+
auth_param: Firmware authentication parameter (empirically 0x0A).
|
|
163
|
+
key_no: Key slot index stored via set_key().
|
|
164
|
+
"""
|
|
165
|
+
if len(data) != 16 * count:
|
|
166
|
+
raise ValueError(f"MIFARE Classic write expects {16 * count} bytes, got {len(data)}")
|
|
167
|
+
payload = bytes([block, count, auth_param, key_no]) + data
|
|
168
|
+
self.send_command(Command.MF_WRITE_BLOCK, payload)
|
|
169
|
+
|
|
170
|
+
def mf_read_value(self, block: int, auth_param: int = 0x0A, key_no: int = 0) -> int:
|
|
171
|
+
"""Read a MIFARE Classic value block; returns a signed 32-bit integer."""
|
|
172
|
+
data = self.send_command(Command.MF_READ_VALUE, bytes([block, auth_param, key_no]))
|
|
173
|
+
return int.from_bytes(data[:4], "little", signed=True)
|
|
174
|
+
|
|
175
|
+
def mf_write_value(self, block: int, value: int,
|
|
176
|
+
auth_param: int = 0x0A, key_no: int = 0) -> None:
|
|
177
|
+
"""Write a MIFARE Classic value block."""
|
|
178
|
+
payload = bytes([block, auth_param, key_no]) + value.to_bytes(4, "little", signed=True)
|
|
179
|
+
self.send_command(Command.MF_WRITE_VALUE, payload)
|
|
180
|
+
|
|
181
|
+
def mf_increment(self, block: int, delta: int,
|
|
182
|
+
auth_param: int = 0x0A, key_no: int = 0) -> None:
|
|
183
|
+
"""Increment a MIFARE Classic value block."""
|
|
184
|
+
data = bytes([block, auth_param, key_no]) + delta.to_bytes(4, "little", signed=False)
|
|
185
|
+
self.send_command(Command.MF_INCREMENT, data)
|
|
186
|
+
|
|
187
|
+
# ------------------------------------------------------------------
|
|
188
|
+
# MIFARE Ultralight
|
|
189
|
+
# ------------------------------------------------------------------
|
|
190
|
+
|
|
191
|
+
def mfu_read_page(self, page: int, count: int = 1) -> bytes:
|
|
192
|
+
"""Read one or more 4-byte MIFARE Ultralight pages."""
|
|
193
|
+
data = self.send_command(Command.MFU_READ_PAGE, bytes([page, count]))
|
|
194
|
+
return data[:4 * count]
|
|
195
|
+
|
|
196
|
+
def mfu_write_page(self, page: int, data: bytes, count: int = 1) -> None:
|
|
197
|
+
"""Write one or more 4-byte MIFARE Ultralight pages."""
|
|
198
|
+
if len(data) != 4 * count:
|
|
199
|
+
raise ValueError(f"Ultralight write expects {4 * count} bytes, got {len(data)}")
|
|
200
|
+
self.send_command(Command.MFU_WRITE_PAGE, bytes([page, count]) + data)
|
|
201
|
+
|
|
202
|
+
def mfu_read_counter(self, counter: int) -> int:
|
|
203
|
+
"""Read a 3-byte NFC counter value."""
|
|
204
|
+
data = self.send_command(Command.MFU_READ_COUNTER, bytes([counter]))
|
|
205
|
+
return int.from_bytes(data[:3], "little")
|
|
206
|
+
|
|
207
|
+
def mfu_increment_counter(self, counter: int, delta: int) -> None:
|
|
208
|
+
"""Increment an NFC counter."""
|
|
209
|
+
data = bytes([counter]) + delta.to_bytes(3, "little")
|
|
210
|
+
self.send_command(Command.MFU_INCREMENT_COUNTER, data)
|
|
211
|
+
|
|
212
|
+
def mfu_passwd_auth(self, password: bytes, pack: bytes = b"\x00\x00") -> bytes:
|
|
213
|
+
"""Authenticate with a 4-byte password; returns 2-byte PACK."""
|
|
214
|
+
if len(password) != 4:
|
|
215
|
+
raise ValueError("Password must be 4 bytes")
|
|
216
|
+
return self.send_command(Command.MFU_PASSWD_AUTH, password + pack)
|
|
217
|
+
|
|
218
|
+
# ------------------------------------------------------------------
|
|
219
|
+
# MIFARE DESFire
|
|
220
|
+
# ------------------------------------------------------------------
|
|
221
|
+
|
|
222
|
+
def mfdf_select_app(self, aid: bytes) -> None:
|
|
223
|
+
"""Select a DESFire application by its 3-byte AID."""
|
|
224
|
+
if len(aid) != 3:
|
|
225
|
+
raise ValueError("AID must be 3 bytes")
|
|
226
|
+
self.send_command(Command.MFDF_SELECT_APP, aid)
|
|
227
|
+
|
|
228
|
+
def mfdf_get_app_ids(self) -> list[bytes]:
|
|
229
|
+
"""Return list of 3-byte AIDs present on the card."""
|
|
230
|
+
data = self.send_command(Command.MFDF_APP_IDS)
|
|
231
|
+
return [data[i:i + 3] for i in range(0, len(data), 3)]
|
|
232
|
+
|
|
233
|
+
def mfdf_auth(self, key_no: int, key_slot: int = 0) -> None:
|
|
234
|
+
"""Authenticate with a DESFire key (3DES/DES) stored in reader key slot."""
|
|
235
|
+
self.send_command(Command.MFDF_AUTH, bytes([key_no, key_slot, 0]))
|
|
236
|
+
|
|
237
|
+
def mfdf_auth_aes(self, key_no: int, key_slot: int = 0) -> None:
|
|
238
|
+
"""Authenticate with a DESFire AES key stored in reader key slot."""
|
|
239
|
+
self.send_command(Command.MFDF_AUTH_AES, bytes([key_no, key_slot, 0]))
|
|
240
|
+
|
|
241
|
+
def mfdf_read_data(self, file_no: int, offset: int, length: int) -> bytes:
|
|
242
|
+
"""Read data from a DESFire standard/backup data file."""
|
|
243
|
+
data = (
|
|
244
|
+
bytes([file_no])
|
|
245
|
+
+ offset.to_bytes(3, "little")
|
|
246
|
+
+ length.to_bytes(3, "little")
|
|
247
|
+
)
|
|
248
|
+
return self.send_command(Command.MFDF_READ_DATA, data)
|
|
249
|
+
|
|
250
|
+
def mfdf_write_data(self, file_no: int, offset: int, data: bytes) -> None:
|
|
251
|
+
"""Write data to a DESFire standard/backup data file."""
|
|
252
|
+
header = (
|
|
253
|
+
bytes([file_no])
|
|
254
|
+
+ offset.to_bytes(3, "little")
|
|
255
|
+
+ len(data).to_bytes(3, "little")
|
|
256
|
+
)
|
|
257
|
+
self.send_command(Command.MFDF_WRITE_DATA, header + data)
|
|
258
|
+
|
|
259
|
+
def mfdf_get_value(self, file_no: int) -> int:
|
|
260
|
+
"""Read a DESFire value file; returns a signed 32-bit integer."""
|
|
261
|
+
data = self.send_command(Command.MFDF_GET_VALUE, bytes([file_no]))
|
|
262
|
+
return int.from_bytes(data[:4], "little", signed=True)
|
|
263
|
+
|
|
264
|
+
def mfdf_commit_transaction(self) -> None:
|
|
265
|
+
"""Commit the current DESFire transaction."""
|
|
266
|
+
self.send_command(Command.MFDF_COMMIT_TRANSACTION)
|
|
267
|
+
|
|
268
|
+
def mfdf_abort_transaction(self) -> None:
|
|
269
|
+
"""Abort the current DESFire transaction."""
|
|
270
|
+
self.send_command(Command.MFDF_ABORT_TRANSACTION)
|
|
271
|
+
|
|
272
|
+
# ------------------------------------------------------------------
|
|
273
|
+
# ICODE
|
|
274
|
+
# ------------------------------------------------------------------
|
|
275
|
+
|
|
276
|
+
def icode_inventory_start(self) -> bytes:
|
|
277
|
+
"""Start ICODE inventory; returns UID of first tag found."""
|
|
278
|
+
return self.send_command(Command.ICODE_INVENTORY_START)
|
|
279
|
+
|
|
280
|
+
def icode_read_block(self, block: int, count: int = 1) -> bytes:
|
|
281
|
+
"""Read one or more ICODE blocks (4 bytes each)."""
|
|
282
|
+
return self.send_command(Command.ICODE_READ_BLOCK, bytes([block, count]))
|
|
283
|
+
|
|
284
|
+
def icode_write_block(self, block: int, data: bytes, count: int = 1) -> None:
|
|
285
|
+
"""Write one or more 4-byte ICODE blocks."""
|
|
286
|
+
if len(data) != 4 * count:
|
|
287
|
+
raise ValueError(f"ICODE write expects {4 * count} bytes, got {len(data)}")
|
|
288
|
+
self.send_command(Command.ICODE_WRITE_BLOCK, bytes([block, count]) + data)
|
|
289
|
+
|
|
290
|
+
def icode_get_system_information(self) -> bytes:
|
|
291
|
+
"""Return ICODE system information."""
|
|
292
|
+
return self.send_command(Command.ICODE_GET_SYSTEM_INFORMATION)
|
|
293
|
+
|
|
294
|
+
# ------------------------------------------------------------------
|
|
295
|
+
# Context manager
|
|
296
|
+
# ------------------------------------------------------------------
|
|
297
|
+
|
|
298
|
+
def __enter__(self) -> "PepperC1":
|
|
299
|
+
return self
|
|
300
|
+
|
|
301
|
+
def __exit__(self, *_: object) -> None:
|
|
302
|
+
self._transport.close()
|
|
303
|
+
|
|
304
|
+
def close(self) -> None:
|
|
305
|
+
"""Close the underlying transport."""
|
|
306
|
+
self._transport.close()
|