blob: 365fd6746355e70152822e89f6b582fa863f1a74 [file] [log] [blame]
Patrick Williams691668f2023-11-01 08:19:10 -05001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Andrew Jeffery4edb7082023-04-05 19:09:52 +09302#ifndef LIBPLDM_UTILS_H
3#define LIBPLDM_UTILS_H
Andrew Jeffery9c766792022-08-10 23:12:49 +09304
5#ifdef __cplusplus
6extern "C" {
7#endif
8
Andrew Jefferyefb40062023-11-10 13:48:39 +10309#include <libpldm/pldm_types.h>
10
Andrew Jeffery9c766792022-08-10 23:12:49 +093011#include <stdbool.h>
12#include <stddef.h>
13#include <stdint.h>
Andrew Jefferya8b8a812023-04-05 14:32:12 +093014#include <sys/types.h>
Andrew Jeffery9c766792022-08-10 23:12:49 +093015
16/** @struct variable_field
17 *
18 * Structure representing variable field in the pldm message
19 */
20struct 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 */
Andrew Jefferyd0c9ae72025-04-05 20:11:02 +103031uint8_t pldm_edac_crc8(const void *data, size_t size);
Andrew Jeffery9c766792022-08-10 23:12:49 +093032uint8_t crc8(const void *data, size_t size);
33
Andrew Jefferya3863482025-04-05 20:11:02 +103034/** @brief Compute crc32 (same as the one used by IEEE802.3)
Andrew Jeffery9c766792022-08-10 23:12:49 +093035 *
36 * @param[in] data - Pointer to the target data
37 * @param[in] size - Size of the data
38 * @return The checksum
39 */
Andrew Jefferya3863482025-04-05 20:11:02 +103040uint32_t pldm_edac_crc32(const void *data, size_t size);
Andrew Jeffery9c766792022-08-10 23:12:49 +093041uint32_t crc32(const void *data, size_t size);
42
43/** @brief Convert ver32_t to string
44 * @param[in] version - Pointer to ver32_t
45 * @param[out] buffer - Pointer to the buffer
Andrew Jefferya8b8a812023-04-05 14:32:12 +093046 * @param[in] buffer_size - Size of the buffer, up to SSIZE_MAX
47 * @return The number of characters written to the buffer (excluding the null
48 * byte). The converted string may be truncated, and truncation is not
49 * considered an error. The result is negative if invalid arguments are supplied
50 * (NULL values for required pointers or the buffer size is beyond a
51 * representable range).
Andrew Jeffery9c766792022-08-10 23:12:49 +093052 */
Andrew Jefferya8b8a812023-04-05 14:32:12 +093053ssize_t ver2str(const ver32_t *version, char *buffer, size_t buffer_size);
Andrew Jeffery9c766792022-08-10 23:12:49 +093054
55/** @brief Convert bcd number(uint8_t) to decimal
56 * @param[in] bcd - bcd number
57 * @return the decimal number
58 */
59uint8_t bcd2dec8(uint8_t bcd);
60
61/** @brief Convert decimal number(uint8_t) to bcd
62 * @param[in] dec - decimal number
63 * @return the bcd number
64 */
65uint8_t dec2bcd8(uint8_t dec);
66
67/** @brief Convert bcd number(uint16_t) to decimal
68 * @param[in] bcd - bcd number
69 * @return the decimal number
70 */
71uint16_t bcd2dec16(uint16_t bcd);
72
73/** @brief Convert decimal number(uint16_t) to bcd
74 * @param[in] dec - decimal number
75 * @return the bcd number
76 */
77uint16_t dec2bcd16(uint16_t dec);
78
79/** @brief Convert bcd number(uint32_t) to decimal
80 * @param[in] bcd - bcd number
81 * @return the decimal number
82 */
83uint32_t bcd2dec32(uint32_t bcd);
84
85/** @brief Convert decimal number(uint32_t) to bcd
86 * @param[in] dec - decimal number
87 * @return the bcd number
88 */
89uint32_t dec2bcd32(uint32_t dec);
90
91/** @brief Check whether the input time is legal
92 *
93 * @param[in] seconds. Value range 0~59
94 * @param[in] minutes. Value range 0~59
95 * @param[in] hours. Value range 0~23
96 * @param[in] day. Value range 1~31
97 * @param[in] month. Value range 1~12
98 * @param[in] year. Value range 1970~
99 * @return true if time is legal,false if time is illegal
100 */
101bool is_time_legal(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day,
102 uint8_t month, uint16_t year);
103
104/** @brief Check whether transfer flag is valid
105 *
106 * @param[in] transfer_flag - TransferFlag
107 *
108 * @return true if transfer flag is valid, false if not
109 */
110bool is_transfer_flag_valid(uint8_t transfer_flag);
111
112#ifdef __cplusplus
113}
114#endif
115
116#endif