| 1 | # | ||
| 2 | # CD::Detect POE wrapper | ||
| 3 | # | ||
| 4 | # Copryright (c) Erick Calder 2001. | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | |||
| 8 | package POE::Component::CD::Detect; | ||
| 9 | |||
| 10 | # --- external modules -------------------------------------------------------- | ||
| 11 | |||
| 12 | use warnings; | ||
| 13 | use strict; | ||
| 14 | use Carp; | ||
| 15 | |||
| 16 | use Fcntl; | ||
| 17 | use IO::Socket; | ||
| 18 | |||
| 19 | use POE; | ||
| 20 | |||
| 21 | # --- module initialisation --------------------------------------------------- | ||
| 22 | |||
| 23 | use vars qw($VERSION); | ||
| 24 | $VERSION = substr q$Revision: 1.1 $, 10; | ||
| 25 | |||
| 26 | # error return codes | ||
| 27 | |||
| 28 | my $ERR_DEVOPEN = 1; | ||
| 29 | my $ERR_DEVREAD = 2; | ||
| 30 | my $ERR_TRKREAD = 3; | ||
| 31 | my @ERRMSG = ("" | ||
| 32 | , "Cannot open CDROM" | ||
| 33 | , "Cannot read TOC" | ||
| 34 | , "Cannot read track info" | ||
| 35 | ); | ||
| 36 | |||
| 37 | # endian check | ||
| 38 | |||
| 39 | my $BIG_ENDIAN = unpack("h*", pack("s", 1)) =~ /01/; | ||
| 40 | |||
| 41 | # --- module interface -------------------------------------------------------- | ||
| 42 | |||
| 43 | sub new { | ||
| 44 | my $class = shift; | ||
| 45 | my $opts = shift; | ||
| 46 | |||
| 47 | my $self = bless({}, $class); | ||
| 48 | |||
| 49 | my %opts = !defined($opts) ? () : ref($opts) ? %$opts : ($opts, @_); | ||
| 50 | %$self = (%$self, %opts); | ||
| 51 | |||
| 52 | $self->{alias} ||= "main"; | ||
| 53 | $self->{delay} ||= 5; | ||
| 54 | |||
| 55 | $self->initdev(); | ||
| 56 | |||
| 57 | POE::Session->create( | ||
| 58 | inline_states => { _start => \&_start, check => \&check }, | ||
| 59 | args => [$self] | ||
| 60 | ); | ||
| 61 | |||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub suspend { | ||
| 66 | my $self = shift; | ||
| 67 | $self->{suspend} = 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub resume { | ||
| 71 | my $self = shift; | ||
| 72 | $self->{suspend} = 0; | ||
| 73 | } | ||
| 74 | |||
| 75 | # --- session events ---------------------------------------------------------- | ||
| 76 | |||
| 77 | sub _start { | ||
| 78 | my ($kernel, $heap, $self) = @_[KERNEL, HEAP, ARG0]; | ||
| 79 | |||
| 80 | $heap->{self} = $self; | ||
| 81 | $kernel->delay("check", $self->{delay}); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub check { | ||
| 85 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||
| 86 | my $self = $heap->{self}; | ||
| 87 | |||
| 88 | $kernel->delay("check", $self->{delay}); # set up to come back | ||
| 89 | return if $self->{suspend}; # no chks when suspended | ||
| 90 | |||
| 91 | my @info = $self->info(); | ||
| 92 | $kernel->post($self->{alias}, "inserted", @info, $self->{dev}) | ||
| 93 | unless $self->{err}; | ||
| 94 | } | ||
| 95 | |||
| 96 | # --- TOC functionality ------------------------------------------------------- | ||
| 97 | |||
| 98 | # | ||
| 99 | # Syntax: | ||
| 100 | # ->info() | ||
| 101 | # Synopsis: | ||
| 102 | # Checks a given device for the presence of a CD. When one | ||
| 103 | # is found, the table of contents (TOC) and disc id are | ||
| 104 | # provided. If either there is no disc present or it can't | ||
| 105 | # be read, a null is returned and the $self->{err} is set. | ||
| 106 | # Returns: | ||
| 107 | # $disc-id, \@TOC | ||
| 108 | # | ||
| 109 | |||
| 110 | sub info { | ||
| 111 | my $self = shift; | ||
| 112 | my $err = $self->{err} = 0; | ||
| 113 | |||
| 114 | $err = !sysopen(CD, $self->{dev}, O_RDONLY | O_NONBLOCK); | ||
| 115 | if ($err) { | ||
| 116 | $self->{err} = $ERR_DEVOPEN; | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | |||
| 120 | my $tochdr = ""; | ||
| 121 | $err = !ioctl(CD, $self->{CDROMREADTOCHDR}, $tochdr); | ||
| 122 | if ($err) { | ||
| 123 | $self->{err} = $ERR_DEVREAD; | ||
| 124 | return; | ||
| 125 | } | ||
| 126 | |||
| 127 | $tochdr = substr($tochdr, 2, 2) if $self->{OS} =~ /BSD/; | ||
| 128 | my ($start, $end) = unpack "CC", $tochdr; | ||
| 129 | my $size = $end - $start + 2; | ||
| 130 | my $toc = " " x ($size * 8); | ||
| 131 | |||
| 132 | my $tocentry; | ||
| 133 | if($self->{OS} =~ /BSD/) { | ||
| 134 | my ($sz_hi, $sz_lo); | ||
| 135 | $sz_hi = $BIG_ENDIAN ? int($size / 256) : $size & 255; | ||
| 136 | $sz_lo = $BIG_ENDIAN ? $size & 255 : int($size / 256); | ||
| 137 | $tocentry = pack "CCCCP8l", $self->{CDROM_MSF}, 0, $sz_hi, $sz_lo, $toc; | ||
| 138 | $err = !ioctl(CD, $self->{CDROMREADTOCENTRY}, $tocentry); | ||
| 139 | if ($err) { | ||
| 140 | $self->{err} = $ERR_TRKREAD; | ||
| 141 | return; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | my ($count, @toc) = 0; | ||
| 146 | for my $i ($start .. $end, 0xAA) { | ||
| 147 | my ($min, $sec, $frame); | ||
| 148 | |||
| 149 | if ($self->{OS} =~ /BSD/) { | ||
| 150 | ($min, $sec, $frame) = unpack "CCC", substr($toc, $count + 5, 3); | ||
| 151 | $count += 8; | ||
| 152 | } | ||
| 153 | else { | ||
| 154 | $tocentry = pack "CCC", $i, 0, $self->{CDROM_MSF}; | ||
| 155 | $err = !ioctl(CD, $self->{CDROMREADTOCENTRY}, $tocentry); | ||
| 156 | if ($err) { | ||
| 157 | $self->{err} = $ERR_TRKREAD; | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | ($min, $sec, $frame) = unpack "CCCC", substr($tocentry, 4, 4); | ||
| 161 | } | ||
| 162 | |||
| 163 | push @toc, { | ||
| 164 | min => $min, | ||
| 165 | sec => $sec, | ||
| 166 | start => int($frame + 75 * (60 * $min + $sec)) | ||
| 167 | }; | ||
| 168 | } | ||
| 169 | |||
| 170 | close(CD); | ||
| 171 | |||
| 172 | my $discid = cddb_discid(@toc); | ||
| 173 | # note that discid needs to be calculated with initial time values | ||
| 174 | for my $i (0 .. @toc - 2) { | ||
| 175 | $toc[$i]->{end} = $toc[$i + 1]->{start} - 1; | ||
| 176 | my $len = $toc[$i]->{length} = $toc[$i]->{end} - $toc[$i]->{start}; | ||
| 177 | my $min = $toc[$i]->{min} = int($len / 60 / 75); | ||
| 178 | $toc[$i]->{sec} = int($len / 75) - $min * 60; | ||
| 179 | } | ||
| 180 | |||
| 181 | return $discid, \@toc; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub cddb_discid { | ||
| 185 | my @toc = @_; | ||
| 186 | my $total = $#toc; | ||
| 187 | my ($i, $t, $n); $i = $t = $n = 0; | ||
| 188 | |||
| 189 | for (my $i = 0; $i < $total; $i++) { | ||
| 190 | $n = $n + cddb_sum(($toc[$i]->{min} * 60) + $toc[$i]->{sec}); | ||
| 191 | } | ||
| 192 | |||
| 193 | $t = (($toc[$total]->{min} * 60) + $toc[$total]->{sec}) | ||
| 194 | - (($toc[0]->{min} * 60) + $toc[0]->{sec}) | ||
| 195 | ; | ||
| 196 | |||
| 197 | return (($n % 0xff) << 24 | $t << 8 | $total); | ||
| 198 | } | ||
| 199 | |||
| 200 | sub cddb_sum { | ||
| 201 | my $n = shift; | ||
| 202 | my $ret = 0; | ||
| 203 | |||
| 204 | while ($n > 0) { | ||
| 205 | $ret += ($n % 10); | ||
| 206 | $n = int $n / 10; | ||
| 207 | } | ||
| 208 | |||
| 209 | return $ret; | ||
| 210 | } | ||
| 211 | |||
| 212 | # --- utility functions ------------------------------------------------------- | ||
| 213 | |||
| 214 | # | ||
| 215 | # select appropriate default device | ||
| 216 | # | ||
| 217 | |||
| 218 | sub initdev { | ||
| 219 | my $self = shift; | ||
| 220 | |||
| 221 | $self->{dev} = "/dev/cdrom"; | ||
| 222 | |||
| 223 | # cdrom IOCTL magic (from c headers) | ||
| 224 | # linux x86 is default | ||
| 225 | |||
| 226 | # /usr/include/linux/cdrom.h | ||
| 227 | $self->{CDROMREADTOCHDR} = 0x5305; | ||
| 228 | $self->{CDROMREADTOCENTRY} = 0x5306; | ||
| 229 | $self->{CDROM_MSF} = 0x02; | ||
| 230 | |||
| 231 | # setup for linux, solaris x86, solaris spark | ||
| 232 | # you freebsd needs looking into | ||
| 233 | |||
| 234 | my $os; chomp($os = `uname -s`); | ||
| 235 | my $machine; chomp($machine = `uname -m`); | ||
| 236 | $self->{OS} = $os; | ||
| 237 | $self->{ARCH} = $machine; | ||
| 238 | |||
| 239 | if ($os eq "SunOS") { | ||
| 240 | # /usr/include/sys/cdio.h | ||
| 241 | |||
| 242 | $self->{CDROMREADTOCHDR} = 0x49b; # 1179 | ||
| 243 | $self->{CDROMREADTOCENTRY} = 0x49c; # 1180 | ||
| 244 | |||
| 245 | if (-e "/vol/dev/aliases/cdrom0") { | ||
| 246 | $self->{dev} = "/vol/dev/aliases/cdrom0"; | ||
| 247 | } | ||
| 248 | elsif ($machine =~ /^sun/) { | ||
| 249 | $self->{dev} = "/dev/rdsk/c0t6d0s0"; # on sparc and old suns | ||
| 250 | } | ||
| 251 | else { | ||
| 252 | $self->{dev} = "/dev/rdsk/c1t0d0p0"; # on intel | ||
| 253 | } | ||
| 254 | |||
| 255 | # works for netbsd, infos for other bsds welcome | ||
| 256 | |||
| 257 | } elsif ($os =~ /BSD/i) { | ||
| 258 | # /usr/include/sys/cdio.h | ||
| 259 | |||
| 260 | $self->{CDROMREADTOCHDR} = 0x40046304; | ||
| 261 | $self->{CDROMREADTOCENTRY} = 0xc0086305; | ||
| 262 | |||
| 263 | $self->{CDDEV} = "/dev/cd0a"; | ||
| 264 | |||
| 265 | $self->{CDDEV} = "/dev/cd0c" if $os eq "OpenBSD"; | ||
| 266 | } | ||
| 267 | } | ||
| 268 | |||
| 269 | 1; # :) | ||
| 270 | |||
| 271 | __END__ |