pdcomm_opcua_client_connect
Function
Connects a client and creates a session.
int32_t pdcomm_opcua_client_connect(const uint8_t *endpoint, size_t size, const pdcomm_opcua_client_options *options, pdcomm_opcua_handle *out)Discovers the endpoints of endpoint, selects the one whose SecurityPolicy and MessageSecurityMode equal the options, opens the secure channel (OpenSecureChannel) and creates a session (CreateSession). The server certificate is validated against trusted_server_der, and for a secure policy the client certificate must be valid, match its private key and contain the application URI. The session is not activated yet: call pdcomm_opcua_client_activate_anonymous() or pdcomm_opcua_client_activate_user_name() next.
memset(&options, 0, sizeof(options));
options.struct_size = (uint32_t)sizeof(options);
options.version = 1;
status = pdcomm_opcua_client_options_init(&options);
if (status != PDCOMM_OPCUA_OK) { print_error("options", status); goto done; }
options.timeout_ms = timeout;
options.policy = policy;
options.mode = mode;
options.flags = policy == PDCOMM_OPCUA_POLICY_NONE ? PDCOMM_OPCUA_ALLOW_NONE : 0;
options.certificate_der.data = certificate;
options.certificate_der.size = certificate_size;
options.private_key_pem.data = key;
options.private_key_pem.size = key_size;
options.trusted_server_der.data = trust;
options.trusted_server_der.size = trust_size;
status = pdcomm_opcua_client_connect((const uint8_t *)argv[1], strlen(argv[1]), &options, &client);
// The SDK copied the identity; wipe the private key now.
example_wipe(key, key_size);
key = NULL;
if (status != PDCOMM_OPCUA_OK) { print_error("connect", status); goto done; }
status = pdcomm_opcua_client_activate_anonymous(client);
(from examples/opcua/opcua_client.c)
Parameters
| Name | Description |
|---|---|
endpoint | opc.tcp:// URL in UTF-8, 1 to 4096 bytes. |
size | Size of endpoint in bytes. |
options | Initialized options; copied during the call only. |
out | Receives the client handle. |
Returns
#PDCOMM_OPCUA_OK; #PDCOMM_OPCUA_INVALID_INPUT for invalid options (an unknown flag, policy or mode, a policy/mode mismatch, None without #PDCOMM_OPCUA_ALLOW_NONE, a missing identity for a secure policy, a certificate or key that cannot be parsed); #PDCOMM_OPCUA_RUNTIME_ERROR when the endpoint is not advertised, the server or the client identity is rejected or the network fails; #PDCOMM_OPCUA_LIMIT; or #PDCOMM_OPCUA_LICENSE_REQUIRED without an opcua-client license.
Notes
- Blocks for up to
timeout_msper network exchange. The private key is copied; wipe the caller's copy after this call.