LibLogicalAccess  2.5.0
An Open Source RFID Library
Loading...
Searching...
No Matches
utils.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <logicalaccess/lla_core_api.hpp>
5#include <bitset>
6#include <chrono>
7#include <cstddef>
8#include <cstdint>
9
14namespace logicalaccess
15{
16LLA_CORE_API uint32_t lla_htonl(uint32_t in);
17LLA_CORE_API uint16_t lla_htons(uint16_t in);
18
25class LLA_CORE_API ElapsedTimeCounter
26{
27 public:
29
34 size_t elapsed() const;
35
40 size_t elapsed_micro() const;
41
42 private:
43 using TimePoint = std::chrono::steady_clock::time_point;
44
46};
47
48template <typename T>
50
51template <size_t Len>
52struct GetBitSetSize<std::bitset<Len>>
53{
54 enum
55 {
56 Length = Len
57 };
58};
59
64template <size_t Val>
66{
67 enum
68 {
69 Value = Val * 2
70 };
71};
72
77template <typename BitSet>
78ByteVector bitsetToVector(const BitSet &in)
79{
80 static_assert(GetBitSetSize<BitSet>::Length % 8 == 0, "Cannot convert a bitset "
81 "whose length is not modulo 8");
82 const size_t len = GetBitSetSize<BitSet>::Length / 8;
83 ByteVector out(len);
84
85 size_t count = 0;
86 for (size_t i = 0; i < len; ++i)
87 {
88 uint8_t b = 0;
89 for (int j = 0; j < 8; ++j)
90 {
91 b |= in[count] << (7 - j);
92 count++;
93 }
94 out[i] = b;
95 }
96 return out;
97}
98
104class LLA_CORE_API ManchesterEncoder
105{
106 public:
107 enum Type
108 {
110 IEEE_802
111 };
112
116 template <typename BitSet>
117 static std::bitset<DoubleValue<GetBitSetSize<BitSet>::Length>::Value>
118 encode(const BitSet &in, Type t)
119 {
120 std::bitset<GetBitSetSize<BitSet>::Length * 2> out;
121
122 for (size_t count = 0, read_count = 0; count < GetBitSetSize<BitSet>::Length * 2;
123 read_count++)
124 {
125 if ((in[read_count] && t == IEEE_802) || (!in[read_count] && t == G_E_THOMAS))
126 {
127 out[count++] = 0;
128 out[count++] = 1;
129 }
130 else
131 {
132 out[count++] = 1;
133 out[count++] = 0;
134 }
135 }
136 return out;
137 }
138
139 static ByteVector encode(const ByteVector &in, Type t);
140 static ByteVector decode(const ByteVector &in, Type t);
141};
142
143LLA_CORE_API int portable_setenv(const char *name, const char *value, int overwrite);
144LLA_CORE_API std::string base64_encode(unsigned char const *, unsigned int len);
145LLA_CORE_API std::string base64_decode(std::string const &s);
146}
Definition: utils.hpp:26
TimePoint creation_
Definition: utils.hpp:45
std::chrono::steady_clock::time_point TimePoint
Definition: utils.hpp:43
Definition: utils.hpp:105
static std::bitset< DoubleValue< GetBitSetSize< BitSet >::Length >::Value > encode(const BitSet &in, Type t)
Definition: utils.hpp:118
Type
Definition: utils.hpp:108
@ G_E_THOMAS
Definition: utils.hpp:109
std::vector< uint8_t > ByteVector
Definition: lla_fwd.hpp:80
Definition: asn1.hpp:9
LLA_CORE_API std::string base64_encode(unsigned char const *, unsigned int len)
Definition: utils.cpp:180
LLA_CORE_API int portable_setenv(const char *name, const char *value, int overwrite)
Definition: utils.cpp:153
LLA_CORE_API std::string base64_decode(std::string const &s)
Definition: utils.cpp:228
LLA_CORE_API uint32_t lla_htonl(uint32_t in)
Definition: utils.cpp:10
ByteVector bitsetToVector(const BitSet &in)
Definition: utils.hpp:78
LLA_CORE_API uint16_t lla_htons(uint16_t in)
Definition: utils.cpp:15
Definition: urirecord.hpp:30
Definition: utils.hpp:66
@ Value
Definition: utils.hpp:69
Definition: utils.hpp:49