server_datasource.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  3. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  4. */
  5. #include <signal.h>
  6. #include <stdio.h>
  7. #ifdef UA_NO_AMALGAMATION
  8. # include "ua_types.h"
  9. # include "ua_server.h"
  10. # include "logger_stdout.h"
  11. # include "networklayer_tcp.h"
  12. #else
  13. # include "open62541.h"
  14. #endif
  15. UA_Boolean running = 1;
  16. UA_Logger logger = Logger_Stdout;
  17. static void stopHandler(int sign) {
  18. UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "received ctrl-c");
  19. running = 0;
  20. }
  21. static UA_StatusCode readInteger(void *handle, const UA_NodeId nodeid, UA_Boolean sourceTimeStamp, const UA_NumericRange *range, UA_DataValue *dataValue) {
  22. dataValue->hasValue = UA_TRUE;
  23. UA_Variant_setScalarCopy(&dataValue->value, (UA_UInt32*)handle, &UA_TYPES[UA_TYPES_INT32]);
  24. //note that this is only possible if the identifier is a string - but we are sure to have one here
  25. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "Node read %.*s",nodeid.identifier.string.length, nodeid.identifier.string.data);
  26. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "read value %i", *(UA_UInt32*)handle);
  27. return UA_STATUSCODE_GOOD;
  28. }
  29. static UA_StatusCode writeInteger(void *handle, const UA_NodeId nodeid, const UA_Variant *data, const UA_NumericRange *range){
  30. if(UA_Variant_isScalar(data) && data->type == &UA_TYPES[UA_TYPES_INT32] && data->data){
  31. *(UA_UInt32*)handle = *(UA_Int32*)data->data;
  32. }
  33. //note that this is only possible if the identifier is a string - but we are sure to have one here
  34. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "Node written %.*s",nodeid.identifier.string.length, nodeid.identifier.string.data);
  35. UA_LOG_INFO(logger, UA_LOGCATEGORY_USERLAND, "written value %i", *(UA_UInt32*)handle);
  36. return UA_STATUSCODE_GOOD;
  37. }
  38. int main(int argc, char** argv) {
  39. signal(SIGINT, stopHandler); /* catches ctrl-c */
  40. UA_Server *server = UA_Server_new(UA_ServerConfig_standard);
  41. UA_Server_setLogger(server, logger);
  42. UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
  43. UA_Int32 myInteger = 42;
  44. /* add a variable node to the address space */
  45. UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
  46. UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
  47. UA_DataSource dateDataSource = (UA_DataSource) {
  48. .handle = &myInteger, .read = readInteger, .write = writeInteger};
  49. UA_VariableAttributes attr;
  50. UA_VariableAttributes_init(&attr);
  51. attr.description = UA_LOCALIZEDTEXT("en_US","the answer");
  52. attr.displayName = UA_LOCALIZEDTEXT("en_US","the answer");
  53. UA_Server_addDataSourceVariableNode(server, myIntegerNodeId,
  54. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  55. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  56. myIntegerName, UA_NODEID_NULL, attr, dateDataSource, NULL);
  57. UA_StatusCode retval = UA_Server_run(server, 1, &running);
  58. UA_Server_delete(server);
  59. return retval;
  60. }