client_access_control.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. /**
  4. * Using access_control_server
  5. */
  6. #include <ua_client.h>
  7. #include <ua_config_default.h>
  8. #include <ua_client_highlevel.h>
  9. int main(void) {
  10. UA_Client *client = UA_Client_new();
  11. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  12. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  13. if(retval != UA_STATUSCODE_GOOD) {
  14. UA_Client_delete(client);
  15. return (int)retval;
  16. }
  17. UA_NodeId newVariableIdRequest = UA_NODEID_NUMERIC(1, 1001);
  18. UA_NodeId newVariableId = UA_NODEID_NULL;
  19. UA_VariableAttributes newVariableAttributes = UA_VariableAttributes_default;
  20. newVariableAttributes.accessLevel = UA_ACCESSLEVELMASK_READ;
  21. newVariableAttributes.description = UA_LOCALIZEDTEXT_ALLOC("en-US", "NewVariable desc");
  22. newVariableAttributes.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", "NewVariable");
  23. newVariableAttributes.dataType = UA_TYPES[UA_TYPES_UINT32].typeId;
  24. {
  25. UA_UInt32 value = 50;
  26. UA_Variant_setScalarCopy(&newVariableAttributes.value, &value, &UA_TYPES[UA_TYPES_UINT32]);
  27. }
  28. UA_StatusCode retCode;
  29. retCode = UA_Client_addVariableNode(client, newVariableIdRequest,
  30. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  31. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  32. UA_QUALIFIEDNAME(1, "newVariable"),
  33. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  34. newVariableAttributes, &newVariableId);
  35. printf("addVariable returned: %s\n", UA_StatusCode_name(retCode));
  36. UA_ExpandedNodeId extNodeId = UA_EXPANDEDNODEID_NUMERIC(0, 0);
  37. extNodeId.nodeId = newVariableId;
  38. retCode = UA_Client_addReference(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  39. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_TRUE,
  40. UA_STRING_NULL, extNodeId, UA_NODECLASS_VARIABLE);
  41. printf("addReference returned: %s\n", UA_StatusCode_name(retCode));
  42. retCode = UA_Client_deleteReference(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  43. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_TRUE, extNodeId,
  44. UA_TRUE);
  45. printf("deleteReference returned: %s\n", UA_StatusCode_name(retCode));
  46. retCode = UA_Client_deleteNode(client, newVariableId, UA_TRUE);
  47. printf("deleteNode returned: %s\n", UA_StatusCode_name(retCode));
  48. /* Clean up */
  49. UA_VariableAttributes_clear(&newVariableAttributes);
  50. UA_Client_delete(client); /* Disconnects the client internally */
  51. return UA_STATUSCODE_GOOD;
  52. }