PLEASE NOTE: BY USING THIS RELEASE YOU ACCEPT THAT IT IS SUBJECT TO THE PRODUCT KIT LICENSE AGREEMENT THAT YOU HAVE ACCEPTED ELECTRONICALLY, A COPY OF WHICH IS AVAILABLE AT https://createpoint.qti.qualcomm.com/dashboard/pkla/license. 

# Qualcomm Smart Audio Platform application for Android
This application, also referred as "QSAP application", allows users to interact with QCS40X devices over WiFi.

The application is supplied as a source code bundle. The IoTivity open source project must be built first. This README.md file exposes the steps to follow in order to successfully build the example application.

:warning: The IoTivity open source project for Android only supports Unix systems for its building process.
OSX is supported but these instructions will only be detailed for Linux systems. As an example the commands for Ubuntu are provided.


## Overview
This is the main folders the project should contain once built:
```
android_qsap                      # also referred as "PROJECT_ROOT" within these instructions
├── IoTControllerSDK              # The controller SDK
    ├── allplay-swaggers          # Swagger json files for Allplay
    ├── ControllerSDK             # Android IoTController SDK for the Smart Audio application     
    ├── iotivity                  # IoTivity open source project
    ├── iotsys-swaggers           # Swagger json files for Iotsys  
    └── swagger2java              # swagger2java Python files
        ├── out                   # Autogenerated Java code
        └── ...
├── patch                         # Patches to build the controller
└── SmartAudio                    # The Qualcomm Smart Audio Platform application for Android
    ├── app
    ├── build.gradle
    └── ...
```

The `iotivity` and `out` folders will be added and/or generated by following the building steps from this README file.

From the provided source code to running the application on an Android device, the process is as follows:
1. Build the IoTivity library, see [Build IoTivity](#build-iotivity-open-source-library-for-android).
2. Generate the code from the IoTivity API definitions, see [Generate the code](#generate-the-autogenerated-code).
3. Run the Android application using Android Studio, see [Run the project](#run-the-project-using-android-studio).


## Build IoTivity open source library for Android
For more information please refer to the Android building instructions from the [IoTivity wiki](https://wiki.iotivity.org/android_build_instructions).

### Requirements
* Git
    ```
    $ sudo apt install git
    ```
* Python v2.7
    ```
    $ sudo apt install python
    $ sudo apt install virtualenv
    ```
* Open JDK v1.7 or later
    ```
    $ sudo apt install openjdk-8-jdk
    $ export JAVA_HOME=/usr/lib/jvm/default-java/bin 	# check this is the right path
    $ export PATH=$PATH:$JAVA_HOME
    ```
* Scons v3.0.5
    ```
    $ virtualenv -p python2.7 venv27
	$ . venv27/bin/activate
	(venv27) $ pip install -U pip wheel setuptools 		# get latest versions for the virtual environment
	(venv27) $ pip install scons==3.0.5					# installing scons v3.0.5
	(venv27) $ deactivate
    ```
* cc compiler
    ```
    $ sudo apt-get install build-essential
    ```
* If running on a 64 bits Linux, install the 32 bits version of std:
    ```
    $ sudo apt-get install lib32stdc++6 lib32z1
    ```

### Step-by-step guide

1. Get IoTivity
    ```
    $ cd {PROJECT_ROOT}/IoTControllerSDK
    $ git clone https://gerrit.iotivity.org/gerrit/iotivity.git iotivity --branch 1.3.1 --recurse-submodules -j3
    ```
2. Apply the provided patch
    ```
    $ cd iotivity
    $ git apply ../patch/resource.patch
    ```
3. Fetch the cryptographic software for iotivity
    ```
    ($ cd iotivity            # already positioned there after executing step 2.)
    $ git clone https://github.com/ARMmbed/mbedtls.git extlibs/mbedtls/mbedtls -b mbedtls-2.4.2
    $ git clone https://github.com/01org/tinycbor.git extlibs/tinycbor/tinycbor -b v0.4.1
    ```
    > A FORKED version of libCoap is contained in the IoTivity open source project to avoid some building issues.
4. Build the IoTivity library
    ```
    ($ cd iotivity            # already positioned there after executing step 2.)
	$ . venv27/bin/activate
    (venv27) $ scons VERBOSE=1 RELEASE=true LOGGING=false TARGET_OS=android TARGET_TRANSPORT=IP TARGET_ARCH=armeabi-v7a
	(venv27) $ deactivate
    ```
    The SConscript files downloads and installs/unarchives all other dependencies with their specific version such as the Android NDK, the SDK, Gradle, Boost, etc.
    However the Android SDK Manager requires the user to interact with its user interface, please follow the instructions as displayed in the terminal:
    ```
    ***************************************** Info ********************************
    *   Either 'Android API 21' is not installed or 'Android SDK Build Tools      *
    *   20.0.0' is not installed. The Android SDK Manager will now open. Please   *
    *   be sure to deselect all options, then select the following 2 packages:    *
    *       1. Under "Tools" select "Android SDK Build-tools" Revision 20.        *
    *       2. Under "Android 5.0.1 (API 21)" select "SDK Platform"               *
    *       3. Continue by selecting "Install 2 Packages"                         *
    [...]
    ...Opening Android SDK Manager now. Once you are finished, the build will continue.
    ```


## Generate the Java code
### Requirements
* Python v3
    ```
    $ sudo apt install python3
    ```

### Step-By-Step guide
1. Navigate to the swagger2java folder
    ```
    $ cd {PROJECT_ROOT}/IoTControllerSDK/swagger2java
    ```
2. Prepare the Python virtual environment
    ```
    $ virtualenv -p python3 venv3
    $ source venv3/bin/activate
	(venv3) $ pip install -U pip wheel setuptools 		# get latest versions for the virtual environment
    (venv3) $ pip install -r requirements.txt			# install the requirements to generate the code
    ```
3. Generate the Java code
    ```
    (venv3) $ python main.py --schema_dir ../allplay-swaggers --schema_dir ../iotsys-swaggers --out_dir out --template_dir java-templates/
    ```
4. Exit the virtual environment
    ```
    (venv3) $ deactivate
    ```


## Run the project using Android Studio

### Requirements
* Android Studio for Linux v3.4.1 - Build #AI-183.6156.11.34.5522156
* Android SDK packages
    1. Start Android Studio IDE and start the SDK Manager.
    2. Select the following packages:
        * SDK Platforms: "Android 9.0 (Pie)" > "Android SDK Platform 28"
        * SDK Tools: "Android SDK Build-Tools 29" > "28.0.3", "CMake", "Android SDK Platform-Tools", "Android SDK Tools", "NDK".
    3. Select "Apply" and their download starts.

### Step-By-Step guide
1. Start Android Studio.
2. Import the Smart Audio project in Android Studio:
    1. If a project is already opened, click on "File" > "Open...", otherwise click on "Import project (Gradle, Eclipse ADT, etc.)
    2. Select the root of the "Smart Audio" project, it is the folder which contains the "app" module and the "build.gradle" file. __Note: it should be detected as a Gradle project.__
3. Android Studio imports the project, automatically resolves the dependencies and builds the project.
4. Once the project has been imported, click on Run to launch the application on a connected Android device or an emulator.