-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmvec_relay_socketcan.cpp
More file actions
160 lines (135 loc) · 5.2 KB
/
mvec_relay_socketcan.cpp
File metadata and controls
160 lines (135 loc) · 5.2 KB
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
// Copyright (c) 2025-present Polymath Robotics, Inc. All rights reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "mvec_lib/mvec_relay_socketcan.hpp"
#include <memory>
#include <mutex>
#include <utility>
namespace polymath::sygnal
{
MvecRelaySocketcan::MvecRelaySocketcan(std::shared_ptr<socketcan::SocketcanAdapter> socketcan_adapter)
: socketcan_adapter_(socketcan_adapter)
, relay_impl_()
{}
MvecMessageType MvecRelaySocketcan::parse(const socketcan::CanFrame & frame)
{
const MvecMessageType message_type = relay_impl_.parseMessage(frame);
// Check if we received an expected response type and fulfill the waiting promise
switch (message_type) {
case MvecMessageType::RELAY_QUERY_RESPONSE: {
const auto & reply = relay_impl_.get_last_relay_query_reply();
std::lock_guard<std::mutex> lock(query_mutex_);
if (reply.is_valid() && query_reply_promise_.has_value()) {
// Fulfill the promise and clear the slot
query_reply_promise_->set_value(reply);
query_reply_promise_.reset();
}
break;
}
case MvecMessageType::RELAY_COMMAND_RESPONSE: {
const auto & reply = relay_impl_.get_last_relay_command_reply();
std::lock_guard<std::mutex> lock(command_mutex_);
if (reply.is_valid() && command_reply_promise_.has_value()) {
command_reply_promise_->set_value(reply);
command_reply_promise_.reset();
}
break;
}
case MvecMessageType::POPULATION_RESPONSE: {
const auto & reply = relay_impl_.get_last_population_reply();
std::lock_guard<std::mutex> lock(population_mutex_);
if (reply.is_valid() && population_reply_promise_.has_value()) {
population_reply_promise_->set_value(reply);
population_reply_promise_.reset();
}
break;
}
default:
// Status messages and other types don't need promise handling
break;
}
return message_type;
}
void MvecRelaySocketcan::set_relay_in_command(uint8_t relay_id, uint8_t relay_state)
{
/// TODO: (zeerek) Where do we check against the population table? When do we initially query the population table?
relay_impl_.set_relay_in_command(relay_id, relay_state);
}
void MvecRelaySocketcan::clear_relay()
{
relay_impl_.clearRelayCommands();
}
std::future<MvecRelayQueryReply> MvecRelaySocketcan::get_relay_state()
{
std::lock_guard<std::mutex> lock(query_mutex_);
// Abandon any in-flight query, caller's future becomes broken_promise if still waiting
query_reply_promise_.reset();
// Create a new promise and get its future
std::promise<MvecRelayQueryReply> promise;
auto future = promise.get_future();
query_reply_promise_.emplace(std::move(promise));
// Transmit the query message via socketcan adapter
socketcan_adapter_->send(relay_impl_.getRelayQueryMessage());
return future;
}
std::future<MvecRelayCommandReply> MvecRelaySocketcan::send_relay_command()
{
std::lock_guard<std::mutex> lock(command_mutex_);
// Abandon any in-flight command, caller's future becomes broken_promise if still waiting
command_reply_promise_.reset();
// Create a new promise and get its future
std::promise<MvecRelayCommandReply> promise;
auto future = promise.get_future();
command_reply_promise_.emplace(std::move(promise));
// Transmit the command message via socketcan adapter
socketcan_adapter_->send(relay_impl_.getRelayCommandMessage());
return future;
}
std::future<MvecPopulationReply> MvecRelaySocketcan::get_relay_population()
{
std::lock_guard<std::mutex> lock(population_mutex_);
// Abandon any in-flight query, caller's future becomes broken_promise if still waiting
population_reply_promise_.reset();
// Create a new promise and get its future
std::promise<MvecPopulationReply> promise;
auto future = promise.get_future();
population_reply_promise_.emplace(std::move(promise));
// Transmit the population query message via socketcan adapter
socketcan_adapter_->send(relay_impl_.getPopulationQueryMessage());
return future;
}
std::optional<MvecFuseStatusMessage> MvecRelaySocketcan::get_last_fuse_status() const
{
const auto & fuse_status = relay_impl_.get_fuse_status_message();
if (fuse_status.is_valid()) {
return fuse_status;
}
return std::nullopt;
}
std::optional<MvecRelayStatusMessage> MvecRelaySocketcan::get_last_relay_status() const
{
const auto & relay_status = relay_impl_.get_relay_status_message();
if (relay_status.is_valid()) {
return relay_status;
}
return std::nullopt;
}
std::optional<MvecErrorStatusMessage> MvecRelaySocketcan::get_last_error_status() const
{
const auto & error_status = relay_impl_.get_error_status_message();
if (error_status.is_valid()) {
return error_status;
}
return std::nullopt;
}
} // namespace polymath::sygnal