-
Notifications
You must be signed in to change notification settings - Fork 3
/
timer.c
292 lines (269 loc) · 9.75 KB
/
timer.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "timer.h"
#include "common.h"
#include "helper.h"
#include <vdr/timers.h>
// Id
// Remote
// Flags
// Channel-ID
// Day/Weekdays
// Start
// Stop
// Priority
// Lifetime
// File
// Aux
// Event-ID
// Recording
// Pending
// InVpsMargin
#define TimerDBusStruct "isussiiiissubbb"
namespace cDBusTimersHelper
{
static const char *_xmlNodeInfoConst =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node>\n"
" <interface name=\""DBUS_VDR_TIMER_INTERFACE"\">\n"
" <method name=\"List\">\n"
" <arg name=\"timer\" type=\"as\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"ListDetailed\">\n"
" <arg name=\"timer\" type=\"a("TimerDBusStruct")\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"Next\">\n"
" <arg name=\"replycode\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"number\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"seconds\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"start\" type=\"t\" direction=\"out\"/>\n"
" <arg name=\"stop\" type=\"t\" direction=\"out\"/>\n"
" <arg name=\"title\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" </interface>\n"
"</node>\n";
static const char *_xmlNodeInfo =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
" \"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node>\n"
" <interface name=\""DBUS_VDR_TIMER_INTERFACE"\">\n"
" <method name=\"New\">\n"
" <arg name=\"timer\" type=\"s\" direction=\"in\"/>\n"
" <arg name=\"replycode\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"replymessage\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"Delete\">\n"
" <arg name=\"number\" type=\"i\" direction=\"in\"/>\n"
" <arg name=\"replycode\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"replymessage\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"List\">\n"
" <arg name=\"timer\" type=\"as\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"ListDetailed\">\n"
" <arg name=\"timer\" type=\"a("TimerDBusStruct")\" direction=\"out\"/>\n"
" </method>\n"
" <method name=\"Next\">\n"
" <arg name=\"replycode\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"number\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"seconds\" type=\"i\" direction=\"out\"/>\n"
" <arg name=\"start\" type=\"t\" direction=\"out\"/>\n"
" <arg name=\"stop\" type=\"t\" direction=\"out\"/>\n"
" <arg name=\"title\" type=\"s\" direction=\"out\"/>\n"
" </method>\n"
" </interface>\n"
"</node>\n";
static void AddTimer(GVariantBuilder *Array, const cTimer *Timer)
{
if (Timer == NULL)
return;
gint32 id = Timer->Index() + 1;
cString remote = "";
#if VDRVERSNUM > 20300
id = Timer->Id();
remote = Timer->Remote() ? Timer->Remote() : "";
#endif
cString channel = Timer->Channel()->GetChannelID().ToString();
cString day = cTimer::PrintDay(Timer->Day(), Timer->WeekDays(), true);
const char *aux = Timer->Aux() ? Timer->Aux() : "";
guint32 eventId = 0;
if (Timer->Event() != NULL)
eventId = Timer->Event()->EventID();
g_variant_builder_add(Array, "("TimerDBusStruct")",
id, *remote, Timer->Flags(), *channel, *day, Timer->Start(), Timer->Stop(),
Timer->Priority(), Timer->Lifetime(), Timer->File(), aux, eventId,
Timer->Recording(), Timer->Pending(), Timer->InVpsMargin());
}
static void New(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
const char *option = NULL;
g_variant_get(Parameters, "(&s)", &option);
if (*option) {
cTimer *timer = new cTimer;
if (timer->Parse(option)) {
cTimers *timers = NULL;
#if VDRVERSNUM > 20300
LOCK_TIMERS_WRITE;
timers = Timers;
#else
timers = &Timers;
#endif
timer->ClrFlags(tfRecording);
timers->Add(timer);
#if VDRVERSNUM < 20300
timers->SetModified();
#endif
isyslog("timer %s added", *timer->ToDescr());
cDBusHelper::SendReply(Invocation, 250, *cString::sprintf("%d %s", timer->Index() + 1, *timer->ToText()));
return;
}
else
cDBusHelper::SendReply(Invocation, 501, "Error in timer settings");
delete timer;
}
else
cDBusHelper::SendReply(Invocation, 501, "Missing timer settings");
};
static void Delete(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
int number = 0;
g_variant_get(Parameters, "(i)", &number);
#if VDRVERSNUM > 20300
LOCK_TIMERS_WRITE;
Timers->SetExplicitModify();
if (cTimer *timer = Timers->GetById(number)) {
if (timer->Recording())
timer->Skip();
Timers->Del(timer);
Timers->SetModified();
cDBusHelper::SendReply(Invocation, 250, *cString::sprintf("Timer \"%d\" deleted", number));
}
else
cDBusHelper::SendReply(Invocation, 501, *cString::sprintf("Timer \"%d\" not defined", number));
#else
if (!Timers.BeingEdited()) {
cTimer *timer = Timers.Get(number - 1);
if (timer) {
if (!timer->Recording()) {
isyslog("deleting timer %s", *timer->ToDescr());
Timers.Del(timer);
Timers.SetModified();
cDBusHelper::SendReply(Invocation, 250, *cString::sprintf("Timer \"%d\" deleted", number));
}
else
cDBusHelper::SendReply(Invocation, 550, *cString::sprintf("Timer \"%d\" is recording", number));
}
else
cDBusHelper::SendReply(Invocation, 501, *cString::sprintf("Timer \"%d\" not defined", number));
}
else
cDBusHelper::SendReply(Invocation, 550, "Timers are being edited - try again later");
#endif
}
static void List(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(as)"));
GVariantBuilder *array = g_variant_builder_new(G_VARIANT_TYPE("as"));
cString text;
const char *tmp;
const cTimers *timers = NULL;
#if VDRVERSNUM > 20300
LOCK_TIMERS_READ;
timers = Timers;
#else
timers = &Timers;
#endif
for (int i = 0; i < timers->Count(); i++) {
const cTimer *timer = timers->Get(i);
if (timer) {
text = timer->ToText(true);
tmp = stripspace((char*)*text);
g_variant_builder_add(array, "s", tmp);
}
}
g_variant_builder_add_value(builder, g_variant_builder_end(array));
g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder));
g_variant_builder_unref(array);
g_variant_builder_unref(builder);
};
static void ListDetailed(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(a("TimerDBusStruct"))"));
GVariantBuilder *array = g_variant_builder_new(G_VARIANT_TYPE("a("TimerDBusStruct")"));
const cTimers *timers = NULL;
#if VDRVERSNUM > 20300
LOCK_TIMERS_READ;
timers = Timers;
#else
timers = &Timers;
#endif
for (int i = 0; i < timers->Count(); i++)
AddTimer(array, timers->Get(i));
g_variant_builder_add_value(builder, g_variant_builder_end(array));
g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder));
g_variant_builder_unref(array);
g_variant_builder_unref(builder);
};
static void Next(cDBusObject *Object, GVariant *Parameters, GDBusMethodInvocation *Invocation)
{
int returncode = 250;
int number = -1;
int seconds = 0;
time_t start = 0;
time_t stop = 0;
const char *title = "";
#if VDRVERSNUM > 20300
LOCK_TIMERS_READ;
const cTimers *timers = Timers;
#else
cTimers *timers = &Timers;
#endif
const cTimer *t = timers->GetNextActiveTimer();
const cEvent *e;
if (t) {
start = t->StartTime();
number = t->Index() + 1;
seconds = start - time(NULL);
stop = t->StopTime();
e = t->Event();
if ((e != NULL) && (e->Title() != NULL))
title = e->Title();
}
else
returncode = 550;
GVariantBuilder *builder = g_variant_builder_new(G_VARIANT_TYPE("(iiitts)"));
g_variant_builder_add(builder, "i", returncode);
g_variant_builder_add(builder, "i", number);
g_variant_builder_add(builder, "i", seconds);
g_variant_builder_add(builder, "t", start);
g_variant_builder_add(builder, "t", stop);
g_variant_builder_add(builder, "s", title);
g_dbus_method_invocation_return_value(Invocation, g_variant_builder_end(builder));
g_variant_builder_unref(builder);
};
}
cDBusTimersConst::cDBusTimersConst(const char *NodeInfo)
:cDBusObject("/Timers", NodeInfo)
{
AddMethod("List", cDBusTimersHelper::List);
AddMethod("ListDetailed", cDBusTimersHelper::ListDetailed);
AddMethod("Next", cDBusTimersHelper::Next);
}
cDBusTimersConst::cDBusTimersConst(void)
:cDBusObject("/Timers", cDBusTimersHelper::_xmlNodeInfoConst)
{
AddMethod("List", cDBusTimersHelper::List);
AddMethod("ListDetailed", cDBusTimersHelper::ListDetailed);
AddMethod("Next", cDBusTimersHelper::Next);
}
cDBusTimersConst::~cDBusTimersConst(void)
{
}
cDBusTimers::cDBusTimers(void)
:cDBusTimersConst(cDBusTimersHelper::_xmlNodeInfo)
{
AddMethod("New", cDBusTimersHelper::New);
AddMethod("Delete", cDBusTimersHelper::Delete);
}
cDBusTimers::~cDBusTimers(void)
{
}