aboutsummaryrefslogtreecommitdiffstats
path: root/dvb-t/se-Karlstad_Sormon
blob: 658bc70dda71bca3c7d31bd3d44a5b152acfbe22 (plain)
1
2
3
4
5
6
7
8
9
# Sweden - Karlstad Sörmon Valid from 2007 09 26. Ver. 2 Correct FEC
# T freq bw fec_hi fec_lo mod transmission-mode guard-interval hierarchy
# Channels
# Mux3=30
T 546000000 8MHz 2/3 NONE QAM64 8k 1/8 NONE
# Mux4=42
T 642000000 8MHz 2/3 NONE QAM64 8k 1/8 NONE
# Mux1=43
T 650000000 8MHz 2/3 NONE QAM64 8k 1/8 NONE
5' href='#n185'>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 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
/*
	libdvbsec - an SEC library

	Copyright (C) 2005 Manu Abraham <abraham.manu@gmail.com>
	Copyright (C) 2006 Andrew de Quincey <adq_dvb@lidskialf.net>

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.
	This library is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.
	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <linux/types.h>
#include <libdvbapi/dvbfe.h>
#include "dvbsec_api.h"

// uncomment this to make dvbsec_command print out debug instead of talking to a frontend
// #define TEST_SEC_COMMAND 1

int dvbsec_set(struct dvbfe_handle *fe,
		   struct dvbsec_config *sec_config,
		   enum dvbsec_diseqc_polarization polarization,
		   enum dvbsec_diseqc_switch sat_pos,
		   enum dvbsec_diseqc_switch switch_option,
		   struct dvbfe_parameters *params,
		   int timeout)
{
	int tmp;
	struct dvbfe_parameters localparams;
	struct dvbfe_parameters *topass = params;

	// perform SEC
	if (sec_config != NULL) {
		switch(sec_config->config_type) {
		case DVBSEC_CONFIG_NONE:
			break;

		case DVBSEC_CONFIG_POWER:
			dvbfe_set_voltage(fe, DVBFE_SEC_VOLTAGE_13);
			break;

		case DVBSEC_CONFIG_STANDARD:
		{
			// calculate the correct oscillator value
			enum dvbsec_diseqc_oscillator osc = DISEQC_OSCILLATOR_LOW;
			if (sec_config->switch_frequency && (sec_config->switch_frequency < params->frequency))
				osc = DISEQC_OSCILLATOR_HIGH;

			if ((tmp = dvbsec_std_sequence(fe,
			     				  osc,
							  polarization,
							  sat_pos,
							  switch_option)) < 0)
				return tmp;
			break;
		}

		case DVBSEC_CONFIG_ADVANCED:
		{
			// are we high or not?
			int high = 0;
			if (sec_config->switch_frequency && (sec_config->switch_frequency < params->frequency))
				high = 1;

			//  determine correct string
			char *cmd = NULL;
			switch(polarization) {
			case DISEQC_POLARIZATION_H:
				if (!high)
					cmd = sec_config->adv_cmd_lo_h;
				else
					cmd = sec_config->adv_cmd_hi_h;
				break;
			case DISEQC_POLARIZATION_V:
				if (!high)
					cmd = sec_config->adv_cmd_lo_v;
				else
					cmd = sec_config->adv_cmd_hi_v;
				break;
			case DISEQC_POLARIZATION_L:
				if (!high)
					cmd = sec_config->adv_cmd_lo_l;
				else
					cmd = sec_config->adv_cmd_hi_l;
				break;
			case DISEQC_POLARIZATION_R:
				if (!high)
					cmd = sec_config->adv_cmd_lo_r;
				else
					cmd = sec_config->adv_cmd_hi_r;
				break;
			default:
				return -EINVAL;
			}

			// do it
			if (cmd)
				if ((tmp = dvbsec_command(fe, cmd)) < 0)
					return tmp;
			break;
		}
		}

		// work out the correct LOF value
		uint32_t lof = 0;
		if ((sec_config->switch_frequency == 0) || (params->frequency < sec_config->switch_frequency)) {
			// LOW band
			switch(polarization) {
			case DISEQC_POLARIZATION_H:
				lof = sec_config->lof_lo_h;
				break;
			case DISEQC_POLARIZATION_V:
				lof = sec_config->lof_lo_v;
				break;
			case DISEQC_POLARIZATION_L:
				lof = sec_config->lof_lo_l;
				break;
			case DISEQC_POLARIZATION_R:
				lof = sec_config->lof_lo_r;
				break;
			case DISEQC_POLARIZATION_UNCHANGED:
				break;
			}
		} else {
			// HIGH band
			switch(polarization) {
			case DISEQC_POLARIZATION_H:
				lof = sec_config->lof_hi_h;
				break;
			case DISEQC_POLARIZATION_V:
				lof = sec_config->lof_hi_v;
				break;
			case DISEQC_POLARIZATION_L:
				lof = sec_config->lof_hi_l;
				break;
			case DISEQC_POLARIZATION_R:
				lof = sec_config->lof_hi_r;
				break;
			case DISEQC_POLARIZATION_UNCHANGED:
				break;
			}
		}

		// do frequency adjustment
		if (lof) {
			memcpy(&localparams, params, sizeof(struct dvbfe_parameters));
			int tmpfreq = localparams.frequency - lof;

			if (tmpfreq < 0)
				tmpfreq *= -1;
			localparams.frequency = (uint32_t) tmpfreq;
			topass = &localparams;
		}
	}

	// set the frontend!
	return dvbfe_set(fe, topass, timeout);
}

int dvbsec_std_sequence(struct dvbfe_handle *fe,
			   enum dvbsec_diseqc_oscillator oscillator,
			   enum dvbsec_diseqc_polarization polarization,
			   enum dvbsec_diseqc_switch sat_pos,
			   enum dvbsec_diseqc_switch switch_option)
{
	dvbfe_set_22k_tone(fe, DVBFE_SEC_TONE_OFF);

	switch(polarization) {
	case DISEQC_POLARIZATION_V:
	case DISEQC_POLARIZATION_R:
		dvbfe_set_voltage(fe, DVBFE_SEC_VOLTAGE_13);
		break;
	case DISEQC_POLARIZATION_H:
	case DISEQC_POLARIZATION_L:
		dvbfe_set_voltage(fe, DVBFE_SEC_VOLTAGE_18);
		break;
	default:
		return -EINVAL;
	}

	dvbsec_diseqc_set_committed_switches(fe,
					    DISEQC_ADDRESS_ANY_DEVICE,
					    oscillator,
					    polarization,
					    sat_pos,
					    switch_option);

	usleep(15000);

	switch(sat_pos) {
	case DISEQC_SWITCH_A:
		dvbfe_set_tone_data_burst(fe, DVBFE_SEC_MINI_A);
		break;
	case DISEQC_SWITCH_B:
		dvbfe_set_tone_data_burst(fe, DVBFE_SEC_MINI_B);
		break;
	default:
		break;
	}

	if (sat_pos != DISEQC_SWITCH_UNCHANGED)
		usleep(15000);

	switch(oscillator) {
	case DISEQC_OSCILLATOR_LOW:
		dvbfe_set_22k_tone(fe, DVBFE_SEC_TONE_OFF);
		break;
	case DISEQC_OSCILLATOR_HIGH:
		dvbfe_set_22k_tone(fe, DVBFE_SEC_TONE_ON);
		break;
	default:
		break;
	}

	return 0;
}

int dvbsec_diseqc_set_reset(struct dvbfe_handle *fe,
			   enum dvbsec_diseqc_address address,
			   enum dvbsec_diseqc_reset state)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x00 };

	if (state == DISEQC_RESET_CLEAR)
		data[2] = 0x01;

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_set_power(struct dvbfe_handle *fe,
			   enum dvbsec_diseqc_address address,
			   enum dvbsec_diseqc_power state)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x02 };

	if (state == DISEQC_POWER_ON)
		data[2] = 0x03;

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_set_listen(struct dvbfe_handle *fe,
			    enum dvbsec_diseqc_address address,
			    enum dvbsec_diseqc_listen state)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x30 };

	if (state == DISEQC_LISTEN_AWAKE)
		data[2] = 0x31;

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_set_committed_switches(struct dvbfe_handle *fe,
					enum dvbsec_diseqc_address address,
					enum dvbsec_diseqc_oscillator oscillator,
					enum dvbsec_diseqc_polarization polarization,
					enum dvbsec_diseqc_switch sat_pos,
					enum dvbsec_diseqc_switch switch_option)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x38, 0x00 };

	switch(oscillator) {
	case DISEQC_OSCILLATOR_LOW:
		data[3] |= 0x10;
		break;
	case DISEQC_OSCILLATOR_HIGH:
		data[3] |= 0x11;
		break;
	case DISEQC_OSCILLATOR_UNCHANGED:
		break;
	}
	switch(polarization) {
	case DISEQC_POLARIZATION_V:
	case DISEQC_POLARIZATION_R:
		data[3] |= 0x20;
		break;
	case DISEQC_POLARIZATION_H:
	case DISEQC_POLARIZATION_L:
		data[3] |= 0x22;
		break;
	default:
		break;
	}
	switch(sat_pos) {
	case DISEQC_SWITCH_A:
		data[3] |= 0x40;
		break;
	case DISEQC_SWITCH_B:
		data[3] |= 0x44;
		break;
	case DISEQC_SWITCH_UNCHANGED:
		break;
	}
	switch(switch_option) {
	case DISEQC_SWITCH_A:
		data[3] |= 0x80;
		break;
	case DISEQC_SWITCH_B:
		data[3] |= 0x88;
		break;
	case DISEQC_SWITCH_UNCHANGED:
		break;
	}

	if (data[3] == 0)
		return 0;

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_set_uncommitted_switches(struct dvbfe_handle *fe,
					  enum dvbsec_diseqc_address address,
					  enum dvbsec_diseqc_switch s1,
					  enum dvbsec_diseqc_switch s2,
					  enum dvbsec_diseqc_switch s3,
					  enum dvbsec_diseqc_switch s4)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x39, 0x00 };

	switch(s1) {
	case DISEQC_SWITCH_A:
		data[3] |= 0x10;
		break;
	case DISEQC_SWITCH_B:
		data[3] |= 0x11;
		break;
	case DISEQC_SWITCH_UNCHANGED:
		break;
	}
	switch(s2) {
	case DISEQC_SWITCH_A:
		data[3] |= 0x20;
		break;
	case DISEQC_SWITCH_B:
		data[3] |= 0x22;
		break;
	case DISEQC_SWITCH_UNCHANGED:
		break;
	}
	switch(s3) {
	case DISEQC_SWITCH_A:
		data[3] |= 0x40;
		break;
	case DISEQC_SWITCH_B:
		data[3] |= 0x44;
		break;
	case DISEQC_SWITCH_UNCHANGED:
		break;
	}
	switch(s4) {
	case DISEQC_SWITCH_A:
		data[3] |= 0x80;
		break;
	case DISEQC_SWITCH_B:
		data[3] |= 0x88;
		break;
	case DISEQC_SWITCH_UNCHANGED:
		break;
	}

	if (data[3] == 0)
		return 0;

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_set_analog_value(struct dvbfe_handle *fe,
				  enum dvbsec_diseqc_address address,
				  enum dvbsec_diseqc_analog_id id,
				  uint8_t value)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x48, value };

	if (id == DISEQC_ANALOG_ID_A1)
		data[2] = 0x49;

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_set_frequency(struct dvbfe_handle *fe,
			       enum dvbsec_diseqc_address address,
			       uint32_t frequency)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x58, 0x00, 0x00, 0x00 };
	int len = 5;

	uint32_t bcdval = 0;
	int i;
	for(i=0; i<=24;i+=4) {
		bcdval |= ((frequency % 10) << i);
		frequency /= 10;
	}

	data[3] = bcdval >> 16;
	data[4] = bcdval >> 8;
	if (bcdval & 0xff) {
		data[5] = bcdval;
		len++;
	}

	return dvbfe_do_diseqc_command(fe, data, len);
}

int dvbsec_diseqc_set_channel(struct dvbfe_handle *fe,
			     enum dvbsec_diseqc_address address,
			     uint16_t channel)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x59, 0x00, 0x00};

	data[3] = channel >> 8;
	data[4] = channel;

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_halt_satpos(struct dvbfe_handle *fe,
			     enum dvbsec_diseqc_address address)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x60};

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_disable_satpos_limits(struct dvbfe_handle *fe,
				       enum dvbsec_diseqc_address address)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x63};

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_set_satpos_limit(struct dvbfe_handle *fe,
				  enum dvbsec_diseqc_address address,
				  enum dvbsec_diseqc_direction direction)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x66};

	if (direction == DISEQC_DIRECTION_WEST)
		data[2] = 0x67;

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_drive_satpos_motor(struct dvbfe_handle *fe,
				    enum dvbsec_diseqc_address address,
				    enum dvbsec_diseqc_direction direction,
				    enum dvbsec_diseqc_drive_mode mode,
				    uint8_t value)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x68, 0x00};

	if (direction == DISEQC_DIRECTION_WEST)
		data[2] = 0x69;

	switch(mode) {
	case DISEQC_DRIVE_MODE_STEPS:
		data[3] = (value & 0x7f) | 0x80;
		break;
	case DISEQC_DRIVE_MODE_TIMEOUT:
		data[3] = value & 0x7f;
		break;
	}

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_store_satpos_preset(struct dvbfe_handle *fe,
				     enum dvbsec_diseqc_address address,
				     uint8_t id)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x6A, id};

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_goto_satpos_preset(struct dvbfe_handle *fe,
				    enum dvbsec_diseqc_address address,
				    uint8_t id)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x6B, id};

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

int dvbsec_diseqc_recalculate_satpos_positions(struct dvbfe_handle *fe,
					      enum dvbsec_diseqc_address address,
					      int val1,
					      int val2)
{
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x6F, 0x00, 0x00};
	int len = 3;

	if (val1 != -1) {
		data[3] = val1;
		len++;
	}
	if (val2 != -1) {
		data[4] = val2;
		len = 5;
	}

	return dvbfe_do_diseqc_command(fe, data, len);
}

int dvbsec_diseqc_goto_rotator_bearing(struct dvbfe_handle *fe,
				      enum dvbsec_diseqc_address address,
				      float angle)
{
	int integer = (int) angle;
	uint8_t data[] = { DISEQC_FRAMING_MASTER_NOREPLY, address, 0x6e, 0x00, 0x00};

	// transform the fraction into the correct representation
	int fraction = (int) (((angle - integer) * 16.0) + 0.9) & 0x0f;
	switch(fraction) {
	case 1:
	case 4:
	case 7:
	case 9:
	case 12:
	case 15:
		fraction--;
		break;
	}

	// generate the command
	if (integer < -256) {
		return -EINVAL;
	} else if (integer < 0) {
		integer = -integer;
		data[3] = 0xf0;
	} else if (integer < 256) {
		data[3] = 0x00;
	} else if (integer < 512) {
		integer -= 256;
		data[3] = 0x10;
	} else {
		return -EINVAL;
	}
	data[3] |= ((integer / 16) & 0x0f);
	integer = integer % 16;
	data[4] |= ((integer & 0x0f) << 4) | fraction;

	return dvbfe_do_diseqc_command(fe, data, sizeof(data));
}

static int skipwhite(char **line, char *end)
{
	while(**line) {
		if (end && (*line >= end))
			return -1;
		if (!isspace(**line))
			return 0;
		(*line)++;
	}

	return -1;
}

static int getstringupto(char **line, char *end, char *matches, char **ptrdest, int *ptrlen)
{
	char *start = *line;

	while(**line) {
		if (end && (*line >= end))
			break;
		if (strchr(matches, **line)) {
			*ptrdest = start;
			*ptrlen = *line - start;
			return 0;
		}
		(*line)++;
	}

	*ptrdest = start;
	*ptrlen = *line - start;
	return 0;
}

static int parsefunction(char **line,
			 char **nameptr, int *namelen,
			 char **argsptr, int *argslen)
{
	if (skipwhite(line, NULL))
		return -1;

	if (getstringupto(line, NULL, "(", nameptr, namelen))
		return -1;
	if ((*line) == 0)
		return -1;
	(*line)++; // skip the '('
	if (getstringupto(line, NULL, ")", argsptr, argslen))
		return -1;
	if ((*line) == 0)
		return -1;
	(*line)++; // skip the ')'

	return 0;
}

static int parseintarg(char **args, char *argsend, int *result)
{
	char tmp[32];
	char *arg;
	int arglen;

	// skip whitespace
	if (skipwhite(args, argsend))
		return -1;

	// get the arg
	if (getstringupto(args, argsend, ",", &arg, &arglen))
		return -1;
	if ((**args) == ',')
		(*args)++; // skip the ',' if present
	if (arglen > 31)
		arglen = 31;
	strncpy(tmp, arg, arglen);
	tmp[arglen] = 0;

	if (sscanf(tmp, "%i", result) != 1)
		return -1;

	return 0;
}

static int parsechararg(char **args, char *argsend, int *result)
{
	char *arg;
	int arglen;

	// skip whitespace
	if (skipwhite(args, argsend))
		return -1;

	// get the arg
	if (getstringupto(args, argsend, ",", &arg, &arglen))
		return -1;
	if ((**args) == ',')
		(*args)++; // skip the ',' if present
	if (arglen > 0)
		*result = arg[0];

	return 0;
}

static int parsefloatarg(char **args, char *argsend, float *result)
{
	char tmp[32];
	char *arg;
	int arglen;

	// skip whitespace
	if (skipwhite(args, argsend))
		return -1;

	// get the arg
	if (getstringupto(args, argsend, ",", &arg, &arglen))
		return -1;
	if ((**args) == ',')
		(*args)++; // skip the ',' if present
	if (arglen > 31)
		arglen = 31;
	strncpy(tmp, arg, arglen);
	arg[arglen] = 0;

	if (sscanf(tmp, "%f", result) != 1)
		return -1;

	return 0;
}

static enum dvbsec_diseqc_switch parse_switch(int c)
{
	switch(toupper(c)) {
	case 'A':
		return DISEQC_SWITCH_A;
	case 'B':
		return DISEQC_SWITCH_B;
	default:
		return DISEQC_SWITCH_UNCHANGED;
	}
}

int dvbsec_command(struct dvbfe_handle *fe, char *command)
{
	char *name;
	char *args;
	int namelen;
	int argslen;
	int address;
	int iarg;
	int iarg2;
	int iarg3;
	int iarg4;
	float farg;

	while(!parsefunction(&command, &name, &namelen, &args, &argslen)) {
		char *argsend = args+argslen;

		if (!strncasecmp(name, "tone", namelen)) {
			if (parsechararg(&args, argsend, &iarg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("tone: %c\n", iarg);
#else
			if (toupper(iarg) == 'B') {
				dvbfe_set_22k_tone(fe, DVBFE_SEC_TONE_ON);
			} else {
				dvbfe_set_22k_tone(fe, DVBFE_SEC_TONE_OFF);
			}
#endif
		} else if (!strncasecmp(name, "voltage", namelen)) {
			if (parseintarg(&args, argsend, &iarg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("voltage: %i\n", iarg);
#else
			switch(iarg) {
			case 0:
				dvbfe_set_voltage(fe, DVBFE_SEC_VOLTAGE_OFF);
				break;
			case 13:
				dvbfe_set_voltage(fe, DVBFE_SEC_VOLTAGE_13);
				break;
			case 18:
				dvbfe_set_voltage(fe, DVBFE_SEC_VOLTAGE_18);
				break;
			default:
				return -1;
			}
#endif
		} else if (!strncasecmp(name, "toneburst", namelen)) {
			if (parsechararg(&args, argsend, &iarg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("toneburst: %c\n", iarg);
#else
			if (toupper(iarg) == 'B') {
				dvbfe_set_tone_data_burst(fe, DVBFE_SEC_MINI_B);
			} else {
				dvbfe_set_tone_data_burst(fe, DVBFE_SEC_MINI_A);
			}
#endif
		} else if (!strncasecmp(name, "highvoltage", namelen)) {
			if (parseintarg(&args, argsend, &iarg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("highvoltage: %i\n", iarg);
#else
			dvbfe_set_high_lnb_voltage(fe, iarg ? 1 : 0);
#endif
		} else if (!strncasecmp(name, "dishnetworks", namelen)) {
			if (parseintarg(&args, argsend, &iarg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("dishnetworks: %i\n", iarg);
#else
			dvbfe_do_dishnetworks_legacy_command(fe, iarg);
#endif
		} else if (!strncasecmp(name, "wait", namelen)) {
			if (parseintarg(&args, argsend, &iarg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("wait: %i\n", iarg);
#else
			if (iarg)
				usleep(iarg * 1000);
#endif
		} else if (!strncasecmp(name, "Dreset", namelen)) {
			if (parseintarg(&args, argsend, &address))
				return -1;
			if (parseintarg(&args, argsend, &iarg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("Dreset: %i %i\n", address, iarg);
#else
			if (iarg) {
				dvbsec_diseqc_set_reset(fe, address, DISEQC_RESET);
			} else {
				dvbsec_diseqc_set_reset(fe, address, DISEQC_RESET_CLEAR);
			}
#endif
		} else if (!strncasecmp(name, "Dpower", namelen)) {
			if (parseintarg(&args, argsend, &address))
				return -1;
			if (parseintarg(&args, argsend, &iarg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("Dpower: %i %i\n", address, iarg);
#else
			if (iarg) {
				dvbsec_diseqc_set_power(fe, address, DISEQC_POWER_ON);
			} else {
				dvbsec_diseqc_set_power(fe, address, DISEQC_POWER_OFF);
			}
#endif
		} else if (!strncasecmp(name, "Dcommitted", namelen)) {
			if (parseintarg(&args, argsend, &address))
				return -1;
			if (parsechararg(&args, argsend, &iarg))
				return -1;
			if (parsechararg(&args, argsend, &iarg2))
				return -1;
			if (parsechararg(&args, argsend, &iarg3))
				return -1;
			if (parsechararg(&args, argsend, &iarg4))
				return -1;

			enum dvbsec_diseqc_oscillator oscillator;
			switch(toupper(iarg)) {
			case 'H':
				oscillator = DISEQC_OSCILLATOR_HIGH;
				break;
			case 'L':
				oscillator = DISEQC_OSCILLATOR_LOW;
				break;
			default:
				oscillator = DISEQC_OSCILLATOR_UNCHANGED;
				break;
			}

			int polarization = -1;
			switch(toupper(iarg2)) {
			case 'H':
				polarization = DISEQC_POLARIZATION_H;
				break;
			case 'V':
				polarization = DISEQC_POLARIZATION_V;
				break;
			case 'L':
				polarization = DISEQC_POLARIZATION_L;
				break;
			case 'R':
				polarization = DISEQC_POLARIZATION_R;
				break;
			default:
				polarization = -1;
				break;
			}

#ifdef TEST_SEC_COMMAND
			printf("Dcommitted: %i %i %i %i %i\n", address,
			       oscillator,
			       polarization,
			       parse_switch(iarg3),
			       parse_switch(iarg4));
#else
			dvbsec_diseqc_set_committed_switches(fe, address,
							    oscillator,
							    polarization,
							    parse_switch(iarg3),
							    parse_switch(iarg4));
#endif
		} else if (!strncasecmp(name, "Duncommitted", namelen)) {
			if (parsechararg(&args, argsend, &address))
				return -1;
			if (parsechararg(&args, argsend, &iarg))
				return -1;
			if (parsechararg(&args, argsend, &iarg2))
				return -1;
			if (parsechararg(&args, argsend, &iarg3))
				return -1;
			if (parsechararg(&args, argsend, &iarg4))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("Duncommitted: %i %i %i %i %i\n", address,
			       parse_switch(iarg),
			       parse_switch(iarg2),
			       parse_switch(iarg3),
			       parse_switch(iarg4));
#else
			dvbsec_diseqc_set_uncommitted_switches(fe, address,
					parse_switch(iarg),
					parse_switch(iarg2),
					parse_switch(iarg3),
					parse_switch(iarg4));
#endif
		} else if (!strncasecmp(name, "Dfrequency", namelen)) {
			if (parseintarg(&args, argsend, &address))
				return -1;
			if (parseintarg(&args, argsend, &iarg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("Dfrequency: %i %i\n", address, iarg);
#else
			dvbsec_diseqc_set_frequency(fe, address, iarg);
#endif
		} else if (!strncasecmp(name, "Dchannel", namelen)) {
			if (parseintarg(&args, argsend, &address))
				return -1;
			if (parseintarg(&args, argsend, &iarg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("Dchannel: %i %i\n", address, iarg);
#else
			dvbsec_diseqc_set_channel(fe, address, iarg);
#endif
		} else if (!strncasecmp(name, "Dgotopreset", namelen)) {
			if (parseintarg(&args, argsend, &address))
				return -1;
			if (parseintarg(&args, argsend, &iarg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("Dgotopreset: %i %i\n", address, iarg);
#else
			dvbsec_diseqc_goto_satpos_preset(fe, address, iarg);
#endif
		} else if (!strncasecmp(name, "Dgotobearing", namelen)) {
			if (parseintarg(&args, argsend, &address))
				return -1;
			if (parsefloatarg(&args, argsend, &farg))
				return -1;

#ifdef TEST_SEC_COMMAND
			printf("Dgotobearing: %i %f\n", address, farg);
#else
			dvbsec_diseqc_goto_rotator_bearing(fe, address, farg);
#endif
		} else {
			return -1;
		}
	}

	return 0;
}