본문으로 건너뛰기

파일 전송 (DarraUa_OpcUaFile_*)

개요​

OPC UA Part 5 §10 6개의 메서드(Open/Read/Write/Close/GetPosition/SetPosition)를 FileType 노출합니다.SDK 이를 하나의 그룹으로 캡슐화하고, DarraUa_OpcUaFile_* C API 여기에 파일 전체를 다루는 DownloadTo 및 UploadFrom이라는 두 가지 고수준 캡슐화를 추가합니다.

API​

함수분류읽기 및 쓰기설명
DarraUa_OpcUaFile_New(session, file_node_id, &out_h)구조—FileType 인스턴스 바인딩, 전송하지 않음 RPC
DarraUa_OpcUaFile_Free(h)해방—소멸자 (닫지 않으면 자동으로 닫힘)
DarraUa_OpcUaFile_Size(h)속성읽기파일의 총 바이트 수, uint64_t 반환
DarraUa_OpcUaFile_Writable(h)속성읽기서버 측에서 작성 가능한지 여부, int32_t 반환
DarraUa_OpcUaFile_UserWritable(h)속성읽기현재 사용자가 쓰기 권한이 있는지 여부를 반환합니다. 반환 값: int32_t
DarraUa_OpcUaFile_OpenCount(h)속성읽기현재 열린 핸들 수, uint16_t 반환
DarraUa_OpcUaFile_IsOpen(h)속성읽기'Open' 상태인지 여부를 반환하며, 반환 값은 int32_t입니다.
DarraUa_OpcUaFile_Open(h, mode)방법쓰기열기, mode는 비트별 OR 연산을 사용하는 열기 모드입니다.
DarraUa_OpcUaFile_Read(h, length, &out_data, &out_n)방법읽기length 바이트 읽기
DarraUa_OpcUaFile_Write(h, data, length)방법쓰기현재 위치까지 작성
DarraUa_OpcUaFile_GetPosition(h, &out_pos)방법읽기현재 위치, uint64_t
DarraUa_OpcUaFile_SetPosition(h, pos)방법쓰기위치
DarraUa_OpcUaFile_Close(h)방법쓰기닫기 (열려 있지 않을 때 무음, Good)
DarraUa_OpcUaFile_DownloadTo(h, local_path, progress, ctx)고층읽기원클릭 다운로드
DarraUa_OpcUaFile_UploadFrom(h, local_path, progress, ctx)고층쓰기원클릭 업로드

관련 구조:

typedef uint32_t DarraUa_OpcUaFileHandle;
#define UA_INVALID_OPCUA_FILE_HANDLE 0u

typedef enum {
UA_FILE_OPEN_READ = 0x01, /* 읽기 (쓰기와 배타적 WRITE ) */
UA_FILE_OPEN_WRITE = 0x02, /* 쓰기 */
UA_FILE_OPEN_ERASE_EXISTING = 0x04, /* 기존 내용을 지우려면 반드시 와 함께 WRITE 설정해야 합니다. */
UA_FILE_OPEN_APPEND = 0x08 /* 끝으로 이동하여 추가 */
} DarraUa_FileOpenMode;

typedef void (*DarraUa_OnFileProgress)(
uint64_t done_bytes,
uint64_t total_bytes, /* 0 = 서버에서 Size를 제공하지 않음 */
void* user_context);

Read의 메모리 할당: SDK 힙에서 할당된 바이트 배열이며 out_data , DarraUa_Free 해제 ( DataValue그게Variant 아님 _Delete).EOF 또는 실패할 때 *out_data == NULL, *out_actual_len == 0.

속성 호출 실패 시 0을 반환합니다.: Size / OpenCount / Writable / UserWritable 읽기 실패 시 항상 0을 반환하므로, "실제로 0인 경우"와 구별할 수 없습니다. 구별하려면 해당 자노드를 DarraUa_Session_ReadSafe로 직접 읽어 그 DataValue 상태를 확인하십시오.

코드 예시​

#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) { /* 서버에서 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 + DarraUa_Session_Connect 생략 ... */

/* 1) 원클릭 펌웨어 다운로드 */
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) 수동 스트리밍 읽기 */
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); /* EOF/실패 시 chunk는 NULL, Free 안전 */
break;
}
process_chunk(chunk, n);
DarraUa_Free(chunk); /* Read의 버퍼 해제 DarraUa_Free */
}
DarraUa_OpcUaFile_Close(f3);
DarraUa_OpcUaFile_Free(f3);

/* 3) 레시피 업로드 */
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;
}

구현 설명​

  • DownloadTo / UploadFrom 내부용 4 KB 블록별 반복
  • DownloadTo 내부로 이동 Open(UA_FILE_OPEN_READ) → 반복 Read → 로컬에 쓰기 → Close; UploadFrom 이동 Open(UA_FILE_OPEN_WRITE | UA_FILE_OPEN_ERASE_EXISTING) → 반복 Write → Close
  • ByteString uint8_t* + size_t 한 쌍의 매개변수를 전달하고, 호출 측에서 Read로 읽어낸 버퍼를 DarraUa_Free 해제합니다.
  • Method NodeId 분석: 우선 TranslateBrowsePaths 분석 대상 Method NodeId, 실패 시 OPC UA 표준 ns= 0 클래스 수준으로 회귀 NodeId
  • New 본체에서 호출하지 않음 RPC, 메서드 분석은 첫 호출 시까지 지연됨 Open

모범 사례​

  • 항상 각 분기에서 호출되지만 Free, C에서는 그렇지 않다. RAII
  • 대용량 파일 분할 재시도
  • 동일한 파일 핸들을 동시에 사용하지 마십시오.
  • 진행 상황 콜백에서 처리해야 할 total_bytes == 0 상황

다국어 대조​

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

다음 단계​