BusBricks: /Users/felixschulke/Developement/Arduino/Modbus_RTU/Modbus_RTU/Modbus-RTU/lib/Service/Service.h Source File
BusBricks  0.1
Customize bus-communication
Loading...
Searching...
No Matches
Service.h
Go to the documentation of this file.
1
24#ifndef SERVICE_H
25#define SERVICE_H
26#ifdef ARDUINO
27 #include <Arduino.h> // include Arduino-Library for platformIO-build
28#else
29 #include <mockArduino.h>
30 using namespace arduinoMocking;
31 #include <cstring>
32#endif
33
34#include "Content.h"
35#include "Content_stack.h"
36
43public:
49 virtual String get_response()=0;
50
57 virtual bool responseAvailable()=0;
58
63 virtual void clearResponse()=0;
64
68 virtual void stackProcessing()=0;
69
79 virtual bool impart_pdu(String* pdu)=0;
80
86 virtual uint8_t* get_ServiceID()=0;
87
93 virtual uint8_t* get_InstanceID()=0;
94
99 virtual ~ServiceBase(){};
100};
101
114template<typename content_class, int stackSize>
115class Service: public ServiceBase{
116protected:
117 uint8_t serviceID; // service-id
118 uint8_t instanceID; // service-instance-id
119 Content_stack<content_class, stackSize> rec_stack; // stack for received content-elements
120 Content_stack<content_class, stackSize> send_stack; // stack for received content-elements
121 String response_pdu; // PDU with response
122
123 // write the response-PDU to the reserved memory of the service-instance
124 void write_response_pdu(content_class response_object){
125 response_pdu = *response_object.get_representation();
126 }
127
128public:
129 Service(uint8_t serviceID, uint8_t instanceID): serviceID(serviceID), instanceID(instanceID){}
130
136 uint8_t* get_ServiceID() override {
137 return &serviceID;
138 }
139
145 uint8_t* get_InstanceID() override {
146 return &instanceID;
147 }
148
158 virtual bool impart_pdu(String* pdu) override {
159 content_class content(pdu); // create Content-object from PDU
160 return rec_stack.addElement(content); // add Content-Object to Rec-Stack
161 };
162
168 virtual String get_response() override {
169 // copy the response element from send-stack
170 content_class response_element = (send_stack.empty())? content_class():*send_stack.getElement();
171 // write pdu from this element to response-pdu-memory of service-instance
172 write_response_pdu(response_element);
173 // return the response-pdu of the service-instance
174 return response_pdu;
175 };
176
183 virtual bool responseAvailable() override {
184 return !send_stack.empty();
185 };
186
191 virtual void clearResponse() override {
192 send_stack.deleteElement(); // delete last element from sendstack
193 response_pdu = "";
194 };
195
199 virtual void stackProcessing() override {
200 if (rec_stack.empty()) return;
201 while(!rec_stack.empty()){
202 content_class recItem = *rec_stack.getElement(); // example implementation: adding received items back to send-stack
203 send_stack.addElement(recItem);
204 rec_stack.deleteElement();
205 }
206 }
207};
208
209#endif // SERVICE_H
Content-Stack-Template the content-stack stores the added items (call-by-value / copy) on internal ar...
Definition Content_stack.h:45
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
Service-base-class to add class-functions to vtable.
Definition Service.h:42
virtual uint8_t * get_ServiceID()=0
Get pointer to the 1-byte ServiceID of the Service (unique for each Service-template derived Class)
virtual uint8_t * get_InstanceID()=0
Get pointer to the 1-byte InstanceID of the Service-instance (unique for each Instance of Service wit...
virtual bool impart_pdu(String *pdu)=0
Add a new Content-Object created from a received payload to the services receive-Stack....
virtual void stackProcessing()=0
Execute the service's functions for each item on the receive-stack and add all response-payloads to b...
virtual ~ServiceBase()
Destroy the Service Base object.
Definition Service.h:99
virtual String get_response()=0
Get the get the response-payload, stored at the response_pdu of the service instance
virtual bool responseAvailable()=0
Check, if a response of the service is available (services sendStack not empty)
virtual void clearResponse()=0
Clear the response-buffer (delete item, that was returned by get_response from the service-sendStack)
Service-Template to derive a Service class by defining the Content (derived Class of "Content") to ha...
Definition Service.h:115
virtual void stackProcessing() override
Execute the service's functions for each item on the receive-stack and add all response-payloads to b...
Definition Service.h:199
uint8_t * get_ServiceID() override
Get pointer to the 1-byte ServiceID of the Service (unique for each Service-template derived Class)
Definition Service.h:136
virtual String get_response() override
Get the get the response-payload, stored at the response_pdu of the service instance
Definition Service.h:168
virtual void clearResponse() override
Clear the response-buffer (delete item, that was returned by get_response from the service-sendStack)
Definition Service.h:191
uint8_t * get_InstanceID() override
Get pointer to the 1-byte InstanceID of the Service-instance (unique for each Instance of Service wit...
Definition Service.h:145
virtual bool impart_pdu(String *pdu) override
Add a new Content-Object created from a received payload to the services receive-Stack....
Definition Service.h:158
virtual bool responseAvailable() override
Check, if a response of the service is available (services sendStack not empty)
Definition Service.h:183
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