-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfc_ndef.c
216 lines (176 loc) · 6.55 KB
/
nfc_ndef.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* MIT License
*
* Copyright (c) 2019 Sean Farrelly
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* File ndef.c
* Created by Sean Farrelly
* Version 1.0
*
*/
/*! @file ndef.c
* @brief Utility tools for NDEF format.
*/
#include "nfc_ndef.h"
/*
* @brief This API parses the next NDEF record found in the buffer.
*/
ndef_status_t ndef_parse_next_rec(uint8_t *buf, ndef_record_t *rec, size_t *br)
{
/* Check parameters are valid */
if((buf == NULL) || (rec == NULL) || (br == 0))
return NDEF_E_INVALID_ARGS;
/* Check the NDEF record has at least the MB or ME set for validity */
if (NDEF_RECORD_GET_FLAG(*buf, NDEF_RECORD_FLAG_MB) == 0 &&
NDEF_RECORD_GET_FLAG(*buf, NDEF_RECORD_FLAG_ME) == 0)
return NDEF_E_NOT_FOUND;
uint8_t *buf_start = buf;
rec->header = *buf++;
rec->type_len = *buf++;
if (NDEF_RECORD_GET_FLAG(rec->header, NDEF_RECORD_FLAG_SR))
{
/* NDEF record is a 'short' sized record, copy the 1-byte 'Paylod Length' field */
rec->payload_len = *buf++;
}
else
{
/* NDEF record is a 'normal' sized record, copy the 4-byte 'Payload Length' field */
rec->payload_len = ntohl(buf);
buf += sizeof(uint32_t);
}
if(NDEF_RECORD_GET_FLAG(rec->header, NDEF_RECORD_FLAG_IL))
rec->id_len = *buf++;
else
rec->id_len = 0;
rec->type = (rec->type_len == 0) ? NULL : buf;
buf += rec->type_len;
rec->id = (rec->id_len == 0) ? NULL : buf;
buf += rec->id_len;
rec->payload = (rec->payload_len == 0) ? NULL : buf;
buf += rec->payload_len;
rec->total_length = buf - buf_start;
*br = rec->total_length;
return NDEF_OK;
}
// uint8_t* NDEF_BuildRecord(uint8_t flags, uint8_t *type, uint8_t typeLength, uint8_t *id, uint8_t idLength, uint8_t *payload, uint32_t payloadLength)
// {
// size_t totalLength;
// uint8_t shortRecord; /* bool */
// uint8_t header;
// uint8_t *pos;
// shortRecord = payloadLength < 256 ? 1 : 0;
// /* ==== Calculate total length of record */
// totalLength = 2; /* Flag + type length */
// totalLength += shortRecord ? sizeof(uint8_t) : sizeof(uint32_t); /* Payload length */
// if(idLength > 0)
// totalLength += 1;
// totalLength += typeLength + idLength + payloadLength;
// /* ===================================== */
// //NDEF_RecordType *rec = malloc(sizeof(NDEF_RecordType));
// uint8_t *buf = malloc(totalLength);
// if(buf == NULL)
// {
// Log_info("NDEF: Failed to allocate record for build");
// return NULL;
// }
// pos = buf; /* Set pointer to beginning of NDEF Record */
// /* ==== Ensure correct flags are set in record */
// header = flags;
// if(idLength > 0)
// header |= NDEF_RECORD_FLAG_IL;
// if(shortRecord)
// header |= NDEF_RECORD_FLAG_SR;
// *pos++ = header;
// *pos++ = typeLength;
// if(shortRecord)
// {
// *pos++ = (uint8_t) payloadLength;
// }
// else
// {
// memcpy(pos, (uint8_t*) &payloadLength, sizeof(payloadLength));
// pos += sizeof(payloadLength);
// }
// if(idLength > 0)
// *pos++ = idLength;
// if((type != NULL) && (typeLength > 0))
// {
// memcpy(pos, type, typeLength);
// pos += sizeof(typeLength);
// }
// if(id != NULL && (idLength > 0))
// {
// memcpy(pos, id, idLength);
// pos += sizeof(idLength);
// }
// if(payload != NULL && (payloadLength > 0))
// {
// memcpy(pos, payload, payloadLength);
// pos += sizeof(payloadLength);
// }
// return buf;
// }
// /* Print our NDEF Record in similar styling to http://www.ndefparser.net */
// void NDEF_PrintRecord(NDEF_RecordType *rec)
// {
// Log_info("======== NDEF RECORD =======");
// Log_info("HEADER: 0x%02X (MB:%d, ME:%d, CF:%d, SR:%d, IL:%d, TNF:%d)",
// rec->header,
// NDEF_RECORD_GET_FLAG(rec->header, NDEF_RECORD_FLAG_MB),
// NDEF_RECORD_GET_FLAG(rec->header, NDEF_RECORD_FLAG_ME),
// NDEF_RECORD_GET_FLAG(rec->header, NDEF_RECORD_FLAG_CF),
// NDEF_RECORD_GET_FLAG(rec->header, NDEF_RECORD_FLAG_SR),
// NDEF_RECORD_GET_FLAG(rec->header, NDEF_RECORD_FLAG_IL),
// (rec->header & NDEF_RECORD_FLAG_TNF_Msk));
// Log_info("TYPE LENGTH: 0x%02X", rec->typeLength);
// Log_info("PAYLOAD LENGTH: 0x%X", rec->payloadLength);
// if(rec->idLength != 0)
// Log_info("ID LENGTH: 0x%02X", rec->idLength);
// else
// Log_info("ID LENGTH: No ID Length field");
// if (rec->typeLength != 0)
// Log_info("TYPE: \"%.*s\"", rec->typeLength, rec->type);
// else
// Log_info("TYPE: No Type field");
// if (rec->idLength != 0)
// Log_info("ID: \"%.*s\"", rec->idLength, rec->id);
// else
// Log_info("ID: No ID field");
// if (rec->payloadLength != 0)
// {
// char buf[(rec->payloadLength*2) + 15];
// char *ptr = &buf[0];
// ptr += sprintf(ptr, "PAYLOAD: 0x");
// for(size_t i = 0 ; i < rec->payloadLength ; i++)
// {
// ptr += sprintf(ptr, "%02X", rec->payload[i]);
// }
// Log_info(buf);
// Log_print("Payload as ASCII: \"", 19);
// Log_print((char*) rec->payload, rec->payloadLength);
// Log_println("\"", 1);
// }
// else
// {
// Log_info("Payload: No Payload");
// }
// Log_info("============================");
// }