| Patrick Williams | 691668f | 2023-11-01 08:19:10 -0500 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
| Andrew Jeffery | 4edb708 | 2023-04-05 19:09:52 +0930 | [diff] [blame] | 2 | #ifndef LIBPLDM_UTILS_H |
| 3 | #define LIBPLDM_UTILS_H |
| Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 4 | |
| 5 | #ifdef __cplusplus |
| 6 | extern "C" { |
| 7 | #endif |
| 8 | |
| Andrew Jeffery | efb4006 | 2023-11-10 13:48:39 +1030 | [diff] [blame] | 9 | #include <libpldm/pldm_types.h> |
| 10 | |
| Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 11 | #include <stdbool.h> |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| Andrew Jeffery | a8b8a81 | 2023-04-05 14:32:12 +0930 | [diff] [blame] | 14 | #include <sys/types.h> |
| Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 15 | |
| 16 | /** @struct variable_field |
| 17 | * |
| 18 | * Structure representing variable field in the pldm message |
| 19 | */ |
| 20 | struct variable_field { |
| 21 | const uint8_t *ptr; |
| 22 | size_t length; |
| 23 | }; |
| 24 | |
| 25 | /** @brief Compute Crc8(same as the one used by SMBUS) |
| 26 | * |
| 27 | * @param[in] data - Pointer to the target data |
| 28 | * @param[in] size - Size of the data |
| 29 | * @return The checksum |
| 30 | */ |
| Andrew Jeffery | d0c9ae7 | 2025-04-05 20:11:02 +1030 | [diff] [blame] | 31 | uint8_t pldm_edac_crc8(const void *data, size_t size); |
| Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 32 | uint8_t crc8(const void *data, size_t size); |
| 33 | |
| Andrew Jeffery | a386348 | 2025-04-05 20:11:02 +1030 | [diff] [blame] | 34 | /** @brief Compute crc32 (same as the one used by IEEE802.3) |
| Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 35 | * |
| 36 | * @param[in] data - Pointer to the target data |
| 37 | * @param[in] size - Size of the data |
| 38 | * @return The checksum |
| 39 | */ |
| Andrew Jeffery | a386348 | 2025-04-05 20:11:02 +1030 | [diff] [blame] | 40 | uint32_t pldm_edac_crc32(const void *data, size_t size); |
| Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 41 | uint32_t crc32(const void *data, size_t size); |
| 42 | |
| 43 | /** @brief Convert ver32_t to string |
| 44 | * @param[in] version - Pointer to ver32_t |
| 45 | * @param[out] buffer - Pointer to the buffer |
| Andrew Jeffery | a8b8a81 | 2023-04-05 14:32:12 +0930 | [diff] [blame] | 46 | * @param[in] buffer_size - Size of the buffer, up to SSIZE_MAX |
| 47 | * @return The number of characters written to the buffer (excluding the null |
| 48 | * byte). The converted string may be truncated, and truncation is not |
| 49 | * considered an error. The result is negative if invalid arguments are supplied |
| 50 | * (NULL values for required pointers or the buffer size is beyond a |
| 51 | * representable range). |
| Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 52 | */ |
| Andrew Jeffery | a8b8a81 | 2023-04-05 14:32:12 +0930 | [diff] [blame] | 53 | ssize_t ver2str(const ver32_t *version, char *buffer, size_t buffer_size); |
| Andrew Jeffery | 9c76679 | 2022-08-10 23:12:49 +0930 | [diff] [blame] | 54 | |
| 55 | /** @brief Convert bcd number(uint8_t) to decimal |
| 56 | * @param[in] bcd - bcd number |
| 57 | * @return the decimal number |
| 58 | */ |
| 59 | uint8_t bcd2dec8(uint8_t bcd); |
| 60 | |
| 61 | /** @brief Convert decimal number(uint8_t) to bcd |
| 62 | * @param[in] dec - decimal number |
| 63 | * @return the bcd number |
| 64 | */ |
| 65 | uint8_t dec2bcd8(uint8_t dec); |
| 66 | |
| 67 | /** @brief Convert bcd number(uint16_t) to decimal |
| 68 | * @param[in] bcd - bcd number |
| 69 | * @return the decimal number |
| 70 | */ |
| 71 | uint16_t bcd2dec16(uint16_t bcd); |
| 72 | |
| 73 | /** @brief Convert decimal number(uint16_t) to bcd |
| 74 | * @param[in] dec - decimal number |
| 75 | * @return the bcd number |
| 76 | */ |
| 77 | uint16_t dec2bcd16(uint16_t dec); |
| 78 | |
| 79 | /** @brief Convert bcd number(uint32_t) to decimal |
| 80 | * @param[in] bcd - bcd number |
| 81 | * @return the decimal number |
| 82 | */ |
| 83 | uint32_t bcd2dec32(uint32_t bcd); |
| 84 | |
| 85 | /** @brief Convert decimal number(uint32_t) to bcd |
| 86 | * @param[in] dec - decimal number |
| 87 | * @return the bcd number |
| 88 | */ |
| 89 | uint32_t dec2bcd32(uint32_t dec); |
| 90 | |
| 91 | /** @brief Check whether the input time is legal |
| 92 | * |
| 93 | * @param[in] seconds. Value range 0~59 |
| 94 | * @param[in] minutes. Value range 0~59 |
| 95 | * @param[in] hours. Value range 0~23 |
| 96 | * @param[in] day. Value range 1~31 |
| 97 | * @param[in] month. Value range 1~12 |
| 98 | * @param[in] year. Value range 1970~ |
| 99 | * @return true if time is legal,false if time is illegal |
| 100 | */ |
| 101 | bool is_time_legal(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, |
| 102 | uint8_t month, uint16_t year); |
| 103 | |
| 104 | /** @brief Check whether transfer flag is valid |
| 105 | * |
| 106 | * @param[in] transfer_flag - TransferFlag |
| 107 | * |
| 108 | * @return true if transfer flag is valid, false if not |
| 109 | */ |
| 110 | bool is_transfer_flag_valid(uint8_t transfer_flag); |
| 111 | |
| 112 | #ifdef __cplusplus |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | #endif |