qahw: test: Add support to test aptx decoder

Add changes to test aptx decoder using playback test app.

CRs-Fixed: 1081424
Change-Id: Ie7913d5a0b0091ce50d9066b7d85100e7f884f83
diff --git a/qahw_api/inc/qahw_defs.h b/qahw_api/inc/qahw_defs.h
index 8b890b9..eb500a2 100644
--- a/qahw_api/inc/qahw_defs.h
+++ b/qahw_api/inc/qahw_defs.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright (C) 2011 The Android Open Source Project *
@@ -233,14 +233,26 @@
     uint16_t  gain_step;
 };
 
+struct aptx_dec_bt_addr {
+    uint32_t nap;
+    uint32_t uap;
+    uint32_t lap;
+};
+
+struct qahw_aptx_dec_param {
+   struct aptx_dec_bt_addr bt_addr;
+};
+
 typedef union {
     struct qahw_source_tracking_param st_params;
     struct qahw_sound_focus_param sf_params;
+    struct qahw_aptx_dec_param aptx_params;
 } qahw_param_payload;
 
 typedef enum {
     QAHW_PARAM_SOURCE_TRACK,
-    QAHW_PARAM_SOUND_FOCUS
+    QAHW_PARAM_SOUND_FOCUS,
+    QAHW_PARAM_APTX_DEC
 } qahw_param_id;
 
 
diff --git a/qahw_api/test/qahw_playback_test.c b/qahw_api/test/qahw_playback_test.c
index 900f189..5ff1f9a 100644
--- a/qahw_api/test/qahw_playback_test.c
+++ b/qahw_api/test/qahw_playback_test.c
@@ -102,7 +102,8 @@
     FILE_EAC3,
     FILE_EAC3_JOC,
     FILE_DTS,
-    FILE_MP2
+    FILE_MP2,
+    FILE_APTX
 };
 
 typedef enum {
@@ -552,6 +553,28 @@
     return rc;
 }
 
+void parse_aptx_dec_bt_addr(char *value, struct qahw_aptx_dec_param *aptx_cfg)
+{
+    int ba[6];
+    char *str, *tok;
+    uint32_t addr[3];
+    int i = 0;
+
+    tok = strtok_r(value, ":", &str);
+    while (tok != NULL) {
+        ba[i] = strtol(tok, NULL, 16);
+        i++;
+        tok = strtok_r(NULL, ":", &str);
+    }
+    addr[0] = (ba[0] << 8) | ba[1];
+    addr[1] = ba[2];
+    addr[2] = (ba[3] << 16) | (ba[4] << 8) | ba[5];
+
+    aptx_cfg->bt_addr.nap = addr[0];
+    aptx_cfg->bt_addr.uap = addr[1];
+    aptx_cfg->bt_addr.lap = addr[2];
+}
+
 void usage() {
     printf(" \n Command \n");
     printf(" \n hal_play_test -f file-path <options>   - Plays audio file from the path provided\n");
@@ -583,6 +606,7 @@
     printf("                                             file path and other file specific options would be ignored in this mode.\n\n");
     printf(" -e  --effect-type <effect type>           - Effect used for test\n");
     printf("                                             0:bassboost 1:virtualizer 2:equalizer 3:visualizer(NA) 4:reverb 5:audiosphere others:null");
+    printf(" -A  --bt-addr <bt device addr>            - Required to set bt device adress for aptx decoder\n");
     printf(" \n Examples \n");
     printf(" hal_play_test -f /etc/Anukoledenadu.wav     -> plays Wav stream with default params\n\n");
     printf(" hal_play_test -f /etc/MateRani.mp3 -t 2 -d 2 -v 0.01 -r 44100 -c 2 \n");
@@ -621,6 +645,10 @@
     printf("                                          -> kvpair(-k) values represent media-info of clip & values should be in below mentioned sequence\n");
     printf("                                          ->avg_bit_rate,sample_rate,wma_bit_per_sample,wma_block_align\n");
     printf("                                          ->wma_channel_mask,wma_encode_option,wma_format_tag\n");
+    printf(" hal_play_test -f /etc/03_Kuch_Khaas_BE.btaptx -t 9 -d 2 -v 0.2 -r 44100 -c 2 -A 00:02:5b:00:ff:03 \n");
+    printf("                                          -> Play aptx clip (-t = 9)\n");
+    printf("                                          -> 2 channels and 44100 sample rate\n");
+    printf("                                          -> BT addr: bt_addr=00:02:5b:00:ff:03\n");
     printf(" hal_play_test -K -F 4                    -> Measure latency KPIs for low latency output\n\n");
     printf(" hal_play_test -f /etc//Moto_320kbps.mp3 -t 2 -d 2 -v 0.1 -r 44100 -c 2 -e 2\n");
     printf("                                          -> plays MP3 stream(-t = 2) on speaker device(-d = 2)\n");
@@ -640,7 +668,10 @@
     char *kvpair_values = NULL;
     char kvpair[1000] = {0};
     struct proxy_data proxy_params;
-
+    char *ba = NULL;
+    qahw_param_payload payload;
+    qahw_param_id param_id;
+    struct qahw_aptx_dec_param aptx_params;
     /*
      * Default values
      */
@@ -675,6 +706,7 @@
         {"flags",         required_argument,    0, 'F'},
         {"kpi-mode",      no_argument,          0, 'K'},
         {"effect-path",   required_argument,    0, 'e'},
+        {"bt-addr",       required_argument,    0, 'A'},
         {"help",          no_argument,          0, 'h'},
         {0, 0, 0, 0}
     };
@@ -698,7 +730,7 @@
     proxy_params.hdr.data_sz = 0;
     while ((opt = getopt_long(argc,
                               argv,
-                              "-f:r:c:b:d:v:l:t:a:w:k:D:KF:e:h",
+                              "-f:r:c:b:d:v:l:t:a:w:k:D:KF:e:A:h",
                               long_options,
                               &option_index)) != -1) {
             switch (opt) {
@@ -767,6 +799,9 @@
                     ethread_func = effect_thread_funcs[effect_index];
                 }
                 break;
+            case 'A':
+                ba = optarg;
+                break;
             case 'h':
                 usage();
                 return 0;
@@ -902,6 +937,9 @@
         case FILE_DTS:
             config.offload_info.format = AUDIO_FORMAT_DTS;
             break;
+        case FILE_APTX:
+            config.offload_info.format = AUDIO_FORMAT_APTX;
+            break;
         default:
            fprintf(stderr, "Does not support given filetype\n");
            usage();
@@ -924,6 +962,23 @@
     if (output_device & AUDIO_DEVICE_OUT_ALL_A2DP) {
         output_device = AUDIO_DEVICE_OUT_PROXY;
     }
+    if (filetype == FILE_APTX) {
+        if (ba != NULL) {
+            parse_aptx_dec_bt_addr(ba, &aptx_params);
+            payload = (qahw_param_payload)aptx_params;
+            param_id = QAHW_PARAM_APTX_DEC;
+            fprintf(log_file, "Send BT addr nap %d, uap %d lap %d to HAL\n", aptx_params.bt_addr.nap,
+                        aptx_params.bt_addr.uap, aptx_params.bt_addr.lap);
+            rc = qahw_set_param_data(qahw_mod_handle, param_id, &payload);
+            if (rc != 0)
+                 printf("Error.Failed Set BT addr\n");
+        } else {
+            fprintf(log_file, "BT addr is NULL, Need valid BT addr for aptx file playback to work\n");
+            fprintf(stderr, "BT addr is NULL, Need valid BT addr for aptx file playback to work\n");
+            goto EXIT;
+        }
+    }
+
     fprintf(log_file, "calling open_out_put_stream:\n");
     rc = qahw_open_output_stream(qahw_mod_handle,
                                  handle,