UA_StatusCode getTypesHierarchy(void *nsCtx, const UA_NodeId *leafType, size_t leafTypeSize, UA_NodeId **typeHierarchy, size_t *typeHierarchySize, UA_Boolean walkDownwards) { UA_NodeId *results = NULL; size_t results_count = 0; for (size_t i = 0; i < leafTypeSize; i++) { UA_NodeId *tmpResults = NULL; size_t tmpResults_size = 0; UA_StatusCode retval = getTypeHierarchy(nsCtx, &leafType[i], &tmpResults, &tmpResults_size, walkDownwards); if(retval != UA_STATUSCODE_GOOD) { UA_Array_delete(results, results_count, &UA_TYPES[UA_TYPES_NODEID]); return retval; } if (tmpResults_size == 0 || tmpResults == NULL ) continue; size_t new_size = sizeof(UA_NodeId) * (results_count + tmpResults_size); UA_NodeId *new_results = (UA_NodeId*)UA_realloc(results, new_size); if(!new_results) { UA_Array_delete(results, results_count, &UA_TYPES[UA_TYPES_NODEID]); return UA_STATUSCODE_BADOUTOFMEMORY; } results = new_results; memcpy(&results[results_count],tmpResults, sizeof(UA_NodeId)*tmpResults_size); /* do not use UA_Array_delete since we still need the content of the nodes */ UA_free(tmpResults); results_count = results_count + tmpResults_size; } *typeHierarchy = results; *typeHierarchySize = results_count; return UA_STATUSCODE_GOOD; }
static void FindOnNetworkAndCheck(UA_String expectedServerNames[], size_t expectedServerNamesSize, const char *filterUri, const char *filterLocale, const char** filterCapabilities, size_t filterCapabilitiesSize) { UA_Client *client = UA_Client_new(); UA_ClientConfig_setDefault(UA_Client_getConfig(client)); UA_ServerOnNetwork* serverOnNetwork = NULL; size_t serverOnNetworkSize = 0; size_t serverCapabilityFilterSize = 0; UA_String *serverCapabilityFilter = NULL; if(filterCapabilitiesSize) { serverCapabilityFilterSize = filterCapabilitiesSize; serverCapabilityFilter = (UA_String*)UA_malloc(sizeof(UA_String) * filterCapabilitiesSize); for(size_t i = 0; i < filterCapabilitiesSize; i++) serverCapabilityFilter[i] = UA_String_fromChars(filterCapabilities[i]); } UA_StatusCode retval = UA_Client_findServersOnNetwork(client, "opc.tcp://localhost:4840", 0, 0, serverCapabilityFilterSize, serverCapabilityFilter, &serverOnNetworkSize, &serverOnNetwork); if(serverCapabilityFilterSize) UA_Array_delete(serverCapabilityFilter, serverCapabilityFilterSize, &UA_TYPES[UA_TYPES_STRING]); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); // only the discovery server is expected ck_assert_uint_eq(serverOnNetworkSize , expectedServerNamesSize); if(expectedServerNamesSize > 0) ck_assert_ptr_ne(serverOnNetwork, NULL); if(serverOnNetwork != NULL) { for(size_t i = 0; i < expectedServerNamesSize; i++) { UA_Boolean expectedServerNameInServerOnNetwork = false; for(size_t j = 0; j < expectedServerNamesSize && !expectedServerNameInServerOnNetwork; j++) { expectedServerNameInServerOnNetwork = UA_String_equal(&serverOnNetwork[j].serverName, &expectedServerNames[i]); } ck_assert_msg(expectedServerNameInServerOnNetwork, "Expected %.*s in serverOnNetwork list, but not found", expectedServerNames[i].length, expectedServerNames[i].data); } } UA_Array_delete(serverOnNetwork, serverOnNetworkSize, &UA_TYPES[UA_TYPES_SERVERONNETWORK]); UA_Client_disconnect(client); UA_Client_delete(client); }
/** * Gets a list of nodeIds which are referenced by this node. * Hereby the list will be filtered to only include references * which are of the given `references` types. */ static UA_StatusCode getReferencedNodesWithReferenceType(UA_NodeId **results_ptr, size_t *results_count, size_t *results_size, const UA_Node *node, const UA_NodeReferenceKind *references, size_t referencesSize) { UA_NodeId *results = *results_ptr; for(size_t i = 0; i < node->referencesSize; ++i) { /* Is the reference kind relevant? */ UA_NodeReferenceKind *refs = &node->references[i]; bool referenceInList = false; for (size_t j=0; j<referencesSize && !referenceInList; j++) { referenceInList = refs->isInverse == references[j].isInverse && UA_NodeId_equal(&references[j].referenceTypeId, &refs->referenceTypeId); } if (!referenceInList) continue; /* Append all targets of the reference kind .. if not a duplicate */ for(size_t j = 0; j < refs->targetIdsSize; ++j) { /* Is the target a duplicate? (multi-inheritance) */ UA_NodeId *targetId = &refs->targetIds[j].nodeId; UA_Boolean duplicate = false; for(size_t k = 0; k < *results_count; ++k) { if(UA_NodeId_equal(targetId, &results[k])) { duplicate = true; break; } } if(duplicate) continue; /* Increase array length if necessary */ if(*results_count >= *results_size) { size_t new_size = sizeof(UA_NodeId) * (*results_size) * 2; UA_NodeId *new_results = (UA_NodeId*)UA_realloc(results, new_size); if(!new_results) { UA_Array_delete(results, *results_count, &UA_TYPES[UA_TYPES_NODEID]); return UA_STATUSCODE_BADOUTOFMEMORY; } results = new_results; *results_ptr = results; *results_size *= 2; } /* Copy new nodeid to the end of the list */ UA_StatusCode retval = UA_NodeId_copy(targetId, &results[*results_count]); if(retval != UA_STATUSCODE_GOOD) { UA_Array_delete(results, *results_count, &UA_TYPES[UA_TYPES_NODEID]); return retval; } *results_count += 1; } } return UA_STATUSCODE_GOOD; }
END_TEST START_TEST(arrayCopyShallMakeADeepCopy) { // given UA_String a1[3]; a1[0] = (UA_String){1, (UA_Byte*)"a"}; a1[1] = (UA_String){2, (UA_Byte*)"bb"}; a1[2] = (UA_String){3, (UA_Byte*)"ccc"}; // when UA_String *a2; UA_Int32 retval = UA_Array_copy((const void *)a1, 3, &UA_TYPES[UA_STRING], (void **)&a2); // then ck_assert_int_eq(retval, UA_STATUSCODE_GOOD); ck_assert_int_eq(a1[0].length, 1); ck_assert_int_eq(a1[1].length, 2); ck_assert_int_eq(a1[2].length, 3); ck_assert_int_eq(a1[0].length, a2[0].length); ck_assert_int_eq(a1[1].length, a2[1].length); ck_assert_int_eq(a1[2].length, a2[2].length); ck_assert_ptr_ne(a1[0].data, a2[0].data); ck_assert_ptr_ne(a1[1].data, a2[1].data); ck_assert_ptr_ne(a1[2].data, a2[2].data); ck_assert_int_eq(a1[0].data[0], a2[0].data[0]); ck_assert_int_eq(a1[1].data[0], a2[1].data[0]); ck_assert_int_eq(a1[2].data[0], a2[2].data[0]); // finally UA_Array_delete((void *)a2, 3, &UA_TYPES[UA_STRING]); }
static void GetEndpointsAndCheck(const char* discoveryUrl, const char* filterTransportProfileUri, const UA_String expectedEndpointUrls[], size_t expectedEndpointUrlsSize) { UA_Client *client = UA_Client_new(); UA_ClientConfig_setDefault(UA_Client_getConfig(client)); ck_assert_uint_eq(UA_Client_connect(client, discoveryUrl), UA_STATUSCODE_GOOD); UA_EndpointDescription* endpointArray = NULL; size_t endpointArraySize = 0; UA_String discoveryUrlUA = UA_String_fromChars(discoveryUrl); UA_StatusCode retval = GetEndpoints(client, &discoveryUrlUA, &endpointArraySize, &endpointArray, filterTransportProfileUri); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); UA_String_deleteMembers(&discoveryUrlUA); ck_assert_uint_eq(endpointArraySize , expectedEndpointUrlsSize); for(size_t j = 0; j < endpointArraySize && j < expectedEndpointUrlsSize; j++) { UA_EndpointDescription* endpoint = &endpointArray[j]; ck_assert(UA_String_equal(&endpoint->endpointUrl, &expectedEndpointUrls[j])); } UA_Array_delete(endpointArray, endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); UA_Client_delete(client); }
static void UA_Node_deleteMembers(UA_Node *p) { UA_NodeId_deleteMembers(&p->nodeId); UA_QualifiedName_deleteMembers(&p->browseName); UA_LocalizedText_deleteMembers(&p->displayName); UA_LocalizedText_deleteMembers(&p->description); UA_Array_delete(p->references, &UA_TYPES[UA_TYPES_REFERENCENODE], p->referencesSize); }
static UA_Int64 sendReadRequest(ConnectionInfo *connectionInfo, UA_Int32 nodeIds_size,UA_NodeId* nodeIds){ /*UA_Int32 sock, UA_UInt32 channelId, UA_UInt32 tokenId, UA_UInt32 sequenceNumber, UA_UInt32 requestId, UA_NodeId authenticationToken, UA_Int32 nodeIds_size,UA_NodeId* nodeIds) { */ UA_ByteString *message = UA_ByteString_new(); UA_ByteString_newMembers(message, 65536); UA_UInt32 tmpChannelId = connectionInfo->channelId; size_t offset = 0; UA_TcpMessageHeader msghdr; msghdr.messageTypeAndFinal = UA_MESSAGETYPEANDFINAL_MSGF; UA_NodeId type; type.identifier.numeric = 631; type.identifierType = UA_NODEIDTYPE_NUMERIC; type.namespaceIndex = 0; UA_ReadRequest rq; UA_ReadRequest_init(&rq); rq.maxAge = 0; rq.nodesToRead = UA_Array_new(&UA_TYPES[UA_TYPES_READVALUEID], nodeIds_size); rq.nodesToReadSize = 1; for(UA_Int32 i=0;i<nodeIds_size;i++) { UA_ReadValueId_init(&(rq.nodesToRead[i])); rq.nodesToRead[i].attributeId = 6; //WriteMask UA_NodeId_init(&(rq.nodesToRead[i].nodeId)); rq.nodesToRead[i].nodeId = nodeIds[i]; UA_QualifiedName_init(&(rq.nodesToRead[0].dataEncoding)); } rq.requestHeader.timeoutHint = 10000; rq.requestHeader.timestamp = UA_DateTime_now(); rq.requestHeader.authenticationToken = connectionInfo->authenticationToken; rq.timestampsToReturn = 0x03; rq.requestHeader.requestHandle = 1 + connectionInfo->sequenceHdr.requestId; msghdr.messageSize = 16 + UA_TcpMessageHeader_calcSizeBinary(&msghdr) + UA_NodeId_calcSizeBinary(&type) + UA_ReadRequest_calcSizeBinary(&rq); UA_TcpMessageHeader_encodeBinary(&msghdr,message,&offset); UA_UInt32_encodeBinary(&tmpChannelId, message, &offset); UA_UInt32_encodeBinary(&connectionInfo->tokenId, message, &offset); UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.sequenceNumber, message, &offset); UA_UInt32_encodeBinary(&connectionInfo->sequenceHdr.requestId, message, &offset); UA_NodeId_encodeBinary(&type,message,&offset); UA_ReadRequest_encodeBinary(&rq, message, &offset); UA_DateTime tic = UA_DateTime_now(); UA_Int32 sendret = send(connectionInfo->socket, message->data, offset, 0); UA_Array_delete(rq.nodesToRead, &UA_TYPES[UA_TYPES_READVALUEID], nodeIds_size); UA_ByteString_delete(message); if (sendret < 0) { printf("send readrequest failed"); return 1; } return tic; }
/* * Get the endpoint from the server, where we can call RegisterServer2 (or RegisterServer). * This is normally the endpoint with highest supported encryption mode. * * @param discoveryServerUrl The discovery url from the remote server * @return The endpoint description (which needs to be freed) or NULL */ static UA_EndpointDescription *getRegisterEndpointFromServer(const char *discoveryServerUrl) { UA_Client *client = UA_Client_new(UA_ClientConfig_default); UA_EndpointDescription *endpointArray = NULL; size_t endpointArraySize = 0; UA_StatusCode retval = UA_Client_getEndpoints(client, discoveryServerUrl, &endpointArraySize, &endpointArray); if (retval != UA_STATUSCODE_GOOD) { UA_Array_delete(endpointArray, endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); UA_LOG_ERROR(logger, UA_LOGCATEGORY_SERVER, "GetEndpoints failed with %s", UA_StatusCode_name(retval)); UA_Client_delete(client); return NULL; } UA_LOG_DEBUG(logger, UA_LOGCATEGORY_SERVER, "Server has %ld endpoints", endpointArraySize); UA_EndpointDescription *foundEndpoint = NULL; for (size_t i = 0; i < endpointArraySize; i++) { UA_LOG_DEBUG(logger, UA_LOGCATEGORY_SERVER, "\tURL = %.*s, SecurityMode = %s", (int) endpointArray[i].endpointUrl.length, endpointArray[i].endpointUrl.data, endpointArray[i].securityMode == UA_MESSAGESECURITYMODE_NONE ? "None" : endpointArray[i].securityMode == UA_MESSAGESECURITYMODE_SIGN ? "Sign" : endpointArray[i].securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT ? "SignAndEncrypt" : "Invalid" ); // find the endpoint with highest supported security mode if ((UA_String_equal(&endpointArray[i].securityPolicyUri, &UA_SECURITY_POLICY_NONE_URI) || UA_String_equal(&endpointArray[i].securityPolicyUri, &UA_SECURITY_POLICY_BASIC128_URI)) && ( foundEndpoint == NULL || foundEndpoint->securityMode < endpointArray[i].securityMode)) foundEndpoint = &endpointArray[i]; } UA_EndpointDescription *returnEndpoint = NULL; if (foundEndpoint != NULL) { returnEndpoint = UA_EndpointDescription_new(); UA_EndpointDescription_copy(foundEndpoint, returnEndpoint); } UA_Array_delete(endpointArray, endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); return returnEndpoint; }
static UA_StatusCode getHierarchyForReferenceTypes(void *nsCtx, const UA_NodeId *leafType, const UA_NodeReferenceKind *references, size_t referencesSize, UA_NodeId **typeHierarchy, size_t *typeHierarchySize) { /* Allocate the results array. Probably too big, but saves mallocs. */ size_t results_size = 20; UA_NodeId *results = (UA_NodeId*)UA_malloc(sizeof(UA_NodeId) * results_size); if(!results) return UA_STATUSCODE_BADOUTOFMEMORY; /* The leaf is the first element */ size_t results_count = 1; UA_StatusCode retval = UA_NodeId_copy(leafType, &results[0]); if(retval != UA_STATUSCODE_GOOD) { UA_free(results); return retval; } /* Loop over the array members .. and add new elements to the end */ for(size_t idx = 0; idx < results_count; ++idx) { /* Get the node */ const UA_Node *node = UA_Nodestore_getNode(nsCtx, &results[idx]); /* Invalid node, remove from the array */ if(!node) { for(size_t i = idx; i < results_count-1; ++i) results[i] = results[i+1]; results_count--; continue; } /* Add references from the current node to the end of the array */ retval = getReferencedNodesWithReferenceType(&results, &results_count, &results_size, node, references, referencesSize); /* Release the node */ UA_Nodestore_releaseNode(nsCtx, node); if(retval != UA_STATUSCODE_GOOD) { UA_Array_delete(results, results_count, &UA_TYPES[UA_TYPES_NODEID]); return retval; } } /* Zero results. The leaf node was not found */ if(results_count == 0) { UA_free(results); results = NULL; } *typeHierarchy = results; *typeHierarchySize = results_count; return UA_STATUSCODE_GOOD; }
static void teardown(void) { removeSubscription(); *running = false; THREAD_JOIN(server_thread); UA_Server_run_shutdown(server); UA_Boolean_delete(running); UA_Server_delete(server); UA_ServerConfig_delete(config); UA_Array_delete(selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]); UA_Client_disconnect(client); UA_Client_delete(client); }
END_TEST START_TEST(Server_HistorizingUpdateUpdate) { UA_HistoryDataBackend backend = UA_HistoryDataBackend_Memory(1, 1); UA_HistorizingNodeIdSettings setting; setting.historizingBackend = backend; setting.maxHistoryDataResponseSize = 1000; setting.historizingUpdateStrategy = UA_HISTORIZINGUPDATESTRATEGY_USER; serverMutexLock(); UA_StatusCode ret = gathering->registerNodeId(server, gathering->context, &outNodeId, setting); serverMutexUnlock(); ck_assert_str_eq(UA_StatusCode_name(ret), UA_StatusCode_name(UA_STATUSCODE_GOOD)); // fill backend with insert ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_INSERT, testData, NULL, NULL)) , UA_StatusCode_name(UA_STATUSCODE_GOOD)); testResult(testDataSorted, NULL); // delete some values ck_assert_str_eq(UA_StatusCode_name(deleteHistory(DELETE_START_TIME, DELETE_STOP_TIME)), UA_StatusCode_name(UA_STATUSCODE_GOOD)); testResult(testDataAfterDelete, NULL); // update all and insert some UA_StatusCode *result; size_t resultSize = 0; ck_assert_str_eq(UA_StatusCode_name(updateHistory(UA_PERFORMUPDATETYPE_UPDATE, testDataSorted, &result, &resultSize)) , UA_StatusCode_name(UA_STATUSCODE_GOOD)); for (size_t i = 0; i < resultSize; ++i) { ck_assert_str_eq(UA_StatusCode_name(result[i]), UA_StatusCode_name(testDataUpdateResult[i])); } UA_Array_delete(result, resultSize, &UA_TYPES[UA_TYPES_STATUSCODE]); UA_HistoryData data; UA_HistoryData_init(&data); testResult(testDataSorted, &data); for (size_t i = 0; i < data.dataValuesSize; ++i) { ck_assert_uint_eq(data.dataValues[i].hasValue, true); ck_assert(data.dataValues[i].value.type == &UA_TYPES[UA_TYPES_INT64]); ck_assert_uint_eq(*((UA_Int64*)data.dataValues[i].value.data), UA_PERFORMUPDATETYPE_UPDATE); } UA_HistoryData_deleteMembers(&data); UA_HistoryDataBackend_Memory_deleteMembers(&setting.historizingBackend); }
END_TEST START_TEST(Client_methodcall) { UA_Client *client = UA_Client_new(UA_ClientConfig_default); UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840"); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); UA_UInt32 subId; retval = UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_default, &subId); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); /* monitor the server state */ UA_UInt32 monId; retval = UA_Client_Subscriptions_addMonitoredItem(client, subId, UA_NODEID_NUMERIC(0, 2259), UA_ATTRIBUTEID_VALUE, NULL, NULL, &monId); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); /* call a method to get monitored item id */ UA_Variant input; UA_Variant_init(&input); UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]); size_t outputSize; UA_Variant *output; retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); ck_assert_uint_eq(outputSize, 2); ck_assert_uint_eq(output[0].arrayLength, 1); ck_assert_uint_eq(*((UA_UInt32*)output[0].data), monId); UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]); UA_Variant_deleteMembers(&input); /* call with invalid subscription id */ UA_Variant_init(&input); subId = 0; UA_Variant_setScalarCopy(&input, &subId, &UA_TYPES[UA_TYPES_UINT32]); retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER), UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_GETMONITOREDITEMS), 1, &input, &outputSize, &output); ck_assert_uint_eq(retval, UA_STATUSCODE_BADSUBSCRIPTIONIDINVALID); UA_Variant_deleteMembers(&input); UA_Client_disconnect(client); UA_Client_delete(client); }
static UA_StatusCode GetEndpoints(UA_Client *client, const UA_String* endpointUrl, size_t* endpointDescriptionsSize, UA_EndpointDescription** endpointDescriptions, const char* filterTransportProfileUri) { UA_GetEndpointsRequest request; UA_GetEndpointsRequest_init(&request); //request.requestHeader.authenticationToken = client->authenticationToken; request.requestHeader.timestamp = UA_DateTime_now(); request.requestHeader.timeoutHint = 10000; request.endpointUrl = *endpointUrl; // assume the endpointurl outlives the service call if (filterTransportProfileUri) { request.profileUrisSize = 1; request.profileUris = (UA_String*)UA_malloc(sizeof(UA_String)); request.profileUris[0] = UA_String_fromChars(filterTransportProfileUri); } UA_GetEndpointsResponse response; UA_GetEndpointsResponse_init(&response); __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_GETENDPOINTSREQUEST], &response, &UA_TYPES[UA_TYPES_GETENDPOINTSRESPONSE]); if (filterTransportProfileUri) { UA_Array_delete(request.profileUris, request.profileUrisSize, &UA_TYPES[UA_TYPES_STRING]); } ck_assert_uint_eq(response.responseHeader.serviceResult, UA_STATUSCODE_GOOD); *endpointDescriptionsSize = response.endpointsSize; *endpointDescriptions = (UA_EndpointDescription*)UA_Array_new(response.endpointsSize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); for(size_t i=0;i<response.endpointsSize;i++) { UA_EndpointDescription_init(&(*endpointDescriptions)[i]); UA_EndpointDescription_copy(&response.endpoints[i], &(*endpointDescriptions)[i]); } UA_GetEndpointsResponse_deleteMembers(&response); return UA_STATUSCODE_GOOD; }
void UA_Node_deleteMembersAnyNodeClass(UA_Node *node) { /* delete standard content */ UA_NodeId_deleteMembers(&node->nodeId); UA_QualifiedName_deleteMembers(&node->browseName); UA_LocalizedText_deleteMembers(&node->displayName); UA_LocalizedText_deleteMembers(&node->description); UA_Array_delete(node->references, node->referencesSize, &UA_TYPES[UA_TYPES_REFERENCENODE]); node->references = NULL; node->referencesSize = 0; /* delete unique content of the nodeclass */ switch(node->nodeClass) { case UA_NODECLASS_OBJECT: break; case UA_NODECLASS_METHOD: break; case UA_NODECLASS_OBJECTTYPE: break; case UA_NODECLASS_VARIABLE: case UA_NODECLASS_VARIABLETYPE: { UA_VariableNode *p = (UA_VariableNode*)node; if(p->valueSource == UA_VALUESOURCE_VARIANT) UA_Variant_deleteMembers(&p->value.variant.value); break; } case UA_NODECLASS_REFERENCETYPE: { UA_ReferenceTypeNode *p = (UA_ReferenceTypeNode*)node; UA_LocalizedText_deleteMembers(&p->inverseName); break; } case UA_NODECLASS_DATATYPE: break; case UA_NODECLASS_VIEW: break; default: break; } }
static void setupSelectClauses(void) { // check for severity (set manually), message (set manually), eventType (automatic) and sourceNode (automatic) selectClauses = (UA_SimpleAttributeOperand *) UA_Array_new(nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]); if (!selectClauses) return; for (size_t i = 0; i < nSelectClauses; ++i) { UA_SimpleAttributeOperand_init(&selectClauses[i]); selectClauses[i].typeDefinitionId = UA_NODEID_NUMERIC(0, UA_NS0ID_BASEEVENTTYPE); selectClauses[i].browsePathSize = 1; selectClauses[i].attributeId = UA_ATTRIBUTEID_VALUE; selectClauses[i].browsePath = (UA_QualifiedName *) UA_Array_new(selectClauses[i].browsePathSize, &UA_TYPES[UA_TYPES_QUALIFIEDNAME]); if (!selectClauses[i].browsePathSize) { UA_Array_delete(selectClauses, nSelectClauses, &UA_TYPES[UA_TYPES_SIMPLEATTRIBUTEOPERAND]); } } selectClauses[0].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Severity"); selectClauses[1].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "Message"); selectClauses[2].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "EventType"); selectClauses[3].browsePath[0] = UA_QUALIFIEDNAME_ALLOC(0, "SourceNode"); }
UA_Int32 UA_SecureConversationMessageFooter_deleteMembers(UA_SecureConversationMessageFooter* p) { UA_Int32 retval = UA_SUCCESS; retval |= UA_Array_delete((void***)&p->padding,p->paddingSize,UA_BYTE); return retval; }
/* Combination of UA_Client_getEndpointsInternal and getEndpoints */ static void responseGetEndpoints(UA_Client *client, void *userdata, UA_UInt32 requestId, void *response) { UA_EndpointDescription* endpointArray = NULL; size_t endpointArraySize = 0; UA_GetEndpointsResponse* resp; resp = (UA_GetEndpointsResponse*)response; if (resp->responseHeader.serviceResult != UA_STATUSCODE_GOOD) { client->connectStatus = resp->responseHeader.serviceResult; UA_LOG_ERROR(&client->config.logger, UA_LOGCATEGORY_CLIENT, "GetEndpointRequest failed with error code %s", UA_StatusCode_name (client->connectStatus)); UA_GetEndpointsResponse_deleteMembers(resp); return; } endpointArray = resp->endpoints; endpointArraySize = resp->endpointsSize; resp->endpoints = NULL; resp->endpointsSize = 0; UA_Boolean endpointFound = false; UA_Boolean tokenFound = false; UA_String securityNone = UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#None"); UA_String binaryTransport = UA_STRING("http://opcfoundation.org/UA-Profile/" "Transport/uatcp-uasc-uabinary"); // TODO: compare endpoint information with client->endpointUri for(size_t i = 0; i < endpointArraySize; ++i) { UA_EndpointDescription* endpoint = &endpointArray[i]; /* look out for binary transport endpoints */ /* Note: Siemens returns empty ProfileUrl, we will accept it as binary */ if(endpoint->transportProfileUri.length != 0 && !UA_String_equal (&endpoint->transportProfileUri, &binaryTransport)) continue; /* Look for an endpoint corresponding to the client security policy */ if(!UA_String_equal(&endpoint->securityPolicyUri, &client->channel.securityPolicy->policyUri)) continue; endpointFound = true; /* Look for a user token policy with an anonymous token */ for(size_t j = 0; j < endpoint->userIdentityTokensSize; ++j) { UA_UserTokenPolicy* userToken = &endpoint->userIdentityTokens[j]; /* Usertokens also have a security policy... */ if(userToken->securityPolicyUri.length > 0 && !UA_String_equal(&userToken->securityPolicyUri, &securityNone)) continue; /* Does the token type match the client configuration? */ if((userToken->tokenType == UA_USERTOKENTYPE_ANONYMOUS && client->config.userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_ANONYMOUSIDENTITYTOKEN] && client->config.userIdentityToken.content.decoded.type != NULL) || (userToken->tokenType == UA_USERTOKENTYPE_USERNAME && client->config.userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_USERNAMEIDENTITYTOKEN]) || (userToken->tokenType == UA_USERTOKENTYPE_CERTIFICATE && client->config.userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_X509IDENTITYTOKEN]) || (userToken->tokenType == UA_USERTOKENTYPE_ISSUEDTOKEN && client->config.userIdentityToken.content.decoded.type != &UA_TYPES[UA_TYPES_ISSUEDIDENTITYTOKEN])) continue; /* Endpoint with matching usertokenpolicy found */ tokenFound = true; UA_EndpointDescription_deleteMembers(&client->config.endpoint); UA_EndpointDescription_copy(endpoint, &client->config.endpoint); UA_UserTokenPolicy_deleteMembers(&client->config.userTokenPolicy); UA_UserTokenPolicy_copy(userToken, &client->config.userTokenPolicy); break; } } UA_Array_delete(endpointArray, endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); if(!endpointFound) { UA_LOG_ERROR(&client->config.logger, UA_LOGCATEGORY_CLIENT, "No suitable endpoint found"); client->connectStatus = UA_STATUSCODE_BADINTERNALERROR; } else if(!tokenFound) { UA_LOG_ERROR(&client->config.logger, UA_LOGCATEGORY_CLIENT, "No suitable UserTokenPolicy found for the possible endpoints"); client->connectStatus = UA_STATUSCODE_BADINTERNALERROR; } requestSession(client, &requestId); }
int main(int argc, char *argv[]) { int defaultParams = argc < 8; //start parameters if(defaultParams) { printf("1st parameter: number of nodes to read \n"); printf("2nd parameter: number of read-tries \n"); printf("3rd parameter: name of the file to save measurement data \n"); printf("4th parameter: 1 = read same node, 0 = read different nodes \n"); printf("5th parameter: ip adress \n"); printf("6th parameter: port \n"); printf("7th parameter: 0=stateful, 1=stateless\n"); printf("8th parameter: 0=tcp, 1=udp (only with stateless calls)\n"); printf("\nUsing default parameters. \n"); } UA_UInt32 nodesToReadSize; UA_UInt32 tries; UA_Boolean alwaysSameNode; UA_ByteString reply; UA_ByteString_newMembers(&reply, 65536); UA_Boolean stateless; UA_Boolean udp; if(defaultParams) nodesToReadSize = 1; else nodesToReadSize = atoi(argv[1]); if(defaultParams) tries= 2; else tries = (UA_UInt32) atoi(argv[2]); if(defaultParams){ alwaysSameNode = UA_TRUE; }else{ if(atoi(argv[4]) != 0) alwaysSameNode = UA_TRUE; else alwaysSameNode = UA_FALSE; } if(defaultParams){ stateless = UA_FALSE; }else{ if(atoi(argv[7]) != 0) stateless = UA_TRUE; else stateless = UA_FALSE; } if(defaultParams){ udp = UA_FALSE; }else{ if(atoi(argv[8]) != 0) udp = UA_TRUE; else udp = UA_FALSE; } //Connect to remote server UA_String endpoint; UA_String_copycstring("none",&endpoint); ConnectionInfo connectionInfo; /* REQUEST START*/ UA_NodeId *nodesToRead; nodesToRead = UA_Array_new(&UA_TYPES[UA_TYPES_NODEID], 1); for(UA_UInt32 i = 0; i<1; i++) { if(alwaysSameNode) nodesToRead[i].identifier.numeric = 2253; //ask always the same node else nodesToRead[i].identifier.numeric = 19000 +i; nodesToRead[i].identifierType = UA_NODEIDTYPE_NUMERIC; nodesToRead[i].namespaceIndex = 0; } UA_DateTime tic, toc; UA_Double *timeDiffs; UA_Int32 received; timeDiffs = UA_Array_new(&UA_TYPES[UA_TYPES_DOUBLE], tries); UA_Double sum = 0; tic = UA_DateTime_now(); /** UA_Double duration; UA_UInt32 count = 0; UA_Double start = 0, stop = 0; UA_UInt32 timeToRun = 30; UA_UInt32 timeToStart = 8; UA_UInt32 timeToStop = 22; do{ toc = UA_DateTime_now(); duration = ((UA_Double)toc-(UA_Double)tic)/(UA_Double)1e4; if(duration>=timeToStart*1000 && duration <= timeToStop*1000){ if(start==0.0){ start=UA_DateTime_now(); } } //if(stateless || (!stateless && i==0)){ if(defaultParams){ if(ua_client_connectUA("127.0.0.1",atoi("16664"),&endpoint,&connectionInfo,stateless,udp) != 0){ return 0; } }else{ if(ua_client_connectUA(argv[5],atoi(argv[6]),&endpoint,&connectionInfo,stateless,udp) != 0){ return 0; } } //} sendReadRequest(&connectionInfo,1,nodesToRead); received = recv(connectionInfo.socket, reply.data, 2000, 0); if(duration>=timeToStart*1000 && duration <= timeToStop*1000){ count++; } if(!stateless){ closeSession(&connectionInfo); recv(connectionInfo.socket, reply.data, 2000, 0); closeSecureChannel(&connectionInfo); } //if(stateless || (!stateless && i==tries-1)){ close(connectionInfo.socket); //} if(duration >= timeToStop*1000 && stop==0){ stop=UA_DateTime_now(); printf("%i messages in %f secs, rate %f m/s\n", count, (stop-start)/(UA_Double)1e7, (UA_Double)count/((stop-start)/(UA_Double)1e7)); } }while(duration<timeToRun*1000); exit(0); **/ for(UA_UInt32 i = 0; i < tries; i++) { //if(stateless || (!stateless && i==0)){ tic = UA_DateTime_now(); if(defaultParams){ if(ua_client_connectUA("127.0.0.1",atoi("16664"),&endpoint,&connectionInfo,stateless,udp) != 0){ return 0; } }else{ if(ua_client_connectUA(argv[5],atoi(argv[6]),&endpoint,&connectionInfo,stateless,udp) != 0){ return 0; } } //} for(UA_UInt32 i = 0; i < nodesToReadSize; i++) { sendReadRequest(&connectionInfo,1,nodesToRead); received = recv(connectionInfo.socket, reply.data, 2000, 0); } if(!stateless){ closeSession(&connectionInfo); recv(connectionInfo.socket, reply.data, 2000, 0); closeSecureChannel(&connectionInfo); } //if(stateless || (!stateless && i==tries-1)){ close(connectionInfo.socket); //} toc = UA_DateTime_now() - tic; timeDiffs[i] = (UA_Double)toc/(UA_Double)1e4; sum = sum + timeDiffs[i]; } /* REQUEST END*/ UA_Double mean = sum / tries; printf("mean time for handling request: %16.10f ms \n",mean); if(received>0) printf("received: %i\n",received); // dummy //save to file char data[100]; const char flag = 'a'; FILE* fHandle = UA_NULL; if (defaultParams) { fHandle = fopen("client.log", &flag); }else{ fHandle = fopen(argv[3], &flag); } //header UA_Int32 bytesToWrite = sprintf(data, "measurement %s in ms, nodesToRead %d \n", argv[3], 1); fwrite(data,1,bytesToWrite,fHandle); for(UA_UInt32 i=0;i<tries;i++) { bytesToWrite = sprintf(data,"%16.10f \n",timeDiffs[i]); fwrite(data,1,bytesToWrite,fHandle); } fclose(fHandle); UA_String_deleteMembers(&reply); UA_Array_delete(nodesToRead,&UA_TYPES[UA_TYPES_NODEID], 1); UA_free(timeDiffs); return 0; }
static UA_StatusCode register_server_with_discovery_server(UA_Server *server, UA_Client *client, const UA_Boolean isUnregister, const char* semaphoreFilePath) { /* Prepare the request. Do not cleanup the request after the service call, * as the members are stack-allocated or point into the server config. */ UA_RegisterServer2Request request; UA_RegisterServer2Request_init(&request); request.requestHeader.timestamp = UA_DateTime_now(); request.requestHeader.timeoutHint = 10000; request.server.isOnline = !isUnregister; request.server.serverUri = server->config.applicationDescription.applicationUri; request.server.productUri = server->config.applicationDescription.productUri; request.server.serverType = server->config.applicationDescription.applicationType; request.server.gatewayServerUri = server->config.applicationDescription.gatewayServerUri; if(semaphoreFilePath) { #ifdef UA_ENABLE_DISCOVERY_SEMAPHORE request.server.semaphoreFilePath = UA_STRING((char*)(uintptr_t)semaphoreFilePath); /* dirty cast */ #else UA_LOG_WARNING(&server->config.logger, UA_LOGCATEGORY_CLIENT, "Ignoring semaphore file path. open62541 not compiled " "with UA_ENABLE_DISCOVERY_SEMAPHORE=ON"); #endif } request.server.serverNames = &server->config.applicationDescription.applicationName; request.server.serverNamesSize = 1; /* Copy the discovery urls from the server config and the network layers*/ size_t config_discurls = server->config.applicationDescription.discoveryUrlsSize; size_t nl_discurls = server->config.networkLayersSize; size_t total_discurls = config_discurls + nl_discurls; UA_STACKARRAY(UA_String, urlsBuf, total_discurls); request.server.discoveryUrls = urlsBuf; request.server.discoveryUrlsSize = total_discurls; for(size_t i = 0; i < config_discurls; ++i) request.server.discoveryUrls[i] = server->config.applicationDescription.discoveryUrls[i]; /* TODO: Add nl only if discoveryUrl not already present */ for(size_t i = 0; i < nl_discurls; ++i) { UA_ServerNetworkLayer *nl = &server->config.networkLayers[i]; request.server.discoveryUrls[config_discurls + i] = nl->discoveryUrl; } #ifdef UA_ENABLE_DISCOVERY_MULTICAST request.discoveryConfigurationSize = 1; request.discoveryConfiguration = UA_ExtensionObject_new(); UA_ExtensionObject_init(&request.discoveryConfiguration[0]); // Set to NODELETE so that we can just use a pointer to the mdns config request.discoveryConfiguration[0].encoding = UA_EXTENSIONOBJECT_DECODED_NODELETE; request.discoveryConfiguration[0].content.decoded.type = &UA_TYPES[UA_TYPES_MDNSDISCOVERYCONFIGURATION]; request.discoveryConfiguration[0].content.decoded.data = &server->config.discovery.mdns; #endif // First try with RegisterServer2, if that isn't implemented, use RegisterServer UA_RegisterServer2Response response; __UA_Client_Service(client, &request, &UA_TYPES[UA_TYPES_REGISTERSERVER2REQUEST], &response, &UA_TYPES[UA_TYPES_REGISTERSERVER2RESPONSE]); UA_StatusCode serviceResult = response.responseHeader.serviceResult; UA_RegisterServer2Response_deleteMembers(&response); UA_Array_delete(request.discoveryConfiguration, request.discoveryConfigurationSize, &UA_TYPES[UA_TYPES_EXTENSIONOBJECT]); request.discoveryConfiguration = NULL; request.discoveryConfigurationSize = 0; if(serviceResult == UA_STATUSCODE_BADNOTIMPLEMENTED || serviceResult == UA_STATUSCODE_BADSERVICEUNSUPPORTED) { /* Try RegisterServer */ UA_RegisterServerRequest request_fallback; UA_RegisterServerRequest_init(&request_fallback); /* Copy from RegisterServer2 request */ request_fallback.requestHeader = request.requestHeader; request_fallback.server = request.server; UA_RegisterServerResponse response_fallback; __UA_Client_Service(client, &request_fallback, &UA_TYPES[UA_TYPES_REGISTERSERVERREQUEST], &response_fallback, &UA_TYPES[UA_TYPES_REGISTERSERVERRESPONSE]); serviceResult = response_fallback.responseHeader.serviceResult; UA_RegisterServerResponse_deleteMembers(&response_fallback); } if(serviceResult != UA_STATUSCODE_GOOD) { UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_CLIENT, "RegisterServer/RegisterServer2 failed with statuscode %s", UA_StatusCode_name(serviceResult)); } return serviceResult; }
int main(int argc, char *argv[]) { UA_Client *client = UA_Client_new(UA_ClientConfig_standard); //listing endpoints UA_EndpointDescription* endpointArray = NULL; size_t endpointArraySize = 0; UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:16664", &endpointArraySize, &endpointArray); //freeing the endpointArray if(retval != UA_STATUSCODE_GOOD) { //cleanup array UA_Array_delete(endpointArray,endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); UA_Client_delete(client); return (int)retval; } printf("%i endpoints found\n", (int)endpointArraySize); for(size_t i=0;i<endpointArraySize;i++){ printf("URL of endpoint %i is %.*s\n", (int)i, (int)endpointArray[i].endpointUrl.length, endpointArray[i].endpointUrl.data); } //cleanup array of enpoints UA_Array_delete(endpointArray,endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); //connect to a server //anonymous connect would be: retval = UA_Client_connect_username(client, UA_ClientConnectionTCP, "opc.tcp://localhost:16664"); retval = UA_Client_connect_username(client, "opc.tcp://localhost:16664", "user1", "password"); if(retval != UA_STATUSCODE_GOOD) { UA_Client_delete(client); return (int)retval; } // Browse some objects printf("Browsing nodes in objects folder:\n"); UA_BrowseRequest bReq; UA_BrowseRequest_init(&bReq); bReq.requestedMaxReferencesPerNode = 0; bReq.nodesToBrowse = UA_BrowseDescription_new(); bReq.nodesToBrowseSize = 1; bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); //browse objects folder bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; //return everything UA_BrowseResponse bResp = UA_Client_Service_browse(client, bReq); printf("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME"); for (size_t i = 0; i < bResp.resultsSize; ++i) { for (size_t j = 0; j < bResp.results[i].referencesSize; ++j) { UA_ReferenceDescription *ref = &(bResp.results[i].references[j]); if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) { printf("%-9d %-16d %-16.*s %-16.*s\n", ref->browseName.namespaceIndex, ref->nodeId.nodeId.identifier.numeric, (int)ref->browseName.name.length, ref->browseName.name.data, (int)ref->displayName.text.length, ref->displayName.text.data); } else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) { printf("%-9d %-16.*s %-16.*s %-16.*s\n", ref->browseName.namespaceIndex, (int)ref->nodeId.nodeId.identifier.string.length, ref->nodeId.nodeId.identifier.string.data, (int)ref->browseName.name.length, ref->browseName.name.data, (int)ref->displayName.text.length, ref->displayName.text.data); } //TODO: distinguish further types } } UA_BrowseRequest_deleteMembers(&bReq); UA_BrowseResponse_deleteMembers(&bResp); // Same thing, this time using the node iterator... UA_NodeId *parent = UA_NodeId_new(); *parent = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); UA_Client_forEachChildNodeCall(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), nodeIter, (void *) parent); UA_NodeId_delete(parent); #ifdef UA_ENABLE_SUBSCRIPTIONS // Create a subscription with interval 0 (immediate)... UA_UInt32 subId=0; UA_Client_Subscriptions_new(client, UA_SubscriptionSettings_standard, &subId); if(subId) printf("Create subscription succeeded, id %u\n", subId); // .. and monitor TheAnswer UA_NodeId monitorThis = UA_NODEID_STRING(1, "the.answer"); UA_UInt32 monId=0; UA_Client_Subscriptions_addMonitoredItem(client, subId, monitorThis, UA_ATTRIBUTEID_VALUE, &handler_TheAnswerChanged, NULL, &monId); if (monId) printf("Monitoring 'the.answer', id %u\n", subId); // First Publish always generates data (current value) and call out handler. UA_Client_Subscriptions_manuallySendPublishRequest(client); // This should not generate anything UA_Client_Subscriptions_manuallySendPublishRequest(client); #endif UA_Int32 value = 0; // Read node's value printf("\nReading the value of node (1, \"the.answer\"):\n"); UA_ReadRequest rReq; UA_ReadRequest_init(&rReq); rReq.nodesToRead = UA_Array_new(1, &UA_TYPES[UA_TYPES_READVALUEID]); rReq.nodesToReadSize = 1; rReq.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */ rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE; UA_ReadResponse rResp = UA_Client_Service_read(client, rReq); if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD && rResp.resultsSize > 0 && rResp.results[0].hasValue && UA_Variant_isScalar(&rResp.results[0].value) && rResp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]) { value = *(UA_Int32*)rResp.results[0].value.data; printf("the value is: %i\n", value); } UA_ReadRequest_deleteMembers(&rReq); UA_ReadResponse_deleteMembers(&rResp); value++; // Write node's value printf("\nWriting a value of node (1, \"the.answer\"):\n"); UA_WriteRequest wReq; UA_WriteRequest_init(&wReq); wReq.nodesToWrite = UA_WriteValue_new(); wReq.nodesToWriteSize = 1; wReq.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */ wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE; wReq.nodesToWrite[0].value.hasValue = true; wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_INT32]; wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE; //do not free the integer on deletion wReq.nodesToWrite[0].value.value.data = &value; UA_WriteResponse wResp = UA_Client_Service_write(client, wReq); if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD) printf("the new value is: %i\n", value); UA_WriteRequest_deleteMembers(&wReq); UA_WriteResponse_deleteMembers(&wResp); // Alternate Form, this time using the hl API value++; UA_Variant *myVariant = UA_Variant_new(); UA_Variant_setScalarCopy(myVariant, &value, &UA_TYPES[UA_TYPES_INT32]); UA_Client_writeValueAttribute(client, UA_NODEID_STRING(1, "the.answer"), myVariant); UA_Variant_delete(myVariant); #ifdef UA_ENABLE_SUBSCRIPTIONS // Take another look at the.answer... this should call the handler. UA_Client_Subscriptions_manuallySendPublishRequest(client); // Delete our subscription (which also unmonitors all items) if(!UA_Client_Subscriptions_remove(client, subId)) printf("Subscription removed\n"); #endif #ifdef UA_ENABLE_METHODCALLS /* Note: This example requires Namespace 0 Node 11489 (ServerType -> GetMonitoredItems) FIXME: Provide a namespace 0 independant example on the server side */ UA_Variant input; UA_String argString = UA_STRING("Hello Server"); UA_Variant_init(&input); UA_Variant_setScalarCopy(&input, &argString, &UA_TYPES[UA_TYPES_STRING]); size_t outputSize; UA_Variant *output; retval = UA_Client_call(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(1, 62541), 1, &input, &outputSize, &output); if(retval == UA_STATUSCODE_GOOD) { printf("Method call was successfull, and %lu returned values available.\n", (unsigned long)outputSize); UA_Array_delete(output, outputSize, &UA_TYPES[UA_TYPES_VARIANT]); } else { printf("Method call was unsuccessfull, and %x returned values available.\n", retval); } UA_Variant_deleteMembers(&input); #endif #ifdef UA_ENABLE_NODEMANAGEMENT /* New ReferenceType */ UA_NodeId ref_id; UA_ReferenceTypeAttributes ref_attr; UA_ReferenceTypeAttributes_init(&ref_attr); ref_attr.displayName = UA_LOCALIZEDTEXT("en_US", "NewReference"); ref_attr.description = UA_LOCALIZEDTEXT("en_US", "References something that might or might not exist"); ref_attr.inverseName = UA_LOCALIZEDTEXT("en_US", "IsNewlyReferencedBy"); retval = UA_Client_addReferenceTypeNode(client, UA_NODEID_NUMERIC(1, 12133), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_NODEID_NUMERIC(0, UA_NS0ID_HASSUBTYPE), UA_QUALIFIEDNAME(1, "NewReference"), ref_attr, &ref_id); if(retval == UA_STATUSCODE_GOOD ) printf("Created 'NewReference' with numeric NodeID %u\n", ref_id.identifier.numeric); /* New ObjectType */ UA_NodeId objt_id; UA_ObjectTypeAttributes objt_attr; UA_ObjectTypeAttributes_init(&objt_attr); objt_attr.displayName = UA_LOCALIZEDTEXT("en_US", "TheNewObjectType"); objt_attr.description = UA_LOCALIZEDTEXT("en_US", "Put innovative description here"); retval = UA_Client_addObjectTypeNode(client, UA_NODEID_NUMERIC(1, 12134), UA_NODEID_NUMERIC(0, UA_NS0ID_BASEOBJECTTYPE), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "NewObjectType"), objt_attr, &objt_id); if(retval == UA_STATUSCODE_GOOD) printf("Created 'NewObjectType' with numeric NodeID %u\n", objt_id.identifier.numeric); /* New Object */ UA_NodeId obj_id; UA_ObjectAttributes obj_attr; UA_ObjectAttributes_init(&obj_attr); obj_attr.displayName = UA_LOCALIZEDTEXT("en_US", "TheNewGreatNode"); obj_attr.description = UA_LOCALIZEDTEXT("de_DE", "Hier koennte Ihre Webung stehen!"); retval = UA_Client_addObjectNode(client, UA_NODEID_NUMERIC(1, 0), UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(1, "TheGreatNode"), UA_NODEID_NUMERIC(1, 12134), obj_attr, &obj_id); if(retval == UA_STATUSCODE_GOOD ) printf("Created 'NewObject' with numeric NodeID %u\n", obj_id.identifier.numeric); /* New Integer Variable */ UA_NodeId var_id; UA_VariableAttributes var_attr; UA_VariableAttributes_init(&var_attr); var_attr.displayName = UA_LOCALIZEDTEXT("en_US", "TheNewVariableNode"); var_attr.description = UA_LOCALIZEDTEXT("en_US", "This integer is just amazing - it has digits and everything."); UA_Int32 int_value = 1234; /* This does not copy the value */ UA_Variant_setScalar(&var_attr.value, &int_value, &UA_TYPES[UA_TYPES_INT32]); var_attr.dataType = UA_TYPES[UA_TYPES_INT32].typeId; retval = UA_Client_addVariableNode(client, UA_NODEID_NUMERIC(1, 0), // Assign new/random NodeID UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_QUALIFIEDNAME(0, "VariableNode"), UA_NODEID_NULL, // no variable type var_attr, &var_id); if(retval == UA_STATUSCODE_GOOD ) printf("Created 'NewVariable' with numeric NodeID %u\n", var_id.identifier.numeric); #endif UA_Client_disconnect(client); UA_Client_delete(client); return (int) UA_STATUSCODE_GOOD; }
END_TEST static void FindAndCheck(const UA_String expectedUris[], size_t expectedUrisSize, const UA_String expectedLocales[], const UA_String expectedNames[], const char *filterUri, const char *filterLocale) { UA_Client *client = UA_Client_new(); UA_ClientConfig_setDefault(UA_Client_getConfig(client)); UA_ApplicationDescription* applicationDescriptionArray = NULL; size_t applicationDescriptionArraySize = 0; size_t serverUrisSize = 0; UA_String *serverUris = NULL; if(filterUri) { serverUrisSize = 1; serverUris = UA_String_new(); serverUris[0] = UA_String_fromChars(filterUri); } size_t localeIdsSize = 0; UA_String *localeIds = NULL; if(filterLocale) { localeIdsSize = 1; localeIds = UA_String_new(); localeIds[0] = UA_String_fromChars(filterLocale); } UA_StatusCode retval = UA_Client_findServers(client, "opc.tcp://localhost:4840", serverUrisSize, serverUris, localeIdsSize, localeIds, &applicationDescriptionArraySize, &applicationDescriptionArray); if(filterUri) UA_Array_delete(serverUris, serverUrisSize, &UA_TYPES[UA_TYPES_STRING]); if(filterLocale) UA_Array_delete(localeIds, localeIdsSize, &UA_TYPES[UA_TYPES_STRING]); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); // only the discovery server is expected ck_assert_uint_eq(applicationDescriptionArraySize, expectedUrisSize); ck_assert(applicationDescriptionArray != NULL); for(size_t i = 0; i < expectedUrisSize; ++i) { ck_assert(UA_String_equal(&applicationDescriptionArray[i].applicationUri, &expectedUris[i])); if(expectedNames) ck_assert(UA_String_equal(&applicationDescriptionArray[i].applicationName.text, &expectedNames[i])); if (expectedLocales) ck_assert(UA_String_equal(&applicationDescriptionArray[i].applicationName.locale, &expectedLocales[i])); } UA_Array_delete(applicationDescriptionArray, applicationDescriptionArraySize, &UA_TYPES[UA_TYPES_APPLICATIONDESCRIPTION]); UA_Client_disconnect(client); UA_Client_delete(client); }
/* Adds a one-way reference to the local nodestore */ static UA_StatusCode addOneWayReferenceWithSession(UA_Server *server, UA_Session *session, const UA_AddReferencesItem *item) { const UA_Node *node = UA_NodeStore_get(server->nodestore, &item->sourceNodeId); if(!node) return UA_STATUSCODE_BADINTERNALERROR; UA_StatusCode retval = UA_STATUSCODE_GOOD; #ifndef UA_MULTITHREADING size_t i = node->referencesSize; if(node->referencesSize < 0) i = 0; size_t refssize = (i+1) | 3; // so the realloc is not necessary every time UA_ReferenceNode *new_refs = UA_realloc(node->references, sizeof(UA_ReferenceNode) * refssize); if(!new_refs) retval = UA_STATUSCODE_BADOUTOFMEMORY; else { UA_ReferenceNode_init(&new_refs[i]); retval = UA_NodeId_copy(&item->referenceTypeId, &new_refs[i].referenceTypeId); new_refs[i].isInverse = !item->isForward; retval |= UA_ExpandedNodeId_copy(&item->targetNodeId, &new_refs[i].targetId); /* hack. be careful! possible only in the single-threaded case. */ UA_Node *mutable_node = (UA_Node*)(uintptr_t)node; mutable_node->references = new_refs; if(retval != UA_STATUSCODE_GOOD) { UA_NodeId_deleteMembers(&new_refs[node->referencesSize].referenceTypeId); UA_ExpandedNodeId_deleteMembers(&new_refs[node->referencesSize].targetId); } else mutable_node->referencesSize = i+1; } UA_NodeStore_release(node); return retval; #else UA_Node *newNode = UA_NULL; void (*deleteNode)(UA_Node*) = UA_NULL; switch(node->nodeClass) { case UA_NODECLASS_OBJECT: newNode = (UA_Node*)UA_ObjectNode_new(); UA_ObjectNode_copy((const UA_ObjectNode*)node, (UA_ObjectNode*)newNode); deleteNode = (void (*)(UA_Node*))UA_ObjectNode_delete; break; case UA_NODECLASS_VARIABLE: newNode = (UA_Node*)UA_VariableNode_new(); UA_VariableNode_copy((const UA_VariableNode*)node, (UA_VariableNode*)newNode); deleteNode = (void (*)(UA_Node*))UA_VariableNode_delete; break; case UA_NODECLASS_METHOD: newNode = (UA_Node*)UA_MethodNode_new(); UA_MethodNode_copy((const UA_MethodNode*)node, (UA_MethodNode*)newNode); deleteNode = (void (*)(UA_Node*))UA_MethodNode_delete; break; case UA_NODECLASS_OBJECTTYPE: newNode = (UA_Node*)UA_ObjectTypeNode_new(); UA_ObjectTypeNode_copy((const UA_ObjectTypeNode*)node, (UA_ObjectTypeNode*)newNode); deleteNode = (void (*)(UA_Node*))UA_ObjectTypeNode_delete; break; case UA_NODECLASS_VARIABLETYPE: newNode = (UA_Node*)UA_VariableTypeNode_new(); UA_VariableTypeNode_copy((const UA_VariableTypeNode*)node, (UA_VariableTypeNode*)newNode); deleteNode = (void (*)(UA_Node*))UA_VariableTypeNode_delete; break; case UA_NODECLASS_REFERENCETYPE: newNode = (UA_Node*)UA_ReferenceTypeNode_new(); UA_ReferenceTypeNode_copy((const UA_ReferenceTypeNode*)node, (UA_ReferenceTypeNode*)newNode); deleteNode = (void (*)(UA_Node*))UA_ReferenceTypeNode_delete; break; case UA_NODECLASS_DATATYPE: newNode = (UA_Node*)UA_DataTypeNode_new(); UA_DataTypeNode_copy((const UA_DataTypeNode*)node, (UA_DataTypeNode*)newNode); deleteNode = (void (*)(UA_Node*))UA_DataTypeNode_delete; break; case UA_NODECLASS_VIEW: newNode = (UA_Node*)UA_ViewNode_new(); UA_ViewNode_copy((const UA_ViewNode*)node, (UA_ViewNode*)newNode); deleteNode = (void (*)(UA_Node*))UA_ViewNode_delete; break; default: return UA_STATUSCODE_BADINTERNALERROR; } UA_Int32 count = node->referencesSize; if(count < 0) count = 0; UA_ReferenceNode *old_refs = newNode->references; UA_ReferenceNode *new_refs = UA_malloc(sizeof(UA_ReferenceNode)*(count+1)); if(!new_refs) { deleteNode(newNode); UA_NodeStore_release(node); return UA_STATUSCODE_BADOUTOFMEMORY; } // insert the new reference UA_memcpy(new_refs, old_refs, sizeof(UA_ReferenceNode)*count); UA_ReferenceNode_init(&new_refs[count]); retval = UA_NodeId_copy(&item->referenceTypeId, &new_refs[count].referenceTypeId); new_refs[count].isInverse = !item->isForward; retval |= UA_ExpandedNodeId_copy(&item->targetNodeId, &new_refs[count].targetId); if(retval != UA_STATUSCODE_GOOD) { UA_Array_delete(new_refs, &UA_TYPES[UA_TYPES_REFERENCENODE], ++count); newNode->references = UA_NULL; newNode->referencesSize = 0; deleteNode(newNode); UA_NodeStore_release(node); return UA_STATUSCODE_BADOUTOFMEMORY; } UA_free(old_refs); newNode->references = new_refs; newNode->referencesSize = ++count; retval = UA_NodeStore_replace(server->nodestore, node, newNode, UA_NULL); UA_NodeStore_release(node); if (retval == UA_STATUSCODE_BADINTERNALERROR) { /* presumably because the node was replaced and an old version was updated at the same time. just try again */ deleteNode(newNode); return addOneWayReferenceWithSession(server, session, item); } return retval; #endif }
int main(void) { /* * Example for calling FindServersOnNetwork */ { UA_ServerOnNetwork *serverOnNetwork = NULL; size_t serverOnNetworkSize = 0; UA_Client *client = UA_Client_new(); UA_ClientConfig_setDefault(UA_Client_getConfig(client)); UA_StatusCode retval = UA_Client_findServersOnNetwork(client, DISCOVERY_SERVER_ENDPOINT, 0, 0, 0, NULL, &serverOnNetworkSize, &serverOnNetwork); if(retval != UA_STATUSCODE_GOOD) { UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Could not call FindServersOnNetwork service. " "Is the discovery server started? StatusCode %s", UA_StatusCode_name(retval)); UA_Client_delete(client); return EXIT_FAILURE; } // output all the returned/registered servers for(size_t i = 0; i < serverOnNetworkSize; i++) { UA_ServerOnNetwork *server = &serverOnNetwork[i]; printf("Server[%lu]: %.*s", (unsigned long) i, (int) server->serverName.length, server->serverName.data); printf("\n\tRecordID: %d", server->recordId); printf("\n\tDiscovery URL: %.*s", (int) server->discoveryUrl.length, server->discoveryUrl.data); printf("\n\tCapabilities: "); for(size_t j = 0; j < server->serverCapabilitiesSize; j++) { printf("%.*s,", (int) server->serverCapabilities[j].length, server->serverCapabilities[j].data); } printf("\n\n"); } UA_Array_delete(serverOnNetwork, serverOnNetworkSize, &UA_TYPES[UA_TYPES_SERVERONNETWORK]); } /* Example for calling FindServers */ UA_ApplicationDescription *applicationDescriptionArray = NULL; size_t applicationDescriptionArraySize = 0; UA_StatusCode retval; { UA_Client *client = UA_Client_new(); UA_ClientConfig_setDefault(UA_Client_getConfig(client)); retval = UA_Client_findServers(client, DISCOVERY_SERVER_ENDPOINT, 0, NULL, 0, NULL, &applicationDescriptionArraySize, &applicationDescriptionArray); UA_Client_delete(client); } if(retval != UA_STATUSCODE_GOOD) { UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Could not call FindServers service. " "Is the discovery server started? StatusCode %s", UA_StatusCode_name(retval)); return EXIT_FAILURE; } // output all the returned/registered servers for(size_t i = 0; i < applicationDescriptionArraySize; i++) { UA_ApplicationDescription *description = &applicationDescriptionArray[i]; printf("Server[%lu]: %.*s", (unsigned long) i, (int) description->applicationUri.length, description->applicationUri.data); printf("\n\tName: %.*s", (int) description->applicationName.text.length, description->applicationName.text.data); printf("\n\tApplication URI: %.*s", (int) description->applicationUri.length, description->applicationUri.data); printf("\n\tProduct URI: %.*s", (int) description->productUri.length, description->productUri.data); printf("\n\tType: "); switch(description->applicationType) { case UA_APPLICATIONTYPE_SERVER: printf("Server"); break; case UA_APPLICATIONTYPE_CLIENT: printf("Client"); break; case UA_APPLICATIONTYPE_CLIENTANDSERVER: printf("Client and Server"); break; case UA_APPLICATIONTYPE_DISCOVERYSERVER: printf("Discovery Server"); break; default: printf("Unknown"); } printf("\n\tDiscovery URLs:"); for(size_t j = 0; j < description->discoveryUrlsSize; j++) { printf("\n\t\t[%lu]: %.*s", (unsigned long) j, (int) description->discoveryUrls[j].length, description->discoveryUrls[j].data); } printf("\n\n"); } /* * Now that we have the list of available servers, call get endpoints on all of them */ printf("-------- Server Endpoints --------\n"); for(size_t i = 0; i < applicationDescriptionArraySize; i++) { UA_ApplicationDescription *description = &applicationDescriptionArray[i]; if(description->discoveryUrlsSize == 0) { UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "[GetEndpoints] Server %.*s did not provide any discovery urls. Skipping.", (int)description->applicationUri.length, description->applicationUri.data); continue; } printf("\nEndpoints for Server[%lu]: %.*s\n", (unsigned long) i, (int) description->applicationUri.length, description->applicationUri.data); UA_Client *client = UA_Client_new(); UA_ClientConfig_setDefault(UA_Client_getConfig(client)); char *discoveryUrl = (char *) UA_malloc(sizeof(char) * description->discoveryUrls[0].length + 1); memcpy(discoveryUrl, description->discoveryUrls[0].data, description->discoveryUrls[0].length); discoveryUrl[description->discoveryUrls[0].length] = '\0'; UA_EndpointDescription *endpointArray = NULL; size_t endpointArraySize = 0; //TODO: adapt to the new async getEndpoint retval = UA_Client_getEndpoints(client, discoveryUrl, &endpointArraySize, &endpointArray); UA_free(discoveryUrl); if(retval != UA_STATUSCODE_GOOD) { UA_Client_disconnect(client); UA_Client_delete(client); break; } for(size_t j = 0; j < endpointArraySize; j++) { UA_EndpointDescription *endpoint = &endpointArray[j]; printf("\n\tEndpoint[%lu]:", (unsigned long) j); printf("\n\t\tEndpoint URL: %.*s", (int) endpoint->endpointUrl.length, endpoint->endpointUrl.data); printf("\n\t\tTransport profile URI: %.*s", (int) endpoint->transportProfileUri.length, endpoint->transportProfileUri.data); printf("\n\t\tSecurity Mode: "); switch(endpoint->securityMode) { case UA_MESSAGESECURITYMODE_INVALID: printf("Invalid"); break; case UA_MESSAGESECURITYMODE_NONE: printf("None"); break; case UA_MESSAGESECURITYMODE_SIGN: printf("Sign"); break; case UA_MESSAGESECURITYMODE_SIGNANDENCRYPT: printf("Sign and Encrypt"); break; default: printf("No valid security mode"); break; } printf("\n\t\tSecurity profile URI: %.*s", (int) endpoint->securityPolicyUri.length, endpoint->securityPolicyUri.data); printf("\n\t\tSecurity Level: %d", endpoint->securityLevel); } UA_Array_delete(endpointArray, endpointArraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); UA_Client_delete(client); } printf("\n"); UA_Array_delete(applicationDescriptionArray, applicationDescriptionArraySize, &UA_TYPES[UA_TYPES_APPLICATIONDESCRIPTION]); return EXIT_SUCCESS; }
void Service_Read(UA_Server *server, UA_Session *session, const UA_ReadRequest *request, UA_ReadResponse *response) { UA_LOG_DEBUG(server->config.logger, UA_LOGCATEGORY_SESSION, "Processing ReadRequest for Session (ns=%i,i=%i)", session->sessionId.namespaceIndex, session->sessionId.identifier.numeric); if(request->nodesToReadSize <= 0) { response->responseHeader.serviceResult = UA_STATUSCODE_BADNOTHINGTODO; return; } if(request->timestampsToReturn > 3){ response->responseHeader.serviceResult = UA_STATUSCODE_BADTIMESTAMPSTORETURNINVALID; return; } size_t size = request->nodesToReadSize; response->results = UA_Array_new(size, &UA_TYPES[UA_TYPES_DATAVALUE]); if(!response->results) { response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY; return; } response->resultsSize = size; if(request->maxAge < 0) { response->responseHeader.serviceResult = UA_STATUSCODE_BADMAXAGEINVALID; return; } #ifdef UA_ENABLE_EXTERNAL_NAMESPACES UA_Boolean isExternal[size]; UA_UInt32 indices[size]; memset(isExternal, UA_FALSE, sizeof(UA_Boolean) * size); for(size_t j = 0;j<server->externalNamespacesSize;j++) { size_t indexSize = 0; for(size_t i = 0;i < size;i++) { if(request->nodesToRead[i].nodeId.namespaceIndex != server->externalNamespaces[j].index) continue; isExternal[i] = UA_TRUE; indices[indexSize] = i; indexSize++; } if(indexSize == 0) continue; UA_ExternalNodeStore *ens = &server->externalNamespaces[j].externalNodeStore; ens->readNodes(ens->ensHandle, &request->requestHeader, request->nodesToRead, indices, indexSize, response->results, UA_FALSE, response->diagnosticInfos); } #endif for(size_t i = 0;i < size;i++) { #ifdef UA_ENABLE_EXTERNAL_NAMESPACES if(!isExternal[i]) #endif Service_Read_single(server, session, request->timestampsToReturn, &request->nodesToRead[i], &response->results[i]); } #ifdef UA_ENABLE_NONSTANDARD_STATELESS /* Add an expiry header for caching */ if(session==&anonymousSession){ UA_ExtensionObject additionalHeader; UA_ExtensionObject_init(&additionalHeader); additionalHeader.typeId = UA_TYPES[UA_TYPES_VARIANT].typeId; additionalHeader.encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING; UA_Variant variant; UA_Variant_init(&variant); UA_DateTime* expireArray = NULL; expireArray = UA_Array_new(&UA_TYPES[UA_TYPES_DATETIME], request->nodesToReadSize); variant.data = expireArray; /*expires in 20 seconds*/ for(UA_Int32 i = 0;i < response->resultsSize;i++) { expireArray[i] = UA_DateTime_now() + 20 * 100 * 1000 * 1000; } UA_Variant_setArray(&variant, expireArray, request->nodesToReadSize, &UA_TYPES[UA_TYPES_DATETIME]); size_t offset = 0; UA_ByteString str; UA_ByteString_newMembers(&str, 65536); UA_Variant_encodeBinary(&variant, &str, &offset); UA_Array_delete(expireArray, &UA_TYPES[UA_TYPES_DATETIME], request->nodesToReadSize); additionalHeader.body = str; additionalHeader.body.length = offset; response->responseHeader.additionalHeader = additionalHeader; } #endif }
int main(int argc, char *argv[]) { UA_Client *client = UA_Client_new(UA_ClientConfig_standard, Logger_Stdout_new()); UA_StatusCode retval = UA_Client_connect(client, ClientNetworkLayerTCP_connect, "opc.tcp://localhost:16664"); if(retval != UA_STATUSCODE_GOOD) { UA_Client_delete(client); return retval; } // Browse some objects printf("Browsing nodes in objects folder:\n"); UA_BrowseRequest bReq; UA_BrowseRequest_init(&bReq); bReq.requestedMaxReferencesPerNode = 0; bReq.nodesToBrowse = UA_BrowseDescription_new(); bReq.nodesToBrowseSize = 1; bReq.nodesToBrowse[0].nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER); //browse objects folder bReq.nodesToBrowse[0].resultMask = UA_BROWSERESULTMASK_ALL; //return everything UA_BrowseResponse bResp = UA_Client_browse(client, &bReq); printf("%-9s %-16s %-16s %-16s\n", "NAMESPACE", "NODEID", "BROWSE NAME", "DISPLAY NAME"); for (int i = 0; i < bResp.resultsSize; ++i) { for (int j = 0; j < bResp.results[i].referencesSize; ++j) { UA_ReferenceDescription *ref = &(bResp.results[i].references[j]); if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_NUMERIC) { printf("%-9d %-16d %-16.*s %-16.*s\n", ref->browseName.namespaceIndex, ref->nodeId.nodeId.identifier.numeric, ref->browseName.name.length, ref->browseName.name.data, ref->displayName.text.length, ref->displayName.text.data); } else if(ref->nodeId.nodeId.identifierType == UA_NODEIDTYPE_STRING) { printf("%-9d %-16.*s %-16.*s %-16.*s\n", ref->browseName.namespaceIndex, ref->nodeId.nodeId.identifier.string.length, ref->nodeId.nodeId.identifier.string.data, ref->browseName.name.length, ref->browseName.name.data, ref->displayName.text.length, ref->displayName.text.data); } //TODO: distinguish further types } } UA_BrowseRequest_deleteMembers(&bReq); UA_BrowseResponse_deleteMembers(&bResp); #ifdef ENABLE_SUBSCRIPTIONS // Create a subscription with interval 0 (immediate)... UA_Int32 subId = UA_Client_newSubscription(client, 0); if (subId) printf("Create subscription succeeded, id %u\n", subId); // .. and monitor TheAnswer UA_NodeId monitorThis; monitorThis = UA_NODEID_STRING_ALLOC(1, "the.answer"); UA_UInt32 monId = UA_Client_monitorItemChanges(client, subId, monitorThis, UA_ATTRIBUTEID_VALUE, &handler_TheAnswerChanged ); if (monId) printf("Monitoring 'the.answer', id %u\n", subId); UA_NodeId_deleteMembers(&monitorThis); // First Publish always generates data (current value) and call out handler. UA_Client_doPublish(client); // This should not generate anything UA_Client_doPublish(client); #endif UA_Int32 value = 0; // Read node's value printf("\nReading the value of node (1, \"the.answer\"):\n"); UA_ReadRequest rReq; UA_ReadRequest_init(&rReq); rReq.nodesToRead = UA_ReadValueId_new(); rReq.nodesToReadSize = 1; rReq.nodesToRead[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */ rReq.nodesToRead[0].attributeId = UA_ATTRIBUTEID_VALUE; UA_ReadResponse rResp = UA_Client_read(client, &rReq); if(rResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD && rResp.resultsSize > 0 && rResp.results[0].hasValue && UA_Variant_isScalar(&rResp.results[0].value) && rResp.results[0].value.type == &UA_TYPES[UA_TYPES_INT32]) { value = *(UA_Int32*)rResp.results[0].value.data; printf("the value is: %i\n", value); } UA_ReadRequest_deleteMembers(&rReq); UA_ReadResponse_deleteMembers(&rResp); value++; // Write node's value printf("\nWriting a value of node (1, \"the.answer\"):\n"); UA_WriteRequest wReq; UA_WriteRequest_init(&wReq); wReq.nodesToWrite = UA_WriteValue_new(); wReq.nodesToWriteSize = 1; wReq.nodesToWrite[0].nodeId = UA_NODEID_STRING_ALLOC(1, "the.answer"); /* assume this node exists */ wReq.nodesToWrite[0].attributeId = UA_ATTRIBUTEID_VALUE; wReq.nodesToWrite[0].value.hasValue = UA_TRUE; wReq.nodesToWrite[0].value.value.type = &UA_TYPES[UA_TYPES_INT32]; wReq.nodesToWrite[0].value.value.storageType = UA_VARIANT_DATA_NODELETE; //do not free the integer on deletion wReq.nodesToWrite[0].value.value.data = &value; UA_WriteResponse wResp = UA_Client_write(client, &wReq); if(wResp.responseHeader.serviceResult == UA_STATUSCODE_GOOD) printf("the new value is: %i\n", value); UA_WriteRequest_deleteMembers(&wReq); UA_WriteResponse_deleteMembers(&wResp); #ifdef ENABLE_SUBSCRIPTIONS // Take another look at the.answer... this should call the handler. UA_Client_doPublish(client); // Delete our subscription (which also unmonitors all items) if(!UA_Client_removeSubscription(client, subId)) printf("Subscription removed\n"); #endif #ifdef ENABLE_METHODCALLS /* Note: This example requires Namespace 0 Node 11489 (ServerType -> GetMonitoredItems) FIXME: Provide a namespace 0 independant example on the server side */ UA_Variant input; UA_String argString = UA_STRING("Hello Server"); UA_Variant_init(&input); UA_Variant_setScalarCopy(&input, &argString, &UA_TYPES[UA_TYPES_STRING]); UA_Int32 outputSize; UA_Variant *output; retval = UA_Client_CallServerMethod(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(1, 62541), 1, &input, &outputSize, &output); if(retval == UA_STATUSCODE_GOOD) { printf("Method call was successfull, and %i returned values available.\n", outputSize); UA_Array_delete(output, &UA_TYPES[UA_TYPES_VARIANT], outputSize); } else { printf("Method call was unsuccessfull, and %x returned values available.\n", retval); } UA_Variant_deleteMembers(&input); #endif #ifdef ENABLE_ADDNODES /* Create a new object type node */ // New ReferenceType UA_AddNodesResponse *adResp = UA_Client_createReferenceTypeNode(client, UA_EXPANDEDNODEID_NUMERIC(1, 12133), // Assign this NodeId (will fail if client is called multiple times) UA_QUALIFIEDNAME(0, "NewReference"), UA_LOCALIZEDTEXT("en_US", "TheNewReference"), UA_LOCALIZEDTEXT("en_US", "References something that might or might not exist."), UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), (UA_UInt32) 0, (UA_UInt32) 0, UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_LOCALIZEDTEXT("en_US", "IsNewlyReferencedBy")); if (adResp->resultsSize > 0 && adResp->results[0].statusCode == UA_STATUSCODE_GOOD ) { printf("Created 'NewReference' with numeric NodeID %u\n", adResp->results[0].addedNodeId.identifier.numeric ); } UA_AddNodesResponse_deleteMembers(adResp); free(adResp); // New ObjectType adResp = UA_Client_createObjectTypeNode(client, UA_EXPANDEDNODEID_NUMERIC(1, 12134), // Assign this NodeId (will fail if client is called multiple times) UA_QUALIFIEDNAME(0, "NewObjectType"), UA_LOCALIZEDTEXT("en_US", "TheNewObjectType"), UA_LOCALIZEDTEXT("en_US", "Put innovative description here."), UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), (UA_UInt32) 0, (UA_UInt32) 0, UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER)); if (adResp->resultsSize > 0 && adResp->results[0].statusCode == UA_STATUSCODE_GOOD ) { printf("Created 'NewObjectType' with numeric NodeID %u\n", adResp->results[0].addedNodeId.identifier.numeric ); } // New Object adResp = UA_Client_createObjectNode(client, UA_EXPANDEDNODEID_NUMERIC(1, 0), // Assign new/random NodeID UA_QUALIFIEDNAME(0, "TheNewGreatNodeBrowseName"), UA_LOCALIZEDTEXT("en_US", "TheNewGreatNode"), UA_LOCALIZEDTEXT("de_DE", "Hier koennte Ihre Webung stehen!"), UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), (UA_UInt32) 0, (UA_UInt32) 0, UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER)); if (adResp->resultsSize > 0 && adResp->results[0].statusCode == UA_STATUSCODE_GOOD ) { printf("Created 'NewObject' with numeric NodeID %u\n", adResp->results[0].addedNodeId.identifier.numeric ); } UA_AddNodesResponse_deleteMembers(adResp); free(adResp); // New Integer Variable UA_Variant *theValue = UA_Variant_new(); UA_Int32 *theValueDate = UA_Int32_new(); *theValueDate = 1234; theValue->type = &UA_TYPES[UA_TYPES_INT32]; theValue->data = theValueDate; adResp = UA_Client_createVariableNode(client, UA_EXPANDEDNODEID_NUMERIC(1, 0), // Assign new/random NodeID UA_QUALIFIEDNAME(0, "VariableNode"), UA_LOCALIZEDTEXT("en_US", "TheNewVariableNode"), UA_LOCALIZEDTEXT("en_US", "This integer is just amazing - it has digits and everything."), UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), (UA_UInt32) 0, (UA_UInt32) 0, UA_EXPANDEDNODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER), UA_NODEID_NUMERIC(0, UA_NS0ID_INT32), theValue); if (adResp->resultsSize > 0 && adResp->results[0].statusCode == UA_STATUSCODE_GOOD ) { printf("Created 'NewVariable' with numeric NodeID %u\n", adResp->results[0].addedNodeId.identifier.numeric ); } UA_AddNodesResponse_deleteMembers(adResp); free(adResp); free(theValue); /* Done creating a new node*/ #endif UA_Client_disconnect(client); UA_Client_delete(client); return UA_STATUSCODE_GOOD; }