Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Adapting Android Accessory to any ARM Platform

jdannenbring
September 18, 2012

Adapting Android Accessory to any ARM Platform

Description of how android accessory software was ported to a new ARM-based platform, and how to use the example code to do this for any USB-Host capable platform. Example code: https://github.com/jdannenbring/android-arm-accessory

jdannenbring

September 18, 2012
Tweet

More Decks by jdannenbring

Other Decks in Programming

Transcript

  1. 8/24/2012 1 Session Overview • Introducing Android Open Accessory •

    Technical Demonstration • Porting the ADK Software to ARM-based HW for Demonstration • Summary and Questions
  2. 8/24/2012 2 Introducing Android Open Accessory What is Android Open

    Accessory? Android Accessory USB Android Device USB Connection USB Device USB Host ADK Hardware
  3. 8/24/2012 3 Android Accessory HW • Hardware for ADK 2011

    is based on a Arduino Mega2560 • Hardware for the ADK 2012 is based on an ARM Cortex M3 • ADK 2012 Guide What is Android Open Accessory? Android Accessory USB Android Device (1) Software on the ADK HW (2) Software on the Android device
  4. 8/24/2012 4 Android Accessory SW 1.Software on the ADK HW

    • Handles communication with Android device • Controls sensors, displays, etc. on the ADK HW 2.Software on the Android device • Can be installed automatically via pop-up URL on connection • Handles communication with ADK HW • Presents a GUI for control, data input/output from ADK AOA 1.0 Protocol • Android Open Accessory 1.0 is a protocol that allows an Android device to interact with an Android USB accessory in a special accessory mode. • Basically there are four steps to the protocol: 1. Wait for and detect connected devices 2. Determine the device’s accessory mode support 3. Attempt to start the device in accessory mode 4. Establish communications
  5. 8/24/2012 5 AOA 1.0 Protocol Android Accessory USB Android Device

    (1) Wait for and detect connected devices USB Connection AOA 1.0 Protocol Android Accessory USB Android Device (2) Determine the device’s accessory mode support Vendor ID Product ID
  6. 8/24/2012 6 AOA 1.0 Protocol Android Accessory USB Android Device

    (3) Attempt to start the device in accessory mode Get Protocol #51 AOA 1.0 AOA 1.0 Protocol Android Accessory USB Android Device (3) Attempt to start the device in accessory mode Identity Strings #52
  7. 8/24/2012 7 AOA 1.0 Protocol Android Accessory USB Android Device

    (3) Attempt to start the device in accessory mode Start Accessory #53 AOA 1.0 Protocol Android Accessory USB Android Device (4) Establish Communications CMD / RESPONSE
  8. 8/24/2012 8 AOA 1.0 Protocol 1.Wait for and detect connected

    devices • This one is self-explanatory. AOA 1.0 Protocol 2. Determine the device’s accessory mode support • The accessory must check the Vendor & Product ID’s of the connected device • Product ID Purpose • 0x2D00 – Supports Accessory Mode • 0x2D01 – Supports Accessory Mode + ADB • If it is already in accessory mode then: • Vendor ID = 0x18D1 (Google) • Product ID = 0x2D00 | | 0x2D01 • If it is not in accessory mode then proceed to step 3
  9. 8/24/2012 9 AOA 1.0 Protocol 3. Attempt to start the

    device in accessory mode • Send a “Get Protocol” request (51) on endpoint 0 • Version 1 or 2 is returned by Android device • If the Version # is okay, then send string’s identifying our ADK to the device • Send “Identity” request (52) for each identifier: • Manufacturer • Model Name • Description • Version • URL • Serial Number • Send “Start Accessory” request (53) to request the Android device re-introduce itself on the bus in accessory mode AOA 1.0 Protocol 4.Establish communications • The accessory must obtain the Bulk endpoints and be prepared to initiate communication with the device. From this point the communication is defined by the ADK Developer
  10. 8/24/2012 10 AOA Protocol version 2.0 • AOA 2.0 was

    released at Google I/O end of June 2012 • AOA 2.0 adds to the original AOA protocol by adding an optional USB audio interface • New Product ID’s • 0x2D02 – Supports Audio • 0x2D03 – Supports Audio + ADB • 0x2D04 – Supports AOA 1.0 + Audio • 0x2D05 – Supports AOA 1.0 + Audio + ADB AOA Protocol version 2.0 • If the Accessory supports Audio streaming an “Audio Support” request (58) must be sent after sending “Identity” • Currently AOA 2.0 only supports 2 Channel, 16-bit PCM @ 44100 KHz
  11. 8/24/2012 11 AOA Protocol version 2.0 • AOA 2.0 also

    adds support for registering HID device with the Android device • Compatible with original AOA 1.0 protocol • Also adds Bluetooth support • Very neat! But not the focus of this presentation… What part of AOA do you Control? • ADK SW • Developer controls what runs on the ADK HW and what runs on the Android device • ADK HW? • Yes! Just requires a USB Host capable device, that implements the ADK Protocol • This is what makes it easy to port the ADK SW to any ARM-based board
  12. 8/24/2012 12 Wait, why do I need AOA then? •

    Couldn’t I just use my own protocol to communicate with Android devices? • Technically yes, but it won’t work for ALL devices running Android 2.3.4 or later! • AOA gives a standard for communication with all the latest Android devices • Recent numbers from Google claim ~900,000 Android Devices activated each day (circa June 2012) • AOA 2.0 on Android Jellybean+ includes features that interact directly with the Android OS Technical Demonstration
  13. 8/24/2012 13 Demonstration Goal • Create an accessory device that:

    • Displays pictures • Allows control of the slideshow from Android • Functions as an Audio dock for any Android Device running Jellybean+ Technical Demonstration • Android Device: • Stock Nexus 7 Tablet running Android Jellybean • Custom ARM-based accessory • iMX6Q ARM Cortex-A9 based board • Running Android ICS
  14. 8/24/2012 14 Technical Demonstration • Why were these particular platforms

    chosen? • Android makes display of pictures and playback of audio simple • Wanted to show ease of porting libusb-based code • Nexus 7 was recent stock device running Jellybean (no modification needed) • The Custom Accessory HW could have run libusb on Linux easily Technical Demo Implementation • The reference software for ADK 2012 from Google is available for download • Important Software Components • Application on the Android device • Android Application presents UI for control, and communication • Software on the Accessory HW • High level communication via AOA Protocol • Audio Streaming, Control of Slideshow
  15. 8/24/2012 15 Application on the Android Device • Must discover

    the accessory when it is attached • Communicate with the accessory via ADK Developer defined commands over USB • Utilizes the USB API in Android Discovering the Accessory • First add an intent-filter to the application’s manifest (AndroidManifest.xml)
  16. 8/24/2012 16 Discovering the Accessory • Then define a resource

    file that details which USB Accessory this Application communicates with (accessory_filter.xml) Communication with the Accessory • Grab the UsbAccessory handle from the Intent received • Open up input/output file streams with the Accessory (myactivity.java)
  17. 8/24/2012 17 Software on the Accessory HW • Software Architecture

    • Utilized Java when Android API’s needed • Android’s AudioTrack API was ideal for sending raw isochronous usb data stream directly to audio hardware • Utilized NDK when libusb API’s needed • Ported Libusb libraries to Android Threads of Execution ADKPictureFrame (Activity) ADKUSBService (Service) USB Attached Event PID / VID Match Accessory Mode? checkAccessorySupport() No connectToAccessory() Yes AOA Step 1 StartService(Intent) Handle GUI-related Events Audio Write() new Thread setupISO() Registers Isochronous USB Transfer Callback monitorCommands() AOA Step 2 & 3 Filled = Native Code Open = Java Code Key
  18. 8/24/2012 18 Audio Data Flow Circular Buffer Isochronous USB Transfer

    Callback Audio write() Thread Audio Track API Audio Hardware jni/adk-service.c • AOA Protocol handled via • checkAccessorySupport() • connectToAccessory() • Receiving audio stream data via Isochronous Endpoint • setupISO() • audio_callback() • monitorCommands() • Writing received audio stream data to Audio HW • write()
  19. 8/24/2012 19 Porting Libusb to Android • Porting libusb to

    Android was necessary to give full access to USB Host functions • Android currently does not support Isochronous Endpoints! • Isochronous endpoint communication is required for accessing the Audio stream provided by an Android Jellybean device Porting Libusb to Android • Libusb is abstracted to allow it to be easily ported to other platforms • Just had to create a Android.mk file • An example of this had already been done • Placed in /external/libusb-android • Added libusb to PRODUCT_PACKAGES in device.mk file • Declared libusb package as optional • LOCAL_MODULE_TAGS := optional • mmm external/libusb-android • Library is compiled and installed at: out/target/product/imx6q_sabrelite/system/lib/libusb.so
  20. 8/24/2012 20 Porting Libusb continued • lsusb is also a

    handy tool when working with USB device descriptors • Placed in /external/lsusb • Created a Android.mk file for lsusb • Added lsusb to PRODUCT_PACKAGES in device.mk file • Declared lsusb package as optional • LOCAL_MODULE_TAGS := optional • mmm external/lsusb • Binary is compiled and installed at: out/target/product/imx6q_sabrelite/system/bin/lsusb Porting Libusb continued • Utilized NDK to call native code directly via JNI • Libusb headers and library referenced in jni/Android.mk file
  21. 8/24/2012 21 jni/Android.mk … • LOCAL_LDLIBS += -L…/product/imx6q_sabrelite/system/lib/ • LOCAL_LDLIBS

    += -lusb –llog • LOCAL_C_INCLUDES += …/external/libusb-android/libusb/ … Summary: Porting the ADK SW
  22. 8/24/2012 22 How was this possible? • The AOA communication

    protocol is defined on TOP of the USB communications • The AOA protocol is open and well- documented by google • The underlying HW used by the ADK kits are merely an example of what you can do with the communication you send/receive with the Android device What did the example achieve? • Implemented the basic AOA 1.0 protocol • Implemented Audio Streaming via the AOA 2.0 protocol • Created a (believable?) demo of a device that acts as Picture Frame + Audio Dock • Full code sources are available at: https://github.com/jdannenbring/android-arm-accessory https://github.com/jdannenbring/android-arm-accessory-app
  23. 8/24/2012 23 References • Di Cerbo, Manuel. “Turn your Linux

    computer into a huge Android USB Accessory” Using Android in Industrial Automation. 5-13-2011. http://android.serverbox.ch/?p=262 • “libusb”. http://www.libusb.org/. 1 June 2012. • “Android Developers”. http://developer.android.com/index.html. 1 June 2012. • “monaka / libusb-android / overview”. https://bitbucket.org/monaka/libusb-android/. 1 June 2012. Portions of these slides are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License. Questions and Feedback • Any Questions? • Email: [email protected] • For more details read the corresponding whitepaper: • AD-2002Paper_Dannenbring.pdf • Evaluations will be sent to each attendee via email. Please take time to fill these out!