Advertisement
Guest User

autoswitchoff.cpp

a guest
Mar 9th, 2011
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.43 KB | None | 0 0
  1. //============================================================================
  2. // Name        : autoswitchoff.cpp
  3. // Author      :
  4. // Version     :
  5. // Copyright   : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <fcntl.h>
  14. #include <sys/socket.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <sys/wait.h>
  18. #include <sys/un.h>
  19. #include <errno.h>
  20. #include <getopt.h>
  21.  
  22. static struct option long_options[] =
  23. {
  24.     {"help", no_argument, NULL, 'h'},
  25.     {"lirc", required_argument, NULL, 'l'},
  26.     {"time", required_argument, NULL, 't'},
  27.     {0, 0, 0, 0}
  28. };
  29.  
  30. int getTimer(int *time) {
  31.     pid_t pid;
  32.     int retTimer;
  33.     int pipefd[2];
  34.     char buf[128];
  35.     char *sec, *pch;
  36.     int ret;
  37.  
  38.     if (pipe(pipefd) == -1) {
  39.         return(EXIT_FAILURE);
  40.     }
  41.     pid = fork();
  42.  
  43.     if (pid == 0)
  44.     {
  45.         close(pipefd[0]);
  46.         dup2 (pipefd[1], STDOUT_FILENO);
  47.         close (pipefd[1]);
  48.         execl ("/usr/bin/perl", "perl", "/usr/local/bin/vdr-timer.pl", "test", "300", NULL);
  49.     }
  50.     if (pid == -1) {
  51.         printf ("Error\n");
  52.         return(EXIT_FAILURE);
  53.     }
  54.     else {
  55.         close(pipefd[1]);
  56.         waitpid(pid, &retTimer, 0);
  57.         retTimer >>= 8;
  58.         // printf("Status: %d\n", retTimer);
  59.         ret = read(pipefd[0], &buf, sizeof(buf));
  60.         close(pipefd[0]);
  61.         if (retTimer == 0)
  62.             return(0);
  63.         else {
  64.             *time = 300;
  65.             return(1);
  66.         }
  67.  
  68.         //else if (retTimer != 1)
  69.         //  return(-1);
  70.     }
  71.  
  72.     sec = strstr(buf, "recording:");
  73.     pch = strchr(sec, '\n');
  74.     if (pch != NULL)
  75.         *pch = '\0';
  76.     *time = atoi(sec+10) + 60;
  77.     if (*time <= 0)
  78.         *time = 60;
  79.  
  80.     return(1);
  81.  
  82. }
  83.  
  84. int main(int argc,char *argv[])
  85. {
  86.     int fd,i;
  87.     char buf[128];
  88.     struct sockaddr_un addr;
  89.     int c;
  90.     char progname[] = "autoswitchoff";
  91.     unsigned int noActivity, nextCheck;
  92.     char lircDev[128];
  93.     int timer;
  94.     fd_set rfds;
  95.     struct timeval tv;
  96.     int ret;
  97.     int maxErr;
  98.  
  99.     addr.sun_family=AF_UNIX;
  100.     strcpy(lircDev, "/var/run/lirc/lircd");
  101.     noActivity = 120*60;
  102.  
  103.     while ((c = getopt_long(argc, argv, "hl:t:", long_options, NULL))
  104.            != EOF) {
  105.         switch (c){
  106.         case 'h':
  107.             printf("Usage: %s -l <lircd> -t <min>\n",argv[0]);
  108.             printf("\t -h --help \t\tdisplay usage summary\n");
  109.             printf("\t -l --lirc \t\tdefault: /var/run/lirc/lircd\n");
  110.             printf("\t -t --time \t\tdefault: 120\n");
  111.             return(EXIT_SUCCESS);
  112.         case 'l':
  113.             strcpy(lircDev, optarg);
  114.             printf("lirc: %s\n", lircDev);
  115.             break;
  116.         case 't':
  117.             noActivity = atoi(optarg);
  118.             printf("Time: %d\n", noActivity);
  119.             noActivity *= 60;
  120.             break;
  121.         case '?':
  122.             fprintf(stderr, "unrecognized option: -%c\n", optopt);
  123.             fprintf(stderr, "Try `%s --help' for more "
  124.                 "information.\n",
  125.                 progname);
  126.             return(EXIT_FAILURE);
  127.         }
  128.     }
  129.     if(argc == optind){
  130.         /* no arguments */
  131.         strcpy(addr.sun_path,lircDev);
  132.     } else {
  133.         fprintf(stderr, "%s: incorrect number of arguments.\n",
  134.             progname);
  135.         fprintf(stderr, "Try `%s --help' for more information.\n",
  136.             progname);
  137.         return(EXIT_FAILURE);
  138.     }
  139.  
  140.     fd=socket(AF_UNIX,SOCK_STREAM,0);
  141.     if(fd==-1)  {
  142.         perror("socket");
  143.         printf("socket error\n");
  144.         exit(errno);
  145.     }
  146.  
  147.     maxErr = 0;
  148.     while (maxErr < 20 && (connect(fd,(struct sockaddr *)&addr,sizeof(addr))==-1)) {
  149.             printf("connect erorr %d\n", errno);
  150.             sleep(1);
  151.             maxErr++;
  152.     }
  153.     if (maxErr >= 20) {
  154.         perror("connect");
  155.         printf("connect erorr\n");
  156.         exit(errno);
  157.     }
  158.  
  159.     FD_ZERO(&rfds);
  160.     FD_SET(fd, &rfds);
  161.  
  162.     // initial interval of no activity
  163.     if (noActivity > 15*60)
  164.         nextCheck = 15*60;
  165.     else
  166.         nextCheck = noActivity;
  167.     maxErr = 0;
  168.  
  169.     while(true)  {
  170.  
  171.         if (nextCheck == 0) {
  172.             ret = getTimer(&timer);
  173.             // timer active
  174.             if (ret == 1) {
  175.                 nextCheck = timer;
  176.                 maxErr = 0;
  177.             }
  178.             // no timer
  179.             else if (ret == 0)
  180.                 nextCheck = 0;
  181.             // error
  182.             else {
  183.                 nextCheck = 60;
  184.                 maxErr++;
  185.                 if (maxErr > 3)
  186.                     nextCheck = 0;
  187.             }
  188.         }
  189.  
  190.         if (nextCheck == 0) {
  191.             close(fd);
  192.             printf("shutting down ....\n");
  193.             ret = system("sudo /usr/local/bin/runxbmc poweroff");
  194.             exit(0);
  195.         }
  196.  
  197.         tv.tv_sec = nextCheck;
  198.         tv.tv_usec = 0;
  199.         ret = select(fd+1, &rfds, NULL, NULL, &tv);
  200.  
  201.         if (ret == -1) {
  202.             printf("Select error\n");
  203.             nextCheck = 0;
  204.         }
  205.         else if (ret) {
  206.             i=read(fd,buf,128);
  207.             // write(STDOUT_FILENO,buf,i);
  208.             nextCheck = noActivity;
  209.         }
  210.         else
  211.             nextCheck = 0;
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement