// SCSIControlWindow.m
//
// Copyright (C) 1990 by Jiro Nakamura and Canon Inc.
// Copyright (C) 1991 by Jiro Nakamura.
// Copyright (C) 1992 by Jiro Nakamura.
// All Rights Reserved.
//
// RCS Information
// Revision Number->	$Revision: 1.8 $
// Last Revised->	$Date: 92/11/13 02:35:45 $
//

static char rcsid[]="$Id: SCSIControlWindow.m,v 1.8 92/11/13 02:35:45 jiro Exp Locker: jiro $";
#import "SCSIControlWindow.h"
#import "SCSISelectorPanel.h"
#import "SCSI.h"
#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/TextField.h>
#import <strings.h>
#import <libc.h>
#import <defaults/defaults.h>		// for NXWriteDefault() 

#define SLEEPLOOP	5	/* Loop 5 times */
#define SLEEPTIME	100	/* 100 microsecond sleep */


@implementation SCSIControlWindow


void removeLastSpace( char *s, int length)
{
	for( length -= 1; length > 0 && (s[length] ==' '||s[length]=='\0');
		 			length -- )
		s[length] = '\0';
}
	

- (BOOL) setTarget: (int) trg lun: (int) ln
{

		
	target = trg;
	lun = ln;
	lastBlock = 0;

	[self closeTarget: self];
	return [self determineTarget: self];
}

- (BOOL) determineTarget: sender
{
	SCSI	*scsi;
	static struct inquiry_reply ibuffer;
	static char vendorBuffer[10], productBuffer[30], revisionBuffer[10],
			typeBuffer[30];
	int sleeper;
	struct capacity_reply rbuffer;
	static char tmpBuffer[50];		
	static char *ansiVersionPointer;
	static char *ansiVersionDescription[4] ={
		"Not compliant",
		"X.131-1986 (SCSI-1)",
		"X.131-198X (SCSI-2)",
		"Reserved" };		

	scsi  = [SCSI new];
	[scsi openSCSI];
	
	if( [scsi setTarget: target lun: lun] != 0 )
		{
		strcpy(vendorBuffer, "-");
		strcpy(typeBuffer, "Cannot access");
		
		strcpy( vendorBuffer,   "-");
		strcpy( productBuffer,  "-");
		strcpy( revisionBuffer, "-");
		ansiVersionPointer = "-";
		deviceType = DEVTYPE_NOTPRESENT;

		[targetText		setIntValue:	target];
		[lunText		setIntValue:	0]; 
		[vendorText		setStringValue:	vendorBuffer];
		[productText		setStringValue:	productBuffer];
		[revisionText		setStringValue: revisionBuffer];
		[ansiVersionText	setStringValue: ansiVersionPointer];
		[typeText		setStringValue: typeBuffer];	

		[deviceButton	setIcon: typeBuffer];
		[scsi closeSCSI];
		[scsi free];
		return NO;

		}
	
		
	for( sleeper = 0; sleeper < SLEEPLOOP; sleeper ++ )
		{
		#ifdef DEBUG
			fprintf(stderr,"Target %d, Loop %2d:\n", 
				target,
				sleeper );
		#endif
			
		if(  [scsi inquirySCSI: &ibuffer] == 0 )
			break;
		else
			usleep( SLEEPTIME );
		}
			
	if(  sleeper < SLEEPLOOP )
		{
		sprintf( vendorBuffer, "%8s",ibuffer.ir_vendorid);
		sprintf( productBuffer, "%16s",ibuffer.ir_productid);
		sprintf( revisionBuffer, "%4s",ibuffer.ir_revision);
		
		vendorBuffer[8] =  productBuffer[16] = '\0';
		removeLastSpace(vendorBuffer,8); 
		removeLastSpace(productBuffer,16); 
		
		ansiVersionPointer =  
			ansiVersionDescription[
				ibuffer.ir_ansiversion < 3 ?
				ibuffer.ir_ansiversion : 3],
		deviceType = ibuffer.ir_devicetype;
		ansiVersion = ibuffer.ir_ansiversion;
		if( (ibuffer.ir_devicetype == DEVTYPE_DISK || 
		     ibuffer.ir_devicetype == DEVTYPE_CDROM || 
		     ibuffer.ir_devicetype == DEVTYPE_OPTICAL) &&
			[scsi readCapacitySCSI: &rbuffer] == 0)
			{
			[moreInfo1TitleText setStringValue:
				"Block Capacity: "];
			[moreInfo2TitleText setStringValue:
				"Block Size: "];
			[moreInfo3TitleText setStringValue:
				"Disk capacity:"];			
			sprintf(tmpBuffer,"%d blocks", 
				(int) rbuffer.cr_lastlba+1);
			[moreInfo1Text setStringValue:
				tmpBuffer];
			sprintf(tmpBuffer,"%d bytes", 
				(int) rbuffer.cr_blklen);
			[moreInfo2Text setStringValue:
				tmpBuffer];
			sprintf(tmpBuffer,"%d bytes (%2.1f megabytes)",
				(int) ((int) rbuffer.cr_lastlba + 
				1) * (int)rbuffer.cr_blklen,
				(double)									
				((int) rbuffer.cr_lastlba + 
				1) * ((int) rbuffer.cr_blklen) /
				(1024*1024));
			[moreInfo3Text setStringValue:
				tmpBuffer];
			
			sprintf(deviceName, "%s %s", vendorBuffer, 
				productBuffer);
			lastBlock = rbuffer.cr_lastlba;
			blockSize = rbuffer.cr_blklen;
			}
		}
	else
		{
		strcpy(vendorBuffer, "-");
		strcpy(typeBuffer, "Not Present");
	
		strcpy( vendorBuffer,   "-");
		strcpy( productBuffer,  "-");
		strcpy( revisionBuffer, "-");
		ansiVersionPointer = "-";
		[moreInfo1TitleText setStringValue: "-"];
		[moreInfo2TitleText setStringValue: "-"];
		[moreInfo3TitleText setStringValue: "-"];
		[moreInfo1Text setStringValue: "-"];
		[moreInfo2Text setStringValue: "-"];
		[moreInfo3Text setStringValue: "-"];
		
		deviceType = DEVTYPE_NOTPRESENT;
		}
	
		
	strcpy( typeBuffer, 
		[scsi identifyDeviceType: &ibuffer]);

	[targetText		setIntValue:	target];
	[lunText		setIntValue:	0]; // lun is always 0 for now
	[vendorText		setStringValue:	vendorBuffer];
	[productText		setStringValue:	productBuffer];
	[revisionText		setStringValue: revisionBuffer];
	[ansiVersionText	setStringValue: ansiVersionPointer];
	[typeText		setStringValue: typeBuffer];	

	[deviceButton	setIcon: typeBuffer];
	[scsi closeSCSI];
	[scsi free];
	
	#ifdef DEBUG
		fprintf(stderr, "SCSIControlWindow: Setting target to %d. "
			"Last block = %ld.\n", target, (int)lastBlock);
	#endif
	


	return( lastBlock != 0);
}	

- (BOOL) acceptsDeviceType: (int) dt andMounted: (int) mt
{
	if( mt == YES || dt != DEVTYPE_DISK)
		return NO;
		
	return YES;
}

- reg: sender
{
	char l[50], k[50];

	strcpy( l, [lic1 stringValue] );
	strcpy( l+4, [lic2 stringValue] );
	strcpy( l+8, [lic3 stringValue] );
	
	
	strcpy(k, [authCode stringValue] );
	NXWriteDefault([NXApp appName], "License", l);	
	NXWriteDefault([NXApp appName], "Key", k);	
	
	#ifdef DEBUG
		printf("Registered license %s, key %s\n", l,k );
	#endif
	
	[self acceptsDeviceType: 0 andMounted: -10];
	[selector inquireAll: self];
	
	return self;
	
}

- newReg: sender
{
	[authCode	setEnabled: YES];
	[authCode	setStringValue: ""];
	[lic1	setEnabled: YES];
	[lic2	setEnabled: YES];
	[lic3	setEnabled: YES];
	[newLic setEnabled: NO];
	[lic1	selectText: self];
	return self;
}


- closeTarget: sender	{return self; }
- closeWindow: sender
	{
	[self closeTarget: self];
	[self close];
	return self;
	}
@end
