[英]Bug splitting NDEF data
我編寫了一個小函數來將原始數據格式化為NDEF,然后將其寫入標簽。
該函數的主要部分正常工作,沒有任何問題,唯一不起作用的是,它一直將0xFF寫入扇區的末尾而不是0x00(如果為空)。
碼:
int write_ndef(FreefareTag tag, uint8_t *data, const uint8_t type, size_t isize) {
uint8_t *ndef_msg;
size_t ndef_msg_len;
int sector_count;
ndef_msg = data;
ndef_msg_len = isize;
uint8_t write_data [4];
printf("Printing raw message :\n");
print_hex(ndef_msg, ndef_msg_len);
size_t encoded_size;
uint8_t *tlv_data = tlv_encode(type, ndef_msg, ndef_msg_len, &encoded_size);
printf("NDEF file is %zu bytes long.\n", encoded_size);
printf("Printing NDEF formatted message :\n");
print_hex(tlv_data, encoded_size);
sector_count = encoded_size / 4;
if((encoded_size%4)!= 0)
sector_count++;
for (size_t i = 0; i < sector_count; i++) {
for (size_t f = 0; f < 4; f++) {
/*once message written fill rest of sector with 0x00*/
if((i * 4 )+ f > encoded_size) {
write_data[f] = 0x00;
}
else {
write_data[f] = tlv_data[(i * 4) + f];
}
}
ntag21x_write(tag, block[i], write_data);/*takes an array with exactly 4 bytes and writes it to given address on given tag*/
}
return 1;
}
程序輸出為:
這可能與我拆分數據以寫入數據的方式有關,但我只是想不出辦法。
最后一個數據塊(在這種情況下為第3個)讀取:0x67、0x6c,0xfe,0xff(而不是應有的0x00)。
達到編碼數據末尾的測試減少了一個。
((i * 4) + f) > encoded_size
這僅是從TLV編碼數據結束后的第二個字節開始才是正確的(例如,如果encoded_data == 0
,則對於i = 0
, f = 1
,測試仍將為假)。
因此,您需要將此條件修改為
if (((i * 4) + f) >= encoded_size) {
write_data[f] = 0x00;
} else {
write_data[f] = tlv_data[(i * 4) + f];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.