JaiaBot 3.0.0
JaiaBot micro-AUV software
Loading...
Searching...
No Matches
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
37namespace jaiabot
38{
39namespace detail
40{
42{
44
45 auto jaia_name_c = getenv("JAIA_DEVICE_NAME");
46 std::string jaia_device_name;
47 if (jaia_name_c)
48 {
49 jaia_device_name = std::string(jaia_name_c);
50 }
51 else
52 {
53 char buffer[256];
54 if (gethostname(buffer, 256) == 0)
55 {
56 jaia_device_name = std::string(buffer);
57 }
58 else
59 {
60 jaia_device_name = "<No Name>";
61 }
62 }
63
64 metadata.set_name(jaia_device_name);
65 auto& jaiabot_version = *metadata.mutable_jaiabot_version();
66 jaiabot_version.set_major(JAIABOT_VERSION_MAJOR);
67 jaiabot_version.set_minor(JAIABOT_VERSION_MINOR);
68 jaiabot_version.set_patch(JAIABOT_VERSION_PATCH);
69
70#ifdef JAIABOT_VERSION_GITHASH
71 jaiabot_version.set_git_hash(JAIABOT_VERSION_GITHASH);
72 jaiabot_version.set_git_branch(JAIABOT_VERSION_GITBRANCH);
73#endif
74
75 auto execute_command = [](const char* cmd) -> std::string {
76 std::array<char, 128> buffer;
77 std::string result;
78 auto close_pipe = [](FILE* f) { pclose(f); };
79 std::unique_ptr<FILE, decltype(close_pipe)> pipe(popen(cmd, "r"), close_pipe);
80 if (!pipe)
81 {
82 throw std::runtime_error("popen() failed!");
83 }
84 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
85 { result += buffer.data(); }
86 return result;
87 };
88
89 // pull deb package info for jaiabot-embedded
90 {
91 std::string cmd = "apt-cache policy jaiabot-embedded";
92 std::string output = execute_command(cmd.c_str());
93
94 std::regex installed_pattern(R"(Installed: (\S+))");
95 std::smatch installed_match;
96 if (std::regex_search(output, installed_match, installed_pattern))
97 {
98 std::string installed_version = installed_match[1];
99
100 if (installed_version != "(none)")
101 {
102 // Perform regex match to extract "repo" and "release_branch"
103 std::regex repo_pattern(R"(packages\.jaia\.tech/ubuntu/([a-z]*)/([X0-9]+\.y*))");
104 std::smatch repo_match;
105 if (std::regex_search(output, repo_match, repo_pattern))
106 {
107 std::string repo = repo_match[1];
108 std::string release_branch = repo_match[2];
109
110 jaiabot_version.set_deb_repository(repo);
111 jaiabot_version.set_deb_release_branch(release_branch);
112 }
113 }
114 }
115 }
116
117 // pull deb package info for moos-ivp-apps
118 {
119 std::string cmd = "apt-cache policy moos-ivp-apps";
120 std::string output = execute_command(cmd.c_str());
121
122 std::regex installed_pattern(R"(Installed: (\S+))");
123 std::smatch installed_match;
124 if (std::regex_search(output, installed_match, installed_pattern))
125 {
126 std::string installed_version = installed_match[1];
127
128 if (installed_version != "(none)")
129 metadata.set_ivp_version(installed_version);
130 }
131 }
132
133 metadata.set_goby_version(goby::VERSION_STRING);
134 metadata.set_moos_version(MOOS_VERSION);
135
136 std::ifstream image_version_file("/etc/jaiabot/version");
137 if (image_version_file.is_open())
138 {
139 std::string line;
140 std::map<std::string, std::string> version_info;
141
142 while (std::getline(image_version_file, line))
143 {
144 auto equal_pos = line.find('=');
145 if (equal_pos == std::string::npos)
146 continue;
147
148 std::string value = line.substr(equal_pos + 1);
149 boost::trim_if(value, boost::is_any_of("\""));
150 version_info[line.substr(0, equal_pos)] = value;
151 }
152 if (version_info.count("JAIABOT_IMAGE_VERSION"))
153 metadata.set_jaiabot_image_version(version_info["JAIABOT_IMAGE_VERSION"]);
154 if (version_info.count("RASPI_FIRMWARE_VERSION"))
155 metadata.set_raspi_firmware_version(version_info["RASPI_FIRMWARE_VERSION"]);
156
157 // Read EEPROM version via rpi-eeprom-update -j
158 try
159 {
160 std::string eeprom_json =
161 execute_command("rpi-eeprom-update -j -m /dev/stderr 2>&1 1>/dev/null");
162 std::regex bootloader_pattern(R"(\"BOOTLOADER_CURRENT\"\s*:\s*([0-9]+))");
163 std::smatch bootloader_match;
164 if (std::regex_search(eeprom_json, bootloader_match, bootloader_pattern))
165 {
166 metadata.set_raspi_eeprom_version(std::stoll(bootloader_match[1]));
167 }
168 }
169 catch (...)
170 {
171 }
172 if (version_info.count("JAIABOT_IMAGE_BUILD_DATE"))
173 metadata.set_jaiabot_image_build_date(version_info["JAIABOT_IMAGE_BUILD_DATE"]);
174 if (version_info.count("JAIABOT_FIRST_BOOT_DATE"))
175 metadata.set_jaiabot_image_first_boot_date(version_info["JAIABOT_FIRST_BOOT_DATE"]);
176 }
177
178 return metadata;
179}
180} // namespace detail
181
182// Reading this shells out to apt-cache twice and to rpi-eeprom-update, which blocks the
183// calling app well past the window goby_coroner allows for a health response, and it is
184// then reported as a died process. None of it changes while the process runs, since a
185// package upgrade restarts the apps. The first call pays that cost, so call it once
186// before joining the middleware rather than letting a query decide when it happens.
188{
189 static const protobuf::DeviceMetadata cached = detail::read_metadata();
190 return cached;
191}
192
193} // namespace jaiabot
194
195#endif
protobuf::DeviceMetadata read_metadata()
Definition metadata.h:41
protobuf::DeviceMetadata metadata()
Definition metadata.h:187
#define MOOS_VERSION
Definition version.h:41
#define JAIABOT_VERSION_PATCH
Definition version.h:34
#define JAIABOT_VERSION_MAJOR
Definition version.h:32
#define JAIABOT_VERSION_MINOR
Definition version.h:33