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