ndn-embeds 0.1.0
Lightweight NDN protocol stack for embedded systems
Loading...
Searching...
No Matches
link.cpp
1#include "ndn/link.hpp"
2
3#include "ndn/tlv.hpp"
4
5namespace ndn {
6
7Link::Link(const Name& name) : name_(name) {}
8
9Link& Link::setName(const Name& name) {
10 name_ = name;
11 return *this;
12}
13
14Error Link::setName(std::string_view uri) {
15 auto result = Name::fromUri(uri);
16 if (!result.ok()) {
17 return result.error;
18 }
19 name_ = result.value;
20 return Error::Success;
21}
22
23Error Link::addDelegation(const Name& delegation) {
24 if (delegationCount_ >= LINK_MAX_DELEGATIONS) {
25 return Error::Full;
26 }
27
28 // Check for duplicates
29 for (size_t i = 0; i < delegationCount_; ++i) {
30 if (delegations_[i].equals(delegation)) {
31 return Error::InvalidParam;
32 }
33 }
34
35 delegations_[delegationCount_++] = delegation;
36 return Error::Success;
37}
38
39Error Link::addDelegation(std::string_view uri) {
40 auto result = Name::fromUri(uri);
41 if (!result.ok()) {
42 return result.error;
43 }
44 return addDelegation(result.value);
45}
46
47const Name* Link::delegation(size_t index) const {
48 if (index >= delegationCount_) {
49 return nullptr;
50 }
51 return &delegations_[index];
52}
53
55 delegationCount_ = 0;
56}
57
58bool Link::hasDelegation(const Name& name) const {
59 for (size_t i = 0; i < delegationCount_; ++i) {
60 if (delegations_[i].equals(name)) {
61 return true;
62 }
63 }
64 return false;
65}
66
67Error Link::toData(Data& data) const {
68 data.setName(name_);
69 data.setContentType(ContentType::Link);
70
71 if (delegationCount_ == 0) {
72 // At least one delegation is required
73 return Error::InvalidParam;
74 }
75
76 // Encode delegations (list of Names) into Content
77 uint8_t contentBuf[DATA_MAX_CONTENT_SIZE];
78 TlvEncoder encoder(contentBuf, sizeof(contentBuf));
79
80 for (size_t i = 0; i < delegationCount_; ++i) {
81 size_t nameLen = 0;
82 const Error err = delegations_[i].encode(encoder.current(), encoder.remaining(), nameLen);
83 if (err != Error::Success) {
84 return err;
85 }
86 encoder.setPosition(encoder.position() + nameLen);
87 }
88
89 const Error err = data.setContent(contentBuf, encoder.size());
90 if (err != Error::Success) {
91 return err;
92 }
93
94 return Error::Success;
95}
96
98 if (data.contentType() != ContentType::Link) {
99 return {.value = Link{}, .error = Error::InvalidPacket};
100 }
101
102 if (!data.hasContent()) {
103 return {.value = Link{}, .error = Error::InvalidPacket};
104 }
105
106 Link link;
107 link.name_ = data.name();
108
109 // Decode list of Names from Content
110 TlvDecoder decoder(data.content(), data.contentSize());
111
112 while (decoder.hasMore()) {
113 size_t bytesRead = 0;
114 auto nameResult = Name::fromWire(decoder.current(), decoder.remaining(), &bytesRead);
115 if (!nameResult.ok()) {
116 return {.value = Link{}, .error = nameResult.error};
117 }
118
119 const Error err = link.addDelegation(nameResult.value);
120 if (err != Error::Success) {
121 return {.value = Link{}, .error = err};
122 }
123
124 decoder.skip(bytesRead);
125 }
126
127 if (link.delegationCount_ == 0) {
128 return {.value = Link{}, .error = Error::InvalidPacket};
129 }
130
131 return {.value = link, .error = Error::Success};
132}
133
134} // namespace ndn
NDN Data packet.
Definition data.hpp:49
const Name & name() const
Get the Name (const reference)
Definition data.hpp:104
bool hasContent() const
Check if content is set.
Definition data.hpp:147
size_t contentSize() const
Get the content size.
Definition data.hpp:141
Data & setContentType(ContentType type)
Set the content type.
Definition data.cpp:54
const uint8_t * content() const
Get a pointer to the content data.
Definition data.hpp:135
Error setContent(const uint8_t *data, size_t size)
Set binary data as content.
Definition data.cpp:26
ContentType contentType() const
Get the content type.
Definition data.hpp:177
Data & setName(const Name &name)
Set the Name (supports method chaining)
Definition data.cpp:12
NDN Name class.
Definition name.hpp:64
static Result< Name > fromWire(const uint8_t *buf, size_t len, size_t *bytesRead=nullptr)
Decode a Name from TLV wire format.
Definition name.cpp:84
static Result< Name > fromUri(std::string_view uri)
Create a Name from a URI string.
Definition name.cpp:25
TLV decoder.
Definition tlv.hpp:235
const uint8_t * current() const
Pointer to current position.
Definition tlv.hpp:308
Error skip(size_t len)
Skip a specified number of bytes.
Definition tlv.cpp:248
size_t remaining() const
Remaining readable bytes.
Definition tlv.hpp:302
bool hasMore() const
Check if there is more data.
Definition tlv.hpp:314
TLV encoder.
Definition tlv.hpp:116
size_t position() const
Get current position.
Definition tlv.hpp:207
void setPosition(size_t pos)
Set current position.
Definition tlv.hpp:213
size_t size() const
Current write position (= number of bytes written)
Definition tlv.hpp:189
size_t remaining() const
Remaining writable bytes.
Definition tlv.hpp:195
uint8_t * current()
Pointer to current position.
Definition tlv.hpp:201
constexpr size_t DATA_MAX_CONTENT_SIZE
Maximum content size of a Data packet (bytes) ESP-NOW v2.0: max 1470 bytes - TLV overhead (approx.
Definition common.hpp:119
constexpr size_t LINK_MAX_DELEGATIONS
Maximum number of Names in a Link.
Definition common.hpp:89
Error
Error codes.
Definition common.hpp:24
Result type template.
Definition common.hpp:147
T value
Result value.
Definition common.hpp:148
NDN TLV (Type-Length-Value) encoding.