blob: df5b62a362874c1a1497b64dd34fa62b35330e84 [file] [log] [blame]
John Wangd8702f62019-10-31 10:06:35 +08001#ifndef UTILS_H__
2#define UTILS_H__
3
4#ifdef __cplusplus
5extern "C" {
6#endif
7
John Wang72136232019-11-14 15:33:42 +08008#include "pldm_types.h"
Xiaochao Ma39ae2a92019-11-12 20:30:34 +08009#include <stdbool.h>
John Wangd8702f62019-10-31 10:06:35 +080010#include <stddef.h>
11#include <stdint.h>
George Liua940d8e2019-11-26 11:07:42 +080012
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 */
19uint8_t crc8(const void *data, size_t size);
20
John Wangd8702f62019-10-31 10:06:35 +080021/** @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 */
27uint32_t crc32(const void *data, size_t size);
28
John Wang72136232019-11-14 15:33:42 +080029/** @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 */
36int ver2str(const ver32_t *version, char *buffer, size_t buffer_size);
37
John Wang5bdb30a2019-11-20 15:28:37 +080038/** @breif Convert bcd number(uint8_t) to decimal
39 * @param[in] bcd - bcd number
40 * @return the decimal number
41 */
42uint8_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 */
48uint8_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 */
54uint16_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 */
60uint16_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 */
66uint32_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 */
72uint32_t dec2bcd32(uint32_t dec);
73
Xiaochao Ma39ae2a92019-11-12 20:30:34 +080074/** @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 */
84bool is_time_legal(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day,
85 uint8_t month, uint16_t year);
86
John Wangd8702f62019-10-31 10:06:35 +080087#ifdef __cplusplus
88}
89#endif
90
John Wang72136232019-11-14 15:33:42 +080091#endif