31 std::shared_ptr<GatewayClientHandlerCollection> a_GatewayClientHandlerCollection,
const std::string& a_RemoteAddress,
32 uint16_t a_TcpPortNbr, uint16_t a_SerialPortNbr): m_IOService(a_IOService), m_ConfigServerHandlerCollection(a_ConfigServerHandlerCollection),
33 m_GatewayClientHandlerCollection(a_GatewayClientHandlerCollection), m_HdlcdClientConnectGuard(a_ConfigServerHandlerCollection, a_SerialPortNbr),
34 m_RemoteAddress(a_RemoteAddress), m_TcpPortNbr(a_TcpPortNbr), m_SerialPortNbr(a_SerialPortNbr), m_Resolver(a_IOService), m_ConnectionRetryTimer(a_IOService) {
36 assert(m_ConfigServerHandlerCollection);
37 assert(m_GatewayClientHandlerCollection);
40 m_bSuspendSerialPort =
false;
46 m_ConfigServerHandlerCollection.reset();
47 m_GatewayClientHandlerCollection.reset();
52 m_bSuspendSerialPort =
true;
61 m_bSuspendSerialPort =
false;
70 if ((m_HdlcdClient) && (!m_bSuspendSerialPort)) {
78 void HdlcdClientHandler::ResolveDestination() {
79 std::stringstream l_OStream;
80 l_OStream << m_TcpPortNbr;
81 m_Resolver.async_resolve({m_RemoteAddress, l_OStream.str()}, [
this](
const boost::system::error_code& a_ErrorCode, boost::asio::ip::tcp::resolver::iterator a_EndpointIterator) {
83 std::cout <<
"Failed to resolve host name: " << m_RemoteAddress << std::endl;
84 m_ConnectionRetryTimer.expires_from_now(boost::posix_time::seconds(2));
85 m_ConnectionRetryTimer.async_wait([
this](
const boost::system::error_code& a_ErrorCode) {
93 std::stringstream l_OStream;
94 l_OStream <<
"/dev/ttyUSB" << m_SerialPortNbr;
96 m_HdlcdClient->SetOnClosedCallback([
this]() {
98 m_ConnectionRetryTimer.expires_from_now(boost::posix_time::seconds(2));
99 m_ConnectionRetryTimer.async_wait([
this](
const boost::system::error_code& a_ErrorCode) {
102 ResolveDestination();
107 m_HdlcdClient->SetOnDataCallback([
this](
const HdlcdPacketData& a_PacketData) {
109 m_GatewayClientHandlerCollection->SendPacket(m_SerialPortNbr, a_PacketData.
GetData());
112 m_HdlcdClient->SetOnCtrlCallback([
this](
const HdlcdPacketCtrl& a_PacketCtrl) {
121 m_HdlcdClient->AsyncConnect(a_EndpointIterator, [
this](
bool a_bSuccess) {
123 if (m_bSuspendSerialPort) {
130 m_ConnectionRetryTimer.expires_from_now(boost::posix_time::seconds(2));
131 m_ConnectionRetryTimer.async_wait([
this](
const boost::system::error_code& a_ErrorCode) {
134 ResolveDestination();
static HdlcdPacketData CreatePacket(const std::vector< unsigned char > a_Payload, bool a_bReliable, bool a_bInvalid=false, bool a_bWasSent=false)
Deliver data packets sent by the device and received by the HDLCd.
Payload, data read and write, port status read and write.
const std::vector< unsigned char > & GetData() const
static HdlcdPacketCtrl CreatePortStatusRequest(bool a_bLockSerialPort)
HdlcdClientHandler(boost::asio::io_service &a_IOService, std::shared_ptr< ConfigServerHandlerCollection > a_ConfigServerHandlerCollection, std::shared_ptr< GatewayClientHandlerCollection > a_GatewayClientHandlerCollection, const std::string &a_RemoteAddress, uint16_t a_TcpPortNbr, uint16_t a_SerialPortNbr)
This file contains the header declaration of class HdlcdClient.
E_CTRL_TYPE GetPacketType() const
bool GetIsLockedBySelf() const
void SendPacket(const std::vector< unsigned char > &a_Payload)
bool GetIsLockedByOthers() const
Class HdlcdSessionDescriptor.