blob: 3a805a50e2ed4b17c1e60679449a08442fa37e92 [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)
28 {
29 }
30
31 BCDTime(uint8_t yearMSB, uint8_t yearLSB, uint8_t month, uint8_t day,
32 uint8_t hour, uint8_t minutes, uint8_t seconds,
33 uint8_t hundredths) :
34 yearMSB(yearMSB),
35 yearLSB(yearLSB), month(month), day(day), hour(hour), minutes(minutes),
36 seconds(seconds), hundredths(hundredths)
37 {
38 }
39
Matt Spinlerdf797f22019-07-09 15:39:51 -050040 bool operator==(const BCDTime& right) const;
41 bool operator!=(const BCDTime& right) const;
42
43} __attribute__((packed));
44
45/**
46 * @brief Converts a time_point into a BCD time
47 *
48 * @param[in] time - the time_point to convert
49 * @return BCDTime - the BCD time
50 */
51BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time);
52
53/**
Matt Spinler5fa87f02019-08-27 16:31:57 -050054 * @brief Converts the number of milliseconds since the epoch into BCD time
55 *
56 * @param[in] milliseconds - Number of milliseconds since the epoch
57 * @return BCDTime - the BCD time
58 */
59BCDTime getBCDTime(uint64_t milliseconds);
60
61/**
Matt Spinlerdf797f22019-07-09 15:39:51 -050062 * @brief Converts a number to a BCD.
63 *
64 * For example 32 -> 0x32.
65 *
66 * Source: PLDM repository
67 *
68 * @param[in] value - the value to convert.
69 *
70 * @return T - the BCD value
71 */
72template <typename T>
73T toBCD(T decimal)
74{
75 T bcd = 0;
76 T remainder = 0;
77 auto count = 0;
78
79 while (decimal)
80 {
81 remainder = decimal % 10;
82 bcd = bcd + (remainder << count);
83 decimal = decimal / 10;
84 count += 4;
85 }
86
87 return bcd;
88}
89
90/**
91 * @brief Stream extraction operator for BCDTime
92 *
93 * @param[in] s - the Stream
94 * @param[out] time - the BCD time
95 *
96 * @return Stream&
97 */
98Stream& operator>>(Stream& s, BCDTime& time);
99
100/**
101 * @brief Stream insertion operator for BCDTime
102 *
103 * @param[in/out] s - the Stream
104 * @param[in] time - the BCD time
105 *
106 * @return Stream&
107 */
Matt Spinlerd7b004b2019-11-06 10:23:34 -0600108Stream& operator<<(Stream& s, const BCDTime& time);
Matt Spinlerdf797f22019-07-09 15:39:51 -0500109
110} // namespace pels
111} // namespace openpower