blob: ba496e7dbec6f7cdc1239dffd8a77f0b3717a026 [file] [log] [blame]
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -05001#pragma once
2
Tom Joseph295f17e2017-09-07 00:09:46 +05303#include "sensorhandler.h"
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -05004#include "types.hpp"
Tom Joseph295f17e2017-09-07 00:09:46 +05305#include "utils.hpp"
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -05006#include "host-ipmid/ipmid-api.h"
7
8namespace ipmi
9{
10namespace sensor
11{
12
13using Assertion = uint16_t;
14using Deassertion = uint16_t;
15using AssertionSet = std::pair<Assertion, Deassertion>;
16
17using Service = std::string;
18using Path = std::string;
19using Interface = std::string;
20
21using ServicePath = std::pair<Path, Service>;
22
23using Interfaces = std::vector<Interface>;
24
25using MapperResponseType = std::map<Path, std::map<Service, Interfaces>>;
26
27/** @brief get the D-Bus service and service path
28 * @param[in] bus - The Dbus bus object
29 * @param[in] interface - interface to the service
30 * @param[in] path - interested path in the list of objects
31 * @return pair of service path and service
32 */
33ServicePath getServiceAndPath(sdbusplus::bus::bus& bus,
34 const std::string& interface,
35 const std::string& path = std::string());
36
37/** @brief Make assertion set from input data
38 * @param[in] cmdData - Input sensor data
39 * @return pair of assertion and deassertion set
40 */
41AssertionSet getAssertionSet(const SetSensorReadingReq& cmdData);
42
43/** @brief send the message to DBus
44 * @param[in] msg - message to send
45 * @return failure status in IPMI error code
46 */
Dhruvaraj Subhashchandran2a444d02017-08-07 01:45:14 -050047ipmi_ret_t updateToDbus(IpmiUpdateData& msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050048
Tom Joseph816e92b2017-09-06 19:23:00 +053049namespace get
50{
51
52/**
53 * @brief Helper function to map the dbus info to sensor's assertion status
54 * for the get sensor reading command.
55 *
56 * @param[in] sensorInfo - Dbus info related to sensor.
57 * @param[in] path - Dbus object path.
58 * @param[in] interface - Dbus interface.
59 *
60 * @return Response for get sensor reading command.
61 */
62GetSensorResponse mapDbusToAssertion(const Info& sensorInfo,
63 const InstancePath& path,
64 const DbusInterface& interface);
65
66/**
67 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
68 * reading command response.
69 *
70 * @param[in] sensorInfo - Dbus info related to sensor.
71 *
72 * @return Response for get sensor reading command.
73 */
74GetSensorResponse assertion(const Info& sensorInfo);
75
Tom Josephe4014fc2017-09-06 23:57:36 +053076/**
77 * @brief Maps the Dbus info to the reading field in the Get sensor reading
78 * command response.
79 *
80 * @param[in] sensorInfo - Dbus info related to sensor.
81 *
82 * @return Response for get sensor reading command.
83 */
84GetSensorResponse eventdata2(const Info& sensorInfo);
85
Tom Joseph295f17e2017-09-07 00:09:46 +053086/**
87 * @brief readingAssertion is a case where the entire assertion state field
88 * serves as the sensor value.
89 *
90 * @tparam T - type of the dbus property related to sensor.
91 * @param[in] sensorInfo - Dbus info related to sensor.
92 *
93 * @return Response for get sensor reading command.
94 */
95template<typename T>
96GetSensorResponse readingAssertion(const Info& sensorInfo)
97{
98 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
99 GetSensorResponse response {};
100 auto responseData = reinterpret_cast<GetReadingResponse*>(response.data());
101
102 auto service = ipmi::getService(bus,
103 sensorInfo.sensorInterface,
104 sensorInfo.sensorPath);
105
106 auto propValue = ipmi::getDbusProperty(
107 bus,
108 service,
109 sensorInfo.sensorPath,
110 sensorInfo.propertyInterfaces.begin()->first,
111 sensorInfo.propertyInterfaces.begin()->second.begin()->first);
112
113 setAssertionBytes(static_cast<uint16_t>(propValue.get<T>()), responseData);
114
115 return response;
116}
117
Tom Joseph816e92b2017-09-06 19:23:00 +0530118} //namespace get
119
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500120namespace set
121{
122
123/** @brief Make a DBus message for a Dbus call
124 * @param[in] updateInterface - Interface name
125 * @param[in] sensorPath - Path of the sensor
126 * @param[in] command - command to be executed
127 * @param[in] sensorInterface - DBus interface of sensor
128 * @return a dbus message
129 */
130IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
131 const std::string& sensorPath,
132 const std::string& command,
133 const std::string& sensorInterface);
134
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500135/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500136 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500137 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500138 * @return a IPMI error code
139 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500140ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
141 const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500142
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500143/** @brief Update d-bus based on a reading assertion
144 * @tparam T - type of d-bus property mapping this sensor
145 * @param[in] cmdData - input sensor data
146 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500147 * @return a IPMI error code
148 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500149template<typename T>
150ipmi_ret_t readingAssertion(const SetSensorReadingReq& cmdData,
151 const Info& sensorInfo)
152{
153 auto msg = makeDbusMsg(
154 "org.freedesktop.DBus.Properties",
155 sensorInfo.sensorPath,
156 "Set",
157 sensorInfo.sensorInterface);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500158
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500159 const auto& interface = sensorInfo.propertyInterfaces.begin();
160 msg.append(interface->first);
161 for (const auto& property : interface->second)
162 {
163 msg.append(property.first);
164 sdbusplus::message::variant<T> value =
165 (cmdData.assertOffset8_14 << 8) | cmdData.assertOffset0_7;
166 msg.append(value);
167 }
168 return updateToDbus(msg);
169}
170
Emily Shaffercc941e12017-06-14 13:06:26 -0700171/** @brief Update d-bus based on a discrete reading
172 * @param[in] cmdData - input sensor data
173 * @param[in] sensorInfo - sensor d-bus info
174 * @return an IPMI error code
175 */
176template<typename T>
177ipmi_ret_t readingData(const SetSensorReadingReq& cmdData,
178 const Info& sensorInfo)
179{
180 auto msg = makeDbusMsg(
181 "org.freedesktop.DBus.Properties",
182 sensorInfo.sensorPath,
183 "Set",
184 sensorInfo.sensorInterface);
185
186 const auto& interface = sensorInfo.propertyInterfaces.begin();
187 msg.append(interface->first);
188
189 ipmi::sensor::Multiplier m = sensorInfo.coefficientM;
190 if (0 == m)
191 {
192 m = 1; // Avoid * 0
193 }
194
195 // TODO: Refactor this into a generated function depending on the type
196 // of conversion for the value between IPMI and dbus.
197 T raw_value = (m * cmdData.reading) + sensorInfo.scaledOffset;
198
199 for (const auto& property : interface->second)
200 {
201 msg.append(property.first);
202 sdbusplus::message::variant<T> value = raw_value;
203 msg.append(value);
204 }
205 return updateToDbus(msg);
206}
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500207
208/** @brief Update d-bus based on eventdata type sensor data
209 * @param[in] cmdData - input sensor data
210 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500211 * @return a IPMI error code
212 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500213ipmi_ret_t eventdata(const SetSensorReadingReq& cmdData,
214 const Info& sensorInfo,
215 uint8_t data);
216
217/** @brief Update d-bus based on eventdata1 type sensor data
218 * @param[in] cmdData - input sensor data
219 * @param[in] sensorInfo - sensor d-bus info
220 * @return a IPMI error code
221 */
222inline ipmi_ret_t eventdata1(const SetSensorReadingReq& cmdData,
223 const Info& sensorInfo)
224{
225 return eventdata(cmdData, sensorInfo, cmdData.eventData1);
226}
227
228/** @brief Update d-bus based on eventdata2 type sensor data
229 * @param[in] cmdData - input sensor data
230 * @param[in] sensorInfo - sensor d-bus info
231 * @return a IPMI error code
232 */
233inline ipmi_ret_t eventdata2(const SetSensorReadingReq& cmdData,
234 const Info& sensorInfo)
235{
236 return eventdata(cmdData, sensorInfo, cmdData.eventData2);
237}
238
239/** @brief Update d-bus based on eventdata3 type sensor data
240 * @param[in] cmdData - input sensor data
241 * @param[in] sensorInfo - sensor d-bus info
242 * @return a IPMI error code
243 */
244inline ipmi_ret_t eventdata3(const SetSensorReadingReq& cmdData,
245 const Info& sensorInfo)
246{
247 return eventdata(cmdData, sensorInfo, cmdData.eventData3);
248}
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500249
250}//namespace set
251
252namespace notify
253{
254
255/** @brief Make a DBus message for a Dbus call
256 * @param[in] updateInterface - Interface name
257 * @param[in] sensorPath - Path of the sensor
258 * @param[in] command - command to be executed
259 * @param[in] sensorInterface - DBus interface of sensor
260 * @return a dbus message
261 */
262IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
263 const std::string& sensorPath,
264 const std::string& command,
265 const std::string& sensorInterface);
266
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500267/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500268 * @param[in] interfaceMap - sensor interface
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500269 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500270 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500271 * @return a IPMI error code
272 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500273ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
274 const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500275
276}//namespace notify
Tom Joseph816e92b2017-09-06 19:23:00 +0530277
278namespace inventory
279{
280
281namespace get
282{
283
284/**
285 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
286 * reading command response.
287 *
288 * @param[in] sensorInfo - Dbus info related to sensor.
289 *
290 * @return Response for get sensor reading command.
291 */
292GetSensorResponse assertion(const Info& sensorInfo);
293
294} // namespace get
295
296} // namespace inventory
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500297}//namespace sensor
298}//namespace ipmi