Skip to content

File syn_opcua.c

FileList > proto > syn_opcua.c

Go to the source code of this file

Micro OPC UA (IEC 62541) Binary Protocol Engine & Embedded Server Implementation.

  • #include "../port/syn_port_system.h"
  • #include "syn_opcua.h"
  • #include <string.h>

Classes

Type Name
struct OPCUA_Reader
Binary deserializer context for OPC UA messages.
struct OPCUA_Writer
Binary serializer context for OPC UA messages.

Public Functions

Type Name
SYN_OPCUA_Node * syn_opcua_server_find_node_num (SYN_OPCUA_Server * srv, uint16_t ns, uint32_t num_id)
Find a node in the address space by numeric NodeId.
SYN_Status syn_opcua_server_init (SYN_OPCUA_Server * srv, const SYN_OPCUA_Config * cfg)
Initialize Micro OPC UA Server.
SYN_Status syn_opcua_server_process_message (SYN_OPCUA_Server * srv, const uint8_t * rx, size_t rx_len, uint8_t * tx, size_t max_tx, size_t * out_len)
Process an incoming raw OPC UA frame and generate a response frame.
SYN_PT_Status syn_opcua_server_pt (SYN_PT * pt, SYN_Task * task)
Protothread continuation function for syn_sched .
SYN_Status syn_opcua_server_read_variable (const SYN_OPCUA_Server * srv, uint16_t ns, uint32_t num_id, SYN_OPCUA_Variant * out_val)
Read a variable node's value from the address space.
SYN_Status syn_opcua_server_register_node (SYN_OPCUA_Server * srv, const SYN_OPCUA_Node * node)
Register a Node in the server address space.
SYN_Status syn_opcua_server_step (SYN_OPCUA_Server * srv, uint32_t now_ms)
Advance OPC UA server state machine by one step / tick.
SYN_Status syn_opcua_server_write_variable (SYN_OPCUA_Server * srv, uint16_t ns, uint32_t num_id, const SYN_OPCUA_Variant * val)
Update a variable node's value in the address space.

Public Static Functions

Type Name
void opcua_populate_standard_nodes (SYN_OPCUA_Server * srv)
Populate default standard OPC UA address space folders and server status node.
bool opcua_read_nodeid_num (OPCUA_Reader * r, uint16_t * out_ns, uint32_t * out_num)
Read numeric OPC UA NodeId.
bool opcua_read_u16 (OPCUA_Reader * r, uint16_t * out)
Read 16-bit little-endian unsigned integer.
bool opcua_read_u32 (OPCUA_Reader * r, uint32_t * out)
Read 32-bit little-endian unsigned integer.
bool opcua_read_u8 (OPCUA_Reader * r, uint8_t * out)
Read 8-bit unsigned integer.
void opcua_reader_init (OPCUA_Reader * r, const uint8_t * buf, size_t size)
Initialize OPC UA binary reader.
bool opcua_write_bytes (OPCUA_Writer * w, const uint8_t * buf, size_t len)
Write raw byte buffer.
bool opcua_write_datavalue (OPCUA_Writer * w, const SYN_OPCUA_DataValue * dv)
Write OPC UA DataValue structure.
bool opcua_write_nodeid_num (OPCUA_Writer * w, uint16_t ns, uint32_t num)
Write numeric OPC UA NodeId.
bool opcua_write_string (OPCUA_Writer * w, const char * str)
Write length-prefixed OPC UA string.
bool opcua_write_u16 (OPCUA_Writer * w, uint16_t v)
Write 16-bit little-endian unsigned integer.
bool opcua_write_u32 (OPCUA_Writer * w, uint32_t v)
Write 32-bit little-endian unsigned integer.
bool opcua_write_u64 (OPCUA_Writer * w, uint64_t v)
Write 64-bit little-endian unsigned integer.
bool opcua_write_u8 (OPCUA_Writer * w, uint8_t v)
Write 8-bit unsigned integer.
bool opcua_write_variant (OPCUA_Writer * w, const SYN_OPCUA_Variant * var)
Write OPC UA variant value.
void opcua_writer_init (OPCUA_Writer * w, uint8_t * buf, size_t size)
Initialize OPC UA binary writer.

Macros

Type Name
define SYN_OPCUA_REQ_ACTIVATE_SESSION 467U
OPC UA ActivateSessionRequest NodeId.
define SYN_OPCUA_REQ_BROWSE 527U
OPC UA BrowseRequest NodeId.
define SYN_OPCUA_REQ_CLOSE_SESSION 473U
OPC UA CloseSessionRequest NodeId.
define SYN_OPCUA_REQ_CREATE_SESSION 461U
OPC UA CreateSessionRequest NodeId.
define SYN_OPCUA_REQ_READ 631U
OPC UA ReadRequest NodeId.
define SYN_OPCUA_REQ_WRITE 673U
OPC UA WriteRequest NodeId.
define SYN_OPCUA_RESP_ACTIVATE_SESSION 470U
OPC UA ActivateSessionResponse NodeId.
define SYN_OPCUA_RESP_BROWSE 530U
OPC UA BrowseResponse NodeId.
define SYN_OPCUA_RESP_CLOSE_SESSION 476U
OPC UA CloseSessionResponse NodeId.
define SYN_OPCUA_RESP_CREATE_SESSION 464U
OPC UA CreateSessionResponse NodeId.
define SYN_OPCUA_RESP_READ 634U
OPC UA ReadResponse NodeId.
define SYN_OPCUA_RESP_WRITE 676U
OPC UA WriteResponse NodeId.

Public Functions Documentation

function syn_opcua_server_find_node_num

Find a node in the address space by numeric NodeId.

SYN_OPCUA_Node * syn_opcua_server_find_node_num (
    SYN_OPCUA_Server * srv,
    uint16_t ns,
    uint32_t num_id
) 

Parameters:

  • srv Server instance.
  • ns Namespace index.
  • num_id Numeric identifier.

Returns:

Pointer to node in address space or NULL if not found.


function syn_opcua_server_init

Initialize Micro OPC UA Server.

SYN_Status syn_opcua_server_init (
    SYN_OPCUA_Server * srv,
    const SYN_OPCUA_Config * cfg
) 

Parameters:

  • srv Server instance to initialize.
  • cfg Server configuration.

Returns:

SYN_OK on success, SYN_INVALID_PARAM on invalid argument.


function syn_opcua_server_process_message

Process an incoming raw OPC UA frame and generate a response frame.

SYN_Status syn_opcua_server_process_message (
    SYN_OPCUA_Server * srv,
    const uint8_t * rx,
    size_t rx_len,
    uint8_t * tx,
    size_t max_tx,
    size_t * out_len
) 

Parameters:

  • srv Server instance.
  • rx Inbound raw message frame.
  • rx_len Inbound length in bytes.
  • tx Outbound response frame buffer.
  • max_tx Outbound buffer capacity.
  • out_len Pointer receiving generated response length.

Returns:

SYN_OK on success.


function syn_opcua_server_pt

Protothread continuation function for syn_sched .

SYN_PT_Status syn_opcua_server_pt (
    SYN_PT * pt,
    SYN_Task * task
) 

Parameters:

  • pt Protothread continuation structure.
  • task Task handle containing SYN_OPCUA_Server in user_data.

Returns:

SYN_PT_Status continuation status.


function syn_opcua_server_read_variable

Read a variable node's value from the address space.

SYN_Status syn_opcua_server_read_variable (
    const SYN_OPCUA_Server * srv,
    uint16_t ns,
    uint32_t num_id,
    SYN_OPCUA_Variant * out_val
) 

Parameters:

  • srv Server instance.
  • ns Namespace index.
  • num_id Numeric identifier.
  • out_val Output variant value pointer.

Returns:

SYN_OK on success, SYN_ERROR if not found.


function syn_opcua_server_register_node

Register a Node in the server address space.

SYN_Status syn_opcua_server_register_node (
    SYN_OPCUA_Server * srv,
    const SYN_OPCUA_Node * node
) 

Parameters:

  • srv Server instance.
  • node Node descriptor to copy into address space.

Returns:

SYN_OK on success, SYN_ERROR if address space is full.


function syn_opcua_server_step

Advance OPC UA server state machine by one step / tick.

SYN_Status syn_opcua_server_step (
    SYN_OPCUA_Server * srv,
    uint32_t now_ms
) 

Parameters:

  • srv Server instance.
  • now_ms Current system timestamp in milliseconds.

Returns:

SYN_OK on success.


function syn_opcua_server_write_variable

Update a variable node's value in the address space.

SYN_Status syn_opcua_server_write_variable (
    SYN_OPCUA_Server * srv,
    uint16_t ns,
    uint32_t num_id,
    const SYN_OPCUA_Variant * val
) 

Parameters:

  • srv Server instance.
  • ns Namespace index.
  • num_id Numeric identifier.
  • val New variant value.

Returns:

SYN_OK on success, SYN_ERROR if not found.


Public Static Functions Documentation

function opcua_populate_standard_nodes

Populate default standard OPC UA address space folders and server status node.

static void opcua_populate_standard_nodes (
    SYN_OPCUA_Server * srv
) 

Parameters:

  • srv Server instance pointer.

function opcua_read_nodeid_num

Read numeric OPC UA NodeId.

static bool opcua_read_nodeid_num (
    OPCUA_Reader * r,
    uint16_t * out_ns,
    uint32_t * out_num
) 

Parameters:

  • r Reader context.
  • out_ns Pointer to receive namespace index.
  • out_num Pointer to receive numeric identifier.

Returns:

True on success, false on invalid format or underflow.


function opcua_read_u16

Read 16-bit little-endian unsigned integer.

static bool opcua_read_u16 (
    OPCUA_Reader * r,
    uint16_t * out
) 

Parameters:

  • r Reader context.
  • out Pointer to receive value.

Returns:

True on success, false on underflow.


function opcua_read_u32

Read 32-bit little-endian unsigned integer.

static bool opcua_read_u32 (
    OPCUA_Reader * r,
    uint32_t * out
) 

Parameters:

  • r Reader context.
  • out Pointer to receive value.

Returns:

True on success, false on underflow.


function opcua_read_u8

Read 8-bit unsigned integer.

static bool opcua_read_u8 (
    OPCUA_Reader * r,
    uint8_t * out
) 

Parameters:

  • r Reader context.
  • out Pointer to receive value.

Returns:

True on success, false on underflow.


function opcua_reader_init

Initialize OPC UA binary reader.

static void opcua_reader_init (
    OPCUA_Reader * r,
    const uint8_t * buf,
    size_t size
) 

Parameters:

  • r Reader context.
  • buf Input buffer.
  • size Input buffer size.

function opcua_write_bytes

Write raw byte buffer.

static bool opcua_write_bytes (
    OPCUA_Writer * w,
    const uint8_t * buf,
    size_t len
) 

Parameters:

  • w Writer context.
  • buf Source bytes.
  • len Number of bytes.

Returns:

True on success, false on overflow.


function opcua_write_datavalue

Write OPC UA DataValue structure.

static bool opcua_write_datavalue (
    OPCUA_Writer * w,
    const SYN_OPCUA_DataValue * dv
) 

Parameters:

  • w Writer context.
  • dv DataValue structure pointer.

Returns:

True on success, false on error.


function opcua_write_nodeid_num

Write numeric OPC UA NodeId.

static bool opcua_write_nodeid_num (
    OPCUA_Writer * w,
    uint16_t ns,
    uint32_t num
) 

Parameters:

  • w Writer context.
  • ns Namespace index.
  • num Numeric identifier.

Returns:

True on success, false on overflow.


function opcua_write_string

Write length-prefixed OPC UA string.

static bool opcua_write_string (
    OPCUA_Writer * w,
    const char * str
) 

Parameters:

  • w Writer context.
  • str Null-terminated string or NULL for null string.

Returns:

True on success, false on overflow.


function opcua_write_u16

Write 16-bit little-endian unsigned integer.

static bool opcua_write_u16 (
    OPCUA_Writer * w,
    uint16_t v
) 

Parameters:

  • w Writer context.
  • v Value to write.

Returns:

True on success, false on overflow.


function opcua_write_u32

Write 32-bit little-endian unsigned integer.

static bool opcua_write_u32 (
    OPCUA_Writer * w,
    uint32_t v
) 

Parameters:

  • w Writer context.
  • v Value to write.

Returns:

True on success, false on overflow.


function opcua_write_u64

Write 64-bit little-endian unsigned integer.

static bool opcua_write_u64 (
    OPCUA_Writer * w,
    uint64_t v
) 

Parameters:

  • w Writer context.
  • v Value to write.

Returns:

True on success, false on overflow.


function opcua_write_u8

Write 8-bit unsigned integer.

static bool opcua_write_u8 (
    OPCUA_Writer * w,
    uint8_t v
) 

Parameters:

  • w Writer context.
  • v Value to write.

Returns:

True on success, false on overflow.


function opcua_write_variant

Write OPC UA variant value.

static bool opcua_write_variant (
    OPCUA_Writer * w,
    const SYN_OPCUA_Variant * var
) 

Parameters:

  • w Writer context.
  • var Variant structure pointer.

Returns:

True on success, false on serialization error.


function opcua_writer_init

Initialize OPC UA binary writer.

static void opcua_writer_init (
    OPCUA_Writer * w,
    uint8_t * buf,
    size_t size
) 

Parameters:

  • w Writer context.
  • buf Output buffer.
  • size Output buffer size.

Macro Definition Documentation

define SYN_OPCUA_REQ_ACTIVATE_SESSION

OPC UA ActivateSessionRequest NodeId.

#define SYN_OPCUA_REQ_ACTIVATE_SESSION `467U`


define SYN_OPCUA_REQ_BROWSE

OPC UA BrowseRequest NodeId.

#define SYN_OPCUA_REQ_BROWSE `527U`


define SYN_OPCUA_REQ_CLOSE_SESSION

OPC UA CloseSessionRequest NodeId.

#define SYN_OPCUA_REQ_CLOSE_SESSION `473U`


define SYN_OPCUA_REQ_CREATE_SESSION

OPC UA CreateSessionRequest NodeId.

#define SYN_OPCUA_REQ_CREATE_SESSION `461U`


define SYN_OPCUA_REQ_READ

OPC UA ReadRequest NodeId.

#define SYN_OPCUA_REQ_READ `631U`


define SYN_OPCUA_REQ_WRITE

OPC UA WriteRequest NodeId.

#define SYN_OPCUA_REQ_WRITE `673U`


define SYN_OPCUA_RESP_ACTIVATE_SESSION

OPC UA ActivateSessionResponse NodeId.

#define SYN_OPCUA_RESP_ACTIVATE_SESSION `470U`


define SYN_OPCUA_RESP_BROWSE

OPC UA BrowseResponse NodeId.

#define SYN_OPCUA_RESP_BROWSE `530U`


define SYN_OPCUA_RESP_CLOSE_SESSION

OPC UA CloseSessionResponse NodeId.

#define SYN_OPCUA_RESP_CLOSE_SESSION `476U`


define SYN_OPCUA_RESP_CREATE_SESSION

OPC UA CreateSessionResponse NodeId.

#define SYN_OPCUA_RESP_CREATE_SESSION `464U`


define SYN_OPCUA_RESP_READ

OPC UA ReadResponse NodeId.

#define SYN_OPCUA_RESP_READ `634U`


define SYN_OPCUA_RESP_WRITE

OPC UA WriteResponse NodeId.

#define SYN_OPCUA_RESP_WRITE `676U`



The documentation for this class was generated from the following file src/syntropic/proto/syn_opcua.c