xref: /openbmc/libpldm/include/libpldm/utils.h (revision efb400697eede42f14ad6290660a4aecbc314141)
1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2 #ifndef LIBPLDM_UTILS_H
3 #define LIBPLDM_UTILS_H
4 
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
8 
9 #include <libpldm/pldm_types.h>
10 
11 #include <stdbool.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <sys/types.h>
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  */
31 uint8_t crc8(const void *data, size_t size);
32 
33 /** @brief Compute Crc32(same as the one used by IEEE802.3)
34  *
35  *  @param[in] data - Pointer to the target data
36  *  @param[in] size - Size of the data
37  *  @return The checksum
38  */
39 uint32_t crc32(const void *data, size_t size);
40 
41 /** @brief Convert ver32_t to string
42  *  @param[in] version - Pointer to ver32_t
43  *  @param[out] buffer - Pointer to the buffer
44  *  @param[in] buffer_size - Size of the buffer, up to SSIZE_MAX
45  *  @return The number of characters written to the buffer (excluding the null
46  * byte). The converted string may be truncated, and truncation is not
47  * considered an error. The result is negative if invalid arguments are supplied
48  * (NULL values for required pointers or the buffer size is beyond a
49  *  representable range).
50  */
51 ssize_t ver2str(const ver32_t *version, char *buffer, size_t buffer_size);
52 
53 /** @brief Convert bcd number(uint8_t) to decimal
54  *  @param[in] bcd - bcd number
55  *  @return the decimal number
56  */
57 uint8_t bcd2dec8(uint8_t bcd);
58 
59 /** @brief Convert decimal number(uint8_t) to bcd
60  *  @param[in] dec - decimal number
61  *  @return the bcd number
62  */
63 uint8_t dec2bcd8(uint8_t dec);
64 
65 /** @brief Convert bcd number(uint16_t) to decimal
66  *  @param[in] bcd - bcd number
67  *  @return the decimal number
68  */
69 uint16_t bcd2dec16(uint16_t bcd);
70 
71 /** @brief Convert decimal number(uint16_t) to bcd
72  *  @param[in] dec - decimal number
73  *  @return the bcd number
74  */
75 uint16_t dec2bcd16(uint16_t dec);
76 
77 /** @brief Convert bcd number(uint32_t) to decimal
78  *  @param[in] bcd - bcd number
79  *  @return the decimal number
80  */
81 uint32_t bcd2dec32(uint32_t bcd);
82 
83 /** @brief Convert decimal number(uint32_t) to bcd
84  *  @param[in] dec - decimal number
85  *  @return the bcd number
86  */
87 uint32_t dec2bcd32(uint32_t dec);
88 
89 /** @brief Check whether the input time is legal
90  *
91  *  @param[in] seconds. Value range 0~59
92  *  @param[in] minutes. Value range 0~59
93  *  @param[in] hours. Value range 0~23
94  *  @param[in] day. Value range 1~31
95  *  @param[in] month. Value range 1~12
96  *  @param[in] year. Value range 1970~
97  *  @return true if time is legal,false if time is illegal
98  */
99 bool is_time_legal(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day,
100 		   uint8_t month, uint16_t year);
101 
102 /** @brief Check whether transfer flag is valid
103  *
104  *  @param[in] transfer_flag - TransferFlag
105  *
106  *  @return true if transfer flag is valid, false if not
107  */
108 bool is_transfer_flag_valid(uint8_t transfer_flag);
109 
110 #ifdef __cplusplus
111 }
112 #endif
113 
114 #endif
115