owf12/paug conf days android system development, maxime ripard, free electrons

25
OpenWorld Forum 2012 Android System Development Maxime Ripard Free Electrons maxime.ripard@free- electrons.com Embedded Linux Developers Free Electrons Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 1/25

Upload: open-world-forum

Post on 26-Dec-2014

4.199 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

OpenWorld Forum 2012

Android SystemDevelopment

Maxime RipardFree [email protected]

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 1/25

Page 2: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Maxime Ripard

I Embedded Linux engineer and trainer at Free Electrons since2011

I Embedded Linux development: kernel and driver development,system integration, boot time and power consumptionoptimization, consulting, etc.

I Embedded Linux training, Linux driver development trainingand Android system development training, with materialsfreely available under a Creative Commons license.

I http://www.free-electrons.com

I Contributor to various open-source projects: Barebox, Linux,Buildroot

I Living in Toulouse, south west of France

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 2/25

Page 3: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Why Modify Android ?

I Because we have a custom set of functions on a given devicethat isn’t handled by the vanilla Android

I Because we want to add a different UI/set of features thanAOSP

I Because since we have access to the source code, we can!

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 3/25

Page 4: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

The Android Stack

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 4/25

Page 5: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Devices in the Source Tree

I The build system is using a configuration mechanism based onvarious layer of hardware design

I The upper layer, the product is typically the one we want tobuild a system for

I The build system has no other configuration mechanism: noKconfig, text file, or any other mecanism that can be easilymodified

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 5/25

Page 6: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Product, devices and boards

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 6/25

Page 7: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Defining new products

I Devices are well supported by the Android build system. Itallows to build multiple devices with the same source tree, tohave a per-device configuration, etc.

I All the product definitions should be put indevice/<company>/<device>

I The entry point is the AndroidProducts.mk file, whichshould define the PRODUCT_MAKEFILES variable

I This variable defines where the actual product definitions arelocated.

I It follows such an architecture because you can have severalproducts using the same device

I If you want your product to appear in the lunch menu, youneed to create a vendorsetup.sh file in the device

directory, with the right calls to add_lunch_combo

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 7/25

Page 8: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Our New Product

I AndroidProducts.mk

PRODUCT_MAKEFILES += \

$(LOCAL_DIR)/full_product.mk

I full_product.mk

$(call inherit-product, build/target/product/generic.mk)

PRODUCT_NAME := full_MyDevice

PRODUCT_DEVICE := MyDevice

PRODUCT_MODEL := Full flavor of My Brand New Device

I vendorsetup.sh

add_lunch_combo full_product-eng

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 8/25

Page 9: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Add a New Component

I The integration of new components into the build system aredone through Android.mk files

I These files just contains where the source code is and whatthe expected result should be: an executable, a shared library,a jar file, etc.

I All the build magic is abstracted by the build system

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 9/25

Page 10: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Add a New Library

I Create a new subfolder under thedevice/company/product/lib folder

I This folder will contain the source code in itself, plus theheaders and the Android.mk file

I To include a new package into a given build, you need to addthe package name to the content of the PRODUCT_PACKAGES

variable

I Let’s use the libusb as an example

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 10/25

Page 11: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Steps to add the libusb

I The first step is obviously to untar the source code in thedevice/company/product/lib/libusb folder

I Since the Android build system doesn’t rely at all on externalbuild tools, we have some bits that are missing

I The first one is that the source code is missing the config.h

header file, that is usually generated with the autoconf tool,through the ./configure command

I When we try to compile, it lacks two things: theTIMESPEC_TO_TIMEVAL macro and the timerfd.h header

I The first one should be declared by the c library, let’s add themacro declaration where the source code is using it

I The timerfd.h file is one of the header exposed by the LinuxKernel. It is not in the headers found in the toolchain used byAndroid though, so we will need to disable the use of suchfeature through the command./configure --disable-timerfd

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 11/25

Page 12: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Library Makefile

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := \

core.c \

descriptor.c \

io.c \

sync.c \

os/linux_usbfs.c

LOCAL_C_INCLUDES += \

$(LOCAL_PATH)/os

LOCAL_MODULE:= libusb

LOCAL_MODULE_TAGS:= optional

LOCAL_PRELINK_MODULE:= false

include $(BUILD_SHARED_LIBRARY)

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 12/25

Page 13: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Next steps

I The libusb uses the device files under the folder/dev/bus/usb, that are only readable by the root user andthe usb group.

I The device files creations are handled by the init application,so that when a new device appears on the system, itsassociated device files will be created as well

I The specific part of init that handles these files is calledueventd

I It uses a set of rules to create device files with the correctpermissions at runtime

I If we want to use a binary running as user and that has accessto the USB devices, we’ll have to modify these permissions

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 13/25

Page 14: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Modify the permissions of the device files

I Permissions on the files are the enforced thanks to twocomponents:

I Permissions on files on the system are defined in a C header:android_filesystem_config.h

I Permissions for device files are handled by ueventd that uses afile called ueventd.rc

I We can modify the permissions on the device files by addingor modifying a ueventd.rc file

/dev/bus/usb/* 0666 root usb

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 14/25

Page 15: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Integrate a binary that uses the libusb

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES:= usb_bin.c

LOCAL_C_INCLUDES += \

$(JNI_H_INCLUDE) \

device/company/device/lib/libusb

LOCAL_SHARED_LIBRARIES := libusb

LOCAL_MODULE := usbbin

LOCAL_MODULE_TAGS := optional

LOCAL_PRELINK_MODULE := false

include $(BUILD_EXECUTABLE)

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 15/25

Page 16: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

JNI Bindings

I A Java framework to call and be called by native applicationswritten in other languages

I Mostly used for:I Writing Java bindings to C/C++ librariesI Accessing platform-specific featuresI Writing high-performance sections

I It is used extensively across the Android userspace to interfacebetween the Java Framework and the native daemons

I Since Gingerbread, you can develop apps in a purely nativeway, possibly calling Java methods through JNI

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 16/25

Page 17: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Libusb bindings

I Create a new subfolder underdevice/company/device/framework/USBFramework/jni

I Write the JNI bindings so that the Java can call functionsfrom the libusb

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 17/25

Page 18: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Libusb bindings

static struct libusb_device_handle *devh;

JNIEXPORT void JNICALL Java_com_fe_USB_init(JNIEnv *env,

jobject obj)

{

ret = libusb_init(NULL);

if (ret < 0)

return ret;

devh = libusb_open_device_with_vid_pid(NULL,

0xdead,

0xbeef);

if (!devh) {

libusb_exit(NULL);

return -EINVAL;

}

return 0;

}Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 18/25

Page 19: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Android.mk for the bindings

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \

usb_jni.c

LOCAL_C_INCLUDES += \

$(JNI_H_INCLUDE) \

device/company/device/lib/libusb

LOCAL_SHARED_LIBRARIES := \

libusb

LOCAL_MODULE := libusb_jni

LOCAL_MODULE_TAGS := optional

LOCAL_PRELINK_MODULE := false

include $(BUILD_SHARED_LIBRARY)

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 19/25

Page 20: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Java Framework

package com.fe;

class USB {

private native void init();

public USB() {

init();

}

static {

System.loadLibrary("usb_jni");

}

}

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 20/25

Page 21: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Android.mk for the framework

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := \

$(call all-subdir-java-files)

LOCAL_MODULE_TAGS := optional

LOCAL_MODULE:= com.fe.usb

include $(BUILD_JAVA_LIBRARY)

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 21/25

Page 22: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Permissions file

<?xml version="1.0" encoding="utf-8"?>

<permissions>

<library name="com.fe.usb"

file="/system/framework/com.fe.usb.jar"/>

</permissions>

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 22/25

Page 23: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Applications using that code

I Any applications can now use the part of the framework wejust added, using a regular Java call

I However, by default, the jar file generated won’t be loaded bydalvik, resulting in a error at runtime.

I You need to add extra bits to the manifest file so that boththe Play Store and Dalvik knows that you need the given jarfile to work properly

I You need to add the <uses-library> XML tag

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 23/25

Page 24: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Android Manifest

<?xml version="1.0" encoding="utf-8"?>

<manifest

xmlns:android="http://schemas.android.com/apk/res/android"

package="com.fe.usb">

<application

android:icon="@drawable/icon"

android:label="@string/app_name">

...

<uses-library android:name="com.fe.usb"

android:required="true" />

</application>

</manifest>

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 24/25

Page 25: OWF12/PAUG Conf Days Android system development, maxime ripard, free electrons

Questions?

Maxime Ripard

[email protected]

Slides under CC-BY-SA 3.0.

Free Electrons. Kernel, drivers and embedded Linux development, consulting, training and support. http://free-electrons.com 25/25