blob: 463b2c435ffd7192bfd80155d5f53e1be01d2ebe [file] [log] [blame]
Gunnar Mills01a323b2017-01-18 09:48:13 -06001#include <iostream>
2#include <string>
3#include <sstream>
4#include <fstream>
5#include <stdexcept>
Gunnar Mills9b7c0b62017-04-24 12:59:58 -05006#include "bmc_version.hpp"
Gunnar Mills01a323b2017-01-18 09:48:13 -06007
8namespace phosphor
9{
10namespace software
11{
12namespace manager
13{
14
Gunnar Mills9b7c0b62017-04-24 12:59:58 -050015const std::string BMCVersion::getVersion() const
Gunnar Mills01a323b2017-01-18 09:48:13 -060016{
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 Mills9b7c0b62017-04-24 12:59:58 -050040const std::string BMCVersion::getId() const
Gunnar Mills01a323b2017-01-18 09:48:13 -060041{
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