39#include "comm_driver.h"
40#include "comm_drv_registry.h"
41#include "comm_drv_file.h"
42#include "ocpn_utils.h"
46 virtual void Notify(std::shared_ptr<const NavMsg> message) {}
64std::shared_ptr<NavAddr> FileCommDriver::GetAddress() {
65 return std::make_shared<NavAddr>(
NavAddrTest(output_path));
68bool FileCommDriver::SendMessage(std::shared_ptr<const NavMsg> msg,
69 std::shared_ptr<const NavAddr> addr) {
71 f.open(output_path, ios::app);
73 wxLogWarning(
"Cannot open file %s for writing", output_path.c_str());
76 f << msg->to_string();
81static vector<unsigned char> HexToChar(
string hex) {
82 if (hex.size() % 2 == 1) hex = string(
"0") + hex;
83 vector<unsigned char> chars;
84 for (
size_t i = 0; i < hex.size(); i += 2) {
85 istringstream ss(hex.substr(i, 2));
87 ss >> std::hex >> ival;
88 chars.push_back(
static_cast<unsigned char>(ival));
93static shared_ptr<const NavMsg> LineToMessage(
const string& line,
94 std::shared_ptr<NavAddr> src) {
95 auto words = ocpn::split(line.c_str(),
" ");
96 NavAddr::Bus bus = NavAddr::StringToBus(words[0]);
98 case NavAddr::Bus::N2000:
100 N2kName name(N2kName::Parse(words[2]));
101 vector<unsigned char> payload(HexToChar(words[3]));
104 return make_shared<NullNavMsg>();
107 case NavAddr::Bus::N0183:
109 const string id(words[2]);
110 return make_shared<Nmea0183Msg>(
id, words[3], src);
114 std::cerr <<
"Cannot parse line: \"" << line <<
"\"\n" << flush;
115 return make_shared<NullNavMsg>();
118 return make_shared<NullNavMsg>();
122 CommDriverRegistry::GetInstance().
Activate(shared_from_this());
123 if (input_path !=
"") {
124 ifstream f(input_path);
126 while (getline(f, line)) {
127 auto msg = LineToMessage(line, GetAddress());
128 if (msg->bus != NavAddr::Bus::Undef) listener.
Notify(msg);
Common interface for all drivers.
void Activate(DriverPtr driver)
Add driver to list of active drivers.
Interface implemented by transport layer and possible other parties like test code which should handl...
virtual void Notify(std::shared_ptr< const NavMsg > message)=0
Handle a received message.
Read and write data to/from files test driver
FileCommDriver(const std::string &opath, const std::string &ipath, DriverListener &l)
An instance which can write to file and play data from another.
void Activate() override
Register driver in the driver Registry.
Where messages are sent to or received from.
Test driver listener sink.
N2k uses CAN which defines the basic properties of messages.