This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbus_cb.cpp
264 lines (242 loc) · 8.51 KB
/
dbus_cb.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
// Copyright (c) 2021 Intel Corporation
//
// 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 "dbus_cb.hpp"
#include "mctp_impl.hpp"
#include <boost/container/flat_map.hpp>
#include <phosphor-logging/log.hpp>
#include <unordered_set>
template <typename T1, typename T2>
using DictType = boost::container::flat_map<T1, T2>;
using MctpPropertiesVariantType =
std::variant<uint16_t, int16_t, int32_t, uint32_t, bool, std::string,
uint8_t, std::vector<uint8_t>>;
using eid_t = uint8_t;
namespace mctpw
{
int onPropertiesChanged(sd_bus_message* rawMsg, void* userData,
sd_bus_error* retError)
{
if (!userData || (retError && sd_bus_error_is_set(retError)))
{
return -1;
}
MCTPImpl* context = static_cast<MCTPImpl*>(userData);
sdbusplus::message::message message{rawMsg};
if (!context->networkChangeCallback)
{
return -1;
}
/* Signal format:
* STRING interface_name,
* DICT<STRING,VARIANT> changed_properties,
* ARRAY<STRING> invalidated_properties
*/
DictType<std::string, MctpPropertiesVariantType> properties;
std::string interface;
message.read(interface, properties);
static const std::unordered_set<std::string> tracedProperties = {
"Eid", "EidPool", "Mode", "NetworkId", "discoveredFlag",
"SlaveAddress", "BusPath"};
for (const auto& [propertyName, propertyVal] : properties)
{
if (tracedProperties.find(propertyName) != tracedProperties.end())
{
// TODO Handle property changed event
}
}
return 1;
}
static eid_t getEIdFromPath(const sdbusplus::message::object_path& object_path)
{
try
{
auto slashLoc = object_path.str.find_last_of('/');
if (object_path.str.npos == slashLoc)
{
throw std::runtime_error("Invalid device path");
}
auto strDeviceId = object_path.str.substr(slashLoc + 1);
return static_cast<eid_t>(std::stoi(strDeviceId));
}
catch (const std::exception& e)
{
throw std::runtime_error(std::string("Error getting eid from ") +
object_path.str + ". " + e.what());
}
}
int onInterfacesAdded(sd_bus_message* rawMsg, void* userData,
sd_bus_error* retError)
{
if (!userData || (retError && sd_bus_error_is_set(retError)))
{
return -1;
}
MCTPImpl* context = static_cast<MCTPImpl*>(userData);
sdbusplus::message::message message{rawMsg};
if (!context->networkChangeCallback)
{
return -1;
}
/* Signal format:
* OBJPATH object_path,
* DICT<STRING,DICT<STRING,VARIANT>> interfaces_and_properties
*/
DictType<std::string, DictType<std::string, MctpPropertiesVariantType>>
values;
sdbusplus::message::object_path object_path;
mctpw::Event event;
try
{
message.read(object_path, values);
auto serviceName = message.get_sender();
auto itSupportedMsgTypes =
values.find("xyz.openbmc_project.MCTP.SupportedMessageTypes");
if (values.end() != itSupportedMsgTypes)
{
event.eid = getEIdFromPath(object_path);
const auto& properties = itSupportedMsgTypes->second;
const auto& registeredMsgType =
properties.at(mctpw::MCTPImpl::msgTypeToPropertyName.at(
context->config.type));
if (std::get<bool>(registeredMsgType))
{
event.type = mctpw::Event::EventType::deviceAdded;
boost::asio::spawn(context->connection->get_io_context(),
[context, object_path, serviceName, userData,
event](boost::asio::yield_context yield) {
context->addToEidMap(yield, serviceName);
context->networkChangeCallback(
userData, event, yield);
return 1;
});
}
}
}
catch (const std::exception& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
(std::string("onInterfaceAdded: ") + e.what()).c_str());
}
return -1;
}
int onInterfacesRemoved(sd_bus_message* rawMsg, void* userData,
sd_bus_error* retError)
{
if (!userData || (retError && sd_bus_error_is_set(retError)))
{
return -1;
}
MCTPImpl* context = static_cast<MCTPImpl*>(userData);
sdbusplus::message::message message{rawMsg};
if (!context->networkChangeCallback)
{
return -1;
}
// MESSAGE "oas"
try
{
sdbusplus::message::object_path object_path;
std::vector<std::string> interfaces;
message.read(object_path, interfaces);
mctpw::Event event;
// TODO Remove match rules for the services going down
if (std::find(interfaces.begin(), interfaces.end(),
"xyz.openbmc_project.MCTP.SupportedMessageTypes") !=
interfaces.end())
{
event.type = mctpw::Event::EventType::deviceRemoved;
event.eid = getEIdFromPath(object_path);
if (context->eraseDevice(event.eid) == 1)
{
boost::asio::spawn(context->connection->get_io_context(),
[context, userData,
event](boost::asio::yield_context yield) {
context->networkChangeCallback(
userData, event, yield);
return 1;
});
}
else
{
phosphor::logging::log<phosphor::logging::level::ERR>(
"Removed device is not in endpoint map");
}
}
}
catch (const std::exception& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
(std::string("onInterfacesRemoved: ") + e.what()).c_str());
}
return -1;
}
int onMessageReceivedSignal(sd_bus_message* rawMsg, void* userData,
sd_bus_error* retError)
{
if (!userData || (retError && sd_bus_error_is_set(retError)))
{
return -1;
}
try
{
MCTPImpl* context = static_cast<MCTPImpl*>(userData);
sdbusplus::message::message message{rawMsg};
if (!context->receiveCallback)
{
return -1;
}
uint8_t messageType = 0;
uint8_t srcEid = 0;
uint8_t msgTag = 0;
bool tagOwner = false;
std::vector<uint8_t> payload;
message.read(messageType, srcEid, msgTag, tagOwner, payload);
if (static_cast<MessageType>(messageType) != context->config.type)
{
return -1;
}
if (static_cast<MessageType>(messageType) == MessageType::vdpci)
{
struct VendorHeader
{
uint8_t vdpciMessageType;
uint16_t vendorId;
uint16_t intelVendorMessageId;
} __attribute__((packed));
VendorHeader* vendorHdr =
reinterpret_cast<VendorHeader*>(payload.data());
if (!context->config.vendorId ||
!context->config.vendorMessageType ||
(vendorHdr->vendorId != context->config.vendorId) ||
((vendorHdr->intelVendorMessageId &
context->config.vendorMessageType->mask) !=
(context->config.vendorMessageType->value &
context->config.vendorMessageType->mask)))
{
return -1;
}
}
context->receiveCallback(context, srcEid, tagOwner, msgTag, payload, 0);
return 1;
}
catch (std::exception& e)
{
phosphor::logging::log<phosphor::logging::level::ERR>(
(std::string("onMessageReceivedSignal: ") + e.what()).c_str());
}
return -1;
}
} // namespace mctpw