client_access_control.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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.h"
  7. int main(void) {
  8. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  9. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  10. if(retval != UA_STATUSCODE_GOOD) {
  11. UA_Client_delete(client);
  12. return (int)retval;
  13. }
  14. UA_NodeId newVariableIdRequest = UA_NODEID_NUMERIC(1, 1001);
  15. UA_NodeId newVariableId = UA_NODEID_NULL;
  16. UA_VariableAttributes newVariableAttributes = UA_VariableAttributes_default;
  17. newVariableAttributes.accessLevel = UA_ACCESSLEVELMASK_READ;
  18. newVariableAttributes.description = UA_LOCALIZEDTEXT_ALLOC("en-US", "NewVariable desc");
  19. newVariableAttributes.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", "NewVariable");
  20. newVariableAttributes.dataType = UA_TYPES[UA_TYPES_UINT32].typeId;
  21. {
  22. UA_UInt32 value = 50;
  23. UA_Variant_setScalarCopy(&newVariableAttributes.value, &value, &UA_TYPES[UA_TYPES_UINT32]);
  24. }
  25. UA_StatusCode retCode;
  26. retCode = UA_Client_addVariableNode(client, newVariableIdRequest,
  27. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  28. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  29. UA_QUALIFIEDNAME(1, "newVariable"),
  30. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  31. newVariableAttributes, &newVariableId);
  32. printf("addVariable returned: %s\n", UA_StatusCode_name(retCode));
  33. UA_ExpandedNodeId extNodeId = UA_EXPANDEDNODEID_NUMERIC(0, 0);
  34. extNodeId.nodeId = newVariableId;
  35. retCode = UA_Client_addReference(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  36. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_TRUE,
  37. UA_STRING_NULL, extNodeId, UA_NODECLASS_VARIABLE);
  38. printf("addReference returned: %s\n", UA_StatusCode_name(retCode));
  39. retCode = UA_Client_deleteReference(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  40. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_TRUE, extNodeId,
  41. UA_TRUE);
  42. printf("deleteReference returned: %s\n", UA_StatusCode_name(retCode));
  43. retCode = UA_Client_deleteNode(client, newVariableId, UA_TRUE);
  44. printf("deleteNode returned: %s\n", UA_StatusCode_name(retCode));
  45. /* Clean up */
  46. UA_VariableAttributes_deleteMembers(&newVariableAttributes);
  47. UA_Client_delete(client); /* Disconnects the client internally */
  48. return UA_STATUSCODE_GOOD;
  49. }