-
Notifications
You must be signed in to change notification settings - Fork 4
/
NDEF.cpp
217 lines (189 loc) · 6.44 KB
/
NDEF.cpp
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
217
/* NDEF - Handle generic NDEF records
*
* Copyright (c) 2015 Eric Brundick <spirilis [at] linux dot com>
* 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.
*/
#include "NDEF.h"
NDEF::NDEF()
{
tnf = 0x00;
payload = NULL;
type = NULL;
id = NULL;
payload_length = 0;
payload_buf_maxlen = 0;
type_length = 0;
id_length = 0;
type_buf_maxlen = 0;
id_buf_maxlen = 0;
}
// Default implementation of sendTo()
int NDEF::sendTo(Print &p, boolean first_msg, boolean last_msg)
{
uint8_t msghdr;
int printedSize = 0;
// Sanity checking
if (type_length && type == NULL)
return -1;
if (id_length && id == NULL)
return -1;
if (payload_length && payload == NULL)
return -1;
// Output valid NDEF binary format
msghdr = 0x00;
if (first_msg)
msghdr |= NDEF_FIELD_MB;
if (last_msg)
msghdr |= NDEF_FIELD_ME;
if (id_length)
msghdr |= NDEF_FIELD_IL;
if (payload_length > 254) {
p.write(tnf | msghdr); // SR cleared, using 32-bit Payload Length
} else {
p.write(tnf | msghdr | NDEF_FIELD_SR);
}
p.write((uint8_t)type_length); // Length of TYPE field
printedSize = 2;
if (payload_length > 254) {
// PAYLOAD_LENGTH is a 32-bit Big-Endian unsigned integer
p.write((uint8_t) (payload_length >> 24));
p.write((uint8_t) ((payload_length >> 16) & 0xFF));
p.write((uint8_t) ((payload_length >> 8) & 0xFF));
p.write((uint8_t) (payload_length & 0xFF));
printedSize += 4;
} else {
// PAYLOAD_LENGTH is an 8-bit unsigned integer
p.write(payload_length);
printedSize++;
}
// ID length
if (id_length) {
p.write((uint8_t) id_length);
printedSize++;
}
/* For TYPE and ID, and PAYLOAD for that matter, keep in mind if their length = 0, p.write() won't do anything.
* So no need to gate these statements with if (type_length) { ... } or if (id_length) { ... } or
* if (payload_length) { ... }
*/
// TYPE
p.write((const uint8_t *)type, type_length);
printedSize += type_length;
// ID
p.write((const uint8_t *)id, id_length);
printedSize += id_length;
// PAYLOAD
p.write((const uint8_t *)payload, payload_length);
printedSize += payload_length;
// NOTE: printedSize may be a smaller variable than payload_length
return printedSize;
}
int NDEF::import(Stream &s)
{
int c, ndef_hdr;
size_t rlen = 0; // Total NDEF record size
uint8_t plen32[4];
int i;
// Requires payload to be a valid buffer (we assume it's writable)
if (payload == NULL || type == NULL || !type_buf_maxlen)
return -1; // Not going to bother attempting to read
type_length = 0;
id_length = 0;
payload_length = 0;
// read NDEF header byte
c = s.read(); if (c < 0) return -1;
ndef_hdr = c;
rlen++;
// Extract TNF
tnf = ndef_hdr & 0x07;
// read TYPE_LENGTH
c = s.read(); if (c < 0) return -1;
type_length = c;
rlen++;
// read PAYLOAD_LENGTH
if (ndef_hdr & NDEF_FIELD_SR) {
c = s.read(); if (c < 0) return -1;
payload_length = c;
rlen++;
} else {
if (s.readBytes((char *)&plen32[0], 4) < 4)
return -1;
payload_length = ((uint32_t) plen32[0]) << 24 |
((uint32_t) plen32[1]) << 16 |
((uint32_t) plen32[2]) << 8 |
((uint32_t) plen32[3]);
rlen += 4;
}
// Was the ID bit set? If so, read ID length.
if (ndef_hdr & NDEF_FIELD_IL) {
c = s.read(); if (c < 0) return -1;
id_length = c;
// Is id_length > 0? If so, be sure we have a buffer for 'id'
if (id_length && id == NULL)
return -1; // No buffer for holding the ID!
rlen++;
} else {
if (id_buf_maxlen && id != NULL) {
// Clear the ID field since there will be none here
id_length = 0;
id[id_length] = '\0';
}
}
// read TYPE
rlen += type_length;
if (type_length >= type_buf_maxlen) {
size_t t = type_buf_maxlen - 1;
if (s.readBytes(type, t) < t)
return -1;
// Drain remaining bytes from the TYPE field
while (type_length > type_buf_maxlen) {
if (s.read() < 0) return -1;
type_length--;
}
} else {
if (s.readBytes(type, type_length) < type_length)
return -1;
}
type[type_length] = '\0'; // Type is always meant to be a string
// read ID, if applicable
if (ndef_hdr & NDEF_FIELD_IL) {
rlen += id_length;
if (id_length >= id_buf_maxlen) {
size_t ti = id_buf_maxlen - 1; // accounting for the '\0' at the end
if (s.readBytes(id, ti) < ti)
return -1;
// Drain remaining bytes from the ID field
while (id_length > id_buf_maxlen) {
if (s.read() < 0) return -1;
id_length--;
}
} else {
if (s.readBytes(id, id_length) < id_length)
return -1;
}
id[id_length] = '\0'; // ID is always meant to be a string
}
// read PAYLOAD
if (s.readBytes((char *)payload, payload_length) < payload_length)
return -1;
rlen += payload_length;
// All done!
return rlen; // Total size of NDEF record
}