198 lines
4.8 KiB
C++
198 lines
4.8 KiB
C++
#include "utilsfunc.h"
|
||
#include <unistd.h>
|
||
#include <memory>
|
||
#include <sys/types.h>
|
||
#include <sys/stat.h>
|
||
#include <fcntl.h>
|
||
#include <fstream>
|
||
using namespace std;
|
||
|
||
#define VMRSS_LINE 21
|
||
#define VMSIZE_LINE 17
|
||
#define PROCESS_ITEM 29
|
||
#define LUT_COLOR_BLACK_PATH "/usr/local/huago/lut_0.bmp"
|
||
#define LUT_COLOR_WHITE_PATH "/usr/local/huago/lut_1.bmp"
|
||
#define LUT_GRAY_BLACK_PATH "/usr/local/huago/lut_2.bmp"
|
||
#define LUT_GRAY_WHITE_PATH "/usr/local/huago/lut_3.bmp"
|
||
#define LUT_COLOR_LUT_PATH "/usr/local/huago/color_lut.bmp"
|
||
#define LUT_GRAY_LUT_PATH "/usr/local/huago/gray_lut.bmp"
|
||
|
||
float curve_coffes[2][3][3] = {
|
||
{
|
||
{-6e-5, 0.9928, 0.289},
|
||
{4e-5, 1.0045, -0.0338},
|
||
{3e-5, 1.0028, -0.2547}
|
||
},
|
||
{
|
||
{0.00025215105482649316 , 0.9162675232424231 , 2.2064225897716527},
|
||
{-6.242394612635725e-05 , 1.0212590446315797 , -0.7470581333124157},
|
||
{-0.0002324551311266845 , 1.072239346671703 , -1.7099159032971474}
|
||
}
|
||
};
|
||
|
||
void SaveScaninfo(const ScannerScanInfo &info)
|
||
{
|
||
js_config.SaveScanInfo(info);
|
||
}
|
||
|
||
ScannerScanInfo GetScanInfoFromJson()
|
||
{
|
||
return js_config.GetScanInfo();
|
||
}
|
||
|
||
void writesyslog(int loglevel, std::string loginfo)
|
||
{
|
||
#ifdef ENLOG_SYSLOG
|
||
openlog("scanservice", LOG_CONS | LOG_PID, LOG_USER);
|
||
syslog(loglevel, "%s \n", loginfo.c_str());
|
||
closelog();
|
||
#endif
|
||
}
|
||
|
||
unsigned int get_proc_virtualmem(unsigned int pid)
|
||
{
|
||
char file_name[64] = {0};
|
||
FILE *fd;
|
||
char line_buff[512] = {0};
|
||
sprintf(file_name, "/proc/%d/status", pid);
|
||
|
||
fd = fopen(file_name, "r");
|
||
if (nullptr == fd)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
char name[64];
|
||
int vmsize;
|
||
for (int i = 0; i < VMSIZE_LINE - 1; i++)
|
||
{
|
||
fgets(line_buff, sizeof(line_buff), fd);
|
||
}
|
||
|
||
fgets(line_buff, sizeof(line_buff), fd);
|
||
sscanf(line_buff, "%s %d", name, &vmsize);
|
||
fclose(fd);
|
||
|
||
return vmsize;
|
||
}
|
||
|
||
unsigned int get_proc_mem(unsigned int pid)
|
||
{
|
||
char file_name[64] = {0};
|
||
FILE *fd;
|
||
char line_buff[512] = {0};
|
||
sprintf(file_name, "/proc/%d/status", pid);
|
||
|
||
fd = fopen(file_name, "r");
|
||
if (nullptr == fd)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
char name[64];
|
||
int vmrss;
|
||
for (int i = 0; i < VMRSS_LINE - 1; i++)
|
||
{
|
||
fgets(line_buff, sizeof(line_buff), fd);
|
||
}
|
||
|
||
fgets(line_buff, sizeof(line_buff), fd);
|
||
sscanf(line_buff, "%s %d", name, &vmrss);
|
||
fclose(fd);
|
||
|
||
return vmrss;
|
||
}
|
||
|
||
int get_total_mem()
|
||
{
|
||
const char *file = "/proc/meminfo"; //文件名
|
||
FILE *fd; //定义文件指针fd
|
||
char line_buff[256] = {0}; //读取行的缓冲区
|
||
fd = fopen(file, "r"); //以R读的方式打开文件再赋给指针fd
|
||
|
||
//获取memtotal:总内存占用大小
|
||
int i;
|
||
char name[32]; //存放项目名称
|
||
int memtotal; //存放内存峰值大小
|
||
char *ret = fgets(line_buff, sizeof(line_buff), fd); //读取memtotal这一行的数据,memtotal在第1行
|
||
sscanf(line_buff, "%s %d", name, &memtotal);
|
||
//fprintf(stderr, "====%s:%d====\n", name, memtotal);
|
||
fclose(fd); //关闭文件fd
|
||
return memtotal;
|
||
}
|
||
|
||
std::string getmeminfo()
|
||
{
|
||
pid_t pid = getpid();
|
||
unsigned int virmem = get_proc_virtualmem(pid);
|
||
unsigned int procmem = get_proc_mem(pid);
|
||
int totalmem = get_total_mem();
|
||
std::string strinfo = "Scanservice current memory info,virtualmem= " + to_string(virmem) + " KB procmem = " + to_string(procmem) + " KB of " + to_string(totalmem) + " KB total";
|
||
return strinfo;
|
||
}
|
||
|
||
void start_enter_lowpwoer()
|
||
{
|
||
#ifdef ENABLE_LOWPOWERMODE
|
||
write_sleep_fifo(true);
|
||
#endif
|
||
}
|
||
|
||
void stop_countdown()
|
||
{
|
||
#ifdef ENABLE_LOWPOWERMODE
|
||
write_sleep_fifo(false);
|
||
#endif
|
||
}
|
||
|
||
|
||
int creat_sleep_fifo()
|
||
{
|
||
if (access("/home/root/dev/", F_OK) == -1)
|
||
{
|
||
system("mkdir /home/root/dev/");
|
||
}
|
||
int ret = mkfifo("/home/root/dev/sleep", 0666);
|
||
if (!ret)
|
||
{
|
||
perror("mkfifo");
|
||
}
|
||
|
||
sleep_fd_ = open("/home/root/dev/sleep", O_WRONLY);
|
||
if(sleep_fd_ < 0)
|
||
{
|
||
perror("open fifo fail");
|
||
}
|
||
return sleep_fd_;
|
||
}
|
||
|
||
int write_sleep_fifo(int status)
|
||
{
|
||
if(sleep_fd_ < 0)
|
||
{
|
||
perror("write_sleep_fifo open fifo fail");
|
||
return sleep_fd_;
|
||
}
|
||
|
||
char buffer[12];
|
||
sprintf(buffer,"%d",status);
|
||
//printf("status:%d\r\n",status);
|
||
int ret = write(sleep_fd_,buffer,strlen(buffer));
|
||
|
||
return ret;
|
||
}
|
||
|
||
//睡眠状态 //更新状态 等等
|
||
int get_devs_status(std::string filename)
|
||
{
|
||
std::ifstream in;
|
||
in.open(filename.c_str(),ios::in);
|
||
if (!in.is_open())
|
||
{
|
||
return -1;
|
||
}
|
||
|
||
char buf[1024] = { 0 };
|
||
in >> buf;
|
||
return atoi(buf);
|
||
} |