rk3399_arm_lvds/capimage/SysInforTool.cpp

182 lines
5.1 KiB
C++
Raw Permalink Normal View History

2024-03-05 03:46:18 +00:00
#pragma once
#include "SysInforTool.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/vfs.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <sys/utsname.h>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <iomanip>
extern int errno;
static void get_system_output(char *cmd, char *output, int size)
{
FILE *fp = NULL;
fp = popen(cmd, "r");
if (fp)
{
if (fgets(output, size, fp) != NULL)
{
if (output[strlen(output) - 1] == '\n')
output[strlen(output) - 1] = '\0';
}
pclose(fp);
}
}
/*block to kbyte*/
static unsigned long kscale(unsigned long m_block, unsigned long m_kbyte)
{
return ((unsigned long long)m_block * m_kbyte + 1024 / 2) / 1024;
}
/*convert size to GB MB KB*/
static void convert_size(float m_size, int *dest)
{
if ((((m_size / 1024.0) / 1024.0)) >= 1.0)
{
*dest = (int)((m_size / 1024.0) / 1024.0);
}
else if ((m_size / 1024.0) >= 1.0)
{
*dest = (int)(m_size / 1024);
}
}
SysInforTool::SysInforTool(ScannerSysInfo tinfo)
{
m_sysinfo = tinfo;
}
SysInforTool::~SysInforTool()
{
}
/*获取文件系统信息*/
int SysInforTool::get_fileSystem_info(const char *fileSystem_name, struct fileSystem_info *fi)
{
struct statfs buf;
float fileSystem_total_size = 0;
float fileSystem_free_size = 0;
if (statfs(fileSystem_name, &buf))
{
fprintf(stderr, "statfs %s\n", strerror(errno));
return -1;
}
switch (buf.f_type)
{
case 0xEF51:
case 0xEF53:
sprintf(fi->fileSystem_format, "EXT");
break;
case 0x4d44:
sprintf(fi->fileSystem_format, "FAT");
break;
case 0x5346544e:
sprintf(fi->fileSystem_format, "NIFS");
break;
default:
sprintf(fi->fileSystem_format, "unknown");
break;
}
// bzero(&fi->fileSystem_total_capacity,sizeof(fi->fileSystem_total_capacity));
// bzero(&fi->fileSystem_free_capacity,sizeof(fi->fileSystem_free_capacity));
printf("blocks %ld\n", buf.f_blocks);
printf("bfree %ld\n", buf.f_bfree);
printf("bsize %ld\n", buf.f_bsize);
fileSystem_total_size =
(float)(kscale(buf.f_blocks, buf.f_bsize));
fileSystem_free_size =
(float)(kscale(buf.f_bfree, buf.f_bsize));
printf("total %f\n", fileSystem_total_size);
printf("free %f\n", fileSystem_free_size);
fi->fileSystem_total_capacity = fileSystem_total_size;
fi->fileSystem_free_capacity = fileSystem_free_size;
// convert_size(fileSystem_total_size,(int*)(&fi->fileSystem_total_capacity));
// convert_size(fileSystem_free_size,(int*)(&fi->fileSystem_free_capacity));
bzero(fi->fileSystem_permissions, sizeof(fi->fileSystem_permissions));
sprintf(fi->fileSystem_permissions, "rw");
return 0;
}
std::string SysInforTool::GetSysInfo()
{
struct sysinfo memInfo;
sysinfo(&memInfo);
m_sysinfo.MemTotal = memInfo.totalram;
fileSystem_info fileinfo;
get_fileSystem_info("/", &fileinfo);
m_sysinfo.DiskTotal = fileinfo.fileSystem_total_capacity; // KB
m_sysinfo.DiskUsed = fileinfo.fileSystem_free_capacity; // KB
struct utsname t_uname;
if (uname(&t_uname) != 0)
perror("uname doesn't return 0, so there is an error");
printf("System Name = %s\n", t_uname.sysname);
printf("Node Name = %s\n", t_uname.nodename);
printf("Version = %s\n", t_uname.version);
printf("Release = %s\n", t_uname.release);
printf("Machine = %s\n", t_uname.machine);
if (strcmp(t_uname.nodename, "linaro-alip") == 0 && strcmp(t_uname.machine, "armv7l") == 0)
{
m_sysinfo.Systype = SysType::Sys_Linux_Debian;
m_sysinfo.CPU = SCPU::CPU_3288;
printf("Machine = %s CPU = %s \n", "Sys_Linux_Debian", "CPU_3288");
}
else if (strcmp(t_uname.nodename, "linaro-alip") == 0 && strcmp(t_uname.machine, "aarch64") == 0)
{
m_sysinfo.Systype = SysType::Sys_Linux_Debian;
m_sysinfo.CPU = SCPU::CPU_3399;
printf("Machine = %s CPU = %s \n", "Sys_Linux_Debian", "CPU_3399");
}
m_sysinfo.Cistype = HGCISType::CIS_DUNNAN_MONO_V0;
m_sysinfo.MtType = SMBType::MB_DRV_ANLU;
char output[512];
get_system_output("uname -a", output, sizeof(output));
std::string ver(output);
m_sysinfo.KernelVersion = ver;
printf("system version = %s \n", ver.c_str());
json j;
struct2json(j, m_sysinfo);
std::ofstream o("/usr/local/huago/sysinfo.json");
o << std::setw(4) << j << std::endl;
o.close();
return j.dump();
}
void SysInforTool::struct2json(json &j, ScannerSysInfo &info)
{
j["CPU"] = info.CPU;
j["Systype"] = info.Systype;
j["Screentype"] = info.Screentype;
j["MtBoardVersion"] = info.MtBoardVersion;
j["MtType"] = info.MtType;
j["FPGAVersion"] = info.FPGAVersion;
j["Cistype"] = info.Cistype;
j["MaxRes"] = info.ResSup;
j["MemTotal"] = info.MemTotal;
j["DiskTotal"] = info.DiskTotal;
j["DiskUsed"] = info.DiskUsed;
j["KernelVersion"] = info.KernelVersion;
j["Have_EthernPort"] = info.Have_EthernPort;
j["ServiceVersion"] = info.ServiceVersion;
j["UsbProtocol"] = info.UsbProtocol;
}