|
| 1 | +/* |
| 2 | + * Copyright 2024 Toni500git |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the |
| 5 | + * following conditions are met: |
| 6 | + * |
| 7 | + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following |
| 8 | + * disclaimer. |
| 9 | + * |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the |
| 11 | + * following disclaimer in the documentation and/or other materials provided with the distribution. |
| 12 | + * |
| 13 | + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote |
| 14 | + * products derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 17 | + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 20 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 21 | + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 22 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +#include "platform.hpp" |
| 27 | +#if CF_ANDROID |
| 28 | + |
| 29 | +#include "query.hpp" |
| 30 | +#include "util.hpp" |
| 31 | + |
| 32 | +#include <unistd.h> |
| 33 | +#include <sys/stat.h> |
| 34 | +#include <array> |
| 35 | +#include <string_view> |
| 36 | + |
| 37 | +#include "../unix/utils/packages.hpp" |
| 38 | + |
| 39 | +using namespace Query; |
| 40 | + |
| 41 | +static System::System_t get_system_infos() |
| 42 | +{ |
| 43 | + System::System_t ret; |
| 44 | + |
| 45 | + ret.os_name = "Android"; |
| 46 | + ret.os_id = "android"; |
| 47 | + ret.os_version_id = get_android_property("ro.build.version.release"); |
| 48 | + ret.os_version_codename = get_android_property("ro.build.version.codename"); |
| 49 | + ret.os_pretty_name = "Android " + ret.os_version_codename + " " + ret.os_version_id; |
| 50 | + |
| 51 | + const std::array<std::string_view, 8> properties_name = {"ro.product.marketname", "ro.vendor.product.display", "ro.config.devicename", "ro.config.marketing_name", "ro.product.vendor.model", "ro.product.oppo_model", "ro.oppo.market.name", "ro.product.brand"}; |
| 52 | + for (const std::string_view name : properties_name) |
| 53 | + { |
| 54 | + if (ret.host_modelname.empty() || ret.host_modelname == UNKNOWN) |
| 55 | + ret.host_modelname = get_android_property(name); |
| 56 | + else |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + ret.host_vendor = get_android_property("ro.product.manufacturer"); |
| 61 | + ret.host_version = get_android_property("ro.product.model"); |
| 62 | + if (access("/system/bin/init", F_OK) == 0) |
| 63 | + { |
| 64 | + ret.os_initsys_name = "init"; |
| 65 | + ret.os_initsys_version.clear(); |
| 66 | + } |
| 67 | + |
| 68 | + return ret; |
| 69 | +} |
| 70 | + |
| 71 | +System::System() |
| 72 | +{ |
| 73 | + if (!m_bInit) |
| 74 | + { |
| 75 | + if (uname(&m_uname_infos) != 0) |
| 76 | + die("uname() failed: {}\nCould not get system infos", strerror(errno)); |
| 77 | + |
| 78 | + if (sysinfo(&m_sysInfos) != 0) |
| 79 | + die("sysinfo() failed: {}\nCould not get system infos", strerror(errno)); |
| 80 | + |
| 81 | + m_system_infos = get_system_infos(); |
| 82 | + } |
| 83 | + m_bInit = true; |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +// clang-format off |
| 88 | +std::string System::kernel_name() noexcept |
| 89 | +{ return m_uname_infos.sysname; } |
| 90 | + |
| 91 | +std::string System::kernel_version() noexcept |
| 92 | +{ return m_uname_infos.release; } |
| 93 | + |
| 94 | +std::string System::hostname() noexcept |
| 95 | +{ return m_uname_infos.nodename; } |
| 96 | + |
| 97 | +std::string System::arch() noexcept |
| 98 | +{ return m_uname_infos.machine; } |
| 99 | + |
| 100 | +long& System::uptime() noexcept |
| 101 | +{ return m_sysInfos.uptime; } |
| 102 | + |
| 103 | +std::string& System::os_pretty_name() noexcept |
| 104 | +{ return m_system_infos.os_pretty_name; } |
| 105 | + |
| 106 | +std::string& System::os_name() noexcept |
| 107 | +{ return m_system_infos.os_name; } |
| 108 | + |
| 109 | +std::string& System::os_id() noexcept |
| 110 | +{ return m_system_infos.os_id; } |
| 111 | + |
| 112 | +std::string& System::os_versionid() noexcept |
| 113 | +{ return m_system_infos.os_version_id; } |
| 114 | + |
| 115 | +std::string& System::os_version_codename() noexcept |
| 116 | +{ return m_system_infos.os_version_codename; } |
| 117 | + |
| 118 | +std::string& System::host_modelname() noexcept |
| 119 | +{ return m_system_infos.host_modelname; } |
| 120 | + |
| 121 | +std::string& System::host_vendor() noexcept |
| 122 | +{ return m_system_infos.host_vendor; } |
| 123 | + |
| 124 | +std::string& System::host_version() noexcept |
| 125 | +{ return m_system_infos.host_version; } |
| 126 | + |
| 127 | +std::string& System::os_initsys_name() |
| 128 | +{ return m_system_infos.os_initsys_name; } |
| 129 | + |
| 130 | +std::string& System::os_initsys_version() |
| 131 | +{ return m_system_infos.os_initsys_version; } |
| 132 | + |
| 133 | +std::string& System::pkgs_installed(const Config& config) |
| 134 | +{ |
| 135 | + static bool done = false; |
| 136 | + if (!done) |
| 137 | + { |
| 138 | + m_system_infos.pkgs_installed = get_all_pkgs(config); |
| 139 | + |
| 140 | + done = true; |
| 141 | + } |
| 142 | + |
| 143 | + return m_system_infos.pkgs_installed; |
| 144 | +} |
| 145 | + |
| 146 | +#endif |
0 commit comments