HDLC-Daemon
SerialPortHandler.h
Go to the documentation of this file.
1 
22 #ifndef SERIAL_PORT_HANDLER_H
23 #define SERIAL_PORT_HANDLER_H
24 
25 #include <memory>
26 #include <string>
27 #include <vector>
28 #include <list>
29 #include <boost/asio.hpp>
30 #include "ISerialPortHandler.h"
31 #include "SerialPortLock.h"
32 #include "BaudRate.h"
34 class HdlcdServerHandler;
35 class ProtocolState;
36 
37 class SerialPortHandler: public ISerialPortHandler, public std::enable_shared_from_this<SerialPortHandler> {
38 public:
39  // CTOR and DTOR
40  SerialPortHandler(const std::string &a_SerialPortName, std::shared_ptr<SerialPortHandlerCollection> a_SerialPortHandlerCollection, boost::asio::io_service& a_IOService);
42 
43  void AddHdlcdServerHandler(std::shared_ptr<HdlcdServerHandler> a_HdlcdServerHandler);
44  void DeliverPayloadToHDLC(const std::vector<unsigned char> &a_Payload, bool a_bReliable);
45 
46  bool Start();
47  void Stop();
48 
49  // Suspend / resume serial port
50  void SuspendSerialPort();
51  void ResumeSerialPort();
52 
54 
55 private:
56  // Called by a ProtocolState object
57  bool RequiresBufferType(E_BUFFER_TYPE a_eBufferType) const;
58  void DeliverBufferToClients(E_BUFFER_TYPE a_eBufferType, const std::vector<unsigned char> &a_Payload, bool a_bReliable, bool a_bInvalid, bool a_bWasSent);
59  bool OpenSerialPort();
60  void ChangeBaudRate();
61  void TransmitHDLCFrame(const std::vector<unsigned char> &a_Payload);
62  void QueryForPayload(bool a_bQueryReliable, bool a_bQueryUnreliable);
63 
64  // Internal helpers
65  void DoRead();
66  void DoWrite();
67  void ForEachHdlcdServerHandler(std::function<void(std::shared_ptr<HdlcdServerHandler>)> a_Function);
68 
69  // Members
70  bool m_Registered;
71  boost::asio::serial_port m_SerialPort;
72  boost::asio::io_service &m_IOService;
73  std::shared_ptr<ProtocolState> m_ProtocolState;
74  std::string m_SerialPortName;
75  std::weak_ptr<SerialPortHandlerCollection> m_SerialPortHandlerCollection;
76  std::list<std::weak_ptr<HdlcdServerHandler>> m_HdlcdServerHandlerList;
77  enum { max_length = 1024 };
78  unsigned char m_ReadBuffer[max_length];
79 
80  std::vector<unsigned char> m_SendBuffer;
81  size_t m_SendBufferOffset;
82  SerialPortLock m_SerialPortLock;
83  BaudRate m_BaudRate;
84 
85  // Track all subscribed clients
86  size_t m_BufferTypeSubscribers[BUFFER_TYPE_ARITHMETIC_ENDMARKER];
87 };
88 
89 #endif // SERIAL_PORT_HANDLER_H
This file contains the header declaration of class BaudRate.
SerialPortHandler(const std::string &a_SerialPortName, std::shared_ptr< SerialPortHandlerCollection > a_SerialPortHandlerCollection, boost::asio::io_service &a_IOService)
void DeliverPayloadToHDLC(const std::vector< unsigned char > &a_Payload, bool a_bReliable)
The HDLC Deamon implements the HDLC protocol to easily talk to devices connected via serial communica...
void AddHdlcdServerHandler(std::shared_ptr< HdlcdServerHandler > a_HdlcdServerHandler)
E_BUFFER_TYPE
Definition: BufferType.h:40
Class BaudRate.
Definition: BaudRate.h:32
Copyright (c) 2016, Florian Evers, florian-evers@gmx.de All rights reserved.