Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Andrew Jeffery | 4668f5c | 2024-01-15 14:59:17 +1030 | [diff] [blame] | 3 | #include <libpldm/instance-id.h> |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 4 | |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 5 | #include <cerrno> |
Andrew Jeffery | da4b13c | 2023-04-28 12:56:20 +0930 | [diff] [blame] | 6 | #include <cstdint> |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 7 | #include <exception> |
Eric Yang | 70262ed | 2025-07-02 06:35:03 +0000 | [diff] [blame^] | 8 | #include <expected> |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <system_error> |
| 11 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 12 | namespace pldm |
| 13 | { |
| 14 | |
Eric Yang | 70262ed | 2025-07-02 06:35:03 +0000 | [diff] [blame^] | 15 | /** |
| 16 | * @class InstanceIdError |
| 17 | * @brief Exception for PLDM instance ID allocation and management errors. |
| 18 | */ |
| 19 | class InstanceIdError : public std::exception |
| 20 | { |
| 21 | public: |
| 22 | InstanceIdError(const InstanceIdError&) noexcept = default; |
| 23 | InstanceIdError(InstanceIdError&&) noexcept = default; |
| 24 | InstanceIdError& operator=(const InstanceIdError&) noexcept = default; |
| 25 | InstanceIdError& operator=(InstanceIdError&&) noexcept = default; |
| 26 | ~InstanceIdError() noexcept override = default; |
| 27 | |
| 28 | /** @brief Construct with an error code. */ |
| 29 | explicit InstanceIdError(int rc) : rc_(rc), msg_(rcToMsg(rc)) {} |
| 30 | |
| 31 | /** @brief Construct with an error code and a string message (copied). */ |
| 32 | InstanceIdError(int rc, const std::string& m) : rc_(rc), msg_(m) {} |
| 33 | |
| 34 | /** @brief Construct with an error code and a string message (moved). */ |
| 35 | InstanceIdError(int rc, std::string&& m) : rc_(rc), msg_(std::move(m)) {} |
| 36 | |
| 37 | /** @brief Get the error code. */ |
| 38 | int rc() const noexcept |
| 39 | { |
| 40 | return rc_; |
| 41 | } |
| 42 | |
| 43 | /** @brief Get the error message. */ |
| 44 | const std::string& msg() const noexcept |
| 45 | { |
| 46 | return msg_; |
| 47 | } |
| 48 | |
| 49 | /** @brief Convert an error code to a message. */ |
| 50 | static std::string rcToMsg(int rc) |
| 51 | { |
| 52 | switch (rc) |
| 53 | { |
| 54 | case -EAGAIN: |
| 55 | return "No free instance ids"; |
| 56 | default: |
| 57 | return std::system_category().message(rc); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** @brief Get the error message (for std::exception). */ |
| 62 | const char* what() const noexcept override |
| 63 | { |
| 64 | return msg_.c_str(); |
| 65 | } |
| 66 | |
| 67 | private: |
| 68 | int rc_; |
| 69 | std::string msg_; |
| 70 | }; |
| 71 | |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 72 | /** @class InstanceId |
| 73 | * @brief Implementation of PLDM instance id as per DSP0240 v1.0.0 |
| 74 | */ |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 75 | class InstanceIdDb |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 76 | { |
| 77 | public: |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 78 | InstanceIdDb() |
| 79 | { |
| 80 | int rc = pldm_instance_db_init_default(&pldmInstanceIdDb); |
| 81 | if (rc) |
| 82 | { |
| 83 | throw std::system_category().default_error_condition(rc); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /** @brief Constructor |
| 88 | * |
| 89 | * @param[in] path - instance ID database path |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 90 | */ |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 91 | InstanceIdDb(const std::string& path) |
| 92 | { |
| 93 | int rc = pldm_instance_db_init(&pldmInstanceIdDb, path.c_str()); |
| 94 | if (rc) |
| 95 | { |
| 96 | throw std::system_category().default_error_condition(rc); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | ~InstanceIdDb() |
| 101 | { |
Andrew Jeffery | 68a525e | 2023-10-18 10:09:33 +1030 | [diff] [blame] | 102 | /* |
| 103 | * Abandon error-reporting. We shouldn't throw an exception from the |
| 104 | * destructor, and the class has multiple consumers using incompatible |
| 105 | * logging strategies. |
| 106 | * |
| 107 | * Broadly, it should be possible to use strace to investigate. |
| 108 | */ |
| 109 | pldm_instance_db_destroy(pldmInstanceIdDb); |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** @brief Allocate an instance ID for the given terminus |
| 113 | * @param[in] tid - the terminus ID the instance ID is associated with |
Eric Yang | 70262ed | 2025-07-02 06:35:03 +0000 | [diff] [blame^] | 114 | * @return - PLDM instance id on success, or InstanceIdError on failure |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 115 | */ |
Eric Yang | 70262ed | 2025-07-02 06:35:03 +0000 | [diff] [blame^] | 116 | std::expected<uint8_t, InstanceIdError> next(uint8_t tid) |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 117 | { |
| 118 | uint8_t id; |
| 119 | int rc = pldm_instance_id_alloc(pldmInstanceIdDb, tid, &id); |
| 120 | |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 121 | if (rc) |
| 122 | { |
Eric Yang | 70262ed | 2025-07-02 06:35:03 +0000 | [diff] [blame^] | 123 | return std::unexpected( |
| 124 | InstanceIdError{rc, "Failed to allocate instance ID for EID " + |
| 125 | std::to_string(tid) + ": " + |
| 126 | InstanceIdError::rcToMsg(rc)}); |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | return id; |
| 130 | } |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 131 | |
| 132 | /** @brief Mark an instance id as unused |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 133 | * @param[in] tid - the terminus ID the instance ID is associated with |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 134 | * @param[in] instanceId - PLDM instance id to be freed |
| 135 | */ |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 136 | void free(uint8_t tid, uint8_t instanceId) |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 137 | { |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 138 | int rc = pldm_instance_id_free(pldmInstanceIdDb, tid, instanceId); |
| 139 | if (rc == -EINVAL) |
| 140 | { |
Rashmica Gupta | f411742 | 2023-05-16 09:46:58 +1000 | [diff] [blame] | 141 | throw std::runtime_error( |
| 142 | "Instance ID " + std::to_string(instanceId) + " for TID " + |
| 143 | std::to_string(tid) + " was not previously allocated"); |
Andrew Jeffery | 7c1dc7e | 2023-04-28 14:52:16 +0930 | [diff] [blame] | 144 | } |
| 145 | if (rc) |
| 146 | { |
| 147 | throw std::system_category().default_error_condition(rc); |
| 148 | } |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | private: |
Rashmica Gupta | f411742 | 2023-05-16 09:46:58 +1000 | [diff] [blame] | 152 | pldm_instance_db* pldmInstanceIdDb = nullptr; |
Deepak Kodihalli | 4de4d00 | 2019-11-11 02:41:43 -0600 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | } // namespace pldm |