diff options
author | Patrick Venture <venture@google.com> | 2018-10-04 10:31:53 -0700 |
---|---|---|
committer | Patrick Venture <venture@google.com> | 2018-10-04 12:55:21 -0700 |
commit | b15b3050600f0201cd0fa81b88af270bb2720de5 (patch) | |
tree | a1016e981d5a0b34c181ad29dda266e2db0b7883 /process.cpp | |
parent | 03f415eaf563596368001a8b5149fa50e748407e (diff) | |
download | phosphor-ipmi-blobs-b15b3050600f0201cd0fa81b88af270bb2720de5.tar.gz phosphor-ipmi-blobs-b15b3050600f0201cd0fa81b88af270bb2720de5.zip |
process: rework lookup to use map for handlers
Switches lookup to use a map as adding another command makes the list
too long.
Change-Id: I35257ff5ae8c4c14c88aa2fa7d866642370ef38f
Signed-off-by: Patrick Venture <venture@google.com>
Diffstat (limited to 'process.cpp')
-rw-r--r-- | process.cpp | 57 |
1 files changed, 19 insertions, 38 deletions
diff --git a/process.cpp b/process.cpp index 595b5c2..b3939eb 100644 --- a/process.cpp +++ b/process.cpp @@ -19,6 +19,7 @@ #include "ipmi.hpp" #include <cstring> +#include <unordered_map> #include <vector> namespace blobs @@ -32,10 +33,22 @@ struct BmcRx uint8_t data; /* one byte minimum of data. */ } __attribute__((packed)); +static const std::unordered_map<BlobOEMCommands, IpmiBlobHandler> handlers = { + {BlobOEMCommands::bmcBlobGetCount, getBlobCount}, + {BlobOEMCommands::bmcBlobEnumerate, enumerateBlob}, + {BlobOEMCommands::bmcBlobOpen, openBlob}, + {BlobOEMCommands::bmcBlobRead, readBlob}, + {BlobOEMCommands::bmcBlobWrite, writeBlob}, + {BlobOEMCommands::bmcBlobCommit, commitBlob}, + {BlobOEMCommands::bmcBlobClose, closeBlob}, + {BlobOEMCommands::bmcBlobDelete, deleteBlob}, + {BlobOEMCommands::bmcBlobStat, statBlob}, + {BlobOEMCommands::bmcBlobSessionStat, sessionStatBlob}, +}; + IpmiBlobHandler validateBlobCommand(CrcInterface* crc, const uint8_t* reqBuf, uint8_t* replyCmdBuf, size_t* dataLen) { - IpmiBlobHandler cmd; size_t requestLength = (*dataLen); /* We know dataLen is at least 1 already */ auto command = static_cast<BlobOEMCommands>(reqBuf[0]); @@ -81,46 +94,14 @@ IpmiBlobHandler validateBlobCommand(CrcInterface* crc, const uint8_t* reqBuf, } } - /* Grab the corresponding handler for the command (could do map or array - * of function pointer lookup). - */ - switch (command) + /* Grab the corresponding handler for the command. */ + auto found = handlers.find(command); + if (found == handlers.end()) { - case BlobOEMCommands::bmcBlobGetCount: - cmd = getBlobCount; - break; - case BlobOEMCommands::bmcBlobEnumerate: - cmd = enumerateBlob; - break; - case BlobOEMCommands::bmcBlobOpen: - cmd = openBlob; - break; - case BlobOEMCommands::bmcBlobRead: - cmd = readBlob; - break; - case BlobOEMCommands::bmcBlobWrite: - cmd = writeBlob; - break; - case BlobOEMCommands::bmcBlobCommit: - cmd = commitBlob; - break; - case BlobOEMCommands::bmcBlobClose: - cmd = closeBlob; - break; - case BlobOEMCommands::bmcBlobDelete: - cmd = deleteBlob; - break; - case BlobOEMCommands::bmcBlobStat: - cmd = statBlob; - break; - case BlobOEMCommands::bmcBlobSessionStat: - cmd = sessionStatBlob; - break; - default: - return nullptr; + return nullptr; } - return cmd; + return found->second; } ipmi_ret_t processBlobCommand(IpmiBlobHandler cmd, ManagerInterface* mgr, |