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