Programming Android in Ada

quantityforeheadΚινητά – Ασύρματες Τεχνολογίες

10 Δεκ 2013 (πριν από 3 χρόνια και 10 μήνες)

75 εμφανίσεις

Slide: 1

Copyright
©
2013 AdaCore

José F. Ruiz
FOSDEM 2013, Brussels
Senior Software
Engineer

AdaCore

Programming Android in Ada
3 February 2013
Slide: 2

Copyright
©
2013 AdaCore

Outline


The
execution

platform




How to
build
applications


Using
the
virtual
machine


Interfacing
the
virtual
machine


Ada
compilers
and
related
technologies



The show


Or how to use
it

Slide: 3

Copyright
©
2013 AdaCore

Android


Linux-based operating system


Open source


Targets smartphones and tablets


But not only


Applications implemented mostly in Java


Available SDK, APIs, IDE


Main hardware platform is ARM


But also x86, MIPS


There are nice emulators to facilitate development and testing
Slide: 4

Copyright
©
2013 AdaCore



Linux kernel


Middleware


Libraries


APIs


Application framework


Includes Java-compatible libraries


Uses the
Dalvik
virtual machine with just-in-time compilation


Run
Dalvik
'
dex
-code' (
Dalvik
Executable)


Usually translated from Java
bytecode

Android software architecture
Slide: 5

Copyright
©
2013 AdaCore

Android SDK


Android Software Development Kit (SDK)


API libraries and developer tools necessary to build, test, and debug apps for Android


Includes simulator


http://developer.android.com/sdk/index.html



Android Native Development Kit (NDK)


Implement parts of your app using native-code languages (C, C++,
Ada
,…)


http://developer.android.com/tools/sdk/ndk/index.html

Slide: 6

Copyright
©
2013 AdaCore

The strategy for Ada


Use the Android SDK for what it handles well


Structure of the application, graphical interface, …


Java at this point


On the
Dalvik
VM


Use
GNATbench
to create a mixed Java/Ada application


Write the Ada subprogram as usual


Automatic creation of the glue code for the interface to Java


Use the generated Java code from the Android application
package
Hello
is
function

Msg
return
String;
end
Hello
;

import

com.example.Hello.Hello_Package
;

public class
MainActivity

extends
Activity {


view.SetText
(
Hello_Package.Msg
().
toString
());

}
Slide: 7

Copyright
©
2013 AdaCore

Interfacing Ada and Java


Goal: applications not written entirely in Java


Standard Java class library missing platform-dependent features


Reuse already existing code


Implement time-critical part of the application


A communication layer between the two languages


Java Native Interface (JNI)


Low level interface between the JVM and the native environment


Expensive and error-prone to use manually


An interface in the target language


Binding generation tool (Ada2Java)


A builder for the whole application


GPRbuild



A debugger for the whole application


Eclipse (with GDB & Java debugger underneath)
Slide: 8

Copyright
©
2013 AdaCore

How it works
I have code written in Ada

I want to use this code in a
Java project

JNI

procedure
Proc (P : Rec);

void
Proc (Rec_Bound P);

On a single machine…

Slide: 9

Copyright
©
2013 AdaCore

Ada2Java


Takes a set of Ada packages as input


The interface that we want to make available to Java


Process the public specifications, using ASIS


Generates Ada and Java connection to JNI


Generates a Java high level and type safe view of the Ada package


The user does not deal with JNI directly
Slide: 10

Copyright
©
2013 AdaCore

Example of binding generation
package
Test
is



function
Addition (A, B :
Integer
)
return

Integer
;

end
Test;
package
Test;

public

class
Test_Package {


static

public

int
Addition (
int
A,
int
B){
// Code …
}
}
public

static

void
main (String [] argv) {
int
r = Test_Package.Addition(10, 20);

System.out.println ("10 + 20 = " + r);
}
Slide: 11

Copyright
©
2013 AdaCore

Ada part of the toolset


GNATbench



Official Android IDE is Eclipse


Eclipse plug-in to build Ada libraries on Android


Ada-Java Interfacing Suite (AJIS)


Java applications on Android can invoke Ada code using interfaces automatically
generated by AJIS



GNAT for Android


For the part of the application running on Linux/Android (outside the VM)


Configured for ARM/Linux
Slide: 12

Copyright
©
2013 AdaCore

Ada tasks on Android


Ada task runs on top of operating system thread


One-to-one correspondence


Task dispatching policy


Can be selected with
pragma
Dispatching_Policy



SCHED_OTHER by default


Preemptive priority scheduling


pragma
Dispatching_Policy
(
FIFO_Within_Priorities
)


pthread_setschedparam
(Thread, SCHED_FIFO,
Param’Access
)


Means run until blocked (or preempted), no time slicing


Reduces non-determinism


Multiprocessing


Unfortunately, the thread affinity interface does not seem to be there
Slide: 13

Copyright
©
2013 AdaCore

Synchronization


A protected type is a data object with locks


Data encapsulation, accessible through interface (locked access routines)


functions (read the data with read lock)


procedures (read/write the data with write lock)


entries (wait until some condition is met, then read/write the data with write lock)


Priority inheritance


Guard against priority inversion


low priority task grabs resource X


high priority task needs resource X, waits


medium priority task preempts low priority task, and runs for a long time, holding up high priority task


Solution, while high priority task is waiting, lend high priority to low priority task


Implementation


Mutual exclusion


Mutex



Waiting / Signaling operations


Conditional variable
Slide: 14

Copyright
©
2013 AdaCore

System programming


Clock and delay


Clock uses
gettimeofday



Delay operations use timed conditional variables


We can wakeup the task before expiration if needed


Interrupt handling


Underlying signal mechanism


A server task per interrupt served


With specific mask to serve the required signal


Call to
sigwait

Slide: 15

Copyright
©
2013 AdaCore

Demo


Lunar Lander


Sample provided by the Android SDK


Reimplement
the computation of physics behavior in Ada


Position and speed of the spacecraft
Slide: 16

Copyright
©
2013 AdaCore

Conclusion


Simple way to benefit from the Android SDK and Ada


Do as much as is convenient with the Android SDK


Then use Ada for


CPU intensive, critical, portable, … parts


The trick is


To realize that the Android platform is a Linux platform on with there is a JVM


To use the automatic generation of Ada/Java bindings


Nice integration with Eclipse to facilitate usage