-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathService.h
209 lines (188 loc) · 8.64 KB
/
Service.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @file Service.h
* @author Felix Schuelke (flxscode@gmail.com)
* @brief
* @version 0.1
* @date 2024-08-18
*
* @copyright * Copyright (C) 2024 Felix Schuelke
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#ifndef SERVICE_H
#define SERVICE_H
#ifdef ARDUINO
#include <Arduino.h> // include Arduino-Library for platformIO-build
#else
#include <mockArduino.h>
using namespace arduinoMocking;
#include <cstring>
#endif
#include "Content.h"
#include "Content_stack.h"
/**
* @author Felix Schuelke (flxscode@gmail.com)
* @brief Service-base-class to add class-functions to vtable
*
*/
class ServiceBase {
public:
/**
* @brief Get the get the response-payload, stored at the response_pdu of the service instance
*
* @return String response-pdu as String
*/
virtual String get_response()=0;
/**
* @brief Check, if a response of the service is available (services sendStack not empty)
*
* @return true a response-payload is available
* @return false no response-payload is available
*/
virtual bool responseAvailable()=0;
/**
* @brief Clear the response-buffer (delete item, that was returned by get_response from the service-sendStack)
*
*/
virtual void clearResponse()=0;
/**
* @brief Execute the service's functions for each item on the receive-stack and add all response-payloads to be send to the send-stack
*/
virtual void stackProcessing()=0;
/**
* @brief Add a new Content-Object created from a received payload to the services receive-Stack.
* The payload is the representation of the Content-Object, during the construction, the Content-object creates it's content from the representation.
* The Content-object is added to the rec_stack.
*
* @param pdu payload from received Frame-object as pointer to string
* @return true content-object added to service-rec_stack
* @return false rec_stack is full, content-object not added
*/
virtual bool impart_pdu(String* pdu)=0;
/**
* @brief Get pointer to the 1-byte ServiceID of the Service (unique for each Service-template derived Class)
*
* @return uint8_t* ServiceID of the service
*/
virtual uint8_t* get_ServiceID()=0;
/**
* @brief Get pointer to the 1-byte InstanceID of the Service-instance (unique for each Instance of Service within the whole communication-network)
*
* @return uint8_t* InstanceID of the Service-instance
*/
virtual uint8_t* get_InstanceID()=0;
/**
* @brief Destroy the Service Base object
*
*/
virtual ~ServiceBase(){};
};
/**
*
* @author Felix Schuelke (flxscode@gmail.com)
* @brief Service-Template to derive a Service class by defining the Content (derived Class of "Content") to handle and the size of the stacks (send and receive).
* A Service has to be instantiated with a unique service-ID and an instance-ID, the service-instance should use for it's communication.
* E.g.: Messenger-service with service-id "m" (specified in derived class, same on every host) is instantiated with a host-specific ID to identify the instances.
* The derived classes have to define the stack-processing to handle the payload at the rec-stack and add payload to the send-stack. Each derived class has a unique serviceID
* to identify the Service-type and each instance in the communication-network has a unique instanceID.
*
* @tparam content_class type of the items stored in the stack
* @tparam stackSize type of the items stored in the stack
*/
template<typename content_class, int stackSize>
class Service: public ServiceBase{
protected:
uint8_t serviceID; // service-id
uint8_t instanceID; // service-instance-id
Content_stack<content_class, stackSize> rec_stack; // stack for received content-elements
Content_stack<content_class, stackSize> send_stack; // stack for received content-elements
String response_pdu; // PDU with response
// write the response-PDU to the reserved memory of the service-instance
void write_response_pdu(content_class response_object){
response_pdu = *response_object.get_representation();
}
public:
Service(uint8_t serviceID, uint8_t instanceID): serviceID(serviceID), instanceID(instanceID){}
/**
* @brief Get pointer to the 1-byte ServiceID of the Service (unique for each Service-template derived Class)
*
* @return uint8_t* ServiceID of the service
*/
uint8_t* get_ServiceID() override {
return &serviceID;
}
/**
* @brief Get pointer to the 1-byte InstanceID of the Service-instance (unique for each Instance of Service within the whole communication-network)
*
* @return uint8_t* InstanceID of the Service-instance
*/
uint8_t* get_InstanceID() override {
return &instanceID;
}
/**
* @brief Add a new Content-Object created from a received payload to the services receive-Stack.
* The payload is the representation of the Content-Object, during the construction, the Content-object creates it's content from the representation.
* The Content-object is added to the rec_stack.
*
* @param pdu payload from received Frame-object as pointer to string
* @return true content-object added to service-rec_stack
* @return false rec_stack is full, content-object not added
*/
virtual bool impart_pdu(String* pdu) override {
content_class content(pdu); // create Content-object from PDU
return rec_stack.addElement(content); // add Content-Object to Rec-Stack
};
/**
* @brief Get the get the response-payload, stored at the response_pdu of the service instance
*
* @return String response-pdu as String
*/
virtual String get_response() override {
// copy the response element from send-stack
content_class response_element = (send_stack.empty())? content_class():*send_stack.getElement();
// write pdu from this element to response-pdu-memory of service-instance
write_response_pdu(response_element);
// return the response-pdu of the service-instance
return response_pdu;
};
/**
* @brief Check, if a response of the service is available (services sendStack not empty)
*
* @return true a response-payload is available
* @return false no response-payload is available
*/
virtual bool responseAvailable() override {
return !send_stack.empty();
};
/**
* @brief Clear the response-buffer (delete item, that was returned by get_response from the service-sendStack)
*
*/
virtual void clearResponse() override {
send_stack.deleteElement(); // delete last element from sendstack
response_pdu = "";
};
/**
* @brief Execute the service's functions for each item on the receive-stack and add all response-payloads to be send to the send-stack
*/
virtual void stackProcessing() override {
if (rec_stack.empty()) return;
while(!rec_stack.empty()){
content_class recItem = *rec_stack.getElement(); // example implementation: adding received items back to send-stack
send_stack.addElement(recItem);
rec_stack.deleteElement();
}
}
};
#endif // SERVICE_H