public
Description: Perl code to control a Denon AVR-4806
Homepage:
Clone URL: git://github.com/bradfitz/perl-denon-avr-4806.git
perl-denon-avr-4806 / denon.pl
100755 98 lines (78 sloc) 2.367 kb
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
#!/usr/bin/perl
 
use strict;
use IO::Socket::INET;
use Carp qw(croak);
 
my $host = "10.0.0.179";
my $port = 23;
 
("\xff\xfd\x03" eq fromhex("ff fd", " 03 ")) or die;
 
my $sock = IO::Socket::INET->new(PeerAddr => $host,
                                 PeerPort => $port)
    or die "Failed to connect to $host:$port";
 
my $hello = fromhex("ff fd 03", # Do Suppress Go ahead
                    "ff fb 18", # Will Terminal Type
                    "ff fb 1f", # Will Negotiate About Window Size
                    "ff fb 20", # Will Terminal Speed
                    "ff fb 21", # Wlil Remote Flow Control
                    "ff fb 22", # Will Linemode
                    "ff fb 27", # Will New Enivronment Option
                    "ff fd 05", # Do Status
                    );
 
send_to_denon($hello);
 
expect_from_denon(fromhex("ff fb 03")); # Will Suppress Go Ahead
expect_from_denon(fromhex("ff fa 18 01 ff f0")); # Send your terminal type
 
print "send terminal.\n";
send_to_denon(fromhex("ff fa 18 00",
                      "rxvt",
                      "ff f0", # suboption end
                      ));
 
# expect_from_denon("BridgeCo AG Telnet server\x0a\x0d");
 
my $child = fork;
unless (defined($child)) {
    die "Fork failure.";
}
 
if ($child) {
    # we're the parent process. accept input.
    $| = 1;
    while (1) {
        print "DENON> ";
        my $line = <STDIN>;
        chomp $line;
        if (!$line) {
            next;
        }
        send_to_denon($line . "\x0d");
    }
} else {
    # child process.
    my $buf;
    while (sysread($sock, $buf, 300)) {
        print "Read: [", printable($buf), "]\n";
    }
}
 
sub expect_from_denon {
    my $expected = shift;
    my $got = "";
    my $buf;
    print "Waiting on ", printable($expected), "...";
    while (length($got) < length($expected) &&
           sysread($sock, $buf, length($expected) - length($got))) {
        $got .= $buf;
    }
    croak "Didn't get expected input." unless $got eq $expected;
    print "Got it.\n";
    return 1;
}
 
sub fromhex {
    my $in = join('', @_);
    $in =~ s/\s*(..)\s*/chr(hex($1))/eg;
    return $in;
}
 
sub send_to_denon {
    my $str = shift;
    syswrite($sock, $str) == length($str) or die;
}
 
sub printable {
    my $str = shift;
    $str =~ s/[^[:print:]]/sprintf("[%02x]", ord($&))/eg;
    return $str;
}