esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
convention.hpp
Go to the documentation of this file.
1#ifndef NDNPH_PACKET_CONVENTION_HPP
2#define NDNPH_PACKET_CONVENTION_HPP
3
4#include "../port/random/port.hpp"
5#include "../port/unixtime/port.hpp"
6#include "../tlv/nni.hpp"
7#include "component.hpp"
8
9namespace ndnph {
10namespace convention {
11
14public:
19 std::pair<bool, uint64_t> toNumber() const {
20 uint64_t value = 0;
21 bool ok = port::RandomSource::generate(reinterpret_cast<uint8_t*>(&value), sizeof(value));
22 return std::make_pair(ok, value);
23 }
24};
25
27class TimeValue {
28public:
29 enum Unit {
30 Seconds = 1000000,
33 };
34
41 explicit TimeValue(uint64_t t = 0, uint64_t unit = Microseconds, bool allowFallback = false)
42 : m_t(t)
43 , m_unit(unit)
44 , m_allowFallback(allowFallback) {}
45
50 std::pair<bool, uint64_t> toNumber() const {
51 uint64_t t = m_t;
52 if (t == 0) {
53 t = port::UnixTime::now();
54 if (!port::UnixTime::valid(t)) {
55 if (m_allowFallback) {
56 return RandomValue().toNumber();
57 } else {
58 return std::make_pair(false, 0);
59 }
60 }
61 }
62 return std::make_pair(true, t / m_unit);
63 }
64
65private:
66 uint64_t m_t;
67 uint64_t m_unit;
68 bool m_allowFallback;
69};
70
71namespace detail {
72
73template<uint16_t tlvType>
75public:
76 static Component create(Region& region, const uint8_t digest[NDNPH_SHA256_LEN]) {
77 return Component(region, tlvType, NDNPH_SHA256_LEN, digest);
78 }
79
80 static bool match(const Component& comp) {
81 return comp.type() == tlvType && comp.length() == NDNPH_SHA256_LEN;
82 }
83
84 static const uint8_t* parse(const Component& comp) {
85 return comp.value();
86 }
87};
88
89template<uint16_t tlvType>
91public:
92 static Component create(Region& region, const char* s) {
93 return Component(region, tlvType, std::strlen(s), reinterpret_cast<const uint8_t*>(s));
94 }
95
96 static bool match(const Component& comp) {
97 return comp.type() == tlvType;
98 }
99
100 static const char* parse(const Component& comp, Region& region) {
101 uint8_t* room = region.alloc(comp.length() + 1);
102 if (room == nullptr) {
103 return nullptr;
104 }
105 std::copy_n(comp.value(), comp.length(), room)[0] = 0;
106 return reinterpret_cast<const char*>(room);
107 }
108};
109
110template<uint16_t tlvType>
112public:
114 static Component create(Region& region, uint64_t value) {
115 return Component::from(region, tlvType, tlv::NNI(value));
116 }
117
125 template<typename G>
126 static Component create(Region& region, const G& gen, decltype(&G::toNumber) = nullptr) {
127 bool ok = false;
128 uint64_t value = 0;
129 std::tie(ok, value) = gen.toNumber();
130 if (!ok) {
131 return Component();
132 }
133 return create(region, value);
134 }
135
136 static bool match(const Component& comp) {
137 return parseImpl(comp).first;
138 }
139
140 static uint64_t parse(const Component& comp) {
141 return parseImpl(comp).second;
142 }
143
144private:
145 static std::pair<bool, uint64_t> parseImpl(const Component& comp) {
146 Decoder::Tlv d;
147 uint64_t value = 0;
148 bool ok = comp.type() == tlvType && Decoder::readTlv(d, comp.tlv(), comp.tlv() + comp.size()) &&
149 tlv::NNI::decode(d, value);
150 return std::make_pair(ok, value);
151 }
152};
153
154} // namespace detail
155
168
175
191
198
210
217
224
235
242
243} // namespace convention
244} // namespace ndnph
245
246#endif // NDNPH_PACKET_CONVENTION_HPP
static bool generate(uint8_t *output, size_t count)
Name component.
Definition component.hpp:16
size_t length() const
Definition component.hpp:145
uint16_t type() const
Definition component.hpp:141
size_t size() const
Definition component.hpp:157
static Component from(Region &region, uint16_t type, const Arg &... arg)
Construct from TLV-TYPE, and several arguments to be encoded to TLV-VALUE.
Definition component.hpp:75
const uint8_t * value() const
Definition component.hpp:149
const uint8_t * tlv() const
Definition component.hpp:153
Decoded TLV.
Definition decoder.hpp:13
static bool readTlv(Tlv &d, const uint8_t *input, const uint8_t *end)
Definition decoder.hpp:46
Region-based memory allocator thats owns memory of NDNph objects.
Definition region.hpp:9
uint8_t * alloc(size_t size)
Allocate a buffer with no alignment requirement.
Definition region.hpp:27
Indicate that TLV-VALUE should be a random number.
Definition convention.hpp:13
std::pair< bool, uint64_t > toNumber() const
Generate TLV-VALUE.
Definition convention.hpp:19
Indicate that TLV-VALUE should be a timestamp.
Definition convention.hpp:27
Unit
Definition convention.hpp:29
@ Microseconds
Definition convention.hpp:32
@ Milliseconds
Definition convention.hpp:31
@ Seconds
Definition convention.hpp:30
std::pair< bool, uint64_t > toNumber() const
Generate TLV-VALUE.
Definition convention.hpp:50
TimeValue(uint64_t t=0, uint64_t unit=Microseconds, bool allowFallback=false)
Constructor.
Definition convention.hpp:41
Definition convention.hpp:74
static Component create(Region &region, const uint8_t digest[NDNPH_SHA256_LEN])
Definition convention.hpp:76
static bool match(const Component &comp)
Definition convention.hpp:80
static const uint8_t * parse(const Component &comp)
Definition convention.hpp:84
Definition convention.hpp:111
static Component create(Region &region, const G &gen, decltype(&G::toNumber)=nullptr)
Create with RandomValue or TimeValue.
Definition convention.hpp:126
static bool match(const Component &comp)
Definition convention.hpp:136
static Component create(Region &region, uint64_t value)
Create with specified value.
Definition convention.hpp:114
static uint64_t parse(const Component &comp)
Definition convention.hpp:140
Definition convention.hpp:90
static bool match(const Component &comp)
Definition convention.hpp:96
static const char * parse(const Component &comp, Region &region)
Definition convention.hpp:100
static Component create(Region &region, const char *s)
Definition convention.hpp:92
NonNegativeInteger encoding.
Definition nni.hpp:118
static bool decode(const Decoder::Tlv &d, I &value, uint64_t max=Limit::max())
Decode NonNegativeInteger.
Definition nni.hpp:127
#define NDNPH_SHA256_LEN
SHA256 digest length.
Definition common.hpp:34
Definition fs.hpp:33