|
7 | 7 | #define EXAMPLEDOIPSERVERMODEL_H |
8 | 8 |
|
9 | 9 | #include "DoIPServerModel.h" |
| 10 | +#include "ThreadSafeQueue.h" |
| 11 | +#include "uds/UdsMock.h" |
| 12 | +#include "uds/UdsResponseCode.h" |
10 | 13 |
|
11 | 14 | using namespace doip; |
12 | 15 |
|
13 | 16 | class ExampleDoIPServerModel : public DoIPServerModel { |
14 | 17 | public: |
15 | 18 | ExampleDoIPServerModel() { |
16 | | - onCloseConnection = [](IConnectionContext &ctx, DoIPCloseReason reason) noexcept { |
| 19 | + onOpenConnection = [this](IConnectionContext &ctx) noexcept { |
17 | 20 | (void)ctx; |
| 21 | + startWorker(); |
| 22 | + }; |
| 23 | + onCloseConnection = [this](IConnectionContext &ctx, DoIPCloseReason reason) noexcept { |
| 24 | + (void)ctx; |
| 25 | + stopWorker(); |
18 | 26 | DOIP_LOG_WARN("Connection closed ({})", fmt::streamed(reason)); |
19 | 27 | }; |
20 | 28 |
|
21 | | - onDiagnosticMessage = [](IConnectionContext &ctx, const DoIPMessage &msg) noexcept -> DoIPDiagnosticAck { |
| 29 | + onDiagnosticMessage = [this](IConnectionContext &ctx, const DoIPMessage &msg) noexcept -> DoIPDiagnosticAck { |
22 | 30 | (void)ctx; |
23 | | - DOIP_LOG_INFO("Received Diagnostic message (from ExampleDoIPServerModel)", fmt::streamed(msg)); |
| 31 | + m_log->info("Received Diagnostic message (from ExampleDoIPServerModel)", fmt::streamed(msg)); |
24 | 32 |
|
25 | 33 | // Example: Access payload using getPayload() |
26 | | - auto payload = msg.getDiagnosticMessagePayload(); |
27 | | - if (payload.second >= 3 && payload.first[0] == 0x22 && payload.first[1] == 0xF1 && payload.first[2] == 0x90) { |
28 | | - DOIP_LOG_INFO(" - Detected Read Data by Identifier for VIN (0xF190) -> send NACK"); |
29 | | - return DoIPNegativeDiagnosticAck::UnknownTargetAddress; |
30 | | - } |
| 34 | + // auto payload = msg.getDiagnosticMessagePayload(); |
| 35 | + // if (payload.second >= 3 && payload.first[0] == 0x22 && payload.first[1] == 0xF1 && payload.first[2] == 0x90) { |
| 36 | + // m_log->info(" - Detected Read Data by Identifier for VIN (0xF190) -> send NACK"); |
| 37 | + // return DoIPNegativeDiagnosticAck::UnknownTargetAddress; |
| 38 | + // } |
31 | 39 |
|
32 | 40 | return std::nullopt; |
33 | 41 | }; |
34 | 42 |
|
35 | | - onDiagnosticNotification = [](IConnectionContext &ctx, DoIPDiagnosticAck ack) noexcept { |
| 43 | + onDiagnosticNotification = [this](IConnectionContext &ctx, DoIPDiagnosticAck ack) noexcept { |
36 | 44 | (void)ctx; |
37 | | - DOIP_LOG_INFO("Diagnostic ACK/NACK sent (from ExampleDoIPServerModel)", fmt::streamed(ack)); |
| 45 | + m_log->info("Diagnostic ACK/NACK sent (from ExampleDoIPServerModel)", fmt::streamed(ack)); |
| 46 | + }; |
| 47 | + |
| 48 | + onDownstreamRequest = [this](IConnectionContext &ctx, const DoIPMessage &msg, ServerModelDownstreamResponseHandler callback) noexcept { |
| 49 | + (void)ctx; |
| 50 | + (void)msg; |
| 51 | + |
| 52 | + m_log->info("Received downstream request (from ExampleDoIPServerModel)", fmt::streamed(msg)); |
| 53 | + m_downstreamCallback = callback; |
| 54 | + if (!m_downstreamCallback) { |
| 55 | + m_log->error("onDownstreamRequest: No callback function passed"); |
| 56 | + return DoIPDownstreamResult::Error; |
| 57 | + } |
| 58 | + |
| 59 | + // Store message in send queue |
| 60 | + auto [data, size] = msg.getDiagnosticMessagePayload(); |
| 61 | + m_tx.push(ByteArray(data, size)); |
| 62 | + m_log->info("Enqueued msg"); |
| 63 | + return DoIPDownstreamResult::Pending; |
38 | 64 | }; |
| 65 | + |
| 66 | + m_uds.registerDefaultServices(); |
| 67 | + |
| 68 | + m_uds.registerDiagnosticSessionControlHandler([this](uint8_t sessionType) { |
| 69 | + m_loguds->info("Diagnostic Session Control requested, sessionType={:02X}", sessionType); |
| 70 | + auto response = ByteArray{sessionType}; // Positive response SID = 0x50 |
| 71 | + response.writeU16BE(m_p2_ms); |
| 72 | + response.writeU16BE(m_p2star_10ms); |
| 73 | + return std::make_pair(uds::UdsResponseCode::PositiveResponse, response); // Positive response |
| 74 | + }); |
| 75 | + |
| 76 | + m_uds.registerECUResetHandler([this](uint8_t resetType) { |
| 77 | + m_loguds->info("ECU Reset requested, resetType={:02X}", resetType); |
| 78 | + return std::make_pair(uds::UdsResponseCode::PositiveResponse, ByteArray{resetType}); // Positive response SID = 0x61 |
| 79 | + }); |
| 80 | + |
| 81 | + m_uds.registerReadDataByIdentifierHandler([this](uint16_t did) { |
| 82 | + m_loguds->info("Read Data By Identifier requested, DID={:04X}", did); |
| 83 | + if (did == 0xF190) { |
| 84 | + // Return example VIN |
| 85 | + ByteArray vinPayload = {'1', 'H', 'G', 'C', 'M', |
| 86 | + '8', '2', '6', '3', '3', |
| 87 | + 'A', '0', '0', '0', '0', '1', 'Z'}; |
| 88 | + ByteArray response = {static_cast<uint8_t>((did >> 8) & 0xFF), static_cast<uint8_t>(did & 0xFF)}; |
| 89 | + response.insert(response.end(), vinPayload.begin(), vinPayload.end()); |
| 90 | + return std::make_pair(uds::UdsResponseCode::PositiveResponse, response); // Positive response |
| 91 | + } |
| 92 | + return std::make_pair(uds::UdsResponseCode::RequestOutOfRange, ByteArray{0x22}); // Positive response |
| 93 | + }); |
| 94 | + |
| 95 | + m_uds.registerWriteDataByIdentifierHandler([this](uint16_t did, ByteArray value) { |
| 96 | + m_loguds->info("Write Data By Identifier requested, DID={:04X}, value={}", did, fmt::streamed(value)); |
| 97 | + if (did == 0xF190) { |
| 98 | + // Accept VIN write |
| 99 | + return std::make_pair(uds::UdsResponseCode::PositiveResponse, ByteArray{static_cast<uint8_t>((did >> 8) & 0xFF), static_cast<uint8_t>(did & 0xFF)}); // Positive response |
| 100 | + } |
| 101 | + return std::make_pair(uds::UdsResponseCode::RequestOutOfRange, ByteArray{0x2E}); // NRC for WriteDataByIdentifier |
| 102 | + }); |
| 103 | + |
| 104 | + m_uds.registerTesterPresentHandler([this](uint8_t subFunction) { |
| 105 | + m_loguds->info("Tester Present requested, subFunction={:02X}", subFunction); |
| 106 | + return std::make_pair(uds::UdsResponseCode::PositiveResponse, ByteArray{0x00}); // Positive response SID = 0x7E |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + private: |
| 111 | + std::shared_ptr<spdlog::logger> m_log = Logger::get("smodel"); |
| 112 | + std::shared_ptr<spdlog::logger> m_loguds = Logger::get("uds"); |
| 113 | + ServerModelDownstreamResponseHandler m_downstreamCallback = nullptr; |
| 114 | + ThreadSafeQueue<ByteArray> m_rx; |
| 115 | + ThreadSafeQueue<ByteArray> m_tx; |
| 116 | + uds::UdsMock m_uds; |
| 117 | + std::thread m_worker; |
| 118 | + bool m_running = true; |
| 119 | + uint16_t m_p2_ms = 1000; |
| 120 | + uint16_t m_p2star_10ms = 200; |
| 121 | + |
| 122 | + |
| 123 | + void startWorker() { |
| 124 | + m_worker = std::thread([this] { |
| 125 | + while (m_running) { |
| 126 | + downstream_thread(); |
| 127 | + } |
| 128 | + }); |
| 129 | + m_log->info("Started worker thread"); |
| 130 | + } |
| 131 | + void stopWorker() { |
| 132 | + m_running = false; |
| 133 | + if (m_worker.joinable()) |
| 134 | + m_worker.join(); |
| 135 | + m_log->info("Stopped worker thread"); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @brief Thread simulating downstream communication (e. g. CAN). |
| 140 | + */ |
| 141 | + void downstream_thread() { |
| 142 | + if (m_tx.size()) { |
| 143 | + // simulate send. In a real environment we could send a CAN message |
| 144 | + ByteArray req; |
| 145 | + m_tx.pop(req); |
| 146 | + m_log->info("Simulate send {}", fmt::streamed(req)); |
| 147 | + // simulate some latency |
| 148 | + std::this_thread::sleep_for(50ms); |
| 149 | + // simulate receive |
| 150 | + m_rx.push(m_uds.handleDiagnosticRequest(req)); |
| 151 | + } |
| 152 | + |
| 153 | + if (m_rx.size()) { |
| 154 | + ByteArray rsp; |
| 155 | + m_rx.pop(rsp); |
| 156 | + m_log->info("Simulate receive {}", fmt::streamed(rsp)); |
| 157 | + if (m_downstreamCallback) { |
| 158 | + m_downstreamCallback(rsp, DoIPDownstreamResult::Handled); |
| 159 | + m_downstreamCallback = nullptr; |
| 160 | + } |
| 161 | + } |
| 162 | + std::this_thread::sleep_for(10ms); |
39 | 163 | } |
40 | 164 | }; |
41 | 165 |
|
|
0 commit comments