s-net-Tools
main-snet-dissector.cpp
Go to the documentation of this file.
1 
22 #include "Config.h"
23 #include <iostream>
24 #include <boost/asio.hpp>
25 #include <boost/program_options.hpp>
26 #include <boost/regex.hpp>
27 #include "HdlcdClient.h"
28 #include "SnetPacketPrinter.h"
29 
30 int main(int argc, char* argv[]) {
31  try {
32  // Declare the supported options.
33  boost::program_options::options_description l_Description("Allowed options");
34  l_Description.add_options()
35  ("help,h", "produce this help message")
36  ("version,v", "show version information")
37  ("connect,c", boost::program_options::value<std::string>(),
38  "syntax: SerialPort@IPAddess:PortNbr\n"
39  " linux: /dev/ttyUSB0@localhost:5001\n"
40  " windows: //./COM1@example.com:5001")
41  ;
42 
43  // Parse the command line
44  boost::program_options::variables_map l_VariablesMap;
45  boost::program_options::store(boost::program_options::parse_command_line(argc, argv, l_Description), l_VariablesMap);
46  boost::program_options::notify(l_VariablesMap);
47  if (l_VariablesMap.count("version")) {
48  std::cerr << "s-net(r) packet dissector version " << SNET_TOOLS_VERSION_MAJOR << "." << SNET_TOOLS_VERSION_MINOR
49  << " built with hdlcd-devel version " << HDLCD_DEVEL_VERSION_MAJOR << "." << HDLCD_DEVEL_VERSION_MINOR
50  << " and snet-devel version " << SNET_DEVEL_VERSION_MAJOR << "." << SNET_DEVEL_VERSION_MINOR << std::endl;
51  } // if
52 
53  if (l_VariablesMap.count("help")) {
54  std::cout << l_Description << std::endl;
55  std::cout << "The dissector for s-net(r) packets for the HDLC Daemon is Copyright (C) 2016, and GNU GPL'd, by Florian Evers." << std::endl;
56  std::cout << "Bug reports, feedback, admiration, abuse, etc, to: https://github.com/Strunzdesign/snet-tools" << std::endl;
57  return 1;
58  } // if
59 
60  if (!l_VariablesMap.count("connect")) {
61  std::cout << "snet-dissector: you have to specify one device to connect to" << std::endl;
62  std::cout << "snet-dissector: Use --help for more information." << std::endl;
63  return 1;
64  } // if
65 
66  // Install signal handlers
67  boost::asio::io_service l_IoService;
68  boost::asio::signal_set l_Signals(l_IoService);
69  l_Signals.add(SIGINT);
70  l_Signals.add(SIGTERM);
71  l_Signals.async_wait([&l_IoService](boost::system::error_code, int){ l_IoService.stop(); });
72 
73  // Parse the destination specifier
74  static boost::regex s_RegEx("^(.*?)@(.*?):(.*?)$");
75  boost::smatch l_Match;
76  if (boost::regex_match(l_VariablesMap["connect"].as<std::string>(), l_Match, s_RegEx)) {
77  // Resolve destination
78  boost::asio::ip::tcp::resolver l_Resolver(l_IoService);
79  auto l_EndpointIterator = l_Resolver.resolve({ l_Match[2], l_Match[3] });
80 
81  // Prepare the HDLCd client entity
83  l_HdlcdClient.SetOnClosedCallback([&l_IoService](){ l_IoService.stop(); });
84  l_HdlcdClient.SetOnDataCallback([](const HdlcdPacketData& a_PacketData){ PrintDissectedSnetPacket(a_PacketData); });
85  l_HdlcdClient.AsyncConnect(l_EndpointIterator, [&l_Signals](bool a_bSuccess) {
86  if (!a_bSuccess) {
87  std::cout << "Failed to connect to the HDLC Daemon!" << std::endl;
88  l_Signals.cancel();
89  } // if
90  }); // AsyncConnect
91 
92  // Start event processing
93  l_IoService.run();
94  } else {
95  throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
96  } // else
97  } catch (std::exception& a_Error) {
98  std::cerr << "Exception: " << a_Error.what() << "\n";
99  } // catch
100 
101  return 0;
102 }
#define HDLCD_DEVEL_VERSION_MINOR
Definition: HdlcdConfig.h:3
#define SNET_DEVEL_VERSION_MAJOR
Definition: SnetConfig.h:2
#define SNET_DEVEL_VERSION_MINOR
Definition: SnetConfig.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
void PrintDissectedSnetPacket(const HdlcdPacketData &a_PacketData)
#define HDLCD_DEVEL_VERSION_MAJOR
Definition: HdlcdConfig.h:2
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
int main(int argc, char *argv[])
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
Copyright (c) 2016, Florian Evers, florian-evers@gmx.de All rights reserved.
A set of tools to exchange and handle packets of s-net(r) devices via the HDLC Daemon. Copyright (C) 2016 Florian Evers, florian-evers@gmx.de.