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
41 public:
48 virtual ServiceBase* getService_byID(uint8_t ServiceID)=0;
49
56 virtual ServiceBase* getService_byPos(uint8_t ServicePosition)=0;
57
63 virtual uint8_t getNumberOfServices() const =0;
64
72 virtual errorCodes impartPdu(Frame* FrameToAdd)=0;
73
79};
80
81
88template<uint8_t number_of_services>
90 private:
91 ServiceBase* services[number_of_services];
92 public:
98 ServiceCluster(ServiceBase* serviceList[number_of_services]) {
99 for (int i = 0; i < number_of_services; ++i) {
100 services[i] = serviceList[i];
101 }
102 }
103
110 ServiceBase* getService_byID(uint8_t ServiceID) override {
111 for (int i = 0; i < number_of_services; ++i) {
112 if (services[i] && *services[i]->get_ServiceID() == ServiceID) {
113 return services[i];
114 }
115 }
116 return nullptr; // no Service with given ID
117 }
118
125 ServiceBase* getService_byPos(uint8_t ServicePosition) override {
126 if (services[ServicePosition]) {
127 return services[ServicePosition];
128 }
129 return nullptr; // no Service with given ID
130 }
131
137 uint8_t getNumberOfServices() const override {
138 return number_of_services;
139 }
140
148 errorCodes impartPdu(Frame* FrameToAdd) override {
149 char ServiceID = FrameToAdd->getServideId(); // Get the Service-ID of the given Frame
150 ServiceBase* destinationService = services->getService_byID(ServiceID); // Pointer to the destination-Service
151 if (!destinationService){
152 raiseError(serviceNotFound); // raise Service-not-found-error if no Service with this Service-ID exists
153 return getErrorState();
154 }
155 String pdu = *FrameToAdd->get_content(); // Get the Frames payload
156 if (!destinationService->impart_pdu(&pdu)){ // Add a Content-Object created from PDU to the Services receive-stack
157 raiseError(overflow); // raise Overflow-Error, if the Rec-Stack of the Service is full
158 };
159 return getErrorState();
160 };
161};
162
163#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
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: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 ~ServiceClusterBase()
Destroy the Service Cluster Base object.
Definition ServiceCluster.h:78
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:89
errorCodes impartPdu(Frame *FrameToAdd) override
Add the payload of the referenced Frame to the belonging Service (Multiplexing by Service-ID)
Definition ServiceCluster.h:148
ServiceCluster(ServiceBase *serviceList[number_of_services])
Construct a new Service Cluster object from associated services.
Definition ServiceCluster.h:98
uint8_t getNumberOfServices() const override
Get the total Number Of Services-objects associated to the cluster.
Definition ServiceCluster.h:137
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:125
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:110
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