Merge "service: Update debug logs"
diff --git a/plugins/tinyalsa/test/agmmixer.c b/plugins/tinyalsa/test/agmmixer.c
index cfebf9f..94584ef 100644
--- a/plugins/tinyalsa/test/agmmixer.c
+++ b/plugins/tinyalsa/test/agmmixer.c
@@ -370,7 +370,7 @@
     return ret;
 }
 
-int set_agm_audio_intf_metadata(struct mixer *mixer, char *intf_name, enum dir d, int rate, int bitwidth, uint32_t val)
+int set_agm_audio_intf_metadata(struct mixer *mixer, char *intf_name, enum usecase_type usecase, int rate, int bitwidth, uint32_t val)
 {
     char *control = "metadata";
     struct mixer_ctl *ctl;
@@ -403,9 +403,12 @@
         return -ENOMEM;
     }
 
-    if (d == PLAYBACK) {
+    if (usecase == PLAYBACK) {
         gkv[0].key = DEVICERX;
         gkv[0].value = SPEAKER;
+    } else if (usecase == HAPTICS) {
+        gkv[0].key = DEVICERX;
+        gkv[0].value = HAPTICS_DEVICE;
     } else if (val == VOICE_UI) {
         gkv[0].key = DEVICETX;
         gkv[0].value = HANDSETMIC_VA;
@@ -562,7 +565,7 @@
 
 }
 
-int set_agm_capture_stream_metadata(struct mixer *mixer, int device, uint32_t val, enum dir d,
+int set_agm_capture_stream_metadata(struct mixer *mixer, int device, uint32_t val, enum usecase_type usecase,
      enum stream_type stype, unsigned int dev_channels)
 {
     char *stream = "PCM";
@@ -649,7 +652,7 @@
     return ret;
 }
 
-int set_agm_stream_metadata(struct mixer *mixer, int device, uint32_t val, enum dir d, enum stream_type stype, char *intf_name)
+int set_agm_stream_metadata(struct mixer *mixer, int device, uint32_t val, enum usecase_type usecase, enum stream_type stype, char *intf_name)
 {
     char *stream = "PCM";
     char *control = "metadata";
@@ -676,6 +679,9 @@
     if (val == PCM_LL_PLAYBACK || val == COMPRESSED_OFFLOAD_PLAYBACK || val == PCM_RECORD)
         num_gkv += 1;
 
+    if (val == HAPTICS_PLAYBACK)
+        num_gkv = 1;
+
     if (val == VOICE_UI) {
         if (intf_name)
             num_gkv = 2;
@@ -723,7 +729,7 @@
             index++;
         }
     } else {
-        if (d == PLAYBACK)
+        if (usecase == PLAYBACK)
             gkv[index].key = STREAMRX;
         else
             gkv[index].key = STREAMTX;
@@ -733,15 +739,16 @@
         index++;
         if (val == PCM_LL_PLAYBACK || val == COMPRESSED_OFFLOAD_PLAYBACK ||
             val == PCM_RECORD) {
+            printf("Instance key is added\n");
             gkv[index].key = INSTANCE;
             gkv[index].value = INSTANCE_1;
             index++;
         }
 
-        if (d == PLAYBACK) {
+        if (usecase == PLAYBACK  && val != HAPTICS_PLAYBACK) {
             gkv[index].key = DEVICEPP_RX;
             gkv[index].value = DEVICEPP_RX_AUDIO_MBDRC;
-        } else {
+        } else if (val != HAPTICS_PLAYBACK) {
             gkv[index].key = DEVICEPP_TX;
             gkv[index].value = DEVICEPP_TX_AUDIO_FLUENCE_SMECNS;
         }
diff --git a/plugins/tinyalsa/test/agmmixer.h b/plugins/tinyalsa/test/agmmixer.h
index 6d1618e..52cf8ae 100644
--- a/plugins/tinyalsa/test/agmmixer.h
+++ b/plugins/tinyalsa/test/agmmixer.h
@@ -33,10 +33,11 @@
 #include <tinyalsa/asoundlib.h>
 #include <kvh2xml.h>
 
-enum dir{
+enum usecase_type{
     PLAYBACK,
     CAPTURE,
     LOOPBACK,
+    HAPTICS,
 };
 
 enum stream_type {
@@ -54,10 +55,10 @@
 int set_agm_device_media_config(struct mixer *mixer, unsigned int channels,
                                 unsigned int rate, unsigned int bits, char *intf_name);
 int connect_play_pcm_to_cap_pcm(struct mixer *mixer, unsigned int p_device, unsigned int c_device);
-int set_agm_audio_intf_metadata(struct mixer *mixer, char *intf_name, enum dir, int rate, int bitwidth, uint32_t val);
+int set_agm_audio_intf_metadata(struct mixer *mixer, char *intf_name, enum usecase_type, int rate, int bitwidth, uint32_t val);
 int set_agm_stream_metadata_type(struct mixer *mixer, int device, char *val, enum stream_type stype);
-int set_agm_stream_metadata(struct mixer *mixer, int device, uint32_t val, enum dir d, enum stream_type stype, char *intf_name);
-int set_agm_capture_stream_metadata(struct mixer *mixer, int device, uint32_t val, enum dir d, enum stream_type stype, unsigned int dev_channels);
+int set_agm_stream_metadata(struct mixer *mixer, int device, uint32_t val, enum usecase_type utype, enum stream_type stype, char *intf_name);
+int set_agm_capture_stream_metadata(struct mixer *mixer, int device, uint32_t val, enum usecase_type utype, enum stream_type stype, unsigned int dev_channels);
 int connect_agm_audio_intf_to_stream(struct mixer *mixer, unsigned int device,
                                   char *intf_name, enum stream_type, bool connect);
 int agm_mixer_register_event(struct mixer *mixer, int device, enum stream_type stype, uint32_t miid, uint8_t is_register);
diff --git a/plugins/tinyalsa/test/agmplay.c b/plugins/tinyalsa/test/agmplay.c
index 8810639..f008653 100644
--- a/plugins/tinyalsa/test/agmplay.c
+++ b/plugins/tinyalsa/test/agmplay.c
@@ -67,7 +67,7 @@
 static int close = 0;
 
 void play_sample(FILE *file, unsigned int card, unsigned int device,
-                 struct chunk_fmt fmt, struct device_config *dev_config);
+                 struct chunk_fmt fmt, struct device_config *dev_config, bool haptics);
 
 void stream_close(int sig)
 {
@@ -83,13 +83,14 @@
     struct chunk_header chunk_header;
     struct chunk_fmt chunk_fmt;
     unsigned int card = 100, device = 100;
+    bool haptics;
     char *intf_name = NULL;
     struct device_config config;
     char *filename;
     int more_chunks = 1, ret = 0;
 
-    if (argc < 2) {
-        printf("Usage: %s file.wav [-D card] [-d device] [-i device_id]\n",
+    if (argc < 3) {
+        printf("Usage: %s file.wav [-D card] [-d device] [-i device_id] [-h haptics]\n",
                 argv[0]);
         return 1;
     }
@@ -145,6 +146,11 @@
             if (*argv)
                 intf_name = *argv;
         }
+       if (strcmp(*argv, "-h") == 0) {
+            argv++;
+            if (*argv)
+                haptics = *argv;
+       }
         if (*argv)
             argv++;
     }
@@ -158,7 +164,7 @@
         fclose(file);
         return ret;
     }
-    play_sample(file, card, device, chunk_fmt, &config);
+    play_sample(file, card, device, chunk_fmt, &config, haptics);
 
     fclose(file);
 
@@ -166,12 +172,13 @@
 }
 
 void play_sample(FILE *file, unsigned int card, unsigned int device,
-                 struct chunk_fmt fmt, struct device_config *dev_config)
+                 struct chunk_fmt fmt, struct device_config *dev_config, bool haptics)
 {
     struct pcm_config config;
     struct pcm *pcm;
     struct mixer *mixer;
     char *buffer;
+    int playback_path, playback_value;
     int size;
     int num_read;
     char *name = dev_config->name;
@@ -205,16 +212,21 @@
         printf("Failed to set device media config\n");
         goto err_close_mixer;
     }
-
-    /* set audio interface metadata mixer control */
-    if (set_agm_audio_intf_metadata(mixer, name, PLAYBACK,
+    if (haptics) {
+        playback_path = HAPTICS;
+        playback_value = HAPTICS_PLAYBACK;
+    } else {
+        playback_path = PLAYBACK;
+        playback_value = PCM_LL_PLAYBACK;
+    }
+     /* set audio interface metadata mixer control */
+    if (set_agm_audio_intf_metadata(mixer, name, playback_path,
                                     dev_config->rate, dev_config->bits, PCM_LL_PLAYBACK)) {
         printf("Failed to set device metadata\n");
         goto err_close_mixer;
     }
-
     /* set audio interface metadata mixer control */
-    if (set_agm_stream_metadata(mixer, device, PCM_LL_PLAYBACK, PLAYBACK, STREAM_PCM, NULL)) {
+    if (set_agm_stream_metadata(mixer, device, playback_value, PLAYBACK, STREAM_PCM, NULL)) {
         printf("Failed to set pcm metadata\n");
         goto err_close_mixer;
     }
diff --git a/service/src/graph.c b/service/src/graph.c
index 94d6af6..c39d82e 100644
--- a/service/src/graph.c
+++ b/service/src/graph.c
@@ -148,6 +148,10 @@
     enum agm_data_mode mode = sess_obj->stream_config.data_mode;
     struct agm_buffer_config buffer_config = {0};
 
+    if (gph_obj == NULL){
+        AGM_LOGE("invalid graph object\n");
+        return -EINVAL;
+    }
     if (gph_obj->is_config_buf_params_done) {
         AGM_LOGD("configure buf params already done");
         return 0;
@@ -1144,7 +1148,7 @@
         AGM_LOGE("gsl_write for size %zu failed with error %d\n", *size, ret);
         goto done;
     }
-    *size = size_written;
+    *size = (size_t)size_written;
 done:
     return ret;
 }
@@ -1737,6 +1741,10 @@
     struct ar_shmem_handle *shmem_handle;
     int ret = -1;
 
+    if (gph_obj == NULL){
+        AGM_LOGE("invalid graph object\n");
+        return -EINVAL;
+    }
     shmem_buf_info = calloc(1, sizeof(struct gsl_cmd_get_shmem_buf_info));
     if (!shmem_buf_info) {
         AGM_LOGE("shmem_buf_info allocation failed\n");
@@ -1838,6 +1846,10 @@
     size_t payload_size = 0;
     uint32_t decoder_miid = 0;
 
+    if (graph_obj == NULL){
+        AGM_LOGE("invalid graph object\n");
+        return -EINVAL;
+    }
     pthread_mutex_lock(&graph_obj->lock);
 
     list_for_each(node, &graph_obj->tagged_mod_list) {
diff --git a/service/src/graph_module.c b/service/src/graph_module.c
index 98e0b52..f899da1 100644
--- a/service/src/graph_module.c
+++ b/service/src/graph_module.c
@@ -920,15 +920,16 @@
         samples_per_msec = sess_obj->in_media_config.rate/1000;
         channels = sess_obj->in_media_config.channels;
         bits = get_pcm_bit_width(sess_obj->in_media_config.format);
+        bits = GET_BITS_PER_SAMPLE(sess_obj->in_media_config.format, bits);
         channels = (channels == 0) ? MONO : channels;
-        bits = (bits == 0) ? 16 : bits;
         frame_size = (sess_obj->in_buffer_config.size * 8) /
                         (channels * bits);
 
         if (samples_per_msec &&
-            (((frame_size/samples_per_msec) * samples_per_msec) != frame_size))
+            (((frame_size/samples_per_msec) * samples_per_msec) != frame_size)) {
             AGM_LOGD("pcm encoder: frame_size %d\n", frame_size);
             ret = configure_pcm_encoder_frame_size(mod, graph_obj, frame_size);
+        }
     }
 
     return ret;
diff --git a/service/src/metadata.c b/service/src/metadata.c
index 441c219..916e119 100644
--- a/service/src/metadata.c
+++ b/service/src/metadata.c
@@ -234,19 +234,24 @@
     for (i = 0; i < num; i++) {
         temp = va_arg(valist, struct agm_meta_data_gsl*);
         if (temp) {
-            memcpy(gkv_offset, temp->gkv.kv, temp->gkv.num_kvs *
-                                  sizeof(struct agm_key_value));
-            gkv_offset += temp->gkv.num_kvs;
+            if (temp->gkv.kv) {
+                memcpy(gkv_offset, temp->gkv.kv, temp->gkv.num_kvs *
+                                      sizeof(struct agm_key_value));
+                gkv_offset += temp->gkv.num_kvs;
+            }
 
-            memcpy(ckv_offset, temp->ckv.kv, temp->ckv.num_kvs *
-                                  sizeof(struct agm_key_value));
-            ckv_offset += temp->ckv.num_kvs;
+            if (temp->ckv.kv) {
+                memcpy(ckv_offset, temp->ckv.kv, temp->ckv.num_kvs *
+                                      sizeof(struct agm_key_value));
+                ckv_offset += temp->ckv.num_kvs;
+            }
 
-            merged->sg_props.prop_id = temp->sg_props.prop_id;
-            memcpy(prop_offset, temp->sg_props.values,
-                   temp->sg_props.num_values * sizeof(uint32_t));
-            prop_offset += temp->sg_props.num_values;
-
+            if (temp->sg_props.values) {
+                merged->sg_props.prop_id = temp->sg_props.prop_id;
+                memcpy(prop_offset, temp->sg_props.values,
+                       temp->sg_props.num_values * sizeof(uint32_t));
+                prop_offset += temp->sg_props.num_values;
+            }
         }
     }
     va_end(valist);