-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathisobus_shortcut_button_interface.cpp
236 lines (209 loc) · 8.53 KB
/
isobus_shortcut_button_interface.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//================================================================================================
/// @file isobus_shortcut_button_interface.cpp
///
/// @brief Implements the interface for an ISOBUS shortcut button
/// @author Adrian Del Grosso
///
/// @copyright 2023 The Open-Agriculture Developers
//================================================================================================
#include "isobus_shortcut_button_interface.hpp"
#include "can_constants.hpp"
#include "can_general_parameter_group_numbers.hpp"
#include "can_network_manager.hpp"
#include "can_stack_logger.hpp"
#include "system_timing.hpp"
#include <algorithm>
#include <cassert>
namespace isobus
{
ShortcutButtonInterface::ShortcutButtonInterface(std::shared_ptr<InternalControlFunction> internalControlFunction, bool serverEnabled) :
sourceControlFunction(internalControlFunction),
txFlags(static_cast<std::uint32_t>(TransmitFlags::NumberOfFlags), process_flags, this),
actAsISBServer(serverEnabled)
{
assert(nullptr != sourceControlFunction); // You need an internal control function for the interface to function
}
ShortcutButtonInterface::~ShortcutButtonInterface()
{
if (initialized)
{
CANNetworkManager::CANNetwork.remove_global_parameter_group_number_callback(static_cast<std::uint32_t>(CANLibParameterGroupNumber::AllImplementsStopOperationsSwitchState), process_rx_message, this);
}
}
void ShortcutButtonInterface::initialize()
{
if (!initialized)
{
CANNetworkManager::CANNetwork.add_global_parameter_group_number_callback(static_cast<std::uint32_t>(CANLibParameterGroupNumber::AllImplementsStopOperationsSwitchState),
process_rx_message,
this);
initialized = true;
}
}
bool ShortcutButtonInterface::get_is_initialized() const
{
return initialized;
}
EventDispatcher<ShortcutButtonInterface::StopAllImplementOperationsState> &ShortcutButtonInterface::get_stop_all_implement_operations_state_event_dispatcher()
{
return ISBEventDispatcher;
}
void ShortcutButtonInterface::set_stop_all_implement_operations_state(StopAllImplementOperationsState newState)
{
if (actAsISBServer)
{
if (newState != commandedState)
{
commandedState = newState;
if (StopAllImplementOperationsState::StopImplementOperations == newState)
{
LOG_ERROR("[ISB]: All implement operations must stop. (Triggered internally)");
}
else
{
LOG_INFO("[ISB]: Internal ISB state is now permitted.");
}
txFlags.set_flag(static_cast<std::uint32_t>(TransmitFlags::SendStopAllImplementOperationsSwitchState));
}
}
else
{
LOG_ERROR("[ISB]: You are attempting to set the internal ISB state but the ISB interface is not configured as a server!");
}
}
ShortcutButtonInterface::StopAllImplementOperationsState ShortcutButtonInterface::get_state() const
{
StopAllImplementOperationsState retVal = StopAllImplementOperationsState::PermitAllImplementsToOperationOn;
if (StopAllImplementOperationsState::StopImplementOperations == commandedState)
{
retVal = commandedState;
}
else
{
for (auto ISB = isobusShorcutButtonList.cbegin(); ISB != isobusShorcutButtonList.end(); ISB++)
{
if (StopAllImplementOperationsState::StopImplementOperations == ISB->commandedState)
{
retVal = ISB->commandedState; // Any stop condition will be returned.
break;
}
}
}
return retVal;
}
void ShortcutButtonInterface::update()
{
if (SystemTiming::time_expired_ms(allImplementsStopOperationsSwitchStateTimestamp_ms, TRANSMISSION_RATE_MS))
{
// Prune old ISBs
isobusShorcutButtonList.erase(std::remove_if(isobusShorcutButtonList.begin(), isobusShorcutButtonList.end(), [](ISBServerData &isb) {
return SystemTiming::time_expired_ms(isb.messageReceivedTimestamp_ms, TRANSMISSION_TIMEOUT_MS);
}),
isobusShorcutButtonList.end());
txFlags.set_flag(static_cast<std::uint32_t>(TransmitFlags::SendStopAllImplementOperationsSwitchState));
}
txFlags.process_all_flags();
}
void ShortcutButtonInterface::process_rx_message(const CANMessage &message, void *parentPointer)
{
assert(nullptr != parentPointer);
static_cast<ShortcutButtonInterface *>(parentPointer)->process_message(message);
}
void ShortcutButtonInterface::process_flags(std::uint32_t flag, void *parent)
{
auto myInterface = static_cast<ShortcutButtonInterface *>(parent);
bool transmitSuccessful = true;
assert(nullptr != parent);
if (flag == static_cast<std::uint32_t>(TransmitFlags::SendStopAllImplementOperationsSwitchState))
{
transmitSuccessful = myInterface->send_stop_all_implement_operations_switch_state();
if (transmitSuccessful)
{
myInterface->stopAllImplementOperationsTransitionNumber++;
}
}
if (!transmitSuccessful)
{
myInterface->txFlags.set_flag(flag);
}
}
void ShortcutButtonInterface::process_message(const CANMessage &message)
{
if ((message.get_can_port_index() == sourceControlFunction->get_can_port()) &&
(static_cast<std::uint32_t>(CANLibParameterGroupNumber::AllImplementsStopOperationsSwitchState) == message.get_identifier().get_parameter_group_number()))
{
if (CAN_DATA_LENGTH == message.get_data_length())
{
auto messageNAME = message.get_source_control_function()->get_NAME();
auto matches_isoname = [messageNAME](ISBServerData &isb) { return isb.ISONAME == messageNAME; };
auto ISB = std::find_if(isobusShorcutButtonList.begin(), isobusShorcutButtonList.end(), matches_isoname);
auto &messageData = message.get_data();
StopAllImplementOperationsState previousState = get_state();
if (isobusShorcutButtonList.end() == ISB)
{
ISBServerData newISB;
LOG_DEBUG("[ISB]: New ISB detected at address %u", message.get_identifier().get_source_address());
newISB.ISONAME = messageNAME;
isobusShorcutButtonList.emplace_back(newISB);
ISB = std::prev(isobusShorcutButtonList.end());
}
if (isobusShorcutButtonList.end() != ISB)
{
std::uint8_t newTransitionCount = messageData.at(6);
if (((ISB->stopAllImplementOperationsTransitionNumber == 255) &&
(0 != newTransitionCount)) ||
((ISB->stopAllImplementOperationsTransitionNumber < 255) &&
(newTransitionCount > ISB->stopAllImplementOperationsTransitionNumber + 1)))
{
// A Working Set shall consider an increase in the transitions without detecting a corresponding
// transition of the Stop all implement operations state as an error and react accordingly.
ISB->commandedState = StopAllImplementOperationsState::StopImplementOperations;
LOG_ERROR("[ISB]: Missed an ISB transition from ISB at address %u", message.get_identifier().get_source_address());
}
else
{
ISB->commandedState = static_cast<StopAllImplementOperationsState>(messageData.at(7) & 0x03);
}
ISB->messageReceivedTimestamp_ms = SystemTiming::get_timestamp_ms();
ISB->stopAllImplementOperationsTransitionNumber = messageData.at(6);
auto newState = get_state();
if (previousState != newState)
{
if (StopAllImplementOperationsState::StopImplementOperations == newState)
{
LOG_ERROR("[ISB]: All implement operations must stop. (ISB at address %u has commanded it)", message.get_identifier().get_source_address());
}
else
{
LOG_INFO("[ISB]: Implement operations now permitted.");
}
ISBEventDispatcher.call(newState);
}
}
}
else
{
LOG_WARNING("[ISB]: Received malformed All Implements Stop Operations Switch State. DLC must be 8.");
}
}
}
bool ShortcutButtonInterface::send_stop_all_implement_operations_switch_state() const
{
std::array<std::uint8_t, CAN_DATA_LENGTH> buffer = {
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
stopAllImplementOperationsTransitionNumber,
static_cast<std::uint8_t>(0xFC | static_cast<std::uint8_t>(commandedState))
};
return CANNetworkManager::CANNetwork.send_can_message(static_cast<std::uint32_t>(CANLibParameterGroupNumber::AllImplementsStopOperationsSwitchState),
buffer.data(),
static_cast<std::uint32_t>(buffer.size()),
sourceControlFunction,
nullptr,
CANIdentifier::CANPriority::Priority3);
}
}