[Internal] Extend Tizen.Inspections internal API (#2797)
authorkkuchciak <k.kuchciak@samsung.com>
Tue, 27 Apr 2021 00:12:23 +0000 (02:12 +0200)
committerGitHub <noreply@github.com>
Tue, 27 Apr 2021 00:12:23 +0000 (09:12 +0900)
Signed-off-by: Konrad Kuchciak <k.kuchciak@samsung.com>
Co-authored-by: WonYoung Choi <wy80.choi@samsung.com>
internals/src/Tizen.Inspections/Interop/Interop.Inspections.cs
internals/src/Tizen.Inspections/Interop/Interop.Libraries.cs
internals/src/Tizen.Inspections/Tizen.Inspections.csproj
internals/src/Tizen.Inspections/Tizen.Inspections/DataRequestReceivedEventArgs.cs [new file with mode: 0644]
internals/src/Tizen.Inspections/Tizen.Inspections/EventReceivedEventArgs.cs
internals/src/Tizen.Inspections/Tizen.Inspections/Inspections.cs

index 0d87539..cc1e99e 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
 using System;
 using System.Runtime.InteropServices;
 using Tizen.Internals;
+using Tizen.Applications;
 
 internal static partial class Interop
 {
@@ -38,30 +39,57 @@ internal static partial class Interop
         };
 
         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-        internal delegate void NotificationCallback(IntPtr ctx, IntPtr data);
+        internal delegate void NotificationCallback(IntPtr ctx, IntPtr user_data);
+
+        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
+        internal delegate void DataRequestCallback(IntPtr data, string[] parameters, int params_size, IntPtr ctx, IntPtr user_data);
 
         [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_set_notification_cb")]
-        internal static extern InspectionError SetNotificationCb(NotificationCallback callback, IntPtr data);
+        internal static extern InspectionError SetNotificationCb(NotificationCallback callback, IntPtr user_data);
 
         [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_unset_notification_cb")]
         internal static extern InspectionError UnsetNotificationCb();
 
+        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_subscribe_event")]
+        internal static extern InspectionError SubscribeEvent(string event_name, string client_id);
+
+        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_set_data_request_cb")]
+        internal static extern InspectionError SetDataRequestCb(DataRequestCallback callback, IntPtr user_data);
+
+        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_unset_data_request_cb")]
+        internal static extern InspectionError UnsetDataRequestCb();
+
         [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_request_client_data")]
         internal static extern InspectionError RequestClientData(string client_id, string[] parameters, int params_size, out IntPtr data);
 
+        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_get_data")]
+        internal static extern InspectionError GetData(IntPtr ctx, string[] parameters, int params_size, out IntPtr data);
+
         [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_data_read")]
         internal static extern InspectionError DataRead(IntPtr data, IntPtr buf, uint count, int timeout_ms, out uint bytes_read);
 
-        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_data_destroy")]
-        internal static extern void DataDestroy(IntPtr data);
+        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_data_write")]
+        internal static extern InspectionError DataWrite(IntPtr data, IntPtr buf, uint count, out uint bytes_written);
+
+        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_send_event")]
+        internal static extern InspectionError SendEvent(string event_name, Bundle event_data);
 
         [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_get_client_id")]
-        internal static extern InspectionError GetClientID(IntPtr ctx, out string client_id);
+        internal static extern InspectionError GetClientID(IntPtr ctx, out IntPtr client_id);
 
-        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_get_data")]
-        internal static extern InspectionError GetData(IntPtr ctx, string[] parameters, int params_size, out IntPtr data);
+        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_get_event_name")]
+        internal static extern InspectionError GetEventName(IntPtr ctx, out IntPtr event_name);
+
+        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_get_event_data")]
+        internal static extern InspectionError GetEventData(IntPtr ctx, out IntPtr event_data);
+
+        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_request_bugreport")]
+        internal static extern InspectionError RequestBugreport(int pid);
+
+        [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_data_destroy")]
+        internal static extern void DataDestroy(IntPtr data);
 
         [DllImport(Libraries.Diagnostics, EntryPoint = "diagnostics_destroy")]
-        internal static extern void Destroy(IntPtr data);
+        internal static extern void Destroy(IntPtr ctx);
     }
 }
index 46d3429..703ac59 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
@@ -18,6 +18,6 @@ internal static partial class Interop
 {
     internal static partial class Libraries
     {
-        public const string Diagnostics = "libdiagnostics.so.1.0.0";
+        public const string Diagnostics = "libdiagnostics.so.1.2.0";
     }
 }
\ No newline at end of file
index 459fe88..cfc2877 100644 (file)
@@ -7,6 +7,7 @@
   <ItemGroup>
     <ProjectReference Include="..\..\..\src\Tizen\Tizen.csproj" />
     <ProjectReference Include="..\..\..\src\Tizen.Log\Tizen.Log.csproj" />
+    <ProjectReference Include="..\..\..\src\Tizen.Applications.Common\Tizen.Applications.Common.csproj" />
   </ItemGroup>
 
 </Project>
diff --git a/internals/src/Tizen.Inspections/Tizen.Inspections/DataRequestReceivedEventArgs.cs b/internals/src/Tizen.Inspections/Tizen.Inspections/DataRequestReceivedEventArgs.cs
new file mode 100644 (file)
index 0000000..d638c74
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+* Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+*
+* Licensed under the Apache License, Version 2.0 (the License);
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an AS IS BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+using System;
+using NativeApi = Interop.Inspections;
+
+namespace Tizen.Inspections
+{
+    /// <summary>
+    /// DataRequestReceivedEventArgs is an extended EventArgs class.
+    /// This class contains event arguments for the DataRequestReceived event from the Inspection class.
+    /// </summary>
+    public class DataRequestReceivedEventArgs : EventArgs
+    {
+        internal DataRequestReceivedEventArgs(InspectionData data, string[] parameters, InspectionContext ctx)
+        {
+            Data = data;
+            Parameters = parameters;
+            Context = ctx;
+        }
+
+        /// <summary>
+        /// The instance of InspectionData.
+        /// </summary>
+        public InspectionData Data { get; internal set; }
+
+        /// <summary>
+        /// An array of data request parameters.
+        /// </summary>
+        public string[] Parameters { get; internal set; }
+
+        /// <summary>
+        /// The instance of InspectionContext.
+        /// </summary>
+        public InspectionContext Context { get; internal set; }
+    }
+}
index d87f5f8..546647b 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
index 38b3821..adb8436 100644 (file)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
@@ -15,6 +15,9 @@
 */
 
 using System;
+using System.Runtime.InteropServices;
+using Tizen;
+using Tizen.Applications;
 using NativeApi = Interop.Inspections;
 
 namespace Tizen.Inspections
@@ -25,7 +28,9 @@ namespace Tizen.Inspections
     public static class Inspector
     {
         private static NativeApi.NotificationCallback _notificationCallback;
+        private static NativeApi.DataRequestCallback _dataRequestCallback;
         private static event EventHandler<EventReceivedEventArgs> _eventReceived;
+        private static event EventHandler<DataRequestReceivedEventArgs> _dataRequestReceived;
 
         /// <summary>
         /// Event to be invoked when new inspection context arrives.
@@ -47,6 +52,55 @@ namespace Tizen.Inspections
         }
 
         /// <summary>
+        /// Event to be invoked when new inspection data request arrives.
+        /// </summary>
+        public static event EventHandler<DataRequestReceivedEventArgs> DataRequestReceived
+        {
+            add
+            {
+                if (_dataRequestReceived == null)
+                    SetDataRequestCallback();
+                _dataRequestReceived += value;
+            }
+            remove
+            {
+                _dataRequestReceived -= value;
+                if (_dataRequestReceived == null)
+                    UnsetDataRequestCallback();
+            }
+        }
+
+        /// <summary>
+        /// Subscribes to inspection event sent by other module.
+        /// </summary>
+        /// <param name="eventName">Event name.</param>
+        /// <param name="moduleID">An ID of the event sender module.</param>
+        /// <remarks>
+        /// This function is permitted only to an app signed by platform level certificates.
+        /// </remarks>
+        public static void SubscribeEvent(string eventName, string moduleID)
+        {
+            var ret = NativeApi.SubscribeEvent(eventName, moduleID);
+            if (ret != NativeApi.InspectionError.None)
+                throw ExceptionFactory.CreateException(ret);
+        }
+
+        /// <summary>
+        /// Sends inspection event.
+        /// </summary>
+        /// <param name="eventName">Event name.</param>
+        /// <param name="eventData">Event data.</param>
+        /// <remarks>
+        /// This function is permitted only to an app signed by platform level certificates.
+        /// </remarks>
+        public static void SendEvent(string eventName, Bundle eventData)
+        {
+            var ret = NativeApi.SendEvent(eventName, eventData);
+            if (ret != NativeApi.InspectionError.None)
+                throw ExceptionFactory.CreateException(ret);
+        }
+
+        /// <summary>
         /// Requests other module (app or service) to provide inspection data.
         /// </summary>
         /// <param name="moduleID">An ID of module to request.</param>
@@ -69,9 +123,22 @@ namespace Tizen.Inspections
             return new InspectionData(data);
         }
 
+        /// <summary>
+        /// Requests bugreport creation.
+        /// </summary>
+        /// <privilege>http://tizen.org/privilege/bugreport.admin</privilege>
+        /// <privlevel>platform</privlevel>
+        /// <param name="pid">An ID of a process that should be livedumped. When pid is 0, system-wide bugreport is created.</param>
+        public static void RequestBugreport(int pid)
+        {
+            var ret = NativeApi.RequestBugreport(pid);
+            if (ret != NativeApi.InspectionError.None)
+                throw ExceptionFactory.CreateException(ret);
+        }
+
         private static void SetNotificationCallback()
         {
-            _notificationCallback = (IntPtr ctx, IntPtr data) =>
+            _notificationCallback = (IntPtr ctx, IntPtr userData) =>
             {
                 _eventReceived?.Invoke(null, new EventReceivedEventArgs(new InspectionContext(ctx)));
             };
@@ -83,7 +150,30 @@ namespace Tizen.Inspections
 
         private static void UnsetNotificationCallback()
         {
-            NativeApi.UnsetNotificationCb();
+            var ret = NativeApi.UnsetNotificationCb();
+            if (ret != NativeApi.InspectionError.None)
+                throw ExceptionFactory.CreateException(ret);
+        }
+
+        private static void SetDataRequestCallback()
+        {
+            _dataRequestCallback = (IntPtr data, string[] parameters, int paramsSize, IntPtr ctx, IntPtr userData) =>
+            {
+                InspectionContext context = ctx != IntPtr.Zero ? new InspectionContext(ctx) : null;
+
+                _dataRequestReceived?.Invoke(null, new DataRequestReceivedEventArgs(new InspectionData(data), parameters, context));
+            };
+
+            var ret = NativeApi.SetDataRequestCb(_dataRequestCallback, IntPtr.Zero);
+            if (ret != NativeApi.InspectionError.None)
+                throw ExceptionFactory.CreateException(ret);
+        }
+
+        private static void UnsetDataRequestCallback()
+        {
+            var ret = NativeApi.UnsetDataRequestCb();
+            if (ret != NativeApi.InspectionError.None)
+                throw ExceptionFactory.CreateException(ret);
         }
     }
 
@@ -103,11 +193,28 @@ namespace Tizen.Inspections
         {
             _handle = handle;
 
-            var ret = NativeApi.GetClientID(handle, out string clientID);
+            IntPtr eventData = IntPtr.Zero;
+
+            var ret = NativeApi.GetClientID(handle, out IntPtr clientID);
             if (ret != NativeApi.InspectionError.None)
                 throw ExceptionFactory.CreateException(ret);
 
-            ModuleID = clientID;
+            ModuleID = Marshal.PtrToStringAnsi(clientID);
+
+            ret = NativeApi.GetEventName(handle, out IntPtr eventName);
+            if (ret != NativeApi.InspectionError.None)
+                throw ExceptionFactory.CreateException(ret);
+
+            EventName = Marshal.PtrToStringAnsi(eventName);
+
+            ret = NativeApi.GetEventData(handle, out eventData);
+            if (ret != NativeApi.InspectionError.None)
+                throw ExceptionFactory.CreateException(ret);
+
+            if (eventData != IntPtr.Zero)
+            {
+                EventData = new Bundle(new SafeBundleHandle(eventData, false));
+            }
         }
 
         /// <summary>
@@ -146,6 +253,16 @@ namespace Tizen.Inspections
         public string ModuleID { get; internal set; }
 
         /// <summary>
+        /// Stores event name.
+        /// </summary>
+        public string EventName { get; internal set; }
+
+        /// <summary>
+        /// Stores event data.
+        /// </summary>
+        public Bundle EventData { get; internal set; }
+
+        /// <summary>
         /// Dispose API for closing the internal resources.
         /// </summary>
         public void Dispose()
@@ -254,6 +371,48 @@ namespace Tizen.Inspections
         }
 
         /// <summary>
+        /// Writes given number of bytes to inspection data.
+        /// </summary>
+        /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream.</param>
+        /// <param name="offset">The zero-based byte offset in buffer at which to begin copying bytes to the current stream.</param>
+        /// <param name="count">The number of bytes to be written to the current stream.</param>
+        /// <returns>
+        /// The number of bytes written to the current stream.
+        /// </returns>
+        public int Write(byte[] buffer, int offset, int count)
+        {
+            return WriteUnsafe(buffer, offset, count);
+        }
+
+        private unsafe int WriteUnsafe(byte[] buffer, int offset, int count)
+        {
+            var length = Convert.ToUInt32(count);
+            uint bytes_written = 0;
+
+            if (buffer == null)
+                throw new ArgumentNullException(nameof(buffer));
+
+            if (offset + count > buffer.Length)
+                throw new ArgumentException("The sum of offset and count is larger than the buffer length");
+
+            if (offset < 0)
+                throw new ArgumentOutOfRangeException(nameof(offset));
+
+            if (count < 0)
+                throw new ArgumentOutOfRangeException(nameof(count));
+
+            fixed (byte* p = &buffer[offset])
+            {
+                IntPtr ptr = (IntPtr)p;
+                var ret = NativeApi.DataWrite(_handle, ptr, length, out bytes_written);
+                if (ret < NativeApi.InspectionError.None)
+                    throw ExceptionFactory.CreateException(ret);
+            }
+
+            return (int)bytes_written;
+        }
+
+        /// <summary>
         /// Dispose API for closing the internal resources.
         /// </summary>
         public void Dispose()