zynq_7010/sleep_processes_code/main.cpp

108 lines
2.2 KiB
C++

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <thread>
#include"JsonConfig.h"
#define erroe_status -1
#define start_sleep 1
#define close_sleep 0
int readfile(int fd, char* buf,int num )
{
lseek(fd, 0, SEEK_SET);
int ret = read(fd, buf, num);
if (ret == -1)
{
printf("readfile error:%s\r\n",strerror(ret));
return erroe_status;
}
if (ret == 0)
{
printf("readfile error:%s\r\n","service connection is lost");
return erroe_status;
}
buf[1] = '\0';
printf("sleep processes len :%d\r\n",ret);
return atoi(buf);
}
void start_enter_lowpwoer(int time)
{
int seconds = time;
if(time < 0 || time < 180)
seconds = 180;
else
seconds = time;
std::string str="sh /mnt/flash-disk/countdown.sh start 0 0 "+std::to_string(seconds) + " &";
printf("start_enter_lowpwoer %s \n",str.c_str());
int ret = system(str.c_str());
}
void stop_countdown()
{
int ret = system("sh /mnt/flash-disk/killtimer.sh");
printf("system stop_countdown :%d\r\n",ret);
}
int main(int argc, char *argv[])
{
int fd = -1;
int ret = 0;
pollfd pfd;
if (access("/home/root/dev/", F_OK) == -1)
{
system("mkdir /home/root/dev/");
}
ret = mkfifo("/home/root/dev/sleep", 0666);
if(ret != 0)
{
printf("sleep processes mkfifo fial !!!\r\n");
}
fd = open("/home/root/dev/sleep", O_RDONLY);//等着只写
if(fd < 0)
{
printf("sleep processes open sleep fail!!!\n");
return -1;
}
printf("sleep processes open sleep succeed!!!\n");
char buffer[12];
JsonConfig js;
while (true)
{
ret = readfile(fd,buffer,sizeof(buffer));
printf("while read buf:[%d]\n", ret);
if (ret == -1)
{
system("sh /mnt/flash-disk/leds.sh 1");
sleep(3);
continue;
}
if (ret == 1)
{
ScannerScanInfo info = js.GetScanInfo();
stop_countdown();
sleep(3);
start_enter_lowpwoer(info.SleepTime);
}
else
stop_countdown();
}
close(fd);
return 0;
}