2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using NativeApi = Interop.Inspections;
20 namespace Tizen.Inspections
23 /// The Inspector class is used for requesting client data and receiving inspection events.
25 public static class Inspector
27 private static NativeApi.NotificationCallback _notificationCallback;
28 private static event EventHandler<EventReceivedEventArgs> _eventReceived;
31 /// Event to be invoked when new inspection context arrives.
33 public static event EventHandler<EventReceivedEventArgs> EventReceived
37 if (_eventReceived == null)
38 SetNotificationCallback();
39 _eventReceived += value;
43 _eventReceived -= value;
44 if (_eventReceived == null)
45 UnsetNotificationCallback();
50 /// Requests other module (app or service) to provide inspection data.
52 /// <param name="moduleID">An ID of module to request.</param>
53 /// <param name="parameters">Array of request parameters passed to the module.</param>
55 /// The instance of InspectionData.
58 /// This function is permitted only to an app signed by platform level certificates.
60 public static InspectionData RequestInspectableData(string moduleID, string[] parameters)
62 if (parameters is null)
63 throw new ArgumentNullException(nameof(parameters));
65 var ret = NativeApi.RequestClientData(moduleID, parameters, parameters.Length, out IntPtr data);
66 if (ret != NativeApi.InspectionError.None)
67 throw ExceptionFactory.CreateException(ret);
69 return new InspectionData(data);
72 private static void SetNotificationCallback()
74 _notificationCallback = (IntPtr ctx, IntPtr data) =>
76 _eventReceived?.Invoke(null, new EventReceivedEventArgs(new InspectionContext(ctx)));
79 var ret = NativeApi.SetNotificationCb(_notificationCallback, IntPtr.Zero);
80 if (ret != NativeApi.InspectionError.None)
81 throw ExceptionFactory.CreateException(ret);
84 private static void UnsetNotificationCallback()
86 NativeApi.UnsetNotificationCb();
91 /// The InspectionContext class is used for getting data from the context.
93 public class InspectionContext : IDisposable
95 private IntPtr _handle = IntPtr.Zero;
96 private bool _disposed = false;
99 /// The InspectionContext class constructor.
101 /// <param name="handle">Inspection context received in the EventReceived event.</param>
102 internal InspectionContext(IntPtr handle)
106 var ret = NativeApi.GetClientID(handle, out string clientID);
107 if (ret != NativeApi.InspectionError.None)
108 throw ExceptionFactory.CreateException(ret);
114 /// Finalizes an instance of the InspectionContext class.
122 /// Gets data from the context based on given parameters.
124 /// <param name="parameters">Array of parameters.</param>
126 /// The instance of InspectionData.
129 /// This function is permitted only to an app signed by platform level certificates.
131 public InspectionData GetInspectableData(string[] parameters)
133 if (parameters is null)
134 throw new ArgumentNullException(nameof(parameters));
136 var ret = NativeApi.GetData(_handle, parameters, parameters.Length, out IntPtr data);
137 if (ret != NativeApi.InspectionError.None)
138 throw ExceptionFactory.CreateException(ret);
140 return new InspectionData(data);
144 /// Stores module ID.
146 public string ModuleID { get; internal set; }
149 /// Dispose API for closing the internal resources.
151 public void Dispose()
154 GC.SuppressFinalize(this);
158 /// Dispose API for closing the internal resources.
160 protected virtual void Dispose(bool disposing)
164 if (_handle != IntPtr.Zero)
166 NativeApi.Destroy(_handle);
167 _handle = IntPtr.Zero;
175 /// The InspectionData class is used for reading inspection data.
177 public class InspectionData : IDisposable
179 private IntPtr _handle = IntPtr.Zero;
180 private bool _disposed = false;
183 /// The InspectionData class constructor.
185 /// <param name="handle">Inspection data handle.</param>
186 internal InspectionData(IntPtr handle)
192 /// Finalizes an instance of the InspectionData class.
200 /// Reads given number of bytes from inspection data.
202 /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
203 /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
204 /// <param name="count">The maximum number of bytes to be read.</param>
206 /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
208 public int Read(byte[] buffer, int offset, int count)
210 return ReadUnsafe(buffer, offset, count, 0);
214 /// Reads given number of bytes from inspection data.
216 /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.</param>
217 /// <param name="offset">The zero-based byte offset in buffer at which to begin storing the data read from the current stream.</param>
218 /// <param name="count">The maximum number of bytes to be read.</param>
219 /// <param name="timeout">Timeout [ms] for reading requested number of bytes.</param>
221 /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
223 public int Read(byte[] buffer, int offset, int count, int timeout)
225 return ReadUnsafe(buffer, offset, count, timeout);
228 private unsafe int ReadUnsafe(byte[] buffer, int offset, int count, int timeout)
230 var length = Convert.ToUInt32(count);
234 throw new ArgumentNullException(nameof(buffer));
236 if (offset + count > buffer.Length)
237 throw new ArgumentException("The sum of offset and count is larger than the buffer length");
240 throw new ArgumentOutOfRangeException(nameof(offset));
243 throw new ArgumentOutOfRangeException(nameof(count));
245 fixed (byte* p = &buffer[offset])
247 IntPtr ptr = (IntPtr)p;
248 var ret = NativeApi.DataRead(_handle, ptr, length, timeout, out bytes_read);
249 if (ret < NativeApi.InspectionError.None)
250 throw ExceptionFactory.CreateException(ret);
253 return (int)bytes_read;
257 /// Dispose API for closing the internal resources.
259 public void Dispose()
262 GC.SuppressFinalize(this);
266 /// Dispose API for closing the internal resources.
268 protected virtual void Dispose(bool disposing)
272 if (_handle != IntPtr.Zero)
274 NativeApi.DataDestroy(_handle);
275 _handle = IntPtr.Zero;