-
Notifications
You must be signed in to change notification settings - Fork 0
/
st25dv_ndef.c
283 lines (236 loc) · 9.14 KB
/
st25dv_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Written by RJRP - 29/10/2023
* ST25 Library for idf framework
* This program is distributed under the MIT License
*/
#include <memory.h>
#include "st25dv_ndef.h"
#include "st25dv_registers.h"
esp_err_t st25dv_ndef_write_ccfile(uint64_t ccfile) {
uint8_t ccbyte[8];
memcpy(ccbyte, &ccfile, sizeof(ccfile));
return st25dv_write(ST25DV_USER_ADDRESS, 0x00, ccbyte, sizeof(ccfile));
}
esp_err_t st25dv_ndef_write_content(st25dv_config st25dv, uint16_t *address, bool mb, bool me, const std25dv_ndef_record record) {
uint8_t type_size = strlen(record.type);
uint16_t payload_size = strlen(record.payload);
uint8_t *record_data = malloc(NDEF_RECORD_SIZE(mb, type_size, payload_size));
uint8_t *data = record_data;
//Total length : Record header + Type length + Payload length + ID + Type + Payload
uint16_t record_length = 1 + 1 + (payload_size > 0xFF ? 4 : 1) + 1 + type_size + payload_size;
//If this ndef is the first one
if (mb) {
//Type5 Tag TLV-Format: T
*data++ = ST25DV_TYPE5_NDEF_MESSAGE;
//Type5 Tag TLV-Format: L
if (record_length > 0xFE) {
*data++ = 0xFF;
*data++ = record_length >> 8;
*data++ = record_length & 0xFF;
} else {
*data++ = record_length;
}
}
//Type5 Tag TLV-Format: V
uint8_t tnf = 0;
tnf |= mb ? NDEF_ST25DV_MB : 0;
tnf |= me ? NDEF_ST25DV_ME : 0;
tnf |= payload_size > 0xFF ? 0 : NDEF_ST25DV_SR;
tnf |= NDEF_ST25DV_IL;
tnf |= record.tnf;
*data++ = tnf;
//Type length
*data++ = type_size;
//Payload length
if (payload_size > 0xFF) {
*data++ = payload_size >> 24;
*data++ = (payload_size >> 16) & 0xFF;
*data++ = (payload_size >> 8) & 0xFF;
*data++ = payload_size & 0xFF;
} else {
*data++ = payload_size;
}
//ID
*data++ = 0x00;
//Add record type
memcpy(data, record.type, type_size);
data += type_size;
//Add record payload
memcpy(data, record.payload, payload_size);
data += payload_size;
uint8_t record_address = CCFILE_LENGTH;
if (*address > CCFILE_LENGTH) {
record_address = *address;
}
//If this ndef record is not the first one, we need to update the TLV-Format L value
if (!mb) {
//Read the possible 3 byte l value
uint8_t *l_value = malloc(0x03);
st25dv_read(st25dv.user_address, CCFILE_LENGTH + 1, l_value, 0x03);
uint16_t old_length = 0;
uint16_t total_length;
if (*l_value == 0xFF) {
//The l value is already 3 byte long
old_length |= *(l_value + 1) << 8;
old_length |= *(l_value + 2) & 0xFF;
total_length = old_length + record_length;
*(l_value + 1) = total_length >> 8;
*(l_value + 2) = total_length & 0xFF;
//Update the value
st25dv_write(st25dv.user_address, CCFILE_LENGTH + 1, l_value, 0x03);
} else {
//The l value is 1 byte long
old_length = *l_value;
total_length = old_length + record_length;
if (total_length > 0xFE) {
//The l value is 1 byte but needs to be 3
*l_value = 0xFF;
*(l_value + 1) = total_length >> 8;
*(l_value + 2) = total_length & 0xFF;
//Copy and move the existing records
uint8_t *st25dv_content = malloc(old_length);
st25dv_read(st25dv.user_address, CCFILE_LENGTH + 2, st25dv_content, old_length);
st25dv_write(st25dv.user_address, CCFILE_LENGTH + 1, l_value, 0x03);
vTaskDelay(100 / portTICK_PERIOD_MS);
st25dv_write(st25dv.user_address, CCFILE_LENGTH + 4, st25dv_content, old_length);
record_address += 2;
free(st25dv_content);
} else {
//The l value is already 1 byte
*l_value = total_length;
//Update the value
st25dv_write_byte(st25dv.user_address, CCFILE_LENGTH + 1, *l_value);
}
}
free(l_value);
}
vTaskDelay(100 / portTICK_PERIOD_MS);
st25dv_write(st25dv.user_address, record_address, record_data, data - record_data);
vTaskDelay(100 / portTICK_PERIOD_MS);
//Add terminator
if (me) {
st25dv_write_byte(st25dv.user_address, record_address + (data - record_data), ST25DV_TYPE5_TERMINATOR_TLV);
}
*address = record_address + (data - record_data);
free(record_data);
return ESP_OK;
}
esp_err_t st25dv_ndef_write_app_launcher_record(st25dv_config st25dv, uint16_t *address, bool mb, bool me, char *app_package) {
char record_type[] = NDEF_APP_LAUNCHER_TYPE;
std25dv_ndef_record record = {
NDEF_ST25DV_TNF_EXTERNAL,
record_type,
app_package
};
st25dv_ndef_write_content(st25dv, address, mb, me, record);
return ESP_OK;
}
esp_err_t st25dv_ndef_write_json_record(st25dv_config st25dv, uint16_t *address, bool mb, bool me, cJSON *json_data) {
char record_type[] = NDEF_JSON_TYPE;
char *json = cJSON_PrintUnformatted(json_data);
std25dv_ndef_record record = {
NDEF_ST25DV_TNF_MIME,
record_type,
json
};
st25dv_ndef_write_content(st25dv, address, mb, me, record);
free(json);
return ESP_OK;
}
esp_err_t st25dv_ndef_read(st25dv_config st25dv, uint8_t record_num, std25dv_ndef_record *output_records, uint8_t *record_count) {
//Get size of the first area
uint8_t enda1 = 0;
*record_count = 0;
st25dv_read_byte(st25dv.system_address, REG_ENDA1, &enda1);
//Convert the block value in bytes
enda1 = enda1 * 32 + 31;
uint16_t address = CCFILE_LENGTH;
//Read Type5 Tag TLV-Format
uint8_t *tlv = malloc(4);
uint16_t l_value = 0;
ESP_ERROR_CHECK(st25dv_read(st25dv.user_address, address, tlv, 4));
ST25DV_CHECK(tlv[0] == ST25DV_TYPE5_NDEF_MESSAGE)
if (*(tlv + 1) == 0xFF) {
//The l value is 3 byte long
l_value |= *(tlv + 2) << 8;
l_value |= *(tlv + 3) & 0xFF;
address += 4;
} else {
l_value = *(tlv + 1);
address += 2;
}
free(tlv);
while (address < enda1 || address < (CCFILE_LENGTH + l_value + 3)) {
//Read a record
uint8_t v_value;
uint8_t payload_type;
bool message_begin, message_end, chunk_flag, short_record, id_length;
//Get header content
ESP_ERROR_CHECK(st25dv_read_byte(st25dv.user_address, address, &v_value));
address++;
message_begin = NDEF_RECORD_HEADER_BIT(v_value, NDEF_ST25DV_MB);
message_end = NDEF_RECORD_HEADER_BIT(v_value, NDEF_ST25DV_ME);
chunk_flag = NDEF_RECORD_HEADER_BIT(v_value, NDEF_ST25DV_CF);
short_record = NDEF_RECORD_HEADER_BIT(v_value, NDEF_ST25DV_SR);
id_length = NDEF_RECORD_HEADER_BIT(v_value, NDEF_ST25DV_IL);
payload_type = v_value & NDEF_ST25DV_PT;
//Get type length
uint8_t type_length;
ESP_ERROR_CHECK(st25dv_read_byte(st25dv.user_address, address, &type_length));
address++;
//Get payload length
uint16_t payload_length = 0;
if (short_record) {
//Payload length is 1 byte
uint8_t data;
ESP_ERROR_CHECK(st25dv_read_byte(st25dv.user_address, address, &data));
payload_length = data;
address++;
} else {
//Payload length is 4 byte
uint8_t *data = malloc(4);
ESP_ERROR_CHECK(st25dv_read(st25dv.user_address, address, data, 4));
payload_length |= *data << 24;
payload_length |= *(data + 1) << 16;
payload_length |= *(data + 2) << 8;
payload_length |= *(data + 3);
free(data);
address += 4;
}
if (id_length) {
address++;
}
//Copying the data for the output
(*record_count)++;
if(*record_count == record_num){
//Add payload type to the output
output_records->tnf = payload_type;
//Get type and add it to the output
char *type = malloc(type_length + 1);
ESP_ERROR_CHECK(st25dv_read(st25dv.user_address, address, (uint8_t *) type, type_length));
*(type + type_length) = 0x00;
output_records->type = type;
address += type_length;
//Get payload and add it to the output
char *payload = malloc(payload_length + 1);
ESP_ERROR_CHECK(st25dv_read(st25dv.user_address, address, (uint8_t *) payload, payload_length));
*(payload + payload_length) = 0x00;
output_records->payload = payload;
address += payload_length;
}else{
address += type_length + payload_length;
}
//Get the end
uint8_t end_char = 0;
ESP_ERROR_CHECK(st25dv_read_byte(st25dv.user_address, address, &end_char));
if (end_char == ST25DV_TYPE5_TERMINATOR_TLV) {
return ESP_OK;
}
}
return ESP_OK;
}
esp_err_t st25dv_ndef_delete_records(std25dv_ndef_record *record) {
free(record->type);
free(record->payload);
return ESP_OK;
}