EVE 1.0
nfc-ndef.h
Go to the documentation of this file.
1 /**********************************************************************/
2 /**
3  * @file
4  * @brief Utility functions to manipulate with NFC NDEF.
5  *
6  * @author SE, Jetro AS
7  ***********************************************************************/
8 
9 #include <stddef.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 
13 #ifndef NFC_NDEF_H
14 #define NFC_NDEF_H
15 
16 /**
17  * \defgroup ndef NFC NDEF manipulation
18  * \ingroup util
19  * \{
20  *
21  * Utility functions to manipulate with NFC NDEF.
22  */
23 
24 #define NDEF_MB (1 << 7) //!< NDEF message first byte: Message begin flag
25 #define NDEF_ME (1 << 6) //!< NDEF message first byte: Message end flag
26 #define NDEF_CF (1 << 5) //!< NDEF message first byte: Message chunk flag
27 #define NDEF_SR (1 << 4) //!< NDEF message first byte: Short record flag: payload length is 1 byte (otherwise 4 bytes)
28 #define NDEF_IL (1 << 3) //!< NDEF message first byte: ID length flag: ID is present if this flag is set
29 #define NDEF_TNF_EMPTY 0x00 //!< NDEF message first byte: Empty Type
30 #define NDEF_TNF_WELLKNOWN 0x01 //!< NDEF message first byte: Well-known Type
31 #define NDEF_TNF_MEDIA 0x02 //!< NDEF message first byte: Media Type
32 #define NDEF_TNF_URI 0x03 //!< NDEF message first byte: Absolute URI Type
33 #define NDEF_TNF_EXT 0x04 //!< NDEF message first byte: External Type
34 #define NDEF_TNF_UNKNOWN 0x05 //!< NDEF message first byte: Unknown Type
35 #define NDEF_TNF_UNCHANGED 0x06 //!< NDEF message first byte: Unchanged Type
36 #define NDEF_TNF_RESERVED 0x07 //!< NDEF message first byte: Reserved Type
37 
38 /******************************************************************************
39  * NFC driver public types
40  *****************************************************************************/
41 
42 /**
43  * @brief The callback is called on NDEF write attempt in card emulation mode
44  * @param Nfc NFC driver descriptor
45  * @param Offset Requested NDEF offset to write to
46  * @param Data Data to write
47  * @param DataLen Data Length
48  * @return bool true on success, false on error
49  */
50 typedef bool nfc_ndef_changed_cb_t(const void* Nfc_p,
51  uint8_t* Type, uint32_t TypeLen,
52  uint8_t* Data, uint32_t DataLen);
53 
54 /**
55  * @brief Table element of interrupt handlers called on NDEF write attempt for
56  * supported NFC message types.
57  */
59 {
60  nfc_ndef_changed_cb_t* NdefChangedCb; //!< Callback to call on NDEF write attempt in card emulation mode
61  const char* TypeName; //!< NFC message type name
62 };
63 
64 /**
65  * @brief Structure describes one parsed NDEF record. See standard NFC Data
66  * Exchange Format specification.
67  */
69 {
70  uint8_t MessageBegin:1; //!< Flag: Record is the beginning of the message
71  uint8_t MessageEnd:1; //!< Flag: Record is the end of the message
72  uint8_t RecordType:3; //!< Type Name Format Field (Well-known, External etc.)
73  uint8_t* TypeName; //!< Pointer to Type Name
74  uint8_t TypeLen; //!< Type Name length
75  uint8_t* Id; //!< Pointer to Record ID
76  uint8_t IdLen; //!< Record ID length
77  uint8_t* Data; //!< Pointer to Record Payload
78  uint32_t DataLen; //!< Record Payload length
79 };
80 
81 
82 /**
83  * @brief Nullify NDEF messege output buffer.
84  *
85  * @param NdefFile Pointer to the NDEF message buffer.
86  * @param NdefFileSize NDEF message buffer size.
87  * @return bool true on success, false on error
88  */
89 bool NfcNdefResetBuffer(uint8_t* NdefFile, uint16_t NdefFileSize);
90 
91 /**
92  * @brief Get current NDEF messege length.
93  *
94  * @param [in] NdefFile Pointer to the NDEF message buffer.
95  * @param [in] NdefFileSize NDEF message buffer size.
96  * @param [out] DataLength NDEF message length.
97  * @return bool true on success, false on error
98  */
99 bool NfcNdefGetDataLength(uint8_t* NdefFile, uint16_t NdefFileSize,
100  uint16_t* DataLength);
101 
102 /**
103  * @brief Increase current NDEF messege length.
104  * The function must be used when adding a record to the NDEF message with
105  * external tool or manually (not NfcNdefAddRecord()).
106  *
107  * @param NdefFile Pointer to the NDEF message buffer.
108  * @param NdefFileSize NDEF message buffer size.
109  * @param DataLength Additional length added to the NDEF message.
110  * @return bool true on success, false on error
111  */
112 bool NfcNdefAddDataLength(uint8_t* NdefFile, uint16_t NdefFileSize,
113  uint16_t DataLength);
114 
115 /**
116  * @brief Add a new record to the NDEF messege.
117  *
118  * @param NdefFile Pointer to the NDEF message buffer.
119  * @param NdefFileSize NDEF message buffer size.
120  * @param NdefRec Pointer to a structure holding the NDEF record to add.
121  * @return bool true on success, false on error
122  */
123 bool NfcNdefAddRecord(uint8_t* NdefFile, uint16_t NdefFileSize,
124  struct ndef_record_t* NdefRec);
125 
126 /**
127  * @brief Close the NDEF messege.
128  * The function must be used after NDEF message altering either with
129  * NfcNdefAddRecord() or NfcNdefAddDataLength() before supplying to NFC library
130  * to ensure all the record headers are valid.
131  *
132  * @param NdefFile Pointer to the NDEF message buffer.
133  * @param NdefFileSize NDEF message buffer size.
134  * @return bool true on success, false on error
135  */
136 bool NfcNdefFinalize(uint8_t* NdefFile, uint16_t NdefFileSize);
137 
138 
139 /**
140  * @brief Get the pointer to the first record inside an NDEF message.
141  *
142  * @param NdefFile [in] Pointer to the NDEF message buffer.
143  * @param NewRecord [out] Pointer to the first record pointer, NULL on error.
144  */
145 void NfcNdefGetFirstRecord(uint8_t* NdefFile, uint8_t** NewRecord);
146 
147 /**
148  * @brief Get the pointer to the next record inside an NDEF message.
149  *
150  * @param NdefFile [in] Pointer to the NDEF message buffer.
151  * @param PrevRecord [in] The previus record pointer.
152  * @param NewRecord [out] Pointer to the next record pointer, NULL on error.
153  */
154 void NfcNdefGetRecord(uint8_t* NdefFile, uint8_t* PrevRecord, uint8_t** NewRecord);
155 
156 /**
157  * @brief Parse raw NDEF record into the structure.
158  *
159  * @param Record [in] Pointer to the NDEF record buffer (Typically a pointer
160  * to NDEF message buffer content obtained with
161  * NfcNdefGetFirstRecord() / NfcNdefGetRecord().
162  * @param NdefRec [out] Pointer to a descriptor of NDEF record to add,
163  * NULL on error.
164  */
165 void NfcNdefRecordParse(uint8_t* Record, struct ndef_record_t* NdefRec);
166 
167 /** \} */
168 
169 #endif /* NFC_NDEF_H */
bool NfcNdefResetBuffer(uint8_t *NdefFile, uint16_t NdefFileSize)
Nullify NDEF messege output buffer.
void NfcNdefGetFirstRecord(uint8_t *NdefFile, uint8_t **NewRecord)
Get the pointer to the first record inside an NDEF message.
void NfcNdefRecordParse(uint8_t *Record, struct ndef_record_t *NdefRec)
Parse raw NDEF record into the structure.
nfc_ndef_changed_cb_t * NdefChangedCb
Callback to call on NDEF write attempt in card emulation mode.
Definition: nfc-ndef.h:60
void NfcNdefGetRecord(uint8_t *NdefFile, uint8_t *PrevRecord, uint8_t **NewRecord)
Get the pointer to the next record inside an NDEF message.
const char * TypeName
NFC message type name.
Definition: nfc-ndef.h:61
bool NfcNdefGetDataLength(uint8_t *NdefFile, uint16_t NdefFileSize, uint16_t *DataLength)
Get current NDEF messege length.
uint8_t TypeLen
Type Name length.
Definition: nfc-ndef.h:74
Table element of interrupt handlers called on NDEF write attempt for supported NFC message types...
Definition: nfc-ndef.h:58
uint8_t * Id
Pointer to Record ID.
Definition: nfc-ndef.h:75
uint32_t DataLen
Record Payload length.
Definition: nfc-ndef.h:78
bool nfc_ndef_changed_cb_t(const void *Nfc_p, uint8_t *Type, uint32_t TypeLen, uint8_t *Data, uint32_t DataLen)
The callback is called on NDEF write attempt in card emulation mode.
Definition: nfc-ndef.h:50
bool NfcNdefAddDataLength(uint8_t *NdefFile, uint16_t NdefFileSize, uint16_t DataLength)
Increase current NDEF messege length. The function must be used when adding a record to the NDEF mess...
uint8_t * Data
Pointer to Record Payload.
Definition: nfc-ndef.h:77
uint8_t * TypeName
Pointer to Type Name.
Definition: nfc-ndef.h:73
uint8_t IdLen
Record ID length.
Definition: nfc-ndef.h:76
bool NfcNdefFinalize(uint8_t *NdefFile, uint16_t NdefFileSize)
Close the NDEF messege. The function must be used after NDEF message altering either with NfcNdefAddR...
Structure describes one parsed NDEF record. See standard NFC Data Exchange Format specification...
Definition: nfc-ndef.h:68
bool NfcNdefAddRecord(uint8_t *NdefFile, uint16_t NdefFileSize, struct ndef_record_t *NdefRec)
Add a new record to the NDEF messege.