MethodBuilder
Overview
OPC UA Call Services (Part 4 §5.11.2) require that input parameters be wrapped into Variant[]. DarraUa_MethodBuilder_* Using a handle-based builder and type-specific Arg* functions, the encoding Variant is automatically selected. C does not have chained syntax; each step involves a single function call.
API
| Function | Category | Reading and Writing | Note |
|---|---|---|---|
| DarraUa_MethodBuilder_New(session, object_id, method_id, &out_b) | Structure | — | Bound to a method |
| DarraUa_MethodBuilder_Free(b) | Release | — | Destructor (including internal Variant cache) |
| DarraUa_MethodBuilder_LoadInputArgs(b) | Detection | Read | Pull up the "InputArguments" description |
| DarraUa_MethodBuilder_ArgBoolean(b, name, value) | Set Value | — | Set Values by Name |
| DarraUa_MethodBuilder_ArgInt32(b, name, value) | Set Value | — | Set Values by Name |
| DarraUa_MethodBuilder_ArgUInt32(b, name, value) | Set Value | — | Set Values by Name |
| DarraUa_MethodBuilder_ArgInt64(b, name, value) | Set Value | — | Set Values by Name |
| DarraUa_MethodBuilder_ArgFloat(b, name, value) | Set Value | — | Set Values by Name |
| DarraUa_MethodBuilder_ArgDouble(b, name, value) | Set Value | — | Set Values by Name |
| DarraUa_MethodBuilder_ArgString(b, name, value) | Set Value | — | Assign Values by Name (Internal strdup) |
| DarraUa_MethodBuilder_ArgDateTime(b, name, value) | Set Value | — | Set Values by Name |
| DarraUa_MethodBuilder_ArgByIndex(b, index, value) | Set Value | — | Set the value by index; value is const DarraUa_Variant* |
| DarraUa_MethodBuilder_Invoke(b, &out_args, &out_n, &out_status) | Terminal | Write | Calling the Method |
typedef uint32_t DarraUa_MethodBuilderHandle;
#define UA_INVALID_METHOD_BUILDER_HANDLE 0u
DarraUa_Status DarraUa_MethodBuilder_New(
DarraUa_SessionHandle session,
const char* object_id,
const char* method_id,
DarraUa_MethodBuilderHandle* out_handle);
void DarraUa_MethodBuilder_Free(DarraUa_MethodBuilderHandle h);
DarraUa_Status DarraUa_MethodBuilder_LoadInputArgs(DarraUa_MethodBuilderHandle h);
DarraUa_Status DarraUa_MethodBuilder_ArgDouble(
DarraUa_MethodBuilderHandle h, const char* name, double value);
DarraUa_Status DarraUa_MethodBuilder_ArgByIndex(
DarraUa_MethodBuilderHandle h, int32_t index, const DarraUa_Variant* value);
DarraUa_Status DarraUa_MethodBuilder_Invoke(
DarraUa_MethodBuilderHandle h,
DarraUa_Variant*** out_args,
uint32_t* out_count,
DarraUa_Status* out_status);
Set Function: ArgBoolean / ArgInt32 / ArgUInt32 / ArgInt64 / ArgFloat / ArgDouble / ArgString / ArgDateTime PressParameter Name Set a value (in the form of ArgDouble(h, "x", 100.0)); ArgByIndex PressIndex Set value (starting at 0). Returns all values or UA_STATUS_GOOD UA_STATUS_INVALID_ARGUMENT, without throwing an exception.
Return Value: Invoke The return value isTransmission Level Status code, *out_status isMethod Level Status code: These two may differ—a return value simply UA_STATUS_GOOD indicates completion RPC; whether the method itself succeeds or fails depends on the circumstances *out_status.
Code Example
#include <opcua.h>
#include <opcua_method_builder.h>
#include <stdio.h>
int main(void)
{
DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = "opc.tcp://localhost:4840";
/* ... DarraUa_Session_Create + (omitted DarraUa_Session_Connect ) ... */
/* 1) Set values by name */
DarraUa_MethodBuilderHandle b = UA_INVALID_METHOD_BUILDER_HANDLE;
if (DarraUa_MethodBuilder_New(session, "ns=2;s=Robot",
"ns=2;s=Robot.MoveTo", &b) != UA_STATUS_GOOD)
return 1;
DarraUa_MethodBuilder_LoadInputArgs(b); /* Optional; failure is not critical */
DarraUa_MethodBuilder_ArgDouble(b, "x", 100.0);
DarraUa_MethodBuilder_ArgDouble(b, "y", 200.0);
DarraUa_MethodBuilder_ArgInt32(b, "speed", 50);
DarraUa_Variant** outs = NULL;
uint32_t n_outs = 0;
DarraUa_Status method_st = 0;
DarraUa_Status st = DarraUa_MethodBuilder_Invoke(b, &outs, &n_outs, &method_st);
if (st != UA_STATUS_GOOD)
fprintf(stderr, "MoveTo 传输失败 0x%08X\n", st);
else if (method_st != UA_STATUS_GOOD)
fprintf(stderr, "MoveTo 方法失败 0x%08X\n", method_st);
DarraUa_VariantArray_Delete(outs, n_outs);
DarraUa_MethodBuilder_Free(b);
/* 2) Set values by index (LoadInputArgs as a fallback when the parameter name isn't specified) */
DarraUa_MethodBuilderHandle b2 = UA_INVALID_METHOD_BUILDER_HANDLE;
DarraUa_MethodBuilder_New(session, "ns=2;s=Calc", "ns=2;s=Calc.Add", &b2);
DarraUa_Variant* a = DarraUa_Variant_New(); /* Variant Opaque; must be heap-allocated */
DarraUa_Variant* c = DarraUa_Variant_New();
DarraUa_Variant_SetDouble(a, 3.14);
DarraUa_Variant_SetDouble(c, 2.71);
DarraUa_MethodBuilder_ArgByIndex(b2, 0, a);
DarraUa_MethodBuilder_ArgByIndex(b2, 1, c);
DarraUa_Variant_Delete(a); /* The data has already been copied internally by the builder, so it can be released immediately here. */
DarraUa_Variant_Delete(c);
DarraUa_Variant** o2 = NULL;
uint32_t n2 = 0;
DarraUa_MethodBuilder_Invoke(b2, &o2, &n2, &method_st);
double sum = 0.0;
if (method_st == UA_STATUS_GOOD && n2 > 0)
DarraUa_Variant_GetDouble(o2[0], &sum); /* o2[0] Already DarraUa_Variant* */
printf("Add → %f\n", sum);
DarraUa_VariantArray_Delete(o2, n2);
DarraUa_MethodBuilder_Free(b2);
return 0;
}
Comparison with CallSafe
/* Old approach: Hand-coded Variant[] (Variant opaque, creating each instance with `new`/Set) */
DarraUa_Variant* v0 = DarraUa_Variant_New();
DarraUa_Variant* v1 = DarraUa_Variant_New();
DarraUa_Variant* v2 = DarraUa_Variant_New();
DarraUa_Variant_SetDouble(v0, 100.0);
DarraUa_Variant_SetDouble(v1, 200.0);
DarraUa_Variant_SetInt32(v2, 50);
const DarraUa_Variant* inputs[3] = { v0, v1, v2 };
DarraUa_Variant** outs = NULL;
uint32_t n = 0;
DarraUa_Status st = DarraUa_Session_CallSafe(session,
"ns=2;s=Robot", "ns=2;s=Robot.MoveTo",
inputs, 3, &outs, &n);
/* ... */
DarraUa_VariantArray_Delete(outs, n);
DarraUa_Variant_Delete(v0); DarraUa_Variant_Delete(v1); DarraUa_Variant_Delete(v2);
/* New syntax: MethodBuilder (The type is recorded internally by the `Arg*` function, aligned with the server parameter names) */
DarraUa_MethodBuilderHandle b;
DarraUa_MethodBuilder_New(session, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo", &b);
/* ... ArgDouble / ArgInt32 / Invoke ... */
DarraUa_Session_Call Avoid using structures DarraUa_NodeId—they're more cumbersome; for strings, just call the methods directly NodeIdDarraUa_Session_CallSafe.
Implementation Constraints
LoadInputArgsCurrently undefined ExtensionObject; this is only an existence check. If the parameter name is not provided, the function can stillArgByIndexbe called using.- Does not support complex structures as input parameters
InvokeReturns a failureDarraUa_Status; does not print a log; the business logic must interpret the result on its own; treat the transport-level and method-level status codes separately.
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | DarraUa_MethodBuilder_New/_Free/_Invoke |
| .Arg(name, value) | .arg(name, value) | .arg(name, value) | .Arg(name, value) | .arg(name, value) | DarraUa_MethodBuilder_ArgDarraUa_MethodBuilder_Arg<Type>(h, name, value)(h, name, value) |