client_access_control.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <stdio.h>
  7. #include "open62541.h"
  8. int main(void) {
  9. UA_Client *client = UA_Client_new(UA_ClientConfig_default);
  10. UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  11. if(retval != UA_STATUSCODE_GOOD) {
  12. UA_Client_delete(client);
  13. return (int)retval;
  14. }
  15. UA_NodeId newVariableIdRequest = UA_NODEID_NUMERIC(1, 1001);
  16. UA_NodeId newVariableId = UA_NODEID_NULL;
  17. UA_VariableAttributes newVariableAttributes = UA_VariableAttributes_default;
  18. newVariableAttributes.accessLevel = UA_ACCESSLEVELMASK_READ;
  19. newVariableAttributes.description = UA_LOCALIZEDTEXT_ALLOC("en-US", "NewVariable desc");
  20. newVariableAttributes.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", "NewVariable");
  21. newVariableAttributes.dataType = UA_TYPES[UA_TYPES_UINT32].typeId;
  22. {
  23. UA_UInt32 value = 50;
  24. UA_Variant_setScalarCopy(&newVariableAttributes.value, &value, &UA_TYPES[UA_TYPES_UINT32]);
  25. }
  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_deleteMembers(&newVariableAttributes);
  48. UA_Client_delete(client); /* Disconnects the client internally */
  49. return UA_STATUSCODE_GOOD;
  50. }