// msACobj.C

// $Source: /projects/HMS/nsr/udfAncil/devsTest/msACobj.h,v $
// $Revision: 1.9 $     $Author: hmgr $
// $State: Exp $        $Locker:  $
// $Date: 96/02/05 09:13:54 $

//      Unpublished Confidential Information of Hewlett-Packard
//      Company. Do not disclose.  Copyright (c) Hewlett-Packard
//      Company 1995. All rights reserved
// 
// The contents of this software are proprietary and confidential to
// the Hewlett- Packard Company, and are limited in distribution to
// those with a direct need to know.  Individuals having access to
// this software are responsible for maintaining the confidentiality
// of the content and for keeping the software secure when not in use.
// Transfer to any party is strictly forbidden other than as expressly
// permitted in writing by Hewlett-Packard Company.  Unauthorized
// transfer to or possession by any unauthorized party may be a
// criminal offense.
//
//                  RESTRICTED RIGHTS LEGEND
//
//      Use,  duplication,  or disclosure by the U. S. Government is
//      subject to restrictions as set forth in subparagraph (c) (1)
//      (ii) in the Rights in Technical Data and Computer Software
//      clause in DFARS 252.227-7013 or any other successor clause.
//
//                      HEWLETT-PACKARD COMPANY
//                      3000 Hanover St.
//                      Palo Alto, CA  94304 U.S.A.
//
//      Rights for non-DOD U.S. Government Departments and Agencies
//      are as set forth in FAR 52.227-19 (c) (1,2) or any other
//      successor clause.

#ifndef _MSACOBJ_H_

#include <ctype.h>
#include <nsrLib.h>
#include <nsrRMSdv.h>
#include "dlist.h"

// Some useful macros

#define NUM_DATA_BUFFERS        3
#define DATA_BUFFER_SIZE        1024
#define MAX_READ_SIZE           262144          // 256 K, should be plenty
#define MAX_WRITE_SIZE          262144          // 256 K, should be plenty
#define MAXLINE                 128
#define PAGESIZE                256
#define LINESIZE                16

#define TEST_SUCCEEDED          0
#define TEST_FAILED             2

//-----------------------------------------------------------------------------
// enum ACState:  Describes the current state of an AC object.
//-----------------------------------------------------------------------------

typedef enum acstate {
    ac_uninitialized,           // No valid autochanger registered yet
    ac_no_memory,               // Memory allocation failure:  fatal!
    ac_ready,                   // Ready to accept commands
    ac_quit,                    // User quit:  test is done
    ac_unknown                  // Unknown state:  fatal!
} ACState;

//-----------------------------------------------------------------------------
// struct surface:  Contains information about a (slot,side) to sid mapping.
//-----------------------------------------------------------------------------

struct surface {
    UINT32      slot;
    UINT32      side;
    sid_t       sid;

    surface() : slot(0), side(0), sid(SID_T_INVALID) {}
};

//-----------------------------------------------------------------------------
// struct drive:  Contains information about a (tgt,lun) to device file mapping.
//-----------------------------------------------------------------------------

struct drive {
    UINT32      tgt;
    UINT32      lun;
    String      *devfile;

    drive() : tgt(0), lun(0), devfile(NULL) {}
};

//-----------------------------------------------------------------------------
// class AC:  This class maintains all test-dependent information.  It
//            contains the actual hpACDev, as well as data buffers and sid map
//            information.  
//-----------------------------------------------------------------------------

class AC {

private:

    ACState     state;                          // State of the AC object
    hpACDev     *acdev;                         // The autochanger device
    BYTE        *readbuf;                       // Read buffer
    BYTE        *writebuf;                      // Write buffer
    BYTE        *buf[NUM_DATA_BUFFERS];         // Data buffers for writes
    dlist       *smap;                          // Surface map list
    dlist       *dmap;                          // Drive map list
    sid_t       lastsid;                        // Sid in last read
    UINT32      lastoff;                        // Offset in last read
    UINT32      lastnbytes;                     // Nbytes from last read
    FILE        *stream;                        // Output stream for messages


public:

                AC(FILE *outstream);
                ~AC();
    void        registerAC(CONST_CHAR_PTR devfile);
    void        map(UINT32 slot,UINT32 side);
    void        unmap(sid_t sid);
    void        drivemap(UINT32 tgt,UINT32 lun,CONST_CHAR_PTR devfile);
    void        show(void);
    void        sectors(sid_t sid);
    int         getsectors(sid_t sid);
    void        size(sid_t sid);
    int         getsize(sid_t sid);
    int         getValidSids(sid_t *arr,int max);
    void        read(sid_t sid,UINT32 start,UINT32 n);
    void        write(sid_t sid,UINT32 start,UINT32 n,UINT32 buf);
    void        write(sid_t sid,UINT32 start,UINT32 n);
    void        writeWOE(sid_t sid,UINT32 start,UINT32 n,UINT32 buf);
    void        writeWOE(sid_t sid,UINT32 start,UINT32 n);
    void        verify(sid_t sid,INT32 mode);
    void        erase(sid_t sid,UINT32 start,UINT32 n);
    BYTE_PTR    readBuf(void) { return readbuf; }
    BYTE_PTR    writeBuf(void) { return writebuf; }
    void        display(UINT32 page);
    void        status(void);
    int         eltaddr(struct element_addresses &eltaddr);
    int         eltstat(int elt,struct element_status &eltstat);
    int         crc(BYTE *data,int len);
    void        quit(void) { state = ac_quit; }
    ACState     acstate(void) { return state; }
    INT32       accepting_commands(void) {
                    return (((state == ac_uninitialized) ||
                             (state == ac_ready)) ? 1 : 0);
                }
};
#endif          // _MSACOBJ_H_
