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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
/*
* diseqc.c: DiSEqC handling
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
* $Id: diseqc.c 4.1 2017/01/09 15:10:40 kls Exp $
*/
#include "diseqc.h"
#include <ctype.h>
#include <linux/dvb/frontend.h>
#include <sys/ioctl.h>
#include "sources.h"
#include "thread.h"
#define ALL_DEVICES (~0) // all bits set to '1'
#define MAX_DEVICES 32 // each bit in a 32-bit integer represents one device
static int CurrentDevices = 0;
static bool IsDeviceNumbers(const char *s)
{
return *s && s[strlen(s) - 1] == ':';
}
static bool ParseDeviceNumbers(const char *s)
{
if (IsDeviceNumbers(s)) {
CurrentDevices = 0;
const char *p = s;
while (*p && *p != ':') {
char *t = NULL;
int d = strtol(p, &t, 10);
p = t;
if (0 < d && d <= MAX_DEVICES)
CurrentDevices |= (1 << d - 1);
else {
esyslog("ERROR: invalid device number %d in '%s'", d, s);
return false;
}
}
}
return true;
}
// --- cDiseqcPositioner -----------------------------------------------------
// See http://www.eutelsat.com/files/live/sites/eutelsatv2/files/contributed/satellites/pdf/Diseqc/associated%20docs/positioner_appli_notice.pdf
cDiseqcPositioner::cDiseqcPositioner(void)
{
SetCapabilities(pcCanDrive |
pcCanStep |
pcCanHalt |
pcCanSetLimits |
pcCanDisableLimits |
pcCanEnableLimits |
pcCanStorePosition |
pcCanRecalcPositions |
pcCanGotoPosition |
pcCanGotoAngle
);
}
void cDiseqcPositioner::SendDiseqc(uint8_t *Codes, int NumCodes)
{
struct dvb_diseqc_master_cmd cmd;
NumCodes = min(NumCodes, int(sizeof(cmd.msg) - 2));
cmd.msg_len = 0;
cmd.msg[cmd.msg_len++] = 0xE0;
cmd.msg[cmd.msg_len++] = 0x31;
for (int i = 0; i < NumCodes; i++)
cmd.msg[cmd.msg_len++] = Codes[i];
CHECK(ioctl(Frontend(), FE_DISEQC_SEND_MASTER_CMD, &cmd));
}
void cDiseqcPositioner::Drive(ePositionerDirection Direction)
{
uint8_t Code[] = { uint8_t(Direction == pdLeft ? 0x68 : 0x69), 0x00 };
SendDiseqc(Code, 2);
}
void cDiseqcPositioner::Step(ePositionerDirection Direction, uint Steps)
{
if (Steps == 0)
return;
uint8_t Code[] = { uint8_t(Direction == pdLeft ? 0x68 : 0x69), 0xFF };
Code[1] -= min(Steps, uint(0x7F)) - 1;
SendDiseqc(Code, 2);
}
void cDiseqcPositioner::Halt(void)
{
uint8_t Code[] = { 0x60 };
SendDiseqc(Code, 1);
}
void cDiseqcPositioner::SetLimit(ePositionerDirection Direction)
{
uint8_t Code[] = { uint8_t(Direction == pdLeft ? 0x66 : 0x67) };
SendDiseqc(Code, 1);
}
void cDiseqcPositioner::DisableLimits(void)
{
uint8_t Code[] = { 0x63 };
SendDiseqc(Code, 1);
}
void cDiseqcPositioner::EnableLimits(void)
{
uint8_t Code[] = { 0x6A, 0x00 };
SendDiseqc(Code, 2);
}
void cDiseqcPositioner::StorePosition(uint Number)
{
uint8_t Code[] = { 0x6A, uint8_t(Number) };
SendDiseqc(Code, 2);
}
void cDiseqcPositioner::RecalcPositions(uint Number)
{
uint8_t Code[] = { 0x6F, uint8_t(Number), 0x00, 0x00 };
SendDiseqc(Code, 4);
}
void cDiseqcPositioner::GotoPosition(uint Number, int Longitude)
{
uint8_t Code[] = { 0x6B, uint8_t(Number) };
SendDiseqc(Code, 2);
cPositioner::GotoPosition(Number, Longitude);
}
void cDiseqcPositioner::GotoAngle(int Longitude)
{
uint8_t Code[] = { 0x6E, 0x00, 0x00 };
int Angle = CalcHourAngle(Longitude);
int a = abs(Angle);
Code[1] = a / 10 / 16;
Code[2] = a / 10 % 16 * 16 + a % 10 * 16 / 10;
Code[1] |= (Angle < 0) ? 0xE0 : 0xD0;
SendDiseqc(Code, 3);
cPositioner::GotoAngle(Longitude);
}
// --- cScr ------------------------------------------------------------------
cScr::cScr(void)
{
devices = 0;
channel = -1;
userBand = 0;
pin = -1;
used = false;
}
bool cScr::Parse(const char *s)
{
if (IsDeviceNumbers(s))
return ParseDeviceNumbers(s);
devices = CurrentDevices;
bool result = false;
int fields = sscanf(s, "%d %u %d", &channel, &userBand, &pin);
if (fields == 2 || fields == 3) {
if (channel >= 0 && channel < 32) {
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;
bool cScrs::Load(const char *FileName, bool AllowComments, bool MustExist)
{
CurrentDevices = ALL_DEVICES;
return cConfig<cScr>::Load(FileName, AllowComments, MustExist);
}
cScr *cScrs::GetUnused(int Device)
{
cMutexLock MutexLock(&mutex);
for (cScr *p = First(); p; p = Next(p)) {
if (!IsBitSet(p->Devices(), Device - 1))
continue;
if (!p->Used()) {
p->SetUsed(true);
return p;
}
}
return NULL;
}
// --- cDiseqc ---------------------------------------------------------------
cDiseqc::cDiseqc(void)
{
devices = 0;
source = 0;
slof = 0;
polarization = 0;
lof = 0;
position = -1;
scrBank = -1;
commands = NULL;
parsing = false;
}
cDiseqc::~cDiseqc()
{
free(commands);
}
bool cDiseqc::Parse(const char *s)
{
if (IsDeviceNumbers(s))
return ParseDeviceNumbers(s);
devices = CurrentDevices;
bool result = false;
char *sourcebuf = NULL;
int fields = sscanf(s, "%m[^ ] %d %c %d %m[^\n]", &sourcebuf, &slof, &polarization, &lof, &commands);
if (fields == 4)
commands = NULL; //XXX Apparently sscanf() doesn't work correctly if the last %m argument results in an empty string
if (4 <= fields && fields <= 5) {
source = cSource::FromString(sourcebuf);
if (Sources.Get(source)) {
polarization = char(toupper(polarization));
if (polarization == 'V' || polarization == 'H' || polarization == 'L' || polarization == 'R') {
parsing = true;
const char *CurrentAction = NULL;
while (Execute(&CurrentAction, NULL, NULL, NULL, NULL) != daNone)
;
parsing = false;
result = !commands || !*CurrentAction;
}
else
esyslog("ERROR: unknown polarization '%c'", polarization);
}
else
esyslog("ERROR: unknown source '%s'", sourcebuf);
}
free(sourcebuf);
return result;
}
int cDiseqc::SetScrFrequency(int SatFrequency, const cScr *Scr, uint8_t *Codes) const
{
if ((Codes[0] & 0xF0) == 0x70 ) { // EN50607 aka JESS
int t = SatFrequency == 0 ? 0 : (SatFrequency - 100);
if (t < 2048 && Scr->Channel() >= 0 && Scr->Channel() < 32) {
Codes[1] = t >> 8 | Scr->Channel() << 3;
Codes[2] = t;
Codes[3] = (t == 0 ? 0 : scrBank);
if (t)
return Scr->UserBand();
}
}
else { // EN50494 aka Unicable
int t = SatFrequency == 0 ? 0 : (SatFrequency + Scr->UserBand() + 2) / 4 - 350; // '+ 2' together with '/ 4' results in rounding!
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;
}
}
esyslog("ERROR: invalid SCR channel number %d or frequency %d", Scr->Channel(),SatFrequency);
return 0;
}
int cDiseqc::SetScrPin(const cScr *Scr, uint8_t *Codes) const
{
if ((Codes[0] & 0xF0) == 0x70 ) { // EN50607 aka JESS
if (Scr->Pin() >= 0 && Scr->Pin() <= 255) {
Codes[0] = 0x71;
Codes[4] = Scr->Pin();
return 5;
}
else {
Codes[0] = 0x70;
return 4;
}
}
else { // EN50494 aka Unicable
if (Scr->Pin() >= 0 && Scr->Pin() <= 255) {
Codes[2] = 0x5C;
Codes[5] = Scr->Pin();
return 6;
}
else {
Codes[2] = 0x5A;
return 5;
}
}
}
const char *cDiseqc::Wait(const char *s) const
{
char *p = NULL;
errno = 0;
int n = strtol(s, &p, 10);
if (!errno && p != s && n >= 0) {
if (!parsing)
cCondWait::SleepMs(n);
return p;
}
esyslog("ERROR: invalid value for wait time in '%s'", s - 1);
return NULL;
}
const char *cDiseqc::GetPosition(const char *s) const
{
if (!*s || !isdigit(*s)) {
position = 0;
return s;
}
char *p = NULL;
errno = 0;
int n = strtol(s, &p, 10);
if (!errno && p != s && n >= 0 && n < 0xFF) {
if (parsing) {
if (position < 0)
position = n;
else
esyslog("ERROR: more than one position in '%s'", s - 1);
}
return p;
}
esyslog("ERROR: invalid satellite position in '%s'", s - 1);
return NULL;
}
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 < 256) {
if (parsing) {
if (scrBank < 0)
scrBank = n;
else
esyslog("ERROR: more than one scr bank in '%s'", s - 1);
}
return p;
}
esyslog("ERROR: invalid value for scr bank in '%s'", s - 1);
return NULL;
}
const char *cDiseqc::GetCodes(const char *s, uchar *Codes, uint8_t *MaxCodes) const
{
const char *e = strchr(s, ']');
if (e) {
int NumCodes = 0;
const char *t = s;
while (t < e) {
if (NumCodes < MaxDiseqcCodes) {
errno = 0;
char *p;
int n = strtol(t, &p, 16);
if (!errno && p != t && 0 <= n && n <= 255) {
if (Codes) {
if (NumCodes < *MaxCodes)
Codes[NumCodes++] = uchar(n);
else {
esyslog("ERROR: too many codes in code sequence '%s'", s - 1);
return NULL;
}
}
t = skipspace(p);
}
else {
esyslog("ERROR: invalid code at '%s'", t);
return NULL;
}
}
else {
esyslog("ERROR: too many codes in code sequence '%s'", s - 1);
return NULL;
}
}
if (MaxCodes)
*MaxCodes = NumCodes;
return e + 1;
}
else
esyslog("ERROR: missing closing ']' in code sequence '%s'", s - 1);
return NULL;
}
cDiseqc::eDiseqcActions cDiseqc::Execute(const char **CurrentAction, uchar *Codes, uint8_t *MaxCodes, const cScr *Scr, int *Frequency) const
{
if (!*CurrentAction)
*CurrentAction = commands;
while (*CurrentAction && **CurrentAction) {
switch (*(*CurrentAction)++) {
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;
case 'W': *CurrentAction = Wait(*CurrentAction); return daWait;
case 'P': *CurrentAction = GetPosition(*CurrentAction);
if (Setup.UsePositioner)
return position ? daPositionN : daPositionA;
break;
case 'S': *CurrentAction = GetScrBank(*CurrentAction); return daScr;
case '[': *CurrentAction = GetCodes(*CurrentAction, Codes, MaxCodes);
if (*CurrentAction) {
if (Scr && Frequency) {
*Frequency = SetScrFrequency(*Frequency, Scr, Codes);
*MaxCodes = SetScrPin(Scr, Codes);
}
return daCodes;
}
break;
default: esyslog("ERROR: unknown diseqc code '%c'", *(*CurrentAction - 1));
return daNone;
}
}
return daNone;
}
// --- cDiseqcs --------------------------------------------------------------
cDiseqcs Diseqcs;
bool cDiseqcs::Load(const char *FileName, bool AllowComments, bool MustExist)
{
CurrentDevices = ALL_DEVICES;
return cConfig<cDiseqc>::Load(FileName, AllowComments, MustExist);
}
const cDiseqc *cDiseqcs::Get(int Device, int Source, int Frequency, char Polarization, const cScr **Scr) const
{
for (const cDiseqc *p = First(); p; p = Next(p)) {
if (!IsBitSet(p->Devices(), Device - 1))
continue;
if (cSource::Matches(p->Source(), Source) && p->Slof() > Frequency && p->Polarization() == toupper(Polarization)) {
if (p->IsScr() && Scr && !*Scr) {
*Scr = Scrs.GetUnused(Device);
if (*Scr)
dsyslog("SCR %d assigned to device %d", (*Scr)->Channel(), Device);
else
esyslog("ERROR: no free SCR entry available for device %d", Device);
}
return p;
}
}
return NULL;
}
|