server_datasource.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #include <signal.h>
  4. #include <stdio.h>
  5. #ifdef UA_NO_AMALGAMATION
  6. # include "ua_types.h"
  7. # include "ua_server.h"
  8. # include "ua_config_standard.h"
  9. # include "ua_network_tcp.h"
  10. # include "ua_log_stdout.h"
  11. #else
  12. # include "open62541.h"
  13. #endif
  14. UA_Boolean running = true;
  15. UA_Logger logger = UA_Log_Stdout;
  16. static void stopHandler(int sign) {
  17. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  18. running = false;
  19. }
  20. static UA_StatusCode
  21. readInteger(void *handle, const UA_NodeId nodeid, UA_Boolean sourceTimeStamp,
  22. const UA_NumericRange *range, UA_DataValue *dataValue) {
  23. dataValue->hasValue = true;
  24. UA_Variant_setScalarCopy(&dataValue->value, (UA_UInt32*)handle, &UA_TYPES[UA_TYPES_INT32]);
  25. // we know the nodeid is a string
  26. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "Node read %.*s",
  27. nodeid.identifier.string.length, nodeid.identifier.string.data);
  28. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "read value %i", *(UA_UInt32*)handle);
  29. return UA_STATUSCODE_GOOD;
  30. }
  31. static UA_StatusCode
  32. writeInteger(void *handle, const UA_NodeId nodeid,
  33. const UA_Variant *data, const UA_NumericRange *range) {
  34. if(UA_Variant_isScalar(data) && data->type == &UA_TYPES[UA_TYPES_INT32] && data->data){
  35. *(UA_UInt32*)handle = *(UA_UInt32*)data->data;
  36. }
  37. // we know the nodeid is a string
  38. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "Node written %.*s",
  39. nodeid.identifier.string.length, nodeid.identifier.string.data);
  40. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "written value %i", *(UA_UInt32*)handle);
  41. return UA_STATUSCODE_GOOD;
  42. }
  43. int main(int argc, char** argv) {
  44. signal(SIGINT, stopHandler); /* catches ctrl-c */
  45. UA_ServerConfig config = UA_ServerConfig_standard;
  46. UA_ServerNetworkLayer nl = UA_ServerNetworkLayerTCP(UA_ConnectionConfig_standard, 16664);
  47. config.networkLayers = &nl;
  48. config.networkLayersSize = 1;
  49. UA_Server *server = UA_Server_new(config);
  50. /* add a variable node to the address space */
  51. UA_Int32 myInteger = 42;
  52. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  53. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  54. UA_DataSource dateDataSource = (UA_DataSource) {
  55. .handle = &myInteger, .read = readInteger, .write = writeInteger};
  56. UA_VariableAttributes attr;
  57. UA_VariableAttributes_init(&attr);
  58. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  59. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  60. UA_Server_addDataSourceVariableNode(server, myIntegerNodeId,
  61. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  62. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  63. myIntegerName, UA_NODEID_NULL, attr, dateDataSource, NULL);
  64. UA_StatusCode retval = UA_Server_run(server, &running);
  65. UA_Server_delete(server);
  66. nl.deleteMembers(&nl);
  67. return (int)retval;
  68. }