client_access_control.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 <open62541/client_config_default.h>
  7. #include <open62541/client_highlevel.h>
  8. #include <stdlib.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_username(client, "opc.tcp://localhost:4840", "paula", "paula123");
  13. if(retval != UA_STATUSCODE_GOOD) {
  14. UA_Client_delete(client);
  15. return EXIT_FAILURE;
  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. UA_UInt32 value = 50;
  25. UA_Variant_setScalarCopy(&newVariableAttributes.value, &value, &UA_TYPES[UA_TYPES_UINT32]);
  26. UA_StatusCode retCode;
  27. retCode = UA_Client_addVariableNode(client, newVariableIdRequest,
  28. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  29. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  30. UA_QUALIFIEDNAME(1, "newVariable"),
  31. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  32. newVariableAttributes, &newVariableId);
  33. printf("addVariable returned: %s\n", UA_StatusCode_name(retCode));
  34. UA_ExpandedNodeId extNodeId = UA_EXPANDEDNODEID_NUMERIC(0, 0);
  35. extNodeId.nodeId = newVariableId;
  36. retCode = UA_Client_addReference(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  37. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_TRUE,
  38. UA_STRING_NULL, extNodeId, UA_NODECLASS_VARIABLE);
  39. printf("addReference returned: %s\n", UA_StatusCode_name(retCode));
  40. retCode = UA_Client_deleteReference(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  41. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_TRUE, extNodeId,
  42. UA_TRUE);
  43. printf("deleteReference returned: %s\n", UA_StatusCode_name(retCode));
  44. retCode = UA_Client_deleteNode(client, newVariableId, UA_TRUE);
  45. printf("deleteNode returned: %s\n", UA_StatusCode_name(retCode));
  46. /* Clean up */
  47. UA_VariableAttributes_clear(&newVariableAttributes);
  48. UA_Client_delete(client); /* Disconnects the client internally */
  49. return EXIT_SUCCESS;
  50. }