1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef PHNFCTYPES_H
18 #define PHNFCTYPES_H
19 
20 #include <stdint.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 
26 #ifndef TRUE
27 #define TRUE            (0x01)            /* Logical True Value */
28 #endif
29 #ifndef FALSE
30 #define FALSE           (0x00)            /* Logical False Value */
31 #endif
32 typedef uint8_t         utf8_t;       /* UTF8 Character String */
33 typedef uint8_t         bool_t;       /* boolean data type */
34 typedef uint16_t        NFCSTATUS;        /* Return values */
35 #define STATIC static
36 
37 #define PHNFC_MAX_UID_LENGTH            0x0AU  /* Maximum UID length expected */
38 #define PHNFC_MAX_ATR_LENGTH            0x30U  /* Maximum ATR_RES (General Bytes) length expected */
39 #define PHNFC_NFCID_LENGTH              0x0AU  /* Maximum length of NFCID 1.3*/
40 #define PHNFC_ATQA_LENGTH               0x02U  /* ATQA length */
41 
42 /*
43  * NFC Data structure
44  */
45 typedef struct phNfc_sData
46 {
47     uint8_t             *buffer; /* Buffer to store data */
48     uint32_t            length;  /* Buffer length */
49 } phNfc_sData_t;
50 
51 /*
52  * Possible Hardware Configuration exposed to upper layer.
53  * Typically this should be port name (Ex:"COM1","COM2") to which PN547 is connected.
54  */
55 typedef enum
56 {
57     ENUM_LINK_TYPE_COM1,
58     ENUM_LINK_TYPE_COM2,
59     ENUM_LINK_TYPE_COM3,
60     ENUM_LINK_TYPE_COM4,
61     ENUM_LINK_TYPE_COM5,
62     ENUM_LINK_TYPE_COM6,
63     ENUM_LINK_TYPE_COM7,
64     ENUM_LINK_TYPE_COM8,
65     ENUM_LINK_TYPE_I2C,
66     ENUM_LINK_TYPE_SPI,
67     ENUM_LINK_TYPE_USB,
68     ENUM_LINK_TYPE_TCP,
69     ENUM_LINK_TYPE_NB
70 } phLibNfc_eConfigLinkType;
71 
72 /*
73  * Deferred message. This message type will be posted to the client application thread
74  * to notify that a deferred call must be invoked.
75  */
76 #define PH_LIBNFC_DEFERREDCALL_MSG        (0x311)
77 
78 /*
79  * Deferred call declaration.
80  * This type of API is called from ClientApplication ( main thread) to notify
81  * specific callback.
82  */
83 typedef void (*pphLibNfc_DeferredCallback_t) (void*);
84 
85 /*
86  * Deferred parameter declaration.
87  * This type of data is passed as parameter from ClientApplication (main thread) to the
88  * callback.
89  */
90 typedef void *pphLibNfc_DeferredParameter_t;
91 
92 /*
93  * Possible Hardware Configuration exposed to upper layer.
94  * Typically this should be at least the communication link (Ex:"COM1","COM2")
95  * the controller is connected to.
96  */
97 typedef struct phLibNfc_sConfig
98 {
99     uint8_t *pLogFile; /* Log File Name*/
100     /* Hardware communication link to the controller */
101     phLibNfc_eConfigLinkType  nLinkType;
102     /* The client ID (thread ID or message queue ID) */
103     unsigned int nClientId;
104 } phLibNfc_sConfig_t, *pphLibNfc_sConfig_t;
105 
106 /*
107  * NFC Message structure contains message specific details like
108  * message type, message specific data block details, etc.
109  */
110 typedef struct phLibNfc_Message
111 {
112     uint32_t eMsgType;   /* Type of the message to be posted*/
113     void   * pMsgData;   /* Pointer to message specific data block in case any*/
114     uint32_t Size;       /* Size of the datablock*/
115 } phLibNfc_Message_t, *pphLibNfc_Message_t;
116 
117 /*
118  * Deferred message specific info declaration.
119  * This type of information is packed as message data when PH_LIBNFC_DEFERREDCALL_MSG
120  * type message is posted to message handler thread.
121  */
122 typedef struct phLibNfc_DeferredCall
123 {
124     pphLibNfc_DeferredCallback_t pCallback;/* pointer to Deferred callback */
125     pphLibNfc_DeferredParameter_t pParameter;/* pointer to Deferred parameter */
126 } phLibNfc_DeferredCall_t;
127 
128 /*
129  * Definitions for supported protocol
130  */
131 typedef struct phNfc_sSupProtocol
132 {
133     unsigned int MifareUL    : 1;  /* Protocol Mifare Ultra Light or any NFC Forum Type-2 tags */
134     unsigned int MifareStd   : 1;  /* Protocol Mifare Standard. */
135     unsigned int ISO14443_4A : 1;  /* Protocol ISO14443-4 Type A.  */
136     unsigned int ISO14443_4B : 1;  /* Protocol ISO14443-4 Type B.  */
137     unsigned int ISO15693    : 1;  /* Protocol ISO15693 HiTag.  */
138     unsigned int Felica      : 1;  /* Protocol Felica. */
139     unsigned int NFC         : 1;  /* Protocol NFC. */
140     unsigned int Jewel       : 1;  /* Protocol Innovision Jewel Tag. or Any T1T*/
141     unsigned int Desfire     : 1;  /*TRUE indicates specified feature (mapping
142                                    or formatting)for DESFire tag supported else not supported.*/
143     unsigned int Kovio       : 1;   /* Protocol Kovio Tag*/
144     unsigned int HID         : 1;   /* Protocol HID(Picopass) Tag*/
145     unsigned int Bprime      : 1;   /* Protocol BPrime Tag*/
146     unsigned int EPCGEN2     : 1;   /* Protocol EPCGEN2 Tag*/
147 } phNfc_sSupProtocol_t;
148 
149 /*
150  *  Enumerated MIFARE Commands
151  */
152 
153 typedef enum phNfc_eMifareCmdList
154 {
155     phNfc_eMifareRaw        = 0x00U,     /* This command performs raw transcations */
156     phNfc_eMifareAuthentA   = 0x60U,     /* This command performs an authentication with KEY A for a sector. */
157     phNfc_eMifareAuthentB   = 0x61U,     /* This command performs an authentication with KEY B for a sector. */
158     phNfc_eMifareRead16     = 0x30U,     /* Read 16 Bytes from a Mifare Standard block */
159     phNfc_eMifareRead       = 0x30U,     /* Read Mifare Standard */
160     phNfc_eMifareWrite16    = 0xA0U,     /* Write 16 Bytes to a Mifare Standard block */
161     phNfc_eMifareWrite4     = 0xA2U,     /* Write 4 bytes. */
162     phNfc_eMifareInc        = 0xC1U,     /* Increment */
163     phNfc_eMifareDec        = 0xC0U,     /* Decrement */
164     phNfc_eMifareTransfer   = 0xB0U,     /* Transfer */
165     phNfc_eMifareRestore    = 0xC2U,     /* Restore.   */
166     phNfc_eMifareReadSector = 0x38U,     /* Read Sector.   */
167     phNfc_eMifareWriteSector= 0xA8U,     /* Write Sector.   */
168     /* Above commands could be used for preparing raw command but below one can not be */
169     phNfc_eMifareReadN      = 0x01,      /* Proprietary Command */
170     phNfc_eMifareWriteN     = 0x02,      /* Proprietary Command */
171     phNfc_eMifareSectorSel  = 0x03,      /* Proprietary Command */
172     phNfc_eMifareAuth       = 0x04,      /* Proprietary Command */
173     phNfc_eMifareProxCheck  = 0x05,      /* Proprietary Command */
174     phNfc_eMifareInvalidCmd = 0xFFU      /* Invalid Command */
175 } phNfc_eMifareCmdList_t;
176 
177 /*
178  * Information about ISO14443A
179  */
180 typedef struct phNfc_sIso14443AInfo
181 {
182     uint8_t         Uid[PHNFC_MAX_UID_LENGTH];      /* UID information of the TYPE A
183                                                      * Tag Discovered */
184     uint8_t         UidLength;                      /* UID information length */
185     uint8_t         AppData[PHNFC_MAX_ATR_LENGTH];  /* Application data information of the
186                               1                      * tag discovered (= Historical bytes for
187                                                      * type A) */
188     uint8_t         AppDataLength;                  /* Application data length */
189     uint8_t         Sak;                            /* SAK information of the TYPE A
190                                                      * Tag Discovered */
191     uint8_t         AtqA[PHNFC_ATQA_LENGTH];        /* ATQA informationof the TYPE A
192                                                      * Tag Discovered */
193     uint8_t         MaxDataRate;                    /* Maximum data rate supported
194                                                      * by the tag Discovered */
195     uint8_t         Fwi_Sfgt;                       /* Frame waiting time and start up
196                                                      * frame guard */
197 } phNfc_sIso14443AInfo_t;
198 
199 /* Remote device information structure */
200 typedef union phNfc_uRemoteDevInfo
201 {
202     phNfc_sIso14443AInfo_t Iso14443A_Info;/* ISO1443A Remote device info */
203 } phNfc_uRemoteDevInfo_t;
204 
205 /*
206 *
207 *  The RF Device Type List is used to identify the type of
208 *  remote device that is discovered and connected.
209 *
210 */
211 
212 typedef enum phNfc_eRFDevType
213 {
214     phNfc_eUnknown_DevType        = 0x00U,
215     phNfc_eISO14443_A_PCD,
216     phNfc_eISO14443_B_PCD,
217     phNfc_eISO14443_BPrime_PCD,
218     phNfc_eFelica_PCD,
219     phNfc_eJewel_PCD,
220     phNfc_eISO15693_PCD,
221     phNfc_eEpcGen2_PCD,
222     phNfc_ePCD_DevType,
223     phNfc_ePICC_DevType,
224     phNfc_eISO14443_A_PICC,
225     phNfc_eISO14443_4A_PICC,
226     phNfc_eISO14443_3A_PICC,
227     phNfc_eMifare_PICC,
228     phNfc_eISO14443_B_PICC,
229     phNfc_eISO14443_4B_PICC,
230     phNfc_eISO14443_BPrime_PICC,
231     phNfc_eFelica_PICC,
232     phNfc_eJewel_PICC,
233     phNfc_eISO15693_PICC,
234     phNfc_eEpcGen2_PICC,
235     phNfc_eNfcIP1_Target,
236     phNfc_eNfcIP1_Initiator,
237     phNfc_eInvalid_DevType
238 } phNfc_eRFDevType_t;
239 
240 /*
241  * The Remote Device Type List is used to identify the type of
242  * remote device that is discovered/connected
243  */
244 typedef phNfc_eRFDevType_t phNfc_eRemDevType_t;
245 typedef phNfc_eRemDevType_t phHal_eRemDevType_t;
246 
247 /*
248  *   Union for each available type of Commands.
249  */
250 
251 typedef union phNfc_uCommand
252 {
253     phNfc_eMifareCmdList_t MfCmd;  /* Mifare command structure.  */
254 } phNfc_uCmdList_t;
255 
256 /*
257  *  The Remote Device Information Structure holds information about one single Remote
258  *  Device detected.
259  */
260 typedef struct phNfc_sRemoteDevInformation
261 {
262     uint8_t                    SessionOpened;       /* Flag indicating the validity of
263                                                      * the handle of the remote device.
264                                                      * 1 = Device is not activer (Only discovered), 2 = Device is active and ready for use*/
265     phNfc_eRemDevType_t        RemDevType;          /* Remote device type */
266     phNfc_uRemoteDevInfo_t     RemoteDevInfo;       /* Union of available Remote Device */
267 } phNfc_sRemoteDevInformation_t;
268 
269 
270 /*
271  * Transceive Information Data Structure for sending commands/response to the remote device
272  */
273 
274 typedef struct phNfc_sTransceiveInfo
275 {
276     phNfc_uCmdList_t                cmd;        /* Command for transceive */
277     uint8_t                         addr;       /* Start Block Number */
278     uint8_t                         NumBlock;   /* Number of Blocks to perform operation */
279     /* For Felica only*/
280     uint16_t *ServiceCodeList;                  /* 2 Byte service Code List */
281     uint16_t *Blocklist;                        /* 2 Byte Block list */
282     phNfc_sData_t                   sSendData; /* Send data */
283     phNfc_sData_t                   sRecvData; /* Recv data */
284     /* For EPC-GEN */
285     uint32_t                        dwWordPtr;   /* Word address for the memory write */
286     uint8_t                         bWordPtrLen; /* Specifies the length of word pointer
287                                                  00: 8  bits
288                                                  01: 16 bits
289                                                  10: 24 bits
290                                                  11: 32 bits
291                                                  */
292     uint8_t                         bWordCount;   /* Number of words to be read or written */
293 } phNfc_sTransceiveInfo_t;
294 
295 #define UNUSED(X) (void)X;
296 
297 #endif /* PHNFCTYPES_H */
298