HDLCd-Tools
main-hdlcd-suspender.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 "HdlcdPacketCtrl.h"
29 #include "HdlcdPacketCtrlPrinter.h"
30 
31 int main(int argc, char* argv[]) {
32  try {
33  // Declare the supported options.
34  boost::program_options::options_description l_Description("Allowed options");
35  l_Description.add_options()
36  ("help,h", "produce this help message")
37  ("version,v", "show version information")
38  ("connect,c", boost::program_options::value<std::string>(),
39  "connect to a single device via the HDLCd\n"
40  "syntax: SerialPort@IPAddess:PortNbr\n"
41  " linux: /dev/ttyUSB0@localhost:5001\n"
42  " windows: //./COM1@example.com:5001")
43  ;
44 
45  // Parse the command line
46  boost::program_options::variables_map l_VariablesMap;
47  boost::program_options::store(boost::program_options::parse_command_line(argc, argv, l_Description), l_VariablesMap);
48  boost::program_options::notify(l_VariablesMap);
49  if (l_VariablesMap.count("version")) {
50  std::cerr << "HDLCd port suspender version " << HDLCD_TOOLS_VERSION_MAJOR << "." << HDLCD_TOOLS_VERSION_MINOR
51  << " built with hdlcd-devel version " << HDLCD_DEVEL_VERSION_MAJOR << "." << HDLCD_DEVEL_VERSION_MINOR << std::endl;
52  } // if
53 
54  if (l_VariablesMap.count("help")) {
55  std::cout << l_Description << std::endl;
56  std::cout << "The port suspender for the HDLC Daemon is Copyright (C) 2016, and GNU GPL'd, by Florian Evers." << std::endl;
57  std::cout << "Bug reports, feedback, admiration, abuse, etc, to: https://github.com/Strunzdesign/hdlcd-tools" << std::endl;
58  return 1;
59  } // if
60 
61  if (!l_VariablesMap.count("connect")) {
62  std::cout << "hdlcd-suspender: you have to specify one device to connect to" << std::endl;
63  std::cout << "hdlcd-suspender: Use --help for more information." << std::endl;
64  return 1;
65  } // if
66 
67  // Install signal handlers
68  boost::asio::io_service l_IoService;
69  boost::asio::signal_set l_Signals(l_IoService);
70  l_Signals.add(SIGINT);
71  l_Signals.add(SIGTERM);
72  l_Signals.async_wait([&l_IoService](boost::system::error_code, int){ l_IoService.stop(); });
73 
74  // Parse the destination specifier
75  static boost::regex s_RegEx("^(.*?)@(.*?):(.*?)$");
76  boost::smatch l_Match;
77  if (boost::regex_match(l_VariablesMap["connect"].as<std::string>(), l_Match, s_RegEx)) {
78  // Resolve destination
79  boost::asio::ip::tcp::resolver l_Resolver(l_IoService);
80  auto l_EndpointIterator = l_Resolver.resolve({ l_Match[2], l_Match[3] });
81 
82  // Prepare the HDLCd client entity
83  HdlcdClient l_HdlcdClient(l_IoService, l_Match[1], HdlcdSessionDescriptor(SESSION_TYPE_TRX_STATUS, SESSION_FLAGS_NONE));
84  l_HdlcdClient.SetOnClosedCallback([&l_IoService](){ l_IoService.stop(); });
85  l_HdlcdClient.SetOnCtrlCallback([](const HdlcdPacketCtrl& a_PacketCtrl){ HdlcdPacketCtrlPrinter(a_PacketCtrl); });
86  l_HdlcdClient.AsyncConnect(l_EndpointIterator, [&l_HdlcdClient, &l_Signals](bool a_bSuccess) {
87  if (a_bSuccess) {
88  // Send port suspend request control packet
90  } else {
91  std::cout << "Failed to connect to the HDLC Daemon!" << std::endl;
92  l_Signals.cancel();
93  } // else
94  }); // AsyncConnect
95 
96  // Start event processing
97  l_IoService.run();
98  } else {
99  throw boost::program_options::validation_error(boost::program_options::validation_error::invalid_option_value);
100  } // else
101  } catch (std::exception& a_Error) {
102  std::cerr << "Exception: " << a_Error.what() << "\n";
103  } // catch
104 
105  return 0;
106 }
#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
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
Copyright (c) 2016, Florian Evers, florian-evers@gmx.de All rights reserved.
void SetOnCtrlCallback(std::function< void(const HdlcdPacketCtrl &a_PacketCtrl)> a_OnCtrlCallback)
Provide a callback method to be called for received control packets.
Definition: HdlcdClient.h:182
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
Additional tools to be used together with the HDLC Daemon. Copyright (C) 2016 Florian Evers...
static HdlcdPacketCtrl CreatePortStatusRequest(bool a_bLockSerialPort)
int main(int argc, char *argv[])
void HdlcdPacketCtrlPrinter(const HdlcdPacketCtrl &a_PacketCtrl)
Copyright (c) 2016, Florian Evers, florian-evers@gmx.de All rights reserved.