9455b51c |
1 | //----------------------------------------------------------------------------- |
2 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, |
3 | // at your option, any later version. See the LICENSE.txt file for the text of |
4 | // the license. |
5 | //----------------------------------------------------------------------------- |
6 | // ISO15693 CRC & other commons |
7 | //----------------------------------------------------------------------------- |
8 | |
9 | |
be09ea86 |
10 | #include "iso15693tools.h" |
11 | |
cd028159 |
12 | #include <stddef.h> |
9455b51c |
13 | #include <stdint.h> |
61aaee35 |
14 | #ifdef ON_DEVICE |
15 | #include "printf.h" |
16 | #else |
17 | #include <stdio.h> |
18 | #endif |
19 | |
9455b51c |
20 | |
be09ea86 |
21 | #define ISO15693_CRC_PRESET (uint16_t)0xFFFF |
22 | #define ISO15693_CRC_POLY (uint16_t)0x8408 |
c3963755 |
23 | |
24 | |
9455b51c |
25 | // The CRC as described in ISO 15693-Part 3-Annex C |
be09ea86 |
26 | // v buffer with data |
27 | // n length |
28 | // returns crc as 16bit value |
29 | uint16_t Iso15693Crc(uint8_t *v, int n) { |
9455b51c |
30 | uint32_t reg; |
31 | int i, j; |
32 | |
be09ea86 |
33 | reg = ISO15693_CRC_PRESET; |
34 | for (i = 0; i < n; i++) { |
9455b51c |
35 | reg = reg ^ ((uint32_t)v[i]); |
36 | for (j = 0; j < 8; j++) { |
37 | if (reg & 0x0001) { |
be09ea86 |
38 | reg = (reg >> 1) ^ ISO15693_CRC_POLY; |
9455b51c |
39 | } else { |
40 | reg = (reg >> 1); |
41 | } |
42 | } |
43 | } |
44 | |
45 | return ~(uint16_t)(reg & 0xffff); |
46 | } |
47 | |
48 | // adds a CRC to a dataframe |
be09ea86 |
49 | // req[] iso15963 frame without crc |
50 | // n length without crc |
9455b51c |
51 | // returns the new length of the dataframe. |
52 | int Iso15693AddCrc(uint8_t *req, int n) { |
be09ea86 |
53 | uint16_t crc = Iso15693Crc(req, n); |
9455b51c |
54 | req[n] = crc & 0xff; |
55 | req[n+1] = crc >> 8; |
be09ea86 |
56 | return n + 2; |
9455b51c |
57 | } |
58 | |
3851172d |
59 | |
9455b51c |
60 | // returns a string representation of the UID |
61 | // UID is transmitted and stored LSB first, displayed MSB first |
be09ea86 |
62 | // target char* buffer, where to put the UID, if NULL a static buffer is returned |
63 | // uid[] the UID in transmission order |
64 | // return: ptr to string |
65 | char* Iso15693sprintUID(char *target, uint8_t *uid) { |
66 | static char tempbuf[2*8+1] = ""; |
67 | if (target == NULL) target = tempbuf; |
68 | sprintf(target, "%02X%02X%02X%02X%02X%02X%02X%02X", uid[7], uid[6], uid[5], uid[4], uid[3], uid[2], uid[1], uid[0]); |
69 | return target; |
9455b51c |
70 | } |
71 | |
be09ea86 |
72 | |
28ae37b7 |
73 | uint16_t iclass_crc16(uint8_t *data_p, unsigned short length) { |
be09ea86 |
74 | unsigned char i; |
75 | unsigned int data; |
76 | uint16_t crc = ISO15693_CRC_PRESET; |
77 | |
78 | if (length == 0) |
79 | return (~crc); |
80 | |
81 | do { |
28ae37b7 |
82 | for (i = 0, data = 0xff & *data_p++; i < 8; i++, data >>= 1) { |
be09ea86 |
83 | if ((crc & 0x0001) ^ (data & 0x0001)) |
84 | crc = (crc >> 1) ^ ISO15693_CRC_POLY; |
85 | else crc >>= 1; |
86 | } |
87 | } while (--length); |
88 | |
89 | crc = ~crc; |
90 | data = crc; |
91 | crc = (crc << 8) | (data >> 8 & 0xff); |
cd028159 |
92 | crc = crc ^ 0x0BC3; |
be09ea86 |
93 | return (crc); |
c3963755 |
94 | } |
9455b51c |
95 | |