2010年10月14日星期四

[Eclipse] Tricks

* Use a Java library inside your Android project:
  1. Create a folder (e.g. “lib”)
  2. Place .jar into this folder
  3. Right-click the jar and select “Build Path” -> Add to build path.

2010年10月12日星期二

[Android] Startup

1. Boot Code (in Chip)
  *Configure Chip: CPU, UART, Memory, (SD), Interrupt controller, watchdog, ...
  *Download Boot Loader into Memory (Internal SRAM or external DRAM)
2. Boot Loader (e.g., U-boot)
  *Initialize Source Sevice: NAND, SD, NOR, USB, Ethernet, ...
  *Initialize Target memory: DRAM, ...
  *Setup file system, network support, memory protections and security options, ...
  *Download kernel & application images from source storage to target memory
3. Linux Kernel
  *Set up interrupt controllers, MMU memory protections, caches and scheduling.
4. Android Init
 
  4.1 Entry Point main() in ~/android/system/core/init/init.c
  4.1.1 Clear the umask
  4.1.2 Setup the basic filesystem (/dev, /proc, /sys)
  4.1.3 Creates device nodes for kmsg and null (not /)
  4.1.4 Initialize Log
  4.1.5 Parse init.rc
  4.1.6 Pull the kernel commandline and ramdisk properties file in
  4.1.7 Parse hardware platform init.xxx.rc

  4.1.8 Trigger "early-init"
  4.1.9 device_init();
  4.1.10 property_init();
  4.1.11 Listen for keychords
  4.1.12 Open console
  4.1.13 Show "Android" by load_565rle_image(INIT_IMAGE_FILE)
  4.1.14 property_set("ro.xxx", ...);
  4.1.15 Trigger "init" (refer to init.rc)
  4.1.16 start_property_service();
  4.1.17 Create a signalling mechanism for the sigchld handler
  4.1.18 Trigger "early-boot"
  4.1.19 Trigger "boot" (refer to init.rc)
  4.1.20 Run all property triggers
  4.1.21 Register file descriptors with polling events

  4.1.22 Infinite Loop
  4.1.22.1  restart_processes() if needed
  4.1.22 .2 Poll registered file descriptors
  4.1.22 .3 Handle revents

  4.2 Start Services in ~/android/system/core/rootdir/init.rc
    *Services: console, adbd, servicemanager,  vold, netd, debuggerd, ril-daemon, zygote,  media, bootanim, dbus, bluetoothd, hfag, opush, pbap, installd, flash_recovery, racoon, mtpd, keystore, dumpstate
    *See the following 
root@android:/ # ps
USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME
root      1     0     296    204   c009b74c 0000caac S /init
root      2     0     0      0     c004e72c 00000000 S kthreadd
root      3     2     0      0     c003fdc8 00000000 S ksoftirqd/0
root      4     2     0      0     c004b2c4 00000000 S events/0
root      5     2     0      0     c004b2c4 00000000 S khelper
root      6     2     0      0     c004b2c4 00000000 S suspend
root      7     2     0      0     c004b2c4 00000000 S kblockd/0
root      8     2     0      0     c004b2c4 00000000 S cqueue
root      9     2     0      0     c018179c 00000000 S kseriod
root      10    2     0      0     c004b2c4 00000000 S kmmcd
root      11    2     0      0     c006fc74 00000000 S pdflush
root      12    2     0      0     c006fc74 00000000 S pdflush
root      13    2     0      0     c00744e4 00000000 S kswapd0
root      14    2     0      0     c004b2c4 00000000 S aio/0
root      22    2     0      0     c017ef48 00000000 S mtdblockd
root      23    2     0      0     c004b2c4 00000000 S kstriped
root      24    2     0      0     c004b2c4 00000000 S hid_compat
root      25    2     0      0     c004b2c4 00000000 S rpciod/0
root      26    1     728    404   c0025d68 afd0e56c S /system/bin/sh
system    27    1     836    264   c01a94a4 afd0dd3c S /system/bin/servicemanager
root      28    1     3760   528   ffffffff afd0e3ac S /system/bin/vold
root      29    1     3736   516   ffffffff afd0e3ac S /system/bin/netd
root      30    1     700    268   c01b52b4 afd0e6cc S /system/bin/debuggerd
radio     31    1     3356   656   ffffffff afd0e3ac S /system/bin/rild
nobody    32    1     63140  22560 00000000 8021c5f8 R zygote
media     33        19852  3724  ffffffff afd0dd3c S /system/bin/mediaserver
bluetooth 34    1     1288   600   c009b74c afd0eb7c S /system/bin/dbus-daemon
root      35        832    276   c01b52b4 afd0e6cc S /system/bin/installd
keystore  36    1     1644   404   c01b52b4 afd0e6cc S /system/bin/keystore
root      37    1     724    372   c0025d68 afd0e56c S /system/bin/sh
root      38        852    336   c00b8fec afd0eafc S /system/bin/qemud
root      40    1     1320   152   ffffffff 0000ee04 S /sbin/adbd
root      56    37    820    272   c02181f4 afd0da9c S /system/bin/qemu-props
root      64    26    924    348   00000000 afd0da9c R ps


  *kthreadd: Linex kernel thread daemon
  4.3 Zygote: Start a Dalvik VM instance and System Server.
            In ~/android/frameworks/base/cmds/app_process/app_main.cpp:
            runtime.start("com.android.internal.os.ZygoteInit",
                startSystemServer); 

            In  ~/android/frameworks/base/core/jni/AndroidRuntime.cpp:
           void AndroidRuntime::start(const char* className, const bool startSystemServer)
           if (startVm(&mJavaVM, &env) != 0)
            In ~/android/frameworks/base/services/java/com/android/server/SystemServer.java:
            startSystemServer();  4.3.1 Dalvik VM
  4.3.2 System Server: Add Critical System Services
          In ~/android/frameworks/base/services/java/com/android/server/SystemServer.java
        // Critical services...
        try {
            Slog.i(TAG, "Entropy Service");
            ServiceManager.addService("entropy", new EntropyService());

            Slog.i(TAG, "Power Manager");
            power = new PowerManagerService();
            ServiceManager.addService(Context.POWER_SERVICE, power);

            Slog.i(TAG, "Activity Manager");
            context = ActivityManagerService.main(factoryTest);

            Slog.i(TAG, "Telephony Registry");
            ServiceManager.addService("telephony.registry", new TelephonyRegistry(context));

            AttributeCache.init(context);
            Slog.i(TAG, "Package Manager");
            pm = PackageManagerService.main(context,
                    factoryTest != SystemServer.FACTORY_TEST_OFF);

            ActivityManagerService.setSystemProcess();
            mContentResolver = context.getContentResolver();
            // The AccountManager must come before the ContentService
            try {
                Slog.i(TAG, "Account Manager");
                ServiceManager.addService(Context.ACCOUNT_SERVICE,
                        new AccountManagerService(context));
            } catch (Throwable e) {
                Slog.e(TAG, "Failure starting Account Manager", e);
            }

            Slog.i(TAG, "Content Manager");
            ContentService.main(context,
                    factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);
            Slog.i(TAG, "System Content Providers");
            ActivityManagerService.installSystemProviders();
            Slog.i(TAG, "Battery Service");
            battery = new BatteryService(context);
            ServiceManager.addService("battery", battery);
            Slog.i(TAG, "Lights Service");
            lights = new LightsService(context);
            Slog.i(TAG, "Vibrator Service");
            ServiceManager.addService("vibrator", new VibratorService(context));
            // only initialize the power service after we have started the
            // lights service, content providers and the battery service.
            power.init(context, lights, ActivityManagerService.getDefault(), battery);
            Slog.i(TAG, "Alarm Manager");
            AlarmManagerService alarm = new AlarmManagerService(context);
            ServiceManager.addService(Context.ALARM_SERVICE, alarm);
            Slog.i(TAG, "Init Watchdog");
            Watchdog.getInstance().init(context, battery, power, alarm,
                    ActivityManagerService.self());
            // Sensor Service is needed by Window Manager, so this goes first
            Slog.i(TAG, "Sensor Service");
            ServiceManager.addService(Context.SENSOR_SERVICE, new SensorService(context));
            Slog.i(TAG, "Window Manager");
            wm = WindowManagerService.main(context, power,
                    factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);
            ServiceManager.addService(Context.WINDOW_SERVICE, wm);
            ((ActivityManagerService)ServiceManager.getService("activity"))
                    .setWindowManager(wm);

5. Start-up Complete
  *Broadcast ACTION_BOOT_COMPLETED



[] http://hi.baidu.com/j_fo/blog/item/c782b11bbb16f1178618bf82.html
[]The Android boot process from power on
[] 如何去寫Android init.rc (Android init language)
[] http://source.android.com/porting/bring_up.html
[] http://blog.chinaunix.net/u2/87328/showart_1678365.html
[] Android Zygote Startup

2010年10月7日星期四

Android Porting: Make Kernel (Example: MSM)

1. Get Android kernel source code
   git clone git://android.git.kernel.org/kernel/common.git

2. Set Environment Variables for ARM Compiler
 in ~/.bashrc, add
export ARCH=arm
export CROSS_COMPLIE=arm-eabi-
export PATH=$PATH:~/android/prebuilt/linux-x86/toolchain/arm-eabi-4.3.1/bin

3.  Set Kernel Configuration
*Based on ~/Documentation/android.txt
*Use MSM as default config
 hmtsay@ubuntu:~/kernel/froyo$ make msm_defconfig
 Then, customized by  $make menuconfig
Device Drivers  ---> 
Network device support  --->
 Wireless LAN  --->
    [*] Wireless LAN (IEEE 802.11)   
    [*] IEEE 802.11 for Host AP (Prism2/2.5/3 and WEP/TKIP/CCMP)   
    [*]   Support downloading firmware images with Host AP driver  
    [*]     Support for non-volatile firmware download

  [*] Power supply class support  --->
    [*]   Generic PDA/phone power driver
  [*] MMC/SD card support  --->


4. add Options in Makefile
ARCH=arm
CROSS_COMPILE=../../android/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/arm-eabi-

5. make
hmtsay@ubuntu:~/kernel/froyo/arch/arm/boot$ ls -l -a
...
-rwxr-xr-x  1 hmtsay hmtsay 1159504 2010-10-07 06:38 zImage
[] http://nemustech.blogspot.com/2009/04/android-on-s3c6410-target-board.html

2010年10月6日星期三

[Android] Linux Kernel Branch

    -Based on Linux 2.6 with modifications
    -Adv:
     1) Memory & Process Management
     2) Permissions-based Secuity model
     3) Network Protocol Stacks
     4) Proven & Mass Drivers: e.g., Display, Camera, Bluetooth, USB, Keypad, WiFi, Audio, Flash
     5) Open Source
    -Modifications
     1) Remove Windowing System
     2) Not Support: glibc
     3) Not Support: Standard Linux Utilities
    -Enhancements for Android
     1) Binder: an OpenBinder-based Driver to Reduce IPC (inter-process communication) Overhead,
     2) Power Management: Light Weight, Optimized for Battery-equiped Devices
     3)  Low Memory Killer: 
          -Kill off processes to free up memory based on hints from the userspace.
          -More flexibility than the Out Of Memory (OOM) killer in the standard kernel.
     4) Logger: Llight weight to capture system, radio, logdata, etc.
     5) Alarm: Provide timers to wake the device up from sleep
     6) USB Gadget: Provide USB function framework.
     7) PMEM: Provide contiguous physical memory regions to userspace libraries that interact with the digital signal processor (DSP) and other specific hardware.
     8) Ashmem (anonymous Shared Memory): new shared memory allocator for Binder
     9) EABI (embedded Application Binary Interface) []http://kezeodsnx.pixnet.net/blog/post/25839746
    [] http://developer.android.com/guide/basics/what-is-android.html
    [] http://elinux.org/Android_Kernel_Features

[Android] Kernel Layout

hmtsay@ubuntu:~/kernel/froyo$ ls
arch     Documentation  init    MAINTAINERS  REPORTING-BUGS  usr
block    drivers        ipc     Makefile     samples         virt
COPYING  firmware       Kbuild  mm           scripts
CREDITS  fs             kernel  net          security
crypto   include        lib     README       sound
[]http://www.mulix.org/lectures/tau_linux_workshop/kernel_src_intro.pdf

Note. Android specific drivers is in ~/drivers/misc.
hmtsay@ubuntu:~/kernel/froyo/drivers/misc$ ls
acer-wmi.c       fujitsu-laptop.c   kgdbts.c           sgi-xp
asus-laptop.c    hdpuftrs           lkdtm.c            sony-laptop.c
atmel_pwm.c      hpilo.c            logger.c           tc1100-wmi.c
atmel-ssc.c      hpilo.h            lowmemorykiller.c  thinkpad_acpi.c
atmel_tclib.c    hp-wmi.c           Makefile           tifm_7xx1.c
binder.c         ibmasm             msi-laptop.c       tifm_core.c
compal-laptop.c  intel_menlow.c     phantom.c          timed_gpio.c
eeepc-laptop.c   ioc4.c             pmem.c             timed_output.c
eeprom_93cx6.c   Kconfig            ram_console.c      uid_stat.c
enclosure.c      kernel_debugger.c  sgi-gru

2010年10月5日星期二

[Android] Project Layout

Core Project: foundation of the Android platform
hmtsay@ubuntu:~/android$ ls
bionic    cts          device      hardware  ndk       prebuilt
bootable  dalvik       external    libcore   out      
sdkbuild     development  frameworks  Makefile  packages  system

[] http://sites.google.com/a/android.com/opensource/projects
cts: Compatibility Test Suite
   []http://source.android.com/compatibility/cts-intro.html
device:
kernel: No (separated Android from Linux kernel)
libcore: ?
ndk: Provide tools and documentation for developers to create native code shared libraries.
out: Compile results? (see PS)
sdk: Provide tools, sample code, and docs for developer to create great applications.

External Project: Android makes use of many other open source projects
hmtsay@ubuntu:~/android/external$ ls
apache-http  elfutils    ipsec-tools     netperf   srec
astl         embunit     iptables        neven     stlport
bison        emma        jdiff           opencore  strace
blktrace     esd         jhead           openssl   svox
bluetooth    expat       jpeg            oprofile  tagsoup
bsdiff       fdlibm      jsilver         ping      tcpdump
bzip2        freetype    jsr305          ppp       tinyxml
clearsilver  fsck_msdos  junit           proguard  tremolo
dbus         genext2fs   kernel-headers  qemu      v8
dhcpcd       giflib      libffi          quake     webkit
dnsmasq      grub        libpcap         safe-iop  wpa_supplicant
dropbear     gtest       libpng          skia      wpa_supplicant_6e2fsprogs    guava       libxml2         sonivox   xmlwritereasymock     icu4c       mtpd            speex     yaffs2
elfcopy      iproute2    netcat          sqlite    zlib


Packages: standard Android applications and services
hmtsay@ubuntu:~/android/packages$ ls
apps  experimental  inputmethods  providers  wallpapers
hmtsay@ubuntu:~/android/packages$ ls apps/
AccountsAndSyncSettings  CertInstaller  Launcher2         QuickSearchBox
AlarmClock               Contacts       Mms               Settings
Bluetooth                DeskClock      Music             SoundRecorder
Browser                  Email          PackageInstaller  SpeechRecorder
Calculator               Gallery        Phone             Stk
Calendar                 Gallery3D      Protips           VoiceDialer
Camera                   HTMLViewer     Provision
hmtsay@ubuntu:~/android/packages$ ls providers/
ApplicationsProvider  DownloadProvider        MediaProvider
CalendarProvider      DrmProvider             TelephonyProvider
ContactsProvider      GoogleContactsProvider  UserDictionaryProvider

PS. out/: compile results
hmtsay@ubuntu:~/android/out$ ls
casecheck.txt  CaseCheck.txt  host  target  tmp  versions_checked.mk

PS. The system image of Android 2.2 Froyo is about 60MB. 
hmtsay@ubuntu:~/android/out/target/product/generic$ ls -l -a
total 60064
drwxr-xr-x  7 hmtsay hmtsay     4096 2010-10-04 07:52 .
drwxr-xr-x  3 hmtsay hmtsay     4096 2010-10-04 05:51 ..
-rw-r--r--  1 hmtsay hmtsay        7 2010-10-04 07:52 android-info.txt
-rw-r--r--  1 hmtsay hmtsay     1160 2010-10-04 05:51 clean_steps.mk
drwxr-xr-x  3 hmtsay hmtsay     4096 2010-10-04 06:20 data
-rw-r--r--  1 hmtsay hmtsay    16134 2010-10-04 07:52 installed-files.txt
drwxr-xr-x 13 hmtsay hmtsay     4096 2010-10-04 07:44 obj
-rw-r--r--  1 hmtsay hmtsay       50 2010-10-04 05:51 previous_build_config.mk
-rw-r--r--  1 hmtsay hmtsay   166468 2010-10-04 07:52 ramdisk.img
drwxr-xr-x  8 hmtsay hmtsay     4096 2010-10-04 06:21 root
drwxr-xr-x  4 hmtsay hmtsay     4096 2010-10-04 06:21 symbols
drwxr-xr-x 10 hmtsay hmtsay     4096 2010-10-04 07:35 system
-rw-------  1 hmtsay hmtsay 59794944 2010-10-04 07:52 system.img-rw-------  1 hmtsay hmtsay  1482624 2010-10-04 07:52 userdata.img

[Android] Development

1. Application (JAVA)


2. Target Porting
  *Target: Board
    -Linux Kernel Driver
    -Android Platform HAL Driver
  *Target: CPU
    -Architecture
    -Build Environment: Complier Flags, Linker Scripts, Prelinnk Map
    -C Library: Assembly, System Call stubs, Dynamic Linking Linker
    -ELF Processing Tool: Prelinker (apriori), Strip (soslim), ELF Section Processing (elfutils)
    -Dalvik Call Bridge: Assembly
    -Tool Chain

3. System Service
   -Bottom-up Design
   1) Add Native Library (C, C++)
   2) Add JAVA Classes in Platform Framework
   3) Connect JAVA Classes and Native Library via  JNI Interface
   4) Application calls new JAVA classes.

Android Eco-system

1. Software Developers  (Applications, Middlewares, Android Market)
2. Hardware Component Provider (ICs, Modules, Sensors)
3. Device Provider (Smart Phone, Tablet, Netbook, E-book, ...)
4. Infra-structure Provider (3G WCDMA, 3.5G HSDPA, USUPA , 4G LTE)
5. Service Provider
6. Users

Android Architecture

Android Architecture (including Patrick Brady's HAL)


Android_Platform

*4 Levels:
  1.Linux Kernel
    See Android Branch
  2. Runtmie Middleware
    1) Dalvik Virtual Machine for JAVA
      -Each application runs in its own porcess with its own instance of VM. Dalvik can handle multiple VMs. 
      -Register-based Architecture (True Java VMs are stack machines.): Less Instructions & Fast Execution
      -Dalvik Executable (.dex) format, Optimizations for memory, not Java bytecode
      - Compilation: .java --> .class --> .dex      -Android 2.2, Dalvik has a just-in-time compiler to speed up execution of bytecode
by compiling some or all of it to native machine code.
     2) Core Libraries (by JAVA) 
     -JAVA APIs: Data Structures, Utilities, File Access, Network Access, Graphics, ... (e.g., new)  
     3) Native Libraries (by C/C++)
       3.1 Bionic libc
          -Google customized C compiler for Android
            *License Issue: Bionic is BSD license to keep GPL out of user-space.
            *Small Size: about 200K, half  of glibc (GNU libc)
            *Fast Speed: small size & fast speed of pthread(POSIX Threads) implementation
            *Built-in support for important Android-specific services such as system properties and logging.
            *Not fully compatible to glib: not support all POSIX features not needed on Android.
[]http://discuz-android.blogspot.com/2008/10/google-android-native-libc-bionic.html
      2.2 Media Framework
         - Based on PacketVideo OpenCore
         -Support: standard video, audio, still-frame formats
         -Support: hardware/software codec plug-ins

ü      3.3 WebKit  web browser
         -Open source
         -Render pages in full (desktop) view
         -Support Full CSS, JavaScript, DOM, AJAX 
         -Support single-column and adaptive view rendering

      3.4 SQLite
ü         -Light-weight relational database management system
         -Popular in many systems         
ü

       3.5 Surface Manager (Surface Flinger)
         -Combine 2D and 3D surfaces from multiple applications to frame buffer device
ü
       -Surfaces passed as buffers via Binder IPC calls
       -Can use OpenGL ES and 2D hardware accelerator for its compositions
       -Double-buffering using page-flip
      3.6 Audio Manager (Audio Flinger): Processing multiple audio streams into PCM audio out paths
      3.7 SGL: 2D graphics engine
    3) HAL (Hardware Abstraction Layer)
      *In user space, Define the standardized interface for hardware drivers
      *Separate Android Platform from Linux Kernel
      *HW Drivers under HAL are not GPL-licensed and not required to be released

  3.Application Framework
    *Define component APIs used by the core applications
    *Developers reuse/replace the same components easily.   

ü
 
Feature
Role
View
System
Components to build appplications (e.g., lists, grids, text boxes, buttons, and embedded web browser)
Content
Provider
Access data from other applications or to share their own data
Resource
Manager
Provide access to non-code resources (e.g., localized string, graphics, and layout files)
Notification Manager
Display customer alerts in the status bar
Activity
Manager
Manage the lifecycle of (window) applications and provide a common navigation backstack
  4. Applications
  -Programming by JAVA
  -Core Applications: üEmail Client, SMS Program, Calendar, Maps, Browser, üContacts

  PS. 2& 3 are middlewares
  Note. Kernel Layer are GPL license; Other layers are Apache License.

Android Products:
 *SmartPhone, SmartBook (netbook), Tablet, Set-top Box, E-book, ...
      3.8 3D Libraries
ü          -Based on OpenGL ES 1.0 APIs
ü          -Can use H/W 3D acceleration oroptimized 3D S/W rasterizer
      3.9 FreeType: Rendering bitmap and vector font

Android Features

Android Features
1. Open (Source code)
2. Free Services
3. Cloud Computing/Storing
4. Support multiple hardware platform based on Linux Kernel
5. Adpots Standardized Technologies
6. Ready SDK, Tools, & Document

and refer to
1. http://en.wikipedia.org/wiki/Android_(operating_system)
2. http://developer.android.com/guide/basics/what-is-android.html

2010年10月4日星期一

Android Porting: Make Platform

1. Setup VMware Player
2. Install ubuntu for Linux
3. Add Packages for Getting and Building Source Code
  $sudo apt-get install git-core gnupg flex bison gperf libsdl-dev libesd0-dev libwxgtk2.6-dev build-essential zip curl libncurses5-dev zliblg-dev
  Issue 1: Add JAVA 5/6 package
  Issue 2. Add repo
    <Step 1>curl http://android.git.kernel.org/repo >~/bin/repo
    <Step 2>chmod a+x ~/bin/repo
    <Step 3> export PATH=~/bin/repo:$PATH
3. Get Android Code Bases
  3.1 Get Android platform source code
  <Step 1> repo init –u git://android.git.kernel.org/platform/manifest.git –b android-2.2_r1
  <Step 2> repo sync


4. Build
   $make

   Issue 1: Build Android 2.2 froyo only in 64bit environment.
   Issue 2:  Build Android 2.2 froyo only by JDK6


[] Android Platform Development Kit: http://www.kandroid.org/android_pdk/index.html
  *Device Requirements:  http://www.kandroid.org/android_pdk/system_requirements.html