2023-04-08 00:56:20 +00:00
# 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 ;
2023-04-23 03:50:17 +00:00
update_state ( ) ;
2023-04-08 00:56:20 +00:00
m_thread . reset ( new std : : thread ( & WakeUp : : input , this ) ) ;
m_time = std : : chrono : : steady_clock : : now ( ) ;
sleeptime = 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 )
2023-04-23 03:50:17 +00:00
releasecallback ( ) ;
2023-04-08 00:56:20 +00:00
write ( fd , & buf , 1 ) ;
close ( fd ) ;
m_status = 0 ;
}
void power_on ( ) {
int fd = open ( dev_name , O_RDWR ) ;
2023-04-23 03:50:17 +00:00
resettime ( ) ;
2023-04-08 00:56:20 +00:00
if ( fd = = - 1 )
return ;
char buf = 0xaa ;
write ( fd , & buf , 1 ) ;
close ( fd ) ;
2023-04-23 03:50:17 +00:00
if ( ( m_status = = 0 ) & & initcallback )
2023-04-08 00:56:20 +00:00
initcallback ( ) ;
m_status = 1 ;
}
int get_status ( )
2023-04-23 03:50:17 +00:00
{
return m_status ;
}
int update_state ( )
2023-04-08 00:56:20 +00:00
{
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 = jsonconfig ( ) . getscannerinfo ( ) ;
var . sleeptime = val ;
jsonconfig ( ) . savescannerinfo ( var ) ;
2023-04-23 03:50:17 +00:00
resettime ( ) ;
2023-04-08 00:56:20 +00:00
}
void setinitcallback ( std : : function < void ( ) > init , std : : function < void ( ) > release ) {
this - > initcallback = init ;
this - > releasecallback = release ;
2023-04-23 03:50:17 +00:00
}
2023-04-08 00:56:20 +00:00
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 ) ;
2023-04-23 03:50:17 +00:00
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 )
2023-04-08 00:56:20 +00:00
{
2023-04-23 03:50:17 +00:00
if ( std : : chrono : : duration < double , std : : milli > ( std : : chrono : : steady_clock : : now ( ) - pressed_time ) . count ( ) > 5000 ) {
printf ( " wake up poweroff . \n " ) ;
system ( " poweroff " ) ;
}
if ( std : : chrono : : duration < double , std : : milli > ( std : : chrono : : steady_clock : : now ( ) - pressed_time ) . count ( ) > 2000 )
{
int val = update_state ( ) ;
if ( val = = 1 ) {
printf ( " system in sleep. \n " ) ;
power_off ( ) ;
}
else if ( val = = 0 ) {
printf ( " system wakeup. \n " ) ;
power_on ( ) ;
resettime ( ) ;
}
else {
printf ( " error sleep index. \n " ) ;
}
}
2023-05-02 01:50:55 +00:00
2023-04-08 00:56:20 +00:00
}
}
}
}
else if ( ret = = 0 )
{
//printf("wakeup_process.c::power key pressed time out...\n");
}
else
{
printf ( " wakeup_process.c::poll err... \n " ) ;
}
2023-04-23 03:50:17 +00:00
if ( ( sleeptime ! = - 1 ) & & ( std : : chrono : : duration < double > ( std : : chrono : : steady_clock : : now ( ) - m_time ) . count ( ) > sleeptime ) & & ( m_status = = 1 ) & & ( ! sleep_falg ) )
2023-04-08 00:56:20 +00:00
{
2023-04-23 03:50:17 +00:00
if ( update_state ( ) = = 1 )
2023-04-08 00:56:20 +00:00
power_off ( ) ;
printf ( " \n wakeup_process sleep... \n " ) ;
}
2023-04-23 03:50:17 +00:00
//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);
2023-04-08 00:56:20 +00:00
std : : this_thread : : sleep_for ( std : : chrono : : milliseconds ( 500 ) ) ;
}
}
bool b_run ;
int sleeptime ;
bool sleep_falg ;
2023-04-23 03:50:17 +00:00
volatile bool m_status ;
2023-04-08 00:56:20 +00:00
std : : function < void ( ) > initcallback ;
std : : function < void ( ) > releasecallback ;
std : : shared_ptr < std : : thread > m_thread ;
std : : chrono : : steady_clock : : time_point m_time ;
2023-04-23 03:50:17 +00:00
std : : chrono : : steady_clock : : time_point pressed_time ;
2023-04-08 00:56:20 +00:00
} ;