blob: 35b039b535c55c6813fa1ad86b78eaa12475efc0 [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
John Wangc2d538c2019-12-20 11:07:52 +080013/** @struct variable_field
14 *
Tom Joseph763b51e2021-06-05 04:50:47 -070015 * Structure representing variable field in the pldm message
John Wangc2d538c2019-12-20 11:07:52 +080016 */
17struct variable_field {
18 const uint8_t *ptr;
19 size_t length;
20};
21
George Liua940d8e2019-11-26 11:07:42 +080022/** @brief Compute Crc8(same as the one used by SMBUS)
23 *
24 * @param[in] data - Pointer to the target data
25 * @param[in] size - Size of the data
26 * @return The checksum
27 */
28uint8_t crc8(const void *data, size_t size);
29
John Wangd8702f62019-10-31 10:06:35 +080030/** @brief Compute Crc32(same as the one used by IEEE802.3)
31 *
32 * @param[in] data - Pointer to the target data
33 * @param[in] size - Size of the data
34 * @return The checksum
35 */
36uint32_t crc32(const void *data, size_t size);
37
John Wang72136232019-11-14 15:33:42 +080038/** @brief Convert ver32_t to string
39 * @param[in] version - Pointer to ver32_t
40 * @param[out] buffer - Pointer to the buffer
41 * @param[in] buffer_size - Size of the buffer
42 * @return The number of characters(excluding the null byte) or negative if
43 * error is encountered
44 */
45int ver2str(const ver32_t *version, char *buffer, size_t buffer_size);
46
John Wangc2d538c2019-12-20 11:07:52 +080047/** @brief Convert bcd number(uint8_t) to decimal
John Wang5bdb30a2019-11-20 15:28:37 +080048 * @param[in] bcd - bcd number
49 * @return the decimal number
50 */
51uint8_t bcd2dec8(uint8_t bcd);
52
John Wangc2d538c2019-12-20 11:07:52 +080053/** @brief Convert decimal number(uint8_t) to bcd
John Wang5bdb30a2019-11-20 15:28:37 +080054 * @param[in] dec - decimal number
55 * @return the bcd number
56 */
57uint8_t dec2bcd8(uint8_t dec);
58
John Wangc2d538c2019-12-20 11:07:52 +080059/** @brief Convert bcd number(uint16_t) to decimal
John Wang5bdb30a2019-11-20 15:28:37 +080060 * @param[in] bcd - bcd number
61 * @return the decimal number
62 */
63uint16_t bcd2dec16(uint16_t bcd);
64
John Wangc2d538c2019-12-20 11:07:52 +080065/** @brief Convert decimal number(uint16_t) to bcd
John Wang5bdb30a2019-11-20 15:28:37 +080066 * @param[in] dec - decimal number
67 * @return the bcd number
68 */
69uint16_t dec2bcd16(uint16_t dec);
70
John Wangc2d538c2019-12-20 11:07:52 +080071/** @brief Convert bcd number(uint32_t) to decimal
John Wang5bdb30a2019-11-20 15:28:37 +080072 * @param[in] bcd - bcd number
73 * @return the decimal number
74 */
75uint32_t bcd2dec32(uint32_t bcd);
76
John Wangc2d538c2019-12-20 11:07:52 +080077/** @brief Convert decimal number(uint32_t) to bcd
John Wang5bdb30a2019-11-20 15:28:37 +080078 * @param[in] dec - decimal number
79 * @return the bcd number
80 */
81uint32_t dec2bcd32(uint32_t dec);
82
Xiaochao Ma39ae2a92019-11-12 20:30:34 +080083/** @brief Check whether the input time is legal
84 *
85 * @param[in] seconds. Value range 0~59
86 * @param[in] minutes. Value range 0~59
87 * @param[in] hours. Value range 0~23
88 * @param[in] day. Value range 1~31
89 * @param[in] month. Value range 1~12
90 * @param[in] year. Value range 1970~
91 * @return true if time is legal,false if time is illegal
92 */
93bool is_time_legal(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day,
94 uint8_t month, uint16_t year);
95
gokulsanker1b909d82021-04-05 17:26:02 +053096/** @brief Check whether transfer flag is valid
97 *
98 * @param[in] transfer_flag - TransferFlag
99 *
100 * @return true if transfer flag is valid, false if not
101 */
102bool is_transfer_flag_valid(uint8_t transfer_flag);
103
John Wangd8702f62019-10-31 10:06:35 +0800104#ifdef __cplusplus
105}
106#endif
107
John Wang72136232019-11-14 15:33:42 +0800108#endif