mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-29 17:28:28 +02:00
Extend nfc ndef records with Text (#2191)
Co-authored-by: Oxan van Leeuwen <oxan@oxanvanleeuwen.nl>
This commit is contained in:
@@ -10,16 +10,13 @@ NdefMessage::NdefMessage(std::vector<uint8_t> &data) {
|
||||
uint8_t index = 0;
|
||||
while (index <= data.size()) {
|
||||
uint8_t tnf_byte = data[index++];
|
||||
bool me = tnf_byte & 0x40;
|
||||
bool sr = tnf_byte & 0x10;
|
||||
bool il = tnf_byte & 0x08;
|
||||
uint8_t tnf = tnf_byte & 0x07;
|
||||
bool me = tnf_byte & 0x40; // Message End bit (is set if this is the last record of the message)
|
||||
bool sr = tnf_byte & 0x10; // Short record bit (is set if payload size is less or equal to 255 bytes)
|
||||
bool il = tnf_byte & 0x08; // ID length bit (is set if ID Length field exists)
|
||||
uint8_t tnf = tnf_byte & 0x07; // Type Name Format
|
||||
|
||||
ESP_LOGVV(TAG, "me=%s, sr=%s, il=%s, tnf=%d", YESNO(me), YESNO(sr), YESNO(il), tnf);
|
||||
|
||||
auto record = make_unique<NdefRecord>();
|
||||
record->set_tnf(tnf);
|
||||
|
||||
uint8_t type_length = data[index++];
|
||||
uint32_t payload_length = 0;
|
||||
if (sr) {
|
||||
@@ -38,28 +35,34 @@ NdefMessage::NdefMessage(std::vector<uint8_t> &data) {
|
||||
ESP_LOGVV(TAG, "Lengths: type=%d, payload=%d, id=%d", type_length, payload_length, id_length);
|
||||
|
||||
std::string type_str(data.begin() + index, data.begin() + index + type_length);
|
||||
record->set_type(type_str);
|
||||
|
||||
index += type_length;
|
||||
|
||||
std::string id_str = "";
|
||||
if (il) {
|
||||
std::string id_str(data.begin() + index, data.begin() + index + id_length);
|
||||
record->set_id(id_str);
|
||||
id_str = std::string(data.begin() + index, data.begin() + index + id_length);
|
||||
index += id_length;
|
||||
}
|
||||
|
||||
uint8_t payload_identifier = 0x00;
|
||||
if (type_str == "U") {
|
||||
payload_identifier = data[index++];
|
||||
payload_length -= 1;
|
||||
std::vector<uint8_t> payload_data(data.begin() + index, data.begin() + index + payload_length);
|
||||
|
||||
std::unique_ptr<NdefRecord> record;
|
||||
|
||||
// Based on tnf and type, create a more specific NdefRecord object
|
||||
// constructed from the payload data
|
||||
if (tnf == TNF_WELL_KNOWN && type_str == "U") {
|
||||
record = make_unique<NdefRecordUri>(payload_data);
|
||||
} else if (tnf == TNF_WELL_KNOWN && type_str == "T") {
|
||||
record = make_unique<NdefRecordText>(payload_data);
|
||||
} else {
|
||||
// Could not recognize the record, so store as generic one.
|
||||
record = make_unique<NdefRecord>(payload_data);
|
||||
record->set_tnf(tnf);
|
||||
record->set_type(type_str);
|
||||
}
|
||||
|
||||
std::string payload_str(data.begin() + index, data.begin() + index + payload_length);
|
||||
record->set_id(id_str);
|
||||
|
||||
if (payload_identifier > 0x00 && payload_identifier <= PAYLOAD_IDENTIFIERS_COUNT) {
|
||||
payload_str.insert(0, PAYLOAD_IDENTIFIERS[payload_identifier]);
|
||||
}
|
||||
|
||||
record->set_payload(payload_str);
|
||||
index += payload_length;
|
||||
|
||||
ESP_LOGV(TAG, "Adding record type %s = %s", record->get_type().c_str(), record->get_payload().c_str());
|
||||
@@ -82,13 +85,10 @@ bool NdefMessage::add_record(std::unique_ptr<NdefRecord> record) {
|
||||
bool NdefMessage::add_text_record(const std::string &text) { return this->add_text_record(text, "en"); };
|
||||
|
||||
bool NdefMessage::add_text_record(const std::string &text, const std::string &encoding) {
|
||||
std::string payload = to_string(text.length()) + encoding + text;
|
||||
return this->add_record(make_unique<NdefRecord>(TNF_WELL_KNOWN, "T", payload));
|
||||
return this->add_record(make_unique<NdefRecordText>(encoding, text));
|
||||
}
|
||||
|
||||
bool NdefMessage::add_uri_record(const std::string &uri) {
|
||||
return this->add_record(make_unique<NdefRecord>(TNF_WELL_KNOWN, "U", uri));
|
||||
}
|
||||
bool NdefMessage::add_uri_record(const std::string &uri) { return this->add_record(make_unique<NdefRecordUri>(uri)); }
|
||||
|
||||
std::vector<uint8_t> NdefMessage::encode() {
|
||||
std::vector<uint8_t> data;
|
||||
|
||||
Reference in New Issue
Block a user