# Dingtian QR code / RFID reader – configuration protocol Reverse engineered from `QRCode_v1_6_5.exe` (Dingtian-Tech QRCode V1.6.5, statically linked Qt 5 Win32 application) by static analysis of the binary plus packet captures against a real reader (serial HC002721, model `doorqrcode`, firmware `ver_client1_1.1`, chip `v3s`, hardware `h42`) and a fake reader used to exercise the Set buttons. `dingtian_qr.py` in this folder is a working, dependency-free Python 3 implementation (discover / info / get / set / set-network) that runs on Linux. ## 1. What the exe does The exe has two tabs. * **Setting** – the configuration tool. It talks to readers over **UDP**: * `Search device` broadcasts command 2 to `:24582` on every local IPv4 interface. Readers answer with command 3 (96 byte DeviceInfo block). Discovered readers are kept in an in-memory list (one 768 byte record per reader) and shown by serial number. * `Get parameter` sends commands 0 and 30 unicast to `:24680` and fills the form from the 64 byte firmware block and the 720 byte parameter struct. * The three `Set` buttons send a fixed sequence of small commands (below). Each write is acknowledged by the reader with `cmd+1` and a 1 byte status (0 = OK); the exe prints "... success!" when the acknowledgement arrives. * The form shows the *stored record* of the selected reader. A new record is filled with built-in defaults until a Get parameter reply has been received, which is the behaviour you see as "the UI does not show the selected device params". Reading the parameters directly (command 30) every time avoids this. * **Test device** – a tiny HTTP (or raw TCP / UDP) server on a configurable port (default 80) that receives the scan pushes the reader makes in HTTP output mode and answers with the JSON documented in the SDK (`qrcode_HTTP_GET.txt`). The "TCP" and "UDP" listener types of that tab were not analysed. The HTTP push is: `GET /qa/mcardsea.php?cardid=&mjihao=&cjihao=&status=<2 digits>&time=` and the reply is `{"data":[{"cardid":"...","cjihao":"...","mjihao":"...","status":1,"time":,"output":0}],"code":0,"message":"success"}` (status 1 = valid, beep twice; 0 = invalid, beep once; `time` syncs the reader clock; `output` 0 access, 1 WG26, 2 WG34). This part is independent of the UDP protocol. The reader's default IP is 192.168.1.99 (see `readme.txt`). A reset hole on the back restores factory settings. ## 2. Frame format (UDP, both directions) ``` offset size field notes 0 4 'XXXX' magic (0x58 0x58 0x58 0x58) 4 4 id little-endian u32; PC sends 0, reader echoes an arbitrary value 8 4 cmd little-endian u32; even = request/write, odd = reply (cmd + 1) 12 4 len little-endian u32; payload length in bytes 16 1 checksum (0xFF - sum(payload bytes)) & 0xFF -> 0xFF when len == 0 17 len payload ``` All integers are little-endian. The exe sends the header and the payload as **two separate datagrams** (17 byte header, then the raw payload). Reader replies arrive as a single datagram (header + payload). The Python tool mimics the two-datagram write. Ports: reader listens on **24680/udp** (unicast) and **24582/udp** (broadcast, used for search and set-network). Replies are sent to the IP:port the request came from, but the reader's *source* port is not fixed: unicast replies came from 24680, search replies came from an ephemeral port (52512 in the capture). A host firewall therefore has to allow inbound UDP to the tool's socket (or track UDP "related" traffic); filtering on the reader's source port does not work. Broadcast address: the exe computes `ip | ~netmask` for every local IPv4 interface and sends the search to each one (it does not use 255.255.255.255). Readers and PC must be on the same layer 2 segment for search and set-network; get/set commands are plain unicast and are routable. The exe also binds its own socket to 24582 so it can receive readers that announce themselves; two copies of the exe on one PC therefore fight over that port. ### Worked examples Search request, no payload (checksum of nothing = 0xFF): ``` 58 58 58 58 00 00 00 00 02 00 00 00 00 00 00 00 ff magic id = 0 cmd = 2 len = 0 checksum ``` Set buzzer on (command 18, payload = u32 1): payload bytes sum to 1, checksum = 0xFF - 1 = 0xFE. Sent as two datagrams: ``` datagram 1: 58 58 58 58 00 00 00 00 12 00 00 00 04 00 00 00 fe datagram 2: 01 00 00 00 ``` Reader acknowledgement (cmd 19 = 18 + 1, one status byte 0x00, checksum 0xFF - 0 = 0xFF): ``` 58 58 58 58 00 00 00 00 13 00 00 00 01 00 00 00 ff 00 ``` Only status 0 has been observed; treat anything else as an error. Set card prefix "AB" with ASCII type (command 44, 72 byte payload): ``` 01 00 00 00 card output format = 1 (ASCII) 01 00 00 00 card prefix/suffix type = 1 (ASCII) 41 42 00 .. 00 prefix "AB", 32 bytes NUL padded 00 00 .. 00 suffix empty, 32 bytes ``` Python reference: `checksum = (0xFF - sum(payload)) & 0xFF`; `header = b'XXXX' + struct.pack('