10 #include <netinet/in.h>
14 #include <sys/ioctl.h>
15 #include <sys/socket.h>
21 #include "DoIPConfig.h"
96 template <
typename Model = DefaultDoIPServerModel>
160 void setVin(
const std::string &VINString);
176 void setEid(uint64_t nputEID);
187 void setGid(uint64_t inputGID);
219 struct sockaddr_in m_serverAddress{};
220 struct sockaddr_in m_clientAddress{};
221 ByteArray m_receiveBuf{};
222 std::string m_clientIp{};
227 std::atomic<bool> m_running{
false};
228 std::vector<std::thread> m_workerThreads;
232 ServerConfig m_config;
237 void setMulticastGroup(
const char *address)
const;
241 template <
typename Model>
242 void tcpListenerThread();
244 void connectionHandlerThread(std::unique_ptr<DoIPConnection> connection);
246 void udpListenerThread();
247 void udpAnnouncementThread();
248 ssize_t sendVehicleAnnouncement();
250 ssize_t sendUdpResponse(DoIPMessage msg);
254 template <
typename Model>
256 static_assert(std::is_default_constructible<Model>::value,
257 "Model must be default-constructible");
260 if (listen(m_tcp_sock, 5) < 0) {
264 int tcpSocket = accept(m_tcp_sock,
nullptr,
nullptr);
269 return std::unique_ptr<DoIPConnection>(
new DoIPConnection(tcpSocket, std::make_unique<Model>()));
275 template <
typename Model>
276 void DoIPServer::tcpListenerThread() {
279 while (m_running.load()) {
280 auto connection = waitForTcpConnection<Model>();
283 if (m_running.load()) {
285 std::this_thread::sleep_for(std::chrono::milliseconds(100));
292 std::thread(&DoIPServer::connectionHandlerThread,
this, std::move(connection)).detach();
Defines the ByteArray type and utility functions for byte manipulation.
#define LOG_TCP_DEBUG(...)
#define LOG_DOIP_INFO(...)
DoIP Server class to handle incoming DoIP connections and UDP messages.
void setLoopbackMode(bool useLoopback)
Enable/disable loopback mode for announcements (no broadcast).
DoIPServer(const DoIPServer &)=delete
bool setupTcpSocket()
Initialize and bind the TCP socket for DoIP.
void setAnnounceNum(int Num)
Set the number of vehicle announcements to send.
DoIPServer & operator=(const DoIPServer &)=delete
void setAnnounceInterval(unsigned int Interval)
Set the interval between announcements in milliseconds.
DoIPFurtherAction getFurtherActionRequired() const
Get current further action requirement status.
void setVin(const std::string &VINString)
Set VIN from a 17-character string.
std::string getClientIp() const
Get last accepted client IP (string form).
bool isRunning() const
Check if the server is currently running.
void setEid(uint64_t nputEID)
Set EID value.
DoIPServer & operator=(DoIPServer &&)=delete
DoIPServer(DoIPServer &&)=delete
const DoIpEid & getEid() const
Get current EID.
void setGid(uint64_t inputGID)
Set GID value.
void closeTcpSocket()
Close the TCP socket if open.
std::unique_ptr< DoIPConnection > waitForTcpConnection()
Block until a TCP client connects and create a DoIP connection.
void setFurtherActionRequired(DoIPFurtherAction furtherActionRequired)
Set further action requirement status.
const DoIpVin & getVin() const
Get current VIN.
DoIPServer(const ServerConfig &config=DefaultServerConfig)
Construct a DoIP server with the given configuration.
bool setDefaultEid()
Sets the EID to a default value based on the MAC address.
const DoIpGid & getGid() const
Get current GID.
bool setupUdpSocket()
Initialize and bind the UDP socket for announcements and UDP messages.
int getClientPort() const
Get last accepted client TCP port.
void setLogicalGatewayAddress(DoIPAddress logicalAddress)
Set the logical DoIP gateway address.
void closeUdpSocket()
Close the UDP socket if open.
static const GenericFixedId Zero
Static instance containing only zeros.
const ServerConfig DefaultServerConfig
uint16_t DoIPAddress
Represents a 16-bit DoIP address consisting of high and low significant bytes.
std::function< std::optional< DoIPServerModel >(DoIPConnection *)> ConnectionAcceptedHandler
Callback invoked when a new TCP connection is established.
constexpr int DOIP_SERVER_TCP_PORT
Server configuration structure used to initialize a DoIP server.
unsigned int announceInterval
DoIPAddress logicalAddress