207 lines
6.2 KiB
C++
207 lines
6.2 KiB
C++
|
#pragma once
|
||
|
#include <linux/input.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <sys/ioctl.h>
|
||
|
#include <sys/epoll.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <unistd.h>
|
||
|
#include <stdio.h>
|
||
|
#include <poll.h>
|
||
|
#include <string.h>
|
||
|
#include <iostream>
|
||
|
#include <atomic>
|
||
|
#include <thread>
|
||
|
#include "jsonconfig.h"
|
||
|
|
||
|
|
||
|
#define dev_name "/dev/fv_en"
|
||
|
#define input_dev_name "/dev/input/event1"
|
||
|
class WakeUp
|
||
|
{
|
||
|
public:
|
||
|
WakeUp()
|
||
|
{
|
||
|
b_run=true;
|
||
|
sleep_falg = false;
|
||
|
update_state();
|
||
|
m_thread.reset(new std::thread(&WakeUp::input,this));
|
||
|
m_time =std::chrono::steady_clock::now();
|
||
|
sleeptime = Get_static_jsonconfig().getscannerinfo().sleeptime;
|
||
|
}
|
||
|
|
||
|
~WakeUp()
|
||
|
{
|
||
|
b_run=false;
|
||
|
if(m_thread.get()&&m_thread->joinable())
|
||
|
{
|
||
|
b_run=false;
|
||
|
m_thread->join();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void power_off(){
|
||
|
int fd=open(dev_name,O_RDWR);
|
||
|
if(fd == -1)
|
||
|
return;
|
||
|
char buf=0x55;
|
||
|
if((m_status == 1)&&releasecallback)
|
||
|
releasecallback();
|
||
|
write(fd,&buf,1);
|
||
|
close(fd);
|
||
|
m_status=0;
|
||
|
}
|
||
|
|
||
|
void power_on(){
|
||
|
int fd=open(dev_name,O_RDWR);
|
||
|
resettime();
|
||
|
if(fd == -1)
|
||
|
return;
|
||
|
char buf=0xaa;
|
||
|
write(fd,&buf,1);
|
||
|
close(fd);
|
||
|
if((m_status == 0)&&initcallback)
|
||
|
initcallback();
|
||
|
m_status=1;
|
||
|
}
|
||
|
|
||
|
int get_status()
|
||
|
{
|
||
|
return m_status;
|
||
|
}
|
||
|
|
||
|
int update_state()
|
||
|
{
|
||
|
int fd= open(dev_name,O_RDWR);
|
||
|
if(fd == -1)
|
||
|
return -1;
|
||
|
char buf=0;
|
||
|
read(fd,&buf,1);
|
||
|
if(buf==0x55)
|
||
|
m_status=0;
|
||
|
else
|
||
|
m_status=1;
|
||
|
close(fd);
|
||
|
return m_status;
|
||
|
}
|
||
|
|
||
|
void resettime()
|
||
|
{
|
||
|
m_time=std::chrono::steady_clock::now();
|
||
|
}
|
||
|
|
||
|
void settime(int val) {
|
||
|
sleeptime= val;
|
||
|
auto var = Get_static_jsonconfig().getscannerinfo();
|
||
|
var.sleeptime = val;
|
||
|
Get_static_jsonconfig().savescannerinfo(var);
|
||
|
resettime();
|
||
|
}
|
||
|
|
||
|
void setinitcallback(std::function<void()> init,std::function<void()> release){
|
||
|
this->initcallback = init;
|
||
|
this->releasecallback = release;
|
||
|
}
|
||
|
|
||
|
int gettime() {return sleeptime;}
|
||
|
|
||
|
bool getsleepfalg() {return sleep_falg;}
|
||
|
|
||
|
void setsleepfalg(bool value) {sleep_falg=value;}
|
||
|
|
||
|
private:
|
||
|
void input(){
|
||
|
int fd;
|
||
|
while (b_run)
|
||
|
{
|
||
|
fd = open(input_dev_name,O_RDWR | O_NONBLOCK);
|
||
|
if(fd<0){
|
||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||
|
//printf("\nopen /dev/input/event1 error.\n");
|
||
|
}
|
||
|
else
|
||
|
break;
|
||
|
}
|
||
|
struct input_id id;
|
||
|
unsigned int evbit[2];
|
||
|
struct input_event event;
|
||
|
struct pollfd fds[1];
|
||
|
nfds_t nfds = 1;
|
||
|
int err = ioctl(fd, EVIOCGID, &id);
|
||
|
int len = ioctl(fd, EVIOCGBIT(0, sizeof(evbit)), &evbit);
|
||
|
int ret=0;
|
||
|
while(b_run)
|
||
|
{
|
||
|
fds[0].fd = fd;
|
||
|
fds[0].events = POLLIN;
|
||
|
fds[0].revents = 0;
|
||
|
ret = poll(fds, nfds, 5000);
|
||
|
if (ret > 0)
|
||
|
{
|
||
|
if (fds[0].revents == POLLIN)
|
||
|
{
|
||
|
while (read(fd, &event, sizeof(event)) == sizeof(event))
|
||
|
{
|
||
|
printf("get event: type = 0x%x, code = 0x%x, value = 0x%x\n", event.type, event.code, event.value);
|
||
|
if(event.type == 0x01 && event.code == 0x3e && event.value == 0x01)
|
||
|
{
|
||
|
pressed_time = std::chrono::steady_clock::now();
|
||
|
}
|
||
|
if(event.type == 0x01 && event.code == 0x3e && event.value == 0x00)
|
||
|
{
|
||
|
if(std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - pressed_time).count() > 3000){
|
||
|
printf("wake up poweroff .\n");
|
||
|
system("poweroff");
|
||
|
}
|
||
|
if(std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - pressed_time).count() > 200)
|
||
|
{
|
||
|
int val= update_state();
|
||
|
// if(val == 1) {
|
||
|
// printf("system in sleep.\n");
|
||
|
// power_off();
|
||
|
// }
|
||
|
// else if(val == 0) {
|
||
|
if(val == 0) {
|
||
|
printf("system wakeup.\n");
|
||
|
power_on();
|
||
|
resettime();
|
||
|
}
|
||
|
else {
|
||
|
printf("error sleep index.\n");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else if (ret == 0)
|
||
|
{
|
||
|
//printf("wakeup_process.c::power key pressed time out...\n");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
printf("wakeup_process.c::poll err...\n");
|
||
|
}
|
||
|
if((sleeptime!=-1) && (std::chrono::duration<double>(std::chrono::steady_clock::now()-m_time).count()>sleeptime) && (m_status==1) && (!sleep_falg) )
|
||
|
{
|
||
|
if(update_state()==1)
|
||
|
power_off();
|
||
|
printf("\nwakeup_process sleep...\n");
|
||
|
}
|
||
|
//printf("\n wake up time ----- %f sleep_flag =%d sleeptime %d",std::chrono::duration<double>(std::chrono::steady_clock::now()-m_time).count(),sleep_falg,sleeptime);
|
||
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||
|
}
|
||
|
|
||
|
}
|
||
|
bool b_run;
|
||
|
int sleeptime;
|
||
|
bool sleep_falg;
|
||
|
volatile bool m_status;
|
||
|
std::function<void()> initcallback;
|
||
|
std::function<void()> releasecallback;
|
||
|
std::shared_ptr<std::thread> m_thread;
|
||
|
std::chrono::steady_clock::time_point m_time;
|
||
|
std::chrono::steady_clock::time_point pressed_time;
|
||
|
};
|