-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
demos: Reorganized common CAN behavior; Review suggestions
- Loading branch information
Showing
5 changed files
with
101 additions
and
130 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// SPDX-FileCopyrightText: 2024 Vector Informatik GmbH | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "silkit/services/can/all.hpp" | ||
#include "silkit/services/can/string_utils.hpp" | ||
#include "silkit/services/logging/ILogger.hpp" | ||
|
||
using namespace SilKit::Services::Can; | ||
|
||
std::ostream& operator<<(std::ostream& out, std::chrono::nanoseconds timestamp) | ||
{ | ||
auto seconds = std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, 1>>>(timestamp); | ||
out << seconds.count() << "s"; | ||
return out; | ||
} | ||
|
||
// This is the common behavior used in CanReaderDemo and CanWriterDemo | ||
namespace CanDemoCommon { | ||
|
||
void FrameTransmitHandler(const CanFrameTransmitEvent& canFrameAck, ILogger* logger) | ||
{ | ||
// Log | ||
std::stringstream buffer; | ||
buffer << "Ack CAN frame, canId=" << canFrameAck.canId << ", status='" << canFrameAck.status << "'"; | ||
logger->Info(buffer.str()); | ||
} | ||
|
||
void FrameHandler(const CanFrameEvent& canFrameEvent, ILogger* logger) | ||
{ | ||
// Indicate frame type in log message | ||
std::string frameTypeHint = ""; | ||
if ((canFrameEvent.frame.flags & static_cast<CanFrameFlagMask>(CanFrameFlag::Fdf)) != 0) | ||
{ | ||
frameTypeHint = "FD "; | ||
} | ||
|
||
// Log | ||
std::string payloadStr(canFrameEvent.frame.dataField.begin(), canFrameEvent.frame.dataField.end()); | ||
std::stringstream buffer; | ||
buffer << "Receive CAN " << frameTypeHint << "frame, canId=" << canFrameEvent.frame.canId << ", data ='" | ||
<< payloadStr << "'"; | ||
logger->Info(buffer.str()); | ||
} | ||
|
||
} // namespace CanDemoBehavior |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters