Add Vconf service
authorLukasz Stanislawski <l.stanislaws@samsung.com>
Tue, 10 Mar 2020 11:27:01 +0000 (12:27 +0100)
committerLukasz Stanislawski/IoT & UI Sample (PLT) /SRPOL/Engineer/Samsung Electronics <l.stanislaws@samsung.com>
Tue, 17 Mar 2020 07:40:34 +0000 (08:40 +0100)
Oobe/OobeCommon/Services/Interop/Interop.VConf.cs [new file with mode: 0644]
Oobe/OobeCommon/Services/Vconf.cs [new file with mode: 0644]
Oobe/OobeCommon/Utils/ExceptionFactory.cs [new file with mode: 0644]

diff --git a/Oobe/OobeCommon/Services/Interop/Interop.VConf.cs b/Oobe/OobeCommon/Services/Interop/Interop.VConf.cs
new file mode 100644 (file)
index 0000000..10bafee
--- /dev/null
@@ -0,0 +1,35 @@
+using System;
+using System.Runtime.InteropServices;
+using static Oobe.Common.Services.Vconf;
+
+internal static partial class Interop
+{
+    internal static partial class Vconf
+    {
+        private const string LIBRARY_VCONF = "libvconf.so.0";
+
+        [DllImport(LIBRARY_VCONF, EntryPoint = "vconf_get_bool")]
+        internal static extern int VconfGetBool(string key, out bool val);
+
+        [DllImport(LIBRARY_VCONF, EntryPoint = "vconf_set_bool")]
+        internal static extern int VconfSetBool(string key, bool intval);
+
+        [DllImport(LIBRARY_VCONF, EntryPoint = "vconf_get_int")]
+        internal static extern int VconfGetInt(string key, out int val);
+
+        [DllImport(LIBRARY_VCONF, EntryPoint = "vconf_set_int")]
+        internal static extern int VconfSetInt(string key, int intval);
+
+        [DllImport(LIBRARY_VCONF, EntryPoint = "vconf_get_str")]
+        internal static extern string VconfGetStr(string key);
+
+        [DllImport(LIBRARY_VCONF, EntryPoint = "vconf_set_str")]
+        internal static extern int VconfSetStr(string key, string value);
+
+        [DllImport(LIBRARY_VCONF, EntryPoint = "vconf_notify_key_changed")]
+        internal static extern void VconfNotifyKeyChanged(string key, NotificationCallback callback, IntPtr userData);
+
+        [DllImport(LIBRARY_VCONF, EntryPoint = "vconf_ignore_key_changed")]
+        internal static extern void VconfIgnoreKeyChanged(string key, NotificationCallback callback);
+    }
+}
diff --git a/Oobe/OobeCommon/Services/Vconf.cs b/Oobe/OobeCommon/Services/Vconf.cs
new file mode 100644 (file)
index 0000000..5a6b955
--- /dev/null
@@ -0,0 +1,127 @@
+using System;
+using System.Runtime.InteropServices;
+using Oobe.Common.Utils;
+using static Interop.Vconf;
+
+namespace Oobe.Common.Services
+{
+    /// <summary>
+    /// This class provides the API to use Vconf methods.
+    /// Vconf is a global configuration registry for the device.
+    /// </summary>
+    public static class Vconf
+    {
+        /// <summary>
+        /// Delegate for notification callbacks.
+        /// </summary>
+        /// <param name="node">Pointer to event node.</param>
+        /// <param name="userData">Pointer to event user data.</param>
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        public delegate void NotificationCallback(IntPtr node, IntPtr userData);
+
+        /// <summary>
+        /// Gets a boolean value from Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <returns>A value assigned to the specified key.</returns>
+        public static bool GetBool(string key)
+        {
+            int errorCode = VconfGetBool(key, out bool value);
+            if (errorCode != 0)
+            {
+                throw ExceptionFactory.GetException(errorCode);
+            }
+
+            return value;
+        }
+
+        /// <summary>
+        /// Sets a boolean value in Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <param name="value">The value to be set.</param>
+        public static void SetBool(string key, bool value)
+        {
+            int errorCode = VconfSetBool(key, value);
+            if (errorCode != 0)
+            {
+                throw ExceptionFactory.GetException(errorCode);
+            }
+        }
+
+        /// <summary>
+        /// Gets an integer value from Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <returns>A value assigned to the specified key.</returns>
+        public static int GetInt(string key)
+        {
+            int errorCode = VconfGetInt(key, out int value);
+            if (errorCode != 0)
+            {
+                throw ExceptionFactory.GetException(errorCode);
+            }
+
+            return value;
+        }
+
+        /// <summary>
+        /// Sets an integer value in Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <param name="value">The value to be set.</param>
+        public static void SetInt(string key, int value)
+        {
+            int errorCode = VconfSetInt(key, value);
+            if (errorCode != 0)
+            {
+                throw ExceptionFactory.GetException(errorCode);
+            }
+        }
+
+        /// <summary>
+        /// Gets a string value from Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <returns>A value assigned to the specified key.</returns>
+        public static string GetString(string key)
+        {
+            return VconfGetStr(key);
+        }
+
+        /// <summary>
+        /// Sets a string value in Vconf.
+        /// </summary>
+        /// <param name="key">The key in Vconf.</param>
+        /// <param name="value">The value to be set.</param>
+        public static void SetString(string key, string value)
+        {
+            int errorCode = VconfSetStr(key, value);
+            if (errorCode != 0)
+            {
+                throw ExceptionFactory.GetException(errorCode);
+            }
+        }
+
+        /// <summary>
+        /// Registers a callback to a KeyChanged event.
+        /// </summary>
+        /// <param name="key">The key to be observed for changes.</param>
+        /// <param name="callback">The callback to be registered.</param>
+        /// <param name="userData">Additional data.</param>
+        public static void NotifyKeyChanged(string key, NotificationCallback callback, IntPtr? userData = null)
+        {
+            VconfNotifyKeyChanged(key, callback, userData ?? IntPtr.Zero);
+        }
+
+        /// <summary>
+        /// Unregisters a callback from a KeyChanged event.
+        /// </summary>
+        /// <param name="key">The key that was observed for changes.</param>
+        /// <param name="callback">The callback to be unregistered.</param>
+        public static void IgnoreKeyChanged(string key, NotificationCallback callback)
+        {
+            VconfIgnoreKeyChanged(key, callback);
+        }
+    }
+}
diff --git a/Oobe/OobeCommon/Utils/ExceptionFactory.cs b/Oobe/OobeCommon/Utils/ExceptionFactory.cs
new file mode 100644 (file)
index 0000000..fb92c35
--- /dev/null
@@ -0,0 +1,43 @@
+using System;
+using Tizen.Internals.Errors;
+
+namespace Oobe.Common.Utils
+{
+    /// <summary>
+    /// This class provides the API to translate Tizen Error Codes to .NET exceptions.
+    /// </summary>
+    public static class ExceptionFactory
+    {
+        /// <summary>
+        /// Gets the exception that best corresponds to the given error code.
+        /// </summary>
+        /// <param name="errorCode">The Tizen Error Code to be translated.</param>
+        /// <returns>An exception object.</returns>
+        public static Exception GetException(int errorCode)
+        {
+            var msg = ErrorFacts.GetErrorMessage(errorCode);
+            var c = (ErrorCode)errorCode;
+
+            switch (c)
+            {
+                case ErrorCode.NotSupported:
+                    return new NotSupportedException(msg);
+
+                case ErrorCode.OutOfMemory:
+                    return new OutOfMemoryException(msg);
+
+                case ErrorCode.InvalidParameter:
+                    return new ArgumentException(msg);
+
+                case ErrorCode.InvalidOperation:
+                    return new InvalidOperationException(msg);
+
+                case ErrorCode.PermissionDenied:
+                    return new UnauthorizedAccessException(msg);
+
+                default:
+                    return new Exception(msg);
+            }
+        }
+    }
+}