iceoryx_doc  1.0.1
ipc_interface_base.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 
18 #ifndef IOX_POSH_RUNTIME_IPC_INTERFACE_BASE_HPP
19 #define IOX_POSH_RUNTIME_IPC_INTERFACE_BASE_HPP
20 
21 #include "iceoryx_posh/iceoryx_posh_types.hpp"
22 #include "iceoryx_posh/internal/runtime/ipc_message.hpp"
23 #include "iceoryx_utils/cxx/deadline_timer.hpp"
24 #include "iceoryx_utils/cxx/optional.hpp"
25 #include "iceoryx_utils/internal/posix_wrapper/unix_domain_socket.hpp"
26 #include "iceoryx_utils/internal/relocatable_pointer/relative_pointer.hpp"
27 #include "iceoryx_utils/internal/units/duration.hpp"
28 #include "iceoryx_utils/platform/fcntl.hpp"
29 #include "iceoryx_utils/platform/stat.hpp"
30 #include "iceoryx_utils/platform/types.hpp"
31 #include "iceoryx_utils/platform/unistd.hpp"
32 
33 #include <cstdint>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string>
38 #include <time.h>
39 
40 #if defined(QNX) || defined(QNX__) || defined(__QNX__)
41 #include <process.h>
42 #endif
43 
44 namespace iox
45 {
46 namespace runtime
47 {
48 enum class IpcMessageType : int32_t
49 {
50  BEGIN = -1,
51  NOTYPE = 0,
52  REG, // register app
53  REG_ACK,
54  CREATE_PUBLISHER,
55  CREATE_PUBLISHER_ACK,
56  CREATE_SUBSCRIBER,
57  CREATE_SUBSCRIBER_ACK,
58  CREATE_INTERFACE,
59  CREATE_INTERFACE_ACK,
60  CREATE_APPLICATION,
61  CREATE_APPLICATION_ACK,
62  CREATE_CONDITION_VARIABLE,
63  CREATE_CONDITION_VARIABLE_ACK,
64  CREATE_NODE,
65  CREATE_NODE_ACK,
66  FIND_SERVICE,
67  KEEPALIVE,
68  TERMINATION,
69  TERMINATION_ACK,
70  PREPARE_APP_TERMINATION,
71  PREPARE_APP_TERMINATION_ACK,
72  ERROR,
73  APP_WAIT,
74  WAKEUP_TRIGGER,
75  REPLAY,
76  SERVICE_REGISTRY_CHANGE_COUNTER,
77  MESSAGE_NOT_SUPPORTED,
78  // etc..
79  END,
80 };
81 
83 enum class IpcMessageErrorType : int32_t
84 {
85  BEGIN,
86  INVALID_STATE,
87  NOTYPE,
89  NO_UNIQUE_CREATED,
90  REQUEST_PUBLISHER_WRONG_IPC_MESSAGE_RESPONSE,
91  REQUEST_PUBLISHER_NO_WRITABLE_SHM_SEGMENT,
92  REQUEST_SUBSCRIBER_WRONG_IPC_MESSAGE_RESPONSE,
93  REQUEST_CONDITION_VARIABLE_WRONG_IPC_MESSAGE_RESPONSE,
94  REQUEST_EVENT_VARIABLE_WRONG_IPC_MESSAGE_RESPONSE,
95  PUBLISHER_LIST_FULL,
96  SUBSCRIBER_LIST_FULL,
97  CONDITION_VARIABLE_LIST_FULL,
98  EVENT_VARIABLE_LIST_FULL,
99  NODE_DATA_LIST_FULL,
100  END,
101 };
102 
103 
106 IpcMessageType stringToIpcMessageType(const char* str) noexcept;
107 
110 std::string IpcMessageTypeToString(const IpcMessageType msg) noexcept;
111 
114 IpcMessageErrorType stringToIpcMessageErrorType(const char* str) noexcept;
117 std::string IpcMessageErrorTypeToString(const IpcMessageErrorType msg) noexcept;
118 
119 class IpcInterfaceUser;
120 class IpcInterfaceCreator;
121 
127 {
128  public:
134  bool receive(IpcMessage& answer) const noexcept;
135 
144  bool timedReceive(const units::Duration timeout, IpcMessage& answer) const noexcept;
145 
151  bool send(const IpcMessage& msg) const noexcept;
152 
160  bool timedSend(const IpcMessage& msg, const units::Duration timeout) const noexcept;
161 
165  const RuntimeName_t& getRuntimeName() const noexcept;
166 
176  bool isInitialized() const noexcept;
177 
181  static void cleanupOutdatedIpcChannel(const RuntimeName_t& name) noexcept;
182 
183  friend class IpcInterfaceUser;
184  friend class IpcInterfaceCreator;
185  friend class IpcRuntimeInterface;
186 
187  protected:
191  bool reopen() noexcept;
192 
197  bool ipcChannelMapsToFile() noexcept;
198 
201  IpcInterfaceBase() = delete;
202 
203  IpcInterfaceBase(const RuntimeName_t& runtimeName, const uint64_t maxMessages, const uint64_t messageSize) noexcept;
204  virtual ~IpcInterfaceBase() noexcept = default;
205 
209  IpcInterfaceBase& operator=(const IpcInterfaceBase&) = delete;
210  IpcInterfaceBase& operator=(IpcInterfaceBase&&) = delete;
211 
216  static bool setMessageFromString(const char* buffer, IpcMessage& answer) noexcept;
217 
224  bool openIpcChannel(const posix::IpcChannelSide channelSide) noexcept;
225 
229  bool closeIpcChannel() noexcept;
230 
238  bool hasClosableIpcChannel() const noexcept;
239 
240  protected:
241  RuntimeName_t m_runtimeName;
242  uint64_t m_maxMessageSize{0U};
243  uint64_t m_maxMessages{0U};
244  iox::posix::IpcChannelSide m_channelSide{posix::IpcChannelSide::CLIENT};
245  IpcChannelType m_ipcChannel;
246 };
247 
248 } // namespace runtime
249 } // namespace iox
250 
251 #endif // IOX_POSH_RUNTIME_IPC_INTERFACE_BASE_HPP
Base-Class should never be used by the end-user. Handles the common properties and methods for the ch...
Definition: ipc_interface_base.hpp:127
bool receive(IpcMessage &answer) const noexcept
Receives a message from the IPC channel and stores it in answer.
static bool setMessageFromString(const char *buffer, IpcMessage &answer) noexcept
Set the content of answer from buffer.
bool openIpcChannel(const posix::IpcChannelSide channelSide) noexcept
Opens a IPC channel and default permissions stored in m_perms and stores the descriptor.
bool hasClosableIpcChannel() const noexcept
If a IPC channel was moved then m_runtimeName was cleared and this object gave up the control of that...
bool reopen() noexcept
Closes and opens an existing IPC channel using the same parameters as before. If the queue was not op...
bool send(const IpcMessage &msg) const noexcept
Tries to send the message specified in msg.
bool ipcChannelMapsToFile() noexcept
Checks if the IPC channel has its counterpart in the file system.
bool closeIpcChannel() noexcept
Closes a IPC channel.
bool isInitialized() const noexcept
If the IPC channel could not be opened or linked in the constructor it will return false,...
const RuntimeName_t & getRuntimeName() const noexcept
Returns the interface name, the unique char string which explicitly identifies the IPC channel.
bool timedReceive(const units::Duration timeout, IpcMessage &answer) const noexcept
Tries to receive a message from the IPC channel within a specified timeout. It stores the message in ...
static void cleanupOutdatedIpcChannel(const RuntimeName_t &name) noexcept
Since there might be an outdated IPC channel due to an unclean temination this function closes the IP...
bool timedSend(const IpcMessage &msg, const units::Duration timeout) const noexcept
Tries to send the message specified in msg to the message queue within a specified timeout.
Class for creating and handling a IPC channel.
Definition: ipc_interface_creator.hpp:31
Class for using a IPC channel.
Definition: ipc_interface_user.hpp:29
Definition: ipc_message.hpp:43
Definition: ipc_runtime_interface.hpp:29
Definition: service_description.hpp:29
iox::posix::UnixDomainSocket IpcChannelType
The socket is created in the current path if no absolute path is given hence we need an absolut path ...
Definition: iceoryx_posh_types.hpp:60