OpenCPN Partial API docs
Loading...
Searching...
No Matches
comm_drv_file.cpp
1/***************************************************************************
2 *
3 * Project: OpenCPN
4 * Purpose: Implement comm_drv_file.h -- driver reading/writing to/from files
5 * Author: David Register, Alec Leamas
6 *
7 ***************************************************************************
8 * Copyright (C) 2022 by David Register, Alec Leamas *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the *
22 * Free Software Foundation, Inc., *
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
24 **************************************************************************/
25
26// For compilers that support precompilation, includes "wx.h".
27#include <wx/wxprec.h>
28
29#ifndef WX_PRECOMP
30#include <wx/wx.h>
31#endif // precompiled headers
32
33#include <iostream>
34#include <fstream>
35#include <string>
36
37#include <wx/log.h>
38
39#include "model/comm_driver.h"
41#include "model/comm_drv_file.h"
42#include "model/ocpn_utils.h"
43
44using namespace std;
45
47 virtual void Notify(std::shared_ptr<const NavMsg> message) {}
48 virtual void Notify(const AbstractCommDriver& driver) {}
49};
50
51static VoidDriverListener kVoidDriverListener;
52
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));
58 unsigned ival;
59 ss >> std::hex >> ival;
60 chars.push_back(static_cast<unsigned char>(ival));
61 }
62 return chars;
63}
64
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]);
69 switch (bus) {
70 case NavAddr::Bus::N2000:
71 if (true) { // Create a separate scope.
72 N2kName name(N2kName::Parse(words[2]));
73 vector<unsigned char> payload(HexToChar(words[3]));
74 // FIXME (Leamas)
75 // return make_shared<Nmea2000Msg>(name, payload, src);
76 return make_shared<NullNavMsg>();
77 }
78 break;
79 case NavAddr::Bus::N0183:
80 if (true) { // Create a separate scope.
81 const string id(words[2]);
82 return make_shared<Nmea0183Msg>(id, words[3], src);
83 }
84 break;
85 default:
86 std::cerr << "Cannot parse line: \"" << line << "\"\n" << flush;
87 return make_shared<NullNavMsg>();
88 break;
89 }
90 return make_shared<NullNavMsg>(); // for the compiler.
91}
92
93FileCommDriver::FileCommDriver(const string& opath, const string& ipath,
95 : AbstractCommDriver(NavAddr::Bus::TestBus, opath),
96 output_path(opath),
97 input_path(ipath),
98 listener(l) {
99 if (input_path != "") {
100 ifstream f(input_path);
101 string line;
102 while (getline(f, line)) {
103 auto msg = LineToMessage(line, GetAddress());
104 if (msg->bus != NavAddr::Bus::Undef) listener.Notify(msg);
105 }
106 }
107}
108
109FileCommDriver::FileCommDriver(const string& opath)
110 : FileCommDriver(opath, "", kVoidDriverListener) {}
111
112std::shared_ptr<NavAddr> FileCommDriver::GetAddress() {
113 return std::make_shared<NavAddr>(NavAddrTest(output_path));
114}
115
116bool FileCommDriver::SendMessage(std::shared_ptr<const NavMsg> msg,
117 std::shared_ptr<const NavAddr> addr) {
118 ofstream f;
119 f.open(output_path, ios::app);
120 if (!f.is_open()) {
121 wxLogWarning("Cannot open file %s for writing", output_path.c_str());
122 return false;
123 }
124 f << msg->to_string();
125 f.close();
126 return true;
127}
Common interface for all drivers.
Definition comm_driver.h:58
Interface implemented by transport layer and possible other parties like test code which should handl...
Definition comm_driver.h:48
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.
Definition comm_navmsg.h:66