BusBricks: /Users/felixschulke/Developement/Arduino/Modbus_RTU/Modbus_RTU/Modbus-RTU/lib/Interface/ServiceInterface.h Source File
BusBricks  0.1
Customize bus-communication
Loading...
Searching...
No Matches
ServiceInterface.h
Go to the documentation of this file.
1
25#ifndef SERVICEINTERFACE_H
26#define SERVICEINTERFACE_H
27#ifdef ARDUINO
28 #include <Arduino.h> // include Arduino-Library for platformIO-build
29#else
30 #include <mockArduino.h>
31 using namespace arduinoMocking;
32#endif
33
34#include <CommInterface.h>
35#include <ServiceCluster.h>
36#include <Frame.h>
37#include <Content_stack.h>
38#include <ErrorService.h>
39#include <ErrorState.h>
40
41#define STACKSIZE 2 // Size of Send- / Rec-Stack
42#define ERRORSERVICE_ID 'e' // Service-ID of the configured Error-Service in Servicecluster
43//#define DEBUG
44
45
55template<typename CommInterfaceBase, typename frameType>
57 public:
68
76 virtual void communicate() = 0;
77
78 protected:
81
84
87
90
93
96
103 virtual void getPDU_from_services()=0;
104
111 virtual void addPDU_to_services()=0;
112
113
118 virtual void processServices(){
119 for (size_t i = 0; i < services->getNumberOfServices(); i++)
120 {
121 ServiceBase* destinationService = services->getService_byPos(i); // Pointer to the destination-Service
122 destinationService->stackProcessing();
123 }
124 }
125
134 ServiceBase* service = services->getService_byID(ERRORSERVICE_ID);
135 if (service) {
136 ErrorService* errService = static_cast<ErrorService*>(service);
137 errService->raiseError(code);
138 }else{
140 }
141 };
142
153 virtual void processSendStack(){
154 // Serial debugging
155 #ifdef DEBUG
156 Serial.println("Processing Send-Stack...");
157 #endif
158 // Handle sendstack
159 while (!sendStack.empty())
160 {
162 Frame* frameToSend = sendStack.getElement(); // Get next item from the send-stack
163 sendItem = *frameToSend->get_representation(); // buffer Frame as char-array
164 comm_interface->sendNewFrame(&sendItem); // Impart Frame that has to be sent next to the CommInterface
165 sendStack.deleteElement(); // delete Item from send-stack
166 comm_interface->sendCycle(); // execute sending
167 }else{
168 comm_interface->sendCycle(); // execute sending
169 };
170
171 // check the commInterfaces ErrorState
172 errorCodes commInterfaceErrorState = comm_interface->getErrorState();
173 if (commInterfaceErrorState!=noError) raiseError(commInterfaceErrorState);
175 };
176 }
177
193 virtual void addPDU_to_services()
194 {
195 // abort, if no new PDU available
196 if (recStack.empty()) return;
197 // Add all received PDUs to the Services
198 while (!recStack.empty())
199 {
200 // Try to add Frame from rec-stack to Service
201 Frame* receivedFrame = recStack.getElement();
202 errorCodes ServicesErrorState = services->impartPdu(receivedFrame);
203
204 // handle the Service-Clusters error-state
205 if (ServicesErrorState != noError) raiseError(ServicesErrorState);
206
207 // clear Error-state after handling error
209
210 // Skip Discard and leave the rec-stack-processing
211 if (ServicesErrorState = overflow) break;
212
213 // discard frame
215 };
216 };
217
218
232 virtual void processRecStack() {
233 // Serial debugging
234 #ifdef DEBUG
235 Serial.println("Processing Receive-Stack...");
236 #endif
237 // Handle receivebuffer
239 if (recItem.getSize() != 0){ // Item not empty
240 frameType recItemFrame(&recItem); // Construct Frame-Class derived Object (nullptr if failed)
241 if (recItemFrame.isValid()){
242 recStack.addElement(recItemFrame); // Add the received element to the stack
243 }else{
244 raiseError(framingError);} // frame-construction failed
245 recItem = CharArray(); // Clear rec-Item
246 }
247
248 // check the commInterfaces ErrorState
249 errorCodes commInterfaceErrorState = comm_interface->getErrorState();
250 if (commInterfaceErrorState!=noError) raiseError(commInterfaceErrorState);
252
253 comm_interface->getReceivedFrame(&recItem); // Impart memory the received item has to be stored at
254 comm_interface->receiveCycle(); // Receive new frames from comm-interface
255 }
256 comm_interface->receiveCycle(); // Receive new frames from comm-interface
257
258 // check the commInterfaces ErrorState
259 errorCodes commInterfaceErrorState = comm_interface->getErrorState();
260 if (commInterfaceErrorState!=noError) raiseError(commInterfaceErrorState);
262 }
263};
264#endif // SERVICEINTERFACE_H
errorCodes
Enumeration for various error codes.
Definition Error.h:47
@ framingError
Structure of the frame is not as expected by the services content-representation-definition.
Definition Error.h:56
@ overflow
Send- or Receivestack reached max. size of items.
Definition Error.h:84
@ noError
no Error
Definition Error.h:51
Class for storing char-array (byte-array) together with size. The Array is stored on Heap-memory and ...
Definition CharArray.h:38
size_t getSize() const
Retrieves the size of the char array.
Definition CharArray.cpp:213
Communcation-Interface-Base-Class of the CommInterface template specifies a standardized interface to...
Definition CommInterface.h:54
virtual bool receivedNewFrame()
Check, if a new Frame was received.
Definition CommInterface.h:90
virtual void getReceivedFrame(CharArray *destFrameBuffer)
Define the destination, the next received Frame should be copied to ba a pointer to an empty String-O...
Definition CommInterface.h:82
virtual bool finishedSending()
Check, if the Frame was sent and the CommInterface is ready to send the next Frame.
Definition CommInterface.h:75
virtual void sendNewFrame(CharArray *sendFrame)
Add a new Frame to the send-buffer.
Definition CommInterface.h:67
Content-Stack-Template the content-stack stores the added items (call-by-value / copy) on internal ar...
Definition Content_stack.h:45
bool full()
Check if the Stack is full (size reached MaxSize)
Definition Content_stack.h:134
bool addElement(content_class element)
Add item to stack.
Definition Content_stack.h:72
bool empty()
Check if the Stack is empty (size is 0)
Definition Content_stack.h:123
content_class * getElement(int index=0)
Get Pointer to element in the stack. Accepts positive and negative indexing.
Definition Content_stack.h:107
bool deleteElement(int index=0)
Delete item on index-position from stack and shift all items with higher index, accepts only positive...
Definition Content_stack.h:87
representation_type * get_representation()
Get the address of the informations representation of type representation-type.
Definition Content.h:66
Service class for handling Errors.
Definition ErrorService.h:69
void raiseError(errorCodes code)
Adds an Error-object for the local Instance-Id to the Error-services rec-stack.
Definition ErrorService.cpp:80
A class to manage and track error states using error codes.
Definition ErrorState.h:49
void clearErrorState()
sets the current error-state to "no Error"
Definition ErrorState.cpp:43
errorCodes getErrorState()
Get the currently active error-code.
Definition ErrorState.cpp:38
void raiseError(errorCodes code)
raises a new error by setting the given error-code as errorState
Definition ErrorState.cpp:33
Frame-Class as derived class from Content The derived classes define: -the conversion from a given pa...
Definition Frame.h:50
Service-base-class to add class-functions to vtable.
Definition Service.h:42
virtual void stackProcessing()=0
Execute the service's functions for each item on the receive-stack and add all response-payloads to b...
ServiceCluster-base-class to add class-functions to vtable.
Definition ServiceCluster.h:40
virtual ServiceBase * getService_byPos(uint8_t ServicePosition)=0
Get pointer to the service at a specific position in the cluster (positions starting by zero)
virtual errorCodes impartPdu(Frame *FrameToAdd)=0
Add the payload of the referenced Frame to the belonging Service.
virtual uint8_t getNumberOfServices() const =0
Get the total Number Of Services-objects associated to the cluster.
virtual ServiceBase * getService_byID(uint8_t ServiceID)=0
Get pointer to the service with the given service-ID by iterating through the associated services and...
Template for generic Service-Interface Defines the handling of incoming frames from CommInterface to ...
Definition ServiceInterface.h:56
virtual void processSendStack()
Send all Frames, stored in the sendStack. As long as the sendStack is not empty, the function is gett...
Definition ServiceInterface.h:153
Content_stack< frameType, STACKSIZE > sendStack
stack for frames to send next (instance of Content_stack-derived class, specified for type of frames,...
Definition ServiceInterface.h:89
void raiseError(errorCodes code)
Check if an ErrorService is registered in the service cluster under the defined ERRORSERVICE_ID of th...
Definition ServiceInterface.h:133
virtual void addPDU_to_services()
Add PDUs from all received frames to the corresponding services.
Definition ServiceInterface.h:193
virtual void processServices()
start the stackProcessing of the registered services
Definition ServiceInterface.h:118
CharArray recItem
Item received last
Definition ServiceInterface.h:95
Content_stack< frameType, STACKSIZE > recStack
stack for received frames (instance of Content_stack-derived class, specified for type of frames,...
Definition ServiceInterface.h:86
virtual void processRecStack()
Add items received by the CommInterface to the recStack and execute the CommInterface's receiveCycle ...
Definition ServiceInterface.h:232
CommInterfaceBase * comm_interface
pointer to Communication-Interface (instance of CommInterface-derived class)
Definition ServiceInterface.h:80
virtual void getPDU_from_services()=0
Add all PDUs provided by the services to the sendstack, depending on how frametype stores the informa...
virtual void addPDU_to_services()=0
Impart all received PDUs from the receivestack to the associated services, depending on how frametype...
ServiceInterface(CommInterfaceBase *comm_interface, ServiceClusterBase *services)
Construct a new Service Interface object.
Definition ServiceInterface.h:64
ServiceClusterBase * services
pointer to ServiceCluster, containing the Services associated with the Interface (instance of Service...
Definition ServiceInterface.h:83
CharArray sendItem
Item to be sent next
Definition ServiceInterface.h:92
virtual void communicate()=0
Execute all relevant tasks for transferring data between CommInterface and Services: Getting payloads...
void println(const std::string &str)
Prints a string followed by a newline to the serial output.
Definition mockArduino.cpp:111
Provides mock implementations of Arduino framework functions and classes for native builds.
Definition mockArduino.cpp:28
MockSerial Serial
Global instance of MockSerial to mimic Arduino's Serial object.
Definition mockArduino.cpp:185