27 #include <wx/wxprec.h>
39 #include "model/comm_driver.h"
40 #include "model/comm_drv_registry.h"
41 #include "model/comm_drv_file.h"
42 #include "model/ocpn_utils.h"
45 virtual void Notify(std::shared_ptr<const NavMsg> message) {}
63 std::shared_ptr<NavAddr> FileCommDriver::GetAddress() {
64 return std::make_shared<NavAddr>(
NavAddrTest(output_path));
67 bool FileCommDriver::SendMessage(std::shared_ptr<const NavMsg> msg,
68 std::shared_ptr<const NavAddr> addr) {
70 f.open(output_path, ios::app);
72 wxLogWarning(
"Cannot open file %s for writing", output_path.c_str());
75 f << msg->to_string();
80 static vector<unsigned char> HexToChar(
string hex) {
81 if (hex.size() % 2 == 1) hex = string(
"0") + hex;
82 vector<unsigned char> chars;
83 for (
size_t i = 0; i < hex.size(); i += 2) {
84 istringstream ss(hex.substr(i, 2));
86 ss >> std::hex >> ival;
87 chars.push_back(
static_cast<unsigned char>(ival));
92 static shared_ptr<const NavMsg> LineToMessage(
const string& line,
93 std::shared_ptr<NavAddr> src) {
94 auto words = ocpn::split(line.c_str(),
" ");
95 NavAddr::Bus bus = NavAddr::StringToBus(words[0]);
97 case NavAddr::Bus::N2000:
99 N2kName name(N2kName::Parse(words[2]));
100 vector<unsigned char> payload(HexToChar(words[3]));
103 return make_shared<NullNavMsg>();
106 case NavAddr::Bus::N0183:
108 const string id(words[2]);
109 return make_shared<Nmea0183Msg>(
id, words[3], src);
113 std::cerr <<
"Cannot parse line: \"" << line <<
"\"\n" << flush;
114 return make_shared<NullNavMsg>();
117 return make_shared<NullNavMsg>();
121 CommDriverRegistry::GetInstance().
Activate(shared_from_this());
122 if (input_path !=
"") {
123 ifstream f(input_path);
125 while (getline(f, line)) {
126 auto msg = LineToMessage(line, GetAddress());
127 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.
N2k uses CAN which defines the basic properties of messages.