device-manager-db: Add functions to write/read preference setting to database 81/183981/1
authorSangchul Lee <sc11.lee@samsung.com>
Thu, 12 Jul 2018 08:47:48 +0000 (17:47 +0900)
committerSangchul Lee <sc11.lee@samsung.com>
Fri, 13 Jul 2018 01:04:14 +0000 (10:04 +0900)
Files are added as below:
 device-manager-db.c
 device-manager-db-priv.h
 device-manager-priv.h

[Version] 11.1.17
[Issue Type] New feature

Change-Id: If455049846440524c7dcf4b43049438249ed6254
Signed-off-by: Sangchul Lee <sc11.lee@samsung.com>
Makefile.am
packaging/pulseaudio-modules-tizen.spec
src/device-manager-db-priv.h [new file with mode: 0644]
src/device-manager-db.c [new file with mode: 0644]
src/device-manager-priv.h [new file with mode: 0644]
src/device-manager.c

index 3a10875..2b83bf9 100644 (file)
@@ -109,7 +109,9 @@ module_tizenaudio_policy_la_SOURCES = \
           src/stream-manager-dbus.c   src/stream-manager-dbus-priv.h   \
           src/stream-manager-restriction.c   src/stream-manager-restriction-priv.h   \
           src/stream-manager-filter.c   src/stream-manager-volume-filter.h   \
-          src/device-manager.c   src/device-manager.h   src/tizen-device.c   src/tizen-device.h   src/tizen-device-def.c  src/tizen-device-def.h  \
+          src/device-manager.c   src/device-manager.h   src/device-manager-priv.h   \
+          src/device-manager-db.c   src/device-manager-db-priv.h  \
+          src/tizen-device.c   src/tizen-device.h   src/tizen-device-def.c  src/tizen-device-def.h  \
           src/subscribe-observer.c   src/subscribe-observer.h
 module_tizenaudio_policy_la_LDFLAGS = $(MODULE_LDFLAGS) -L$(pulsemodlibexecdir)
 module_tizenaudio_policy_la_LIBADD = $(MODULE_LIBADD) $(DBUS_LIBS) $(VCONF_LIBS) $(INIPARSER_LIBS) $(LIBJSON_LIBS) libhal-interface.la libcommunicator.la
index af044a5..32a3a4e 100644 (file)
@@ -1,6 +1,6 @@
 Name:             pulseaudio-modules-tizen
 Summary:          Pulseaudio modules for Tizen
-Version:          11.1.16
+Version:          11.1.17
 Release:          0
 Group:            Multimedia/Audio
 License:          LGPL-2.1+
diff --git a/src/device-manager-db-priv.h b/src/device-manager-db-priv.h
new file mode 100644 (file)
index 0000000..5bd20b8
--- /dev/null
@@ -0,0 +1,38 @@
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2018 Sangchul Lee <sc11.lee@samsung.com>
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifndef foodevicemanagerdbprivfoo
+#define foodevicemanagerdbprivfoo
+
+#include "device-manager.h"
+
+typedef struct _prefer_entry {
+    bool avoid_resampling;
+    pa_sample_format_t format;
+    uint32_t rate;
+} prefer_entry;
+
+int32_t init_database(pa_device_manager *dm);
+int32_t write_prefer_entry(pa_device_manager *dm, const char *key, prefer_entry *e);
+prefer_entry *read_prefer_entry(pa_device_manager *dm, const char *key); /* prefer_entry should be freed by using pa_xfree() */
+void deinit_database(pa_device_manager *dm);
+
+#endif
diff --git a/src/device-manager-db.c b/src/device-manager-db.c
new file mode 100644 (file)
index 0000000..0ed37a8
--- /dev/null
@@ -0,0 +1,142 @@
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2018 Sangchul Lee <sc11.lee@samsung.com>
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <errno.h>
+#include <pulsecore/core-error.h>
+#include <pulsecore/core-util.h>
+#include "device-manager-priv.h"
+#include "device-manager-db-priv.h"
+
+static void dump_prefer_entry(prefer_entry *e) {
+    pa_assert(e);
+
+    pa_log_info("device preference 'avoid-resampling(%d), format(%s), rate(%d)'",
+                 e->avoid_resampling, pa_sample_format_to_string(e->format), e->rate);
+}
+
+int32_t init_database(pa_device_manager *dm) {
+    char *db_path;
+
+    pa_assert(dm);
+
+    if (!(db_path = pa_state_path("device-preferences", true))) {
+        pa_log_error("failed to get path for database");
+        return -1;
+    }
+
+    if (!(dm->database = pa_database_open(db_path, true))) {
+        pa_log_error("failed to open database '%s': %s", db_path, pa_cstrerror(errno));
+        pa_xfree(db_path);
+        return -1;
+    }
+    pa_log_info("database file '%s'", db_path);
+
+    pa_xfree(db_path);
+
+    return 0;
+}
+
+int32_t write_prefer_entry(pa_device_manager *dm, const char *key, prefer_entry *e) {
+    pa_tagstruct *t;
+    pa_datum db_key;
+    pa_datum db_data;
+    int ret;
+
+    pa_assert(dm);
+    pa_assert(dm->database);
+    pa_assert(key);
+    pa_assert(e);
+
+    t = pa_tagstruct_new();
+    pa_tagstruct_put(t,
+                     PA_TAG_BOOLEAN, e->avoid_resampling,
+                     PA_TAG_U8, e->format,
+                     PA_TAG_U32, e->rate,
+                     PA_TAG_INVALID);
+
+    db_key.data = (char*)key;
+    db_key.size = strlen(key);
+
+    db_data.data = (void *)pa_tagstruct_data(t, &db_data.size);
+
+    if ((ret = pa_database_set(dm->database, &db_key, &db_data, true))) {
+        pa_log_error("failed to write preference to db");
+    } else {
+        dump_prefer_entry(e);
+        pa_database_sync(dm->database);
+        pa_log_info("write sync successfully");
+    }
+
+    pa_tagstruct_free(t);
+
+    return ret;
+}
+
+prefer_entry* read_prefer_entry(pa_device_manager *dm, const char *key) {
+    pa_tagstruct *t;
+    prefer_entry *e;
+    pa_datum db_key;
+    pa_datum db_data;
+
+    pa_assert(dm);
+    pa_assert(dm->database);
+    pa_assert(key);
+
+    pa_zero(db_data);
+
+    db_key.data = (char*)key;
+    db_key.size = strlen(key);
+
+    if (!pa_database_get(dm->database, &db_key, &db_data)) {
+        pa_log_error("failed to get data with key %s", key);
+        return NULL;
+    }
+
+    t = pa_tagstruct_new_fixed(db_data.data, db_data.size);
+
+    e = pa_xnew0(prefer_entry, 1);
+
+    if (pa_tagstruct_get(t,
+                         PA_TAG_BOOLEAN, &e->avoid_resampling,
+                         PA_TAG_U8, &e->format,
+                         PA_TAG_U32, &e->rate,
+                         PA_TAG_INVALID)) {
+        pa_log_error("failed to get preference values from tagstruct");
+        pa_xfree(e);
+    }
+
+    pa_tagstruct_free(t);
+    pa_datum_free(&db_data);
+
+    dump_prefer_entry(e);
+
+    return e;
+}
+
+void deinit_database(pa_device_manager *dm) {
+    pa_assert(dm);
+
+    if (dm->database)
+        pa_database_close(dm->database);
+}
diff --git a/src/device-manager-priv.h b/src/device-manager-priv.h
new file mode 100644 (file)
index 0000000..32dc8d2
--- /dev/null
@@ -0,0 +1,79 @@
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2018 Sangchul Lee <sc11.lee@samsung.com>
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifndef foodevicemanagerprivfoo
+#define foodevicemanagerprivfoo
+
+#ifdef HAVE_DBUS
+#include <pulsecore/dbus-shared.h>
+#include <pulsecore/dbus-util.h>
+#include <pulsecore/protocol-dbus.h>
+#endif
+#include <pulsecore/hashmap.h>
+#include <pulsecore/idxset.h>
+#include <pulsecore/database.h>
+#include "communicator.h"
+#include "hal-interface.h"
+
+/*
+    Structure to save parsed information about device-file.
+*/
+struct device_file_map
+{
+    /* { key:device_string -> value:device_file_prop } */
+    pa_idxset *playback;
+    pa_idxset *capture;
+};
+
+struct pa_device_manager {
+    PA_REFCNT_DECLARE;
+
+    pa_core *core;
+    pa_hook_slot *sink_put_hook_slot, *sink_unlink_hook_slot, *sink_state_changed_slot;
+    pa_hook_slot *source_put_hook_slot, *source_unlink_hook_slot, *source_state_changed_slot;
+    pa_hook_slot *comm_hook_device_connection_changed_slot;
+    pa_hook_slot *comm_hook_device_state_changed_slot;
+    pa_hook_slot *comm_hook_device_running_changed_slot;
+    pa_communicator *comm;
+    pa_hal_interface *hal_interface;
+
+    /*
+       Idxset for save parsed information about device-type.
+       { device_type_info }
+    */
+    pa_idxset *type_infos;
+    /* For save Parsed information about device-file */
+    struct device_file_map *file_map;
+
+    /* device list */
+    pa_idxset *device_list;
+    /*
+       Hashmap for save statuses got through dbus.
+       { key:device_type -> value:(audio_detected_type_t or device_detected_t) }
+    */
+    pa_idxset *device_status;
+    pa_dbus_connection *dbus_conn;
+
+    /* database for device preferences */
+    pa_database *database;
+};
+
+#endif
index 49fa516..9d52a00 100644 (file)
 #include <pulsecore/namereg.h>
 #include <pulsecore/shared.h>
 #include <pulsecore/dynarray.h>
-#include <pulsecore/hashmap.h>
 
 #include <vconf.h>
 #include <vconf-keys.h>
-#ifdef HAVE_DBUS
-#include <pulsecore/dbus-shared.h>
-#include <pulsecore/dbus-util.h>
-#include <pulsecore/protocol-dbus.h>
-#endif
 
-#include "communicator.h"
-#include "hal-interface.h"
 #include "stream-manager.h"
 #include "device-manager.h"
+#include "device-manager-priv.h"
+#include "device-manager-db-priv.h"
 
 #define SHARED_DEVICE_MANAGER "tizen-device-manager"
 
@@ -343,16 +337,6 @@ typedef enum dm_device_changed_into_type {
     DM_DEVICE_CHANGED_INFO_MAX
 } dm_device_changed_info_t;
 
-
-/*
-    Structure to save parsed information about device-file.
-*/
-struct device_file_map {
-    /* { key:device_string -> value:device_file_prop } */
-    pa_idxset *playback;
-    pa_idxset *capture;
-};
-
 /***************** structures for static information get from json *********/
 
 /*
@@ -402,36 +386,8 @@ struct pulse_device_prop {
     /* For save that this devie_type is activated or not on sink/source */
     int status;
 };
-/******************************************************************************/
-struct pa_device_manager {
-    PA_REFCNT_DECLARE;
-
-    pa_core *core;
-    pa_hook_slot *sink_put_hook_slot, *sink_unlink_hook_slot, *sink_state_changed_slot;
-    pa_hook_slot *source_put_hook_slot, *source_unlink_hook_slot, *source_state_changed_slot;
-    pa_hook_slot *comm_hook_device_connection_changed_slot;
-    pa_hook_slot *comm_hook_device_state_changed_slot;
-    pa_hook_slot *comm_hook_device_running_changed_slot;
-    pa_communicator *comm;
-    pa_hal_interface *hal_interface;
 
-    /*
-       Idxset for save parsed information about device-type.
-       { device_type_info }
-    */
-    pa_idxset *type_infos;
-    /* For save Parsed information about device-file */
-    struct device_file_map *file_map;
-
-    /* device list */
-    pa_idxset *device_list;
-    /*
-       Hashmap for save statuses got through dbus.
-       { key:device_type -> value:(audio_detected_type_t or device_detected_t) }
-    */
-    pa_idxset *device_status;
-    pa_dbus_connection *dbus_conn;
-};
+/******************************************************************************/
 
 struct composite_type {
     const char *type;
@@ -3993,12 +3949,12 @@ static void endpoint_done(pa_device_manager *dm) {
     }
 }
 
-static void dbus_init(pa_device_manager *dm) {
+static void init_dbus(pa_device_manager *dm) {
     DBusError error;
     pa_dbus_connection *connection = NULL;
 
     pa_assert(dm);
-    pa_log_info("Dbus init");
+    pa_log_info("init Dbus");
     dbus_error_init(&error);
 
     if (!(connection = pa_dbus_bus_get(dm->core, DBUS_BUS_SYSTEM, &error)) || dbus_error_is_set(&error)) {
@@ -4024,10 +3980,10 @@ fail:
     dbus_error_free(&error);
 }
 
-static void dbus_deinit(pa_device_manager *dm) {
+static void deinit_dbus(pa_device_manager *dm) {
     pa_assert(dm);
 
-    pa_log_info("Dbus deinit");
+    pa_log_info("deinit Dbus");
 
     endpoint_done(dm);
     unwatch_signals(dm);
@@ -4384,7 +4340,7 @@ pa_device_manager* pa_device_manager_get(pa_core *c) {
     dm->device_list = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
     dm->device_status = pa_idxset_new(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func);
 
-    dbus_init(dm);
+    init_dbus(dm);
 
     dm->sink_put_hook_slot = pa_hook_connect(&dm->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_LATE+10, (pa_hook_cb_t) sink_put_hook_callback, dm);
     dm->sink_unlink_hook_slot = pa_hook_connect(&dm->core->hooks[PA_CORE_HOOK_SINK_UNLINK], PA_HOOK_EARLY, (pa_hook_cb_t) sink_unlink_hook_callback, dm);
@@ -4489,7 +4445,7 @@ void pa_device_manager_unref(pa_device_manager *dm) {
     if (dm->device_status)
         pa_idxset_free(dm->device_status, NULL);
 
-    dbus_deinit(dm);
+    deinit_dbus(dm);
 
     if (dm->core)
         pa_shared_remove(dm->core, SHARED_DEVICE_MANAGER);