This repository was archived by the owner on Dec 17, 2025. It is now read-only.
forked from AVL-DiTEST-DiagDev/libdoip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexampleDoIPServer.cpp
More file actions
132 lines (107 loc) · 3.4 KB
/
exampleDoIPServer.cpp
File metadata and controls
132 lines (107 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <chrono>
#include <iomanip>
#include <iostream>
#include <thread>
#include "DoIPAddress.h"
#include "DoIPServer.h"
#include "Logger.h"
#include "DoIPServer.h"
#include "ExampleDoIPServerModel.h"
using namespace doip;
using namespace std;
static const DoIPAddress LOGICAL_ADDRESS(0x0028);
DoIPServer server;
std::vector<std::thread> doipReceiver;
bool serverActive = false;
std::unique_ptr<DoIPConnection> tcpConnection(nullptr);
void listenUdp();
void listenTcp();
/*
* Check permantly if udp message was received
*/
void listenUdp() {
UDP_LOG_INFO("UDP listener thread started");
while (serverActive) {
ssize_t result = server.receiveUdpMessage();
// If timeout (result == 0), sleep briefly to prevent CPU spinning
if (result == 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
}
/*
* Check permantly if tcp message was received
*/
void listenTcp() {
UDP_LOG_INFO("TCP listener thread started");
while (true) {
tcpConnection = server.waitForTcpConnection<ExampleDoIPServerModel>();
while (tcpConnection->isSocketActive()) {
tcpConnection->receiveTcpMessage();
}
}
}
static void ConfigureDoipServer() {
// VIN needs to have a fixed length of 17 bytes.
// Shorter VINs will be padded with '0'
server.setVIN("EXAMPLESERVER");
server.setLogicalGatewayAddress(LOGICAL_ADDRESS.toUint16());
server.setGID(0);
server.setFAR(DoIPFurtherAction::NoFurtherAction);
server.setEID(0);
// be more relaxed for testing purposes
server.setAnnounceInterval(2000);
server.setAnnounceNum(10);
// doipserver->setAnnounceNum(tempNum);
// doipserver->setAnnounceInterval(tempInterval);
}
static void printUsage(const char *progName) {
cout << "Usage: " << progName << " [OPTIONS]\n";
cout << "Options:\n";
cout << " --loopback Use loopback (127.0.0.1) for announcements instead of broadcast\n";
cout << " --help Show this help message\n";
}
int main(int argc, char *argv[]) {
bool useLoopback = false;
// Parse command line arguments
for (int i = 1; i < argc; i++) {
string arg = argv[i];
if (arg == "--loopback") {
useLoopback = true;
DOIP_LOG_INFO("Loopback mode enabled");
} else if (arg == "--help") {
printUsage(argv[0]);
return 0;
} else {
cout << "Unknown argument: " << arg << endl;
printUsage(argv[0]);
return 1;
}
}
// Configure logging
doip::Logger::setLevel(spdlog::level::debug);
DOIP_LOG_INFO("Starting DoIP Server Example");
ConfigureDoipServer();
// Configure server based on mode
if (useLoopback) {
server.setAnnouncementMode(true);
}
if (!server.setupUdpSocket()) {
DOIP_LOG_CRITICAL("Failed to set up UDP socket");
return 1;
}
if (!server.setupTcpSocket()) {
DOIP_LOG_CRITICAL("Failed to set up TCP socket");
return 1;
}
serverActive = true;
DOIP_LOG_INFO("Starting UDP and TCP listener threads");
doipReceiver.push_back(thread(&listenUdp));
doipReceiver.push_back(thread(&listenTcp));
server.sendVehicleAnnouncement();
DOIP_LOG_INFO("Vehicle announcement sent");
doipReceiver.at(0).join();
doipReceiver.at(1).join();
DOIP_LOG_INFO("DoIP Server Example terminated");
return 0;
}