mifare.h

00001 /*-
00002  * Public platform independent Near Field Communication (NFC) library examples
00003  * 
00004  * Copyright (C) 2009, Roel Verdult
00005  * Copyright (C) 2010, Romuald Conty, Romain Tartière
00006  * 
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *  1) Redistributions of source code must retain the above copyright notice,
00010  *  this list of conditions and the following disclaimer. 
00011  *  2 )Redistributions in binary form must reproduce the above copyright
00012  *  notice, this list of conditions and the following disclaimer in the
00013  *  documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
00026  * 
00027  * Note that this license only applies on the examples, NFC library itself is under LGPL
00028  *
00029  */
00030 
00036 #ifndef _LIBNFC_MIFARE_H_
00037 #  define _LIBNFC_MIFARE_H_
00038 
00039 #  include <nfc/nfc-types.h>
00040 
00041 // Compiler directive, set struct alignment to 1 byte_t for compatibility
00042 #  pragma pack(1)
00043 
00044 typedef enum {
00045   MC_AUTH_A = 0x60,
00046   MC_AUTH_B = 0x61,
00047   MC_READ = 0x30,
00048   MC_WRITE = 0xA0,
00049   MC_TRANSFER = 0xB0,
00050   MC_DECREMENT = 0xC0,
00051   MC_INCREMENT = 0xC1,
00052   MC_STORE = 0xC2
00053 } mifare_cmd;
00054 
00055 // MIFARE command params
00056 typedef struct {
00057   byte_t  abtKey[6];
00058   byte_t  abtUid[4];
00059 } mifare_param_auth;
00060 
00061 typedef struct {
00062   byte_t  abtData[16];
00063 } mifare_param_data;
00064 
00065 typedef struct {
00066   byte_t  abtValue[4];
00067 } mifare_param_value;
00068 
00069 typedef union {
00070   mifare_param_auth mpa;
00071   mifare_param_data mpd;
00072   mifare_param_value mpv;
00073 } mifare_param;
00074 
00075 // Reset struct alignment to default
00076 #  pragma pack()
00077 
00078 bool    nfc_initiator_mifare_cmd (nfc_device_t * pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param * pmp);
00079 
00080 // Compiler directive, set struct alignment to 1 byte_t for compatibility
00081 #  pragma pack(1)
00082 
00083 // MIFARE Classic
00084 typedef struct {
00085   byte_t  abtUID[4];
00086   byte_t  btBCC;
00087   byte_t  btUnknown;
00088   byte_t  abtATQA[2];
00089   byte_t  abtUnknown[8];
00090 } mifare_classic_block_manufacturer;
00091 
00092 typedef struct {
00093   byte_t  abtData[16];
00094 } mifare_classic_block_data;
00095 
00096 typedef struct {
00097   byte_t  abtKeyA[6];
00098   byte_t  abtAccessBits[4];
00099   byte_t  abtKeyB[6];
00100 } mifare_classic_block_trailer;
00101 
00102 typedef union {
00103   mifare_classic_block_manufacturer mbm;
00104   mifare_classic_block_data mbd;
00105   mifare_classic_block_trailer mbt;
00106 } mifare_classic_block;
00107 
00108 typedef struct {
00109   mifare_classic_block amb[256];
00110 } mifare_classic_tag;
00111 
00112 // MIFARE Ultralight
00113 typedef struct {
00114   byte_t  sn0[3];
00115   byte_t  btBCC0;
00116   byte_t  sn1[4];
00117   byte_t  btBCC1;
00118   byte_t  internal;
00119   byte_t  lock[2];
00120   byte_t  otp[4];
00121 } mifareul_block_manufacturer;
00122 
00123 typedef struct {
00124   byte_t  abtData[16];
00125 } mifareul_block_data;
00126 
00127 typedef union {
00128   mifareul_block_manufacturer mbm;
00129   mifareul_block_data mbd;
00130 } mifareul_block;
00131 
00132 typedef struct {
00133   mifareul_block amb[4];
00134 } mifareul_tag;
00135 
00136 // Reset struct alignment to default
00137 #  pragma pack()
00138 
00139 #endif // _LIBNFC_MIFARE_H_