BusBricks: /Users/felixschulke/Developement/Arduino/Modbus_RTU/Modbus_RTU/Modbus-RTU/lib/Service/ServiceCluster.h Source File
BusBricks  0.1
Customize bus-communication
Loading...
Searching...
No Matches
ServiceCluster.h
Go to the documentation of this file.
1
25#ifndef SERVICECLUSTER_H
26#define SERVICECLUSTER_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 <Service.h>
35#include <ErrorState.h>
36#include <Frame.h>
37
43 public:
50 virtual ServiceBase* getService_byID(uint8_t ServiceID)=0;
51
58 virtual ServiceBase* getService_byPos(uint8_t ServicePosition)=0;
59
65 virtual uint8_t getNumberOfServices() const =0;
66
74 virtual errorCodes impartPdu(Frame* FrameToAdd)=0;
75
81};
82
83
90template<uint8_t number_of_services>
92 private:
93 ServiceBase* services[number_of_services];
94 public:
100 ServiceCluster(ServiceBase* serviceList[number_of_services]) {
101 for (int i = 0; i < number_of_services; ++i) {
102 services[i] = serviceList[i];
103 }
104 }
105
112 ServiceBase* getService_byID(uint8_t ServiceID) override {
113 for (int i = 0; i < number_of_services; ++i) {
114 if (services[i] && *services[i]->get_ServiceID() == ServiceID) {
115 return services[i];
116 }
117 }
118 return nullptr; // no Service with given ID
119 }
120
127 ServiceBase* getService_byPos(uint8_t ServicePosition) override {
128 if (services[ServicePosition]) {
129 return services[ServicePosition];
130 }
131 return nullptr; // no Service with given ID
132 }
133
139 uint8_t getNumberOfServices() const override {
140 return number_of_services;
141 }
142
150 errorCodes impartPdu(Frame* FrameToAdd) override {
151 char ServiceID = FrameToAdd->getServiceId(); // Get the Service-ID of the given Frame
152 ServiceBase* destinationService = getService_byID(ServiceID); // Pointer to the destination-Service
153 if (!destinationService){
154 raiseError(serviceNotFound); // raise Service-not-found-error if no Service with this Service-ID exists
155 return getErrorState();
156 }
157 String pdu = *FrameToAdd->get_content(); // Get the Frames payload
158 if (!destinationService->impart_pdu(&pdu)){ // Add a Content-Object created from PDU to the Services receive-stack
159 raiseError(overflow); // raise Overflow-Error, if the Rec-Stack of the Service is full
160 };
161 return getErrorState();
162 };
163};
164
165#endif // SERVICECLUSTER_H
errorCodes
Enumeration for various error codes.
Definition Error.h:47
@ overflow
Send- or Receivestack reached max. size of items.
Definition Error.h:84
@ serviceNotFound
The Service-Id addressed by the frame was not found in the service-cluster.
Definition Error.h:72
content_type * get_content()
Get the address of the informations content of type content_type.
Definition Content.h:75
A class to manage and track error states using error codes.
Definition ErrorState.h:49
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
virtual uint8_t getServiceId()=0
Get the ServiceId, the PDU of the Frame belongs to (necessary for Service-multiplexing)....
Service-base-class to add class-functions to vtable.
Definition Service.h:42
virtual bool impart_pdu(String *pdu)=0
Add a new Content-Object created from a received payload to the services receive-Stack....
ServiceCluster-base-class to add class-functions to vtable.
Definition ServiceCluster.h:42
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 ~ServiceClusterBase()
Destroy the Service Cluster Base object.
Definition ServiceCluster.h:80
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...
The ServiceCluster provides functions to manage multiple services. The ServiceCluster is added to the...
Definition ServiceCluster.h:91
errorCodes impartPdu(Frame *FrameToAdd) override
Add the payload of the referenced Frame to the belonging Service (Multiplexing by Service-ID)
Definition ServiceCluster.h:150
ServiceCluster(ServiceBase *serviceList[number_of_services])
Construct a new Service Cluster object from associated services.
Definition ServiceCluster.h:100
uint8_t getNumberOfServices() const override
Get the total Number Of Services-objects associated to the cluster.
Definition ServiceCluster.h:139
ServiceBase * getService_byPos(uint8_t ServicePosition) override
Get pointer to the service at a specific position in the cluster (positions starting by zero)
Definition ServiceCluster.h:127
ServiceBase * getService_byID(uint8_t ServiceID) override
Get pointer to the service with the given service-ID by iterating through the associated services and...
Definition ServiceCluster.h:112
Provides mock implementations of Arduino framework functions and classes for native builds.
Definition mockArduino.cpp:28
std::string String
Alias for std::string to simulate Arduino's String type.
Definition mockArduino.h:51