agm: check state for parent device if it exists

When device state changes in agm, only parent device
get updated, causing error when we try to check state
of child device.

Add device_get_state to return proper state for device
object.

Change-Id: I83baae29c6788bf4d3fe188b661b97c62284243e
diff --git a/service/inc/private/agm/device.h b/service/inc/private/agm/device.h
index 0324658..7fda4a3 100644
--- a/service/inc/private/agm/device.h
+++ b/service/inc/private/agm/device.h
@@ -240,5 +240,6 @@
 int device_get_group_list(struct aif_info *aif_list, size_t *num_groups);
 
 int device_get_start_refcnt(struct device_obj *dev_obj);
+int device_get_state(struct device_obj *dev_obj);
 bool get_file_path_extn(char* file_path_extn, char* file_path_extn_wo_variant);
 #endif
diff --git a/service/src/device.c b/service/src/device.c
index 7c03b25..743972f 100644
--- a/service/src/device.c
+++ b/service/src/device.c
@@ -937,6 +937,19 @@
 
 }
 
+int device_get_state(struct device_obj *dev_obj)
+{
+    if (dev_obj == NULL) {
+        AGM_LOGE("Invalid device object\n");
+        return DEV_CLOSED;
+    }
+
+    if (dev_obj->parent_dev)
+        return dev_obj->parent_dev->state;
+    else
+        return dev_obj->state;
+}
+
 static struct device_group_data* device_get_group_data_by_name(char *dev_name)
 {
     struct device_group_data *grp_data = NULL;
diff --git a/service/src/session_obj.c b/service/src/session_obj.c
index 92b4b75..622cad7 100644
--- a/service/src/session_obj.c
+++ b/service/src/session_obj.c
@@ -660,8 +660,11 @@
         return ret;
 
     pthread_mutex_lock(&dev_obj->lock);
-    if ((dev_obj->state == DEV_OPENED || dev_obj->state == DEV_STARTED ||
-        dev_obj->state == DEV_PREPARED) && dev_obj->params != NULL) {
+
+    if ((device_get_state(dev_obj) == DEV_OPENED ||
+         device_get_state(dev_obj) == DEV_STARTED ||
+         device_get_state(dev_obj) == DEV_PREPARED) &&
+        dev_obj->params != NULL) {
         ret = graph_set_config(sess_obj->graph, dev_obj->params,
                 dev_obj->params_size);
         if (ret)
@@ -1049,7 +1052,7 @@
                     goto done;
                 }
 
-                if (ec_ref_dev_obj->state != DEV_STARTED) {
+                if (device_get_state(ec_ref_dev_obj) != DEV_STARTED) {
                     AGM_LOGE("Error:%d Device object with aif id:%d\n"
                               "not in STARTED state, current state:%d\n",
                               ret, sess_obj->ec_ref_aif_id,