ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
nci_message.cpp
Go to the documentation of this file.
1#include "nci_core.h"
2#include "nci_message.h"
3#include "esphome/core/log.h"
4
5#include <cstdio>
6
7namespace esphome::nfc {
8
9static const char *const TAG = "NciMessage";
10
11NciMessage::NciMessage(const uint8_t message_type, const std::vector<uint8_t> &payload) {
12 this->set_message(message_type, payload);
13}
14
15NciMessage::NciMessage(const uint8_t message_type, const uint8_t gid, const uint8_t oid) {
16 this->set_header(message_type, gid, oid);
17}
18
19NciMessage::NciMessage(const uint8_t message_type, const uint8_t gid, const uint8_t oid,
20 const std::vector<uint8_t> &payload) {
21 this->set_message(message_type, gid, oid, payload);
22}
23
24NciMessage::NciMessage(const std::vector<uint8_t> &raw_packet) { this->nci_message_ = raw_packet; };
25
26std::vector<uint8_t> NciMessage::encode() {
27 this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET] = this->nci_message_.size() - nfc::NCI_PKT_HEADER_SIZE;
28 std::vector<uint8_t> message = this->nci_message_;
29 return message;
30}
31
32void NciMessage::reset() { this->nci_message_ = {0, 0, 0}; }
33
35 return this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & nfc::NCI_PKT_MT_MASK;
36}
37
38uint8_t NciMessage::get_gid() const { return this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & nfc::NCI_PKT_GID_MASK; }
39
40uint8_t NciMessage::get_oid() const { return this->nci_message_[nfc::NCI_PKT_OID_OFFSET] & nfc::NCI_PKT_OID_MASK; }
41
42uint8_t NciMessage::get_payload_size(const bool recompute) {
43 if (!this->nci_message_.empty()) {
44 if (recompute) {
45 this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET] = this->nci_message_.size() - nfc::NCI_PKT_HEADER_SIZE;
46 }
47 return this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET];
48 }
49 return 0;
50}
51
53 if (this->nci_message_.size() > nfc::NCI_PKT_PAYLOAD_OFFSET) {
54 return this->nci_message_[nfc::NCI_PKT_PAYLOAD_OFFSET];
55 }
56 return STATUS_FAILED;
57}
58
59uint8_t NciMessage::get_message_byte(const uint8_t offset) const {
60 if (this->nci_message_.size() > offset) {
61 return this->nci_message_[offset];
62 }
63 return 0;
64}
65
66std::vector<uint8_t> &NciMessage::get_message() { return this->nci_message_; }
67
68bool NciMessage::has_payload() const { return this->nci_message_.size() > nfc::NCI_PKT_HEADER_SIZE; }
69
70bool NciMessage::message_type_is(const uint8_t message_type) const {
71 if (!this->nci_message_.empty()) {
72 return message_type == (this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & nfc::NCI_PKT_MT_MASK);
73 }
74 return false;
75}
76
77bool NciMessage::message_length_is(const uint8_t message_length, const bool recompute) {
78 if (this->nci_message_.size() > nfc::NCI_PKT_LENGTH_OFFSET) {
79 if (recompute) {
80 this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET] = this->nci_message_.size() - nfc::NCI_PKT_HEADER_SIZE;
81 }
82 return message_length == this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET];
83 }
84 return false;
85}
86
87bool NciMessage::gid_is(const uint8_t gid) const {
88 if (this->nci_message_.size() > nfc::NCI_PKT_MT_GID_OFFSET) {
89 return gid == (this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & nfc::NCI_PKT_GID_MASK);
90 }
91 return false;
92}
93
94bool NciMessage::oid_is(const uint8_t oid) const {
95 if (this->nci_message_.size() > nfc::NCI_PKT_OID_OFFSET) {
96 return oid == (this->nci_message_[nfc::NCI_PKT_OID_OFFSET] & nfc::NCI_PKT_OID_MASK);
97 }
98 return false;
99}
100
101bool NciMessage::simple_status_response_is(const uint8_t response) const {
102 if (this->nci_message_.size() > nfc::NCI_PKT_PAYLOAD_OFFSET) {
103 return response == this->nci_message_[nfc::NCI_PKT_PAYLOAD_OFFSET];
104 }
105 return false;
106}
107
108void NciMessage::set_header(const uint8_t message_type, const uint8_t gid, const uint8_t oid) {
109 if (this->nci_message_.size() < nfc::NCI_PKT_HEADER_SIZE) {
110 this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
111 }
112 this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] =
113 (message_type & nfc::NCI_PKT_MT_MASK) | (gid & nfc::NCI_PKT_GID_MASK);
114 this->nci_message_[nfc::NCI_PKT_OID_OFFSET] = oid & nfc::NCI_PKT_OID_MASK;
115}
116
117void NciMessage::set_message(const uint8_t message_type, const std::vector<uint8_t> &payload) {
118 this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
119 this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET] = payload.size();
120 this->nci_message_.insert(this->nci_message_.end(), payload.begin(), payload.end());
121}
122
123void NciMessage::set_message(const uint8_t message_type, const uint8_t gid, const uint8_t oid,
124 const std::vector<uint8_t> &payload) {
125 this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
126 this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] =
127 (message_type & nfc::NCI_PKT_MT_MASK) | (gid & nfc::NCI_PKT_GID_MASK);
128 this->nci_message_[nfc::NCI_PKT_OID_OFFSET] = oid & nfc::NCI_PKT_OID_MASK;
129 this->nci_message_[nfc::NCI_PKT_LENGTH_OFFSET] = payload.size();
130 this->nci_message_.insert(this->nci_message_.end(), payload.begin(), payload.end());
131}
132
133void NciMessage::set_message_type(const uint8_t message_type) {
134 if (this->nci_message_.size() < nfc::NCI_PKT_HEADER_SIZE) {
135 this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
136 }
137 auto mt_masked = this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & ~nfc::NCI_PKT_MT_MASK;
138 this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] = mt_masked | (message_type & nfc::NCI_PKT_MT_MASK);
139}
140
141void NciMessage::set_gid(const uint8_t gid) {
142 if (this->nci_message_.size() < nfc::NCI_PKT_HEADER_SIZE) {
143 this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
144 }
145 auto gid_masked = this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & ~nfc::NCI_PKT_GID_MASK;
146 this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] = gid_masked | (gid & nfc::NCI_PKT_GID_MASK);
147}
148
149void NciMessage::set_oid(const uint8_t oid) {
150 if (this->nci_message_.size() < nfc::NCI_PKT_HEADER_SIZE) {
151 this->nci_message_.resize(nfc::NCI_PKT_HEADER_SIZE);
152 }
153 this->nci_message_[nfc::NCI_PKT_OID_OFFSET] = oid & nfc::NCI_PKT_OID_MASK;
154}
155
156void NciMessage::set_payload(const std::vector<uint8_t> &payload) {
157 std::vector<uint8_t> message(this->nci_message_.begin(), this->nci_message_.begin() + nfc::NCI_PKT_HEADER_SIZE);
158
159 message.insert(message.end(), payload.begin(), payload.end());
160 message[nfc::NCI_PKT_LENGTH_OFFSET] = payload.size();
161 this->nci_message_ = message;
162}
163
164} // namespace esphome::nfc
uint8_t get_payload_size(bool recompute=false)
bool message_type_is(uint8_t message_type) const
uint8_t get_oid() const
std::vector< uint8_t > encode()
bool simple_status_response_is(uint8_t response) const
uint8_t get_message_byte(uint8_t offset) const
bool gid_is(uint8_t gid) const
void set_gid(uint8_t gid)
std::vector< uint8_t > & get_message()
void set_message(uint8_t message_type, const std::vector< uint8_t > &payload)
void set_header(uint8_t message_type, uint8_t gid, uint8_t oid)
bool message_length_is(uint8_t message_length, bool recompute=false)
void set_oid(uint8_t oid)
void set_payload(const std::vector< uint8_t > &payload)
uint8_t get_gid() const
std::vector< uint8_t > nci_message_
Definition nci_message.h:45
void set_message_type(uint8_t message_type)
uint8_t get_simple_status_response() const
bool oid_is(uint8_t oid) const
uint8_t get_message_type() const
const char * message
Definition component.cpp:35