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 - 1);
576 for (uint8_t index = 0; index <
_size - 1; ++index) {
577 stream.write(length >> ((
_size - index - 2) << 3));
595 for (uint8_t index = 0; index <
_size; ++index) {
615 uint8_t *pointer = buffer;
617 *pointer = 0x80 |
_size - 1;
618 pointer +=
_size - 1;
621 for (uint8_t index = 0; index <
_size - 1; ++index) {
628 return buffer +
_size;
638 uint8_t *pointer = buffer;
643 for (uint8_t index = 0; index <
_size; ++index) {
665 operator unsigned int()
const {
800 BER(
const unsigned int type) :
821 _type.encode(stream);
837 for (uint8_t index = 0; index <
_length; ++index) {
838 stream.write(value >> ((
_length - index - 1) << 3));
852 _type.decode(stream);
871 *value = stream.peek() & 0x80 ? 0xFFFFFFFF : 0;
875 for (uint8_t index = 0; index <
_length; ++index) {
877 *value |=
static_cast<uint8_t
>(stream.read());
889 virtual uint8_t*
encode(uint8_t *buffer) {
890 uint8_t *pointer =
_type.encode(buffer);
891 pointer =
_length.encode(pointer);
908 for (uint8_t index = 0; index <
_length; ++index) {
909 *pointer++ = value >> ((
_length - index - 1) << 3);
922 virtual uint8_t*
decode(uint8_t *buffer) {
923 uint8_t *pointer =
_type.decode(buffer);
924 pointer =
_length.decode(pointer);
944 *value = *pointer & 0x80 ? 0xFFFFFFFF : 0;
948 for (
unsigned int index = 0; index <
_length; ++index) {
950 *value |=
static_cast<uint8_t
>(*pointer++);
964 unsigned int length =
sizeof(T);
965 for (; length > 1; --length) {
966 uint16_t word = value >> ((length - 2) << 3);
967 if ((word & 0xFF80) != 0xFF80) {
982 unsigned int length = 0;
985 carry = value & 0x80;
988 }
while (value | carry);
1022 virtual const unsigned int getSize(
const bool refresh =
false) {
1081 stream.write(
_value ? 0xFF : 0x00);
1109 *pointer =
_value ? 0xFF : 0x00;
1110 return buffer +
SIZE;
1125 return buffer +
SIZE;
1404 void setValue(
const char *value,
const unsigned int length) {
1407 memcpy(
_value, value, length);
1416 const bool getBit(
const unsigned int index)
const {
1417 const unsigned int byte = index / 8;
1418 const uint8_t bit = index % 8;
1419 return _value[byte] & (0x80 >> bit);
1515 unsigned int index = 0;
1516 uint32_t subidentifier = 0;
1518 char *token =
const_cast<char*
>(
_value.c_str());
1519 while (token != NULL) {
1522 subidentifier = atoi(token);
1525 subidentifier = subidentifier * 40 + atoi(++token);
1526 stream.write(subidentifier);
1531 subidentifier = atol(++token);
1532 uint32_t value = subidentifier;
1533 uint32_t length = 0;
1543 token = strchr(token,
'.');
1558 unsigned int index = 0;
1559 uint32_t subidentifier = 0;
1561 unsigned int length =
_length;
1569 byte = stream.read();
1570 subidentifier <<= 7;
1571 subidentifier +=
byte & 0x7F;
1572 }
while (
byte & 0x80);
1573 _value +=
"." + String(subidentifier);
1576 subidentifier = stream.read();
1577 _value = String(subidentifier / 40) +
"." + String(subidentifier % 40);
1592 unsigned int index = 0;
1593 uint32_t subidentifier = 0;
1595 char *token =
const_cast<char*
>(
_value.c_str());
1596 while (token != NULL) {
1599 subidentifier = atoi(token);
1602 subidentifier = subidentifier * 40 + atoi(++token);
1603 *pointer = subidentifier;
1607 subidentifier = atol(++token);
1608 uint32_t value = subidentifier;
1609 uint32_t length = 0;
1619 token = strchr(token,
'.');
1635 unsigned int index = 0;
1636 uint32_t subidentifier = 0;
1638 uint8_t *end = pointer +
_length;
1643 subidentifier <<= 7;
1644 subidentifier += *pointer & 0x7F;
1645 }
while (*pointer++ & 0x80);
1646 _value +=
"." + String(subidentifier);
1648 subidentifier = *pointer++;
1649 _value = String(subidentifier / 40) +
"."
1650 + String(subidentifier % 40);
1652 }
while (pointer < end);
1675 unsigned int index = 0;
1676 uint32_t subidentifier = 0;
1677 char *token =
const_cast<char*
>(
_value.c_str());
1678 while (token != NULL) {
1681 subidentifier = atoi(token);
1684 subidentifier = subidentifier * 40 + atoi(++token);
1688 subidentifier = atol(++token);
1690 subidentifier >>= 7;
1692 }
while (subidentifier);
1696 token = strchr(token,
'.');
1715template<const u
int8_t U>
1734 for (
auto ber :
_bers) {
1739 for (uint8_t index = 0; index <
_count; ++index) {
1740 delete _bers[index];
1756 for (uint8_t index = 0; index <
_count; ++index) {
1757 _bers[index]->encode(stream);
1779 unsigned int length =
_length;
1807 for (
auto ber :
_bers) {
1808 pointer = ber->encode(pointer);
1811 for (uint8_t index = 0; index <
_count; ++index) {
1812 pointer =
_bers[index]->encode(pointer);
1830 uint8_t *end = pointer +
_length;
1837 pointer = ber->
decode(pointer);
1840 }
while (pointer < end);
1860 virtual const unsigned int getSize(
const bool refresh =
false) {
1863 for (uint8_t index = 0; index <
_count; ++index) {
1879 return _bers[index];
1902 _bers.push_back(ber);
2067 for (uint8_t index = 0; index <
LENGTH; ++index) {
2068 _value[index] = value[index];
2078 return reinterpret_cast<uint8_t*
>(
_value);
2342 _ber->encode(stream);
2371 length -=
_ber->getSize();
2388 return _ber->encode(pointer);
2402 uint8_t *end = pointer +
_length;
2409 pointer =
_ber->decode(pointer);
2411 }
while (pointer < end);
2431 virtual const unsigned int getSize(
const bool refresh =
false) {
2525 *(
reinterpret_cast<uint32_t*
>(&
_value)), buffer);
2539 reinterpret_cast<uint32_t*
>(&
_value), buffer);
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.
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
Counter32BER(const uint32_t value)
Creates a Counter32BER object.
Counter64BER(const uint64_t value)
Creates a Counter64BER object.
EndOfMIBViewBER()
Creates a EndOfMIBViewBER object.
Helper class for internal flag.
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.
Gauge32BER(const uint32_t value)
Creates a Gauge32BER object.
IPAddressBER(IPAddress value)
Creates an IPAddressBER object.
const uint8_t * getValue()
Gets the IPAddressBER value.
static constexpr uint8_t LENGTH
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.
NoSuchInstanceBER()
Creates a NoSuchInstanceBER object.
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.
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.
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.
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.
OpaqueFloatBER(const float value)
Creates an OpaqueFloatBER object.
SequenceBER(const uint8_t type=Type::Sequence)
Creates a SequenceBER.
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.
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.
Error()
Initializes to default values.
Trap()
Initializes to default values.
Helper struct to handle SNMP versions.