/**
 * Licensed to the Rhiot under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.rhiot.component.deviceio.i2c.driver;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import jdk.dio.ClosedDeviceException;
import jdk.dio.DeviceDescriptor;
import jdk.dio.UnavailableDeviceException;
import jdk.dio.i2cbus.I2CDevice;

public abstract class I2CDriverAbstract implements I2CDriver, I2CDevice {

    protected I2CDevice device;

    public I2CDriverAbstract(I2CDevice device) {
        this.device = device;
    }

    @Override
    public void init() throws Exception {
    }

    @Override
    public void start() throws Exception {
    }

    @Override
    public void stop() throws Exception {
    }

    @Override
    public void shutdown() throws Exception {
    }

    @Override
    public void suspend() throws Exception {
    }

    @Override
    public void resume() throws Exception {
    }

    public void sleep(long howMuch) {
        try {
            Thread.sleep(howMuch);
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }

    @Override
    public final void begin() throws IOException, UnavailableDeviceException, ClosedDeviceException {
        device.begin();
    }

    @Override
    public final void close() throws IOException {
        device.close();
    }

    @Override
    public final void end() throws IOException, UnavailableDeviceException, ClosedDeviceException {
        device.end();
    }

    @Override
    public final Bus getBus() throws IOException {
        return device.getBus();
    }

    @Override
    public final <U extends I2CDevice> DeviceDescriptor<U> getDescriptor() {
        return device.getDescriptor();
    }

    @Override
    public final ByteBuffer getInputBuffer() throws ClosedDeviceException, IOException {
        return device.getInputBuffer();
    }

    @Override
    public final ByteBuffer getOutputBuffer() throws ClosedDeviceException, IOException {
        return device.getOutputBuffer();
    }

    @Override
    public final boolean isOpen() {
        return device.isOpen();
    }

    @Override
    public final int read() throws IOException, UnavailableDeviceException, ClosedDeviceException {
        return device.read();
    }

    @Override
    public final int read(ByteBuffer dst) throws IOException, UnavailableDeviceException, ClosedDeviceException {
        return device.read(dst);
    }

    @Override
    public final int read(int skip, ByteBuffer dst)
            throws IOException, UnavailableDeviceException, ClosedDeviceException {
        return device.read(skip, dst);
    }

    @Override
    public final int read(int subaddress, int subaddressSize, ByteBuffer dst)
            throws IOException, UnavailableDeviceException, ClosedDeviceException {
        return device.read(subaddress, subaddressSize, dst);
    }

    @Override
    public final int read(int subaddress, int subaddressSize, int skip, java.nio.ByteBuffer dst)
            throws IOException, UnavailableDeviceException, ClosedDeviceException {
        return device.read(subaddress, subaddressSize, skip, dst);
    }

    @Override
    public final void tryLock(int arg0) throws UnavailableDeviceException, ClosedDeviceException, IOException {
        device.tryLock(arg0);
    }

    @Override
    public final void unlock() throws IOException {
        device.unlock();
    }

    @Override
    public final int write(ByteBuffer arg0) throws IOException, UnavailableDeviceException, ClosedDeviceException {
        return device.write(arg0);
    }

    @Override
    public final int write(int arg0, int arg1, ByteBuffer arg2)
            throws IOException, UnavailableDeviceException, ClosedDeviceException {
        return device.write(arg0, arg1, arg2);
    }

    @Override
    public final void write(int arg0) throws IOException, UnavailableDeviceException, ClosedDeviceException {
        device.write(arg0);
    }

    public int readU16BigEndian(int register) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(2);
        bb.order(ByteOrder.BIG_ENDIAN);
        device.read(register, bb);
        return bb.getInt();
    }

    public int readU16LittleEndian(int register) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(2);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        device.read(register, bb);
        return bb.getInt();
    }

    public int readU24LittleEndian(int register) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(3);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        device.read(register, bb);
        return bb.getInt();
    }

    public int readU24BigEndian(int register) throws IOException {
        ByteBuffer bb = ByteBuffer.allocate(3);
        bb.order(ByteOrder.LITTLE_ENDIAN);
        device.read(register, bb);
        return bb.getInt();
    }

}

@@ -4868,34 +4868,36 @@
    return bb.get
-In
+Shor
 t();%0A    %7D%0A%0A    
@@ -5101,34 +5101,36 @@
    return bb.get
-In
+Shor
 t();%0A    %7D%0A%0A    
@@ -5120,32 +5120,35 @@
 rt();%0A    %7D%0A%0A   
+ //
  public int read
@@ -5194,35 +5194,34 @@
 Exception %7B%0A    
-   
+//
  ByteBuffer bb =
@@ -5241,35 +5241,34 @@
 llocate(3);%0A    
-   
+//
  bb.order(ByteOr
@@ -5283,35 +5283,34 @@
 LE_ENDIAN);%0A    
-   
+//
  device.read(reg
@@ -5317,35 +5317,34 @@
 ister, bb);%0A    
-   
+//
  return bb.getIn
@@ -5344,38 +5344,50 @@
 b.getInt();%0A    
-%7D%0A
+// %7D%0A    //
 %0A   
+ //
  public int read
@@ -5430,35 +5430,34 @@
 Exception %7B%0A    
-   
+//
  ByteBuffer bb =
@@ -5481,27 +5481,26 @@
 ate(3);%0A    
-   
+//
  bb.order(By
@@ -5499,38 +5499,35 @@
 order(ByteOrder.
-LITTLE
+BIG
 _ENDIAN);%0A      
@@ -5516,35 +5516,34 @@
 IG_ENDIAN);%0A    
-   
+//
  device.read(reg
@@ -5550,35 +5550,34 @@
 ister, bb);%0A    
-   
+//
  return bb.getIn
@@ -5580,18 +5580,21 @@
 etInt();%0A   
+ //
  %7D%0A%0A%7D%0A

correct big little Endian