Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create and update Asset tag for the system #483

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ static constexpr auto SHIFT_BITS_5 = 5;

static constexpr auto ASCII_OF_SPACE = 32;

constexpr auto systemInvPath = "/xyz/openbmc_project/inventory/system";
constexpr auto pimPath = "/xyz/openbmc_project/inventory";
constexpr auto pimIntf = "xyz.openbmc_project.Inventory.Manager";
constexpr auto ipzVpdInf = "com.ibm.ipzvpd.";
Expand Down Expand Up @@ -134,6 +135,7 @@ constexpr auto biosConfigMgrInterface =
"xyz.openbmc_project.BIOSConfig.Manager";
constexpr auto systemVpdInvPath =
"/xyz/openbmc_project/inventory/system/chassis/motherboard";
constexpr auto assetTagInf = "xyz.openbmc_project.Inventory.Decorator.AssetTag";
static constexpr auto BD_YEAR_END = 4;
static constexpr auto BD_MONTH_END = 7;
static constexpr auto BD_DAY_END = 10;
Expand Down
12 changes: 12 additions & 0 deletions include/manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ class Manager
* @throw std::runtime_error
*/
void SetTimerToDetectVpdCollectionStatus();

/**
* @brief API to register callback for "AssetTag" property change.
*/
void registerAssetChangeCallback();

/**
* @brief Callback API to be triggered on "AssetTag" property change.
*
* @param[in] i_msg - The callback message.
*/
void processAssetTagChangeCallback(sdbusplus::message_t& i_msg);
#endif

/**
Expand Down
12 changes: 12 additions & 0 deletions include/worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,15 @@ class Worker
void processEnabledProperty(const std::string& i_inventoryObjPath,
types::InterfaceMap& io_interfaces);

/**
* @brief API to form asset tag string for the system.
*
* @param[in] i_parsedVpdMap - Parsed VPD map.
* @return - Formed asset tag string. Empty in case of any error.
*/
std::string
createAssetTagString(const types::VPDMapVariant& i_parsedVpdMap);

// Parsed JSON file.
nlohmann::json m_parsedJson{};

Expand All @@ -462,6 +471,9 @@ class Worker
// collection. It just states, if the VPD collection process is over or not.
bool m_isAllFruCollected = false;

// To distinguish the factory reset path.
bool m_isFactoryResetDone = false;

// Mutex to guard critical resource m_activeCollectionThreadCount.
std::mutex m_mutex;
};
Expand Down
63 changes: 63 additions & 0 deletions src/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "utility/vpd_specific_utility.hpp"

#include <boost/asio/steady_timer.hpp>
#include <sdbusplus/bus/match.hpp>
#include <sdbusplus/message.hpp>

namespace vpd
Expand All @@ -33,6 +34,9 @@ Manager::Manager(
// Set up minimal things that is needed before bus name is claimed.
m_worker->performInitialSetup();

// set callback to detect any asset tag change
registerAssetChangeCallback();
SunnySrivastava1984 marked this conversation as resolved.
Show resolved Hide resolved

// set async timer to detect if system VPD is published on D-Bus.
SetTimerToDetectSVPDOnDbus();

Expand Down Expand Up @@ -116,6 +120,65 @@ Manager::Manager(
}

#ifdef IBM_SYSTEM
void Manager::registerAssetTagChangeCallback()
{
static std::shared_ptr<sdbusplus::bus::match_t> l_assetMatch =
std::make_shared<sdbusplus::bus::match_t>(
*m_asioConnection,
sdbusplus::bus::match::rules::propertiesChanged(
constants::systemInvPath, constants::assetTagInf),
[this](sdbusplus::message_t& l_msg) {
processAssetTagChangeCallback(l_msg)
});
}

void Manager::processAssetTagChangeCallback(sdbusplus::message_t& i_msg)
{
try
{
if (i_msg.is_method_error())
{
throw std::runtime_error(
"Error reading callback msg for asset tag.");
}

std::string l_objectPath;
types::PropertyMap l_propMap;
i_msg.read(l_objectPath, l_propMap);

const auto& l_itrToAssetTag = l_propMap.find("AssetTag");
if (l_itrToAssetTag != l_propMap.end())
{
if (auto l_assetTag =
std::get_if<std::string>(&(l_itrToAssetTag->second)))
{
// Call Notify to persist the AssetTag
types::ObjectMap l_objectMap = {
{sdbusplus::message::object_path(constants::systemInvPath),
{{constants::assetTagInf, {{"AssetTag", *l_assetTag}}}}}};

// Notify PIM
if (!dbusUtility::callPIM(move(l_objectMap)))
{
throw std::runtime_error(
"Call to PIM failed for asset tag update.");
}
}
}
else
{
throw std::runtime_error(
"Could not find asset tag in callback message.");
}
}
catch (const std::exception& l_ex)
{
// TODO: Log PEL with below description.
logging::logMessage("Asset tag callback update failed with error: " +
std::string(l_ex.what()));
}
}

void Manager::SetTimerToDetectSVPDOnDbus()
{
static boost::asio::steady_timer timer(*m_ioContext);
Expand Down
80 changes: 80 additions & 0 deletions src/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ void Worker::setDeviceTreeAndJson()
ec.message());
}
}

m_isFactoryResetDone = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will the above throw cause any issue to this value ? guess not , wanted your confirmation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, as of now, in case of throw above this place, we exit the service. Hence from our application point of view we have still not processed factory reset flow.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "m_isFactoryResetApplied" suits more

Copy link
Collaborator Author

@SunnySrivastava1984 SunnySrivastava1984 Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, Applied has a different meaning.
Done implies that an action has been finished. This variable denotes that when we are reaching this point of code, there has been a factory reset done in the system.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

}

// JSON is madatory for processing of this API.
Expand Down Expand Up @@ -1101,13 +1103,91 @@ void Worker::populateDbus(const types::VPDMapVariant& parsedVpdMap,
}
}

std::string
Worker::createAssetTagString(const types::VPDMapVariant& i_parsedVpdMap)
{
std::string l_assetTag;

try
{
// system VPD will be in IPZ format.
if (auto l_parsedVpdMap =
std::get_if<types::IPZVpdMap>(&i_parsedVpdMap))
{
auto l_itrToVsys = (*l_parsedVpdMap).find(constants::recVSYS);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can create this variable inside if condition .
As this variable is not being used outside anywhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"l_itrToVsys" is being used in the "if" condition itself.
We can't check it before creation.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant to create in the if condition itself.
but still its fine as its scope is limited.

if (l_itrToVsys != (*l_parsedVpdMap).end())
{
std::string l_tmKwdValue;
vpdSpecificUtility::getKwVal(l_itrToVsys->second,
constants::kwdTM, l_tmKwdValue);

std::string l_seKwdValue;
vpdSpecificUtility::getKwVal(l_itrToVsys->second,
constants::kwdSE, l_seKwdValue);

l_assetTag = std::string{"Server-"} + l_tmKwdValue +
std::string{"-"} + l_seKwdValue;
}
else
{
throw std::runtime_error(
"VSYS record not found in parsed VPD map to create Asset tag.");
}
}
else
{
throw std::runtime_error(
"Invalid VPD type recieved to create Asset tag.");
}
}
catch (const std::exception& l_ex)
{
// TODO:Log PEL with below description.
logging::logMessage("Asset tag can't be formed. Error = " +
std::string(l_ex.what()));
}

return l_assetTag;
}

void Worker::publishSystemVPD(const types::VPDMapVariant& parsedVpdMap)
{
types::ObjectMap objectInterfaceMap;

if (std::get_if<types::IPZVpdMap>(&parsedVpdMap))
{
populateDbus(parsedVpdMap, objectInterfaceMap, SYSTEM_VPD_FILE_PATH);

try
{
if (m_isFactoryResetDone)
jinuthomas marked this conversation as resolved.
Show resolved Hide resolved
{
const auto& l_assetTag = createAssetTagString(parsedVpdMap);

auto l_itrToSystemPath = objectInterfaceMap.find(
sdbusplus::message::object_path(constants::systemInvPath));
if (l_itrToSystemPath == objectInterfaceMap.end())
{
throw std::runtime_error(
"System Path not found in object map.");
}

types::PropertyMap l_assetTagProperty;
l_assetTagProperty.emplace("AssetTag", l_assetTag);

(l_itrToSystemPath->second)
.emplace(constants::assetTagInf,
std::move(l_assetTagProperty));
}
}
catch (const std::exception& l_ex)
{
// TODO Log PEL with below description
logging::logMessage(
"Asset tag update failed with following error: " +
jinuthomas marked this conversation as resolved.
Show resolved Hide resolved
std::string(l_ex.what()));
}

// Notify PIM
if (!dbusUtility::callPIM(move(objectInterfaceMap)))
{
Expand Down