changeset 259:ff88da88dd88

DIO-25: Support 64 bits platforms Summary: Native pointers holder is jlong now Reviewed-by: duke
author snazarki
date Wed, 27 Sep 2017 14:48:10 +0300
parents 2da1e9b94ad7
children 1b3d9720d724
files src/se/classes/com/oracle/dio/impl/Event.java src/se/classes/com/oracle/dio/impl/EventQueueManager.java src/se/classes/com/oracle/dio/impl/Handle.java src/se/classes/com/oracle/dio/uart/impl/ModemSignalDispatcher.java src/se/classes/com/oracle/dio/uart/impl/UARTOptionsHandler.java src/se/linux/native/com/oracle/dio/rs485_enabler.cpp src/se/native/com/oracle/dio/dio_common.cpp src/se/native/com/oracle/dio/dio_event_queue.cpp src/se/native/com/oracle/dio/dio_event_queue.h src/se/native/com/oracle/dio/gpio/impl/jni_gpio.cpp src/se/native/com/oracle/dio/uart/impl/jni_signal_dispatcher.cpp src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp src/share/classes/com/oracle/dio/utils/Constants.java src/share/linux/native/com/oracle/dio/gpio/gpio.c src/share/linux/native/com/oracle/dio/i2c/i2c.c
diffstat 15 files changed, 203 insertions(+), 123 deletions(-) [+]
line wrap: on
line diff
--- a/src/se/classes/com/oracle/dio/impl/Event.java	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/classes/com/oracle/dio/impl/Event.java	Wed Sep 27 14:48:10 2017 +0300
@@ -58,13 +58,16 @@
 
     @Override
     public String toString() {
-        StringBuilder b = new StringBuilder();
+        StringBuilder b = new StringBuilder("Event from ");
+        b.append(eventType.toString());
 
         if (payload != null) {
-            b.append(" length = ").append(payload.length).append(" bytes =");
-        }
-        for (int i = 0; i < payload.length; i++) {
-            b.append(" ").append(payload[i]);
+            b.append(" with payload length = ").append(payload.length).append(" bytes =");
+            for (int i = 0; i < payload.length; i++) {
+                b.append(" ").append(payload[i]);
+            }
+        } else {
+            b.append(" with empty payload");
         }
 
         return b.toString();
--- a/src/se/classes/com/oracle/dio/impl/EventQueueManager.java	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/classes/com/oracle/dio/impl/EventQueueManager.java	Wed Sep 27 14:48:10 2017 +0300
@@ -53,13 +53,13 @@
     private EventQueueManager() {
     }
 
-    private int getHash(int deviceType, int eventType, int nativeHandle) {
-        return deviceType * (deviceType + deviceType * eventType) + nativeHandle;
+    private int getHash(int deviceType, int eventType, long nativeHandle) {
+        return (int)(deviceType * (deviceType + deviceType * eventType) + nativeHandle);
     }
 
     public void setEventListener(Class<? extends Device> clazz, int eventSubType, AbstractPeripheral listener) {
         final int eventType = clazz.hashCode();
-        final int nativeHandle = listener.getHandle().getNativeHandle();
+        final long nativeHandle = listener.getHandle().getNativeHandle();
         if (!registeredTypes.contains(eventType)) {
             // prevent overriding that leads to message losses
             queue.registerForEvent(clazz, this);
@@ -71,7 +71,7 @@
 
     public void removeEventListener(Class<? extends Device> clazz, int eventSubType, AbstractPeripheral listener) {
         final int eventType = clazz.hashCode();
-        final int nativeHandle = listener.getHandle().getNativeHandle();
+        final long nativeHandle = listener.getHandle().getNativeHandle();
         listenerRegistry.remove(getHash(eventType, eventSubType, nativeHandle));
     }
 
@@ -91,11 +91,11 @@
             Tuple tuple = (Tuple)event;
             tuple.receiver.processDeviceEvent(tuple.eventType, tuple.event);
         } else {
-            IntBuffer payload = ByteBuffer.wrap(event.getPayload()).asIntBuffer();
+            ByteBuffer payload = ByteBuffer.wrap(event.getPayload());
             // all data are stored in big endian format
-            int handle = payload.get();
-            int subEvent  = payload.get();
-            int data   = payload.get();
+            long handle = payload.getLong();
+            int subEvent  = payload.getInt();
+            int data   = payload.getInt();
             AbstractPeripheral handler = listenerRegistry.get(getHash(event.getType().hashCode(), subEvent, handle));
             if (handler != null) {
                 handler.processNativeEvent(subEvent, data);
--- a/src/se/classes/com/oracle/dio/impl/Handle.java	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/classes/com/oracle/dio/impl/Handle.java	Wed Sep 27 14:48:10 2017 +0300
@@ -32,7 +32,7 @@
 public class Handle {
 
     /** Device native handle */
-    protected int device_reference = Constants.INVALID_HANDLE;
+    protected long device_reference = Constants.INVALID_HANDLE;
 
     /** Closes the device */
     public native void close();
@@ -44,7 +44,7 @@
     public native void unlock();
 
     /** Returns the device handle */
-    public final int getNativeHandle() {
+    public final long getNativeHandle() {
         return device_reference;
     }
 
@@ -56,6 +56,6 @@
     /** Returns a hash code value for the object. */
     @Override
     public int hashCode() {
-        return device_reference;
+        return ((int)device_reference) ^ ((int)(device_reference >>> Integer.SIZE));
     }
 }
--- a/src/se/classes/com/oracle/dio/uart/impl/ModemSignalDispatcher.java	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/classes/com/oracle/dio/uart/impl/ModemSignalDispatcher.java	Wed Sep 27 14:48:10 2017 +0300
@@ -25,6 +25,7 @@
 package com.oracle.dio.uart.impl;
 
 import java.nio.Buffer;
+import java.nio.ByteBuffer;
 import java.util.*;
 
 import com.oracle.dio.impl.Event;
@@ -73,23 +74,23 @@
     private static class SerialContext {
 
         /** Serial signal context */
-        final int context;
+        final long context;
 
         /** Serial signal listeners */
         final List<SerialSignalListener> listeners = new ArrayList<SerialSignalListener>();
 
-        SerialContext(int context) {
+        SerialContext(long context) {
             this.context = context;
         }
     }
 
-    private Map<Integer, SerialContext> contextMap = new HashMap<Integer, SerialContext>();
+    private Map<Long, SerialContext> contextMap = new HashMap<>();
 
-    private List<SerialSignalListener> getListeners(int serialHandler) {
+    private List<SerialSignalListener> getListeners(long serialHandler) {
         return contextMap.get(serialHandler).listeners;
     }
 
-    private int getContext(int serialHandler) {
+    private long getContext(long serialHandler) {
         return contextMap.get(serialHandler).context;
     }
 
@@ -99,9 +100,9 @@
      * @param serialHandler serial port handler
      * @param listener serial signal listener
      */
-    synchronized void addListener(int serialHandler, SerialSignalListener listener) {
+    synchronized void addListener(long serialHandler, SerialSignalListener listener) {
         if (! contextMap.containsKey(serialHandler)) {
-            int context = startListening(serialHandler);
+            long context = startListening(serialHandler);
             contextMap.put(serialHandler, new SerialContext(context));
         }
 
@@ -117,13 +118,13 @@
      * @param serialHandler serial port handler
      * @param listener serial signal listener
      */
-    synchronized void removeListener(int serialHandler, SerialSignalListener listener) {
+    synchronized void removeListener(long serialHandler, SerialSignalListener listener) {
         if (contextMap.containsKey(serialHandler)) {
             List<SerialSignalListener> listeners = getListeners(serialHandler);
             listeners.remove(listener);
 
             if (listeners.isEmpty()) {
-                int context = getContext(serialHandler);
+                long context = getContext(serialHandler);
                 stopListening(context);
                 contextMap.remove(serialHandler);
             }
@@ -136,7 +137,7 @@
      * @param serialHandler serial port handler
      * @return handler of serial signal context
      */
-    private int startListening(int serialHandler) {
+    private long startListening(long serialHandler) {
         return startListening0(serialHandler);
     }
 
@@ -145,7 +146,7 @@
      *
      * @param context handler of serial signal context
      */
-    private void stopListening(int context) {
+    private void stopListening(long context) {
         if (context != Constants.INVALID_HANDLE) {
             stopListening0(context);
         }
@@ -153,25 +154,25 @@
 
     private static class SignalEvent extends Event {
 
+        private long handler;
+        private int line;
+        private boolean state;
+
         SignalEvent(byte[] payload) {
-            super(ModemUART.class, payload);
+            super(ModemUART.class, null);
+            ByteBuffer bb = ByteBuffer.wrap(getPayload());
+            handler = bb.getLong();
+            line = bb.getInt();
+            state = bb.get() != 0;
         }
 
-        int getHandler() {
-            byte[] payload = getPayload();
-            int handler = (((int)(0x00ff & payload[0])) << 24) | (((int)(0x00ff & payload[1])) << 16) |
-                          (((int)(0x00ff & payload[2])) << 8 ) | (((int)(0x00ff & payload[3])));
+        long getHandler() {
             return handler;
         }
         int getLine() {
-            byte[] payload = getPayload();
-            int line = (((int)(0x00ff & payload[4])) << 24) | (((int)(0x00ff & payload[5])) << 16) |
-                       (((int)(0x00ff & payload[6])) << 8 ) | (((int)(0x00ff & payload[7])));
             return line;
         }
         boolean getState() {
-            byte[] payload = getPayload();
-            boolean state =  payload[8] != 0;
             return state;
         }
     }
@@ -184,7 +185,7 @@
     public boolean handleEvent(Event event) {
         SignalEvent e = new SignalEvent(event.getPayload());
 
-        int serialHandler = e.getHandler();
+        long serialHandler = e.getHandler();
         int signalLine = e.getLine();
         boolean signalState = e.getState();
 
@@ -204,13 +205,13 @@
      * @param serialHandler serial port handler
      * @return handler of serial signal context
      */
-    private native int startListening0(int serialHandler);
+    private native long startListening0(long serialHandler);
 
     /**
      * Stops serial signal listening
      *
      * @param context handler of serial signal context
      */
-    private native void stopListening0(int context);
+    private native void stopListening0(long context);
 
 }
--- a/src/se/classes/com/oracle/dio/uart/impl/UARTOptionsHandler.java	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/classes/com/oracle/dio/uart/impl/UARTOptionsHandler.java	Wed Sep 27 14:48:10 2017 +0300
@@ -62,5 +62,5 @@
         }
     }
 
-    private static native boolean enableRS485(int handle, boolean rts_on_send);
+    private static native boolean enableRS485(long handle, boolean rts_on_send);
 }
--- a/src/se/linux/native/com/oracle/dio/rs485_enabler.cpp	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/linux/native/com/oracle/dio/rs485_enabler.cpp	Wed Sep 27 14:48:10 2017 +0300
@@ -40,7 +40,7 @@
 
 
 JNIEXPORT jboolean JNICALL Java_com_oracle_dio_uart_impl_UARTOptionsHandler_enableRS485
-  (JNIEnv* env, jint def_ref_handle, jboolean rts_on_send) {
+  (JNIEnv* env, jlong def_ref_handle, jboolean rts_on_send) {
     struct serial_rs485 rs485conf;
     device_reference dev_ref = (device_reference)def_ref_handle;
     if (def_ref_handle == -1) {
--- a/src/se/native/com/oracle/dio/dio_common.cpp	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/native/com/oracle/dio/dio_common.cpp	Wed Sep 27 14:48:10 2017 +0300
@@ -130,11 +130,11 @@
     if (deviceHandleClass == NULL) {
         return JAVACALL_DIO_FAIL;
     }
-    jfieldID deviceNativeHandleField = env->GetFieldID(deviceHandleClass, "device_reference", "I");
+    jfieldID deviceNativeHandleField = env->GetFieldID(deviceHandleClass, "device_reference", "J");
     if (deviceNativeHandleField == NULL) {
         return JAVACALL_DIO_FAIL;
     }
-    env->SetIntField(handleObj, deviceNativeHandleField, (jint)device);
+    env->SetLongField(handleObj, deviceNativeHandleField, (jlong)device);
     return JAVACALL_DIO_OK;
 }
 
@@ -145,11 +145,11 @@
     if (deviceHandleClass == NULL) {
         return INVALID_DEVICE_REFERENCE;
     }
-    jfieldID deviceNativeHandleField = env->GetFieldID(deviceHandleClass, "device_reference", "I");
+    jfieldID deviceNativeHandleField = env->GetFieldID(deviceHandleClass, "device_reference", "J");
     if (deviceNativeHandleField == NULL) {
         return INVALID_DEVICE_REFERENCE;
     }
-    jint value = env->GetIntField(handleObj, deviceNativeHandleField);
+    jlong value = env->GetLongField(handleObj, deviceNativeHandleField);
     return (device_reference)value;
 }
 
--- a/src/se/native/com/oracle/dio/dio_event_queue.cpp	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/native/com/oracle/dio/dio_event_queue.cpp	Wed Sep 27 14:48:10 2017 +0300
@@ -96,3 +96,28 @@
     }
     cachedJVM->DetachCurrentThread();
 }
+
+static char* serialize_data(char* buffer, size_t buffer_size, char* data, size_t data_size) {
+  if (data_size > buffer_size) data_size = buffer_size;
+
+  if (!data_size) return buffer;
+
+  char* to = buffer;
+  char* from  = data + (data_size - 1);
+
+  while (data_size-- > 0) {
+    *to = *from;
+    to++;
+    from--;
+  }
+
+  return to;
+}
+
+char* serialize_pointer(char* buffer, size_t buffer_size, jlong ptr) {
+  return serialize_data(buffer, buffer_size, (char*)&ptr, sizeof(jlong));
+}
+
+char* serialize_int(char* buffer, size_t buffer_size, jint data) {
+  return serialize_data(buffer, buffer_size, (char*)&data, sizeof(jint));
+}
--- a/src/se/native/com/oracle/dio/dio_event_queue.h	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/native/com/oracle/dio/dio_event_queue.h	Wed Sep 27 14:48:10 2017 +0300
@@ -36,4 +36,15 @@
 void event_queue_put_native_event
   (JavaVM* cachedJVM, const char* device_type, const char *payload, int payload_size);
 
+/*
+ * Writes jlong type data into the buffer in BigEndian format
+ */
+char* serialize_pointer(char* buffer, size_t buffer_size, jlong ptr);
+
+/*
+ * Writes jint type data into the buffer in BigEndian format
+ */
+char* serialize_int(char* buffer, size_t buffer_size, jint data);
+
+
 #endif /* __DEVICEACCESS_EVENT_QUEUE_H */
--- a/src/se/native/com/oracle/dio/gpio/impl/jni_gpio.cpp	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/native/com/oracle/dio/gpio/impl/jni_gpio.cpp	Wed Sep 27 14:48:10 2017 +0300
@@ -23,6 +23,8 @@
  * questions.
  */
 
+#include <assert.h>
+
 #include <dio_common.h>
 #include <dio_exceptions.h>
 #include <javacall_gpio.h>
@@ -370,13 +372,23 @@
     if (device == INVALID_DEVICE_REFERENCE) {
         return;
     }
-    const int size = 12; // reserve 4 bytes for the handle, 4 for subevent (0 in this case), and 4 bytes for the value
+    const size_t size = sizeof(jlong) + (2 * sizeof(jint)); // reserve 8 bytes for the handle, 4 for subevent (0 in this case), and 4 bytes for the value
     char payload[size];
     memset(payload, 0, size);
-    payload[0] = (unsigned int)device >> 24, payload[1] = (unsigned int)device >> 16;
-    payload[2] = (unsigned int)device >> 8,  payload[3] = (unsigned int)device;
-    payload[8] = value >> 24, payload[9] = value >> 16;
-    payload[10] = value >> 8,  payload[11] = value;
+
+    char* ptr = payload;
+    size_t remains = size;
+
+    ptr = serialize_pointer(ptr, remains, (jlong)device);
+    remains -= sizeof(jlong);
+
+    assert(ptr < &payload[size]);
+    ptr = serialize_int(ptr, remains, (jint)0);
+    remains -= sizeof(jint);
+
+    assert(ptr < &payload[size]);
+    ptr = serialize_int(ptr, remains, (jint)value);
+
     JavaVM* vm = getGlobalJavaVM();
     event_queue_put_native_event(vm, "jdk.dio.gpio.GPIOPin", payload, size);
 }
@@ -392,13 +404,23 @@
     if (device == INVALID_DEVICE_REFERENCE) {
         return;
     }
-    const int size = 12; // reserve 4 bytes for the handle, 4 for subevent (0 in this case),  and 4 bytes for the value
+    const size_t size = sizeof(jlong) + (2 * sizeof(jint)); // reserve 8 bytes for the handle, 4 for subevent (0 in this case), and 4 bytes for the value
     char payload[size];
     memset(payload, 0, size);
-    payload[0] = (unsigned int)device >> 24, payload[1] = (unsigned int)device >> 16;
-    payload[2] = (unsigned int)device >> 8,  payload[3] = (unsigned int)device;
-    payload[8] = value >> 24, payload[9] = value >> 16;
-    payload[10] = value >> 8,  payload[11] = value;
+
+    char* ptr = payload;
+    size_t remains = size;
+
+    ptr = serialize_pointer(ptr, remains, (jlong)device);
+    remains -= sizeof(jlong);
+
+    assert(ptr < &payload[size]);
+    ptr = serialize_int(ptr, remains, (jint)0);
+    remains -= sizeof(jint);
+
+    assert(ptr < &payload[size]);
+    ptr = serialize_int(ptr, remains, (jint)value);
+
     JavaVM* vm = getGlobalJavaVM();
     event_queue_put_native_event(vm, "jdk.dio.gpio.GPIOPort", payload, size);
 }
--- a/src/se/native/com/oracle/dio/uart/impl/jni_signal_dispatcher.cpp	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/native/com/oracle/dio/uart/impl/jni_signal_dispatcher.cpp	Wed Sep 27 14:48:10 2017 +0300
@@ -23,6 +23,8 @@
  * questions.
  */
 
+#include <assert.h>
+
 #include <dio_common.h>
 #include <dio_exceptions.h>
 #include <javacall_serial.h>
@@ -36,8 +38,8 @@
  * Method:    startListening0
  * Signature: (II)Z
  */
-JNIEXPORT jint JNICALL Java_com_oracle_dio_uart_impl_ModemSignalDispatcher_startListening0
-  (JNIEnv* env, jobject obj, jint serialHandler) {
+JNIEXPORT jlong JNICALL Java_com_oracle_dio_uart_impl_ModemSignalDispatcher_startListening0
+  (JNIEnv* env, jobject obj, jlong serialHandler) {
     javacall_handle handle = (javacall_handle)serialHandler;
     javacall_handle context;
     javacall_result result;
@@ -47,7 +49,7 @@
     if (JAVACALL_OK != result) {
         context = JAVACALL_INVALID_HANDLE;
     }
-    return (jint)context;
+    return (jlong)context;
 }
 
 /*
@@ -56,7 +58,7 @@
  * Signature: (I)V
  */
 JNIEXPORT void JNICALL Java_com_oracle_dio_uart_impl_ModemSignalDispatcher_stopListening0
-  (JNIEnv* env, jobject obj, jint context) {
+  (JNIEnv* env, jobject obj, jlong context) {
     if ((javacall_handle)context != JAVACALL_INVALID_HANDLE) {
         javacall_serial_stop_dce_signal_listening((javacall_handle)context);
     }
@@ -78,16 +80,24 @@
         return;
     }
 
-    // reserve 4 bytes for the port handler, 4 bytes for the signal line,
-    // and 4 byte for the value
-    const int size = 12;
+    // reserve 8 bytes for the port handler, 4 bytes for the signal line,
+    // and 1 byte for the value
+    const size_t size = sizeof(jlong) + sizeof(jint) + 1;
     char payload[size];
+
+    char* pbuf = payload;
+    size_t remains = size;
+
     memset(payload, 0, size);
-    payload[0] = (jint)device >> 24, payload[1] = (jint)device >> 16;
-    payload[2] = (jint)device >> 8,  payload[3] = (jint)device >> 0;
-    payload[4] = (jint)signal >> 24, payload[5] = (jint)signal >> 16;
-    payload[6] = (jint)signal >> 8,  payload[7] = (jint)signal >> 0;
-    payload[8] = (value != JAVACALL_FALSE ? 1 : 0);
+
+    pbuf = serialize_pointer(pbuf, remains, (jlong)device);
+    remains -= sizeof(jlong);
+
+    assert(pbuf < &payload[size]);
+    pbuf = serialize_int(pbuf, remains, (jint)signal);
+
+    assert(pbuf < &payload[size]);
+    *pbuf = (value != JAVACALL_FALSE ? 1 : 0);
 
     JavaVM* vm = getGlobalJavaVM();
     event_queue_put_native_event(vm, "jdk.dio.uart.ModemUART", payload, size);
--- a/src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/se/native/com/oracle/dio/uart/impl/jni_uart.cpp	Wed Sep 27 14:48:10 2017 +0300
@@ -23,6 +23,8 @@
  * questions.
  */
 
+#include <assert.h>
+
 #include <dio_common.h>
 #include <dio_exceptions.h>
 #include <dio_nio.h>
@@ -633,17 +635,23 @@
         return;
     }
 
-    // reserve 4 bytes for the port handler, 4 bytes for event type,
+    // reserve 8 bytes for the port handler, 4 bytes for event type,
     // and 4 bytes for the bytes processed value
-    const int size = 12;
+    const size_t size = sizeof(jlong) + (2 * sizeof(jint));
     char payload[size];
 
-    payload[0] = (jint)device >> 24, payload[1] = (jint)device >> 16;
-    payload[2] = (jint)device >> 8,  payload[3] = (jint)device >> 0;
-    payload[4] = (jint)type >> 24, payload[5] = (jint)type >> 16;
-    payload[6] = (jint)type >> 8,  payload[7] = (jint)type >> 0;
-    payload[8]  = (jint)bytes >> 24, payload[9]  = (jint)bytes >> 16;
-    payload[10] = (jint)bytes >> 8,  payload[11] = (jint)bytes >> 0;
+    char* ptr = payload;
+    size_t remains = size;
+
+    ptr = serialize_pointer(ptr, remains, (jlong)device);
+    remains -= sizeof(jlong);
+
+    assert(ptr < &payload[size]);
+    ptr = serialize_int(ptr, remains, (jint)type);
+    remains -= sizeof(jint);
+
+    assert(ptr < &payload[size]);
+    ptr = serialize_int(ptr, remains, (jint)bytes);
 
     JavaVM* vm = getGlobalJavaVM();
     event_queue_put_native_event(vm, "jdk.dio.uart.UART", payload, size);
--- a/src/share/classes/com/oracle/dio/utils/Constants.java	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/share/classes/com/oracle/dio/utils/Constants.java	Wed Sep 27 14:48:10 2017 +0300
@@ -32,7 +32,7 @@
     public static final String FACTORY_PREFIX = "com.oracle.dio.";
     public static final String FACTORY = "Factory";
     public static final String CONFIG = "Config";
-    public static final int INVALID_HANDLE = -1;
+    public static final long INVALID_HANDLE = -1;
     public static final String PATH = "controllerName";
     public static final String IMPL = ".impl.";
     public static final String TYPE = "type";
--- a/src/share/linux/native/com/oracle/dio/gpio/gpio.c	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/share/linux/native/com/oracle/dio/gpio/gpio.c	Wed Sep 27 14:48:10 2017 +0300
@@ -520,7 +520,7 @@
         return res;
     }
 
-    snprintf(bufForDirectionFilename, DIRECTION_FILENAME_MAX_LENGTH, GPIO_DIRECTION_FILENAME_TEMPLATE, pin);
+    snprintf(bufForDirectionFilename, DIRECTION_FILENAME_MAX_LENGTH, GPIO_DIRECTION_FILENAME_TEMPLATE, (int)pin);
 
     // restore root privileges temporary
     restore_privileges();
--- a/src/share/linux/native/com/oracle/dio/i2c/i2c.c	Fri Mar 24 15:06:53 2017 +0300
+++ b/src/share/linux/native/com/oracle/dio/i2c/i2c.c	Wed Sep 27 14:48:10 2017 +0300
@@ -39,7 +39,7 @@
 #include "javacall_memory.h"
 
 
-#if ENABLE_DEBUG    
+#if ENABLE_DEBUG
     #define BUS_WR_LOCK(bus) \
       JAVACALL_REPORT_ERROR3(JC_DIO, "going to wrlock on bus 0x%x (file %s, line %d) ", (bus),__FILE__,__LINE__); \
       pthread_rwlock_wrlock(&(bus)->lock); \
@@ -177,7 +177,7 @@
 
 static javacall_dio_result close_i2c_bus_file(i2c_bus * bus) {
    int rv;
-   
+
    JAVACALL_REPORT_INFO1(JC_DIO, "[I2C] Closing bus file /dev/i2c-%d", bus->busId);
    /* If i2c device fd is opened */
     if (bus->fd >= 0) {
@@ -239,7 +239,7 @@
     JAVACALL_REPORT_ERROR1(JC_DIO, "[I2C] inconsistency in bus list, failed to release %d", bus->busId);
 
     BUSLIST_UNLOCK;
-    BUS_UNLOCK(bus);    
+    BUS_UNLOCK(bus);
     return JAVACALL_DIO_FAIL;
 }
 
@@ -256,12 +256,12 @@
     restore_privileges();
     slave->bus->fd = open(filename, O_RDWR);
     drop_privileges();
-   
+
     if (slave->bus->fd < 0) {
         JAVACALL_REPORT_ERROR1(JC_DIO, "[I2C] Could not open bus file %s ", filename);
         return JAVACALL_DIO_FAIL;
     }
-    
+
     return JAVACALL_DIO_OK;
 }
 
@@ -271,7 +271,7 @@
     i2c_bus *bus;
     i2c_slave *tmpSlave;
 
-    /* get bus descriptor */    
+    /* get bus descriptor */
     rv = i2c_get_bus(busId, &bus);
 
     if (JAVACALL_DIO_OK != rv) {
@@ -294,7 +294,7 @@
     slave->bus = bus;
     slave->next = bus->slaves;
     bus->slaves = slave;
-    
+
     /*  open bus file is needed */
     javacall_dio_result ret =  open_setup_i2c_bus_file(slave, busId);
     BUS_UNLOCK(bus);
@@ -346,12 +346,12 @@
 }
 
 
-#if ENABLE_DEBUG    
+#if ENABLE_DEBUG
 static void dumpBuf(i2c_message* msg) {
-    int i; 
+    int i;
 
     printf("[I2C] (address = 0x%X)", msg->addr);
-    
+
     if (msg->flags & I2C_M_RD) {
         printf("--->");
     } else {
@@ -378,28 +378,28 @@
         const javacall_bool exclusive,
         /*OUT*/javacall_handle* pHandle) {
     javacall_dio_result ret;
-    
+
     if (JAVACALL_TRUE != exclusive) {
         JAVACALL_REPORT_ERROR(JC_DIO, "[I2C] Shared mode is unsupported for I2C device");
         return JAVACALL_DIO_UNSUPPORTED_ACCESS_MODE;
     }
-    
+
     if (clockFrequency != PERIPHERAL_CONFIG_DEFAULT) {
         JAVACALL_REPORT_ERROR(JC_DIO, "[I2C] Clock frequency == -1 is supported only.");
         return JAVACALL_DIO_INVALID_CONFIG;
     }
-    
+
     if (addrSize == PERIPHERAL_CONFIG_DEFAULT)
         addrSize = 7;
-    
+
     if (busNum == PERIPHERAL_CONFIG_DEFAULT)
         busNum = 1;
-    
+
     if (addrSize != 7 && addrSize != 10) {
         JAVACALL_REPORT_ERROR(JC_DIO, "[I2C] Configuration error: addrSize != 7 or 10");
         return JAVACALL_DIO_INVALID_CONFIG;
     }
-    
+
     if (busNum < 0) {
         JAVACALL_REPORT_ERROR(JC_DIO,
                 "[I2C] Configuration error: busNum should not be less than zero");
@@ -408,7 +408,7 @@
 
     i2c_slave *slave;
     *pHandle = NULL;
-   
+
     slave = (i2c_slave*) javacall_calloc(1, sizeof(i2c_slave));
 
     if (slave == NULL) {
@@ -416,12 +416,12 @@
                 "[I2C] Memory allocation error, javacall_i2c_open_slave_with_config");
         return JAVACALL_DIO_OUT_OF_MEMORY;
     }
-    
-    
+
+
     slave->address = (long)devAddr;
     slave->addressSize = (long)addrSize;
     slave->clockFrequency = clockFrequency;
-    
+
     /* Attach device to a bus */
     ret = i2c_attach_slave_to_bus(slave, busNum);
     if (JAVACALL_DIO_OK != ret) {
@@ -460,17 +460,17 @@
     int i;
 
     i2c_frame* transaction = bus->currentTransaction;
-    
+
     if (transaction == NULL) {
         JAVACALL_REPORT_ERROR1(JC_DIO, "[I2C] Transaction is NULL for bus %d", bus->busId);
         return JAVACALL_DIO_FAIL;
     } else {
         nextFrame = transaction;
-        
+
         while(nextFrame != NULL) {
             nextFrame = nextFrame->next; transactionLength++;
         }
-    
+
         i2c_message* messages = (i2c_message*)javacall_malloc(sizeof(i2c_message)*transactionLength);
 
         if (messages == NULL) {
@@ -502,7 +502,7 @@
     i2c_frame* nextFrame;
     i2c_frame* curFrame;
     int i;
-    
+
     rv=ioctl(pBus->fd, I2C_RDWR, rdwr_data);
 
 #ifdef ENABLE_DEBUG
@@ -510,10 +510,10 @@
         dumpBuf(rdwr_data->msgs + i);
     }
 #endif
-    
+
     if ( rv < 0 ) {
         JAVACALL_REPORT_ERROR2(JC_DIO, "[I2C] rdwr ioctl error: %d (%s)", errno, strerror(errno));
-    } 
+    }
 
     if (rv >= 0) {
         rv = (rdwr_data->msgs + rdwr_data->nmsgs - 1)->len;
@@ -528,7 +528,7 @@
     javacall_free(rdwr_data->msgs);
     javacall_free(ctx);
 
-    return (void*)rv;
+    return (void*)(intptr_t)rv;
 }
 
 javacall_dio_result javacall_i2c_transfer_start(const javacall_handle handle,
@@ -539,9 +539,9 @@
 
     javacall_dio_result operationResult = JAVACALL_DIO_OK;
     i2c_slave* pDev = (i2c_slave*)handle;
-        
+
     pthread_attr_t attr;
-    
+
     if (JAVACALL_I2C_COMBINED_START == type || JAVACALL_I2C_REGULAR == type) {
         BUS_WR_LOCK(pDev->bus);
         if (NULL != pDev->bus->currentTransaction) {
@@ -556,45 +556,45 @@
 
     do {
         i2c_frame* nextFrame = (i2c_frame*) javacall_malloc(sizeof(i2c_frame));
-    
+
         if (NULL == nextFrame) {
             JAVACALL_REPORT_ERROR1(JC_DIO, "[I2C] Could not allocate i2c message structure, errno=%d", errno);
             operationResult =  JAVACALL_DIO_OUT_OF_MEMORY;
             break;
         }
-        
+
         nextFrame->msg.addr = pDev->address;
         nextFrame->msg.len = len;
         nextFrame->msg.buf = pData;
-    
+
         if (write == JAVACALL_TRUE) {
            nextFrame->msg.flags = 0;
         } else {
            nextFrame->msg.flags = I2C_M_RD;
         }
-    
+
         if (pDev->addressSize == 10) {
             nextFrame->msg.flags |= I2C_M_TEN;
         }
-        
+
         nextFrame->next = pDev->bus->currentTransaction;
         pDev->bus->currentTransaction = nextFrame;
-    
+
         if (JAVACALL_I2C_COMBINED_START == type || JAVACALL_I2C_COMBINED_BODY == type) {
             *pBytes = len;
             return JAVACALL_DIO_OK;
         }
 
-        
+
         i2c_io_context *ctx = javacall_malloc(sizeof(i2c_io_context));
-        
+
         if (NULL == ctx) {
             JAVACALL_REPORT_ERROR1(JC_DIO, "[I2C] Could not allocate temporary buffer, errno=%d", errno);
             operationResult =  JAVACALL_DIO_OUT_OF_MEMORY;
             break;
         }
 
-        ctx->pDev = pDev;       
+        ctx->pDev = pDev;
         ctx->pBus = pDev->bus;
 
         if (JAVACALL_DIO_OK != (operationResult = transaction_to_i2c_rdwr_ioctl_data(ctx->pBus, &ctx->rdwr_data))) {
@@ -610,13 +610,13 @@
             break;
         }
     } while (0);
-    
+
     if (operationResult != JAVACALL_DIO_OK) {
         BUS_WR_LOCK(pDev->bus);
-        cleanupTransaction(pDev->bus);        
+        cleanupTransaction(pDev->bus);
         BUS_UNLOCK(pDev->bus);
         return operationResult;
-    } 
+    }
 
     return JAVACALL_DIO_WOULD_BLOCK;
 }
@@ -628,11 +628,11 @@
     i2c_slave* pDev = (i2c_slave*)handle;
 
     BUS_WR_LOCK(pDev->bus);
-    cleanupTransaction(pDev->bus);        
+    cleanupTransaction(pDev->bus);
     BUS_UNLOCK(pDev->bus);
-    
+
     if (cancel == JAVACALL_TRUE) {
-        if (pDev->context != NULL) {
+        if (pDev->context != 0) {
             pthread_detach(pDev->context);
         }
     } else {
@@ -659,7 +659,7 @@
 javacall_dio_result javacall_i2c_get_group_id(const javacall_handle handle,
         /*OUT*/ int* grpId) {
     i2c_slave *pDev = (i2c_slave*)handle;
-    *grpId = (int)pDev->bus;
+    *grpId = (intptr_t)pDev->bus;
     return JAVACALL_DIO_OK;
 }