Skip to content

Commit

Permalink
compile nix32
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Emile committed Aug 1, 2012
1 parent e203fa1 commit 7659ae4
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 70 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.kevoree.fota.api.IFota;
import org.kevoree.fota.events.FotaEvent;
import org.kevoree.fota.events.UploadedFotaEvent;
import org.kevoree.fota.events.WaitingBLFotaEvent;
import org.kevoree.fota.utils.Board;
import org.kevoree.fota.utils.Constants;
import org.kevoree.fota.utils.FotaException;
Expand All @@ -29,6 +30,8 @@ public class Fota implements IFota {
private Nativelib nativelib;
private boolean finished=false;
private double timeout=0;
private Thread monitoringprogress;

public Fota(String deviceport,Board type) throws FotaException
{
if(deviceport.equals("*"))
Expand Down Expand Up @@ -77,7 +80,6 @@ public void close()
{
finished =true;
nativelib.close_flash();
nativelib= null;
}

public void fireFlashEvent (FotaEvent evt)
Expand All @@ -90,7 +92,10 @@ public void fireFlashEvent (FotaEvent evt)
((FotaEventListener) listeners[i + 1]).completedEvent(evt);
finished=true;
}
else
else if(evt instanceof WaitingBLFotaEvent)
{
// todo
}else
{
((FotaEventListener) listeners[i + 1]).progressEvent(evt);
}
Expand All @@ -107,13 +112,11 @@ public void upload(String path_hex_array) throws FotaException
program_size = nativelib.write_on_the_air_program(deviceport,devicetype,path_hex_array);
if(program_size < 0)
{
System.out.println("ici");
throw new FotaException("Empty");
throw new FotaException("The hex file is empty "+path_hex_array);
}
}catch (Exception e)
{
System.out.print("upload "+e);

}
}

Expand All @@ -132,4 +135,5 @@ public long getDuree() {
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.kevoree.fota;

import org.kevoree.fota.utils.ClassLoaderUtil;

import java.lang.reflect.Method;
import java.net.URLClassLoader;
import java.util.LinkedList;
import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: jed
* Date: 30/07/12
* Time: 10:14
* To change this template use File | Settings | File Templates.
*/
public class HelloWorld {
private native void print();

private static List<ClassLoader> loaders = new LinkedList<ClassLoader>();

public static void main(String[] args) throws Exception {
for (int i = 0; i < 3; i++) {
// Clone the current class loader. Use null as parent, so that no classes (but the standard java
// classes) are ever shared.
URLClassLoader loader = new URLClassLoader(((URLClassLoader) HelloWorld.class.getClassLoader()).getURLs(), null);
//Just to make sure the classloader is not GCed.
loaders.add(loader);

Class main;
main = loader.loadClass(HelloWorld.class.getName());
Method start = main.getMethod("start", new Class[0]);
start.invoke(null, new Object[0]);
//Now clear the dlls cache so that another classloader can load the same
List<String> dlls = new LinkedList<String>();
dlls.add("HelloWorld.dll");
ClassLoaderUtil.closeDLLs(loader, dlls);
}
}

public static void start() {
try {
System.loadLibrary("HelloWorld");
new HelloWorld().print();
} catch (Throwable th) {
th.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.EventObject;
import java.util.Random;

/**
* Created with IntelliJ IDEA.
Expand All @@ -30,15 +31,14 @@ public class Nativelib extends EventObject implements FotaEvent {
public native int write_on_the_air_program(String port_device,int target,String path_hex_file);
public native boolean register();
public native void close_flash();

private Fota fota;
private int size_uploaded;

private int size_uploaded=-1;
private double last_progress=0;

public Nativelib(Fota o) throws FotaException {
super(o);
fota = o;
// configureCL();
configureCL();
}
/**
* method call from JNI C
Expand All @@ -54,7 +54,7 @@ public void dispatchEvent(int evt)
fota.close();
} else if(evt == Constants.RE_SEND_EVENT)
{
logger.warn("RE_SEND_EVENT ");
logger.debug("RE_SEND_EVENT ");
}else if(evt == Constants.FAIL_OPEN_FILE)
{
System.out.println("FAIL_OPEN_FILE ");
Expand All @@ -71,7 +71,20 @@ else if(evt == Constants.ERROR_WRITE || evt == Constants.ERROR_READ)
}
else if(evt > 0)
{
if(evt > size_uploaded)
{
last_progress = System.currentTimeMillis();
}

double duree = ( System.currentTimeMillis() - last_progress) / 1000;

if(duree> 2)
{
logger.error("The bootloader is not responding");
fota.close();
}
this.size_uploaded = evt;

fota.fireFlashEvent(this);
}
}catch (FotaException e)
Expand Down Expand Up @@ -125,7 +138,7 @@ public static void addLibraryPath(String pathToAdd) throws Exception{
usrPathsField.set(null, newPaths);
}

public static String configureCL()
public static String configureCL()
{
try
{
Expand All @@ -136,16 +149,20 @@ public static String configureCL()
}
folder.mkdirs();

addLibraryPath(folder.getAbsolutePath());
String absolutePath = copyFileFromStream(getPath("native.so"), folder.getAbsolutePath(), "libnative" + getExtension());
addLibraryPath(folder.getAbsolutePath());
//http://developer.android.com/guide/practices/jni.html
//http://phani-bandanakanti.blogspot.fr/
// load the librairy with a different name - Bad approach :-)
String r = ""+new Random().nextInt(800);
String absolutePath = copyFileFromStream(getPath("native.so"), folder.getAbsolutePath(),"libnative"+r+""+ getExtension());

// System.load(absolutePath);
return absolutePath;
System.loadLibrary("native"+r);

return absolutePath;
} catch (Exception e) {
e.printStackTrace();
return null;
e.printStackTrace();
return null;
}

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Test {
public static void main(String[] args) throws Exception {



try
{
Fota fota = new Fota("*", Board.ATMEGA328);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package org.kevoree.fota.utils;

/**
* Created with IntelliJ IDEA.
* User: jed
* Date: 30/07/12
* Time: 09:45
* To change this template use File | Settings | File Templates.
*/
import java.lang.reflect.Field;
import java.net.URLClassLoader;
import java.text.MessageFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;


/**
* Provides utility functions related to URLClassLoaders or subclasses of it.
*
*/
public class ClassLoaderUtil {

private static final String CLASSLOADER_LOADED_LIBRARY_NAMES = "loadedLibraryNames"; //$NON-NLS-1$
private static final String CLASSLOADER_NATIVE_LIBRARIES = "nativeLibraries"; //$NON-NLS-1$
private static final String CLASSLOADER_NATIVELIBRARY_INNER_CLASS_NAME = "java.lang.ClassLoader$NativeLibrary"; //$NON-NLS-1$
private static final String CLASSLOADER_NATIVELIBRARY_FIELD_NAME = "name"; //$NON-NLS-1$

/* Fields used during processing - they can be set up once and then used repeatedly */
private static Class classLoaderInnerClass;
private static Field loadedLibNamesField;
private static Field nativeLibsField;
private static Field nameField;

private static boolean initDone = false;

/**
*Initializes the class.
*<p>
*Each utility method should invoke init() before doing their own work
*to make sure the initialization is done.
*@throws any Throwable detected during static init.
*/
private static void init() throws Throwable {
if ( ! initDone) {
initForClosingDlls();
initDone = true;
}
}

/**
*Sets up variables used in closing a loader's jar files.
*@throws NoSuchFieldException in case a field of interest is not found where expected
*/
private static void initForClosingDlls() throws NoSuchFieldException {
classLoaderInnerClass = getInnerClass(ClassLoader.class, CLASSLOADER_NATIVELIBRARY_INNER_CLASS_NAME);
nameField = getField(classLoaderInnerClass, CLASSLOADER_NATIVELIBRARY_FIELD_NAME);
loadedLibNamesField = getField(ClassLoader.class, CLASSLOADER_LOADED_LIBRARY_NAMES);
nativeLibsField = getField(ClassLoader.class, CLASSLOADER_NATIVE_LIBRARIES);
}

/**
*Retrieves a Field object for a given field on the specified class, having
*set it accessible.
*@param cls the Class on which the field is expected to be defined
*@param fieldName the name of the field of interest
*@throws NoSuchFieldException in case of any error retriving information about the field
*/
private static Field getField(Class cls, String fieldName) throws NoSuchFieldException {
try {
Field field = cls.getDeclaredField(fieldName);
field.setAccessible(true);
return field;
} catch (NoSuchFieldException nsfe) {
NoSuchFieldException e = new NoSuchFieldException (getMessage("classloaderutil.errorGettingField", fieldName)); //$NON-NLS-1$
e.initCause(nsfe);
throw e;
}
}

/**
*Retrieves a given inner class definition from the specified outer class.
*@param cls the outer Class
*@param innerClassName the fully-qualified name of the inner class of interest
*/
private static Class getInnerClass(Class cls, String innerClassName) {
Class result = null;
Class [] innerClasses = cls.getDeclaredClasses();
for (Class c : innerClasses) {
if (c.getName().equals(innerClassName)) {
result = c;
break;
}
}
return result;
}

//Just a workaround. couldn't find an alternate to close dll connections.
public static void closeDLLs(URLClassLoader classLoader, List<String> dlls) {
try {
init();

Vector<String> loadedLibNames = (Vector) loadedLibNamesField.get(classLoader);
Vector nativeLibs = (Vector) nativeLibsField.get(classLoader);

synchronized (loadedLibNames) {
for (int index = 0; index < dlls.size(); index++) {
Iterator<String> iter = loadedLibNames.iterator();
while (iter.hasNext()) {
String libName = iter.next();
if (libName != null && libName.indexOf(dlls.get(index)) != -1) {
iter.remove();
}
}
}
}

synchronized (nativeLibs) {
for (int index = 0; index < dlls.size(); index++) {
Iterator nIter = nativeLibs.iterator();
while (nIter.hasNext()) {
Object ClassLoader$NativeLibrary = nIter.next();
String name = (String) nameField.get(ClassLoader$NativeLibrary);
if (name != null && name.indexOf(dlls.get(index)) != -1) {
nIter.remove();
}
}
}
}
} catch (Throwable th) {
// ignore
}
}


/**
*Returns a formatted string, using the key to find the full message and
*substituting any parameters.
*@param key the message key with which to locate the message of interest
*@param o the object(s), if any, to be substituted into the message
*@return a String containing the formatted message
*/
private static String getMessage(String key, Object ... o) {
return MessageFormat.format(key, o);
}
}
Binary file not shown.

This file was deleted.

Loading

0 comments on commit 7659ae4

Please sign in to comment.