ua_session.h 5.9 KB

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