ua_session.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #include <open62541/util.h>
  10. #include "ua_securechannel.h"
  11. _UA_BEGIN_DECLS
  12. #define UA_MAXCONTINUATIONPOINTS 5
  13. typedef struct ContinuationPointEntry {
  14. LIST_ENTRY(ContinuationPointEntry) pointers;
  15. UA_ByteString identifier;
  16. UA_BrowseDescription browseDescription;
  17. UA_UInt32 maxReferences;
  18. /* The last point in the node references? */
  19. size_t referenceKindIndex;
  20. size_t targetIndex;
  21. } ContinuationPointEntry;
  22. struct UA_Subscription;
  23. typedef struct UA_Subscription UA_Subscription;
  24. #ifdef UA_ENABLE_SUBSCRIPTIONS
  25. typedef struct UA_PublishResponseEntry {
  26. SIMPLEQ_ENTRY(UA_PublishResponseEntry) listEntry;
  27. UA_UInt32 requestId;
  28. UA_PublishResponse response;
  29. } UA_PublishResponseEntry;
  30. #endif
  31. typedef struct {
  32. UA_SessionHeader header;
  33. UA_ApplicationDescription clientDescription;
  34. UA_String sessionName;
  35. UA_Boolean activated;
  36. void *sessionHandle; // pointer assigned in userland-callback
  37. UA_NodeId sessionId;
  38. UA_UInt32 maxRequestMessageSize;
  39. UA_UInt32 maxResponseMessageSize;
  40. UA_Double timeout; // [ms]
  41. UA_DateTime validTill;
  42. UA_ByteString serverNonce;
  43. UA_UInt16 availableContinuationPoints;
  44. LIST_HEAD(ContinuationPointList, ContinuationPointEntry) continuationPoints;
  45. #ifdef UA_ENABLE_SUBSCRIPTIONS
  46. UA_UInt32 lastSubscriptionId;
  47. UA_UInt32 lastSeenSubscriptionId;
  48. LIST_HEAD(UA_ListOfUASubscriptions, UA_Subscription) serverSubscriptions;
  49. SIMPLEQ_HEAD(UA_ListOfQueuedPublishResponses, UA_PublishResponseEntry) responseQueue;
  50. UA_UInt32 numSubscriptions;
  51. UA_UInt32 numPublishReq;
  52. size_t totalRetransmissionQueueSize; /* Retransmissions of all subscriptions */
  53. #endif
  54. } UA_Session;
  55. /**
  56. * Session Lifecycle
  57. * ----------------- */
  58. void UA_Session_init(UA_Session *session);
  59. void UA_Session_deleteMembersCleanup(UA_Session *session, UA_Server *server);
  60. void UA_Session_attachToSecureChannel(UA_Session *session, UA_SecureChannel *channel);
  61. void UA_Session_detachFromSecureChannel(UA_Session *session);
  62. UA_StatusCode UA_Session_generateNonce(UA_Session *session);
  63. /* If any activity on a session happens, the timeout is extended */
  64. void UA_Session_updateLifetime(UA_Session *session);
  65. /**
  66. * Subscription handling
  67. * --------------------- */
  68. #ifdef UA_ENABLE_SUBSCRIPTIONS
  69. void
  70. UA_Session_addSubscription(UA_Server *server,
  71. UA_Session *session,
  72. UA_Subscription *newSubscription);
  73. UA_Subscription *
  74. UA_Session_getSubscriptionById(UA_Session *session,
  75. UA_UInt32 subscriptionId);
  76. UA_StatusCode
  77. UA_Session_deleteSubscription(UA_Server *server, UA_Session *session,
  78. UA_UInt32 subscriptionId);
  79. void
  80. UA_Session_queuePublishReq(UA_Session *session,
  81. UA_PublishResponseEntry* entry,
  82. UA_Boolean head);
  83. UA_PublishResponseEntry *
  84. UA_Session_dequeuePublishReq(UA_Session *session);
  85. #endif
  86. /**
  87. * Log Helper
  88. * ----------
  89. * We have to jump through some hoops to enable the use of format strings
  90. * without arguments since (pedantic) C99 does not allow variadic macros with
  91. * zero arguments. So we add a dummy argument that is not printed (%.0s is
  92. * string of length zero). */
  93. #define UA_LOG_SESSION_INTERNAL(LOGGER, LEVEL, SESSION, MSG, ...) do { \
  94. UA_String idString = UA_STRING_NULL; \
  95. UA_NodeId_toString(&(SESSION)->sessionId, &idString); \
  96. UA_LOG_##LEVEL(LOGGER, UA_LOGCATEGORY_SESSION, \
  97. "Connection %i | SecureChannel %i | Session %.*s | " MSG "%.0s", \
  98. ((SESSION)->header.channel ? \
  99. ((SESSION)->header.channel->connection ? \
  100. (int)((SESSION)->header.channel->connection->sockfd) : 0) : 0), \
  101. ((SESSION)->header.channel ? \
  102. (SESSION)->header.channel->securityToken.channelId : 0), \
  103. (int)idString.length, idString.data, __VA_ARGS__); \
  104. UA_String_deleteMembers(&idString); \
  105. } while(0)
  106. #if UA_LOGLEVEL <= 100
  107. #define UA_LOG_TRACE_SESSION(LOGGER, SESSION, ...) \
  108. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, TRACE, SESSION, __VA_ARGS__, ""))
  109. #else
  110. #define UA_LOG_TRACE_SESSION(LOGGER, SESSION, ...) do {} while(0)
  111. #endif
  112. #if UA_LOGLEVEL <= 200
  113. #define UA_LOG_DEBUG_SESSION(LOGGER, SESSION, ...) \
  114. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, DEBUG, SESSION, __VA_ARGS__, ""))
  115. #else
  116. #define UA_LOG_DEBUG_SESSION(LOGGER, SESSION, ...) do {} while(0)
  117. #endif
  118. #if UA_LOGLEVEL <= 300
  119. #define UA_LOG_INFO_SESSION(LOGGER, SESSION, ...) \
  120. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, INFO, SESSION, __VA_ARGS__, ""))
  121. #else
  122. #define UA_LOG_INFO_SESSION(LOGGER, SESSION, ...) do {} while(0)
  123. #endif
  124. #if UA_LOGLEVEL <= 400
  125. #define UA_LOG_WARNING_SESSION(LOGGER, SESSION, ...) \
  126. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, WARNING, SESSION, __VA_ARGS__, ""))
  127. #else
  128. #define UA_LOG_WARNING_SESSION(LOGGER, SESSION, ...) do {} while(0)
  129. #endif
  130. #if UA_LOGLEVEL <= 500
  131. #define UA_LOG_ERROR_SESSION(LOGGER, SESSION, ...) \
  132. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, ERROR, SESSION, __VA_ARGS__, ""))
  133. #else
  134. #define UA_LOG_ERROR_SESSION(LOGGER, SESSION, ...) do {} while(0)
  135. #endif
  136. #if UA_LOGLEVEL <= 600
  137. #define UA_LOG_FATAL_SESSION(LOGGER, SESSION, ...) \
  138. UA_MACRO_EXPAND(UA_LOG_SESSION_INTERNAL(LOGGER, FATAL, SESSION, __VA_ARGS__, ""))
  139. #else
  140. #define UA_LOG_FATAL_SESSION(LOGGER, SESSION, ...) do {} while(0)
  141. #endif
  142. _UA_END_DECLS
  143. #endif /* UA_SESSION_H_ */