7 static const char *
const TAG =
"nfc.ndef_message";
10 ESP_LOGV(TAG,
"Building NdefMessage with %zu bytes", data.size());
12 while (index <= data.size()) {
13 uint8_t tnf_byte = data[index++];
14 bool me = tnf_byte & 0x40;
15 bool sr = tnf_byte & 0x10;
16 bool il = tnf_byte & 0x08;
17 uint8_t tnf = tnf_byte & 0x07;
19 ESP_LOGVV(TAG,
"me=%s, sr=%s, il=%s, tnf=%d", YESNO(me), YESNO(sr), YESNO(il), tnf);
21 uint8_t type_length = data[index++];
22 uint32_t payload_length = 0;
24 payload_length = data[index++];
26 payload_length = (
static_cast<uint32_t
>(data[index]) << 24) | (
static_cast<uint32_t
>(data[index + 1]) << 16) |
27 (
static_cast<uint32_t
>(data[index + 2]) << 8) |
static_cast<uint32_t
>(data[index + 3]);
31 uint8_t id_length = 0;
33 id_length = data[index++];
36 ESP_LOGVV(TAG,
"Lengths: type=%d, payload=%" PRIu32
", id=%d", type_length, payload_length, id_length);
38 std::string type_str(data.begin() + index, data.begin() + index + type_length);
42 std::string id_str =
"";
44 id_str = std::string(data.begin() + index, data.begin() + index + id_length);
48 if ((data.begin() + index > data.end()) || (data.begin() + index + payload_length > data.end())) {
49 ESP_LOGE(TAG,
"Corrupt record encountered; NdefMessage constructor aborting");
53 std::vector<uint8_t> payload_data(data.begin() + index, data.begin() + index + payload_length);
55 std::unique_ptr<NdefRecord> record;
59 if (tnf == TNF_WELL_KNOWN && type_str ==
"U") {
60 record = make_unique<NdefRecordUri>(payload_data);
61 }
else if (tnf == TNF_WELL_KNOWN && type_str ==
"T") {
62 record = make_unique<NdefRecordText>(payload_data);
65 record = make_unique<NdefRecord>(payload_data);
67 record->set_type(type_str);
70 record->set_id(id_str);
72 index += payload_length;
74 ESP_LOGV(TAG,
"Adding record type %s = %s", record->get_type().c_str(), record->get_payload().c_str());
83 if (this->
records_.size() >= MAX_NDEF_RECORDS) {
84 ESP_LOGE(TAG,
"Too many records. Max: %d", MAX_NDEF_RECORDS);
87 this->
records_.emplace_back(std::move(record));
94 return this->
add_record(make_unique<NdefRecordText>(encoding, text));
100 std::vector<uint8_t> data;
102 for (
size_t i = 0; i < this->
records_.size(); i++) {
103 auto encoded_record = this->
records_[i]->encode(i == 0, (i + 1) == this->
records_.size());
104 data.insert(data.end(), encoded_record.begin(), encoded_record.end());
bool add_text_record(const std::string &text)
bool add_record(std::unique_ptr< NdefRecord > record)
std::vector< std::shared_ptr< NdefRecord > > records_
std::vector< uint8_t > encode()
bool add_uri_record(const std::string &uri)
Implementation of SPI Controller mode.