daemon: Murphy/resource interfacing code overhaul.
authorKrisztian Litkey <kli@iki.fi>
Fri, 11 Oct 2013 15:36:46 +0000 (18:36 +0300)
committerKrisztian Litkey <kli@iki.fi>
Fri, 10 Jan 2014 15:07:32 +0000 (17:07 +0200)
src/Makefile.am
src/daemon/client.c
src/daemon/client.h
src/daemon/context.h
src/daemon/daemon.c
src/daemon/resctl.c [new file with mode: 0644]
src/daemon/resctl.h [new file with mode: 0644]
src/daemon/resourceif.c [deleted file]
src/daemon/resourceif.h [deleted file]

index 9c220ff..5ab305c 100644 (file)
@@ -19,7 +19,6 @@ srs_daemon_PUBLIC_SYMBOLS =                   \
 srs_daemon_SOURCES =                           \
                daemon/daemon.c                 \
                daemon/config.c                 \
-               daemon/resourceif.c             \
                daemon/resctl.c                 \
                daemon/client.c                 \
                daemon/plugin.c                 \
index 94006be..7a19f70 100644 (file)
@@ -30,7 +30,7 @@
 #include <murphy/common/mm.h>
 #include <murphy/common/log.h>
 
-#include "src/daemon/resourceif.h"
+#include "src/daemon/resctl.h"
 #include "src/daemon/recognizer.h"
 #include "src/daemon/client.h"
 
@@ -42,6 +42,8 @@ typedef struct {
 } voice_req_t;
 
 
+static void resource_event(srs_resctl_event_t *e, void *user_data);
+
 void client_reset_resources(srs_context_t *srs)
 {
     mrp_list_hook_t *p, *n;
@@ -61,7 +63,7 @@ void client_create_resources(srs_context_t *srs)
 
     mrp_list_foreach(&srs->clients, p, n) {
         c = mrp_list_entry(p, typeof(*c), hook);
-        resource_create(c);
+        c->rset = srs_resctl_create(srs, c->appclass, resource_event, c);
     }
 }
 
@@ -194,7 +196,7 @@ srs_client_t *client_create(srs_context_t *srs, srs_client_type_t type,
     }
 
     if (srs->rctx != NULL)
-        resource_create(c);
+        c->rset = srs_resctl_create(srs, c->appclass, resource_event, c);
 
     mrp_list_append(&srs->clients, &c->hook);
 
@@ -223,7 +225,8 @@ void client_destroy(srs_client_t *c)
                      c->appclass, c->name);
 
         srs_srec_del_client(c->srs, c);
-        resource_destroy(c);
+        srs_resctl_destroy(c->rset);
+        c->rset = NULL;
 
         mrp_list_delete(&c->hook);
 
@@ -270,56 +273,61 @@ int client_request_focus(srs_client_t *c, srs_voice_focus_t focus)
 {
     mrp_debug("client %s requested %s focus", c->id, focus_string(focus));
 
-    if (c->focus != focus) {
-        c->focus = focus;
+    if (c->requested != focus) {
+        c->requested = focus;
 
-        if (c->focus != SRS_VOICE_FOCUS_NONE)
+        if (c->requested != SRS_VOICE_FOCUS_NONE) {
             c->enabled = TRUE;
-
-        if (c->focus != SRS_VOICE_FOCUS_NONE)
-            return resource_acquire(c);
+            c->shared  = focus == SRS_VOICE_FOCUS_SHARED;
+            return srs_resctl_acquire(c->rset, c->shared);
+        }
         else
-            return resource_release(c);
+            return srs_resctl_release(c->rset);
     }
+    else
+        mrp_debug("client %s has already the requested %s focus", c->id,
+                  focus_string(focus));
 
     return TRUE;
 }
 
 
-static int notify_focus(srs_client_t *c)
+static void notify_focus(srs_client_t *c, int granted)
 {
+    srs_voice_focus_t focus;
+
     if (!c->enabled)
-        return TRUE;
+        return;
 
-    if (!c->allowed)
-        mrp_log_info("Client %s has lost voice focus.", c->id);
+    if (granted & SRS_RESCTL_MASK_SREC) {
+        if (c->shared)
+            focus = SRS_VOICE_FOCUS_SHARED;
+        else
+            focus = SRS_VOICE_FOCUS_EXCLUSIVE;
+    }
     else
-        mrp_log_info("Client %s has gained %svoice focus.", c->id,
-                     c->focus == SRS_VOICE_FOCUS_SHARED ?
-                     "shared " : "exclusive ");
+        focus = SRS_VOICE_FOCUS_NONE;
 
-    c->ops.notify_focus(c, c->focus);
+    mrp_log_info("Client %s has %s %svoice focus.", c->id,
+                 focus ? "gained" : "lost",
+                 focus ? (c->shared ? "shared " : "exclusive ") : "");
 
-    return TRUE;
+    c->ops.notify_focus(c, focus);
+
+    c->granted = granted;
 }
 
 
-void client_resource_event(srs_client_t *c, srs_resset_event_t e)
+static void resource_event(srs_resctl_event_t *e, void *user_data)
 {
-    switch (e) {
-    case SRS_RESSET_GRANTED:
-        c->allowed = TRUE;
-        break;
-
-    case SRS_RESSET_RELEASED:
-        c->allowed = FALSE;
-        break;
+    srs_client_t *c = (srs_client_t *)user_data;
 
-    default:
+    if (e->type != SRS_RESCTL_EVENT_RESOURCE)
         return;
-    }
 
-    notify_focus(c);
+    notify_focus(c, e->resource.granted);
+
+    c->granted = e->resource.granted;
 }
 
 
@@ -328,7 +336,13 @@ void client_notify_command(srs_client_t *c, int index,
                            uint32_t *start, uint32_t *end,
                            srs_audiobuf_t *audio)
 {
-    if (c->enabled && c->allowed && 0 <= index && index < c->ncommand) {
+    if (!c->enabled)
+        return;
+
+    if (!(c->granted & SRS_RESCTL_MASK_SREC))
+        return;
+
+    if (0 <= index && index < c->ncommand) {
         c->ops.notify_command(c, index, ntoken, (char **)tokens,
                               start, end, audio);
     }
index 7b30035..499ba5f 100644 (file)
@@ -33,7 +33,7 @@
 typedef struct srs_client_s srs_client_t;
 
 #include "src/daemon/context.h"
-#include "src/daemon/resourceif.h"
+#include "src/daemon/resctl.h"
 #include "src/daemon/audiobuf.h"
 #include "srs/daemon/voice.h"
 
@@ -139,13 +139,11 @@ struct srs_client_s {
     int                     ncommand;    /* number of commands */
     char                   *id;          /* client id */
     srs_context_t          *srs;         /* context back pointer */
-    mrp_res_resource_set_t *rset;        /* resource set */
+    srs_resset_t           *rset;        /* resource set */
     srs_voice_focus_t       requested;   /* requested voice focus */
-    srs_voice_focus_t       granted;     /* granted voice focus */
-    srs_voice_focus_t       focus;       /* requested voice focus */
+    int                     granted;     /* granted resources */
     int                     enabled : 1; /* interested in commands */
-    int                     allowed : 1; /* has resource granted */
-    int                     shared : 1;  /* */
+    int                     shared : 1;  /* whether voice focus is shared */
     mrp_list_hook_t         voices;      /* unfinished voice requests */
     srs_client_ops_t        ops;         /* client ops (notifications)  */
     void                   *user_data;   /* opaque client data */
@@ -186,9 +184,6 @@ void client_create_resources(srs_context_t *srs);
 /** Reset the resource sets of all clients. */
 void client_reset_resources(srs_context_t *srs);
 
-/** Deliver a resource notification event to the client. */
-void client_resource_event(srs_client_t *c, srs_resset_event_t event);
-
 /** Query voice actors. */
 int client_query_voices(srs_client_t *c, const char *language,
                         srs_voice_actor_t **actorsp);
index 838989e..2546e05 100644 (file)
@@ -58,9 +58,8 @@ struct srs_context_s {
     mrp_list_hook_t    clients;          /* connected clients */
     mrp_list_hook_t    plugins;          /* loaded plugins */
     mrp_timer_t       *rtmr;             /* resource reconnect timer */
-    mrp_res_context_t *rctx;             /* resource context */
+    srs_resctx_t      *rctx;             /* resource context */
     mrp_res_logger_t   rlog;             /* original resource logger */
-    srs_resctx_t      *resctx;           /* resource context */
     mrp_list_hook_t    recognizers;      /* speech recognition backends */
     void              *default_srec;     /* default backend */
     void              *cached_srec;      /* previously looked up backend */
index d828833..80ec4e3 100644 (file)
@@ -38,7 +38,6 @@
 
 #include "src/daemon/context.h"
 #include "src/daemon/config.h"
-#include "src/daemon/resourceif.h"
 #include "src/daemon/resctl.h"
 #include "src/daemon/plugin.h"
 #include "src/daemon/client.h"
 
 
 static void cleanup_mainloop(srs_context_t *srs);
-static void resctl_state_change(srs_resctl_event_t e, void *user_data);
+static void resctl_state_change(srs_resctl_event_t *e, void *user_data);
 
 static void cleanup_context(srs_context_t *srs)
 {
     if (srs != NULL) {
         srs_resctl_disconnect(srs);
-        resource_disconnect(srs);
         cleanup_mainloop(srs);
 
         /*
@@ -133,8 +131,7 @@ static void create_mainloop(srs_context_t *srs)
     }
 
     if (srs->pa != NULL && srs->ml != NULL) {
-        srs_resctl_connect(srs, resctl_state_change, srs, TRUE);
-        if (resource_connect(srs))
+        if (srs_resctl_connect(srs, resctl_state_change, srs, TRUE))
             return;
     }
 
@@ -185,20 +182,20 @@ static void cleanup_mainloop(srs_context_t *srs)
 }
 
 
-static void resctl_state_change(srs_resctl_event_t e, void *user_data)
+static void resctl_state_change(srs_resctl_event_t *e, void *user_data)
 {
     srs_context_t *srs = (srs_context_t *)user_data;
 
-    switch (e) {
-    case SRS_RESCTL_UP:
+    if (e->type != SRS_RESCTL_EVENT_CONNECTION)
+        return;
+
+    if (e->connection.up) {
         mrp_log_info("Resource control connection is up.");
         client_create_resources(srs);
-        break;
-
-    case SRS_RESCTL_DOWN:
+    }
+    else {
         mrp_log_info("Resource control connection is down.");
         client_reset_resources(srs);
-        break;
     }
 }
 
diff --git a/src/daemon/resctl.c b/src/daemon/resctl.c
new file mode 100644 (file)
index 0000000..9358130
--- /dev/null
@@ -0,0 +1,379 @@
+/*
+ * Copyright (c) 2012, 2013, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *   * Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *   * Neither the name of Intel Corporation nor the names of its contributors
+ *     may be used to endorse or promote products derived from this software
+ *     without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "srs/daemon/context.h"
+#include "srs/daemon/resctl.h"
+
+#define CONNECT_TIMER (5 * 1000)
+
+struct srs_resctx_s {
+    srs_context_t         *srs;          /* SRS context */
+    mrp_res_context_t     *ctx;          /* resource context */
+    mrp_list_hook_t        sets;         /* resource sets */
+    srs_resctl_event_cb_t  cb;           /* notification callback */
+    void                  *user_data;    /* opaque notification data */
+    mrp_timer_t           *t;            /* (re)connect timer */
+};
+
+
+struct srs_resset_s {
+    srs_resctx_t           *ctx;         /* resource context */
+    mrp_res_resource_set_t *set;         /* resource set */
+    mrp_list_hook_t         hook;        /* to list of resource sets */
+    srs_resctl_event_cb_t   cb;          /* notification callback */
+    void                   *user_data;   /* opaque notification data */
+    char                   *appclass;    /* application class */
+    int                     shared : 1;  /* whether currently shared */
+};
+
+
+static void context_event(mrp_res_context_t *rctx, mrp_res_error_t err,
+                          void *user_data);
+static void set_event(mrp_res_context_t *rctx,
+                      const mrp_res_resource_set_t *rset, void *user_data);
+static void stop_connect(srs_resctx_t *ctx);
+static void notify_connect(srs_resctx_t *ctx);
+static void notify_disconnect(srs_resctx_t *ctx, int requested);
+
+
+#define CONFIG_SREC  "resource.recognition"
+#define DEFAULT_SREC "speech_recognition"
+#define CONFIG_SSYN  "resource.synthesis"
+#define DEFAULT_SSYN "speech_synthesis"
+
+static const char *name_srec;
+static const char *name_ssyn;
+
+
+static void get_resource_names(srs_cfg_t *cfg)
+{
+    name_srec = srs_get_string_config(cfg, CONFIG_SREC, DEFAULT_SREC);
+    name_ssyn = srs_get_string_config(cfg, CONFIG_SSYN, DEFAULT_SSYN);
+
+    mrp_log_info("Using resource '%s' for speech recognition.", name_srec);
+    mrp_log_info("Using resoruce '%s' for speech synthesis.", name_ssyn);
+}
+
+
+static int try_connect(srs_resctx_t *ctx)
+{
+    srs_context_t *srs = ctx->srs;
+
+    ctx->ctx = mrp_res_create(srs->ml, context_event, ctx);
+
+    if (ctx->ctx != NULL)
+        return TRUE;
+    else
+        return FALSE;
+}
+
+
+static void connect_timer_cb(mrp_timer_t *t, void *user_data)
+{
+    srs_resctx_t *ctx = (srs_resctx_t *)user_data;
+
+    MRP_UNUSED(t);
+
+    if (try_connect(ctx))
+        stop_connect(ctx);
+}
+
+
+static int start_connect(srs_resctx_t *ctx)
+{
+    srs_context_t *srs = ctx->srs;
+
+    ctx->t = mrp_add_timer(srs->ml, CONNECT_TIMER, connect_timer_cb, ctx);
+
+    return (ctx->t != NULL);
+}
+
+
+static void stop_connect(srs_resctx_t *ctx)
+{
+    mrp_del_timer(ctx->t);
+    ctx->t = NULL;
+}
+
+
+int srs_resctl_connect(srs_context_t *srs, srs_resctl_event_cb_t cb,
+                       void *user_data, int reconnect)
+{
+    srs_resctx_t *ctx;
+
+    if (srs->rctx != NULL)
+        return FALSE;
+
+    ctx = mrp_allocz(sizeof(*ctx));
+
+    if (ctx == NULL)
+        return FALSE;
+
+    mrp_list_init(&ctx->sets);
+    ctx->srs       = srs;
+    ctx->cb        = cb;
+    ctx->user_data = user_data;
+    srs->rctx      = ctx;
+
+    if (try_connect(ctx) || (reconnect && start_connect(ctx)))
+        return TRUE;
+    else {
+        mrp_free(ctx);
+        srs->rctx = NULL;
+        return FALSE;
+    }
+}
+
+
+void srs_resctl_disconnect(srs_context_t *srs)
+{
+    srs_resctx_t *ctx = srs->rctx;
+
+    if (ctx != NULL) {
+        stop_connect(ctx);
+        mrp_res_destroy(ctx->ctx);
+        ctx->ctx = NULL;
+        notify_disconnect(ctx, TRUE);
+
+        mrp_free(ctx);
+        srs->rctx = NULL;
+    }
+}
+
+
+static void context_event(mrp_res_context_t *rctx, mrp_res_error_t err,
+                          void *user_data)
+{
+    srs_resctx_t  *ctx = (srs_resctx_t *)user_data;
+    srs_context_t *srs = ctx->srs;
+
+    MRP_UNUSED(err);
+
+    switch (rctx->state) {
+    case MRP_RES_CONNECTED:
+        mrp_log_info("Resource context connection is up.");
+        stop_connect(ctx);
+        notify_connect(ctx);
+        break;
+
+    case MRP_RES_DISCONNECTED:
+        mrp_log_info("Resource context connection is down.");
+        notify_disconnect(ctx, FALSE);
+        start_connect(ctx);
+    }
+}
+
+
+static void notify_connect(srs_resctx_t *ctx)
+{
+    srs_resctl_event_t e;
+
+    if (ctx->cb != NULL) {
+        e.connection.type = SRS_RESCTL_EVENT_CONNECTION;
+        e.connection.up   = TRUE;
+        ctx->cb(&e, ctx->user_data);
+    }
+}
+
+
+static void notify_disconnect(srs_resctx_t *ctx, int requested)
+{
+    mrp_list_hook_t    *p, *n;
+    srs_resset_t       *set;
+    srs_resctl_event_t  e;
+
+    if (!requested) {
+        if (ctx->cb != NULL) {
+            e.connection.type = SRS_RESCTL_EVENT_CONNECTION;
+            e.connection.up   = FALSE;
+            ctx->cb(&e, ctx->user_data);
+        }
+    }
+
+    e.type = SRS_RESCTL_EVENT_DESTROYED;
+    mrp_list_foreach(&ctx->sets, p, n) {
+        set = mrp_list_entry(p, typeof(*set), hook);
+
+        if (set->cb != NULL)
+            set->cb(&e, set->user_data);
+
+        mrp_list_delete(&set->hook);
+        mrp_free(set);
+    }
+}
+
+
+srs_resset_t *srs_resctl_create(srs_context_t *srs, char *appclass,
+                                srs_resctl_event_cb_t cb, void *user_data)
+{
+    srs_resctx_t *ctx = srs->rctx;
+    srs_resset_t *set;
+    int           shared;
+
+    if (ctx == NULL)
+        return NULL;
+
+    set = mrp_allocz(sizeof(*set));
+
+    if (set == NULL)
+        return NULL;
+
+    mrp_list_init(&set->hook);
+    set->ctx       = ctx;
+    set->cb        = cb;
+    set->user_data = user_data;
+    set->shared    = shared = TRUE;
+    set->appclass  = mrp_strdup(appclass);
+
+    set->set = mrp_res_create_resource_set(ctx->ctx, appclass, set_event, set);
+
+    if (set->set == NULL)
+        goto fail;
+
+    if (name_srec == NULL || name_ssyn == NULL)
+        get_resource_names(srs->settings);
+
+    if (!mrp_res_create_resource(ctx->ctx, set->set, name_srec, TRUE, shared) ||
+        !mrp_res_create_resource(ctx->ctx, set->set, name_ssyn, TRUE, shared))
+        goto fail;
+
+    mrp_list_append(&ctx->sets, &set->hook);
+
+    return set;
+
+ fail:
+    if (set != NULL) {
+        if (set->set != NULL)
+            mrp_res_delete_resource_set(ctx->ctx, set->set);
+
+        mrp_free(set->appclass);
+        mrp_free(set);
+    }
+
+    return NULL;
+}
+
+
+void srs_resctl_destroy(srs_resset_t *set)
+{
+    srs_resctx_t *ctx = set ? set->ctx : NULL;
+
+    if (set != NULL) {
+        if (ctx != NULL)
+            mrp_res_delete_resource_set(ctx->ctx, set->set);
+
+        mrp_list_delete(&set->hook);
+        mrp_free(set->appclass);
+        mrp_free(set);
+    }
+}
+
+
+int srs_resctl_acquire(srs_resset_t *set, int shared)
+{
+    srs_resctx_t *ctx = set ? set->ctx : NULL;
+
+    if (ctx == NULL || ctx->ctx == NULL || set == NULL || set->set == NULL)
+        return FALSE;
+
+    if (!!shared != !!set->shared) {
+        mrp_res_delete_resource_set(ctx->ctx, set->set);
+        set->shared = !!shared;
+        set->set    = NULL;
+
+        set->set = mrp_res_create_resource_set(ctx->ctx, set->appclass,
+                                               set_event, set);
+
+        if (set->set == NULL)
+            goto fail;
+
+        if (!mrp_res_create_resource(ctx->ctx, set->set,
+                                     name_srec, TRUE, shared) ||
+            !mrp_res_create_resource(ctx->ctx, set->set,
+                                     name_ssyn, TRUE, shared))
+            goto fail;
+    }
+
+    if (mrp_res_acquire_resource_set(ctx->ctx, set->set) == 0)
+        return TRUE;
+    else
+        /* fall through */;
+
+ fail:
+    if (set->set != NULL) {
+        mrp_res_delete_resource_set(ctx->ctx, set->set);
+        set->set = NULL;
+    }
+
+    return FALSE;
+}
+
+
+int srs_resctl_release(srs_resset_t *set)
+{
+    srs_resctx_t *ctx = set ? set->ctx : NULL;
+
+    if (ctx == NULL || ctx->ctx == NULL || set == NULL || set->set == NULL)
+        return FALSE;
+
+    if (mrp_res_release_resource_set(ctx->ctx, set->set) >= 0)
+        return TRUE;
+    else
+        return FALSE;
+}
+
+
+static void set_event(mrp_res_context_t *rctx,
+                      const mrp_res_resource_set_t *rset, void *user_data)
+{
+    srs_resset_t       *set  = (srs_resset_t *)user_data;
+    mrp_res_resource_t *srec, *ssyn;
+    srs_resctl_event_t  e;
+
+    srec = mrp_res_get_resource_by_name(rctx, rset, name_srec);
+    ssyn = mrp_res_get_resource_by_name(rctx, rset, name_ssyn);
+
+    if (srec == NULL || ssyn == NULL || srec->state != ssyn->state) {
+        mrp_log_error("Inconsistent resources in set.");
+        return;
+    }
+
+    if (set->cb == NULL)
+        return;
+
+    e.resource.type    = SRS_RESCTL_EVENT_RESOURCE;
+    e.resource.granted = 0;
+
+    if (srec->state)
+        e.resource.granted |= SRS_RESCTL_MASK_SREC;
+    if (ssyn->state)
+        e.resource.granted |= SRS_RESCTL_MASK_SYNT;
+
+    set->cb(&e, set->user_data);
+}
diff --git a/src/daemon/resctl.h b/src/daemon/resctl.h
new file mode 100644 (file)
index 0000000..7dfd205
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2012, 2013, Intel Corporation
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ *   * Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *   * Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *   * Neither the name of Intel Corporation nor the names of its contributors
+ *     may be used to endorse or promote products derived from this software
+ *     without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __SRS_DAEMON_RESCTL_H__
+#define __SRS_DAEMON_RESCTL_H__
+
+#include <murphy/common/list.h>
+
+typedef struct srs_resctx_s srs_resctx_t;
+typedef struct srs_resset_s srs_resset_t;
+
+#include "srs/daemon/context.h"
+
+typedef enum {
+    SRS_RESCTL_EVENT_UNKNOWN = 0,
+    SRS_RESCTL_EVENT_CONNECTION,         /* connection up/down event */
+    SRS_RESCTL_EVENT_RESOURCE,           /* resource state change event */
+    SRS_RESCTL_EVENT_DESTROYED,          /* set destroyed event */
+} srs_resctl_event_type_t;
+
+typedef enum {
+    SRS_RESCTL_MASK_NONE = 0x0,          /* no resources */
+    SRS_RESCTL_MASK_SREC = 0x1,          /* speech recognition */
+    SRS_RESCTL_MASK_SYNT = 0x2,          /* speech synthesis */
+} srs_resctl_mask_t;
+
+typedef union {
+    srs_resctl_event_type_t type;        /* event type */
+    struct {                             /* connection event */
+        srs_resctl_event_type_t type;    /* SRS_RESCTL_CONNECTION */
+        int                     up;      /* whether connected */
+    } connection;
+    struct {                             /* resource state change event */
+        srs_resctl_event_type_t type;    /* SRS_RESCTL_STATE */
+        srs_resctl_mask_t       granted; /* mask of granted resources */
+    } resource;
+} srs_resctl_event_t;
+
+
+
+typedef void (*srs_resctl_event_cb_t)(srs_resctl_event_t *e, void *user_data);
+
+int srs_resctl_connect(srs_context_t *srs, srs_resctl_event_cb_t cb,
+                       void *user_data, int reconnect);
+void srs_resctl_disconnect(srs_context_t *srs);
+
+srs_resset_t *srs_resctl_create(srs_context_t *srs, char *appclass,
+                                srs_resctl_event_cb_t cb, void *user_data);
+void srs_resctl_destroy(srs_resset_t *set);
+
+int srs_resctl_acquire(srs_resset_t *set, int shared);
+int srs_resctl_release(srs_resset_t *set);
+
+#endif /* __SRS_DAEMON_RESCTL_H__ */
diff --git a/src/daemon/resourceif.c b/src/daemon/resourceif.c
deleted file mode 100644 (file)
index a3107e2..0000000
+++ /dev/null
@@ -1,220 +0,0 @@
-/*
- * Copyright (c) 2012, Intel Corporation
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- *   * Redistributions of source code must retain the above copyright notice,
- *     this list of conditions and the following disclaimer.
- *   * Redistributions in binary form must reproduce the above copyright
- *     notice, this list of conditions and the following disclaimer in the
- *     documentation and/or other materials provided with the distribution.
- *   * Neither the name of Intel Corporation nor the names of its contributors
- *     may be used to endorse or promote products derived from this software
- *     without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "src/daemon/client.h"
-#include "src/daemon/resourceif.h"
-
-#define RECONNECT_TIMER 5000
-#define RESOURCE        "speech_recognition"
-
-static int start_reconnect(srs_context_t *srs);
-static void stop_reconnect(srs_context_t *srs);
-static int try_connect(srs_context_t *srs);
-
-static void reconnect(mrp_timer_t *t, void *user_data)
-{
-    srs_context_t *srs = (srs_context_t *)user_data;
-
-    MRP_UNUSED(t);
-
-    if (try_connect(srs))
-        stop_reconnect(srs);
-}
-
-
-static int start_reconnect(srs_context_t *srs)
-{
-    srs->rtmr = mrp_add_timer(srs->ml, RECONNECT_TIMER, reconnect, srs);
-
-    if (srs->rtmr != NULL)
-        return TRUE;
-    else
-        return FALSE;
-}
-
-
-static void stop_reconnect(srs_context_t *srs)
-{
-    mrp_del_timer(srs->rtmr);
-    srs->rtmr = NULL;
-}
-
-
-static void resctx_event(mrp_res_context_t *ctx, mrp_res_error_t err,
-                         void *user_data)
-{
-    srs_context_t *srs = (srs_context_t *)user_data;
-
-    MRP_UNUSED(err);
-
-    switch (ctx->state) {
-    case MRP_RES_CONNECTED:
-        mrp_log_info("Resource context connection is up.");
-        stop_reconnect(srs);
-        break;
-
-    case MRP_RES_DISCONNECTED:
-        mrp_log_info("Resource context connection is down.");
-        client_reset_resources(srs);
-        start_reconnect(srs);
-    }
-}
-
-
-static int try_connect(srs_context_t *srs)
-{
-    srs->rctx = mrp_res_create(srs->ml, resctx_event, srs);
-
-    if (srs->rctx != NULL)
-        return TRUE;
-    else
-        return FALSE;
-}
-
-
-int resource_connect(srs_context_t *srs)
-{
-    if (try_connect(srs) || start_reconnect(srs))
-        return TRUE;
-    else
-        return FALSE;
-}
-
-
-void resource_disconnect(srs_context_t *srs)
-{
-    stop_reconnect(srs);
-
-    if (srs->rctx != NULL) {
-        mrp_res_destroy(srs->rctx);
-        srs->rctx = NULL;
-    }
-}
-
-
-static void resource_event(mrp_res_context_t *rctx,
-                           const mrp_res_resource_set_t *rset, void *user_data)
-{
-    srs_client_t       *c = (srs_client_t *)user_data;
-    mrp_res_resource_t *r = mrp_res_get_resource_by_name(rctx, rset, RESOURCE);
-    srs_resset_event_t  e;
-
-    mrp_debug("received resource event for set %p", rset);
-
-    if (r != NULL) {
-        switch (r->state) {
-        case MRP_RES_RESOURCE_ACQUIRED:
-            e = SRS_RESSET_GRANTED;
-            break;
-        case MRP_RES_RESOURCE_LOST:
-            e = SRS_RESSET_RELEASED;
-            break;
-        default:
-            return;
-        }
-
-        client_resource_event(c, e);
-    }
-}
-
-
-int resource_create(srs_client_t *c)
-{
-    mrp_res_context_t      *rctx = c->srs->rctx;
-    mrp_res_resource_set_t *rset;
-    bool                    shared;
-
-    rset = mrp_res_create_resource_set(rctx, c->appclass, resource_event, c);
-
-    if (rset != NULL) {
-        c->rset = rset;
-        shared  = (c->focus != SRS_VOICE_FOCUS_EXCLUSIVE);
-
-        if (!mrp_res_create_resource(rctx, rset, RESOURCE, TRUE, shared)) {
-            resource_destroy(c);
-
-            return FALSE;
-        }
-
-        if (c->focus != SRS_VOICE_FOCUS_NONE) {
-            c->shared = !!shared;
-            if (!resource_acquire(c)) {
-                resource_destroy(c);
-
-                return FALSE;
-            }
-            else
-                return TRUE;
-        }
-    }
-
-    return FALSE;
-}
-
-
-void resource_destroy(srs_client_t *c)
-{
-    if (c->srs->rctx != NULL && c->rset != NULL)
-        mrp_res_delete_resource_set(c->srs->rctx, c->rset);
-
-    c->rset   = NULL;
-    c->shared =  0;
-}
-
-
-int resource_acquire(srs_client_t *c)
-{
-    if (c->srs->rctx == NULL)
-        return FALSE;
-
-    if (c->rset == NULL && !resource_create(c))
-        return FALSE;
-
-    if (c->focus != SRS_VOICE_FOCUS_SHARED && c->shared) {
-        resource_destroy(c);
-        return resource_create(c);
-    }
-
-    if (mrp_res_acquire_resource_set(c->srs->rctx, c->rset) >= 0)
-        return TRUE;
-    else
-        return FALSE;
-}
-
-
-int resource_release(srs_client_t *c)
-{
-    if (c->srs->rctx == NULL || c->rset == NULL)
-        return FALSE;
-
-    if (mrp_res_release_resource_set(c->srs->rctx, c->rset) >= 0)
-        return TRUE;
-    else
-        return FALSE;
-}
diff --git a/src/daemon/resourceif.h b/src/daemon/resourceif.h
deleted file mode 100644 (file)
index 49f5d8d..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Copyright (c) 2012, Intel Corporation
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are
- * met:
- *
- *   * Redistributions of source code must retain the above copyright notice,
- *     this list of conditions and the following disclaimer.
- *   * Redistributions in binary form must reproduce the above copyright
- *     notice, this list of conditions and the following disclaimer in the
- *     documentation and/or other materials provided with the distribution.
- *   * Neither the name of Intel Corporation nor the names of its contributors
- *     may be used to endorse or promote products derived from this software
- *     without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef __SRS_DAEMON_RESOURCEIF_H__
-#define __SRS_DAEMON_RESOURCEIF_H__
-
-typedef enum {
-    SRS_RESSET_UNKNOWN = 0,
-    SRS_RESSET_GRANTED,                  /* resource set granted */
-    SRS_RESSET_RELEASED,                 /* resource set released/lost */
-} srs_resset_event_t;
-
-#include "src/daemon/client.h"
-
-int resource_connect(srs_context_t *srs);
-void resource_disconnect(srs_context_t *srs);
-
-typedef void (*srs_resset_cb_t)(srs_client_t *c, srs_resset_event_t event);
-
-int resource_create(srs_client_t *c);
-void resource_destroy(srs_client_t *c);
-
-int resource_acquire(srs_client_t *c);
-int resource_release(srs_client_t *c);
-
-#endif /* __SRS_DAEMON_RESOURCEIF_H__ */