blob: e2187564c3ff20db032580983ded35ab67b79880 [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
25 bool operator==(const BCDTime& right) const;
26 bool operator!=(const BCDTime& right) const;
27
28} __attribute__((packed));
29
30/**
31 * @brief Converts a time_point into a BCD time
32 *
33 * @param[in] time - the time_point to convert
34 * @return BCDTime - the BCD time
35 */
36BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time);
37
38/**
39 * @brief Converts a number to a BCD.
40 *
41 * For example 32 -> 0x32.
42 *
43 * Source: PLDM repository
44 *
45 * @param[in] value - the value to convert.
46 *
47 * @return T - the BCD value
48 */
49template <typename T>
50T toBCD(T decimal)
51{
52 T bcd = 0;
53 T remainder = 0;
54 auto count = 0;
55
56 while (decimal)
57 {
58 remainder = decimal % 10;
59 bcd = bcd + (remainder << count);
60 decimal = decimal / 10;
61 count += 4;
62 }
63
64 return bcd;
65}
66
67/**
68 * @brief Stream extraction operator for BCDTime
69 *
70 * @param[in] s - the Stream
71 * @param[out] time - the BCD time
72 *
73 * @return Stream&
74 */
75Stream& operator>>(Stream& s, BCDTime& time);
76
77/**
78 * @brief Stream insertion operator for BCDTime
79 *
80 * @param[in/out] s - the Stream
81 * @param[in] time - the BCD time
82 *
83 * @return Stream&
84 */
85Stream& operator<<(Stream& s, BCDTime& time);
86
87} // namespace pels
88} // namespace openpower