8#if __has_include("SNMPcfg.h")
27#define SNMP_CAPACITY 6
235 static void encode7bits(uint32_t value, Stream &stream,
const uint8_t size) {
236 for (uint8_t index = 0; index < size; ++index) {
237 stream.write((value >> (7 * (size - index - 1)) & 0x7F) | ((size - index - 1) ? 0x80 : 0x00));
254 void encode7bits(uint32_t value, uint8_t *buffer,
const uint8_t size) {
256 for (uint8_t index = 0; index < size; ++index) {
257 *buffer-- = (value & 0x7F) | (index ? 0x80 : 0x00);
329 Type(
const unsigned int type = 0) :
342 Type(
const uint8_t flag,
const unsigned int tag) :
374 _type = stream.read();
383 _type |= stream.read();
386 }
while (
_type & 0x80);
408 return buffer +
_size;
422 if ((
_tag & 0x1F) == 0x1F) {
429 }
while (
_type & 0x80);
442 operator unsigned int()
const {
501 for (uint8_t index = 0; index <
_size - 1; ++index) {
534 for (int32_t index =
_size - 2; index > -1; --index) {
536 _type |= tag >> (index << 3) & 0x7F;
537 _type |= index ? 0x80 : 0x00;
574 stream.write(0x80 |
_size);
576 for (uint8_t index = 0; index <
_size; ++index) {
577 stream.write(length >> ((
_size - index - 1) << 3));
594 for (uint8_t index = 0; index <
_size; ++index) {
616 uint8_t *pointer = buffer;
618 *pointer = 0x80 |
_size;
621 for (uint8_t index = 0; index <
_size; ++index) {
628 return buffer +
_size;
638 uint8_t *pointer = buffer;
643 for (uint8_t index = 0; index <
_size; ++index) {
664 operator unsigned int()
const {
799 BER(
const unsigned int type) :
836 for (uint8_t index = 0; index <
_length; ++index) {
837 stream.write(value >> ((
_length - index - 1) << 3));
870 *value = stream.peek() & 0x80 ? 0xFFFFFFFF : 0;
874 for (uint8_t index = 0; index <
_length; ++index) {
876 *value |=
static_cast<uint8_t
>(stream.read());
888 virtual uint8_t*
encode(uint8_t *buffer) {
907 for (uint8_t index = 0; index <
_length; ++index) {
908 *pointer++ = value >> ((
_length - index - 1) << 3);
921 virtual uint8_t*
decode(uint8_t *buffer) {
943 *value = *pointer & 0x80 ? 0xFFFFFFFF : 0;
947 for (
unsigned int index = 0; index <
_length; ++index) {
949 *value |=
static_cast<uint8_t
>(*pointer++);
963 unsigned int length =
sizeof(T);
964 for (; length > 1; --length) {
965 uint16_t word = value >> ((length - 2) << 3);
966 if ((word & 0xFF80) != 0xFF80) {
981 unsigned int length = 0;
984 carry = value & 0x80;
987 }
while (value | carry);
1021 virtual const unsigned int getSize(
const bool refresh =
false) {
1080 stream.write(
_value ? 0xFF : 0x00);
1108 *pointer =
_value ? 0xFF : 0x00;
1109 return buffer +
SIZE;
1124 return buffer +
SIZE;
1403 void setValue(
const char *value,
const unsigned int length) {
1406 memcpy(
_value, value, length);
1415 const bool getBit(
const unsigned int index)
const {
1416 const unsigned int byte = index / 8;
1417 const uint8_t bit = index % 8;
1418 return _value[byte] & (0x80 >> bit);
1514 unsigned int index = 0;
1515 uint32_t subidentifier = 0;
1517 char *token =
const_cast<char*
>(
_value.c_str());
1518 while (token != NULL) {
1521 subidentifier = atoi(token);
1524 subidentifier = subidentifier * 40 + atoi(++token);
1525 stream.write(subidentifier);
1530 subidentifier = atol(++token);
1531 uint32_t value = subidentifier;
1532 uint32_t length = 0;
1542 token = strchr(token,
'.');
1557 unsigned int index = 0;
1558 uint32_t subidentifier = 0;
1560 unsigned int length =
_length;
1568 byte = stream.read();
1569 subidentifier <<= 7;
1570 subidentifier +=
byte & 0x7F;
1571 }
while (
byte & 0x80);
1572 _value +=
"." + String(subidentifier);
1575 subidentifier = stream.read();
1576 _value = String(subidentifier / 40) +
"." + String(subidentifier % 40);
1591 unsigned int index = 0;
1592 uint32_t subidentifier = 0;
1594 char *token =
const_cast<char*
>(
_value.c_str());
1595 while (token != NULL) {
1598 subidentifier = atoi(token);
1601 subidentifier = subidentifier * 40 + atoi(++token);
1602 *pointer = subidentifier;
1606 subidentifier = atol(++token);
1607 uint32_t value = subidentifier;
1608 uint32_t length = 0;
1618 token = strchr(token,
'.');
1634 unsigned int index = 0;
1635 uint32_t subidentifier = 0;
1637 uint8_t *end = pointer +
_length;
1642 subidentifier <<= 7;
1643 subidentifier += *pointer & 0x7F;
1644 }
while (*pointer++ & 0x80);
1645 _value +=
"." + String(subidentifier);
1647 subidentifier = *pointer++;
1648 _value = String(subidentifier / 40) +
"."
1649 + String(subidentifier % 40);
1651 }
while (pointer < end);
1674 unsigned int index = 0;
1675 uint32_t subidentifier = 0;
1676 char *token =
const_cast<char*
>(
_value.c_str());
1677 while (token != NULL) {
1680 subidentifier = atoi(token);
1683 subidentifier = subidentifier * 40 + atoi(++token);
1687 subidentifier = atol(++token);
1689 subidentifier >>= 7;
1691 }
while (subidentifier);
1695 token = strchr(token,
'.');
1714template<const u
int8_t U>
1733 for (
auto ber :
_bers) {
1738 for (uint8_t index = 0; index <
_count; ++index) {
1739 delete _bers[index];
1755 for (uint8_t index = 0; index <
_count; ++index) {
1756 _bers[index]->encode(stream);
1778 unsigned int length =
_length;
1806 for (
auto ber :
_bers) {
1807 pointer = ber->encode(pointer);
1810 for (uint8_t index = 0; index <
_count; ++index) {
1811 pointer =
_bers[index]->encode(pointer);
1829 uint8_t *end = pointer +
_length;
1836 pointer = ber->
decode(pointer);
1839 }
while (pointer < end);
1859 virtual const unsigned int getSize(
const bool refresh =
false) {
1862 for (uint8_t index = 0; index <
_count; ++index) {
1878 return _bers[index];
1901 _bers.push_back(ber);
2066 for (uint8_t index = 0; index <
LENGTH; ++index) {
2067 _value[index] = value[index];
2077 return reinterpret_cast<uint8_t*
>(
_value);
2401 uint8_t *end = pointer +
_length;
2410 }
while (pointer < end);
2430 virtual const unsigned int getSize(
const bool refresh =
false) {
2524 *(
reinterpret_cast<uint32_t*
>(&
_value)), buffer);
2538 reinterpret_cast<uint32_t*
>(&
_value), buffer);
Base class for BER array of BERs.
virtual void decode(Stream &stream, const uint8_t flag=Flag::None)
Decodes ArrayBER from stream.
void remove()
Removes the last BER in the array.
BER * operator[](const unsigned int index)
Array subscript operator.
const uint8_t count() const
Gets the count of BERs in the array.
std::vector< BER * > _bers
BER * add(BER *ber)
Adds a BER to the array.
~ArrayBER()
ArrayBER destructor.
ArrayBER(const uint8_t type)
Creates an ArrayBER.
virtual uint8_t * encode(uint8_t *buffer)
Encodes ArrayBER to memory buffer.
virtual void encode(Stream &stream)
Encodes ArrayBER to stream.
virtual uint8_t * decode(uint8_t *buffer)
Decodes ArrayBER from memory buffer.
virtual const unsigned int getSize(const bool refresh=false)
Gets the size of the ArrayBER.
virtual void encode(Stream &stream)
Encodes BER type and length to stream.
virtual const unsigned int getSize(const bool refresh=false)
Gets the size of the BER.
virtual void decode(Stream &stream, const uint8_t flag=Flag::None)
Decodes BER type and length from stream.
void decodeNumeric(T *value, Stream &stream, const uint8_t flag=Flag::None)
Decodes BER numeric value from stream.
const unsigned int getType() const
Gets the BER type.
uint8_t * encodeNumeric(T value, uint8_t *buffer)
Encodes BER numeric value to memory buffer.
void setNegative(T value)
Computes BER length of a negative integer.
uint8_t * decodeNumeric(T *value, uint8_t *buffer, const uint8_t flag=Flag::None)
Decodes BER numeric value from memory buffer.
virtual uint8_t * encode(uint8_t *buffer)
Encodes BER type and length to memory buffer.
virtual ~BER()
BER destructor.
BER * create(const Type &type)
Creates a BER of given type.
virtual uint8_t * decode(uint8_t *buffer)
Decodes BER type and length from memory buffer.
BER(const unsigned int type)
Creates a BER object.
const unsigned int getLength() const
Gets the BER length.
void setPositive(T value)
Computes BER length of a positive integer.
void encodeNumeric(T value, Stream &stream)
Encodes BER numeric value to stream.
Base class for BER, Length and Type.
static void encode7bits(uint32_t value, Stream &stream, const uint8_t size)
Encodes integer value to stream.
void encode7bits(uint32_t value, uint8_t *buffer, const uint8_t size)
Encodes integer value to memory buffer.
BER object to handle boolean.
BooleanBER(const bool value)
Creates a BooleanBER object.
virtual void decode(Stream &stream, const uint8_t flag=Flag::None)
Decodes BooleanBER from stream.
virtual void encode(Stream &stream)
Encodes BooleanBER to stream.
virtual uint8_t * decode(uint8_t *buffer)
Decodes BooleanBER from memory buffer.
virtual uint8_t * encode(uint8_t *buffer)
Encodes BooleanBER to memory buffer.
static constexpr uint8_t SIZE
void setValue(const bool value)
Sets the BooleanBER value.
const bool getValue() const
Gets the BooleanBER value.
static constexpr uint8_t LENGTH
BER object to handle 32-bit counter.
Counter32BER(const uint32_t value)
Creates a Counter32BER object.
BER object to handle 64-bit counter.
Counter64BER(const uint64_t value)
Creates a Counter64BER object.
BER object to handle endOfMIBView exception.
EndOfMIBViewBER()
Creates a EndOfMIBViewBER object.
Helper class for internal flag.
BER object to handle float.
const float getValue() const
Gets the FloatBER value.
FloatBER(const float value, const unsigned int type=Type::Float)
Creates a FloatBER object.
virtual void decode(Stream &stream, const uint8_t flag=Flag::None)
Decodes FloatBER from stream.
virtual void encode(Stream &stream)
Encodes FloatBER to stream.
static constexpr uint8_t LENGTH
void setValue(const float value)
Sets the FloatBER value.
virtual uint8_t * decode(uint8_t *buffer)
Decodes FloatBER from memory buffer.
virtual uint8_t * encode(uint8_t *buffer)
Encodes FloatBER to memory buffer.
BER object to handle 32-bit gauge.
Gauge32BER(const uint32_t value)
Creates a Gauge32BER object.
BER object to handle IP address.
IPAddressBER(IPAddress value)
Creates an IPAddressBER object.
const uint8_t * getValue()
Gets the IPAddressBER value.
static constexpr uint8_t LENGTH
BER object to handle integer.
const int32_t getValue() const
Gets the IntegerBER value.
virtual void decode(Stream &stream, const uint8_t flag=Flag::None)
Decodes IntegerBER from stream.
virtual void encode(Stream &stream)
Encodes IntegerBER to stream.
virtual uint8_t * decode(uint8_t *buffer)
Decodes IntegerBER from memory buffer.
IntegerBER(const int32_t value)
Creates an IntegerBER object.
virtual uint8_t * encode(uint8_t *buffer)
Encodes IntegerBER to memory buffer.
void setValue(const int32_t value)
Sets the IntegerBER value.
Length & operator+=(const unsigned int length)
Addition operator.
void setLength(unsigned int length)
Sets length from integer.
Length & operator-=(const unsigned int length)
Subtraction operator.
uint8_t * encode(uint8_t *buffer)
Encodes BER length to memory buffer.
void decode(Stream &stream)
Decodes BER length from stream.
uint8_t * decode(uint8_t *buffer)
Decodes BER length from memory buffer.
Length & operator=(const unsigned int length)
Unsigned int assignment operator.
Length & operator++()
Prefix increment operator.
Length(unsigned int length=0)
Creates a BER length from a length value.
void encode(Stream &stream)
Encodes BER length to stream.
BER object to handle noSuchInstance exception.
NoSuchInstanceBER()
Creates a NoSuchInstanceBER object.
BER object to handle noSuchObject exception.
NoSuchObjectBER()
Creates a NoSuchObjectBER object.
BER object to handle null.
NullBER(const uint8_t type=Type::Null)
Creates a NullBER object.
BER object to handle OID.
virtual uint8_t * encode(uint8_t *buffer)
Encodes ObjectIdentifierBER to memory buffer.
virtual void decode(Stream &stream, const uint8_t flag=Flag::None)
Decodes ObjectIdentifierBER from stream.
void setValue(const char *value)
Set the ObjectIdentifierBER value.
const char * getValue() const
Gets the ObjectIdentifierBER value.
virtual void encode(Stream &stream)
Encodes ObjectIdentifierBER to stream.
virtual uint8_t * decode(uint8_t *buffer)
Decodes ObjectIdentifierBER from memory buffer.
ObjectIdentifierBER(const char *value)
Creates an ObjectIdentifierBER object.
BER object to handle octet string.
OctetStringBER(const char *value)
Creates an OctetStringBER object.
virtual uint8_t * decode(uint8_t *buffer)
Decodes OctetStringBER from memory buffer.
virtual void decode(Stream &stream, const uint8_t flag=Flag::None)
Decodes OctetStringBER from stream.
OctetStringBER(const uint8_t type=Type::OctetString)
Creates an empty OctetStringBER object.
OctetStringBER(const char *value, const uint32_t length)
Creates an OctetStringBER object.
virtual uint8_t * encode(uint8_t *buffer)
Encodes OctetStringBER to memory buffer.
const char * getValue() const
Gets the OctetStringBER value.
const bool getBit(const unsigned int index) const
Gets bit at a given index in the OctetStringBER value.
void allocate()
Allocates the char array.
void setValue(const char *value, const unsigned int length)
Sets the OctetStringBER value.
virtual ~OctetStringBER()
OctetStringBER destructor.
virtual void encode(Stream &stream)
Encodes OctetStringBER to stream.
BER object to handle embedded BER object.
virtual uint8_t * decode(uint8_t *buffer)
Decodes OpaqueBER from memory buffer.
virtual const unsigned int getSize(const bool refresh=false)
Gets the size of the OpaqueBER.
OpaqueBER(BER *ber)
Creates an OpaqueBER.
BER * getBER()
Gets the embedded BER.
virtual ~OpaqueBER()
OpaqueBER destructor.
virtual void encode(Stream &stream)
Encodes OpaqueBER to stream.
virtual uint8_t * encode(uint8_t *buffer)
Encodes OpaqueBER to memory buffer.
virtual void decode(Stream &stream, const uint8_t flag=Flag::None)
Decodes OpaqueBER from stream.
BER object to handle float.
OpaqueFloatBER(const float value)
Creates an OpaqueFloatBER object.
BER object to handle sequence of BER objects.
SequenceBER(const uint8_t type=Type::Sequence)
Creates a SequenceBER.
BER object to handle time.
TimeTicksBER(const uint32_t value)
Creates a TimeTicksBER object.
void setType(unsigned int type)
Sets BER type from type value.
Type(const uint8_t flag, const unsigned int tag)
Creates a BER type from a composite type value.
const uint8_t getClass() const
Gets class from BER type.
void decode(Stream &stream)
Decodes BER type from stream.
uint8_t * encode(uint8_t *buffer)
Encodes BER type to memory buffer.
uint8_t * decode(uint8_t *buffer)
Decodes BER type from memory buffer.
Type(const unsigned int type=0)
Creates a BER type from a type value.
void encode(Stream &stream)
Encodes BER type to stream.
const unsigned int getTag() const
Gets tag from BER type.
const uint8_t getForm() const
Gets form from BER type.
void setFlagTag(uint8_t flag, unsigned int tag)
Creates a BER type from a composite type value.
Base class for unsigned integer BER.
virtual uint8_t * decode(uint8_t *buffer)
Decodes UIntegerBER from memory buffer.
void setValue(const T value)
Sets the UIntegerBER value.
const T getValue() const
Gets the UIntegerBER value.
virtual void decode(Stream &stream, const uint8_t flag=Flag::None)
Decodes UIntegerBER from stream.
virtual uint8_t * encode(uint8_t *buffer)
Encodes UIntegerBER to memory buffer.
UIntegerBER(const T value, const uint8_t type)
Creates an UIntegerBER.
virtual void encode(Stream &stream)
Encodes UIntegerBER to stream.
BER object to handle variable binding.
BER * getValue() const
Gets variable binding value.
VarBind(const char *oid, BER *value=nullptr)
Creates a VarBind.
const char * getName() const
Gets variable binding name.
BER object to handle a list of variable bindings.
VarBind * operator[](const uint32_t index)
Array subscript operator.
Helper struct to handle class of BER type.
Helper struct to handle SNMP error.
Error()
Initializes to default values.
Helper struct to handle trap PDU.
Trap()
Initializes to default values.
Helper struct to handle SNMP versions.