Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : autoswitchoff.cpp
- // Author :
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <sys/un.h>
- #include <errno.h>
- #include <getopt.h>
- static struct option long_options[] =
- {
- {"help", no_argument, NULL, 'h'},
- {"lirc", required_argument, NULL, 'l'},
- {"time", required_argument, NULL, 't'},
- {0, 0, 0, 0}
- };
- int getTimer(int *time) {
- pid_t pid;
- int retTimer;
- int pipefd[2];
- char buf[128];
- char *sec, *pch;
- int ret;
- if (pipe(pipefd) == -1) {
- return(EXIT_FAILURE);
- }
- pid = fork();
- if (pid == 0)
- {
- close(pipefd[0]);
- dup2 (pipefd[1], STDOUT_FILENO);
- close (pipefd[1]);
- execl ("/usr/bin/perl", "perl", "/usr/local/bin/vdr-timer.pl", "test", "300", NULL);
- }
- if (pid == -1) {
- printf ("Error\n");
- return(EXIT_FAILURE);
- }
- else {
- close(pipefd[1]);
- waitpid(pid, &retTimer, 0);
- retTimer >>= 8;
- // printf("Status: %d\n", retTimer);
- ret = read(pipefd[0], &buf, sizeof(buf));
- close(pipefd[0]);
- if (retTimer == 0)
- return(0);
- else {
- *time = 300;
- return(1);
- }
- //else if (retTimer != 1)
- // return(-1);
- }
- sec = strstr(buf, "recording:");
- pch = strchr(sec, '\n');
- if (pch != NULL)
- *pch = '\0';
- *time = atoi(sec+10) + 60;
- if (*time <= 0)
- *time = 60;
- return(1);
- }
- int main(int argc,char *argv[])
- {
- int fd,i;
- char buf[128];
- struct sockaddr_un addr;
- int c;
- char progname[] = "autoswitchoff";
- unsigned int noActivity, nextCheck;
- char lircDev[128];
- int timer;
- fd_set rfds;
- struct timeval tv;
- int ret;
- int maxErr;
- addr.sun_family=AF_UNIX;
- strcpy(lircDev, "/var/run/lirc/lircd");
- noActivity = 120*60;
- while ((c = getopt_long(argc, argv, "hl:t:", long_options, NULL))
- != EOF) {
- switch (c){
- case 'h':
- printf("Usage: %s -l <lircd> -t <min>\n",argv[0]);
- printf("\t -h --help \t\tdisplay usage summary\n");
- printf("\t -l --lirc \t\tdefault: /var/run/lirc/lircd\n");
- printf("\t -t --time \t\tdefault: 120\n");
- return(EXIT_SUCCESS);
- case 'l':
- strcpy(lircDev, optarg);
- printf("lirc: %s\n", lircDev);
- break;
- case 't':
- noActivity = atoi(optarg);
- printf("Time: %d\n", noActivity);
- noActivity *= 60;
- break;
- case '?':
- fprintf(stderr, "unrecognized option: -%c\n", optopt);
- fprintf(stderr, "Try `%s --help' for more "
- "information.\n",
- progname);
- return(EXIT_FAILURE);
- }
- }
- if(argc == optind){
- /* no arguments */
- strcpy(addr.sun_path,lircDev);
- } else {
- fprintf(stderr, "%s: incorrect number of arguments.\n",
- progname);
- fprintf(stderr, "Try `%s --help' for more information.\n",
- progname);
- return(EXIT_FAILURE);
- }
- fd=socket(AF_UNIX,SOCK_STREAM,0);
- if(fd==-1) {
- perror("socket");
- printf("socket error\n");
- exit(errno);
- }
- maxErr = 0;
- while (maxErr < 20 && (connect(fd,(struct sockaddr *)&addr,sizeof(addr))==-1)) {
- printf("connect erorr %d\n", errno);
- sleep(1);
- maxErr++;
- }
- if (maxErr >= 20) {
- perror("connect");
- printf("connect erorr\n");
- exit(errno);
- }
- FD_ZERO(&rfds);
- FD_SET(fd, &rfds);
- // initial interval of no activity
- if (noActivity > 15*60)
- nextCheck = 15*60;
- else
- nextCheck = noActivity;
- maxErr = 0;
- while(true) {
- if (nextCheck == 0) {
- ret = getTimer(&timer);
- // timer active
- if (ret == 1) {
- nextCheck = timer;
- maxErr = 0;
- }
- // no timer
- else if (ret == 0)
- nextCheck = 0;
- // error
- else {
- nextCheck = 60;
- maxErr++;
- if (maxErr > 3)
- nextCheck = 0;
- }
- }
- if (nextCheck == 0) {
- close(fd);
- printf("shutting down ....\n");
- ret = system("sudo /usr/local/bin/runxbmc poweroff");
- exit(0);
- }
- tv.tv_sec = nextCheck;
- tv.tv_usec = 0;
- ret = select(fd+1, &rfds, NULL, NULL, &tv);
- if (ret == -1) {
- printf("Select error\n");
- nextCheck = 0;
- }
- else if (ret) {
- i=read(fd,buf,128);
- // write(STDOUT_FILENO,buf,i);
- nextCheck = noActivity;
- }
- else
- nextCheck = 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement