blob: 5679a5b4187c6ddf6bedc43fecde43809e8c6167 [file] [log] [blame]
Matt Spinlerdf797f22019-07-09 15:39:51 -05001#pragma once
2#include "stream.hpp"
3
4#include <chrono>
5
6namespace openpower
7{
8namespace pels
9{
10
11/**
12 * @brief A structure that contains a PEL timestamp in BCD.
13 */
14struct BCDTime
15{
16 uint8_t yearMSB;
17 uint8_t yearLSB;
18 uint8_t month;
19 uint8_t day;
20 uint8_t hour;
21 uint8_t minutes;
22 uint8_t seconds;
23 uint8_t hundredths;
24
Matt Spinler86c70362019-12-02 15:37:21 -060025 BCDTime() :
26 yearMSB(0), yearLSB(0), month(0), day(0), hour(0), minutes(0),
27 seconds(0), hundredths(0)
Patrick Williams2544b412022-10-04 08:41:06 -050028 {}
Matt Spinler86c70362019-12-02 15:37:21 -060029
30 BCDTime(uint8_t yearMSB, uint8_t yearLSB, uint8_t month, uint8_t day,
31 uint8_t hour, uint8_t minutes, uint8_t seconds,
32 uint8_t hundredths) :
33 yearMSB(yearMSB),
34 yearLSB(yearLSB), month(month), day(day), hour(hour), minutes(minutes),
35 seconds(seconds), hundredths(hundredths)
Patrick Williams2544b412022-10-04 08:41:06 -050036 {}
Matt Spinler86c70362019-12-02 15:37:21 -060037
Matt Spinlerdf797f22019-07-09 15:39:51 -050038 bool operator==(const BCDTime& right) const;
39 bool operator!=(const BCDTime& right) const;
40
41} __attribute__((packed));
42
43/**
44 * @brief Converts a time_point into a BCD time
45 *
46 * @param[in] time - the time_point to convert
47 * @return BCDTime - the BCD time
48 */
49BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time);
50
51/**
Matt Spinler5fa87f02019-08-27 16:31:57 -050052 * @brief Converts the number of milliseconds since the epoch into BCD time
53 *
54 * @param[in] milliseconds - Number of milliseconds since the epoch
55 * @return BCDTime - the BCD time
56 */
57BCDTime getBCDTime(uint64_t milliseconds);
58
59/**
Matt Spinlerdf797f22019-07-09 15:39:51 -050060 * @brief Converts a number to a BCD.
61 *
62 * For example 32 -> 0x32.
63 *
64 * Source: PLDM repository
65 *
66 * @param[in] value - the value to convert.
67 *
68 * @return T - the BCD value
69 */
70template <typename T>
71T toBCD(T decimal)
72{
73 T bcd = 0;
74 T remainder = 0;
75 auto count = 0;
76
77 while (decimal)
78 {
79 remainder = decimal % 10;
80 bcd = bcd + (remainder << count);
81 decimal = decimal / 10;
82 count += 4;
83 }
84
85 return bcd;
86}
87
88/**
89 * @brief Stream extraction operator for BCDTime
90 *
91 * @param[in] s - the Stream
92 * @param[out] time - the BCD time
93 *
94 * @return Stream&
95 */
96Stream& operator>>(Stream& s, BCDTime& time);
97
98/**
99 * @brief Stream insertion operator for BCDTime
100 *
101 * @param[in/out] s - the Stream
102 * @param[in] time - the BCD time
103 *
104 * @return Stream&
105 */
Matt Spinlerd7b004b2019-11-06 10:23:34 -0600106Stream& operator<<(Stream& s, const BCDTime& time);
Matt Spinlerdf797f22019-07-09 15:39:51 -0500107
108} // namespace pels
109} // namespace openpower