esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
nni.hpp
Go to the documentation of this file.
1#ifndef NDNPH_TLV_NNI_HPP
2#define NDNPH_TLV_NNI_HPP
3
4#include "decoder.hpp"
5#include "encoder.hpp"
6
7namespace ndnph {
8namespace tlv {
9namespace detail {
10
11template<typename T>
12class NNIValue {
13public:
14 static bool decode(const Decoder::Tlv& d, T& value) {
15 if (d.length != sizeof(T)) {
16 return false;
17 }
18 value = readValue(d.value);
19 return true;
20 }
21
22 explicit NNIValue(T number)
23 : m_number(number) {}
24
25 void encodeTo(Encoder& encoder) const {
26 uint8_t* room = encoder.prependRoom(sizeof(m_number));
27 if (room != nullptr) {
28 writeValue(room, m_number);
29 }
30 }
31
32 static T readValue(const uint8_t* input);
33
34 static void writeValue(uint8_t* room, T n);
35
36private:
37 T m_number = 0;
38};
39
40template<>
41inline uint8_t
42NNIValue<uint8_t>::readValue(const uint8_t* input) {
43 return input[0];
44}
45
46template<>
47inline void
48NNIValue<uint8_t>::writeValue(uint8_t* room, uint8_t n) {
49 room[0] = n;
50}
51
52template<>
53inline uint16_t
54NNIValue<uint16_t>::readValue(const uint8_t* input) {
55 return (static_cast<uint16_t>(input[0]) << 8) | static_cast<uint16_t>(input[1]);
56}
57
58template<>
59inline void
60NNIValue<uint16_t>::writeValue(uint8_t* room, uint16_t n) {
61 room[0] = n >> 8;
62 room[1] = n;
63}
64
65template<>
66inline uint32_t
67NNIValue<uint32_t>::readValue(const uint8_t* input) {
68 return (static_cast<uint32_t>(input[0]) << 24) | (static_cast<uint32_t>(input[1]) << 16) |
69 (static_cast<uint32_t>(input[2]) << 8) | static_cast<uint32_t>(input[3]);
70}
71
72template<>
73inline void
74NNIValue<uint32_t>::writeValue(uint8_t* room, uint32_t n) {
75 room[0] = n >> 24;
76 room[1] = n >> 16;
77 room[2] = n >> 8;
78 room[3] = n;
79}
80
81template<>
82inline uint64_t
83NNIValue<uint64_t>::readValue(const uint8_t* input) {
84 return (static_cast<uint64_t>(input[0]) << 56) | (static_cast<uint64_t>(input[1]) << 48) |
85 (static_cast<uint64_t>(input[2]) << 40) | (static_cast<uint64_t>(input[3]) << 32) |
86 (static_cast<uint64_t>(input[4]) << 24) | (static_cast<uint64_t>(input[5]) << 16) |
87 (static_cast<uint64_t>(input[6]) << 8) | static_cast<uint64_t>(input[7]);
88}
89
90template<>
91inline void
92NNIValue<uint64_t>::writeValue(uint8_t* room, uint64_t n) {
93 room[0] = n >> 56;
94 room[1] = n >> 48;
95 room[2] = n >> 40;
96 room[3] = n >> 32;
97 room[4] = n >> 24;
98 room[5] = n >> 16;
99 room[6] = n >> 8;
100 room[7] = n;
101}
102
103} // namespace detail
104
107
110
113
116
118class NNI {
119public:
125 template<typename I, typename Limit = typename std::enable_if<std::is_integral<I>::value,
126 std::numeric_limits<I>>::type>
127 static bool decode(const Decoder::Tlv& d, I& value, uint64_t max = Limit::max()) {
128 uint64_t n = 0;
129 switch (d.length) {
130 case 1:
131 n = NNI1::readValue(d.value);
132 break;
133 case 2:
134 n = NNI2::readValue(d.value);
135 break;
136 case 4:
137 n = NNI4::readValue(d.value);
138 break;
139 case 8:
140 n = NNI8::readValue(d.value);
141 break;
142 default:
143 return false;
144 }
145 value = n;
146 return n <= max;
147 }
148
149 explicit NNI(uint64_t number)
150 : m_number(number) {}
151
152 void encodeTo(Encoder& encoder) const {
153 if (m_number <= std::numeric_limits<uint8_t>::max()) {
154 NNI1(m_number).encodeTo(encoder);
155 } else if (m_number <= std::numeric_limits<uint16_t>::max()) {
156 NNI2(m_number).encodeTo(encoder);
157 } else if (m_number <= std::numeric_limits<uint32_t>::max()) {
158 NNI4(m_number).encodeTo(encoder);
159 } else {
160 NNI8(m_number).encodeTo(encoder);
161 }
162 }
163
164private:
165 uint64_t m_number = 0;
166};
167
169template<typename N = NNI>
171public:
172 template<typename I>
173 explicit NniElement(uint32_t type, I value)
174 : m_type(type)
175 , m_nni(value) {}
176
177 void encodeTo(Encoder& encoder) const {
178 encoder.prependTlv(m_type, m_nni);
179 }
180
181private:
182 uint32_t m_type;
183 N m_nni;
184};
185
186} // namespace tlv
187} // namespace ndnph
188
189#endif // NDNPH_TLV_NNI_HPP
Decoded TLV.
Definition decoder.hpp:13
size_t length
Definition decoder.hpp:39
const uint8_t * value
Definition decoder.hpp:40
TLV encoder that accepts items in reverse order.
Definition encoder.hpp:10
uint8_t * prependRoom(size_t size)
Make room to prepend an object.
Definition encoder.hpp:90
bool prependTlv(uint32_t type, OmitEmptyTag omitEmpty, const Arg &... arg)
Prepend TLV, measuring TLV-LENGTH automatically.
Definition encoder.hpp:143
NonNegativeInteger encoding.
Definition nni.hpp:118
void encodeTo(Encoder &encoder) const
Definition nni.hpp:152
static bool decode(const Decoder::Tlv &d, I &value, uint64_t max=Limit::max())
Decode NonNegativeInteger.
Definition nni.hpp:127
NNI(uint64_t number)
Definition nni.hpp:149
Encode to a TLV element where TLV-VALUE is a NonNegativeInteger.
Definition nni.hpp:170
void encodeTo(Encoder &encoder) const
Definition nni.hpp:177
NniElement(uint32_t type, I value)
Definition nni.hpp:173
Definition nni.hpp:12
static bool decode(const Decoder::Tlv &d, T &value)
Definition nni.hpp:14
void encodeTo(Encoder &encoder) const
Definition nni.hpp:25
static void writeValue(uint8_t *room, T n)
NNIValue(T number)
Definition nni.hpp:22
static T readValue(const uint8_t *input)
detail::NNIValue< uint32_t > NNI4
4-byte number encoding.
Definition nni.hpp:112
detail::NNIValue< uint16_t > NNI2
2-byte number encoding.
Definition nni.hpp:109
detail::NNIValue< uint64_t > NNI8
8-byte number encoding.
Definition nni.hpp:115
detail::NNIValue< uint8_t > NNI1
1-byte number encoding.
Definition nni.hpp:106
Definition fs.hpp:33