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
| Function | Category | Reading and Writing | Note |
|---|---|---|---|
| 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) | Properties | Read | Total number of bytes in the file; returns uint64_t |
| DarraUa_OpcUaFile_Writable(h) | Properties | Read | Is it writable at the server level? Returns int32_t. |
| DarraUa_OpcUaFile_UserWritable(h) | Properties | Read | Whether the current user has write access; returns int32_t |
| DarraUa_OpcUaFile_OpenCount(h) | Properties | Read | Current number of open handles; returns uint16_t |
| DarraUa_OpcUaFile_IsOpen(h) | Properties | Read | Whether it is in the "Open" state; returns int32_t |
| DarraUa_OpcUaFile_Open(h, mode) | Methods | Write | Open; "mode" is an XOR open mode |
| DarraUa_OpcUaFile_Read(h, length, &out_data, &out_n) | Methods | Read | Read length bytes |
| DarraUa_OpcUaFile_Write(h, data, length) | Methods | Write | Write to the current position |
| DarraUa_OpcUaFile_GetPosition(h, &out_pos) | Methods | Read | Current location, uint64_t |
| DarraUa_OpcUaFile_SetPosition(h, pos) | Methods | Write | Positioning |
| DarraUa_OpcUaFile_Close(h) | Methods | Write | Close (Silent when not open; Good) |
| DarraUa_OpcUaFile_DownloadTo(h, local_path, progress, ctx) | High-level | Read | One-Click Download |
| DarraUa_OpcUaFile_UploadFrom(h, local_path, progress, ctx) | High-level | Write | One-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/UploadFromFor internal use 4 KB Block loopDownloadToInternal loopOpen(UA_FILE_OPEN_READ)→ LoopRead→ Write to local →Close;UploadFromLoopOpen(UA_FILE_OPEN_WRITE | UA_FILE_OPEN_ERASE_EXISTING)→ LoopWrite→Close- ByteString Pass a pair
uint8_t* + size_tof parameters; the caller uses them to freeDarraUa_Freethe buffer read from theReadfunction. - 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
NewNot 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 == 0that Progress Callbacks Need to Handle
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | OpcUaFile | DarraUa_OpcUaFile_New/_Free/_Open/_Close |
| DownloadTo | download_to | downloadTo | DownloadTo | download_to | DarraUa_OpcUaFile_DownloadTo |
| UploadFrom | upload_from | uploadFrom | UploadFrom | upload_from | DarraUa_OpcUaFile_UploadFrom |