scripting: support for zone based routing in application classes
[profile/ivi/pulseaudio-module-murphy-ivi.git] / murphy / murphyif.c
index d7d21ab..9c36cc3 100644 (file)
@@ -20,6 +20,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <string.h>
+#include <alloca.h>
 #include <errno.h>
 
 #include <pulse/utf8.h>
@@ -168,13 +169,16 @@ typedef struct {
     mir_node   *node;
     rset_data  *rset;
 } pid_hash;
-#endif
 
+typedef struct {
+    size_t      nnode;
+    mir_node  **nodes;
+    rset_data  *rset;
+} rset_hash;
 
-#ifdef WITH_RESOURCES
-static mir_node *find_node_by_rsetid(struct userdata *, const char *);
 #endif
 
+
 #ifdef WITH_DOMCTL
 static void domctl_connect_notify(mrp_domctl_t *,int,int,const char *,void *);
 static void domctl_watch_notify(mrp_domctl_t *,mrp_domctl_data_t *,int,void *);
@@ -226,11 +230,12 @@ static void connect_attempt(pa_mainloop_api *, pa_time_event *,
 static void schedule_connect(struct userdata *, resource_interface *);
 static void cancel_schedule(struct userdata *, resource_interface *);
 
-static int  node_put_rset(struct userdata *, mir_node *, rset_data *);
+static rset_hash *node_put_rset(struct userdata *, mir_node *, rset_data *);
 static void node_enforce_resource_policy(struct userdata *, mir_node *,
                                          rset_data *);
 static rset_data *rset_data_dup(rset_data *);
 static void rset_data_copy(rset_data *, rset_data *);
+static void rset_data_update(rset_data *, rset_data *);
 static void rset_data_free(rset_data *);
 
 static void        pid_hashmap_free(void *, void *);
@@ -240,6 +245,12 @@ static mir_node   *pid_hashmap_get_node(struct userdata *, const char *);
 static rset_data  *pid_hashmap_get_rset(struct userdata *, const char *);
 static mir_node   *pid_hashmap_remove_node(struct userdata *, const char *);
 static rset_data  *pid_hashmap_remove_rset(struct userdata *, const char *);
+
+static void       rset_hashmap_free(void *, void *);
+static rset_hash *rset_hashmap_put(struct userdata *, const char *, mir_node *);
+static rset_hash *rset_hashmap_get(struct userdata *u, const char *rsetid);
+static int        rset_hashmap_remove(struct userdata *,const char *,mir_node*);
+
 #endif
 
 static pa_proplist *get_node_proplist(struct userdata *, mir_node *);
@@ -319,6 +330,9 @@ void pa_murphyif_done(struct userdata *u)
 #ifdef WITH_RESOURCES
     resource_attribute *attr, *a;
     resource_request *req, *r;
+    void *state;
+    rset_hash *rh;
+    pid_hash *ph;
 #endif
 
     if (u && (murphyif = u->murphyif)) {
@@ -330,7 +344,6 @@ void pa_murphyif_done(struct userdata *u)
         dif = &murphyif->domctl;
 
         mrp_domctl_destroy(dif->ctl);
-        mrp_mainloop_destroy(murphyif->ml);
 
         if (dif->ntable > 0 && dif->tables) {
             for (i = 0;  i < dif->ntable;  i++) {
@@ -360,9 +373,24 @@ void pa_murphyif_done(struct userdata *u)
 
         resource_transport_destroy(murphyif);
 
-        pa_xfree((void *)rif->atype);
-        pa_hashmap_free(rif->nodes.rsetid, NULL, NULL);
-        pa_hashmap_free(rif->nodes.pid, pid_hashmap_free, NULL);
+        PA_HASHMAP_FOREACH(rh, rif->nodes.rsetid, state) {
+            if (rh) {
+                pa_xfree(rh->nodes);
+                rset_data_free(rh->rset);
+                pa_xfree(rh);
+            }
+        }
+
+        PA_HASHMAP_FOREACH(ph, rif->nodes.pid, state) {
+            if (ph) {
+                pa_xfree((void *)ph->pid);
+                rset_data_free(ph->rset);
+                pa_xfree(ph);
+            }
+        }
+
+        pa_hashmap_free(rif->nodes.rsetid, NULL);
+        pa_hashmap_free(rif->nodes.pid, NULL);
 
         PA_LLIST_FOREACH_SAFE(attr, a, rif->attrs)
             resource_attribute_destroy(rif, attr);
@@ -374,6 +402,7 @@ void pa_murphyif_done(struct userdata *u)
         pa_xfree((void *)rif->inpres.name);
         pa_xfree((void *)rif->outres.name);
 #endif
+        mrp_mainloop_destroy(murphyif->ml);
 
         pa_xfree(murphyif);
     }
@@ -616,6 +645,9 @@ void pa_murphyif_destroy_resource_set(struct userdata *u, mir_node *node)
     pa_assert_se((murphyif = u->murphyif));
 
     if (node->localrset && node->rsetid) {
+
+        pa_murphyif_delete_node(u, node);
+
         rsetid = strtoul(node->rsetid, &e, 10);
 
         if (e == node->rsetid || *e) {
@@ -623,10 +655,15 @@ void pa_murphyif_destroy_resource_set(struct userdata *u, mir_node *node)
                    node->rsetid);
         }
         else {
+            if (rset_hashmap_remove(u, node->rsetid, node) < 0) {
+                pa_log_debug("failed to remove resource set %s from hashmap",
+                             node->rsetid);
+            }
+
             if (resource_set_destroy_node(u, rsetid))
-                pa_log_debug("resource set %u destruction request", rsetid);
+                pa_log_debug("sent resource set %u destruction request", rsetid);
             else {
-                pa_log("falied to destroy resourse set %u for node '%s'",
+                pa_log("failed to destroy resourse set %u for node '%s'",
                        rsetid, node->amname);
             }
 
@@ -635,8 +672,6 @@ void pa_murphyif_destroy_resource_set(struct userdata *u, mir_node *node)
             node->localrset = FALSE;
             node->rsetid = NULL;
         }
-
-        pa_murphyif_delete_node(u, node);
     }
 }
 
@@ -647,6 +682,7 @@ int pa_murphyif_add_node(struct userdata *u, mir_node *node)
     resource_interface *rif;
     const char *pid;
     rset_data *rset;
+    rset_hash *rh;
     char buf[64];
 
     pa_assert(u);
@@ -657,8 +693,8 @@ int pa_murphyif_add_node(struct userdata *u, mir_node *node)
     rif = &murphyif->resource;
 
     if (!node->rsetid) {
-        pa_log("can't register resource set for node '%s'.: missing rsetid",
-               node->amname);
+        pa_log("can't register resource set for node %u '%s'.: missing rsetid",
+               node->paidx, node->amname);
     }
     else if (pa_streq(node->rsetid, PA_RESOURCE_SET_ID_PID)) {
         if (!(pid = get_node_pid(u,node)))
@@ -671,7 +707,7 @@ int pa_murphyif_add_node(struct userdata *u, mir_node *node)
                 pa_log_debug("found resource-set %s for node '%s'",
                              rset->id, node->amname);
 
-                if (node_put_rset(u, node, rset) == 0) {
+                if (node_put_rset(u, node, rset)) {
                     node_enforce_resource_policy(u, node, rset);
                     rset_data_free(rset);
                     return 0;
@@ -689,12 +725,18 @@ int pa_murphyif_add_node(struct userdata *u, mir_node *node)
         }
     }
     else {
-        if (pa_hashmap_put(rif->nodes.rsetid, node->rsetid, node) == 0)
+        if ((rh = rset_hashmap_put(u, node->rsetid, node))) {
+            rset = rh->rset;
+
+            pa_log_debug("enforce policies on node %u '%s' rsetid:%s autorel:%s "
+                         "state:%s grant:%s policy:%s", node->paidx, node->amname,
+                         rset->id, rset->autorel ? "yes":"no",
+                         rset->state == RSET_ACQUIRE ? "acquire":"release",
+                         rset->grant ? "yes":"no", rset->policy);
+
+            node_enforce_resource_policy(u, node, rset);
             return 0;
-        else {
-            pa_log("can't register resource set for node '%s': conflicting "
-                   "resource id '%s'", node->amname, node->rsetid);
-        } 
+        }
     }
 
     return -1;
@@ -721,39 +763,24 @@ void pa_murphyif_delete_node(struct userdata *u, mir_node *node)
     if (node->rsetid) {
         if (pa_streq(node->rsetid, PA_RESOURCE_SET_ID_PID)) {
             if ((pid = get_node_pid(u, node))) {
-                deleted = pid_hashmap_remove_node(u, pid);
-                pa_assert(!deleted || deleted == node);
+                if (node == pid_hashmap_get_node(u, pid))
+                    pid_hashmap_remove_node(u, pid);
+                else {
+                    pa_log("pid %s seems to have multiple resource sets. "
+                           "Refuse to delete node %u (%s) from hashmap",
+                           pid, node->index, node->amname);
+                }
             }
         }
         else {
-            deleted = pa_hashmap_remove(rif->nodes.rsetid, node->rsetid);
-            pa_assert(!deleted || deleted == node);
+            if (rset_hashmap_remove(u, node->rsetid, node) < 0) {
+                pa_log("failed to remove node '%s' from rset hash", node->amname);
+            }
         }
     }
 #endif
 }
 
-#ifdef WITH_RESOURCES
-static mir_node *find_node_by_rsetid(struct userdata *u, const char *rsetid)
-{
-    pa_murphyif *murphyif;
-    resource_interface *rif;
-    mir_node *node;
-
-    pa_assert(u);
-    pa_assert_se((murphyif = u->murphyif));
-
-    rif = &murphyif->resource;
-
-    if (!rsetid)
-        node = NULL;
-    else
-        node = pa_hashmap_get(rif->nodes.rsetid, rsetid);
-
-    return node;
-}
-#endif
-
 
 #ifdef WITH_DOMCTL
 static void domctl_connect_notify(mrp_domctl_t *dc, int connected, int errcode,
@@ -1171,8 +1198,10 @@ static pa_bool_t resource_set_destroy_all(struct userdata *u)
 
                 if (e == node->rsetid || *e)
                     success = FALSE;
-                else
+                else {
+                    rset_hashmap_remove(u, node->rsetid, node);
                     success &= resource_set_destroy_node(u, rsetid);
+                }
             }
 
             pa_xfree(node->rsetid);
@@ -1202,8 +1231,10 @@ static void resource_set_notification(struct userdata *u,
     mrp_domctl_value_t *cpolicy;
     char rsetid[32];
     const char *pid;
-    mir_node *node;
+    mir_node *node, **nodes;
+    rset_hash *rh;
     rset_data rset, *rs;
+    size_t i, size;
 
     pa_assert(u);
     pa_assert(table);
@@ -1257,7 +1288,7 @@ static void resource_set_notification(struct userdata *u,
             continue;
         }
 
-        if (!(node = find_node_by_rsetid(u, rset.id))) {
+        if (!(rh = rset_hashmap_get(u, rset.id))) {
             if (!pid) {
                 pa_log_debug("can't find node for resource set %s "
                              "(pid in resource set unknown)", rset.id);
@@ -1268,7 +1299,7 @@ static void resource_set_notification(struct userdata *u,
                 pa_log_debug("found node %s for resource-set '%s'",
                              node->amname, rset.id);
 
-                if (node_put_rset(u, node, &rset) < 0) {
+                if (!(rh = node_put_rset(u, node, &rset))) {
                     pa_log("can't register resource set for node '%s': "
                            "failed to set rsetid", node->amname);
                     continue;
@@ -1298,13 +1329,23 @@ static void resource_set_notification(struct userdata *u,
             }
         }
 
-        pa_log_debug("resource notification for node '%s' autorel:%s state:%s "
-                     "grant:%s pid:%s policy:%s", node->amname,
-                     rset.autorel ? "yes":"no",
-                     rset.state == RSET_ACQUIRE ? "acquire":"release",
-                     rset.grant ? "yes":"no", pid, rset.policy);
+        rset_data_update(rh->rset, &rset);
+
+        /* we need to make a copy of this as node_enforce_resource_policy()
+           will delete/modify it */
+        size = sizeof(mir_node *) * (rh->nnode + 1);
+        nodes = alloca(size);
+        memcpy(nodes, rh->nodes, size);
+
+        for (i = 0;  (node = nodes[i]);  i++) {
+            pa_log_debug("%u: resource notification for node '%s' autorel:%s "
+                         "state:%s grant:%s pid:%s policy:%s", i,
+                         node->amname, rset.autorel ? "yes":"no",
+                         rset.state == RSET_ACQUIRE ? "acquire":"release",
+                         rset.grant ? "yes":"no", pid, rset.policy);
 
-        node_enforce_resource_policy(u, node, &rset);
+            node_enforce_resource_policy(u, node, &rset);
+        }
     }
 }
 
@@ -1768,11 +1809,12 @@ static void cancel_schedule(struct userdata *u, resource_interface *rif)
     }
 }
 
-static int node_put_rset(struct userdata *u, mir_node *node, rset_data *rset)
+static rset_hash *node_put_rset(struct userdata *u, mir_node *node, rset_data *rset)
 {
     pa_murphyif *murphyif;
     resource_interface *rif;
     pa_proplist *pl;
+    rset_hash *rh;
 
     pa_assert(u);
     pa_assert(node);
@@ -1787,26 +1829,28 @@ static int node_put_rset(struct userdata *u, mir_node *node, rset_data *rset)
 
     pa_log_debug("setting rsetid %s for node %s", rset->id, node->amname);
 
-    pa_xfree(node->rsetid);
+    if (node->rsetid) {
+        pa_xfree(node->rsetid);
+    }
     node->rsetid = pa_xstrdup(rset->id);
 
     if (!(pl = get_node_proplist(u, node))) {
         pa_log("can't obtain property list for node %s", node->amname);
-        return -1;
+        return NULL;
     }
 
     if ((pa_proplist_sets(pl, PA_PROP_RESOURCE_SET_ID, node->rsetid) < 0)) {
         pa_log("failed to set '" PA_PROP_RESOURCE_SET_ID "' property "
                "of '%s' node", node->amname);
-        return -1;
+        return NULL;
     }
 
-    if (pa_hashmap_put(rif->nodes.rsetid, node->rsetid, node) < 0) {
+    if (!(rh = rset_hashmap_put(u, node->rsetid, node))) {
         pa_log("conflicting rsetid %s for %s", node->rsetid, node->amname);
-        return -1;
+        return NULL;
     }
 
-    return 0;
+    return rh;
 }
 
 static void node_enforce_resource_policy(struct userdata *u,
@@ -1822,7 +1866,7 @@ static void node_enforce_resource_policy(struct userdata *u,
 
     if (pa_streq(rset->policy, "relaxed"))
         req = PA_STREAM_RUN;
-    else {
+    else if (pa_streq(rset->policy, "strict")) {
         if (rset->state == RSET_RELEASE)
             req = PA_STREAM_KILL;
         else {
@@ -1832,6 +1876,9 @@ static void node_enforce_resource_policy(struct userdata *u,
                 req = PA_STREAM_BLOCK;
         }
     }
+    else {
+        req = PA_STREAM_BLOCK;
+    }
 
     pa_stream_state_change(u, node, req);
 }
@@ -1875,6 +1922,27 @@ static void rset_data_copy(rset_data *dst, rset_data *src)
 }
 
 
+static void rset_data_update(rset_data *dst, rset_data *src)
+{
+    rset_data *dup;
+
+    pa_assert(dst);
+    pa_assert(dst->id);
+    pa_assert(src);
+    pa_assert(src->id);
+    pa_assert(src->policy);
+
+    pa_assert_se(pa_streq(src->id, dst->id));
+
+    pa_xfree((void *)dst->policy);
+
+    dst->autorel = src->autorel;
+    dst->state   = src->state;
+    dst->grant   = src->grant;
+    dst->policy  = pa_xstrdup(src->policy);
+}
+
+
 static void rset_data_free(rset_data *rset)
 {
     if (rset) {
@@ -2009,6 +2077,124 @@ static rset_data *pid_hashmap_remove_rset(struct userdata *u, const char *pid)
 }
 
 
+static void rset_hashmap_free(void *r, void *userdata)
+{
+    rset_hash *rh = (rset_hash *)r;
+
+    (void)userdata;
+
+    if (rh) {
+        pa_xfree(rh->nodes);
+        rset_data_free(rh->rset);
+        pa_xfree(rh);
+    }
+}
+
+static rset_hash *rset_hashmap_put(struct userdata *u,
+                                   const char *rsetid,
+                                   mir_node *node)
+{
+    pa_murphyif *murphyif;
+    resource_interface *rif;
+    rset_hash *rh;
+    rset_data *rset;
+    size_t i;
+
+    pa_assert(u);
+    pa_assert(rsetid);
+    pa_assert(node);
+    pa_assert_se((murphyif = u->murphyif));
+    
+    rif = &murphyif->resource;
+
+    if ((rh = pa_hashmap_get(rif->nodes.rsetid, rsetid))) {
+        for (i = 0;  i < rh->nnode;  i++) {
+            if (rh->nodes[i] == node)
+                return NULL;
+        }
+
+        i = rh->nnode++;
+        rh->nodes = pa_xrealloc(rh->nodes, sizeof(mir_node *) * (rh->nnode+1));
+    }
+    else {
+        rset = pa_xnew0(rset_data, 1);
+
+        rset->id = pa_xstrdup(rsetid);
+        rset->policy = pa_xstrdup("unknown");
+
+        rh = pa_xnew0(rset_hash, 1);
+
+        rh->nnode = 1;
+        rh->nodes = pa_xnew0(mir_node *, 2);
+        rh->rset  = rset;
+
+        pa_hashmap_put(rif->nodes.rsetid, rh->rset->id, rh);
+
+        i = 0;
+    }
+
+
+    rh->nodes[i+0] = node;
+    rh->nodes[i+1] = NULL;
+
+    return rh;
+}
+
+static rset_hash *rset_hashmap_get(struct userdata *u, const char *rsetid)
+{
+    pa_murphyif *murphyif;
+    resource_interface *rif;
+    rset_hash *rh;
+
+    pa_assert(u);
+    pa_assert(rsetid);
+    pa_assert(murphyif = u->murphyif);
+    
+    rif = &murphyif->resource;
+
+    if ((rh = pa_hashmap_get(rif->nodes.rsetid, rsetid)))
+        return rh;
+
+    return NULL;
+}
+
+static int rset_hashmap_remove(struct userdata *u,
+                               const char *rsetid,
+                               mir_node *node)
+{
+    pa_murphyif *murphyif;
+    resource_interface *rif;
+    rset_hash *rh;
+    size_t i,j;
+
+    pa_assert(u);
+    pa_assert_se((murphyif = u->murphyif));
+
+    rif = &murphyif->resource;
+
+    if ((rh = pa_hashmap_get(rif->nodes.rsetid, rsetid))) {
+
+        for (i = 0;  i < rh->nnode;  i++) {
+            if (node == rh->nodes[i]) {
+                if (rh->nnode <= 1) {
+                    pa_hashmap_remove(rif->nodes.rsetid, rsetid);
+                    rset_hashmap_free(rh, NULL);
+                    return 0;
+                }
+                else {
+                    for (j = i;  j < rh->nnode;  j++)
+                        rh->nodes[j] = rh->nodes[j+1];
+
+                    rh->nnode--;
+
+                    return 0;
+                }
+            }
+        }
+    }
+
+    return -1;
+}
 
 #endif