From e8e81401388e3a5015016b37c3a32064981d8dab Mon Sep 17 00:00:00 2001 From: Gonzalo Brusco Date: Thu, 22 May 2025 09:03:30 -0300 Subject: [PATCH 1/2] Allow specificTrap to be higher than 255 --- src/BER.h | 2 +- src/SNMPMessage.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BER.h b/src/BER.h index cda0a8f..c63ae31 100644 --- a/src/BER.h +++ b/src/BER.h @@ -156,7 +156,7 @@ struct Trap { /** Generic trap code. */ uint8_t _genericTrap; /** Specific trap code. */ - uint8_t _specificTrap; + uint32_t _specificTrap; /** Time elapsed since device startup. */ uint32_t _timeStamp; }; diff --git a/src/SNMPMessage.h b/src/SNMPMessage.h index fad7e2b..b7dc14e 100644 --- a/src/SNMPMessage.h +++ b/src/SNMPMessage.h @@ -339,7 +339,7 @@ class Message: public SequenceBER, private PDU { * @param generic Generic trap code. * @param specific Specific trap code. */ - void setTrap(const uint8_t generic, const uint8_t specific = 0) { + void setTrap(const uint8_t generic, const uint32_t specific = 0) { _trap._genericTrap = generic; _trap._specificTrap = specific; } From da40e722498d1d750a427d1ac1839fba22a2fd63 Mon Sep 17 00:00:00 2001 From: Gonzalo Brusco Date: Thu, 22 May 2025 09:19:03 -0300 Subject: [PATCH 2/2] Allow setting the uptime externally --- src/SNMPMessage.h | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/SNMPMessage.h b/src/SNMPMessage.h index b7dc14e..2a93731 100644 --- a/src/SNMPMessage.h +++ b/src/SNMPMessage.h @@ -345,19 +345,24 @@ class Message: public SequenceBER, private PDU { } /** - * @brief Sets the SNMPTRAPOID variable binding. - - * @warning Valid only for InformRequest or SNMPv2Trap PDU. + * @brief Adds the mandatory variable bindings for an SNMPv2 trap or inform. + * + * This function sets the required variable bindings for SNMPv2-Trap or InformRequest PDUs: + * - `sysUpTime.0` is automatically set at build time using `millis()` if not explicitly provided. + * - `snmpTrapOID.0` is set to the given `name` value. * - * - Adds mandatory variable binding *sysUpTime.0*. Value will be set at build - * time. - *- Adds variable binding *snmpTrapOID.0* with value name. + * @warning This function is only valid for InformRequest or SNMPv2-Trap PDUs. * - * @param name snmpTrapOID.0 value. + * @param name The value to assign to `snmpTrapOID.0` (typically a trap OID string). + * @param sysuptime The system uptime in hundredths of a second. If set to 0 (default), + * the value is automatically computed from `millis()`. */ - void setSNMPTrapOID(const char *name) { -// add(OID::SYSUPTIME, new TimeTicksBER(0)); - add(OID::SYSUPTIME, new TimeTicksBER(millis() / 10)); + void setSNMPTrapOID(const char *name, uint32_t sysuptime = 0) { + if(sysuptime == 0) { + add(OID::SYSUPTIME, new TimeTicksBER(millis() / 10)); + } else { + add(OID::SYSUPTIME, new TimeTicksBER(sysuptime)); + } add(OID::SNMPTRAPOID, new ObjectIdentifierBER(name)); }