HDLCd-Tools
main-hdlcd-hexchanger.cpp
Go to the documentation of this file.
1 
22 #include "Config.h"
23 #include <iostream>
24 #include <vector>
25 #include <boost/asio.hpp>
26 #include <boost/program_options.hpp>
27 #include <boost/regex.hpp>
28 #include "HdlcdClient.h"
29 #include "HdlcdPacketDataPrinter.h"
30 #include "LineReader.h"
31 
32 int main(int argc, char* argv[]) {
33  try {
34  // Declare the supported options.
35  boost::program_options::options_description l_Description("Allowed options");
36  l_Description.add_options()
37  ("help,h", "produce this help message")
38  ("version,v", "show version information")
39  ("connect,c", boost::program_options::value<std::string>(),
40  "connect to a single device via the HDLCd\n"
41  "syntax: SerialPort@IPAddess:PortNbr\n"
42  " linux: /dev/ttyUSB0@localhost:5001\n"
43  " windows: //./COM1@example.com:5001")
44  ;
45 
46  // Parse the command line
47  boost::program_options::variables_map l_VariablesMap;
48  boost::program_options::store(boost::program_options::parse_command_line(argc, argv, l_Description), l_VariablesMap);
49  boost::program_options::notify(l_VariablesMap);
50  if (l_VariablesMap.count("version")) {
51  std::cerr << "HDLCd payload exchanger (hexdumps via STDIO) version " << HDLCD_TOOLS_VERSION_MAJOR << "." << HDLCD_TOOLS_VERSION_MINOR
52  << " built with hdlcd-devel v" << HDLCD_DEVEL_VERSION_MAJOR << "." << HDLCD_DEVEL_VERSION_MINOR << std::endl;
53  } // if
54 
55  if (l_VariablesMap.count("help")) {
56  std::cout << l_Description << std::endl;
57  std::cout << "The HDLC hex exchanger is Copyright (C) 2016, and GNU GPL'd, by Florian Evers." << std::endl;
58  std::cout << "Bug reports, feedback, admiration, abuse, etc, to: https://github.com/Strunzdesign/hdlcd-tools" << std::endl;
59  return 1;
60  } // if
61 
62  if (!l_VariablesMap.count("connect")) {
63  std::cout << "hdlcd-hexchanger: you have to specify one device to connect to" << std::endl;
64  std::cout << "hdlcd-hexchanger: Use --help for more information." << std::endl;
65  return 1;
66  } // if
67 
68  // Install signal handlers
69  boost::asio::io_service l_IoService;
70  boost::asio::signal_set l_Signals(l_IoService);
71  l_Signals.add(SIGINT);
72  l_Signals.add(SIGTERM);
73  l_Signals.async_wait([&l_IoService](boost::system::error_code, int){ l_IoService.stop(); });
74 
75  // Parse the destination specifier
76  static boost::regex s_RegEx("^(.*?)@(.*?):(.*?)$");
77  boost::smatch l_Match;
78  if (boost::regex_match(l_VariablesMap["connect"].as<std::string>(), l_Match, s_RegEx)) {
79  // Resolve destination
80  boost::asio::ip::tcp::resolver l_Resolver(l_IoService);
81  auto l_EndpointIterator = l_Resolver.resolve({ l_Match[2], l_Match[3] });
82 
83  // Prepare input
84  LineReader l_LineReader(l_IoService);
85 
86  // Prepare the HDLCd client entity
88  l_HdlcdClient.SetOnClosedCallback([&l_IoService](){ l_IoService.stop(); });
89  l_HdlcdClient.SetOnDataCallback([](const HdlcdPacketData& a_PacketData){ HdlcdPacketDataPrinter(a_PacketData); });
90  l_HdlcdClient.AsyncConnect(l_EndpointIterator, [&l_HdlcdClient, &l_LineReader, &l_Signals](bool a_bSuccess) {
91  if (a_bSuccess) {
92  l_LineReader.SetOnInputLineCallback([&l_HdlcdClient](const std::vector<unsigned char> a_Buffer){ l_HdlcdClient.Send(HdlcdPacketData::CreatePacket(a_Buffer, true));});
93  } else {
94  std::cout << "Failed to connect to the HDLC Daemon!" << std::endl;
95  l_Signals.cancel();
96  } // else
97  }); // AsyncConnect
98 
99  // Start event processing
100  l_IoService.run();
101  } else {
102  throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
103  } // else
104  } catch (std::exception& a_Error) {
105  std::cerr << "Exception: " << a_Error.what() << "\n";
106  } // catch
107 
108  return 0;
109 }
110 
#define HDLCD_DEVEL_VERSION_MINOR
Definition: HdlcdConfig.h:3
void AsyncConnect(boost::asio::ip::tcp::resolver::iterator a_EndpointIterator, std::function< void(bool a_bSuccess)> a_OnConnectedCallback)
Perform an asynchronous connect procedure regarding both TCP sockets.
Definition: HdlcdClient.h:82
Additional tools to be used together with the HDLC Daemon. Copyright (C) 2016 Florian Evers...
bool Send(const HdlcdPacketData &a_PacketData, std::function< void()> a_OnSendDoneCallback=nullptr)
Send a single data packet to the peer entity.
Definition: HdlcdClient.h:203
#define HDLCD_DEVEL_VERSION_MAJOR
Definition: HdlcdConfig.h:2
static HdlcdPacketData CreatePacket(const std::vector< unsigned char > a_Payload, bool a_bReliable, bool a_bInvalid=false, bool a_bWasSent=false)
void HdlcdPacketDataPrinter(const HdlcdPacketData &a_PacketData)
Additional tools to be used together with the HDLC Daemon. Copyright (C) 2016 Florian Evers...
int main(int argc, char *argv[])
Class HdlcdClient.
Definition: HdlcdClient.h:54
void SetOnClosedCallback(std::function< void()> a_OnClosedCallback)
Provide a callback method to be called if this client entity is closing.
Definition: HdlcdClient.h:192
void SetOnDataCallback(std::function< void(const HdlcdPacketData &a_PacketData)> a_OnDataCallback)
Provide a callback method to be called for received data packets.
Definition: HdlcdClient.h:172
void SetOnInputLineCallback(std::function< void(const std::vector< unsigned char >)> a_OnInputLineCallback)
Definition: LineReader.h:37
Copyright (c) 2016, Florian Evers, florian-evers@gmx.de All rights reserved.