JaiaBot 2.1.1
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{
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
130 metadata.set_goby_version(goby::VERSION_STRING);
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
void set_major(ArgT0 &&arg0, ArgT... args)
::jaiabot::protobuf::DeviceMetadata_Version * mutable_jaiabot_version()
void set_moos_version(ArgT0 &&arg0, ArgT... args)
void set_jaiabot_image_first_boot_date(ArgT0 &&arg0, ArgT... args)
void set_ivp_version(ArgT0 &&arg0, ArgT... args)
void set_name(ArgT0 &&arg0, ArgT... args)
void set_jaiabot_image_build_date(ArgT0 &&arg0, ArgT... args)
void set_jaiabot_image_version(ArgT0 &&arg0, ArgT... args)
void set_raspi_firmware_version(ArgT0 &&arg0, ArgT... args)
void set_goby_version(ArgT0 &&arg0, ArgT... args)
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