-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageService.h
136 lines (124 loc) · 5.19 KB
/
MessageService.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
/**
* @file MessageService.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 MESSAGE_SERVICE_H
#define MESSAGE_SERVICE_H
#ifdef ARDUINO
#include <Arduino.h> // include Arduino-Library for platformIO-build
#else
#include <mockArduino.h>
#include <cstring>
using namespace arduinoMocking;
#endif
#include "Service.h"
#include "Message.h"
/// @brief Number of elements the services stacks can store
#define STACKSIZE 2
/// @brief Service-id ASCII: "m"
#define FUNCTIONCODE 0x6D
/**
* @class MessageService
* @brief Service class for handling messages and acknowledgments.
*
* The `MessageService` class manages messages of type `Message`, processes incoming messages from the receive stack, and handles sending acknowledgments.
* It inherits from a Service for conetnt_type 'Message' (`Service<Message, STACKSIZE>`).
*
* @section Constructors
* - `MessageService(uint8_t instance_id)`: Constructs a `MessageService` with a default service ID of "m".
* - `MessageService(uint8_t instance_id, uint8_t service_id)`: Constructs a `MessageService` with a custom service ID.
*
* @section Methods
* - `void stackProcessing() override`: Processes all messages from the receive stack until it is empty. Adds acknowledgments to the send stack and prints received messages.
* - `uint8_t get_destinationId()`: Retrieves the destination instance ID for the current response PDU.
* - `void sendMessage(char receiverId, String messagetext)`: Sends a new message with the specified receiver ID and message text.
*
* @private
* - `void sendAck(Message* message)`: Adds an acknowledgment for the given message to the send stack.
* - `void printMessage(Message* message)`: Prints the content of the received message.
*
* @note
* - The class handles both the processing of incoming messages and the sending of acknowledgments.
* - The `stackProcessing` method is responsible for managing the receive and send stacks, ensuring messages are properly handled and acknowledged.
*/
class MessageService: public Service<Message, STACKSIZE>
{
public:
/**
* @brief Constructor for `MessageService` with default Service ID "m".
*
* Initializes the service with the given instance ID and a default service ID.
*
* @param instance_id The instance ID for the service.
*/
MessageService(uint8_t instance_id);
/**
* @brief Constructor for `MessageService` with a custom Service ID.
*
* Initializes the service with the given instance ID and a custom service ID.
*
* @param instance_id The instance ID for the service.
* @param service_id The custom service ID.
*/
MessageService(uint8_t instance_id, uint8_t service_id);
/**
* @brief Processes all messages from the receive stack.
*
* Processes each message in the receive stack until it is empty. For each message, it prints the message content to the serial output,
* generates and adds an acknowledgment PDU to the send stack, and removes the processed message from the receive stack.
*/
void stackProcessing() override;
/**
* @brief Retrieves the destination instance ID for the current response PDU in the send stack.
*
* @return uint8_t The destination instance ID extracted from the message in the send stack.
*/
uint8_t get_destinationId();
/**
* @brief Sends a new message.
*
* Constructs and sends a message with the specified receiver ID and message text.
* The message is added to the send stack for transmission.
*
* @param receiverId char, The ID of the message receiver.
* @param messagetext String, The text of the message to be sent.
*/
void sendMessage(char receiverId, String messagetext);
private:
/**
* @brief Adds an acknowledgment for the given message to the send stack.
*
* Constructs an acknowledgment message based on the provided message content and adds it to the send stack.
* The acknowledgment message is addressed to the sender of the received message.
*
* @param message The message for which to send an acknowledgment.
*/
void sendAck(Message* message);
/**
* @brief Prints the content of the received message.
*
* Outputs the string representation of the message to the serial monitor.
*
* @param message The message to be printed.
*/
void printMessage(Message* message);
};
#endif // MESSAGE_SERVICE_H