GM6000 Digital Heater Controller Branch: main
SDX-1330
Pipe.h
Go to the documentation of this file.
1#ifndef Driver_TPipe_Pipe_h_
2#define Driver_TPipe_Pipe_h_
3/*-----------------------------------------------------------------------------
4* This file is part of the Colony.Core Project. The Colony.Core Project is an
5* open source project with a BSD type of licensing agreement. See the license
6* agreement (license.txt) in the top/ directory or on the Internet at
7* http://integerfox.com/colony.core/license.txt
8*
9* Copyright (c) 2014-2022 John T. Taylor
10*
11* Redistributions of the source code must retain the above copyright notice.
12*----------------------------------------------------------------------------*/
13/** @file */
14
15#include "Cpl/Container/Map.h"
16#include "Driver/TPipe/Tx.h"
20#include "Cpl/System/Mutex.h"
21#include "Cpl/Io/Input.h"
22#include "Cpl/Io/Output.h"
23
24///
25namespace Driver {
26///
27namespace TPipe {
28
29/** This concrete class provides the implementation of TPipe.
30
31 The implementation is thread safe with respect to transmitting frames. In
32 addition, all transmitted frames are atomic with respect to each other.
33
34 Reception of frames is done a single thread, i.e. the callback to individual
35 Receive Frame Handler occur in the TPipe's thread. It is APPLICATION's
36 responsibility to provide thread safety for its Receive Frame Handlers.
37
38 The TPipe dynamically allocates memory for its incoming frame buffer on
39 start-up. The memory is freed when the TPipe is shutdown.
40
41 The TPipe also assumes that the runnable object for its thread is a
42 Cpl::System::PeridiocScheduler. The Periodic Scheduler semantics impose
43 the following constraints on the Application (who is responsible for creating
44 the Periodic Scheduler)
45
46 - The application MUST call the TPipe's start() method in the 'beginThreadProcessing'
47 function for the thread/PeriodicScheduler.
48
49 - The application MUST call the TPipe's stop() method in the 'endThreadProcessing'
50 function for the thread/PeriodicScheduler
51
52 - The application MUST call the TPipe's poll() method in the 'idleProcessing'
53 function for the thread/PeriodicScheduler
54
55 - The concrete Frame decoder class MUST use non-blocking semantics AND
56 the input stream MUST support the Cpl::Io::Input.available() method. See
57 Cpl::Text::Frame::StreamDecoder for more details.
58 */
59class Pipe : public Tx
60{
61public:
62 /** Constructor.
63
64 @param rxFrameHdlrs The set of received frame handlers. Note: frame
65 handler's self register
66 @param deframer Frame decoder used to identify individual command
67 strings within the raw Input stream. NOTE: The
68 decoder instance MUST use non-blocking semantics
69 @param framer Frame encoder used to encapsulate the output of
70 command in the Output stream.
71 @param rxFrameSize The size, in bytes, of the buffer used to hold
72 an incoming Frame. The behavior of what happens
73 if the incoming data exceeds the frame size is
74 defined by the 'deframer' (typically this is to
75 discard the in-progress frame and being looking
76 for a new SOF).
77 @param verbDelimiters The delimiter characters used to separate the
78 command verb from the rest of commands tokens/data.
79 This string must stay in scope for the life of the
80 Pipe instance.
81 */
85 size_t rxFrameSize,
86 const char* verbDelimiters=" "
87 );
88
89 /// Destructor
91
92public:
93 /** This method performs the in-thread initialization of the TPipe. It
94 MUST be called in the 'beginThreadProcessing' for the Periodic Scheduler
95 of which the TPipe executes in.
96 */
97 void start( Cpl::Io::Input& inStream, Cpl::Io::Output& outStream ) noexcept;
98
99 /** This method performs the in-thread shutdown of the TPipe. It
100 MUST be called in the 'endThreadProcessing' for the Periodic Scheduler
101 of which the TPipe executes in.
102 */
103 void stop() noexcept;
104
105 /** This method provides the TPipe CPU/Execution time. It MUST be called
106 in the 'idleFunction' for the Periodic Scheduler of which the TPipe
107 executes in.
108
109 Return false if a Stream IO error occurred; else true is returned
110 */
111 bool poll() noexcept;
112
113public:
114 /// See Driver::TPipe::Tx
115 bool sendCommand( const char* completeCommandText, size_t numBytes ) noexcept;
116
117 /// See Driver::TPipe::Tx
118 bool sendRawCommand( const char* completeCommandText, size_t numBytes ) noexcept;
119
120public:
121 /** This method returns the number of received frames that there was no
122 registered frame handler to process the incoming frame.
123
124 This method is thread safe.
125 */
126 size_t getUnknownFrameCount() noexcept;
127
128protected:
129 /// List of Frame handlers
131
132 /// Frame Decoder
133 Cpl::Text::Frame::StreamDecoder& m_deframer;
134
135 /// Frame Encoder
136 Cpl::Text::Frame::StreamEncoder& m_framer;
137
138 /// Frame buffer
140
141 /// Cache the handle to the output stream (for raw-commands)
142 Cpl::Io::Output* m_outfdPtr;
143
144 /// Lock for thread safety and atomic transmits
145 Cpl::System::Mutex m_lock;
146
147 /// Frame buffer size (not including the null terminator)
149
150 /// Delimiter(s) to find the end of the command verb
151 const char* m_verbDelimiters;
152
153 /// Track the number of unknown frames (i.e. frame received with no register frame handler)
155};
156
157}; // end namespaces
158};
159#endif // end header latch
This template class implements a THREAD SAFE Ring Buffer.
Definition RingBufferMT.h:33
This partially abstract class defines a interface for operating on an input stream (example of a stre...
Definition Input.h:37
This partially abstract class defines a interface for operating on an output stream (example of a str...
Definition Output.h:34
This partially concrete class defines an interface a Text "Decoder" that has a Cpl::Io::Input stream ...
Definition StreamDecoder.h:34
This concrete class implements the Encoder API where the Output destination is a Cpl::Io::Output stre...
Definition StreamEncoder.h:36
This concrete class provides the implementation of TPipe.
Definition Pipe.h:60
bool poll() noexcept
This method provides the TPipe CPU/Execution time.
char * m_frameBuffer
Frame buffer.
Definition Pipe.h:139
bool sendCommand(const char *completeCommandText, size_t numBytes) noexcept
See Driver::TPipe::Tx.
Cpl::Text::Frame::StreamEncoder & m_framer
Frame Encoder.
Definition Pipe.h:136
void stop() noexcept
This method performs the in-thread shutdown of the TPipe.
Cpl::System::Mutex m_lock
Lock for thread safety and atomic transmits.
Definition Pipe.h:145
size_t getUnknownFrameCount() noexcept
This method returns the number of received frames that there was no registered frame handler to proce...
void start(Cpl::Io::Input &inStream, Cpl::Io::Output &outStream) noexcept
This method performs the in-thread initialization of the TPipe.
Cpl::Container::Map< RxFrameHandlerApi > & m_rxHandlers
List of Frame handlers.
Definition Pipe.h:130
size_t m_unknownFrames
Track the number of unknown frames (i.e. frame received with no register frame handler)
Definition Pipe.h:154
Cpl::Text::Frame::StreamDecoder & m_deframer
Frame Decoder.
Definition Pipe.h:133
~Pipe()
Destructor.
bool sendRawCommand(const char *completeCommandText, size_t numBytes) noexcept
See Driver::TPipe::Tx.
Pipe(Cpl::Container::Map< RxFrameHandlerApi > &rxFrameHdlrs, Cpl::Text::Frame::StreamDecoder &deframer, Cpl::Text::Frame::StreamEncoder &framer, size_t rxFrameSize, const char *verbDelimiters=" ")
Constructor.
Cpl::Io::Output * m_outfdPtr
Cache the handle to the output stream (for raw-commands)
Definition Pipe.h:142
size_t m_frameBufSize
Frame buffer size (not including the null terminator)
Definition Pipe.h:148
const char * m_verbDelimiters
Delimiter(s) to find the end of the command verb.
Definition Pipe.h:151
This abstract class defines the 'Received Frame Handler' interface for the TPipe.
Definition RxFrameHandlerApi.h:29
This abstract class defines the 'Transmit Command' interface for the TPipe.
Definition Tx.h:27
The 'Cpl' namespace is the root name space for the Colony.
Definition Api16.h:20
namespace