blob: 010772d58c87a486ef4c6d7394cf0e98ee5628ad [file] [log] [blame]
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -05001#pragma once
2
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -05003#include "types.hpp"
4#include "host-ipmid/ipmid-api.h"
5
6namespace ipmi
7{
8namespace sensor
9{
10
11using Assertion = uint16_t;
12using Deassertion = uint16_t;
13using AssertionSet = std::pair<Assertion, Deassertion>;
14
15using Service = std::string;
16using Path = std::string;
17using Interface = std::string;
18
19using ServicePath = std::pair<Path, Service>;
20
21using Interfaces = std::vector<Interface>;
22
23using MapperResponseType = std::map<Path, std::map<Service, Interfaces>>;
24
25/** @brief get the D-Bus service and service path
26 * @param[in] bus - The Dbus bus object
27 * @param[in] interface - interface to the service
28 * @param[in] path - interested path in the list of objects
29 * @return pair of service path and service
30 */
31ServicePath getServiceAndPath(sdbusplus::bus::bus& bus,
32 const std::string& interface,
33 const std::string& path = std::string());
34
35/** @brief Make assertion set from input data
36 * @param[in] cmdData - Input sensor data
37 * @return pair of assertion and deassertion set
38 */
39AssertionSet getAssertionSet(const SetSensorReadingReq& cmdData);
40
41/** @brief send the message to DBus
42 * @param[in] msg - message to send
43 * @return failure status in IPMI error code
44 */
Dhruvaraj Subhashchandran2a444d02017-08-07 01:45:14 -050045ipmi_ret_t updateToDbus(IpmiUpdateData& msg);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050046
Tom Joseph816e92b2017-09-06 19:23:00 +053047namespace get
48{
49
50/**
51 * @brief Helper function to map the dbus info to sensor's assertion status
52 * for the get sensor reading command.
53 *
54 * @param[in] sensorInfo - Dbus info related to sensor.
55 * @param[in] path - Dbus object path.
56 * @param[in] interface - Dbus interface.
57 *
58 * @return Response for get sensor reading command.
59 */
60GetSensorResponse mapDbusToAssertion(const Info& sensorInfo,
61 const InstancePath& path,
62 const DbusInterface& interface);
63
64/**
65 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
66 * reading command response.
67 *
68 * @param[in] sensorInfo - Dbus info related to sensor.
69 *
70 * @return Response for get sensor reading command.
71 */
72GetSensorResponse assertion(const Info& sensorInfo);
73
Tom Josephe4014fc2017-09-06 23:57:36 +053074/**
75 * @brief Maps the Dbus info to the reading field in the Get sensor reading
76 * command response.
77 *
78 * @param[in] sensorInfo - Dbus info related to sensor.
79 *
80 * @return Response for get sensor reading command.
81 */
82GetSensorResponse eventdata2(const Info& sensorInfo);
83
Tom Joseph816e92b2017-09-06 19:23:00 +053084} //namespace get
85
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050086namespace set
87{
88
89/** @brief Make a DBus message for a Dbus call
90 * @param[in] updateInterface - Interface name
91 * @param[in] sensorPath - Path of the sensor
92 * @param[in] command - command to be executed
93 * @param[in] sensorInterface - DBus interface of sensor
94 * @return a dbus message
95 */
96IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
97 const std::string& sensorPath,
98 const std::string& command,
99 const std::string& sensorInterface);
100
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500101/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500102 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500103 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500104 * @return a IPMI error code
105 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500106ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
107 const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500108
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500109/** @brief Update d-bus based on a reading assertion
110 * @tparam T - type of d-bus property mapping this sensor
111 * @param[in] cmdData - input sensor data
112 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500113 * @return a IPMI error code
114 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500115template<typename T>
116ipmi_ret_t readingAssertion(const SetSensorReadingReq& cmdData,
117 const Info& sensorInfo)
118{
119 auto msg = makeDbusMsg(
120 "org.freedesktop.DBus.Properties",
121 sensorInfo.sensorPath,
122 "Set",
123 sensorInfo.sensorInterface);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500124
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500125 const auto& interface = sensorInfo.propertyInterfaces.begin();
126 msg.append(interface->first);
127 for (const auto& property : interface->second)
128 {
129 msg.append(property.first);
130 sdbusplus::message::variant<T> value =
131 (cmdData.assertOffset8_14 << 8) | cmdData.assertOffset0_7;
132 msg.append(value);
133 }
134 return updateToDbus(msg);
135}
136
Emily Shaffercc941e12017-06-14 13:06:26 -0700137/** @brief Update d-bus based on a discrete reading
138 * @param[in] cmdData - input sensor data
139 * @param[in] sensorInfo - sensor d-bus info
140 * @return an IPMI error code
141 */
142template<typename T>
143ipmi_ret_t readingData(const SetSensorReadingReq& cmdData,
144 const Info& sensorInfo)
145{
146 auto msg = makeDbusMsg(
147 "org.freedesktop.DBus.Properties",
148 sensorInfo.sensorPath,
149 "Set",
150 sensorInfo.sensorInterface);
151
152 const auto& interface = sensorInfo.propertyInterfaces.begin();
153 msg.append(interface->first);
154
155 ipmi::sensor::Multiplier m = sensorInfo.coefficientM;
156 if (0 == m)
157 {
158 m = 1; // Avoid * 0
159 }
160
161 // TODO: Refactor this into a generated function depending on the type
162 // of conversion for the value between IPMI and dbus.
163 T raw_value = (m * cmdData.reading) + sensorInfo.scaledOffset;
164
165 for (const auto& property : interface->second)
166 {
167 msg.append(property.first);
168 sdbusplus::message::variant<T> value = raw_value;
169 msg.append(value);
170 }
171 return updateToDbus(msg);
172}
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500173
174/** @brief Update d-bus based on eventdata type sensor data
175 * @param[in] cmdData - input sensor data
176 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500177 * @return a IPMI error code
178 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500179ipmi_ret_t eventdata(const SetSensorReadingReq& cmdData,
180 const Info& sensorInfo,
181 uint8_t data);
182
183/** @brief Update d-bus based on eventdata1 type sensor data
184 * @param[in] cmdData - input sensor data
185 * @param[in] sensorInfo - sensor d-bus info
186 * @return a IPMI error code
187 */
188inline ipmi_ret_t eventdata1(const SetSensorReadingReq& cmdData,
189 const Info& sensorInfo)
190{
191 return eventdata(cmdData, sensorInfo, cmdData.eventData1);
192}
193
194/** @brief Update d-bus based on eventdata2 type sensor data
195 * @param[in] cmdData - input sensor data
196 * @param[in] sensorInfo - sensor d-bus info
197 * @return a IPMI error code
198 */
199inline ipmi_ret_t eventdata2(const SetSensorReadingReq& cmdData,
200 const Info& sensorInfo)
201{
202 return eventdata(cmdData, sensorInfo, cmdData.eventData2);
203}
204
205/** @brief Update d-bus based on eventdata3 type sensor data
206 * @param[in] cmdData - input sensor data
207 * @param[in] sensorInfo - sensor d-bus info
208 * @return a IPMI error code
209 */
210inline ipmi_ret_t eventdata3(const SetSensorReadingReq& cmdData,
211 const Info& sensorInfo)
212{
213 return eventdata(cmdData, sensorInfo, cmdData.eventData3);
214}
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500215
216}//namespace set
217
218namespace notify
219{
220
221/** @brief Make a DBus message for a Dbus call
222 * @param[in] updateInterface - Interface name
223 * @param[in] sensorPath - Path of the sensor
224 * @param[in] command - command to be executed
225 * @param[in] sensorInterface - DBus interface of sensor
226 * @return a dbus message
227 */
228IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
229 const std::string& sensorPath,
230 const std::string& command,
231 const std::string& sensorInterface);
232
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500233/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500234 * @param[in] interfaceMap - sensor interface
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500235 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500236 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500237 * @return a IPMI error code
238 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500239ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
240 const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500241
242}//namespace notify
Tom Joseph816e92b2017-09-06 19:23:00 +0530243
244namespace inventory
245{
246
247namespace get
248{
249
250/**
251 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
252 * reading command response.
253 *
254 * @param[in] sensorInfo - Dbus info related to sensor.
255 *
256 * @return Response for get sensor reading command.
257 */
258GetSensorResponse assertion(const Info& sensorInfo);
259
260} // namespace get
261
262} // namespace inventory
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500263}//namespace sensor
264}//namespace ipmi