Merge "agm: check for boundary size" into audio-platform-arintf.lnx.1.0.c10
diff --git a/ipc/SwBinders/agm_server/src/ipc_proxy_server.cpp b/ipc/SwBinders/agm_server/src/ipc_proxy_server.cpp
index 2840a1c..82cd647 100644
--- a/ipc/SwBinders/agm_server/src/ipc_proxy_server.cpp
+++ b/ipc/SwBinders/agm_server/src/ipc_proxy_server.cpp
@@ -1190,11 +1190,27 @@
         clbk_data_obj->client_data = (void *)data.readInt64();
         sp<IBinder> binder = data.readStrongBinder();
         clbk_data_obj->cb_binder = interface_cast<ICallback>(binder);
-        list_add_tail(&clbk_data_list, &clbk_data_obj->list);
-        pthread_mutex_unlock(&clbk_data_list_lock);
+        if (clbk_data_obj->cb_func != NULL) {
+            list_add_tail(&clbk_data_list, &clbk_data_obj->list);
+            rc = ipc_agm_session_register_cb(clbk_data_obj->session_id,
+                            &ipc_cb, evnt, clbk_data_obj->client_data);
+        } else {
+            clbk_data *clbk_data_obj_tmp = NULL;
+            struct listnode *node = NULL, *next = NULL;
+            list_for_each_safe(node, next, &clbk_data_list) {
+                clbk_data_obj_tmp = node_to_item(node, clbk_data, list);
+                if ((clbk_data_obj_tmp->session_id == clbk_data_obj->session_id) &&
+                    (clbk_data_obj_tmp->client_data == clbk_data_obj->client_data)) {
+                    list_remove(&clbk_data_obj_tmp->list);
+                    free(clbk_data_obj_tmp);
+                }
+            }
+            rc = ipc_agm_session_register_cb(clbk_data_obj->session_id,
+                            NULL, evnt, clbk_data_obj->client_data);
+            free(clbk_data_obj);
+        }
 
-        rc = ipc_agm_session_register_cb(clbk_data_obj->session_id,
-                        &ipc_cb, evnt, clbk_data_obj->client_data);
+        pthread_mutex_unlock(&clbk_data_list_lock);
         reply->writeInt32(rc);
         break ; }
 
diff --git a/plugins/tinyalsa/src/agm_pcm_plugin.c b/plugins/tinyalsa/src/agm_pcm_plugin.c
index 92c53ce..176a218 100644
--- a/plugins/tinyalsa/src/agm_pcm_plugin.c
+++ b/plugins/tinyalsa/src/agm_pcm_plugin.c
@@ -340,6 +340,7 @@
     uint32_t read_index, wall_clk_msw, wall_clk_lsw;
     int64_t delta_wall_clk_us = 0;
     uint32_t delta_wall_clk_frames = 0;
+    uint64_t sub_res = 0;
     int ret = 0;
     uint32_t period_size = priv->period_size; /** in frames */
     uint32_t crossed_boundary = 0;
@@ -371,7 +372,8 @@
                                          priv->pos_buf->wall_clk_lsw);
             // Compute delta only if diff is greater than zero
             if (dsp_wall_clk > cached_wall_clk) {
-                delta_wall_clk_us = (int64_t)(dsp_wall_clk - cached_wall_clk);
+                __builtin_usubl_overflow(dsp_wall_clk,cached_wall_clk,&sub_res);
+                delta_wall_clk_us = (int64_t)sub_res;
             }
         }
         // Identify the number of times of shared buffer length that the
diff --git a/service/src/agm.c b/service/src/agm.c
index bc8bb0c..f56e7a4 100644
--- a/service/src/agm.c
+++ b/service/src/agm.c
@@ -648,6 +648,9 @@
     struct session_obj *obj = NULL;
     int ret = 0;
 
+    AGM_LOGI("%sconnecting aifid:%d with session id=%d\n",
+                                      (state ? "": "dis"), aif_id, session_id);
+
     ret = session_obj_get(session_id, &obj);
     if (ret) {
         AGM_LOGE("Error:%d retrieving session obj with session id=%d\n",
diff --git a/service/src/device.c b/service/src/device.c
index 808b927..5d509bc 100644
--- a/service/src/device.c
+++ b/service/src/device.c
@@ -738,9 +738,11 @@
 {
    int ret = 0;
 
+   AGM_LOGI("Setting device metadata for %s\n", dev_obj->name);
    pthread_mutex_lock(&dev_obj->lock);
    metadata_free(&dev_obj->metadata);
    ret = metadata_copy(&(dev_obj->metadata), size, metadata);
+   metadata_print(&(dev_obj->metadata));
    pthread_mutex_unlock(&dev_obj->lock);
 
    return ret;
diff --git a/service/src/graph.c b/service/src/graph.c
index fe2f228..481baa2 100644
--- a/service/src/graph.c
+++ b/service/src/graph.c
@@ -538,6 +538,7 @@
     size_t arraysize = 0;
     module_info_t *stream_module_list = NULL;
     module_info_t *hw_ep_module = NULL;
+    module_info_t *add_module = NULL;
 
     list_declare(node_sess);
     list_init(&node_sess);
@@ -616,10 +617,16 @@
                         goto free_graph_obj;
                     }
                     mod = mod_list->data;
-                    mod->miid = gsl_tag_entry->module_entry[0].module_iid;
-                    mod->mid = gsl_tag_entry->module_entry[0].module_id;
-                    AGM_LOGD("miid %x mid %x tag %x", mod->miid, mod->mid, mod->tag);
-                    ADD_MODULE(*mod, NULL);
+                    add_module = ADD_MODULE(*mod, NULL);
+                    if (!add_module) {
+                        AGM_LOGE("no memory to allocate add_module");
+                        ret = -ENOMEM;
+                        goto free_graph_obj;
+                    }
+                    add_module->miid = gsl_tag_entry->module_entry[0].module_iid;
+                    add_module->mid = gsl_tag_entry->module_entry[0].module_id;
+                    add_module->gkv = NULL;
+                    AGM_LOGD("miid %x mid %x tag %x", add_module->miid, add_module->mid, add_module->tag);
                     goto tag_list;
                 }
             }
@@ -641,8 +648,14 @@
                         goto free_graph_obj;
                     }
                     mod = mod_list->data;
-                    mod->miid = gsl_tag_entry->module_entry[0].module_iid;
-                    mod->mid = gsl_tag_entry->module_entry[0].module_id;
+                    add_module = ADD_MODULE(*mod, dev_obj);
+                    if (!add_module) {
+                        AGM_LOGE("no memory to allocate add_module");
+                        ret = -ENOMEM;
+                        goto free_graph_obj;
+                    }
+                    add_module->miid = gsl_tag_entry->module_entry[0].module_iid;
+                    add_module->mid = gsl_tag_entry->module_entry[0].module_id;
                     /*store GKV which describes/contains this module*/
                     gkv = calloc(1, sizeof(struct agm_key_vector_gsl));
                     if (!gkv) {
@@ -660,9 +673,8 @@
                     }
                     memcpy(gkv->kv, meta_data_kv->gkv.kv,
                           gkv->num_kvs * sizeof(struct agm_key_value));
-                    mod->gkv = gkv;
-                    AGM_LOGD("miid %x mid %x tag %x", mod->miid, mod->mid, mod->tag);
-                    ADD_MODULE(*mod, dev_obj);
+                    add_module->gkv = gkv;
+                    AGM_LOGD("miid %x mid %x tag %x", add_module->miid, add_module->mid, add_module->tag);
                     goto tag_list;
                 }
             }
@@ -919,6 +931,7 @@
     }
 
     if (meta_data) {
+        metadata_print(meta_data);
         memcpy (&(gsl_cmd_prop.gkv), &(meta_data->gkv),
                                        sizeof(struct gsl_key_vector));
         gsl_cmd_prop.property_id = meta_data->sg_props.prop_id;
@@ -1458,6 +1471,7 @@
         bool mod_present = false;
         size_t arraysize;
         module_info_t *hw_ep_module = NULL;
+        module_info_t *add_module = NULL;
 
         get_hw_ep_module_list_array(&hw_ep_module, &arraysize);
         if (dev_obj->hw_ep_info.dir == AUDIO_OUTPUT)
@@ -1475,8 +1489,6 @@
                           mod->tag);
             goto done;
         }
-        mod->miid = module_info->module_entry[0].module_iid;
-        mod->mid = module_info->module_entry[0].module_id;
         /**
          *Check if this is the same device object as was passed for graph open
          *or a new one.We do this by comparing the module_iid of the module
@@ -1485,7 +1497,7 @@
          */
         list_for_each(node, &graph_obj->tagged_mod_list) {
             temp_mod = node_to_item(node, module_info_t, list);
-            if (temp_mod->miid == mod->miid) {
+            if (temp_mod->miid == module_info->module_entry[0].module_iid) {
                 mod_present = true;
                 /**
                  * Module might have configured previously as we don't reset in
@@ -1502,6 +1514,14 @@
              */
             /*Make a local copy of gkv and use when we query gsl
             for tagged data*/
+            add_module = ADD_MODULE(*mod, dev_obj);
+            if (!add_module) {
+                AGM_LOGE("no memory to allocate add_module");
+                ret = -ENOMEM;
+                goto done;
+            }
+            add_module->miid = module_info->module_entry[0].module_iid;
+            add_module->mid = module_info->module_entry[0].module_id;
             gkv = calloc(1, sizeof(struct agm_key_vector_gsl));
             if (!gkv) {
                 AGM_LOGE("No memory to allocate for gkv\n");
@@ -1518,11 +1538,10 @@
             }
             memcpy(gkv->kv, meta_data_kv->gkv.kv,
                     gkv->num_kvs * sizeof(struct agm_key_value));
-            mod->gkv = gkv;
+            add_module->gkv = gkv;
             gkv = NULL;
             AGM_LOGD("Adding the new module tag %x mid %x miid %x\n",
-                                    mod->tag, mod->mid, mod->miid);
-            ADD_MODULE(*mod, dev_obj);
+                    add_module->tag, add_module->mid, add_module->miid);
         }
     }
     /* Configure the newly added modules only if graph is in start state,
diff --git a/service/src/session_obj.c b/service/src/session_obj.c
index be3591a..f3295b0 100644
--- a/service/src/session_obj.c
+++ b/service/src/session_obj.c
@@ -1254,6 +1254,7 @@
     struct aif *aif_obj = NULL;
     enum agm_session_mode sess_mode = sess_obj->stream_config.sess_mode;
     struct listnode *node = NULL;
+    struct listnode *next = NULL;
 
     AGM_LOGD("enter");
     if (sess_obj->state == SESSION_CLOSED) {
@@ -1279,7 +1280,7 @@
     sess_obj->loopback_state = false;
 
     if (sess_mode != AGM_SESSION_NON_TUNNEL  && sess_mode != AGM_SESSION_NO_CONFIG) {
-        list_for_each(node, &sess_obj->aif_pool) {
+        list_for_each_safe(node, next, &sess_obj->aif_pool) {
             aif_obj = node_to_item(node, struct aif, node);
             if (!aif_obj) {
                 AGM_LOGE("Error:%d could not find aif node\n", ret);
@@ -1746,6 +1747,7 @@
     int ret = 0;
     struct aif *aif_obj = NULL;
 
+    AGM_LOGI("Setting metadata for sess aif id %d\n", aif_id);
     pthread_mutex_lock(&sess_obj->lock);
     ret = aif_obj_get(sess_obj, aif_id, &aif_obj);
     if (ret) {
@@ -1761,9 +1763,11 @@
                   sess_id:%d, aif_id:%d \n",
                   sess_obj->sess_id, aif_obj->aif_id);
     }
+    metadata_print(&(aif_obj->sess_aif_meta));
 
 done:
     pthread_mutex_unlock(&sess_obj->lock);
+    AGM_LOGI("Exit");
     return ret;
 }