tutorial_datatypes.c 4.1 KB

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