Skrevet av Emne: Perl Device::SCSI example code  (Lest 4417 ganger)

ATC

  • Gjest
Perl Device::SCSI example code
« på: 27. ſeptember 2008, 18:24 pm »
  • [applaud]0
  • [smite]0
  • The Perl module "Device::SCSI" documentation does not provide any example code showing how to construct SCSI commands or parse the result data.



    ATC

    • Gjest
    [Solved] Perl Device::SCSI example code
    « Svar #1 på: 27. ſeptember 2008, 18:24 pm »
  • [applaud]0
  • [smite]0
  • Here is an example showing how to perform a command and fetch the results. Note that this is an EXAMPLE only; your mileage may vary!

    sub read_element_status {
      my $self = shift;
      my $element = shift;
      my %options = @_;
      my $hashref = {};

      unless (defined $element) {
        warn "No element id specified for read_element_status()";
        return $hashref;
      }

      my $command = pack("C12",
        0xb8,       # READ ELEMENT STATUS
        ($options{'LUN'} << 5),     # Bits 0-3 is Element Type Code (0), bits 5-7 is LUN
        $element >> 8,              # $element MSB
        $element & 0xff,            # $element LSB
        0,                          # Number of elements, MSB
        1,                          # Number of elements, LSB
        0x00,                       # (reserved)
        0,                          # Allocation length, MSB
        0,                          # Allocation length, ...
        32,                         # Allocation length, LSB (32 bytes)
        0x00,                       # (reserved)
        0x00,                       # (reserved)
      );      
      my $wanted = 32;

      my ($result, $sense) = $self->execute($command, $wanted);

      unless ($self->sense_error($sense)) {

        # Element Status Data - discard this header, we have no use for it
        my $unused = substr($result, 0, 10);
        $result = substr($result, 8);  

        # Element Status Page - discard this header, we have no use for it
        $unused = substr($result, 0, 8);
        $result = substr($result, 8);  

        # Element Status Descriptor
        my $page = substr($result, 0, 16);

        my $flags, $valid;
       
        (
          $hashref->{'ELEMENT'},
          $flags,
          $unused,                  # (reserved)
          $hashref->{'ADDITIONAL_SENSE_CODE'},
          $hashref->{'ADDITIONAL_SENSE_QUALIFIER'},
          $unused,                  # (reserved)
          $unused,                  # (reserved)
          $unused,                  # (reserved)
          $valid,
          $hashref->{'SOURCE_ELEMENT'},
          $unused,                  # (reserved)
          $unused,                  # (reserved)
          $unused,                  # (reserved)
          $unused,                  # (reserved)
        ) = unpack("n1 C8 n1 C4", $result);

        unless ($valid & 0x80) { delete $hashref->{'SOURCE_ELEMENT'}; }
        $hashref->{'FULL'} = ($flags & 0x01) ? 1 : 0;
        $hashref->{'EXCEPTION'} = ($flags & 0x04) ? 1 : 0;
      }  
     
      return $hashref;
    }