/*
 * ScsiAdr.java
 *
 * Copyright (c) 2007  Vreixo Formoso
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See COPYING file for details.
 */

package org.pykix.libburnia.libburn;

/**
 * SCSI address.
 * 
 * @author Vreixo Formoso
 * @since 0.1
 */
public class ScsiAdr {

	/** "Bus Number" (something like a virtual controller) */
	public int busNo;
	
	/** "Host Number" (something like half a virtual controller) */
	public int hostNo;
	
	/** "Channel Number" (other half of "Host Number"). */
	public int channelNo;
	
	/** "Target Number" or "SCSI Id" (a device). */
	public int targetNo;
	
	/** "Logical Unit Number" (a sub device). */
	public int lunNo;
	
	/**
	 * Creates a new SCSI address.
	 * 
	 * Note: bus and (host,channel) are supposed to be redundant.
	 * 
	 * @param busNo
	 * @param hostNo
	 * @param channelNo
	 * @param targetNo
	 * @param lunNo
	 */
	public ScsiAdr(int busNo, int hostNo, int channelNo, int targetNo, 
			int lunNo) {
		super();
		this.busNo = busNo;
		this.hostNo = hostNo;
		this.channelNo = channelNo;
		this.targetNo = targetNo;
		this.lunNo = lunNo;
	}
	
	/**
	 * Try to convert a given SCSI address of bus,host,channel,target,lun into
	 * a persistent drive address. If a SCSI address component parameter is < 0
	 * then it is not decisive and the first enumerated address which matches
	 * the >= 0 parameters is taken as result.
	 * 
	 * @param adr
	 * 		SCSI adr to conver
	 * @return
	 * 		The persistent drive address
	 * @throws BurnException
	 * 		On error.
	 */
	public static String converScsiAdr(ScsiAdr adr) throws BurnException {
		return burn_drive_convert_scsi_adr(adr.busNo, adr.hostNo, adr.channelNo,
				adr.targetNo, adr.lunNo);
	}
	
	/**
	 * Try to obtain bus,host,channel,target,lun from path. If there is an SCSI
	 * address at all, then this call should succeed with a persistent
	 * drive address obtained via {@link DriveInfo#getAdr()}. It is also supposed 
	 * to succeed with any device file of a (possibly emulated) SCSI device.
	 * 
	 * @param path
	 * 		
	 * @return
	 * @throws BurnException
	 * 		On error
	 */
	public static ScsiAdr obtainScsiAdr(String path) throws BurnException {
		return burn_drive_obtain_scsi_adr(path);
	}
	
	@Override
	public String toString() {
		return "busNo=" + busNo + ", hostNo=" + hostNo + ", channelNo=" + 
			channelNo + ", targetNo=" + targetNo + " ,lunNo=" + lunNo;
	}
	
	private native static String burn_drive_convert_scsi_adr(int bus_no, 
			int host_no, int channel_no, int target_no, int lun_no) 
	throws BurnException;
	
	private native static ScsiAdr burn_drive_obtain_scsi_adr(String path)
	throws BurnException;
	
}
