SNMP 2.0.0
Loading...
Searching...
No Matches
SNMP.h
Go to the documentation of this file.
1#ifndef SNMP_H_
2#define SNMP_H_
3
4#include "SNMPMessage.h"
5
6#include <Udp.h>
7
12namespace SNMP {
13
23struct Port {
24 static constexpr uint16_t SNMP = 161;
25 static constexpr uint16_t Trap = 162;
26};
27
32class SNMP {
50 using Event = void (*)(const Message*, const IPAddress, const uint16_t);
51
52public:
59 bool begin(UDP& udp) {
60 _udp = &udp;
61 return _udp->begin(_port);
62 }
63
72 void loop() {
73#if SNMP_STREAM
74 if (_udp->parsePacket()) {
75 Message *message = new Message();
76 message->parse(*_udp);
77 _onMessage(message, _udp->remoteIP(), _udp->remotePort());
78 delete message;
79 }
80#else
81 if (_udp->parsePacket()) {
82 uint32_t length = _udp->available();
83 uint8_t *buffer = static_cast<uint8_t*>(malloc(length));
84 if (buffer) {
85 _udp->read(buffer, length);
86 Message *message = new Message();
87 message->parse(buffer);
88 free(buffer);
89 _onMessage(message, _udp->remoteIP(), _udp->remotePort());
90 delete message;
91 }
92 }
93#endif
94 }
95
106 bool send(Message *message, const IPAddress ip, const uint16_t port) {
107#if SNMP_STREAM
108 _udp->beginPacket(ip, port);
109 message->build(*_udp);
110 return _udp->endPacket();
111#else
112 uint32_t length = message->getSize(true);
113 uint8_t *buffer = static_cast<uint8_t*>(malloc(length));
114 message->build(buffer);
115 _udp->beginPacket(ip, port);
116 _udp->write(buffer, length);
117 free(buffer);
118 return _udp->endPacket();
119#endif
120 }
121
127 void onMessage(Event event) {
128 _onMessage = event;
129 }
130
131private:
137 SNMP(const uint16_t port) {
138 _port = port;
139 }
140
142 uint16_t _port = Port::SNMP;
144 UDP *_udp = nullptr;
146 Event _onMessage = nullptr;
147
148 friend class Agent;
149 friend class Manager;
150};
151
158class Agent: public SNMP {
159public:
165 SNMP(Port::SNMP) {
166 }
167};
168
175class Manager: public SNMP {
176public:
182 SNMP(Port::Trap) {
183 }
184};
185
186} // namespace SNMP
187
188#endif /* SNMP_H_ */
SNMP agent.
Definition SNMP.h:158
Agent()
Creates an SNMP agent.
Definition SNMP.h:164
SNMP manager.
Definition SNMP.h:175
Manager()
Creates an SNMP manager.
Definition SNMP.h:181
SNMP message object.
void build()
Builds the message.
virtual const unsigned int getSize(const bool refresh=false)
Gets the size of the message.
void parse()
Parses the message.
void loop()
Network read operation.
Definition SNMP.h:72
bool send(Message *message, const IPAddress ip, const uint16_t port)
Network write operation.
Definition SNMP.h:106
void(*)(const Message *, const IPAddress, const uint16_t) Event
On message event user handler type.
Definition SNMP.h:50
bool begin(UDP &udp)
Initializes network.
Definition SNMP.h:59
SNMP(const uint16_t port)
Creates an SNMP object.
Definition SNMP.h:137
void onMessage(Event event)
Sets on message event user handler.
Definition SNMP.h:127
SNMP library namespace.
Helper struct to handle UDP ports.
Definition SNMP.h:23
static constexpr uint16_t SNMP
Definition SNMP.h:24
Helper struct to handle trap PDU.
Definition BER.h:123