JaiaBot  1.19.0
JaiaBot micro-AUV software
metadata.h
Go to the documentation of this file.
1 // Copyright 2024:
2 // JaiaRobotics LLC
3 // File authors:
4 // Toby Schneider <toby@gobysoft.org>
5 //
6 //
7 // This file is part of the JaiaBot Project Libraries
8 // ("The Jaia Libraries").
9 //
10 // The Jaia Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Jaia Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with the Jaia Libraries. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef JAIABOT_SRC_LIB_METADATA_H
24 #define JAIABOT_SRC_LIB_METADATA_H
25 
26 #include <fstream>
27 #include <iostream>
28 #include <regex>
29 
30 #include <boost/algorithm/string.hpp>
31 
32 #include <goby/version.h>
33 
35 #include "jaiabot/version.h"
36 
37 namespace jaiabot
38 {
40 {
42 
43  auto jaia_name_c = getenv("JAIA_DEVICE_NAME");
44  std::string jaia_device_name;
45  if (jaia_name_c)
46  {
47  jaia_device_name = std::string(jaia_name_c);
48  }
49  else
50  {
51  char buffer[256];
52  if (gethostname(buffer, 256) == 0)
53  {
54  jaia_device_name = std::string(buffer);
55  }
56  else
57  {
58  jaia_device_name = "<No Name>";
59  }
60  }
61 
62  metadata.set_name(jaia_device_name);
63  auto& jaiabot_version = *metadata.mutable_jaiabot_version();
64  jaiabot_version.set_major(JAIABOT_VERSION_MAJOR);
65  jaiabot_version.set_minor(JAIABOT_VERSION_MINOR);
66  jaiabot_version.set_patch(JAIABOT_VERSION_PATCH);
67 
68 #ifdef JAIABOT_VERSION_GITHASH
69  jaiabot_version.set_git_hash(JAIABOT_VERSION_GITHASH);
70  jaiabot_version.set_git_branch(JAIABOT_VERSION_GITBRANCH);
71 #endif
72 
73  auto execute_command = [](const char* cmd) -> std::string {
74  std::array<char, 128> buffer;
75  std::string result;
76  std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
77  if (!pipe)
78  {
79  throw std::runtime_error("popen() failed!");
80  }
81  while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
82  { result += buffer.data(); }
83  return result;
84  };
85 
86  // pull deb package info for jaiabot-embedded
87  {
88  std::string cmd = "apt-cache policy jaiabot-embedded";
89  std::string output = execute_command(cmd.c_str());
90 
91  std::regex installed_pattern(R"(Installed: (\S+))");
92  std::smatch installed_match;
93  if (std::regex_search(output, installed_match, installed_pattern))
94  {
95  std::string installed_version = installed_match[1];
96 
97  if (installed_version != "(none)")
98  {
99  // Perform regex match to extract "repo" and "release_branch"
100  std::regex repo_pattern(R"(packages\.jaia\.tech/ubuntu/([a-z]*)/([X0-9]+\.y*))");
101  std::smatch repo_match;
102  if (std::regex_search(output, repo_match, repo_pattern))
103  {
104  std::string repo = repo_match[1];
105  std::string release_branch = repo_match[2];
106 
107  jaiabot_version.set_deb_repository(repo);
108  jaiabot_version.set_deb_release_branch(release_branch);
109  }
110  }
111  }
112  }
113 
114  // pull deb package info for moos-ivp-apps
115  {
116  std::string cmd = "apt-cache policy moos-ivp-apps";
117  std::string output = execute_command(cmd.c_str());
118 
119  std::regex installed_pattern(R"(Installed: (\S+))");
120  std::smatch installed_match;
121  if (std::regex_search(output, installed_match, installed_pattern))
122  {
123  std::string installed_version = installed_match[1];
124 
125  if (installed_version != "(none)")
126  metadata.set_ivp_version(installed_version);
127  }
128  }
129 
132 
133  std::ifstream image_version_file("/etc/jaiabot/version");
134  if (image_version_file.is_open())
135  {
136  std::string line;
137  std::map<std::string, std::string> version_info;
138 
139  while (std::getline(image_version_file, line))
140  {
141  auto equal_pos = line.find('=');
142  if (equal_pos == std::string::npos)
143  continue;
144 
145  std::string value = line.substr(equal_pos + 1);
146  boost::trim_if(value, boost::is_any_of("\""));
147  version_info[line.substr(0, equal_pos)] = value;
148  }
149  if (version_info.count("JAIABOT_IMAGE_VERSION"))
150  metadata.set_jaiabot_image_version(version_info["JAIABOT_IMAGE_VERSION"]);
151  if (version_info.count("RASPI_FIRMWARE_VERSION"))
152  metadata.set_raspi_firmware_version(version_info["RASPI_FIRMWARE_VERSION"]);
153  if (version_info.count("JAIABOT_IMAGE_BUILD_DATE"))
154  metadata.set_jaiabot_image_build_date(version_info["JAIABOT_IMAGE_BUILD_DATE"]);
155  if (version_info.count("JAIABOT_FIRST_BOOT_DATE"))
156  metadata.set_jaiabot_image_first_boot_date(version_info["JAIABOT_FIRST_BOOT_DATE"]);
157  }
158 
159  return metadata;
160 }
161 
162 } // namespace jaiabot
163 
164 #endif
jaiabot::VERSION_STRING
constexpr const char * VERSION_STRING
Definition: version.h:43
jaiabot::metadata
protobuf::DeviceMetadata metadata()
Definition: metadata.h:39
JAIABOT_VERSION_MAJOR
#define JAIABOT_VERSION_MAJOR
Definition: version.h:30
jaiabot::protobuf::DeviceMetadata::mutable_jaiabot_version
::jaiabot::protobuf::DeviceMetadata_Version * mutable_jaiabot_version()
Definition: metadata.pb.h:1633
version.h
JAIABOT_VERSION_PATCH
#define JAIABOT_VERSION_PATCH
Definition: version.h:32
jaiabot::protobuf::DeviceMetadata
Definition: metadata.pb.h:461
jaiabot::protobuf::DeviceMetadata_Version::set_major
void set_major(const ::std::string &value)
Definition: metadata.pb.h:1089
jaiabot::protobuf::DeviceMetadata::set_raspi_firmware_version
void set_raspi_firmware_version(const ::std::string &value)
Definition: metadata.pb.h:2009
JAIABOT_VERSION_MINOR
#define JAIABOT_VERSION_MINOR
Definition: version.h:31
jaiabot::protobuf::DeviceMetadata::set_jaiabot_image_first_boot_date
void set_jaiabot_image_first_boot_date(const ::std::string &value)
Definition: metadata.pb.h:2207
jaiabot::protobuf::DeviceMetadata::set_jaiabot_image_build_date
void set_jaiabot_image_build_date(const ::std::string &value)
Definition: metadata.pb.h:2141
jaiabot::protobuf::DeviceMetadata::set_jaiabot_image_version
void set_jaiabot_image_version(const ::std::string &value)
Definition: metadata.pb.h:2075
jaiabot::protobuf::DeviceMetadata::set_moos_version
void set_moos_version(const ::std::string &value)
Definition: metadata.pb.h:1745
metadata.pb.h
jaiabot
Definition: config.pb.h:56
MOOS_VERSION
#define MOOS_VERSION
Definition: version.h:39
jaiabot::protobuf::DeviceMetadata::set_ivp_version
void set_ivp_version(const ::std::string &value)
Definition: metadata.pb.h:1811
jaiabot::protobuf::DeviceMetadata::set_goby_version
void set_goby_version(const ::std::string &value)
Definition: metadata.pb.h:1679
jaiabot::protobuf::DeviceMetadata::set_name
void set_name(const ::std::string &value)
Definition: metadata.pb.h:1555