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 | |
| 13 | /** @brief Compute Crc8(same as the one used by SMBUS) |
| 14 | * |
| 15 | * @param[in] data - Pointer to the target data |
| 16 | * @param[in] size - Size of the data |
| 17 | * @return The checksum |
| 18 | */ |
| 19 | uint8_t crc8(const void *data, size_t size); |
| 20 | |
John Wang | d8702f6 | 2019-10-31 10:06:35 +0800 | [diff] [blame] | 21 | /** @brief Compute Crc32(same as the one used by IEEE802.3) |
| 22 | * |
| 23 | * @param[in] data - Pointer to the target data |
| 24 | * @param[in] size - Size of the data |
| 25 | * @return The checksum |
| 26 | */ |
| 27 | uint32_t crc32(const void *data, size_t size); |
| 28 | |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 29 | /** @brief Convert ver32_t to string |
| 30 | * @param[in] version - Pointer to ver32_t |
| 31 | * @param[out] buffer - Pointer to the buffer |
| 32 | * @param[in] buffer_size - Size of the buffer |
| 33 | * @return The number of characters(excluding the null byte) or negative if |
| 34 | * error is encountered |
| 35 | */ |
| 36 | int ver2str(const ver32_t *version, char *buffer, size_t buffer_size); |
| 37 | |
John Wang | 5bdb30a | 2019-11-20 15:28:37 +0800 | [diff] [blame] | 38 | /** @breif Convert bcd number(uint8_t) to decimal |
| 39 | * @param[in] bcd - bcd number |
| 40 | * @return the decimal number |
| 41 | */ |
| 42 | uint8_t bcd2dec8(uint8_t bcd); |
| 43 | |
| 44 | /** @breif Convert decimal number(uint8_t) to bcd |
| 45 | * @param[in] dec - decimal number |
| 46 | * @return the bcd number |
| 47 | */ |
| 48 | uint8_t dec2bcd8(uint8_t dec); |
| 49 | |
| 50 | /** @breif Convert bcd number(uint16_t) to decimal |
| 51 | * @param[in] bcd - bcd number |
| 52 | * @return the decimal number |
| 53 | */ |
| 54 | uint16_t bcd2dec16(uint16_t bcd); |
| 55 | |
| 56 | /** @breif Convert decimal number(uint16_t) to bcd |
| 57 | * @param[in] dec - decimal number |
| 58 | * @return the bcd number |
| 59 | */ |
| 60 | uint16_t dec2bcd16(uint16_t dec); |
| 61 | |
| 62 | /** @breif Convert bcd number(uint32_t) to decimal |
| 63 | * @param[in] bcd - bcd number |
| 64 | * @return the decimal number |
| 65 | */ |
| 66 | uint32_t bcd2dec32(uint32_t bcd); |
| 67 | |
| 68 | /** @breif Convert decimal number(uint32_t) to bcd |
| 69 | * @param[in] dec - decimal number |
| 70 | * @return the bcd number |
| 71 | */ |
| 72 | uint32_t dec2bcd32(uint32_t dec); |
| 73 | |
Xiaochao Ma | 39ae2a9 | 2019-11-12 20:30:34 +0800 | [diff] [blame] | 74 | /** @brief Check whether the input time is legal |
| 75 | * |
| 76 | * @param[in] seconds. Value range 0~59 |
| 77 | * @param[in] minutes. Value range 0~59 |
| 78 | * @param[in] hours. Value range 0~23 |
| 79 | * @param[in] day. Value range 1~31 |
| 80 | * @param[in] month. Value range 1~12 |
| 81 | * @param[in] year. Value range 1970~ |
| 82 | * @return true if time is legal,false if time is illegal |
| 83 | */ |
| 84 | bool is_time_legal(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, |
| 85 | uint8_t month, uint16_t year); |
| 86 | |
John Wang | d8702f6 | 2019-10-31 10:06:35 +0800 | [diff] [blame] | 87 | #ifdef __cplusplus |
| 88 | } |
| 89 | #endif |
| 90 | |
John Wang | 7213623 | 2019-11-14 15:33:42 +0800 | [diff] [blame] | 91 | #endif |