ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
nci_message.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/log.h"
5
6#include <initializer_list>
7#include <span>
8
9namespace esphome::nfc {
10
11// An NCI packet is a three-byte header followed by up to 255 payload bytes
12static constexpr size_t NCI_PKT_MAX_PAYLOAD_SIZE = 255;
13static constexpr size_t NCI_PKT_MAX_SIZE = 3 + NCI_PKT_MAX_PAYLOAD_SIZE;
14
17 public:
19
20 NciMessage() { this->reset(); }
21 NciMessage(uint8_t message_type, std::span<const uint8_t> payload);
22 NciMessage(uint8_t message_type, std::initializer_list<uint8_t> payload);
23 NciMessage(uint8_t message_type, uint8_t gid, uint8_t oid);
24 NciMessage(uint8_t message_type, uint8_t gid, uint8_t oid, std::span<const uint8_t> payload);
25 NciMessage(uint8_t message_type, uint8_t gid, uint8_t oid, std::initializer_list<uint8_t> payload);
26 explicit NciMessage(std::span<const uint8_t> raw_packet);
27
29 std::span<const uint8_t> encode();
30 void reset();
31
32 uint8_t get_message_type() const;
33 uint8_t get_gid() const;
34 uint8_t get_oid() const;
35 uint8_t get_payload_size(bool recompute = false);
36 uint8_t get_simple_status_response() const;
37 uint8_t get_message_byte(uint8_t offset) const;
38 Buffer &get_message() { return this->nci_message_; }
39 const Buffer &get_message() const { return this->nci_message_; }
41 std::span<const uint8_t> get_payload() const;
42
43 bool has_payload() const;
44 bool message_type_is(uint8_t message_type) const;
45 bool message_length_is(uint8_t message_length, bool recompute = false);
46 bool gid_is(uint8_t gid) const;
47 bool oid_is(uint8_t oid) const;
48 bool simple_status_response_is(uint8_t response) const;
49
50 void set_header(uint8_t message_type, uint8_t gid, uint8_t oid);
51 void set_message(uint8_t message_type, std::span<const uint8_t> payload);
52 void set_message(uint8_t message_type, uint8_t gid, uint8_t oid, std::span<const uint8_t> payload);
53 void set_message_type(uint8_t message_type);
54 void set_gid(uint8_t gid);
55 void set_oid(uint8_t oid);
56 void set_payload(std::span<const uint8_t> payload);
57 void set_payload(std::initializer_list<uint8_t> payload) {
58 this->set_payload(std::span<const uint8_t>(payload.begin(), payload.size()));
59 }
61 void append(std::span<const uint8_t> data);
62 void append(std::initializer_list<uint8_t> data) {
63 this->append(std::span<const uint8_t>(data.begin(), data.size()));
64 }
66 void set_payload_size(uint8_t size);
67
68 protected:
69 Buffer nci_message_; // MT/PBF/GID, OID, payload length, then the payload
70};
71
72} // namespace esphome::nfc
One NCI packet, held in a fixed buffer so building and reading messages never allocates.
Definition nci_message.h:16
uint8_t get_payload_size(bool recompute=false)
const Buffer & get_message() const
Definition nci_message.h:39
bool message_type_is(uint8_t message_type) const
uint8_t get_oid() const
bool simple_status_response_is(uint8_t response) const
uint8_t get_message_byte(uint8_t offset) const
std::span< const uint8_t > encode()
Stamps the payload length into the header and returns the packet ready to send.
bool gid_is(uint8_t gid) const
void set_payload(std::initializer_list< uint8_t > payload)
Definition nci_message.h:57
void set_gid(uint8_t gid)
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_payload_size(uint8_t size)
Sets the packet size to the header plus size payload bytes, for a bus driver filling the buffer direc...
std::span< const uint8_t > get_payload() const
The payload bytes that follow the header.
void append(std::initializer_list< uint8_t > data)
Definition nci_message.h:62
void set_oid(uint8_t oid)
void set_message(uint8_t message_type, std::span< const uint8_t > payload)
uint8_t get_gid() const
void set_message_type(uint8_t message_type)
uint8_t get_simple_status_response() const
bool oid_is(uint8_t oid) const
void set_payload(std::span< const uint8_t > payload)
uint8_t get_message_type() const
void append(std::span< const uint8_t > data)
Appends bytes to the payload; bytes that do not fit are dropped.
uint16_t size
Definition helpers.cpp:34