blob: 3a66f48d4b6a504cf37710605f637000c997309e [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
74} //namespace get
75
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050076namespace set
77{
78
79/** @brief Make a DBus message for a Dbus call
80 * @param[in] updateInterface - Interface name
81 * @param[in] sensorPath - Path of the sensor
82 * @param[in] command - command to be executed
83 * @param[in] sensorInterface - DBus interface of sensor
84 * @return a dbus message
85 */
86IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
87 const std::string& sensorPath,
88 const std::string& command,
89 const std::string& sensorInterface);
90
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -050091/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050092 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -050093 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050094 * @return a IPMI error code
95 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -050096ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
97 const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -050098
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -050099/** @brief Update d-bus based on a reading assertion
100 * @tparam T - type of d-bus property mapping this sensor
101 * @param[in] cmdData - input sensor data
102 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500103 * @return a IPMI error code
104 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500105template<typename T>
106ipmi_ret_t readingAssertion(const SetSensorReadingReq& cmdData,
107 const Info& sensorInfo)
108{
109 auto msg = makeDbusMsg(
110 "org.freedesktop.DBus.Properties",
111 sensorInfo.sensorPath,
112 "Set",
113 sensorInfo.sensorInterface);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500114
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500115 const auto& interface = sensorInfo.propertyInterfaces.begin();
116 msg.append(interface->first);
117 for (const auto& property : interface->second)
118 {
119 msg.append(property.first);
120 sdbusplus::message::variant<T> value =
121 (cmdData.assertOffset8_14 << 8) | cmdData.assertOffset0_7;
122 msg.append(value);
123 }
124 return updateToDbus(msg);
125}
126
Emily Shaffercc941e12017-06-14 13:06:26 -0700127/** @brief Update d-bus based on a discrete reading
128 * @param[in] cmdData - input sensor data
129 * @param[in] sensorInfo - sensor d-bus info
130 * @return an IPMI error code
131 */
132template<typename T>
133ipmi_ret_t readingData(const SetSensorReadingReq& cmdData,
134 const Info& sensorInfo)
135{
136 auto msg = makeDbusMsg(
137 "org.freedesktop.DBus.Properties",
138 sensorInfo.sensorPath,
139 "Set",
140 sensorInfo.sensorInterface);
141
142 const auto& interface = sensorInfo.propertyInterfaces.begin();
143 msg.append(interface->first);
144
145 ipmi::sensor::Multiplier m = sensorInfo.coefficientM;
146 if (0 == m)
147 {
148 m = 1; // Avoid * 0
149 }
150
151 // TODO: Refactor this into a generated function depending on the type
152 // of conversion for the value between IPMI and dbus.
153 T raw_value = (m * cmdData.reading) + sensorInfo.scaledOffset;
154
155 for (const auto& property : interface->second)
156 {
157 msg.append(property.first);
158 sdbusplus::message::variant<T> value = raw_value;
159 msg.append(value);
160 }
161 return updateToDbus(msg);
162}
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500163
164/** @brief Update d-bus based on eventdata type sensor data
165 * @param[in] cmdData - input sensor data
166 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500167 * @return a IPMI error code
168 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500169ipmi_ret_t eventdata(const SetSensorReadingReq& cmdData,
170 const Info& sensorInfo,
171 uint8_t data);
172
173/** @brief Update d-bus based on eventdata1 type sensor data
174 * @param[in] cmdData - input sensor data
175 * @param[in] sensorInfo - sensor d-bus info
176 * @return a IPMI error code
177 */
178inline ipmi_ret_t eventdata1(const SetSensorReadingReq& cmdData,
179 const Info& sensorInfo)
180{
181 return eventdata(cmdData, sensorInfo, cmdData.eventData1);
182}
183
184/** @brief Update d-bus based on eventdata2 type sensor data
185 * @param[in] cmdData - input sensor data
186 * @param[in] sensorInfo - sensor d-bus info
187 * @return a IPMI error code
188 */
189inline ipmi_ret_t eventdata2(const SetSensorReadingReq& cmdData,
190 const Info& sensorInfo)
191{
192 return eventdata(cmdData, sensorInfo, cmdData.eventData2);
193}
194
195/** @brief Update d-bus based on eventdata3 type sensor data
196 * @param[in] cmdData - input sensor data
197 * @param[in] sensorInfo - sensor d-bus info
198 * @return a IPMI error code
199 */
200inline ipmi_ret_t eventdata3(const SetSensorReadingReq& cmdData,
201 const Info& sensorInfo)
202{
203 return eventdata(cmdData, sensorInfo, cmdData.eventData3);
204}
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500205
206}//namespace set
207
208namespace notify
209{
210
211/** @brief Make a DBus message for a Dbus call
212 * @param[in] updateInterface - Interface name
213 * @param[in] sensorPath - Path of the sensor
214 * @param[in] command - command to be executed
215 * @param[in] sensorInterface - DBus interface of sensor
216 * @return a dbus message
217 */
218IpmiUpdateData makeDbusMsg(const std::string& updateInterface,
219 const std::string& sensorPath,
220 const std::string& command,
221 const std::string& sensorInterface);
222
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500223/** @brief Update d-bus based on assertion type sensor data
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500224 * @param[in] interfaceMap - sensor interface
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500225 * @param[in] cmdData - input sensor data
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500226 * @param[in] sensorInfo - sensor d-bus info
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500227 * @return a IPMI error code
228 */
Deepak Kodihalli1bb0d382017-08-12 02:01:27 -0500229ipmi_ret_t assertion(const SetSensorReadingReq& cmdData,
230 const Info& sensorInfo);
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500231
232}//namespace notify
Tom Joseph816e92b2017-09-06 19:23:00 +0530233
234namespace inventory
235{
236
237namespace get
238{
239
240/**
241 * @brief Map the Dbus info to sensor's assertion status in the Get sensor
242 * reading command response.
243 *
244 * @param[in] sensorInfo - Dbus info related to sensor.
245 *
246 * @return Response for get sensor reading command.
247 */
248GetSensorResponse assertion(const Info& sensorInfo);
249
250} // namespace get
251
252} // namespace inventory
Dhruvaraj Subhashchandrane0af7202017-07-12 06:35:20 -0500253}//namespace sensor
254}//namespace ipmi