Skip to main content

Event Subscription (C)

OPC UA events (Alarms & Conditions) are subscribed to through DarraUa_Subscription_AddEventItem. These are OPC UA protocol events and are not the same thing as the SDK's DataChange callback.

Difference
  • DarraUa_Subscription_AddEventItem — OPC UA Protocol Events (Alarms / Conditions, Server-Side Push Notifications, This Section)
  • DarraUa_OnDataChange — MI Changes in Data (The variable value has changed)
  • DarraUa_OnSubscriptionState — SDK Internal Subscription Status (Not related to the agreement)

Event API in a separate header file:

#include <opcua/opcua_event.h>

API​

OPCUA_API DarraUa_Status OPCUA_CALL DarraUa_Subscription_AddEventItem(
DarraUa_SubscriptionHandle sub,
const char* object_node_id_str, /* NULL = "i=2253" Server */
DarraUa_OnEvent cb,
void* user_context,
DarraUa_MonitoredItemHandle* out_mi);
ParametersDefaultNote
object_node_id_strNULL → i=2253 (Server)The event source node must have the EventNotifier attribute (12)
cb—Event arrival callback
out_mi—Exit, New Handle MI

Event Payload​

typedef struct {
uint16_t severity; /* 1-1000 */
const char* message; /* LocalizedText.text, UTF-8 */
const char* source_name; /* UTF-8 */
DarraUa_DateTime time; /* UTC 100ns since 1601 */
const char* event_type; /* NodeId String, possibly NULL */
} DarraUa_Event;
FieldMeaning
severity1–1000; the higher the number, the more severe the condition
messageHuman-readable message
source_nameName of the source object that triggered the event
timeEvent timestamp (server-side)
event_typeEvent type NodeId, such as "i=2041" (BaseEventType)

Life Cycle: ev Pointers are only valid within the callback; the same applies to string fields. If you need to retain them, strdup or copy them to your own buffer.

Severity Level​

  • 1-249 — Low
  • 250-499 — Medium
  • 500-749 — High
  • 750-1000 — HighHigh

Event Callback Signature​

typedef void (*DarraUa_OnEvent)(
DarraUa_SubscriptionHandle sub,
DarraUa_MonitoredItemHandle mi,
const DarraUa_Event* ev,
void* user_context);

Built-in EventFilter​

The SDK always uses 4 fixed fields (BaseEventType standard fields, as SimpleAttributeOperand):

IndexField
0Severity (UInt16)
1Message (LocalizedText)
2SourceName (String)
3Time (DateTime)

whereClause = Empty, meaning all events are reported. Applies to the vast majority of servers.

To customize the SelectClause / where condition, use DarraUa_EventSubscription_SubscribeWithFilter (see EventFilter), and it and AddEventItem both build the same type of Event MI.

Complete Example​

#include <opcua/opcua.h>
#include <opcua/opcua_event.h>
#include <stdio.h>

#if defined(_WIN32)
# include <windows.h>
# define OPCUA_SLEEP_MS(ms) Sleep(ms)
#else
# include <unistd.h>
# define OPCUA_SLEEP_MS(ms) usleep((ms) * 1000)
#endif

static int g_event_count = 0;

static const char* severity_label(uint16_t s)
{
if (s == 0) return "None";
if (s < 250) return "Low";
if (s < 500) return "Medium";
if (s < 750) return "High";
return "HighHigh";
}

static void on_event(DarraUa_SubscriptionHandle sub,
DarraUa_MonitoredItemHandle mi,
const DarraUa_Event* ev, void* ctx)
{
(void)sub; (void)mi; (void)ctx;
if (!ev) return;
g_event_count++;
printf("[event #%d] sev=%u (%s) src=\"%s\" type=\"%s\" time=%lld msg=\"%s\"\n",
g_event_count,
(unsigned)ev->severity, severity_label(ev->severity),
ev->source_name ? ev->source_name : "",
ev->event_type ? ev->event_type : "",
(long long)ev->time,
ev->message ? ev->message : "");
}

int main(int argc, char** argv)
{
const char* endpoint = (argc >= 2) ? argv[1] : "opc.tcp://localhost:4840";
DarraUa_Initialize();

DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = endpoint;
cfg.security_policy_uri = "http://opcfoundation.org/UA/SecurityPolicy#None";

DarraUa_SessionHandle h = UA_INVALID_SESSION_HANDLE;
DarraUa_Session_Create(&cfg, &h);
DarraUa_Session_Connect(h);

DarraUa_SubscriptionConfig sc;
DarraUa_SubscriptionConfig_Init(&sc);
sc.publishing_interval_ms = 500.0;
DarraUa_SubscriptionHandle sub = UA_INVALID_SUBSCRIPTION_HANDLE;
DarraUa_Subscription_Create(h, &sc, &sub);

/* Add an Event on Server (i=2253) MI */
DarraUa_MonitoredItemHandle ev_mi = UA_INVALID_MONITORED_ITEM_HANDLE;
DarraUa_Status st = DarraUa_Subscription_AddEventItem(
sub, "i=2253", on_event, NULL, &ev_mi);
if (!UA_STATUS_IS_GOOD(st)) {
fprintf(stderr, "AddEventItem failed: %s\n", DarraUa_StatusName(st));
}

/* Pump for 10 seconds */
for (int i = 0; i < 20; ++i) {
OPCUA_SLEEP_MS(500);
DarraUa_Session_Publish(h, 2000);
}
printf("Total events received: %d\n", g_event_count);

DarraUa_Subscription_Delete(sub);
DarraUa_Session_Disconnect(h);
DarraUa_Session_Close(h);
DarraUa_Shutdown();
return 0;
}

Reusing an Existing Subscription​

Events MI can be attached to existing data subscriptions and shared publishingInterval:

DarraUa_SubscriptionHandle sub = /* Existing Data Subscriptions */;
DarraUa_MonitoredItemHandle data_mi = UA_INVALID_MONITORED_ITEM_HANDLE;
DarraUa_MonitoredItemHandle ev_mi = UA_INVALID_MONITORED_ITEM_HANDLE;

DarraUa_Subscription_AddNode(sub, "ns=2;s=Counter", UA_ATTR_VALUE,
-1.0, on_change, NULL, &data_mi); /* Data MI */
DarraUa_Subscription_AddEventItem(sub, "i=2253",
on_event, NULL, &ev_mi); /* Event MI */

out_mi A valid pointer (not NULL) must be passed — the handle MI will SDK be written to a newly allocated handle.

EventNotifier​

Only nodes with a non-0 EventNotifier attribute (12) can generate events. By default, Server(i=2253) acts as an EventNotifier. Whether a custom node can act as an event source depends on the server implementation.

Alarm Operations (Acknowledge / Confirm)​

Upon receiving an AlarmConditionType event, the operator is typically required to acknowledge it. By DarraUa_Session_Call Acknowledgment Method for Calling Alarm Nodes:

/* condition_id This is the condition node that triggers the alarm. NodeId */
DarraUa_NodeId obj_id, mtd_id;
/* Parsing condition_id and condition_id + ".Acknowledge" into the structure NodeId , omitted */
DarraUa_Variant* args[2] = { /* eventId */, /* comment */ };
DarraUa_Variant* outs = NULL;
uint32_t out_n = 0;
DarraUa_Status method_st = 0;
DarraUa_Session_Call(h, &obj_id, &mtd_id,
(const DarraUa_Variant* const*)args, 2, &outs, &out_n, &method_st);

String Save Template​

Fields within callbacks have a short lifespan, and I want to preserve the event:

typedef struct {
uint16_t severity;
char* message;
char* source_name;
DarraUa_DateTime time;
} SavedEvent;

static SavedEvent* g_events = NULL;
static int g_n = 0, g_cap = 0;

static char* dup_or_null(const char* s)
{
if (!s) return NULL;
char* out = (char*)malloc(strlen(s) + 1);
if (out) strcpy(out, s);
return out;
}

static void on_event_save(DarraUa_SubscriptionHandle s, DarraUa_MonitoredItemHandle m,
const DarraUa_Event* ev, void* ctx)
{
(void)s; (void)m; (void)ctx;
if (!ev) return;
if (g_n == g_cap) {
g_cap = g_cap ? g_cap * 2 : 16;
g_events = (SavedEvent*)realloc(g_events, g_cap * sizeof(SavedEvent));
}
SavedEvent* dst = &g_events[g_n++];
dst->severity = ev->severity;
dst->message = dup_or_null(ev->message);
dst->source_name = dup_or_null(ev->source_name);
dst->time = ev->time;
}

Next Step​