Gunnar Mills | 01a323b | 2017-01-18 09:48:13 -0600 | [diff] [blame] | 1 | #include <iostream> |
| 2 | #include <string> |
| 3 | #include <sstream> |
| 4 | #include <fstream> |
| 5 | #include <stdexcept> |
Gunnar Mills | 9b7c0b6 | 2017-04-24 12:59:58 -0500 | [diff] [blame] | 6 | #include "bmc_version.hpp" |
Gunnar Mills | 01a323b | 2017-01-18 09:48:13 -0600 | [diff] [blame] | 7 | |
| 8 | namespace phosphor |
| 9 | { |
| 10 | namespace software |
| 11 | { |
| 12 | namespace manager |
| 13 | { |
| 14 | |
Gunnar Mills | 9b7c0b6 | 2017-04-24 12:59:58 -0500 | [diff] [blame] | 15 | const std::string BMCVersion::getVersion() const |
Gunnar Mills | 01a323b | 2017-01-18 09:48:13 -0600 | [diff] [blame] | 16 | { |
| 17 | // Get version from /etc/os-release. |
| 18 | std::string versionKey = "VERSION_ID="; |
| 19 | std::string version{}; |
| 20 | std::ifstream efile; |
| 21 | std::string line; |
| 22 | efile.open("/etc/os-release"); |
| 23 | |
| 24 | while (getline(efile, line)) |
| 25 | { |
| 26 | if (line.substr(0, versionKey.size()).find(versionKey) |
| 27 | != std::string::npos) |
| 28 | { |
| 29 | // This line looks like VERSION_ID="v1.99.0-353-ga3b8a0a-dirty". |
| 30 | // So grab everything in quotes. |
| 31 | std::size_t pos = line.find_first_of('"') + 1; |
| 32 | version = line.substr(pos, line.find_last_of('"') - pos); |
| 33 | break; |
| 34 | } |
| 35 | } |
| 36 | efile.close(); |
| 37 | return version; |
| 38 | } |
| 39 | |
Gunnar Mills | 9b7c0b6 | 2017-04-24 12:59:58 -0500 | [diff] [blame] | 40 | const std::string BMCVersion::getId() const |
Gunnar Mills | 01a323b | 2017-01-18 09:48:13 -0600 | [diff] [blame] | 41 | { |
| 42 | auto version = getVersion(); |
| 43 | std::stringstream hexId; |
| 44 | |
| 45 | if (version.empty()) |
| 46 | { |
| 47 | throw std::runtime_error("Software version is empty"); |
| 48 | } |
| 49 | |
| 50 | // Only want 8 hex digits. |
| 51 | hexId << std::hex << ((std::hash<std::string> {}(version)) & 0xFFFFFFFF); |
| 52 | return hexId.str(); |
| 53 | } |
| 54 | |
| 55 | } // namespace manager |
| 56 | } // namespace software |
| 57 | } // namepsace phosphor |