Wednesday, June 25, 2014

Short travel to Baltimore

趁着QQ去Baltimore开会的机会去感受一下这个马里兰州最大的城市。巴尔的摩是大西洋沿岸重要的海港城市,有着得天独厚的海运条件,紧邻的切萨皮克湾很宽广,而且航道很深,万吨级的远洋轮可直接驶入巴尔的摩港区,它是美国五大湖去,中央盆地与大西洋上联系的一个重要的出海口。在这个工业港口城市里,黑人约占55%,这里既有1812年间美国独立战争时的遗迹,也有内战期间亲南民众与政府军之间的弹火冲突。有意思的是这里是美国国歌的诞生地,1904年的巴尔的摩大火催生了国家标准与技术院(NIST)。
周六上午在费城30街Amtrak火车站搭乘驶向Baltimore的火车。车厢内有免费的Wifi,到Baltimore大概一个小时的路程。刚下火车感觉破破烂烂的,有点费城的味道。出了火车站,我们坐taxi来到之前预订的QualityInn,进门之后有股阴暗发霉的味道,房间里也很阴暗。不过酒店离QQ开会的地方还是很近的,遂我们决定先去那边转转,顺道搞些吃的。我们沿着接到走了10分钟左右就到了Baltimore的商业观光区Inner Harbor。这里是由之前的工业区和居民区转变成的观光旅游去。内港上有各式各样的油轮,皮挺和脚踏船,还有各式各样的餐厅。来到港口城市当然不能少了海鲜,正赶上这里的crab & beer festival,一家历史悠久的海鲜餐厅Phillips就坐落在这里。正好遇到QQ的lab mates,我们4个人就坐下开始享受海鲜大烩了。我们点了著名的blue crab和其他贝壳累的海鲜。抱参之后我们就去看之前查过的出租自行车的店了,这个时候我的肚子又不争气了,回来的途中拉了一路。。
由于受不了QualityInn阴暗的环境,我们第二天又搬进了附近的一家叫作brookshire suits的酒店,这次环境好多了,而且离开会的地方又尽了些。我拿出跑鞋开始了在Baltimore的第一次慢跑,一路上随手拍了一些Inner Harbor的景色。













What you 'can' and 'cannot' do with Tizen SDK for Wearable

I was trying to using Samsung galaxy gear 2 for Bluetooth device discovering and audio recording. However, the SDK wouldn't let me create a native project. So I figured out I could using the Web App API to control the Bluetooth and microphone components of the watch. I was wrong, these features are not supported yet according to the release notes of the SDK.

The Bluetooth

They mentioned in the SDK version 1.0.0b1:
Device APIs to access to a device’s platform capabilities support
  • Alarm, Application, Bluetooth, Filesystem, System Information, Power, Motion(currently pedometer supported), SAP(communication between host and wearable device) API
But in the SDK version 1.0.0b2, they stated:
The Bluethooth API , which was incorrectly listed in the supported API lists in the 1.0.0b1 release note, is now removed in the list

The Microphone/Audio Input

It's not clear whether audio recording is supported or not now. In their release note 1.0.0b1 they stated that the SDK support webkit framework and HTML5 audio/video element. And in the release note 1.0.0b2, they also stated in the fixed bugs section:
  • Audio recording support with Camera API.
    • “audio:true” of MediaStreamOptions is now supported in getUserMedia() API
    • Audio recording is supported with Camera API by passing audio-only MediaStream object to createCameraControl() API
    • Supported audio recording format : AMR and 3GP
In a word, for Tizen SDK for wearable, control of Bluetooth is not supported yet and audio may be supported by using HTML 5 element (here is the link of how to capture audio/video in HTML 5: http://www.html5rocks.com/en/tutorials/getusermedia/intro/).

Sunday, June 8, 2014

Make USB OTG work on Nexus 4

It's not a trivial stuff to make thumb USB drive / speaker / keyboard work on Nexus 4 since USB host / OTG is not supported by Nexus 4. It needs hacking from both hardware and software aspects to make it work.

Hardware

In order to use USB OTG devices on Nexus 4, you have to use a special USB OTG Y-Cable like this one:
 
Because Nexus 4 Micro-USB host doesn't have power supply to the USB devices, so we need to use external power supply. We bought an 3000mah power bank like this one:
 

 That's it. Next we need to take care of the Android system.

Software / Android ROM

The built-in kernel of Android system doesn't have USB-OTG support. Yes, you are right, I don't understand why they cut off such useful feature, either. Maybe for power saving, lower cost? Anyway, it's not a problem for the hackers!

WARN: The following operations needs some basic hacker skills such as flashing a now system ROM, use of adb and fastboot. Even you do have that kind of skills, you may also turn your Android to a brick. So read carefully before you start hacking.

  1. Install the recovery image
    The recovery image is used to take care of the operations such as wiping the data partition, install a new ROM/zip file when your Android phone is in recovery mode. I followed some tutorials online, then I decide I'll use the CWM recovery. You may also try TWRP recovery which is also frequently used.
    Download recovery image for your device: http://clockworkmod.com/rommanager
    Open a terminal, reboot the device into fastboot mode by typing
    adb reboot-bootloader 
    Or power off your device then press volume up, volume down and power simultaneously.
    Then flash recovery image onto your device by using the following command:
    fastboot flash recovery recovery.img

  2. Install Cyanogen system from recovey
    In order to get a rooted system (which is not necessary in my case), we downloaded and installed Cyanogenmod system: http://download.cyanogenmod.org/?device=mako&type=stable. At first I download newest/nightly ROM, but it doesn't let me using the WiFi after I flashed the ziddey-OTG kernel. Then I just use the stable Cyanogenmod version: cm-10.2.1-mako.zip (Android 4.3.1). Then use the recovery to install the new ROM.
    Reboot to recovery mode. Then select wipe data/factory reset. Then you can install zip from sdcard or from sideloading. You just keep the system image on your PC and issue the following command after you select install zip from sideload:
    adb sideload cm-10.2.1-mako.zip
    

    Then you'll have a Cyanogenmod system.
  3. Install ziddey USB OTG kernel
    ziddey-OTG kernel is a patch to the Franco-CM system to enable kernel OTG support. You can download the right version on this page: http://forum.xda-developers.com/nexus-4/orig-development/usb-otg-externally-powered-usb-otg-t2181820. And use adb sideload install the zip file.
  4. Try OTG!

Show Cases

Now it's time to show off your OTG devices!  First I use thumb USB drive on my Nexus 4.
Displaying photo.JPG
After plug in, you can check your storage from Settings->Storage->USB Storage.
The main reason I want to use OTG feature of Nexus 4 is for my fingerprint reader project. We have U.ARE.U Fingerprint Development Kit and need to use digitalPersona 5160 fingerprint reader on Nexus 4. Here is the screenshot showing it works on my Nexue 4:


Debugging with ADB from Wireless Connection

Want to debug the USB device using gdb but the micro-USB I/F has already been occupied? You can use the wireless network to make the connection from your PC to Nexus 4. First you need to turn adb to tcpip mode instead of using USB. Connect your phone to PC using USB cable, issue 'adb usb' to set adb running in USB mode first. Then 'adb tcpip 5555' to setup adb in internet mode. Then check the IP address of your android device on: Settings->About phone->Status->IP address. Connect adb host to device 'adb connect IP_addr'. If succeed, you'll see
$ adb devices
List of devices attached 
#.#.#.#:5555 devic
Details about adb wireless usage, please refer to: http://developer.android.com/tools/help/adb.html

Acknowledgement

Cyanogen Team:
[1] Install recovery image and Cyanogen system, http://wiki.cyanogenmod.org/w/Install_CM_for_mako
[2] Cyanogenmod ROM: http://download.cyanogenmod.org/?device=mako&type=stable

AndroidCentral
[1] USB OTG on Nexus 4: http://www.androidcentral.com/android-advanced-usb-otg-nexus-4
[2] Nexus 4 Unlock & Root: http://forums.androidcentral.com/nexus-4-rooting-roms-hacks/224861-guide-nexus-4-unlock-root.html

XDA Developers
[1] Externally powered USB OTG - Nexus 4: http://forum.xda-developers.com/nexus-4/orig-development/usb-otg-externally-powered-usb-otg-t2181820

Thursday, June 5, 2014

Build Android 4.2 AOSP project for Nexus 4


Make Your OWN Android AOSP System for Nexus 4



Build and Install AOSP JellyBeans Platform for Nexus 4 (Code named Mako)

Before start, make sure your already have a 64-bit linux system used for development! By saying 64-bit, I'm not kidding, because I've tried using my 32-bit poor Centos and failed with endless problems. Finally I found out only 64-bit development systems are supported by JellyBeans now! So get yourself a 64-bit linux system, or if you're a guy who likes hacking around and get the luck to successfully build a JB platform on 32-bit system, please let me know and share the happiness with you!
And please read through the standard android document from AOSP if you've never built an Android image before.
Guess you are out of patience to get your hands dirty to setup your nexus 4 running your own system. OK. let's start by downloading the latestest android system which integrated mako target. Your can find all the Android platforms in AOSP here The one I use is android-4.2.2_r1, which is the latest version when writing this article. I assume you've known how to download the source from AOSP. Otherwise, read the AOSP document Downloading the Source Tree.
Besides the platform source code, we need the driver binaries in order to run system on real devices. Official binaries can be downloaded from Google's Nexus driver page. Download and extract the vendor drivers of "Nexus 4 binaries for Android 4.2.2 (JDQ39)" on that page. Details about how to extract the proprietary binary drivers refer to the Obtaining proprietary binaries part. You'd better do this before build the platform, or you have to make clober and rebuild the whole system, which takes lots of time!
It's time to give birth to the new image now!
$ . build/envsetup.sh
$ lunch full_mako-eng
$ make -j[N]
Now we have our own system image at .../out/target/product/mako. We'll use fastboot to flash the system image. This requires a unlocked bootloader. The default bootloader is locked, but you can turn it to unlocked in the fastboot mode(press and hold both Volumn Up and Power or adb reboot-bootloader) by
fastboot oem unlock

Flash all in one single command: this writes boot, recovery and system images to corresponding partitions together. "-w" option can wipe cache partition and data.
cd out/target/product/mako 
fastboot -w flashall

Build Linux Kernel and Create Boot Image (Kernel + Init Ramdisk)

The kernel sources support nuxus 4 (mako) are in the Qualcomm MSM project, the latest kernel source can be downloaded:
git clone https://android.googlesource.com/kernel/msm -b android-msm-mako-3.4-jb-mr1.1
Now config and build the kernel: the built kernel will be output at arch/arm/boot/zImage
export CROSS_COMPILE=arm-linux-androideabi-
export ARCH=arm
make mako_defconfig
make -j[N]
You'll probably have an compile error if you following the instruction to this point.
drivers/gpu/msm/adreno.c:433:1: warning: the frame size of 1032 bytes is larger than 1024 bytes [-Wframe-larger-than=]
error, forbidden warning: adreno.c:433
make[3]: *** [drivers/gpu/msm/adreno.o] Error 1
This is caused by warn config stack frame size in the kernel. Therefore you need to make menuconfig and reconfig the CONFIG_FRAME_WARN value which located at Kernel hacking->Warn for stack frames larger than (needs gcc 4.4).
The tricky part is to make boot image using the kernel image built previously. Actually, the boot image consists of kernel image and init ramdisk image. Yes, we also need the ramdisk image so as to make the boot image. The approach is to extract the ramdisk image and kernel configuration file from the official boot.img in Google's factory image for nexus 4. This can be done by using abootimg tool. Some articles recommend "split_bootimg.pl", but doesn't work for nexus 4 or for me.
To extract kernel config file, kernel image and ramdisk image from official boot image: the outputs are bootimg.cfg zImage initrd.img respectively.
abootimg -x boot.img
Remove the bootsize property in bootimg.cfg as our kernel image can be larger than the one from the factory image. Now we can create our boot image by:
abootimg --create myboot.img -f boot.cfg -k [path-to-your-zImage] -r initrd.img
Verify the boot image without flashing it:
fastboot boot myboot.img
You can root Android by modify the official ramdisk image. First, un-gzip and un-cpio the official ramdisk image extracted from boot.img.
mkdir ramdisk && cd ramdisk
gunzip -c ../initrd.img | cpio -i
Edit default.prop file in the ramdisk, set "ro.secure=1" to "ro.secure=0".
Repack the ramdisk:
find . | cpio -o -H newc | gzip > ../myramdisk.gz
Now create the insecure boot image with this ramdisk image, which allows you to login as root.
abootimg --create myboot-rooted.img -f boot.cfg -k [path-to-your-zImage] -r myramdisk.gz
Flash your rooted boot image by
fastboot flash boot myboot-rooted.img

Check your kernel version when your Android system started!

References:

  • http://nosemaj.org/howto-build-android-nexus-4
  • http://forum.xda-developers.com/showthread.php?t=2131953