OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_drv_file.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2022 by David Register *
3 * Copyright (C) 2022 by Alec Leamas *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
17 **************************************************************************/
18
25// For compilers that support precompilation, includes "wx.h".
26#include <wx/wxprec.h>
27
28#ifndef WX_PRECOMP
29#include <wx/wx.h>
30#endif // precompiled headers
31
32#include <iostream>
33#include <fstream>
34#include <string>
35
36#include <wx/log.h>
37
38#include "model/comm_driver.h"
40#include "model/comm_drv_file.h"
41#include "model/ocpn_utils.h"
42
43using namespace std;
44
46 virtual void Notify(std::shared_ptr<const NavMsg> message) {}
47 virtual void Notify(const AbstractCommDriver& driver) {}
48};
49
50static VoidDriverListener kVoidDriverListener;
51
52static vector<unsigned char> HexToChar(string hex) {
53 if (hex.size() % 2 == 1) hex = string("0") + hex;
54 vector<unsigned char> chars;
55 for (size_t i = 0; i < hex.size(); i += 2) {
56 istringstream ss(hex.substr(i, 2));
57 unsigned ival;
58 ss >> std::hex >> ival;
59 chars.push_back(static_cast<unsigned char>(ival));
60 }
61 return chars;
62}
63
64static shared_ptr<const NavMsg> LineToMessage(const string& line,
65 std::shared_ptr<NavAddr> src) {
66 auto words = ocpn::split(line.c_str(), " ");
67 NavAddr::Bus bus = NavAddr::StringToBus(words[0]);
68 switch (bus) {
69 case NavAddr::Bus::N2000:
70 if (true) { // Create a separate scope.
71 N2kName name(N2kName::Parse(words[2]));
72 vector<unsigned char> payload(HexToChar(words[3]));
73 // FIXME (Leamas)
74 // return make_shared<Nmea2000Msg>(name, payload, src);
75 return make_shared<NullNavMsg>();
76 }
77 break;
78 case NavAddr::Bus::N0183:
79 if (true) { // Create a separate scope.
80 const string id(words[2]);
81 return make_shared<Nmea0183Msg>(id, words[3], src);
82 }
83 break;
84 default:
85 std::cerr << "Cannot parse line: \"" << line << "\"\n" << flush;
86 return make_shared<NullNavMsg>();
87 break;
88 }
89 return make_shared<NullNavMsg>(); // for the compiler.
90}
91
92FileCommDriver::FileCommDriver(const string& opath, const string& ipath,
94 : AbstractCommDriver(NavAddr::Bus::TestBus, opath),
95 output_path(opath),
96 input_path(ipath),
97 listener(l) {
98 if (input_path != "") {
99 ifstream f(input_path);
100 string line;
101 while (getline(f, line)) {
102 auto msg = LineToMessage(line, GetAddress());
103 if (msg->bus != NavAddr::Bus::Undef) listener.Notify(msg);
104 }
105 }
106}
107
108FileCommDriver::FileCommDriver(const string& opath)
109 : FileCommDriver(opath, "", kVoidDriverListener) {}
110
111std::shared_ptr<NavAddr> FileCommDriver::GetAddress() {
112 return std::make_shared<NavAddr>(NavAddrTest(output_path));
113}
114
115bool FileCommDriver::SendMessage(std::shared_ptr<const NavMsg> msg,
116 std::shared_ptr<const NavAddr> addr) {
117 ofstream f;
118 f.open(output_path, ios::app);
119 if (!f.is_open()) {
120 wxLogWarning("Cannot open file %s for writing", output_path.c_str());
121 return false;
122 }
123 f << msg->to_string();
124 f.close();
125 return true;
126}
Common interface for all drivers.
Definition comm_driver.h:65
Interface for handling incoming messages.
Definition comm_driver.h:50
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.
Test driver which reads and writes data to/from files.
Driver registration container, a singleton.
std::vector< std::string > split(const char *token_string, const std::string &delimiter)
Return vector of items in s separated by delimiter.
Miscellaneous utilities, many of which string related.
N2k uses CAN which defines the basic properties of messages.
Definition comm_navmsg.h:70