server_datasource.c 3.1 KB

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