REST: Add method return support for variants
Variants are in their own container. In order to
get the type of the data contained in the variant,
the peek API needs to be used.
Resolves openbmc/bmcweb#24
Change-Id: I085064a8eabe2f57cfa0f1977fe7b9c19baa80e2
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp
index a5024be..9f282a6 100644
--- a/include/openbmc_dbus_rest.hpp
+++ b/include/openbmc_dbus_rest.hpp
@@ -995,6 +995,41 @@
return 0;
}
+int readVariantFromMessage(sdbusplus::message::message &m, nlohmann::json &data)
+{
+ const char *containerType;
+ int r = sd_bus_message_peek_type(m.get(), NULL, &containerType);
+ if (r < 0)
+ {
+ BMCWEB_LOG_ERROR << "sd_bus_message_peek_type failed";
+ return r;
+ }
+
+ r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_VARIANT,
+ containerType);
+ if (r < 0)
+ {
+ BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc "
+ << r;
+ return r;
+ }
+
+ r = convertDBusToJSON(containerType, m, data);
+ if (r < 0)
+ {
+ return r;
+ }
+
+ r = sd_bus_message_exit_container(m.get());
+ if (r < 0)
+ {
+ BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed";
+ return r;
+ }
+
+ return 0;
+}
+
int convertDBusToJSON(const std::string &returnType,
sdbusplus::message::message &m, nlohmann::json &response)
{
@@ -1133,9 +1168,16 @@
return r;
}
}
+ else if (boost::starts_with(typeCode, "v"))
+ {
+ r = readVariantFromMessage(m, thisElement);
+ if (r < 0)
+ {
+ return r;
+ }
+ }
else
{
- // TODO: add variant support
BMCWEB_LOG_ERROR << "Invalid D-Bus signature type " << typeCode;
return -2;
}