2002-10-06 10:25:42 +02:00
|
|
|
/*
|
|
|
|
* diseqc.c: DiSEqC handling
|
|
|
|
*
|
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
|
|
* how to reach the author.
|
|
|
|
*
|
2013-05-02 09:31:31 +02:00
|
|
|
* $Id: diseqc.c 3.1 2013/05/02 09:30:33 kls Exp $
|
2002-10-06 10:25:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "diseqc.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "sources.h"
|
2004-10-24 11:12:05 +02:00
|
|
|
#include "thread.h"
|
2002-10-06 10:25:42 +02:00
|
|
|
|
2011-09-17 14:13:31 +02:00
|
|
|
static bool ParseDeviceNumbers(const char *s, int &Devices)
|
|
|
|
{
|
|
|
|
if (*s && s[strlen(s) - 1] == ':') {
|
|
|
|
const char *p = s;
|
|
|
|
while (*p && *p != ':') {
|
|
|
|
char *t = NULL;
|
|
|
|
int d = strtol(p, &t, 10);
|
|
|
|
p = t;
|
|
|
|
if (0 < d && d < 31)
|
|
|
|
Devices |= (1 << d - 1);
|
|
|
|
else {
|
|
|
|
esyslog("ERROR: invalid device number %d in '%s'", d, s);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-11 14:09:03 +02:00
|
|
|
// --- cScr ------------------------------------------------------------------
|
|
|
|
|
|
|
|
cScr::cScr(void)
|
|
|
|
{
|
2011-09-17 14:13:31 +02:00
|
|
|
devices = 0;
|
2011-09-11 14:09:03 +02:00
|
|
|
channel = -1;
|
|
|
|
userBand = 0;
|
|
|
|
pin = -1;
|
|
|
|
used = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cScr::Parse(const char *s)
|
|
|
|
{
|
2011-09-17 14:13:31 +02:00
|
|
|
if (!ParseDeviceNumbers(s, devices))
|
|
|
|
return false;
|
|
|
|
if (devices)
|
|
|
|
return true;
|
2011-09-11 14:09:03 +02:00
|
|
|
bool result = false;
|
|
|
|
int fields = sscanf(s, "%d %u %d", &channel, &userBand, &pin);
|
|
|
|
if (fields == 2 || fields == 3) {
|
|
|
|
if (channel >= 0 && channel < 8) {
|
|
|
|
result = true;
|
|
|
|
if (fields == 3 && (pin < 0 || pin > 255)) {
|
|
|
|
esyslog("Error: invalid SCR pin '%d'", pin);
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("Error: invalid SCR channel '%d'", channel);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cScrs -----------------------------------------------------------------
|
|
|
|
|
|
|
|
cScrs Scrs;
|
|
|
|
|
2011-09-17 14:13:31 +02:00
|
|
|
cScr *cScrs::GetUnused(int Device)
|
2011-09-11 14:09:03 +02:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2011-09-17 14:13:31 +02:00
|
|
|
int Devices = 0;
|
2011-09-11 14:09:03 +02:00
|
|
|
for (cScr *p = First(); p; p = Next(p)) {
|
2011-09-17 14:13:31 +02:00
|
|
|
if (p->Devices()) {
|
|
|
|
Devices = p->Devices();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Devices && !(Devices & (1 << Device - 1)))
|
|
|
|
continue;
|
2011-09-11 14:09:03 +02:00
|
|
|
if (!p->Used()) {
|
|
|
|
p->SetUsed(true);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-02-10 14:11:47 +01:00
|
|
|
// --- cDiseqc ---------------------------------------------------------------
|
2002-10-06 10:25:42 +02:00
|
|
|
|
|
|
|
cDiseqc::cDiseqc(void)
|
|
|
|
{
|
2010-02-06 15:56:01 +01:00
|
|
|
devices = 0;
|
|
|
|
source = 0;
|
|
|
|
slof = 0;
|
|
|
|
polarization = 0;
|
|
|
|
lof = 0;
|
2011-09-11 14:09:03 +02:00
|
|
|
scrBank = -1;
|
2002-10-06 10:25:42 +02:00
|
|
|
commands = NULL;
|
|
|
|
parsing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cDiseqc::~cDiseqc()
|
|
|
|
{
|
|
|
|
free(commands);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cDiseqc::Parse(const char *s)
|
|
|
|
{
|
2011-09-17 14:13:31 +02:00
|
|
|
if (!ParseDeviceNumbers(s, devices))
|
|
|
|
return false;
|
|
|
|
if (devices)
|
|
|
|
return true;
|
2002-10-06 10:25:42 +02:00
|
|
|
bool result = false;
|
|
|
|
char *sourcebuf = NULL;
|
|
|
|
int fields = sscanf(s, "%a[^ ] %d %c %d %a[^\n]", &sourcebuf, &slof, &polarization, &lof, &commands);
|
|
|
|
if (fields == 4)
|
|
|
|
commands = NULL; //XXX Apparently sscanf() doesn't work correctly if the last %a argument results in an empty string
|
|
|
|
if (4 <= fields && fields <= 5) {
|
|
|
|
source = cSource::FromString(sourcebuf);
|
|
|
|
if (Sources.Get(source)) {
|
2009-12-06 12:57:45 +01:00
|
|
|
polarization = char(toupper(polarization));
|
2005-01-09 13:18:15 +01:00
|
|
|
if (polarization == 'V' || polarization == 'H' || polarization == 'L' || polarization == 'R') {
|
2002-10-06 10:25:42 +02:00
|
|
|
parsing = true;
|
2011-05-22 10:51:03 +02:00
|
|
|
const char *CurrentAction = NULL;
|
2011-09-11 14:09:03 +02:00
|
|
|
while (Execute(&CurrentAction, NULL, NULL, NULL, NULL) != daNone)
|
2002-12-08 09:55:26 +01:00
|
|
|
;
|
2002-10-06 10:25:42 +02:00
|
|
|
parsing = false;
|
2002-12-08 09:55:26 +01:00
|
|
|
result = !commands || !*CurrentAction;
|
2002-10-06 10:25:42 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("ERROR: unknown polarization '%c'", polarization);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("ERROR: unknown source '%s'", sourcebuf);
|
|
|
|
}
|
|
|
|
free(sourcebuf);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-09-11 14:09:03 +02:00
|
|
|
uint cDiseqc::SetScrFrequency(uint SatFrequency, const cScr *Scr, uint8_t *Codes) const
|
|
|
|
{
|
2011-09-17 10:41:00 +02:00
|
|
|
uint t = SatFrequency == 0 ? 0 : (SatFrequency + Scr->UserBand() + 2) / 4 - 350; // '+ 2' together with '/ 4' results in rounding!
|
2011-09-11 14:09:03 +02:00
|
|
|
if (t < 1024 && Scr->Channel() >= 0 && Scr->Channel() < 8) {
|
|
|
|
Codes[3] = t >> 8 | (t == 0 ? 0 : scrBank << 2) | Scr->Channel() << 5;
|
|
|
|
Codes[4] = t;
|
|
|
|
if (t)
|
|
|
|
return (t + 350) * 4 - SatFrequency;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cDiseqc::SetScrPin(const cScr *Scr, uint8_t *Codes) const
|
|
|
|
{
|
|
|
|
if (Scr->Pin() >= 0 && Scr->Pin() <= 255) {
|
|
|
|
Codes[2] = 0x5C;
|
|
|
|
Codes[5] = Scr->Pin();
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Codes[2] = 0x5A;
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-22 10:51:03 +02:00
|
|
|
const char *cDiseqc::Wait(const char *s) const
|
2002-10-06 10:25:42 +02:00
|
|
|
{
|
|
|
|
char *p = NULL;
|
|
|
|
errno = 0;
|
|
|
|
int n = strtol(s, &p, 10);
|
|
|
|
if (!errno && p != s && n >= 0) {
|
|
|
|
if (!parsing)
|
2004-10-24 11:12:05 +02:00
|
|
|
cCondWait::SleepMs(n);
|
2002-10-06 10:25:42 +02:00
|
|
|
return p;
|
|
|
|
}
|
2005-12-30 15:43:21 +01:00
|
|
|
esyslog("ERROR: invalid value for wait time in '%s'", s - 1);
|
2002-10-06 10:25:42 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-11 14:09:03 +02:00
|
|
|
const char *cDiseqc::GetScrBank(const char *s) const
|
|
|
|
{
|
|
|
|
char *p = NULL;
|
|
|
|
errno = 0;
|
|
|
|
int n = strtol(s, &p, 10);
|
|
|
|
if (!errno && p != s && n >= 0 && n < 8) {
|
|
|
|
if (parsing) {
|
|
|
|
if (scrBank < 0)
|
|
|
|
scrBank = n;
|
|
|
|
else
|
|
|
|
esyslog("ERROR: more than one scr bank in '%s'", s - 1);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2013-05-02 09:31:31 +02:00
|
|
|
esyslog("ERROR: invalid value for scr bank in '%s'", s - 1);
|
2011-09-11 14:09:03 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-10 13:55:08 +02:00
|
|
|
const char *cDiseqc::GetCodes(const char *s, uchar *Codes, uint8_t *MaxCodes) const
|
2002-10-06 10:25:42 +02:00
|
|
|
{
|
2011-05-22 10:51:03 +02:00
|
|
|
const char *e = strchr(s, ']');
|
2002-10-06 10:25:42 +02:00
|
|
|
if (e) {
|
2011-05-22 10:51:03 +02:00
|
|
|
int NumCodes = 0;
|
|
|
|
const char *t = s;
|
2002-10-06 10:25:42 +02:00
|
|
|
while (t < e) {
|
2011-05-22 10:51:03 +02:00
|
|
|
if (NumCodes < MaxDiseqcCodes) {
|
2002-10-06 10:25:42 +02:00
|
|
|
errno = 0;
|
2011-08-06 10:45:05 +02:00
|
|
|
char *p;
|
2002-10-06 10:25:42 +02:00
|
|
|
int n = strtol(t, &p, 16);
|
|
|
|
if (!errno && p != t && 0 <= n && n <= 255) {
|
2011-09-10 13:55:08 +02:00
|
|
|
if (Codes) {
|
|
|
|
if (NumCodes < *MaxCodes)
|
|
|
|
Codes[NumCodes++] = uchar(n);
|
|
|
|
else {
|
|
|
|
esyslog("ERROR: too many codes in code sequence '%s'", s - 1);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-05-22 10:51:03 +02:00
|
|
|
}
|
2002-10-06 10:25:42 +02:00
|
|
|
t = skipspace(p);
|
|
|
|
}
|
|
|
|
else {
|
2005-12-30 15:43:21 +01:00
|
|
|
esyslog("ERROR: invalid code at '%s'", t);
|
2002-10-06 10:25:42 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
esyslog("ERROR: too many codes in code sequence '%s'", s - 1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-09-10 13:55:08 +02:00
|
|
|
if (MaxCodes)
|
|
|
|
*MaxCodes = NumCodes;
|
2002-10-06 10:25:42 +02:00
|
|
|
return e + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("ERROR: missing closing ']' in code sequence '%s'", s - 1);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-11 14:09:03 +02:00
|
|
|
cDiseqc::eDiseqcActions cDiseqc::Execute(const char **CurrentAction, uchar *Codes, uint8_t *MaxCodes, const cScr *Scr, uint *Frequency) const
|
2002-10-06 10:25:42 +02:00
|
|
|
{
|
2002-12-08 09:55:26 +01:00
|
|
|
if (!*CurrentAction)
|
|
|
|
*CurrentAction = commands;
|
|
|
|
while (*CurrentAction && **CurrentAction) {
|
|
|
|
switch (*(*CurrentAction)++) {
|
2002-10-06 10:25:42 +02:00
|
|
|
case ' ': break;
|
|
|
|
case 't': return daToneOff;
|
|
|
|
case 'T': return daToneOn;
|
|
|
|
case 'v': return daVoltage13;
|
|
|
|
case 'V': return daVoltage18;
|
|
|
|
case 'A': return daMiniA;
|
|
|
|
case 'B': return daMiniB;
|
2002-12-08 09:55:26 +01:00
|
|
|
case 'W': *CurrentAction = Wait(*CurrentAction); break;
|
2011-09-11 14:09:03 +02:00
|
|
|
case 'S': *CurrentAction = GetScrBank(*CurrentAction); break;
|
|
|
|
case '[': *CurrentAction = GetCodes(*CurrentAction, Codes, MaxCodes);
|
|
|
|
if (*CurrentAction) {
|
|
|
|
if (Scr && Frequency) {
|
|
|
|
*Frequency = SetScrFrequency(*Frequency, Scr, Codes);
|
|
|
|
*MaxCodes = SetScrPin(Scr, Codes);
|
|
|
|
}
|
|
|
|
return daCodes;
|
|
|
|
}
|
|
|
|
break;
|
2002-10-06 10:25:42 +02:00
|
|
|
default: return daNone;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return daNone;
|
|
|
|
}
|
|
|
|
|
2008-02-10 14:11:47 +01:00
|
|
|
// --- cDiseqcs --------------------------------------------------------------
|
2002-10-06 10:25:42 +02:00
|
|
|
|
|
|
|
cDiseqcs Diseqcs;
|
|
|
|
|
2011-09-11 14:09:03 +02:00
|
|
|
const cDiseqc *cDiseqcs::Get(int Device, int Source, int Frequency, char Polarization, const cScr **Scr) const
|
2002-10-06 10:25:42 +02:00
|
|
|
{
|
2010-02-06 15:56:01 +01:00
|
|
|
int Devices = 0;
|
2011-05-22 10:51:03 +02:00
|
|
|
for (const cDiseqc *p = First(); p; p = Next(p)) {
|
2010-02-06 15:56:01 +01:00
|
|
|
if (p->Devices()) {
|
|
|
|
Devices = p->Devices();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Devices && !(Devices & (1 << Device - 1)))
|
|
|
|
continue;
|
2011-09-11 14:09:03 +02:00
|
|
|
if (p->Source() == Source && p->Slof() > Frequency && p->Polarization() == toupper(Polarization)) {
|
|
|
|
if (p->IsScr() && Scr && !*Scr) {
|
2011-09-17 14:13:31 +02:00
|
|
|
*Scr = Scrs.GetUnused(Device);
|
2011-09-11 14:09:03 +02:00
|
|
|
if (*Scr)
|
2011-09-17 14:13:31 +02:00
|
|
|
dsyslog("SCR %d assigned to device %d", (*Scr)->Channel(), Device);
|
2011-09-11 14:09:03 +02:00
|
|
|
else
|
|
|
|
esyslog("ERROR: no free SCR entry available for device %d", Device);
|
|
|
|
}
|
2011-05-21 22:07:08 +02:00
|
|
|
return p;
|
2011-09-11 14:09:03 +02:00
|
|
|
}
|
2002-10-06 10:25:42 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|