| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 1 | #pragma once | 
 | 2 | #include "stream.hpp" | 
 | 3 |  | 
 | 4 | #include <chrono> | 
 | 5 |  | 
 | 6 | namespace openpower | 
 | 7 | { | 
 | 8 | namespace pels | 
 | 9 | { | 
 | 10 |  | 
 | 11 | /** | 
 | 12 |  * @brief A structure that contains a PEL timestamp in BCD. | 
 | 13 |  */ | 
 | 14 | struct 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 |  */ | 
 | 36 | BCDTime getBCDTime(std::chrono::time_point<std::chrono::system_clock>& time); | 
 | 37 |  | 
 | 38 | /** | 
| Matt Spinler | 5fa87f0 | 2019-08-27 16:31:57 -0500 | [diff] [blame] | 39 |  * @brief Converts the number of milliseconds since the epoch into BCD time | 
 | 40 |  * | 
 | 41 |  * @param[in] milliseconds - Number of milliseconds since the epoch | 
 | 42 |  * @return BCDTime - the BCD time | 
 | 43 |  */ | 
 | 44 | BCDTime getBCDTime(uint64_t milliseconds); | 
 | 45 |  | 
 | 46 | /** | 
| Matt Spinler | df797f2 | 2019-07-09 15:39:51 -0500 | [diff] [blame] | 47 |  * @brief Converts a number to a BCD. | 
 | 48 |  * | 
 | 49 |  * For example 32 -> 0x32. | 
 | 50 |  * | 
 | 51 |  * Source: PLDM repository | 
 | 52 |  * | 
 | 53 |  * @param[in] value - the value to convert. | 
 | 54 |  * | 
 | 55 |  * @return T - the BCD value | 
 | 56 |  */ | 
 | 57 | template <typename T> | 
 | 58 | T toBCD(T decimal) | 
 | 59 | { | 
 | 60 |     T bcd = 0; | 
 | 61 |     T remainder = 0; | 
 | 62 |     auto count = 0; | 
 | 63 |  | 
 | 64 |     while (decimal) | 
 | 65 |     { | 
 | 66 |         remainder = decimal % 10; | 
 | 67 |         bcd = bcd + (remainder << count); | 
 | 68 |         decimal = decimal / 10; | 
 | 69 |         count += 4; | 
 | 70 |     } | 
 | 71 |  | 
 | 72 |     return bcd; | 
 | 73 | } | 
 | 74 |  | 
 | 75 | /** | 
 | 76 |  * @brief Stream extraction operator for BCDTime | 
 | 77 |  * | 
 | 78 |  * @param[in] s - the Stream | 
 | 79 |  * @param[out] time - the BCD time | 
 | 80 |  * | 
 | 81 |  * @return Stream& | 
 | 82 |  */ | 
 | 83 | Stream& operator>>(Stream& s, BCDTime& time); | 
 | 84 |  | 
 | 85 | /** | 
 | 86 |  * @brief Stream insertion operator for BCDTime | 
 | 87 |  * | 
 | 88 |  * @param[in/out] s - the Stream | 
 | 89 |  * @param[in] time - the BCD time | 
 | 90 |  * | 
 | 91 |  * @return Stream& | 
 | 92 |  */ | 
 | 93 | Stream& operator<<(Stream& s, BCDTime& time); | 
 | 94 |  | 
 | 95 | } // namespace pels | 
 | 96 | } // namespace openpower |