Skip to content

Commit

Permalink
move tools to lithium.cxxtools
Browse files Browse the repository at this point in the history
fix carbon compilation problem
  • Loading branch information
AstroAir committed May 26, 2024
1 parent 93eeb5f commit 5d69ebc
Show file tree
Hide file tree
Showing 65 changed files with 1,537 additions and 1,506 deletions.
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ find_package(fmt REQUIRED)
# 构建所有需要的依赖库
add_subdirectory(libs/)

# Build all tools
# 所有的工具组件
add_subdirectory(tools)

# Build Atom core library
# 构建Atom核心库
add_subdirectory(${lithium_module_dir})
Expand Down
2 changes: 1 addition & 1 deletion driver/client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
cmake_minimum_required(VERSION 3.20)
project(atom-client C CXX)

add_subdirectory(atom-hydrogen)
# add_subdirectory(atom-hydrogen)
51 changes: 24 additions & 27 deletions modules/atom.sysinfo/_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,33 @@ Description: A collector for system information, not the same as atom.system

#include "atom/log/loguru.hpp"

SysInfoComponent::SysInfoComponent(const std::string& name)
: Component(name) {
SysInfoComponent::SysInfoComponent(const std::string& name) : Component(name) {
LOG_F(INFO, "SysInfoComponent Constructed");

def("cpu_usage", &getCurrentCpuUsage, "cpu",
"Get current CPU usage percentage");
def("cpu_temperature", &getCurrentCpuTemperature, "cpu",
"Get current CPU temperature");
def("memory_usage", &getMemoryUsage, "memory",
"Get current memory usage percentage");
def("is_charging", &isBatteryCharging, PointerSentinel(this),
"battery", "Check if the battery is charging");
def("battery_level", &getCurrentBatteryLevel,
PointerSentinel(this), "battery",
"Get current battery level");
def("disk_usage", &getDiskUsage, "disk",
"Get current disk usage percentage");
def("is_hotspot_connected", &isHotspotConnected, "wifi",
"Check if the hotspot is connected");
def("wired_network", &getCurrentWiredNetwork, "wifi",
"Get current wired network");
def("wifi_name", &getCurrentWifi, "wifi",
"Get current wifi name");
def("current_ip", &getHostIPs, "network",
"Get current IP address");
def("gpu_info", &getGPUInfo, "gpu", "Get GPU info");
def("os_name", &getOSName, PointerSentinel(this), "os",
"Get OS name");
def("cpu_usage", &atom::system::getCurrentCpuUsage, "cpu",
"Get current CPU usage percentage");
def("cpu_temperature", &atom::system::getCurrentCpuTemperature, "cpu",
"Get current CPU temperature");
def("memory_usage", &atom::system::getMemoryUsage, "memory",
"Get current memory usage percentage");
def("is_charging", &isBatteryCharging, PointerSentinel(this), "battery",
"Check if the battery is charging");
def("battery_level", &getCurrentBatteryLevel, PointerSentinel(this),
"battery", "Get current battery level");
def("disk_usage", &atom::system::getDiskUsage, "disk",
"Get current disk usage percentage");
def("is_hotspot_connected", &atom::system::isHotspotConnected, "wifi",
"Check if the hotspot is connected");
def("wired_network", &atom::system::getCurrentWiredNetwork, "wifi",
"Get current wired network");
def("wifi_name", &atom::system::getCurrentWifi, "wifi",
"Get current wifi name");
def("current_ip", &atom::system::getHostIPs, "network",
"Get current IP address");
def("gpu_info", &atom::system::getGPUInfo, "gpu", "Get GPU info");
def("os_name", &getOSName, PointerSentinel(this), "os", "Get OS name");
def("os_version", &getOSVersion, PointerSentinel(this), "os",
"Get OS version");
"Get OS version");
}

SysInfoComponent::~SysInfoComponent() {
Expand Down
2 changes: 1 addition & 1 deletion modules/atom.sysinfo/src/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Description: System Information Module - Battery
**************************************************/

#include "battery.hpp"
#include "atom/sysinfo/battery.hpp"

#include <fstream>
#include <functional>
Expand Down
2 changes: 1 addition & 1 deletion modules/atom.sysinfo/src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Description: System Information Module - CPU
**************************************************/

#include "cpu.hpp"
#include "atom/sysinfo/cpu.hpp"

#include <cstdlib>
#include <filesystem>
Expand Down
103 changes: 47 additions & 56 deletions modules/atom.sysinfo/src/disk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ Description: System Information Module - Disk
**************************************************/

#include "disk.hpp"
#include "atom/sysinfo/disk.hpp"

#include "atom/log/loguru.hpp"

#include <array>
#include <fstream>
#include <ranges>
#include <span>
#include <sstream>

#ifdef _WIN32
#include <Windows.h>
#include <Psapi.h>
#include <intrin.h>
#include <iphlpapi.h>
#include <pdh.h>
#include <tlhelp32.h>
#include <wincon.h>
#include <windows.h>
#elif __linux__
#include <dirent.h>
#include <limits.h>
Expand All @@ -40,13 +37,7 @@ Description: System Information Module - Disk
#elif __APPLE__
#include <CoreFoundation/CoreFoundation.h>
#include <DiskArbitration/DiskArbitration.h>
#include <mach/mach_init.h>
#include <mach/task_info.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <mntent.h>
#endif

namespace atom::system {
Expand Down Expand Up @@ -105,32 +96,30 @@ std::vector<std::pair<std::string, float>> getDiskUsage() {
return disk_usage;
}

std::string getDriveModel(const std::string &drivePath) {
std::string getDriveModel(const std::string& drivePath) {
std::string model;

#ifdef _WIN32
HANDLE hDevice =
CreateFileA(drivePath.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
nullptr, OPEN_EXISTING, 0, nullptr);
if (hDevice != INVALID_HANDLE_VALUE) {
STORAGE_PROPERTY_QUERY query;
char buffer[1024];
ZeroMemory(&query, sizeof(query));
ZeroMemory(buffer, sizeof(buffer));
STORAGE_PROPERTY_QUERY query = {};
std::array<char, 1024> buffer = {};
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
DWORD bytesReturned = 0;
if (DeviceIoControl(hDevice, IOCTL_STORAGE_QUERY_PROPERTY, &query,
sizeof(query), buffer, sizeof(buffer),
&bytesReturned, NULL)) {
STORAGE_DEVICE_DESCRIPTOR *desc =
(STORAGE_DEVICE_DESCRIPTOR *)buffer;
char *vendorId = (char *)(buffer + desc->VendorIdOffset);
char *productId = (char *)(buffer + desc->ProductIdOffset);
char *productRevision =
(char *)(buffer + desc->ProductRevisionOffset);
model = vendorId + std::string(" ") + productId + std::string(" ") +
productRevision;
sizeof(query), buffer.data(), buffer.size(),
&bytesReturned, nullptr)) {
auto desc =
reinterpret_cast<STORAGE_DEVICE_DESCRIPTOR*>(buffer.data());
std::string_view vendorId(buffer.data() + desc->VendorIdOffset);
std::string_view productId(buffer.data() + desc->ProductIdOffset);
std::string_view productRevision(buffer.data() +
desc->ProductRevisionOffset);
model = std::string(vendorId) + " " + std::string(productId) + " " +
std::string(productRevision);
}
CloseHandle(hDevice);
}
Expand All @@ -149,8 +138,9 @@ std::string getDriveModel(const std::string &drivePath) {
if (disk != nullptr) {
CFDictionaryRef desc = DADiskCopyDescription(disk);
if (desc != nullptr) {
CFStringRef modelRef = (CFStringRef)CFDictionaryGetValue(
desc, kDADiskDescriptionDeviceModelKey);
CFStringRef modelRef =
static_cast<CFStringRef>(CFDictionaryGetValue(
desc, kDADiskDescriptionDeviceModelKey));
if (modelRef != nullptr) {
char buffer[256];
CFStringGetCString(modelRef, buffer, 256,
Expand All @@ -169,7 +159,6 @@ std::string getDriveModel(const std::string &drivePath) {
std::ifstream inFile("/sys/block/" + drivePath + "/device/model");
if (inFile.is_open()) {
std::getline(inFile, model);
inFile.close();
}
#endif

Expand All @@ -178,42 +167,45 @@ std::string getDriveModel(const std::string &drivePath) {

std::vector<std::pair<std::string, std::string>> getStorageDeviceModels() {
std::vector<std::pair<std::string, std::string>> storage_device_models;

#ifdef _WIN32
char driveStrings[1024];
DWORD length = GetLogicalDriveStringsA(sizeof(driveStrings), driveStrings);
if (length > 0 && length <= sizeof(driveStrings)) {
char *drive = driveStrings;
while (*drive) {
UINT driveType = GetDriveTypeA(drive);
std::array<char, 1024> driveStrings = {};
DWORD length =
GetLogicalDriveStringsA(driveStrings.size(), driveStrings.data());
if (length > 0 && length <= driveStrings.size()) {
std::span driveSpan(driveStrings.data(), length);
for (const auto& drive :
std::ranges::views::split(driveSpan, '\0') |
std::views::filter(
[](const std::span<char>& s) { return !s.empty(); })) {
std::string drivePath(drive.data(), drive.size());
UINT driveType = GetDriveTypeA(drivePath.c_str());
if (driveType == DRIVE_FIXED) {
std::string drivePath = drive;
std::string model = getDriveModel(drivePath);
if (!model.empty()) {
storage_device_models.push_back(
std::make_pair(drivePath, model));
storage_device_models.emplace_back(drivePath,
std::move(model));
}
}
drive += strlen(drive) + 1;
}
}
#else
DIR *dir = opendir("/sys/block/");
if (dir != NULL) {
struct dirent *ent;
while ((ent = readdir(dir)) != NULL) {
std::string deviceName = ent->d_name;
if (deviceName != "." && deviceName != "..") {
std::string devicePath = deviceName;
fs::path sysBlockDir("/sys/block/");
if (fs::exists(sysBlockDir) && fs::is_directory(sysBlockDir)) {
for (const auto& entry : fs::directory_iterator(sysBlockDir)) {
if (entry.is_directory() && entry.path().filename() != "." &&
entry.path().filename() != "..") {
std::string devicePath = entry.path().filename().string();
std::string model = getDriveModel(devicePath);
if (!model.empty()) {
storage_device_models.push_back(
std::make_pair(devicePath, model));
storage_device_models.emplace_back(devicePath,
std::move(model));
}
}
}
closedir(dir);
}
#endif

return storage_device_models;
}

Expand All @@ -230,10 +222,9 @@ std::vector<std::string> getAvailableDrives() {
drivesBitMask >>= 1;
}
#elif __linux__
// Linux 下不需要获取可用驱动器列表
drives.push_back("/");
#elif __APPLE__
struct statfs *mounts;
struct statfs* mounts;
int numMounts = getmntinfo(&mounts, MNT_NOWAIT);
for (int i = 0; i < numMounts; ++i) {
drives.push_back(mounts[i].f_mntonname);
Expand Down
2 changes: 1 addition & 1 deletion modules/atom.sysinfo/src/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Description: System Information Module - GPU
**************************************************/

#include "gpu.hpp"
#include "atom/sysinfo/gpu.hpp"

#ifdef _WIN32
#include <windows.h>
Expand Down
2 changes: 1 addition & 1 deletion modules/atom.sysinfo/src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Description: System Information Module - Memory
**************************************************/

#include "memory.hpp"
#include "atom/sysinfo/memory.hpp"

#include <fstream>
#include <sstream>
Expand Down
2 changes: 1 addition & 1 deletion modules/atom.sysinfo/src/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Description: System Information Module - Wifi Information
**************************************************/

#include "wifi.hpp"
#include "atom/sysinfo/wifi.hpp"

#include <string>
#include <vector>
Expand Down
44 changes: 44 additions & 0 deletions modules/lithium.cxxtools/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app


build
.vscode
node_modules

test

*.log
*.xml

.xmake
Loading

0 comments on commit 5d69ebc

Please sign in to comment.