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