John Wang | d8702f6 | 2019-10-31 10:06:35 +0800 | [diff] [blame] | 1 | #ifndef UTILS_H__ |
| 2 | #define UTILS_H__ |
| 3 | |
| 4 | #ifdef __cplusplus |
| 5 | extern "C" { |
| 6 | #endif |
| 7 | |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 8 | #include "pldm_types.h" |
Xiaochao Ma | 39ae2a9 | 2019-11-12 20:30:34 +0800 | [diff] [blame] | 9 | #include <stdbool.h> |
John Wang | d8702f6 | 2019-10-31 10:06:35 +0800 | [diff] [blame] | 10 | #include <stddef.h> |
| 11 | #include <stdint.h> |
George Liu | a940d8e | 2019-11-26 11:07:42 +0800 | [diff] [blame] | 12 | |
John Wang | c2d538c | 2019-12-20 11:07:52 +0800 | [diff] [blame] | 13 | /** @struct variable_field |
| 14 | * |
Tom Joseph | 763b51e | 2021-06-05 04:50:47 -0700 | [diff] [blame] | 15 | * Structure representing variable field in the pldm message |
John Wang | c2d538c | 2019-12-20 11:07:52 +0800 | [diff] [blame] | 16 | */ |
| 17 | struct variable_field { |
| 18 | const uint8_t *ptr; |
| 19 | size_t length; |
| 20 | }; |
| 21 | |
George Liu | a940d8e | 2019-11-26 11:07:42 +0800 | [diff] [blame] | 22 | /** @brief Compute Crc8(same as the one used by SMBUS) |
| 23 | * |
| 24 | * @param[in] data - Pointer to the target data |
| 25 | * @param[in] size - Size of the data |
| 26 | * @return The checksum |
| 27 | */ |
| 28 | uint8_t crc8(const void *data, size_t size); |
| 29 | |
John Wang | d8702f6 | 2019-10-31 10:06:35 +0800 | [diff] [blame] | 30 | /** @brief Compute Crc32(same as the one used by IEEE802.3) |
| 31 | * |
| 32 | * @param[in] data - Pointer to the target data |
| 33 | * @param[in] size - Size of the data |
| 34 | * @return The checksum |
| 35 | */ |
| 36 | uint32_t crc32(const void *data, size_t size); |
| 37 | |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 38 | /** @brief Convert ver32_t to string |
| 39 | * @param[in] version - Pointer to ver32_t |
| 40 | * @param[out] buffer - Pointer to the buffer |
| 41 | * @param[in] buffer_size - Size of the buffer |
| 42 | * @return The number of characters(excluding the null byte) or negative if |
| 43 | * error is encountered |
| 44 | */ |
| 45 | int ver2str(const ver32_t *version, char *buffer, size_t buffer_size); |
| 46 | |
John Wang | c2d538c | 2019-12-20 11:07:52 +0800 | [diff] [blame] | 47 | /** @brief Convert bcd number(uint8_t) to decimal |
John Wang | 5bdb30a | 2019-11-20 15:28:37 +0800 | [diff] [blame] | 48 | * @param[in] bcd - bcd number |
| 49 | * @return the decimal number |
| 50 | */ |
| 51 | uint8_t bcd2dec8(uint8_t bcd); |
| 52 | |
John Wang | c2d538c | 2019-12-20 11:07:52 +0800 | [diff] [blame] | 53 | /** @brief Convert decimal number(uint8_t) to bcd |
John Wang | 5bdb30a | 2019-11-20 15:28:37 +0800 | [diff] [blame] | 54 | * @param[in] dec - decimal number |
| 55 | * @return the bcd number |
| 56 | */ |
| 57 | uint8_t dec2bcd8(uint8_t dec); |
| 58 | |
John Wang | c2d538c | 2019-12-20 11:07:52 +0800 | [diff] [blame] | 59 | /** @brief Convert bcd number(uint16_t) to decimal |
John Wang | 5bdb30a | 2019-11-20 15:28:37 +0800 | [diff] [blame] | 60 | * @param[in] bcd - bcd number |
| 61 | * @return the decimal number |
| 62 | */ |
| 63 | uint16_t bcd2dec16(uint16_t bcd); |
| 64 | |
John Wang | c2d538c | 2019-12-20 11:07:52 +0800 | [diff] [blame] | 65 | /** @brief Convert decimal number(uint16_t) to bcd |
John Wang | 5bdb30a | 2019-11-20 15:28:37 +0800 | [diff] [blame] | 66 | * @param[in] dec - decimal number |
| 67 | * @return the bcd number |
| 68 | */ |
| 69 | uint16_t dec2bcd16(uint16_t dec); |
| 70 | |
John Wang | c2d538c | 2019-12-20 11:07:52 +0800 | [diff] [blame] | 71 | /** @brief Convert bcd number(uint32_t) to decimal |
John Wang | 5bdb30a | 2019-11-20 15:28:37 +0800 | [diff] [blame] | 72 | * @param[in] bcd - bcd number |
| 73 | * @return the decimal number |
| 74 | */ |
| 75 | uint32_t bcd2dec32(uint32_t bcd); |
| 76 | |
John Wang | c2d538c | 2019-12-20 11:07:52 +0800 | [diff] [blame] | 77 | /** @brief Convert decimal number(uint32_t) to bcd |
John Wang | 5bdb30a | 2019-11-20 15:28:37 +0800 | [diff] [blame] | 78 | * @param[in] dec - decimal number |
| 79 | * @return the bcd number |
| 80 | */ |
| 81 | uint32_t dec2bcd32(uint32_t dec); |
| 82 | |
Xiaochao Ma | 39ae2a9 | 2019-11-12 20:30:34 +0800 | [diff] [blame] | 83 | /** @brief Check whether the input time is legal |
| 84 | * |
| 85 | * @param[in] seconds. Value range 0~59 |
| 86 | * @param[in] minutes. Value range 0~59 |
| 87 | * @param[in] hours. Value range 0~23 |
| 88 | * @param[in] day. Value range 1~31 |
| 89 | * @param[in] month. Value range 1~12 |
| 90 | * @param[in] year. Value range 1970~ |
| 91 | * @return true if time is legal,false if time is illegal |
| 92 | */ |
| 93 | bool is_time_legal(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, |
| 94 | uint8_t month, uint16_t year); |
| 95 | |
gokulsanker | 1b909d8 | 2021-04-05 17:26:02 +0530 | [diff] [blame] | 96 | /** @brief Check whether transfer flag is valid |
| 97 | * |
| 98 | * @param[in] transfer_flag - TransferFlag |
| 99 | * |
| 100 | * @return true if transfer flag is valid, false if not |
| 101 | */ |
| 102 | bool is_transfer_flag_valid(uint8_t transfer_flag); |
| 103 | |
John Wang | d8702f6 | 2019-10-31 10:06:35 +0800 | [diff] [blame] | 104 | #ifdef __cplusplus |
| 105 | } |
| 106 | #endif |
| 107 | |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 108 | #endif |