JaiaBot 1.10.0+14+g8dbf2589
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
34#include "jaiabot/messages/metadata.pb.h"
35#include "jaiabot/version.h"
36
37namespace jaiabot
38{
39inline protobuf::DeviceMetadata metadata()
40{
41 protobuf::DeviceMetadata metadata;
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 {
75 std::array<char, 128> buffer;
76 std::string result;
77 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
78 if (!pipe)
79 {
80 throw std::runtime_error("popen() failed!");
81 }
82 while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
83 {
84 result += buffer.data();
85 }
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 if (version_info.count("JAIABOT_IMAGE_BUILD_DATE"))
157 metadata.set_jaiabot_image_build_date(version_info["JAIABOT_IMAGE_BUILD_DATE"]);
158 if (version_info.count("JAIABOT_FIRST_BOOT_DATE"))
159 metadata.set_jaiabot_image_first_boot_date(version_info["JAIABOT_FIRST_BOOT_DATE"]);
160 }
161
162 return metadata;
163}
164
165} // namespace jaiabot
166
167#endif
protobuf::DeviceMetadata metadata()
Definition metadata.h:39
#define MOOS_VERSION
Definition version.h:39
#define JAIABOT_VERSION_PATCH
Definition version.h:32
#define JAIABOT_VERSION_MAJOR
Definition version.h:30
#define JAIABOT_VERSION_MINOR
Definition version.h:31