-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserial.c
319 lines (271 loc) · 6.72 KB
/
serial.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* serial plugin for VDR
*
* Copyright (C) 2003 Andreas Regel <[email protected]>
*
* This code is distributed under the terms and conditions of the
* GNU GENERAL PUBLIC LICENSE. See the file COPYING for details.
*
*/
#include <vdr/plugin.h>
#include <vdr/remote.h>
#include <vdr/thread.h>
#include <vdr/status.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <getopt.h>
#include "i18n.h"
#define LEDREC TIOCM_RTS
#define LED2 TIOCM_SR
#define MAXCYCLE 10
static const char *VERSION = "0.0.6a";
static const char *DESCRIPTION = "Frontpanel plugin for the serial line";
//static const char *MAINMENUENTRY = NULL;
static const char *SerPort[] = { "", "/dev/ttyS0", "/dev/ttyS1", "/dev/ttyS2", "/dev/ttyS3" };
// Setup Menu
struct sSerialSetup {
int iFlash;
int Port;
};
sSerialSetup SerialSetup;
class cMenuSetupSerial : public cMenuSetupPage {
private:
sSerialSetup newSerialSetup;
protected:
virtual void Store(void);
public:
cMenuSetupSerial(void);
};
cMenuSetupSerial::cMenuSetupSerial(void)
{
newSerialSetup.iFlash = SerialSetup.iFlash;
newSerialSetup.Port = SerialSetup.Port;
Add(new cMenuEditIntItem( tr("Flashrate"), &newSerialSetup.iFlash, 1, 10));
Add(new cMenuEditIntItem( tr("Serial Port (COM)"), &newSerialSetup.Port, 1, 4));
}
void cMenuSetupSerial::Store(void)
{
SetupStore("Flashrate", SerialSetup.iFlash = newSerialSetup.iFlash);
SetupStore("Port", SerialSetup.Port = newSerialSetup.Port);
}
class cSerialStatus : public cStatus
{
private:
int iLedStatus[MAXCYCLE];
int iNumDevices;
protected:
virtual void Recording(const cDevice *Device, const char *Name);
public:
int GetLedStatus(int i);
cSerialStatus(void);
int iCardIsRecording[MAXDEVICES];
};
cSerialStatus::cSerialStatus(void)
{
for(int i=0; i<MAXCYCLE; i++)
iLedStatus[i] = 0;
for(int i=0; i<MAXDEVICES; i++)
iCardIsRecording[i] = 0;
iNumDevices = 0;
}
int cSerialStatus::GetLedStatus(int j)
{
int iOccupiedDevices = 0;
int i;
for(i=0; i<iNumDevices; i++)
if(iCardIsRecording[i])
iOccupiedDevices++;
if((iNumDevices == iOccupiedDevices) && (iOccupiedDevices >0)) // Blinken
{
for(i=0; i<SerialSetup.iFlash; i++)
iLedStatus[i] |= LEDREC; // Bit setzen
for(i=SerialSetup.iFlash; i<MAXCYCLE; i++)
iLedStatus[i] &= ~LEDREC; // Bit loeschen
}
else if(iOccupiedDevices > 0) // Dauerleuchten
{
for(int i=0;i<MAXCYCLE;i++)
iLedStatus[i] |= LEDREC; // Bit setzen
}
else // LED aus
{
for(int i=0;i<MAXCYCLE;i++)
iLedStatus[i] &= ~LEDREC; // Bit loeschen
}
return iLedStatus[i % MAXCYCLE];
}
void cSerialStatus::Recording(const cDevice *Device, const char *Name)
{
int iCardIndex = Device->CardIndex();
iNumDevices = Device->NumDevices();
if(iCardIndex < MAXDEVICES)
{
if(Name && Name[0])
iCardIsRecording[iCardIndex]++;
else
iCardIsRecording[iCardIndex]--;
}
}
class cSerialRemote : public cRemote, private cThread
{
private:
int fd;
int iCycle;
virtual void Action(void);
virtual void SetState(void);
cSerialStatus *cSSstat;
public:
cSerialRemote(cSerialStatus *stat);
virtual ~cSerialRemote(void);
int Open(const char *device, cSerialStatus *stat);
void Close(int fd);
};
cSerialRemote::cSerialRemote(cSerialStatus *stat)
:cRemote("Serial")
{
fd = Open(SerPort[SerialSetup.Port], stat);
}
cSerialRemote::~cSerialRemote()
{
int state = 0;
ioctl(fd, TIOCMSET, &state);
Cancel();
}
void cSerialRemote::SetState(void)
{
int iStateWithDTR = 0;
iCycle = (iCycle + 1) % MAXCYCLE;
iStateWithDTR = cSSstat->GetLedStatus(iCycle);
iStateWithDTR |= TIOCM_DTR;
ioctl(fd, TIOCMSET, &iStateWithDTR);
}
void cSerialRemote::Action(void)
{
dsyslog("Serial remote control thread started (pid=%d)", getpid());
int state = 0;
int button = 0;
char str[32];
while(fd >= 0)
{
ioctl(fd, TIOCMGET, &state);
button = 0;
if(state & TIOCM_CTS)
button += 1;
if(state & TIOCM_DSR)
button += 2;
if(state & TIOCM_RNG)
button += 4;
if(state & TIOCM_CAR)
button += 8;
if(button > 0)
{
sprintf(str, "Button%d", button);
Put(str);
}
SetState();
usleep(100000);
}
dsyslog("Serial remote control thread ended (pid=%d)", getpid());
}
int cSerialRemote::Open(const char *device, cSerialStatus *stat)
{
int fd = 0;
int state = 0;
cSSstat = stat;
iCycle = 0;
fd = open(device , O_RDONLY | O_NDELAY);
if(fd >= 0)
{
/* Set DTR to high */
state |= TIOCM_DTR;
ioctl(fd, TIOCMSET, &state);
Start();
return fd;
}
return -1;
}
void cSerialRemote::Close(int fd)
{
if(fd >= 0)
close(fd);
}
class cPluginSerial : public cPlugin
{
private:
// Add any member variables or functions you may need here.
cSerialRemote *ser;
cSerialStatus *stat;
public:
cPluginSerial(void);
virtual ~cPluginSerial();
virtual const char *Version(void) { return VERSION; }
virtual const char *Description(void) { return DESCRIPTION; }
virtual const char *CommandLineHelp(void);
virtual bool ProcessArgs(int argc, char *argv[]);
virtual bool Start(void);
virtual void Housekeeping(void);
virtual const char *MainMenuEntry(void) { return NULL; }
virtual cOsdObject *MainMenuAction(void);
virtual cMenuSetupPage *SetupMenu(void);
virtual bool SetupParse(const char *Name, const char *Value);
};
cPluginSerial::cPluginSerial(void)
{
// Initialize any member variables here.
// DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
// VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
}
cPluginSerial::~cPluginSerial()
{
// Clean up after yourself!
}
const char *cPluginSerial::CommandLineHelp(void)
{
// Return a string that describes all known command line options.
return NULL;
}
bool cPluginSerial::ProcessArgs(int argc, char *argv[])
{
// Implement command line argument processing here if applicable.
return true;
}
bool cPluginSerial::Start(void)
{
// Start any background activities the plugin shall perform.
stat = new cSerialStatus();
ser = new cSerialRemote(stat);
RegisterI18n(Phrases);
return true;
}
void cPluginSerial::Housekeeping(void)
{
// Perform any cleanup or other regular tasks.
}
cOsdObject *cPluginSerial::MainMenuAction(void)
{
// Perform the action when selected from the main VDR menu.
return NULL;
}
cMenuSetupPage *cPluginSerial::SetupMenu(void)
{
// Return a setup menu in case the plugin supports one.
return new cMenuSetupSerial;
}
bool cPluginSerial::SetupParse(const char *Name, const char *Value)
{
// Parse your own setup parameters and store their values.
if (!strcasecmp(Name,"Flashrate"))
SerialSetup.iFlash = atoi(Value);
else if (!strcasecmp(Name,"Port"))
SerialSetup.Port = atoi(Value);
else
return false;
return true;
}
VDRPLUGINCREATOR(cPluginSerial); // Don't touch this!