//-----------------------------------------------------------------------------
// ssDevTest.C:  Sample test program for testing compilation and
// linking of SSDevice object.
//
//-----------------------------------------------------------------------------

// $Source: /projects/HMS/nsr/udfAncil/devsTest/ssDevTst.cpp,v $
// $Revision: 1.6 $     $Author: hmgr $
// $State: Exp $        $Locker:  $
// $Date: 96/02/05 09:13:59 $

//      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.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>               
#include <errno.h>
#include <assert.h>
#include "nsrLib.h"
#include "nsrRSSdv.h"

// Defines

#define BYTEARRAY_MAX   65535   // Max size of a ByteArray, in bytes

// Function prototypes

static INT32 CheckSum(BYTE_PTR,UINT32);
static void initData(void);

// Some global data

static BYTE     data_buffer[BYTEARRAY_MAX];
static BYTE     zero_buffer[BYTEARRAY_MAX];
static int retcode = 0;


//-----------------------------------------------------------------------------
// main():
//
// We expect one argument, the device file to test on; if we don't
// get any arguments, or an argument we don't understand, just use some
// default ("/dev/rdsk/c201d4s0")
//-----------------------------------------------------------------------------


int main (int argc, CHAR_PTR argv[])
{
    INT32       iter;
    INT32       ssize,nsect;
    DriveDevice *ddev;
    String      *str;
    CONST_CHAR_PTR devfile;
    Error       error;

    // Prepare a String variable for use with the SSVolume and DriveDevice
    // constructors

    if(argc != 2)
    {
        str = new String("/dev/rdsk/c201d4s0");
    }
    else
    {
        str = new String(argv[1]);
    }
    if(str == NULL)
    {
        (void) fprintf(stderr,"ddev construction failed:  no free store!\n");
        exit(1);
    }
    devfile = (CONST_CHAR_PTR) *str;
    printf("Using device file %s\n",devfile);

    // Create a DriveDevice and an SSVolume object.

    ddev = new DriveDevice(*str);
    if(ddev == NULL)
    {
        (void) fprintf(stderr,"ddev construction failed:  no free store!\n");
        exit(1);
    }
    if(ddev->error() != NSR::error_None)
    {
        (void) fprintf(stderr,"ddev construction failed, error = %d: %s\n",
                       (INT32) ddev->error(),
                       (ddev->error()).message());
        delete ddev;
        exit(1);
    }

    initData();
    delete ddev;
    return retcode;
}

//-----------------------------------------------------------------------------
// CheckSum():  The usual checksum/CRC code, stolen once again from 
//              distress.c...
//-----------------------------------------------------------------------------

static INT32
CheckSum(BYTE_PTR data,UINT32 size)
{
    INT32 i;
    INT32 csum = 0;

    for(i = 0; i < size; i++) {
        csum = ((unsigned)csum<<1 | (unsigned)csum>>31) ^ (unsigned)data[i];
    }
    return csum;
}

//-----------------------------------------------------------------------------
// initData():  Fill up the data array with random data.
//-----------------------------------------------------------------------------

static void
initData(void)
{
    INT32       i;

    srand(time((time_t *) NULL));
    for(i = 0; i < BYTEARRAY_MAX; i++)
    {
        data_buffer[i] = rand() % 256;
        zero_buffer[i] = 0;
    }
}

