summaryrefslogtreecommitdiff
path: root/fskcheck.c
blob: c860c3052d33da1203859819af96c9948dc91147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//***************************************************************************
/*
 * fskcheck.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: fskcheck.c,v 1.3 2007/01/13 07:52:27 root Exp $
 */
//***************************************************************************

//***************************************************************************
// Includes
//***************************************************************************

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/msg.h>

#include "def.h"
#include "talk.h"

//*************************************************************************
// Global
//*************************************************************************

int choice(char** argv, int& cmd, const char*& msg, long& timeout);

const char* commands[] =
{
   "Check",
   "Info",

   0
};

enum Command
{
   cmdUnknown = na,

   cmdCheck,
   cmdInfo,

   cmdCount
};

//***************************************************************************
// Main
//***************************************************************************

int main(int argc, char** argv)
{
   Talk sender;
   int status;
   int event = Talk::evtUnknown;
   const char* msg;
   int cmd;
   long t;

   if ((status = sender.open(11)) != success)
   {
      printf("Falal: Can't init message queue, '%s'!\n", strerror(errno));
      return fail;
   }

   t = sender.getTimeout();
   msg = 0;

   if (choice(argv, cmd, msg, t) != success)
   {
      printf("Usage: fskcheck <command> [-t timeout][-v]\n");
      printf("         command:  check          - check if vdr currently protected\n");
      printf("         command:  info <message> - show message on osd\n");

      return fail;
   }

   // init

   sender.init(); 
   sender.setTimeout(t);

   // select command

   switch (cmd)
   {
      case cmdCheck: event = Talk::evtCheck;  break;
      case cmdInfo:  event = Talk::evtShow;   break;
   }

   // send 

   if (sender.send(10, event, msg) != success)
   {
      printf("Fatal: Can't send event (%d) with message '%s' to vdr (port %d), '%s'\n", 
             event, msg ? msg : "", 10, strerror(errno));
      return fail;
   }

   // wait

   if (cmd == cmdCheck)
   {
      if (sender.wait() == success)
      {
         // printf("Got (%d) \n", sender.getEvent());

         if (sender.getEvent() == Talk::evtConfirm) return 1;
         if (sender.getEvent()  == Talk::evtAbort)   return 2;

         return 0;
      }

      else
         return 0;
   }

   return 0;
}

//***************************************************************************
// Choise
//***************************************************************************

int choice(char** argv, int& cmd, const char*& msg, long& timeout)
{
   // skip program path

   argv++;

   // get command

   if (!(*argv)) 
      return fail;

   for (cmd = 0; cmd < cmdCount; cmd++)
      if (strcasecmp(commands[cmd], *argv) == 0)
         break;

   if (cmd >= cmdCount)
      return fail;

   argv++;

   while (*argv)
   {
      if (**argv == '-')
      {
         if (strcmp(*argv, "-t") == 0)
         {
            // timeout 

            if (!(*(++argv))) break;

            timeout = atoi(*argv);
         }
      }
      else
      {
         // the message

         msg = *argv;
      }

      argv++;
   }

   return success;
}