ESPHome  2024.11.0
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 
7 namespace esphome {
8 namespace nfc {
9 
10 static const char *const TAG = "NciMessage";
11 
12 NciMessage::NciMessage(const uint8_t message_type, const std::vector<uint8_t> &payload) {
13  this->set_message(message_type, payload);
14 }
15 
16 NciMessage::NciMessage(const uint8_t message_type, const uint8_t gid, const uint8_t oid) {
17  this->set_header(message_type, gid, oid);
18 }
19 
20 NciMessage::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 
25 NciMessage::NciMessage(const std::vector<uint8_t> &raw_packet) { this->nci_message_ = raw_packet; };
26 
27 std::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 
33 void 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 
39 uint8_t NciMessage::get_gid() const { return this->nci_message_[nfc::NCI_PKT_MT_GID_OFFSET] & nfc::NCI_PKT_GID_MASK; }
40 
41 uint8_t NciMessage::get_oid() const { return this->nci_message_[nfc::NCI_PKT_OID_OFFSET] & nfc::NCI_PKT_OID_MASK; }
42 
43 uint8_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 
60 uint8_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 
67 std::vector<uint8_t> &NciMessage::get_message() { return this->nci_message_; }
68 
69 bool NciMessage::has_payload() const { return this->nci_message_.size() > nfc::NCI_PKT_HEADER_SIZE; }
70 
71 bool 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 
78 bool 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 
88 bool 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 
95 bool 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 
102 bool 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 
109 void 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 
118 void 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 
124 void 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 
134 void 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 
142 void 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 
150 void 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 
157 void 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_gid() const
Definition: nci_message.cpp:39
uint8_t get_message_type() const
Definition: nci_message.cpp:35
bool message_type_is(uint8_t message_type) const
Definition: nci_message.cpp:71
uint8_t get_message_byte(uint8_t offset) const
Definition: nci_message.cpp:60
void set_oid(uint8_t oid)
std::vector< uint8_t > & get_message()
Definition: nci_message.cpp:67
void set_header(uint8_t message_type, uint8_t gid, uint8_t oid)
bool gid_is(uint8_t gid) const
Definition: nci_message.cpp:88
void set_message_type(uint8_t message_type)
std::vector< uint8_t > nci_message_
Definition: nci_message.h:46
bool simple_status_response_is(uint8_t response) const
uint8_t get_oid() const
Definition: nci_message.cpp:41
void set_payload(const std::vector< uint8_t > &payload)
void set_message(uint8_t message_type, const std::vector< uint8_t > &payload)
bool oid_is(uint8_t oid) const
Definition: nci_message.cpp:95
uint8_t get_payload_size(bool recompute=false)
Definition: nci_message.cpp:43
void set_gid(uint8_t gid)
bool message_length_is(uint8_t message_length, bool recompute=false)
Definition: nci_message.cpp:78
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< uint8_t > encode()
Definition: nci_message.cpp:27
uint8_t get_simple_status_response() const
Definition: nci_message.cpp:53