1 /*************************************************************************/
3 /* Mifare support for accessing RFID cards with Infratec RFID reader */
5 /* Copyright (C) 2007 Milosch Meriac <meriac@bitmanufaktur.de> */
7 /* Redistribution and use in source and binary forms, with or without */
8 /* modification, are permitted provided that the following conditions are*/
11 /* Redistributions of source code must retain the above copyright notice,*/
12 /* this list of conditions and the following disclaimer. */
13 /* Redistributions in binary form must reproduce the above copyright */
14 /* notice, this list of conditions and the following disclaimer in the */
15 /* documentation and/or other materials provided with the distribution. */
17 /* The name of the author may not be used to endorse or promote products */
18 /* derived from this software without specific prior written permission. */
20 /* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR */
21 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
22 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE*/
23 /* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, */
24 /* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
25 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR */
26 /* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) */
27 /* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, */
28 /* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING */
29 /* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
30 /* POSSIBILITY OF SUCH DAMAGE. */
32 /*************************************************************************/
41 #define EXPORT_CONVENTION __stdcall
44 #define EXPORT __declspec(dllexport)
47 #define EXPORT __declspec(dllimport)
50 #define PCDERROR_NONE 0
51 #define PCDERROR_INVALID_PARAMETER -1
52 #define PCDERROR_KEY_FORMAT -2
53 #define PCDERROR_KEY_AUTH -3
54 #define PCDERROR_NO_CARD_FOUND -4
55 #define PCDERROR_LAYER2_INIT -5
56 #define PCDERROR_LAYER2_OPEN -6
57 #define PCDERROR_LAYER3_INIT -7
58 #define PCDERROR_LAYER3_OPEN -8
59 #define PCDERROR_SELECT -9
60 #define PCDERROR_READ_FAILED -10
61 #define PCDERROR_WRITE_FAILED -11
62 #define PCDERROR_CLOSED -12
63 #define PCDERROR_NO_READER -13
64 #define PCDERROR_OUT_OF_MEMORY -14
65 #define PCDERROR_READER_VERSION -15
67 #define PCDAUTH_KEY_LENGTH 6
68 #define PCDAUTH_KEYID_1A 0
69 #define PCDAUTH_KEYID_1B 1
71 typedef void* MIFARE_HANDLE;
73 /*************************************************************************/
75 /* Six steps for reading/writing to MIFARE cards */
77 /*************************************************************************/
79 /* Step 1. open reader
81 supply the address of your handle variable to retrieve a handle
82 to the current reader.
84 EXPORT int EXPORT_CONVENTION openpcd_open_reader(MIFARE_HANDLE *handle);
86 /* Step 2. set MIFARE classic key
88 if your key differs from the default Infineon key (6*0xFF), you can
89 supply a different key here. The key size is PCDAUTH_KEY_LENGTH bytes.
90 You can chose to set key_id to PCDAUTH_KEYID_1A or *_1B.
92 EXPORT int EXPORT_CONVENTION openpcd_set_key(MIFARE_HANDLE handle,unsigned int key_id,const void* key);
94 /* Step 3. select card
96 start the anticollosion to select a card in the reader field - retry if
97 it fails. Currently supports only on card in the readerv field.
99 EXPORT int EXPORT_CONVENTION openpcd_select_card(MIFARE_HANDLE handle);
101 /* Step 4. read/write card
103 read, write from the selected card - specify the page and supply up to
106 EXPORT int EXPORT_CONVENTION openpcd_read(MIFARE_HANDLE handle,int page, void* data, int len);
107 EXPORT int EXPORT_CONVENTION openpcd_write(MIFARE_HANDLE handle,int page,const void *data,int len);
109 /* Step 5. deselect card when done
111 EXPORT int EXPORT_CONVENTION openpcd_deselect_card(MIFARE_HANDLE handle);
113 /* Step 6. close reader after deselected card
115 EXPORT int EXPORT_CONVENTION openpcd_close_reader(MIFARE_HANDLE handle);
118 /*************************************************************************/
120 /* Support functions */
122 /*************************************************************************/
124 /* openpcd_get_error_text:
126 Used for converting the error code into a string
128 EXPORT char* EXPORT_CONVENTION openpcd_get_error_text(int error);
131 /* openpcd_get_card_id:
133 Get the card id of a selected RFID card
135 EXPORT int EXPORT_CONVENTION openpcd_get_card_id(MIFARE_HANDLE handle,unsigned int *uid);
137 /* openpcd_get_api_version:
139 Get the USB api version of the reader
141 EXPORT int EXPORT_CONVENTION openpcd_get_api_version(MIFARE_HANDLE handle,unsigned int *version);
143 /* openpcd_reset_reader:
145 Reset the attached reader
147 EXPORT int EXPORT_CONVENTION openpcd_reset_reader(MIFARE_HANDLE handle);
149 /* openpcd_get_environment
151 Store the given data to the nonvolatile reader flash
152 Returns read data count at index or error code
154 EXPORT int EXPORT_CONVENTION openpcd_get_environment(
155 MIFARE_HANDLE handle,
160 /* openpcd_set_environment
162 Read data from nonvolatile reader flash
163 Returns written data count at index or error code
165 EXPORT int EXPORT_CONVENTION openpcd_set_environment(
166 MIFARE_HANDLE handle,
168 const unsigned char* data
173 #endif/*__cplusplus*/
174 #endif/*__OPENPCD_H__*/