vdr/remote.c

505 lines
12 KiB
C
Raw Normal View History

2000-02-19 13:36:48 +01:00
/*
* remote.c: Interface to the Remote Control Unit
*
2000-04-24 09:46:05 +02:00
* See the main source file 'vdr.c' for copyright information and
2000-02-19 13:36:48 +01:00
* how to reach the author.
*
2000-07-15 12:39:20 +02:00
* Ported to LIRC by Carsten Koch <Carsten.Koch@icem.de> 2000-06-16.
*
* $Id: remote.c 1.27 2002/05/18 12:55:39 kls Exp $
2000-02-19 13:36:48 +01:00
*/
#include "remote.h"
#include <fcntl.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <termios.h>
#include <unistd.h>
2000-07-15 12:39:20 +02:00
#if defined REMOTE_LIRC
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#endif
#if defined REMOTE_KBD
#include <ncurses.h>
#endif
2000-07-15 12:39:20 +02:00
#include "config.h"
2000-02-19 13:36:48 +01:00
#include "tools.h"
2000-07-15 12:39:20 +02:00
// --- cRcIoBase -------------------------------------------------------------
cRcIoBase::cRcIoBase(void)
{
t = 0;
}
cRcIoBase::~cRcIoBase()
{
}
// --- cRcIoKBD --------------------------------------------------------------
#if defined REMOTE_KBD
cRcIoKBD::cRcIoKBD(void)
{
f.Open(0); // stdin
2000-07-15 12:39:20 +02:00
}
cRcIoKBD::~cRcIoKBD()
{
}
2000-09-19 17:48:42 +02:00
void cRcIoKBD::Flush(int WaitMs)
2000-07-15 12:39:20 +02:00
{
2000-09-19 17:48:42 +02:00
int t0 = time_ms();
2000-07-15 12:39:20 +02:00
timeout(10);
for (;;) {
while (getch() > 0)
2000-09-19 17:48:42 +02:00
t0 = time_ms();
if (time_ms() - t0 >= WaitMs)
2000-07-15 12:39:20 +02:00
break;
}
}
2000-10-08 09:25:20 +02:00
bool cRcIoKBD::InputAvailable(void)
2000-07-15 12:39:20 +02:00
{
2000-10-08 09:25:20 +02:00
return f.Ready(false);
2000-07-15 12:39:20 +02:00
}
bool cRcIoKBD::GetCommand(unsigned int *Command, bool *Repeat, bool *Release)
2000-07-15 12:39:20 +02:00
{
if (Command) {
*Command = getch();
return int(*Command) > 0;
2000-07-15 12:39:20 +02:00
}
return false;
}
// --- cRcIoRCU --------------------------------------------------------------
#elif defined REMOTE_RCU
2000-10-08 09:25:20 +02:00
#define REPEATLIMIT 20 // ms
#define REPEATDELAY 350 // ms
2000-07-15 12:39:20 +02:00
cRcIoRCU::cRcIoRCU(char *DeviceName)
2000-02-19 13:36:48 +01:00
{
dp = 0;
mode = modeB;
code = 0;
address = 0xFFFF;
2000-10-08 09:25:20 +02:00
receivedAddress = 0;
receivedCommand = 0;
receivedData = receivedRepeat = receivedRelease = false;
lastNumber = 0;
2000-10-08 09:25:20 +02:00
if ((f = open(DeviceName, O_RDWR | O_NONBLOCK)) >= 0) {
2000-02-19 13:36:48 +01:00
struct termios t;
if (tcgetattr(f, &t) == 0) {
cfsetspeed(&t, B9600);
cfmakeraw(&t);
2000-10-08 09:25:20 +02:00
if (tcsetattr(f, TCSAFLUSH, &t) == 0) {
Start();
2000-02-19 13:36:48 +01:00
return;
2000-10-08 09:25:20 +02:00
}
2000-02-19 13:36:48 +01:00
}
2000-04-16 13:54:16 +02:00
LOG_ERROR_STR(DeviceName);
2000-10-08 09:25:20 +02:00
close(f);
2000-02-19 13:36:48 +01:00
}
2000-04-16 13:54:16 +02:00
else
LOG_ERROR_STR(DeviceName);
2000-10-08 09:25:20 +02:00
f = -1;
2000-02-19 13:36:48 +01:00
}
2000-07-15 12:39:20 +02:00
cRcIoRCU::~cRcIoRCU()
2000-02-19 13:36:48 +01:00
{
2000-12-08 16:23:32 +01:00
Cancel();
2000-02-19 13:36:48 +01:00
}
2000-10-08 09:25:20 +02:00
void cRcIoRCU::Action(void)
{
#pragma pack(1)
union {
struct {
unsigned short address;
unsigned int command;
} data;
unsigned char raw[6];
} buffer;
#pragma pack()
2002-05-13 16:35:49 +02:00
dsyslog("RCU remote control thread started (pid=%d)", getpid());
2000-10-08 09:25:20 +02:00
int FirstTime = 0;
unsigned int LastCommand = 0;
2000-10-08 09:25:20 +02:00
for (; f >= 0;) {
LOCK_THREAD;
if (ReceiveByte(REPEATLIMIT) == 'X') {
for (int i = 0; i < 6; i++) {
int b = ReceiveByte();
if (b >= 0) {
buffer.raw[i] = b;
if (i == 5) {
unsigned short Address = ntohs(buffer.data.address); // the PIC sends bytes in "network order"
unsigned int Command = ntohl(buffer.data.command);
if (code == 'B' && address == 0x0000 && Command == 0x00004000)
// Well, well, if it isn't the "d-box"...
// This remote control sends the above command before and after
// each keypress - let's just drop this:
break;
if (!receivedData) { // only accept new data the previous data has been fetched
int Now = time_ms();
if (Command != LastCommand) {
receivedAddress = Address;
receivedCommand = Command;
receivedData = true;
receivedRepeat = receivedRelease = false;
FirstTime = Now;
}
else {
if (Now - FirstTime < REPEATDELAY)
break; // repeat function kicks in after a short delay
receivedData = receivedRepeat = true;
}
LastCommand = Command;
WakeUp();
2000-10-08 09:25:20 +02:00
}
}
}
else
break;
}
}
else if (receivedData) { // the last data before releasing the key hasn't been fetched yet
if (receivedRepeat) { // it was a repeat, so let's make it a release
receivedRepeat = false;
receivedRelease = true;
2000-10-08 09:25:20 +02:00
LastCommand = 0;
WakeUp();
2000-10-08 09:25:20 +02:00
}
}
else if (receivedRepeat) { // all data has already been fetched, but the last one was a repeat, so let's generate a release
receivedData = receivedRelease = true;
2000-10-08 09:25:20 +02:00
receivedRepeat = false;
LastCommand = 0;
WakeUp();
}
else
LastCommand = 0;
}
}
int cRcIoRCU::ReceiveByte(int TimeoutMs)
2000-04-23 15:38:16 +02:00
{
// Returns the byte if one was received within a timeout, -1 otherwise
2000-10-08 09:25:20 +02:00
if (cFile::FileReady(f, TimeoutMs)) {
2000-04-23 15:38:16 +02:00
unsigned char b;
2001-08-12 15:22:48 +02:00
if (safe_read(f, &b, 1) == 1)
2000-04-23 15:38:16 +02:00
return b;
2000-06-17 17:43:05 +02:00
else
LOG_ERROR;
2000-02-19 13:36:48 +01:00
}
return -1;
}
2000-07-15 12:39:20 +02:00
bool cRcIoRCU::SendByteHandshake(unsigned char c)
2000-02-19 13:36:48 +01:00
{
2000-10-08 09:25:20 +02:00
if (f >= 0) {
2000-06-17 17:43:05 +02:00
int w = write(f, &c, 1);
if (w == 1) {
2000-10-08 09:25:20 +02:00
for (int reply = ReceiveByte(REPEATLIMIT); reply >= 0;) {
2000-06-17 17:43:05 +02:00
if (reply == c)
return true;
else if (reply == 'X') {
// skip any incoming RC code - it will come again
for (int i = 6; i--;) {
2000-10-08 09:25:20 +02:00
if (ReceiveByte() < 0)
2000-06-17 17:43:05 +02:00
return false;
}
}
else
return false;
2000-02-19 13:36:48 +01:00
}
2000-06-17 17:43:05 +02:00
}
LOG_ERROR;
2000-02-19 13:36:48 +01:00
}
return false;
}
2000-07-15 12:39:20 +02:00
bool cRcIoRCU::SendByte(unsigned char c)
2000-02-19 13:36:48 +01:00
{
2000-10-08 09:25:20 +02:00
LOCK_THREAD;
2000-02-19 13:36:48 +01:00
for (int retry = 5; retry--;) {
if (SendByteHandshake(c))
return true;
}
return false;
}
2000-07-15 12:39:20 +02:00
bool cRcIoRCU::SetCode(unsigned char Code, unsigned short Address)
{
code = Code;
address = Address;
return SendCommand(code);
}
bool cRcIoRCU::SetMode(unsigned char Mode)
{
mode = Mode;
return SendCommand(mode);
}
2000-09-19 17:48:42 +02:00
void cRcIoRCU::Flush(int WaitMs)
2000-02-19 13:36:48 +01:00
{
2000-10-08 09:25:20 +02:00
LOCK_THREAD;
2000-02-19 13:36:48 +01:00
2000-10-08 09:25:20 +02:00
int t0 = time_ms();
2000-02-19 13:36:48 +01:00
for (;;) {
2000-10-08 09:25:20 +02:00
while (ReceiveByte() >= 0)
2000-09-19 17:48:42 +02:00
t0 = time_ms();
if (time_ms() - t0 >= WaitMs)
2000-02-19 13:36:48 +01:00
break;
}
2000-10-08 09:25:20 +02:00
receivedData = receivedRepeat = false;
2000-02-19 13:36:48 +01:00
}
bool cRcIoRCU::GetCommand(unsigned int *Command, bool *Repeat, bool *Release)
2000-02-19 13:36:48 +01:00
{
2000-10-08 09:25:20 +02:00
if (receivedData) { // first we check the boolean flag without a lock, to avoid delays
2000-02-19 13:36:48 +01:00
2000-10-08 09:25:20 +02:00
LOCK_THREAD;
if (receivedData) { // need to check again, since the status might have changed while waiting for the lock
if (Command)
*Command = receivedCommand;
if (Repeat)
*Repeat = receivedRepeat;
if (Release)
*Release = receivedRelease;
2000-10-08 09:25:20 +02:00
receivedData = false;
return true;
2000-02-19 13:36:48 +01:00
}
}
if (time(NULL) - t > 60) {
SendCommand(code); // in case the PIC listens to the wrong code
t = time(NULL);
}
return false;
}
2000-07-15 12:39:20 +02:00
bool cRcIoRCU::SendCommand(unsigned char Cmd)
2000-02-19 13:36:48 +01:00
{
return SendByte(Cmd | 0x80);
}
2000-07-15 12:39:20 +02:00
bool cRcIoRCU::Digit(int n, int v)
2000-02-19 13:36:48 +01:00
{
return SendByte(((n & 0x03) << 5) | (v & 0x0F) | (((dp >> n) & 0x01) << 4));
}
2000-07-15 12:39:20 +02:00
bool cRcIoRCU::Number(int n, bool Hex)
2000-02-19 13:36:48 +01:00
{
2000-10-08 09:25:20 +02:00
LOCK_THREAD;
2000-02-19 13:36:48 +01:00
if (!Hex) {
char buf[8];
sprintf(buf, "%4d", n & 0xFFFF);
n = 0;
for (char *d = buf; *d; d++) {
if (*d == ' ')
*d = 0xF;
n = (n << 4) | ((*d - '0') & 0x0F);
}
}
lastNumber = n;
2000-02-19 13:36:48 +01:00
for (int i = 0; i < 4; i++) {
if (!Digit(i, n))
return false;
n >>= 4;
}
return SendCommand(mode);
}
2000-07-15 12:39:20 +02:00
bool cRcIoRCU::String(char *s)
2000-02-19 13:36:48 +01:00
{
2000-10-08 09:25:20 +02:00
LOCK_THREAD;
2000-04-22 15:21:41 +02:00
const char *chars = mode == modeH ? "0123456789ABCDEF" : "0123456789-EHLP ";
2000-02-19 13:36:48 +01:00
int n = 0;
for (int i = 0; *s && i < 4; s++, i++) {
n <<= 4;
2000-04-22 15:21:41 +02:00
for (const char *c = chars; *c; c++) {
2000-02-19 13:36:48 +01:00
if (*c == *s) {
n |= c - chars;
break;
}
}
}
return Number(n, true);
2000-02-19 13:36:48 +01:00
}
2000-07-15 12:39:20 +02:00
void cRcIoRCU::SetPoints(unsigned char Dp, bool On)
{
if (On)
dp |= Dp;
else
dp &= ~Dp;
Number(lastNumber, true);
}
2000-07-15 12:39:20 +02:00
bool cRcIoRCU::DetectCode(unsigned char *Code, unsigned short *Address)
2000-02-19 13:36:48 +01:00
{
// Caller should initialize 'Code' to 0 and call DetectCode()
// until it returns true. Whenever DetectCode() returns false
// and 'Code' is not 0, the caller can use 'Code' to display
// a message like "Trying code '%c'". If false is returned and
// 'Code' is 0, all possible codes have been tried and the caller
// can either stop calling DetectCode() (and give some error
// message), or start all over again.
if (*Code < 'A' || *Code > 'D') {
*Code = 'A';
return false;
}
if (*Code <= 'D') {
SetMode(modeH);
char buf[5];
sprintf(buf, "C0D%c", *Code);
String(buf);
SetCode(*Code, 0);
2000-10-08 09:25:20 +02:00
delay_ms(REPEATDELAY);
receivedData = receivedRepeat = 0;
delay_ms(REPEATDELAY);
if (GetCommand()) {
*Address = receivedAddress;
SetMode(modeB);
String("----");
2000-02-19 13:36:48 +01:00
return true;
}
2000-02-19 13:36:48 +01:00
if (*Code < 'D') {
(*Code)++;
return false;
}
}
*Code = 0;
return false;
}
2000-07-15 12:39:20 +02:00
// --- cRcIoLIRC -------------------------------------------------------------
#elif defined REMOTE_LIRC
2000-10-08 09:25:20 +02:00
#define REPEATLIMIT 20 // ms
#define REPEATDELAY 350 // ms
2000-07-15 12:39:20 +02:00
cRcIoLIRC::cRcIoLIRC(char *DeviceName)
{
2000-10-08 09:25:20 +02:00
*keyName = 0;
receivedData = receivedRepeat = false;
2000-07-15 12:39:20 +02:00
struct sockaddr_un addr;
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, DeviceName);
2000-10-08 09:25:20 +02:00
if ((f = socket(AF_UNIX, SOCK_STREAM, 0)) >= 0) {
if (connect(f, (struct sockaddr *)&addr, sizeof(addr)) >= 0) {
Start();
2000-07-15 12:39:20 +02:00
return;
}
2000-07-15 12:39:20 +02:00
LOG_ERROR_STR(DeviceName);
2000-10-08 09:25:20 +02:00
close(f);
2000-07-15 12:39:20 +02:00
}
else
LOG_ERROR_STR(DeviceName);
2000-10-08 09:25:20 +02:00
f = -1;
2000-07-15 12:39:20 +02:00
}
cRcIoLIRC::~cRcIoLIRC()
{
2000-12-08 16:23:32 +01:00
Cancel();
2000-07-15 12:39:20 +02:00
}
2000-10-08 09:25:20 +02:00
void cRcIoLIRC::Action(void)
2000-07-15 12:39:20 +02:00
{
2002-05-13 16:35:49 +02:00
dsyslog("LIRC remote control thread started (pid=%d)", getpid());
2000-10-08 09:25:20 +02:00
int FirstTime = 0;
int LastTime = 0;
2000-10-08 09:25:20 +02:00
char buf[LIRC_BUFFER_SIZE];
char LastKeyName[LIRC_KEY_BUF];
2000-10-08 09:25:20 +02:00
for (; f >= 0;) {
2000-10-08 09:25:20 +02:00
LOCK_THREAD;
2001-08-12 15:22:48 +02:00
if (cFile::FileReady(f, REPEATLIMIT) && safe_read(f, buf, sizeof(buf)) > 21) {
if (!receivedData) { // only accept new data the previous data has been fetched
int count;
sscanf(buf, "%*x %x %29s", &count, LastKeyName); // '29' in '%29s' is LIRC_KEY_BUF-1!
int Now = time_ms();
if (count == 0) {
strcpy(keyName, LastKeyName);
receivedData = true;
receivedRepeat = receivedRelease = false;
FirstTime = Now;
}
else {
if (Now - FirstTime < REPEATDELAY)
continue; // repeat function kicks in after a short delay
receivedData = receivedRepeat = true;
receivedRelease = false;
}
LastTime = Now;
WakeUp();
2000-10-08 09:25:20 +02:00
}
}
else if (receivedData) { // the last data before releasing the key hasn't been fetched yet
if (receivedRepeat) { // it was a repeat, so let's make it a release
if (time_ms() - LastTime > REPEATDELAY) {
receivedRepeat = false;
receivedRelease = true;
WakeUp();
}
2000-10-08 09:25:20 +02:00
}
}
else if (receivedRepeat) { // all data has already been fetched, but the last one was a repeat, so let's generate a release
2001-07-27 10:18:57 +02:00
if (time_ms() - LastTime > REPEATDELAY) {
receivedData = receivedRelease = true;
receivedRepeat = false;
WakeUp();
}
2000-10-08 09:25:20 +02:00
}
}
2000-07-15 12:39:20 +02:00
}
bool cRcIoLIRC::GetCommand(unsigned int *Command, bool *Repeat, bool *Release)
2000-07-15 12:39:20 +02:00
{
2000-10-08 09:25:20 +02:00
if (receivedData) { // first we check the boolean flag without a lock, to avoid delays
2000-07-15 12:39:20 +02:00
2000-10-08 09:25:20 +02:00
LOCK_THREAD;
2000-07-15 12:39:20 +02:00
2000-10-08 09:25:20 +02:00
if (receivedData) { // need to check again, since the status might have changed while waiting for the lock
if (Command)
*Command = Keys.Encode(keyName);
if (Repeat)
*Repeat = receivedRepeat;
if (Release)
*Release = receivedRelease;
2000-10-08 09:25:20 +02:00
receivedData = false;
2000-07-15 12:39:20 +02:00
return true;
}
}
return false;
}
#endif