tutorial_datatypes.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. * .. _types-tutorial:
  5. *
  6. * Working with Data Types
  7. * -----------------------
  8. *
  9. * OPC UA defines a type system for values that can be encoded in the protocol
  10. * messages. This tutorial shows some examples for available data types and
  11. * their use. See the section on :ref:`types` for the full definitions.
  12. *
  13. * Basic Data Handling
  14. * ^^^^^^^^^^^^^^^^^^^
  15. * This section shows the basic interaction patterns for data types. Make
  16. * sure to compare with the type definitions in ``ua_types.h``. */
  17. #include <assert.h>
  18. #include "open62541.h"
  19. static void
  20. variables_basic(void) {
  21. /* Int32 */
  22. UA_Int32 i = 5;
  23. UA_Int32 j;
  24. UA_Int32_copy(&i, &j);
  25. UA_Int32 *ip = UA_Int32_new();
  26. UA_Int32_copy(&i, ip);
  27. UA_Int32_delete(ip);
  28. /* String */
  29. UA_String s;
  30. UA_String_init(&s); /* _init zeroes out the entire memory of the datatype */
  31. char *test = "test";
  32. s.length = strlen(test);
  33. s.data = (UA_Byte*)test;
  34. UA_String s2;
  35. UA_String_copy(&s, &s2);
  36. UA_String_deleteMembers(&s2); /* Copying heap-allocated the dynamic content */
  37. UA_String s3 = UA_STRING("test2");
  38. UA_String s4 = UA_STRING_ALLOC("test2"); /* Copies the content to the heap */
  39. UA_Boolean eq = UA_String_equal(&s3, &s4);
  40. UA_String_deleteMembers(&s4);
  41. if(!eq)
  42. return;
  43. /* Structured Type */
  44. UA_CallRequest cr;
  45. UA_init(&cr, &UA_TYPES[UA_TYPES_CALLREQUEST]); /* Generic method */
  46. UA_CallRequest_init(&cr); /* Shorthand for the previous line */
  47. cr.requestHeader.timestamp = UA_DateTime_now(); /* Members of a structure */
  48. cr.methodsToCall = (UA_CallMethodRequest *)UA_Array_new(5, &UA_TYPES[UA_TYPES_CALLMETHODREQUEST]);
  49. cr.methodsToCallSize = 5; /* Array size needs to be made known */
  50. UA_CallRequest *cr2 = UA_CallRequest_new();
  51. UA_copy(&cr, cr2, &UA_TYPES[UA_TYPES_CALLREQUEST]);
  52. UA_CallRequest_deleteMembers(&cr);
  53. UA_CallRequest_delete(cr2);
  54. }
  55. /**
  56. * NodeIds
  57. * ^^^^^^^
  58. * An OPC UA information model is made up of nodes and references between nodes.
  59. * Every node has a unique :ref:`nodeid`. NodeIds refer to a namespace with an
  60. * additional identifier value that can be an integer, a string, a guid or a
  61. * bytestring. */
  62. static void
  63. variables_nodeids(void) {
  64. UA_NodeId id1 = UA_NODEID_NUMERIC(1, 1234);
  65. id1.namespaceIndex = 3;
  66. UA_NodeId id2 = UA_NODEID_STRING(1, "testid"); /* the string is static */
  67. UA_Boolean eq = UA_NodeId_equal(&id1, &id2);
  68. if(eq)
  69. return;
  70. UA_NodeId id3;
  71. UA_NodeId_copy(&id2, &id3);
  72. UA_NodeId_deleteMembers(&id3);
  73. UA_NodeId id4 = UA_NODEID_STRING_ALLOC(1, "testid"); /* the string is copied
  74. to the heap */
  75. UA_NodeId_deleteMembers(&id4);
  76. }
  77. /**
  78. * Variants
  79. * ^^^^^^^^
  80. * The datatype :ref:`variant` belongs to the built-in datatypes of OPC UA and
  81. * is used as a container type. A variant can hold any other datatype as a
  82. * scalar (except variant) or as an array. Array variants can additionally
  83. * denote the dimensionality of the data (e.g. a 2x3 matrix) in an additional
  84. * integer array. */
  85. static void
  86. variables_variants(void) {
  87. /* Set a scalar value */
  88. UA_Variant v;
  89. UA_Int32 i = 42;
  90. UA_Variant_setScalar(&v, &i, &UA_TYPES[UA_TYPES_INT32]);
  91. /* Make a copy */
  92. UA_Variant v2;
  93. UA_Variant_copy(&v, &v2);
  94. UA_Variant_deleteMembers(&v2);
  95. /* Set an array value */
  96. UA_Variant v3;
  97. UA_Double d[9] = {1.0, 2.0, 3.0,
  98. 4.0, 5.0, 6.0,
  99. 7.0, 8.0, 9.0};
  100. UA_Variant_setArrayCopy(&v3, d, 9, &UA_TYPES[UA_TYPES_DOUBLE]);
  101. /* Set array dimensions */
  102. v3.arrayDimensions = (UA_UInt32 *)UA_Array_new(2, &UA_TYPES[UA_TYPES_UINT32]);
  103. v3.arrayDimensionsSize = 2;
  104. v3.arrayDimensions[0] = 3;
  105. v3.arrayDimensions[1] = 3;
  106. UA_Variant_deleteMembers(&v3);
  107. }
  108. /** It follows the main function, making use of the above definitions. */
  109. int main(void) {
  110. variables_basic();
  111. variables_nodeids();
  112. variables_variants();
  113. return 0;
  114. }