41#include "model/comm_drv_file.h"
42#include "model/ocpn_utils.h"
47 virtual void Notify(std::shared_ptr<const NavMsg> message) {}
53static vector<unsigned char> HexToChar(
string hex) {
54 if (hex.size() % 2 == 1) hex = string(
"0") + hex;
55 vector<unsigned char> chars;
56 for (
size_t i = 0; i < hex.size(); i += 2) {
57 istringstream ss(hex.substr(i, 2));
59 ss >> std::hex >> ival;
60 chars.push_back(
static_cast<unsigned char>(ival));
65static shared_ptr<const NavMsg> LineToMessage(
const string& line,
66 std::shared_ptr<NavAddr> src) {
67 auto words = ocpn::split(line.c_str(),
" ");
68 NavAddr::Bus bus = NavAddr::StringToBus(words[0]);
70 case NavAddr::Bus::N2000:
72 N2kName name(N2kName::Parse(words[2]));
73 vector<unsigned char> payload(HexToChar(words[3]));
76 return make_shared<NullNavMsg>();
79 case NavAddr::Bus::N0183:
81 const string id(words[2]);
82 return make_shared<Nmea0183Msg>(
id, words[3], src);
86 std::cerr <<
"Cannot parse line: \"" << line <<
"\"\n" << flush;
87 return make_shared<NullNavMsg>();
90 return make_shared<NullNavMsg>();
99 if (input_path !=
"") {
100 ifstream f(input_path);
102 while (getline(f, line)) {
103 auto msg = LineToMessage(line, GetAddress());
104 if (msg->bus != NavAddr::Bus::Undef) listener.Notify(msg);
112std::shared_ptr<NavAddr> FileCommDriver::GetAddress() {
113 return std::make_shared<NavAddr>(
NavAddrTest(output_path));
116bool FileCommDriver::SendMessage(std::shared_ptr<const NavMsg> msg,
117 std::shared_ptr<const NavAddr> addr) {
119 f.open(output_path, ios::app);
121 wxLogWarning(
"Cannot open file %s for writing", output_path.c_str());
124 f << msg->to_string();
Common interface for all drivers.
Interface implemented by transport layer and possible other parties like test code which should handl...
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.
Where messages are sent to or received from.
Communication driver layer.
Driver registration container, a singleton.
N2k uses CAN which defines the basic properties of messages.