Call (C)
API
OPCUA_API DarraUa_Status OPCUA_CALL DarraUa_Session_Call(
DarraUa_SessionHandle h,
const DarraUa_NodeId* object_id,
const DarraUa_NodeId* method_id,
const DarraUa_Variant* const* input_args,
uint32_t input_count,
DarraUa_Variant** out_args, /* Output Parameter: The array of slots is allocated by the caller */
uint32_t* out_count,
DarraUa_Status* out_status); /* Output Parameters: Method Business State */
String NodeId Version (input and output parameters are the same, but strings are passed directly method_id, and object_id the output parameter group is allocated by the Stack):
UA_SDK_API DarraUa_Status UA_SDK_CALL DarraUa_Session_CallSafe(
DarraUa_SessionHandle session,
const char* object_id,
const char* method_id,
const DarraUa_Variant* const* input_args,
uint32_t input_count,
DarraUa_Variant*** out_args, /* Output: Stack Allocation */
uint32_t* out_count);
| Parameters | Note |
|---|---|
| object_id | Method on the Object node (Call pass structure DarraUa_NodeId, CallSafe pass string) |
| method_id | The Method Itself NodeId |
| input_args | Pass an array of pointers as an argument; length = input_count |
| out_args | Call: The caller preallocates a slot DarraUa_Variant* array (with a capacity of at least max(input_count, 32)); CallSafe: Allocated using a stack; if that fails, *out_args = NULL |
| out_count | Number of Outputs |
| out_status | Call Proprietary: Method business status code (transmission OK, but the business operation may have failed).CallSafe Without this output parameter, the business status is included in the return value. |
| Return | Meaning |
|---|---|
| UA_STATUS_GOOD | Transmission level: OK; *out_status still needs to be checked |
| UA_STATUS_BAD_COMMUNICATION_ERROR | Network Outage |
| UA_STATUS_INVALID_ARGUMENT | object_id / method_id is empty |
| UA_STATUS_INVALID_HANDLE | Invalid session handle |
*out_status This is the business-level Method code returned by the server (Good / BadUserAccessDenied / business-defined Bad...). SDK Only common codes are included; if a code not included in this list is returned DarraUa_StatusName "Unknown", please print the raw value using 0x%08X.
Memory Ownership (Important)
| Data | Release |
|---|---|
| input_args[i] | Held by the caller; released automatically after the call ends DarraUa_Variant_Delete |
Call the out_args slot array itself | If it was allocated by the caller, the caller should free it — Don't Use DarraUa_VariantArray_Delete (it will free the array as well) |
Call out_args[i] | One by one DarraUa_Variant_Delete |
CallSafe out_args (the array itself + each element) | DarraUa_VariantArray_Delete(outs, out_count) One-time release |
DarraUa_VariantArray_Delete(arr, count) Equivalent to: For each arr[i] call DarraUa_Variant_Delete, free the array itself. This only applies toAssigned by Stack an array of (i.e.CallSafe, *out_args).
Do not mix them free() (Cross-inconsistency DLL CRT).
Complete Example
#include <opcua/opcua.h>
#include <stdio.h>
int main(int argc, char** argv)
{
const char* endpoint = (argc >= 2) ? argv[1] : "opc.tcp://localhost:4840";
DarraUa_Initialize();
/* ... Connect [omitted] ... */
DarraUa_SessionHandle h = /* ... */ 0;
/* 1. Analysis NodeId */
DarraUa_NodeId obj_id = { 0 }, mtd_id = { 0 };
DarraUa_NodeId_ParseString("ns=2;s=Calculator", &obj_id);
DarraUa_NodeId_ParseString("ns=2;s=Calculator.Add", &mtd_id);
/* 2. Input Parameters */
DarraUa_Variant* in0 = DarraUa_Variant_New();
DarraUa_Variant* in1 = DarraUa_Variant_New();
DarraUa_Variant_SetInt32(in0, 3);
DarraUa_Variant_SetInt32(in1, 4);
const DarraUa_Variant* inputs[2] = { in0, in1 };
/* 3. Call (output slots are allocated by the caller; capacity is at least max(input_count, 32)) */
DarraUa_Variant* outs[32] = { NULL };
uint32_t out_n = 0;
DarraUa_Status method_st = 0;
DarraUa_Status st = DarraUa_Session_Call(
h, &obj_id, &mtd_id, inputs, 2, outs, &out_n, &method_st);
if (!UA_STATUS_IS_GOOD(st)) {
fprintf(stderr, "Transport error: %s\n", DarraUa_StatusName(st));
} else if (!UA_STATUS_IS_GOOD(method_st)) {
fprintf(stderr, "Method error: %s\n", DarraUa_StatusName(method_st));
} else {
for (uint32_t i = 0; i < out_n; ++i) {
int32_t r = 0;
DarraUa_Variant_GetInt32(outs[i], &r);
printf("sum = %d\n", (int)r);
}
}
/* 4. Free the parameters: The slot array belongs to the caller; free the elements one by one. */
for (uint32_t i = 0; i < out_n; ++i) DarraUa_Variant_Delete(outs[i]);
/* 5. Free the input parameters */
DarraUa_Variant_Delete(in0);
DarraUa_Variant_Delete(in1);
/* 6. Release the internal NodeId string */
DarraUa_NodeId_ClearExport(&obj_id);
DarraUa_NodeId_ClearExport(&mtd_id);
DarraUa_Shutdown();
return 0;
}
Error Code
Top-Level Return (Transport Layer):
| Status Code | Meaning |
|---|---|
| Good | Call completed |
| BadCommunicationError | Transport Error |
| InvalidArgument | object_id / method_id is empty |
| InvalidHandle | Invalid session handle |
Business-level method (only *out_status within; CallSafe otherwise, check the return value):
| Status Code | Meaning |
|---|---|
| Good | The method executed successfully |
| BadUserAccessDenied | No permission to call |
| Other server-side code | Pass through the business code provided by the server exactly as is |
Only common codes are included; codes outside this list come back as "Unknown" from DarraUa_StatusName.
Business-level failures are reported *out_status via; if they fail, they are returned at the top level.
Looking for a way to... NodeId
/* 1. Browse Object Child Node Filtering Method */
DarraUa_BrowseResult* br = NULL;
DarraUa_Session_BrowseNode(
h, "ns=2;s=Calculator",
UA_NODE_CLASS_METHOD, &br);
uint32_t n = DarraUa_BrowseResult_GetCount(br);
char id_buf[128];
for (uint32_t i = 0; i < n; ++i) {
const DarraUa_ReferenceDescription* rd =
DarraUa_BrowseResult_GetReference(br, i);
DarraUa_Ref_GetNodeIdString(rd, id_buf, (int32_t)sizeof(id_buf));
const char* bn = DarraUa_Ref_GetBrowseName(rd);
printf(" %s -> %s\n", bn ? bn : "", id_buf);
}
DarraUa_BrowseResult_Delete(br);
/* 2. Check the input/output parameter signatures (Browse the InputArguments / in the method node OutputArguments) */
br = NULL;
DarraUa_Session_BrowseNode(
h, "ns=2;s=Calculator.Add",
UA_NODE_CLASS_VARIABLE, &br);
/* InputArguments Is a Property, NodeClass=Variable, with a value of the Argument[] array */
DarraUa_BrowseResult_Delete(br);
Input Parameter Construction
Constructing method signatures Variant:
/* Add(Int32, Int32) -> Int32 */
DarraUa_Variant* a = DarraUa_Variant_New(); DarraUa_Variant_SetInt32(a, 3);
DarraUa_Variant* b = DarraUa_Variant_New(); DarraUa_Variant_SetInt32(b, 4);
/* SetTemperature(string deviceId, double value) */
DarraUa_Variant* dev = DarraUa_Variant_New(); DarraUa_Variant_SetString(dev, "Boiler1");
DarraUa_Variant* val = DarraUa_Variant_New(); DarraUa_Variant_SetDouble(val, 85.5);
/* Array Parameters */
DarraUa_Variant* arr = DarraUa_Variant_New();
double points[] = { 1.0, 2.0, 3.0 };
DarraUa_Variant_SetDoubleArray(arr, points, 3);
DarraUa_Variant_New() It is allocated on the heap (Variant as it is an opaque type, it cannot be declared on the stack and used directly). The available constructor entries cover built-in scalars, arrays, and QualifiedName / LocalizedText; see Variant.
Synchronous / Asynchronous
DarraUa_Session_Call This is a synchronous, blocking call; internally, it issues a "RPC wait" response. If the upper layer wants to make it asynchronous, it should use a thread pool for scheduling.