Skip to main content

DataChange State Callbacks (C)

The C SDK exposes data changes and subscription status as two separate callback signatures:

CallbackRegister FunctionTrigger Conditions
DarraUa_OnDataChange_AddNode / _AddNodes / _MonitoredItem_SetCallbackMI Changes in Data
DarraUa_OnSubscriptionState_Subscription_SetStateCallbackSubscription-level status changes (LATE / KEEP_ALIVE / LIFETIME_EXPIRED / RECREATING)
Relationship

DataChange Callback​

typedef void (*DarraUa_OnDataChange)(
DarraUa_SubscriptionHandle sub,
DarraUa_MonitoredItemHandle mi,
const DarraUa_DataValue* value,
void* user_context);

Parameter Description​

  • sub — Triggered subscription handle
  • mi — Triggered MonitoredItem local handle
  • value — DataValue, Lifetime = Inside the callback function, Do not store pointers permanently
  • user_context — The context pointer passed during registration

Usage​

static void on_change(DarraUa_SubscriptionHandle sub,
DarraUa_MonitoredItemHandle mi,
const DarraUa_DataValue* dv,
void* ctx)
{
if (!dv) return;

uint32_t status = DarraUa_DataValue_GetStatus(dv);
DarraUa_DateTime ts = DarraUa_DataValue_GetSourceTimestamp(dv);
const DarraUa_Variant* v = DarraUa_DataValue_GetValue(dv);
DarraUa_BuiltinType type = v ? DarraUa_Variant_GetType(v) : UA_TYPE_NULL;

if (UA_STATUS_IS_BAD(status)) {
printf("[mi=%u] BAD: %s\n", (unsigned)mi, DarraUa_StatusName(status));
return;
}

if (type == UA_TYPE_DOUBLE) {
double d = 0.0;
DarraUa_Variant_GetDouble(v, &d);
printf("[mi=%u] %.3f @ ft=%lld\n", (unsigned)mi, d, (long long)ts);
}
}

How to Use user_context​

Pass user_context the MI metadata (NodeId string / business object pointer) for each via, and look mi it up by in the callback:

typedef struct { const char* node_id; int channel_index; } MiContext;

static MiContext g_ctx[100];

for (int i = 0; i < 100; ++i) {
g_ctx[i].node_id = "ns=2;s=...";
g_ctx[i].channel_index = i;
DarraUa_MonitoredItemHandle mi = UA_INVALID_MONITORED_ITEM_HANDLE;
DarraUa_Subscription_AddNode(sub, g_ctx[i].node_id, UA_ATTR_VALUE,
-1.0, on_change, &g_ctx[i], &mi); /* out_mi It must be a valid pointer. */
}

static void on_change(DarraUa_SubscriptionHandle sub,
DarraUa_MonitoredItemHandle mi,
const DarraUa_DataValue* dv, void* ctx)
{
MiContext* mc = (MiContext*)ctx;
/* Press mc->channel_index to find a business channel */
}

Subscription-Level Status Callbacks​

typedef enum {
UA_SUB_STATE_GOOD = 0,
UA_SUB_STATE_LATE = 1,
UA_SUB_STATE_KEEP_ALIVE = 2,
UA_SUB_STATE_LIFETIME_EXPIRED = 3,
UA_SUB_STATE_RECREATING = 4
} DarraUa_SubscriptionState;

typedef void (*DarraUa_OnSubscriptionState)(
DarraUa_SubscriptionHandle sub,
DarraUa_SubscriptionState new_state,
DarraUa_Status status,
void* user_context);

OPCUA_API DarraUa_Status OPCUA_CALL
DarraUa_Subscription_SetStateCallback(
DarraUa_SubscriptionHandle sub,
DarraUa_OnSubscriptionState cb,
void* user_context);

Meaning of Status​

  • GOOD — Normal, data is being pushed continuously
  • LATE — The server is unable to PublishRequest keep up publishingInterval; data may be delayed
  • KEEP_ALIVE — KeepAlive Cycle (No Data Changes)
  • LIFETIME_EXPIRED — No messages received within lifetime_count PublishRequest; the server clears the subscription
  • RECREATING — Subscription is being rebuilt / migrated (in the process of reconnecting or transferring); for more details, see Subscription Resumes Automatically

Usage​

static void on_sub_state(DarraUa_SubscriptionHandle sub,
DarraUa_SubscriptionState s,
DarraUa_Status code, void* ctx)
{
(void)sub; (void)ctx;
if (s == UA_SUB_STATE_LIFETIME_EXPIRED)
fprintf(stderr, "Subscription expired (%s)\n", DarraUa_StatusName(code));
}

DarraUa_Subscription_SetStateCallback(sub, on_sub_state, NULL);

Thread Model​

Callback atWhich thread? It depends on the Publish mode:

PatternCallback ThreadSource
Manual Pump (DarraUa_Session_Publish(h, timeout_ms))Calling Thread — Callbacks are triggered synchronously within the call Session_Publishopcua_session.h
Backend (DarraUa_Session_StartAutoPublish)Internal Publish Threads in Stackopcua_subscribe.h
Background mode must be switched back to UI threads

AutoPublish Once enabled, callbacks are triggered by SDK internal threads, Not on the caller's thread. In the callback, you must manually UI marshal the data to the UI thread; otherwise, accessing the control across threads will cause a crash.

  • Do not perform lengthy operations in callbacks, as this will block the publish queue.
  • If you want to read from the same node again within the callback, use a synchronous DarraUa_Session_ReadNode re-read (do not copy the value pointer out of the callback, as it will immediately become invalid).
  • Multiple /MI Multiple subscriptions may trigger callbacks in different threads; the business logic should lock the shared data on its own.

Cross-Platform Atom Counting:

#if defined(_WIN32)
# include <intrin.h>
# define ATOMIC_INC(p) _InterlockedIncrement((volatile long*)(p))
#else
# define ATOMIC_INC(p) __sync_add_and_fetch((p), 1)
#endif

static volatile long g_callback_count = 0;
static void on_change(...) { ATOMIC_INC(&g_callback_count); }

Comparison with ua.Events.* (C# Concept)​

C# provides a global ua.Events.DataChange aggregation channel. The C SDK has no such facility; the business side maintains its own global callback (i.e., single_cb shared across _AddNodes instances) to achieve the same effect. Alternatively, use user_context in each MI's callback for a "unified sink + business distribution" approach.

Next Step​