snmpWaitEvent (Function)

Top  Previous  Next

Architecture:

NX32L

Firmware version:

1.50.00


This function will until a trap event is received or an optional timeout. Only traps registered with the snmpRegisterTrap function will generate an event.

When a SNMP event is received, the variables associated with the event can be read by calling snmpGetNextVar once for each variable.

 

Note: Calling snmpWaitEvent again will clear the variables from the previous event!

 

Input:

Timeout : DINT (default -1)

Time in milliseconds to wait for trap event before returning. (-1: wait forever.)

 

Output:

Event : USINT

The event type of the returned event.

_SNMP_EVENT_NOEVENT

If the function returns with a failure, check the return code.

_SNMP_EVENT_TIMEOUT

When a timeout occurred according to the Timeout parameter.

_SNMP_EVENT_TRAP

A registered trap was received.

_SNMP_EVENT_SET

A published variable was changed with a snmpSet call.

 

OID : STRING

The OID of the received SNMP event.

 

Vars : INT

The number of variables received with the SNMP event.

 

Returns: INT

1

- Event received.

0

- This function is not supported.

-2

- Invalid parameter.

-3

- Events have been lost due to too many events in queue.

-7

- Timeout.

-12

- General error.

 

Declaration

FUNCTION snmpWaitEvent : INT;
VAR_INPUT
   Timeout : DINT := -1;
   Event   : ACCESS USINT;
   OID     : ACCESS STRING;
   Vars    : ACCESS INT;
END_VAR;

 

Example:

INCLUDE rtcu.inc
 
THREAD_BLOCK snmpMonitor;
VAR
   count   : INT;
   oid     : STRING;
   rc      : INT;
   val     : snmpVariable;
   str     : STRING;
   event_t : USINT;
END_VAR;
 
WHILE TRUE DO
   rc := snmpWaitEvent(timeout := -1event:=event_toid:=oidvars:=count);
   CASE event_t OF
      _SNMP_EVENT_NOEVENT :
         CASE rc OF
             1DebugMsg(message:="ERROR: NOEVENT should not give return code 1!");
             0DebugMsg(message:="ERROR: The function is not supported!");
            -2DebugMsg(message:="ERROR: Invalid input");
            -3DebugMsg(message := "(Trap events lost)");
           -12DebugMsg(message := "General error!");
         ELSE
            DebugFmt(message:="ERROR: unknown return code from snmpReadEvent (rc=\1)", v1 := rc);
         END_CASE;
      _SNMP_EVENT_TIMEOUT :
          DebugMsg(message:="snmpWaitEvent: Timeout!");
      _SNMP_EVENT_TRAP :
          DebugMsg(message := "Trap [" + oid + "] received");
          DebugFmt(message := "Variables: \1", v1 := count);
      _SNMP_EVENT_SET :
          DebugMsg(message:="Set request received on OID " + oid);
   ELSE
      DebugFmt(message:="ERROR: snmpWaitEvent - unknown event type \1!", v1 := event_t);
   END_CASE;
   IF count > 0 THEN
      // Iterate variables
      rc := 1;
      WHILE rc = 1 DO
         rc := snmpGetNextVar(data := val);
         IF rc = 1 THEN
            // Build output
            str := "[" + val.oid + "] = ";
            CASE val.type OF
               1str := str + dintToStr(v := val.time_value);
               2str := str + dintToStr(v := val.int_value);
               3str := str + dintToStr(v := val.uint_value);
               4str := str + "[" + val.str_value + "]";
               5str := str + "[" + val.hex_value + "]";
               6str := str + "[" + val.oid_value + "]";
               7str := str + "[" + val.ip_value + "]";
               8str := str + floatToStr(v := val.float_value);
               9str := str + doubleToStr(v := val.double_value);
            ELSE
               str := str + "Unknown variable type (" + intToStr(v := val.type) + ")";
            END_CASE;
         END_IF;
      END_WHILE;
      IF rc < 1 AND rc <> -11 THEN
         DebugFmt(message := "snmpGetNextVar=\1", v1 := rc);
      END_IF;
   END_IF;
END_WHILE;
END_THREAD_BLOCK;
 
PROGRAM example;
VAR
   mon      : snmpMonitor;
   handle   : SYSHANDLE;
   iface    : SINT := 2;
   rc       : INT;
END_VAR;
 
   // Initialize network
   rc := netOpen(iface := iface);
   DebugFmt(message := "netOpen (rc=\1)", v1 := rc);
   WHILE NOT netConnected(iface := ifaceDO
      Sleep(Delay := 2000);
   END_WHILE;
 
   // Initialize SNMP
   rc := snmpStartListen(
                         handle    := handle,
                         community := "public",
                         port      := 162
                        );
   DebugFmt(message := "snmpStartListen = \1", v1 := rc);
   rc := snmpRegisterTrap(oid := "1.3.6.1.4.1.6101.1.8.8.2.6");
   DebugFmt(message := "snmpRegisterTrap = \1", v1 := rc);
   mon();
 
BEGIN
END;
END_PROGRAM;