diff --git a/src/SNMP_Agent.cpp b/src/SNMP_Agent.cpp index d1f02fc..6b9a627 100644 --- a/src/SNMP_Agent.cpp +++ b/src/SNMP_Agent.cpp @@ -190,6 +190,14 @@ ValueCallback* SNMPAgent::addGaugeHandler(const char *oid, uint32_t* value, bool return addHandler(new Gauge32Callback(oidType, value), false); } +ValueCallback* addIPAddressHandler(const char *oid, IPAddress* value, bool isSettable, bool overwritePrefix){ + if(!value) return nullptr; + + SortableOIDType* oidType = buildOIDWithPrefix(oid, overwritePrefix); + if(!oidType) return nullptr; + return addHandler(new NetworkAddressCallback(oidType, value), false); +} + ValueCallback * SNMPAgent::addHandler(ValueCallback *callback, bool isSettable) { callback->isSettable = isSettable; this->callbacks.push_back(callback); diff --git a/src/SNMP_Agent.h b/src/SNMP_Agent.h index a34e67e..1208a17 100644 --- a/src/SNMP_Agent.h +++ b/src/SNMP_Agent.h @@ -68,19 +68,18 @@ class SNMPAgent { ValueCallback* addCounter64Handler(const char *oid, uint64_t* value, bool overwritePrefix = false); ValueCallback* addCounter32Handler(const char *oid, uint32_t* value, bool overwritePrefix = false); ValueCallback* addGaugeHandler(const char *oid, uint32_t* value, bool overwritePrefix = false); + ValueCallback* addIPAddressHandler(const char *oid, IPAddress* value, bool isSettable = false, bool overwritePrefix = false); + // Depreciated, use addGaugeHandler() __attribute__((deprecated)) ValueCallback* addGuageHandler(const char *oid, uint32_t* value, bool overwritePrefix = false) { return addGaugeHandler(oid, value, overwritePrefix); } - void - setUDP(UDP* udp); + void setUDP(UDP* udp); bool restartUDP(); - void - begin(); - void - begin(const char* oidPrefix); + void begin(); + void begin(const char* oidPrefix); void stop(); enum SNMP_ERROR_RESPONSE loop(); diff --git a/src/ValueCallbacks.cpp b/src/ValueCallbacks.cpp index 8a984e9..b932c47 100644 --- a/src/ValueCallbacks.cpp +++ b/src/ValueCallbacks.cpp @@ -205,6 +205,22 @@ SNMP_ERROR_STATUS Counter64Callback::setTypeWithValue(BER_CONTAINER* rawValue){ return NO_ERROR; } +std::shared_ptr NetworkAddressCallback::buildTypeWithValue(){ + ASSERT_VALID_VALUE(this->value); + + return std::make_shared(*this->value); +} + +SNMP_ERROR_STATUS NetworkAddressCallback::setTypeWithValue(BER_CONTAINER* rawValue){ + ASSERT_CALLBACK_SETTABLE(); + ASSERT_VALID_SETTABLE_VALUE(this->value); + + NetworkAddress* val = static_cast(rawValue); + *this->value = val->_value; + + return NO_ERROR; +} + bool SortableOIDType::sort_oids(SortableOIDType* oid1, SortableOIDType* oid2){ // returns true if oid1 EARLIER than oid2 const auto& map1 = oid1->sortingMap; const auto& map2 = oid2->sortingMap; diff --git a/src/include/ValueCallbacks.h b/src/include/ValueCallbacks.h index ede67ec..40169ed 100644 --- a/src/include/ValueCallbacks.h +++ b/src/include/ValueCallbacks.h @@ -226,4 +226,15 @@ class Counter64Callback: public ValueCallback { SNMP_ERROR_STATUS setTypeWithValue(BER_CONTAINER* value) override; }; +class NetworkAddressCallback: public ValueCallback { + public: + NetworkAddressCallback(SortableOIDType* oid, IPAddress* value): ValueCallback(oid, NETWORK_ADDRESS), value(value) {}; + + protected: + IPAddress* const value; + + std::shared_ptr buildTypeWithValue() override; + SNMP_ERROR_STATUS setTypeWithValue(BER_CONTAINER* value) override; +}; + #endif