Skip to main content

File Transfer (DarraUa_OpcUaFile_*)

Overview​

OPC UA Part 5 §10 FileType exposes 6 methods (Open/Read/Write/Close/GetPosition/SetPosition). The SDK wraps them as a group of DarraUa_OpcUaFile_* C APIs, plus two high-level wrappers for whole files: DownloadTo and UploadFrom.

API​

FunctionCategoryReading and WritingNote
DarraUa_OpcUaFile_New(session, file_node_id, &out_h)Structure—Bind instance FileType, do not send RPC
DarraUa_OpcUaFile_Free(h)Release—Destructor (Closes automatically if not closed)
DarraUa_OpcUaFile_Size(h)PropertiesReadTotal number of bytes in the file; returns uint64_t
DarraUa_OpcUaFile_Writable(h)PropertiesReadIs it writable at the server level? Returns int32_t.
DarraUa_OpcUaFile_UserWritable(h)PropertiesReadWhether the current user has write access; returns int32_t
DarraUa_OpcUaFile_OpenCount(h)PropertiesReadCurrent number of open handles; returns uint16_t
DarraUa_OpcUaFile_IsOpen(h)PropertiesReadWhether it is in the "Open" state; returns int32_t
DarraUa_OpcUaFile_Open(h, mode)MethodsWriteOpen; "mode" is an XOR open mode
DarraUa_OpcUaFile_Read(h, length, &out_data, &out_n)MethodsReadRead length bytes
DarraUa_OpcUaFile_Write(h, data, length)MethodsWriteWrite to the current position
DarraUa_OpcUaFile_GetPosition(h, &out_pos)MethodsReadCurrent location, uint64_t
DarraUa_OpcUaFile_SetPosition(h, pos)MethodsWritePositioning
DarraUa_OpcUaFile_Close(h)MethodsWriteClose (Silent when not open; Good)
DarraUa_OpcUaFile_DownloadTo(h, local_path, progress, ctx)High-levelReadOne-Click Download
DarraUa_OpcUaFile_UploadFrom(h, local_path, progress, ctx)High-levelWriteOne-Click Upload

Related Structures:

typedef uint32_t DarraUa_OpcUaFileHandle;
#define UA_INVALID_OPCUA_FILE_HANDLE 0u

typedef enum {
UA_FILE_OPEN_READ = 0x01, /* Read (mutually exclusive WRITE ) */
UA_FILE_OPEN_WRITE = 0x02, /* Write */
UA_FILE_OPEN_ERASE_EXISTING = 0x04, /* Clear existing content; must be set WRITE along with . */
UA_FILE_OPEN_APPEND = 0x08 /* Move to the end and append */
} DarraUa_FileOpenMode;

typedef void (*DarraUa_OnFileProgress)(
uint64_t done_bytes,
uint64_t total_bytes, /* 0 = The server did not provide the Size */
void* user_context);

Memory Allocation for Read: out_data A byte array allocated on the heap SDK, using DarraUa_Free Release (not DataValue/Variant that one _Delete).EOF Or when it fails *out_data == NULL, *out_actual_len == 0.

Returns 0 if the property operation fails: Size / OpenCount / Writable / UserWritable A read failure always returns 0, which cannot be distinguished from "actually 0"; to distinguish between the two, read the status of DataValue the corresponding child node directly DarraUa_Session_ReadSafe.

Code Example​

#include <opcua.h>
#include <opcua_file_transfer.h>
#include <stdio.h>

static void on_progress(uint64_t done, uint64_t total, void* ctx) {
(void)ctx;
if (total == 0) { /* The server did not provide the size. */
printf("\r%llu bytes", (unsigned long long)done);
return;
}
printf("\r%llu%% (%llu/%llu)",
(unsigned long long)(done * 100 / total),
(unsigned long long)done,
(unsigned long long)total);
}

int main(void)
{
DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = "opc.tcp://device:4840";
/* ... DarraUa_Session_Create + (omitted DarraUa_Session_Connect ) ... */

/* 1) One-click firmware download */
DarraUa_OpcUaFileHandle file = UA_INVALID_OPCUA_FILE_HANDLE;
if (DarraUa_OpcUaFile_New(session, "ns=2;s=Firmware.Image", &file) != UA_STATUS_GOOD)
return 1;

printf("Size = %llu\n", (unsigned long long)DarraUa_OpcUaFile_Size(file));

DarraUa_Status rc = DarraUa_OpcUaFile_DownloadTo(file,
"C:\\dl\\firmware.bin", on_progress, NULL);
if (rc != UA_STATUS_GOOD) printf("\n下载失败 0x%08X\n", rc);
DarraUa_OpcUaFile_Free(file);

/* 2) Manual streaming read */
DarraUa_OpcUaFileHandle f3 = UA_INVALID_OPCUA_FILE_HANDLE;
DarraUa_OpcUaFile_New(session, "ns=2;s=Logs.Current", &f3);

DarraUa_OpcUaFile_Open(f3, UA_FILE_OPEN_READ);
for (;;) {
uint8_t* chunk = NULL;
size_t n = 0;
DarraUa_Status st = DarraUa_OpcUaFile_Read(f3, 4096, &chunk, &n);
if (st != UA_STATUS_GOOD || n == 0) {
DarraUa_Free(chunk); /* /When a EOFfailure occurs, the chunk is NULL, Free and secure */
break;
}
process_chunk(chunk, n);
DarraUa_Free(chunk); /* Free DarraUa_Free the buffer used by `Read` */
}
DarraUa_OpcUaFile_Close(f3);
DarraUa_OpcUaFile_Free(f3);

/* 3) Upload the recipe */
DarraUa_OpcUaFileHandle up = UA_INVALID_OPCUA_FILE_HANDLE;
DarraUa_OpcUaFile_New(session, "ns=2;s=Recipe.Current", &up);
DarraUa_OpcUaFile_UploadFrom(up, "C:\\recipes\\r01.csv", on_progress, NULL);
DarraUa_OpcUaFile_Free(up);
return 0;
}

Implementation Notes​

  • DownloadTo / UploadFrom For internal use 4 KB Block loop
  • DownloadTo Internal loop Open(UA_FILE_OPEN_READ) → Loop Read → Write to local → Close; UploadFrom Loop Open(UA_FILE_OPEN_WRITE | UA_FILE_OPEN_ERASE_EXISTING) → Loop Write → Close
  • ByteString Pass a pair uint8_t* + size_t of parameters; the caller uses them to free DarraUa_Free the buffer read from the Read function.
  • Method NodeId Analysis: Prioritize TranslateBrowsePaths analyzing the Method instance NodeId; if that fails, fall back to OPC UA the standard ns=0 class level NodeId
  • New Not fired on its own RPC; method resolution is deferred until the first call. Open

Best Practices​

  • Always in every branch Free; C does not have this. RAII
  • Retrying in Segments for Large Files
  • Do not access the same file handle concurrently
  • Situations total_bytes == 0 that Progress Callbacks Need to Handle

Cross-Language Comparison​

C#PythonJavaC++RustC
OpcUaFileOpcUaFileOpcUaFileOpcUaFileOpcUaFileDarraUa_OpcUaFile_New/_Free/_Open/_Close
DownloadTodownload_todownloadToDownloadTodownload_toDarraUa_OpcUaFile_DownloadTo
UploadFromupload_fromuploadFromUploadFromupload_fromDarraUa_OpcUaFile_UploadFrom

Next Step​