import com.dalsemi.onewire.adapter.TINIInternalAdapter;
import com.dalsemi.onewire.OneWireException;

class EthernetAddressReader {
	static final int TARGET_FAMILY_ID    = 0x89;
	static final int START_ADDRESS       = 0x6;
	static final int READ_MEMORY_COMMAND = 0xf0;

	public static void main(String[] args) {
		TINIInternalAdapter adapter = new TINIInternalAdapter();
		boolean foundIt = false;

		try {
			adapter.beginExclusive(true);
			if (adapter.findFirstDevice()) {
				// Test LSB (family id) against target
				if ((adapter.getAddressAsLong()&0xff) == TARGET_FAMILY_ID) {
					foundIt = true;
				}
				while (!foundIt && adapter.findNextDevice()) {
					if ((adapter.getAddressAsLong() & 0xFF) ==
						 TARGET_FAMILY_ID) {
						foundIt = true;
					}
				}
				if (foundIt) {
					/* 
					 * data[0] -> read memory command byte
					 * data[1] -> low byte of starting address
					 * data[2] -> high byte of starting address
					 */
					byte[] command = new byte[3];
					command[0] = (byte) READ_MEMORY_COMMAND;
					command[1] = START_ADDRESS & 0xFF;
					command[2] = (START_ADDRESS >>> 8) & 0xFF;

					// Send the command and starting memory address
					adapter.dataBlock(command, 0, command.length);
					// Read 48-bit ethernet address
					byte[] macID = adapter.getBlock(6);		
					for (int i = 5; i >= 0; i--) {
						System.out.print(Integer.toHexString(macID[i] & 0xff));
						if (i != 0) {
							System.out.print(":");
						}
					}
					System.out.println();
				} else {
					System.out.println("Device not found");
				}
			}
		} catch (OneWireException owe) {
			System.out.println(owe.getMessage());
		} finally {
			adapter.endExclusive();
		}
	}
}
