ua_session.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. */
  7. #ifndef UA_SESSION_H_
  8. #define UA_SESSION_H_
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. #include "ua_securechannel.h"
  13. #include "ua_util.h"
  14. #define UA_MAXCONTINUATIONPOINTS 5
  15. typedef struct ContinuationPointEntry {
  16. LIST_ENTRY(ContinuationPointEntry) pointers;
  17. UA_ByteString identifier;
  18. UA_BrowseDescription browseDescription;
  19. UA_UInt32 maxReferences;
  20. /* The last point in the node references? */
  21. size_t referenceKindIndex;
  22. size_t targetIndex;
  23. } ContinuationPointEntry;
  24. struct UA_Subscription;
  25. typedef struct UA_Subscription UA_Subscription;
  26. #ifdef UA_ENABLE_SUBSCRIPTIONS
  27. typedef struct UA_PublishResponseEntry {
  28. SIMPLEQ_ENTRY(UA_PublishResponseEntry) listEntry;
  29. UA_UInt32 requestId;
  30. UA_PublishResponse response;
  31. } UA_PublishResponseEntry;
  32. #endif
  33. typedef struct {
  34. UA_SessionHeader header;
  35. UA_ApplicationDescription clientDescription;
  36. UA_String sessionName;
  37. UA_Boolean activated;
  38. void *sessionHandle; // pointer assigned in userland-callback
  39. UA_NodeId sessionId;
  40. UA_UInt32 maxRequestMessageSize;
  41. UA_UInt32 maxResponseMessageSize;
  42. UA_Double timeout; // [ms]
  43. UA_DateTime validTill;
  44. UA_ByteString serverNonce;
  45. UA_UInt16 availableContinuationPoints;
  46. LIST_HEAD(ContinuationPointList, ContinuationPointEntry) continuationPoints;
  47. #ifdef UA_ENABLE_SUBSCRIPTIONS
  48. UA_UInt32 lastSubscriptionId;
  49. UA_UInt32 lastSeenSubscriptionId;
  50. LIST_HEAD(UA_ListOfUASubscriptions, UA_Subscription) serverSubscriptions;
  51. SIMPLEQ_HEAD(UA_ListOfQueuedPublishResponses, UA_PublishResponseEntry) responseQueue;
  52. UA_UInt32 numSubscriptions;
  53. UA_UInt32 numPublishReq;
  54. #endif
  55. } UA_Session;
  56. /**
  57. * Session Lifecycle
  58. * ----------------- */
  59. void UA_Session_init(UA_Session *session);
  60. void UA_Session_deleteMembersCleanup(UA_Session *session, UA_Server *server);
  61. void UA_Session_attachToSecureChannel(UA_Session *session, UA_SecureChannel *channel);
  62. void UA_Session_detachFromSecureChannel(UA_Session *session);
  63. UA_StatusCode UA_Session_generateNonce(UA_Session *session);
  64. /* If any activity on a session happens, the timeout is extended */
  65. void UA_Session_updateLifetime(UA_Session *session);
  66. /**
  67. * Subscription handling
  68. * --------------------- */
  69. #ifdef UA_ENABLE_SUBSCRIPTIONS
  70. void UA_Session_addSubscription(UA_Session *session, UA_Subscription *newSubscription);
  71. UA_Subscription * UA_Session_getSubscriptionById(UA_Session *session, UA_UInt32 subscriptionId);
  72. UA_StatusCode UA_Session_deleteSubscription(UA_Server *server, UA_Session *session, UA_UInt32 subscriptionId);
  73. void UA_Session_queuePublishReq(UA_Session *session, UA_PublishResponseEntry* entry, UA_Boolean head);
  74. UA_PublishResponseEntry* UA_Session_dequeuePublishReq(UA_Session *session);
  75. #endif
  76. /**
  77. * Log Helper
  78. * ----------
  79. * We have to jump through some hoops to enable the use of format strings
  80. * without arguments since (pedantic) C99 does not allow variadic macros with
  81. * zero arguments. So we add a dummy argument that is not printed (%.0s is
  82. * string of length zero). */
  83. #define UA_LOG_SESSION_INTERNAL(LOGGER, LEVEL, SESSION, MSG, ...) do { \
  84. UA_String idString = UA_STRING_NULL; \
  85. UA_NodeId_toString(&(SESSION)->sessionId, &idString); \
  86. UA_LOG_##LEVEL(LOGGER, UA_LOGCATEGORY_SESSION, \
  87. "Connection %i | SecureChannel %i | Session %.*s | " MSG "%.0s", \
  88. ((SESSION)->header.channel ? ((SESSION)->header.channel->connection ? (SESSION)->header.channel->connection->sockfd : 0) : 0), \
  89. ((SESSION)->header.channel ? (SESSION)->header.channel->securityToken.channelId : 0), \
  90. (int)idString.length, idString.data, __VA_ARGS__); \
  91. UA_String_deleteMembers(&idString); \
  92. } while(0)
  93. #define UA_LOG_TRACE_SESSION(LOGGER, SESSION, ...) \
  94. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, TRACE, SESSION, __VA_ARGS__, ""))
  95. #define UA_LOG_DEBUG_SESSION(LOGGER, SESSION, ...) \
  96. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, DEBUG, SESSION, __VA_ARGS__, ""))
  97. #define UA_LOG_INFO_SESSION(LOGGER, SESSION, ...) \
  98. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, INFO, SESSION, __VA_ARGS__, ""))
  99. #define UA_LOG_WARNING_SESSION(LOGGER, SESSION, ...) \
  100. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, WARNING, SESSION, __VA_ARGS__, ""))
  101. #define UA_LOG_ERROR_SESSION(LOGGER, SESSION, ...) \
  102. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, ERROR, SESSION, __VA_ARGS__, ""))
  103. #define UA_LOG_FATAL_SESSION(LOGGER, SESSION, ...) \
  104. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, FATAL, SESSION, __VA_ARGS__, ""))
  105. #ifdef __cplusplus
  106. } // extern "C"
  107. #endif
  108. #endif /* UA_SESSION_H_ */