From f3b6efd74dbdca24b2d369f82f651a40648daa64 Mon Sep 17 00:00:00 2001 From: pjh9216 Date: Tue, 17 Jul 2018 16:56:28 +0900 Subject: [PATCH] [Appfw] Add APIs for RPC (#330) * [AppFw] Add classes for RPC port Signed-off-by: Junghoon Park * [AppFw] Add additional APIs for RPC port Signed-off-by: Junghoon Park * [AppFw] Add event handlers Signed-off-by: Junghoon Park * [AppFw][RpcPort] Modify description for privileges Signed-off-by: Junghoon Park * [AppFw][RpcPort] Add description for return value Signed-off-by: Junghoon Park --- .../Interop/Interop.Libraries.cs | 1 + .../Interop/Interop.RPCPort.cs | 251 +++++++++++++++ .../Tizen.Applications.RPCPort/Exceptions.cs | 56 ++++ .../Tizen.Applications.RPCPort/Parcel.cs | 341 +++++++++++++++++++++ .../Tizen.Applications.RPCPort/Port.cs | 46 +++ .../Tizen.Applications.RPCPort/ProxyBase.cs | 204 ++++++++++++ .../Tizen.Applications.RPCPort/StubBase.cs | 209 +++++++++++++ 7 files changed, 1108 insertions(+) mode change 100644 => 100755 src/Tizen.Applications.Common/Interop/Interop.Libraries.cs create mode 100755 src/Tizen.Applications.Common/Interop/Interop.RPCPort.cs create mode 100755 src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Exceptions.cs create mode 100755 src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Parcel.cs create mode 100755 src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Port.cs create mode 100755 src/Tizen.Applications.Common/Tizen.Applications.RPCPort/ProxyBase.cs create mode 100755 src/Tizen.Applications.Common/Tizen.Applications.RPCPort/StubBase.cs diff --git a/src/Tizen.Applications.Common/Interop/Interop.Libraries.cs b/src/Tizen.Applications.Common/Interop/Interop.Libraries.cs old mode 100644 new mode 100755 index 227d014..b1a7e53 --- a/src/Tizen.Applications.Common/Interop/Interop.Libraries.cs +++ b/src/Tizen.Applications.Common/Interop/Interop.Libraries.cs @@ -28,5 +28,6 @@ internal static partial class Interop public const string Libc = "libc.so.6"; public const string Application = "libcapi-appfw-application.so.0"; public const string Icuuc = "libicuuc.so"; + public const string RpcPort = "librpc-port.so.1"; } } diff --git a/src/Tizen.Applications.Common/Interop/Interop.RPCPort.cs b/src/Tizen.Applications.Common/Interop/Interop.RPCPort.cs new file mode 100755 index 0000000..bef6586 --- /dev/null +++ b/src/Tizen.Applications.Common/Interop/Interop.RPCPort.cs @@ -0,0 +1,251 @@ +/* + * Copyright (c) 2018 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 System.Runtime.InteropServices; + +using Tizen.Internals.Errors; +using Tizen.Applications; + +internal static partial class Interop +{ + internal static partial class LibRPCPort + { + internal enum ErrorCode + { + None = Tizen.Internals.Errors.ErrorCode.None, + InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter, + OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory, + PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied, + IoError = Tizen.Internals.Errors.ErrorCode.IoError, + } + + internal enum PortType + { + Main, + Callback + } + + internal static partial class Parcel + { + //int rpc_port_parcel_create(rpc_port_parcel_h *h); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_create")] + internal static extern ErrorCode Create(out IntPtr handle); + + //int rpc_port_parcel_create_from_port(rpc_port_parcel_h *h, rpc_port_h port); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_create_from_port")] + internal static extern ErrorCode CreateFromPort(out IntPtr parcelHandle, IntPtr portHandle); + + //int rpc_port_parcel_send(rpc_port_parcel_h h, rpc_port_h port); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_send")] + internal static extern ErrorCode Send(IntPtr parcelHandle, IntPtr portHandle); + + //int rpc_port_parcel_destroy(rpc_port_parcel_h h); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_destroy")] + internal static extern ErrorCode Destroy(IntPtr handle); + + //int rpc_port_parcel_write_byte(rpc_port_parcel_h h, char b); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_write_byte")] + internal static extern ErrorCode WriteByte(IntPtr parcelHandle, byte b); + + //int rpc_port_parcel_write_int16(rpc_port_parcel_h h, short i); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_write_int16")] + internal static extern ErrorCode WriteInt16(IntPtr parcelHandle, short i); + + //int rpc_port_parcel_write_int32(rpc_port_parcel_h h, int i); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_write_int32")] + internal static extern ErrorCode WriteInt32(IntPtr parcelHandle, int i); + + //int rpc_port_parcel_write_int64(rpc_port_parcel_h h, int i); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_write_int64")] + internal static extern ErrorCode WriteInt64(IntPtr parcelHandle, long i); + + //int rpc_port_parcel_write_float(rpc_port_parcel_h h, float f); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_write_float")] + internal static extern ErrorCode WriteFloat(IntPtr parcelHandle, float f); + + //int rpc_port_parcel_write_double(rpc_port_parcel_h h, double d); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_write_double")] + internal static extern ErrorCode WriteDouble(IntPtr parcelHandle, double d); + + //int rpc_port_parcel_write_string(rpc_port_parcel_h h, const char* str); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_write_string")] + internal static extern ErrorCode WriteString(IntPtr parcelHandle, string str); + + //int rpc_port_parcel_write_bool(rpc_port_parcel_h h, bool b); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_write_bool")] + internal static extern ErrorCode WriteBool(IntPtr parcelHandle, bool b); + + //int rpc_port_parcel_write_bundle(rpc_port_parcel_h h, bundle* b); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_write_bundle")] + internal static extern ErrorCode WriteBundle(IntPtr parcelHandle, IntPtr b); + + //int rpc_port_parcel_write_array_count(rpc_port_parcel_h h, int count); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_write_array_count")] + internal static extern ErrorCode WriteArrayCount(IntPtr parcelHandle, int count); + + //int rpc_port_parcel_read_byte(rpc_port_parcel_h h, char* b); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_read_byte")] + internal static extern ErrorCode ReadByte(IntPtr parcelHandle, out byte b); + + //int rpc_port_parcel_read_int16(rpc_port_parcel_h h, short *i); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_read_int16")] + internal static extern ErrorCode ReadInt16(IntPtr parcelHandle, out short i); + + //int rpc_port_parcel_read_int32(rpc_port_parcel_h h, int* i); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_read_int32")] + internal static extern ErrorCode ReadInt32(IntPtr parcelHandle, out int i); + + //int rpc_port_parcel_read_int64(rpc_port_parcel_h h, long long* i); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_read_int64")] + internal static extern ErrorCode ReadInt64(IntPtr parcelHandle, out long i); + + //int rpc_port_parcel_read_float(rpc_port_parcel_h h, float *f); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_read_float")] + internal static extern ErrorCode ReadFloat(IntPtr parcelHandle, out float f); + + //int rpc_port_parcel_read_double(rpc_port_parcel_h h, double *d); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_read_double")] + internal static extern ErrorCode ReadDouble(IntPtr parcelHandle, out double f); + + //int rpc_port_parcel_read_string(rpc_port_parcel_h h, char** str); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_read_string")] + internal static extern ErrorCode ReadString(IntPtr parcelHandle, out string str); + + //int rpc_port_parcel_read_bool(rpc_port_parcel_h h, bool *b); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_read_bool")] + internal static extern ErrorCode ReadBool(IntPtr parcelHandle, out bool b); + + //int rpc_port_parcel_read_bundle(rpc_port_parcel_h h, bundle** b); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_read_bundle")] + internal static extern ErrorCode ReadBundle(IntPtr parcelHandle, out IntPtr b); + + //int rpc_port_parcel_read_array_count(rpc_port_parcel_h h, int *count); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_read_array_count")] + internal static extern ErrorCode ReadArrayCount(IntPtr parcelHandle, out int count); + + //int rpc_port_parcel_burst_read(rpc_port_parcel_h h, unsigned char *buf, unsigned int size); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_burst_read")] + internal static extern ErrorCode Read(IntPtr parcelHandle, [In, Out] byte[] buf, int size); + + //int rpc_port_parcel_burst_write(rpc_port_parcel_h h, const unsigned char *buf, unsigned int size); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_parcel_burst_write")] + internal static extern ErrorCode Write(IntPtr parcelHandle, byte[] buf, int size); + } + + internal static partial class Proxy + { + //typedef void (*rpc_port_proxy_connected_event_cb)(const char *ep, const char* port_name, rpc_port_h port, void* data); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ConnectedEventCallback(string endPoint, string port_name, IntPtr port, IntPtr data); + + //typedef void (*rpc_port_proxy_disconnected_event_cb)(const char *ep, const char* port_name, void* data); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void DisconnectedEventCallback(string endPoint, string port_name, IntPtr data); + + //typedef void (*rpc_port_proxy_rejected_event_cb) (const char* ep, const char* port_name, void* data); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void RejectedEventCallback(string endPoint, string port_name, IntPtr data); + + //typedef void (*rpc_port_proxy_received_event_cb) (const char* ep, const char* port_name, void* data); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ReceivedEventCallback(string endPoint, string port_name, IntPtr data); + + //int rpc_port_proxy_create(rpc_port_proxy_h *h); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_proxy_create")] + internal static extern ErrorCode Create(out IntPtr handle); + + //int rpc_port_proxy_destroy(rpc_port_proxy_h h); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_proxy_destroy")] + internal static extern ErrorCode Destroy(IntPtr handle); + + //int rpc_port_proxy_connect(rpc_port_proxy_h h, const char *appid, const char* port); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_proxy_connect")] + internal static extern ErrorCode Connect(IntPtr handle, string appId, string port); + + //int rpc_port_proxy_add_connected_event_cb(rpc_port_proxy_h h, rpc_port_proxy_connected_event_cb cb, void* data); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_proxy_add_connected_event_cb")] + internal static extern ErrorCode AddConnectedEventCb(IntPtr handle, ConnectedEventCallback cb, IntPtr data); + + //int rpc_port_proxy_add_disconnected_event_cb(rpc_port_proxy_h h, rpc_port_proxy_disconnected_event_cb cb, void* data); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_proxy_add_disconnected_event_cb")] + internal static extern ErrorCode AddDisconnectedEventCb(IntPtr handle, DisconnectedEventCallback cb, IntPtr data); + + //int rpc_port_proxy_add_rejected_event_cb(rpc_port_proxy_h h, rpc_port_proxy_rejected_event_cb cb, void* data); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_proxy_add_rejected_event_cb")] + internal static extern ErrorCode AddRejectedEventCb(IntPtr handle, RejectedEventCallback cb, IntPtr data); + + //int rpc_port_proxy_add_received_event_cb(rpc_port_proxy_h h, rpc_port_proxy_received_event_cb cb, void* data); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_proxy_add_received_event_cb")] + internal static extern ErrorCode AddReceivedEventCb(IntPtr handle, ReceivedEventCallback cb, IntPtr data); + + //int rpc_port_proxy_get_port(rpc_port_proxy_h h, rpc_port_port_type_e type, rpc_port_h* port); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_proxy_get_port")] + internal static extern ErrorCode GetPort(IntPtr handle, PortType t, out IntPtr port); + } + + internal static partial class Stub + { + //typedef void (*rpc_port_stub_connected_event_cb)(const char *sender, const char *instance, void* data); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void ConnectedEventCallback(string sender, string instance, IntPtr data); + + //typedef void (* rpc_port_stub_disconnected_event_cb) (const char* sender, const char *instance, void* data); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void DisconnectedEventCallback(string sender, string instance, IntPtr data); + + //typedef void (* rpc_port_stub_received_event_cb) (const char* sender, const char *instance, rpc_port_h port, void* data); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate int ReceivedEventCallback(string sender, string instance, IntPtr port, IntPtr data); + + //int rpc_port_stub_create(rpc_port_stub_h* h, const char* port_name); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_stub_create")] + internal static extern ErrorCode Create(out IntPtr handle, string portName); + + //int rpc_port_stub_destroy(rpc_port_stub_h h); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_stub_destroy")] + internal static extern ErrorCode Destroy(IntPtr handle); + + //int rpc_port_stub_listen(rpc_port_stub_h h); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_stub_listen")] + internal static extern ErrorCode Listen(IntPtr handle); + + //int rpc_port_stub_add_connected_event_cb(rpc_port_stub_h h, rpc_port_stub_connected_event_cb cb, void* data); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_stub_add_connected_event_cb")] + internal static extern ErrorCode AddConnectedEventCb(IntPtr handle, ConnectedEventCallback cb, IntPtr data); + + //int rpc_port_stub_add_disconnected_event_cb(rpc_port_stub_h h, rpc_port_stub_disconnected_event_cb cb, void* data); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_stub_add_disconnected_event_cb")] + internal static extern ErrorCode AddDisconnectedEventCb(IntPtr handle, DisconnectedEventCallback cb, IntPtr data); + + //int rpc_port_stub_add_received_event_cb(rpc_port_stub_h h, rpc_port_stub_received_event_cb cb, void* data); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_stub_add_received_event_cb")] + internal static extern ErrorCode AddReceivedEventCb(IntPtr handle, ReceivedEventCallback cb, IntPtr data); + + //int rpc_port_stub_add_privilege(rpc_port_stub_h h, const char *privilege); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_stub_add_privilege")] + internal static extern ErrorCode AddPrivilege(IntPtr handle, string privilege); + + //int rpc_port_stub_set_trusted(rpc_port_stub_h h, const bool trusted); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_stub_set_trusted")] + internal static extern ErrorCode SetTrusted(IntPtr handle, bool trusted); + + //int rpc_port_stub_get_port(rpc_port_stub_h h, rpc_port_port_type_e type, const char* instance, rpc_port_h *port); + [DllImport(Libraries.RpcPort, EntryPoint = "rpc_port_stub_get_port")] + internal static extern ErrorCode GetPort(IntPtr handle, PortType t, string instance, out IntPtr port); + } + } +} diff --git a/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Exceptions.cs b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Exceptions.cs new file mode 100755 index 0000000..4ff403b --- /dev/null +++ b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Exceptions.cs @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2018 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; + +namespace Tizen.Applications.RPCPort +{ + /// + /// Exception class for invalid IO + /// + /// 5 + public class InvalidIOException : InvalidOperationException { } + + /// + /// Exception class for invalid ID + /// + /// 5 + public class InvalidIDException : InvalidOperationException { } + + /// + /// Exception class for permission denied + /// + /// 5 + public class PermissionDeniedException : InvalidOperationException { } + + /// + /// Exception class for invalid protocol + /// + /// 5 + public class InvalidProtocolException : InvalidOperationException { } + + /// + /// Exception class which will be thrown when not connected socket is used + /// + /// 5 + public class NotConnectedSocketException : InvalidOperationException { } + + /// + /// Exception class which will be thrown when invalid callback object is used + /// + /// 5 + public class InvalidCallbackException : InvalidOperationException { } +} diff --git a/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Parcel.cs b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Parcel.cs new file mode 100755 index 0000000..de8bc1c --- /dev/null +++ b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Parcel.cs @@ -0,0 +1,341 @@ +/* + * Copyright (c) 2018 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; + +namespace Tizen.Applications.RPCPort +{ + /// + /// The class for helping marshalling and unmarshalling for RPC + /// + /// 5 + public class Parcel : IDisposable + { + private IntPtr _handle; + + /// + /// Constructor for this class + /// + /// Thrown when internal IO error happens + /// 5 + public Parcel() + { + var r = Interop.LibRPCPort.Parcel.Create(out _handle); + if (r != Interop.LibRPCPort.ErrorCode.None) + throw new InvalidIOException(); + } + + /// + /// Constructor with Port object + /// + /// Port object + /// Thrown when internal IO error happens + /// 5 + public Parcel(Port port) + { + var r = Interop.LibRPCPort.Parcel.CreateFromPort(out _handle, port.Handle); + if (r != Interop.LibRPCPort.ErrorCode.None) + throw new InvalidIOException(); + } + + /// + /// Sends parcel data through the port + /// + /// The RPC port object for writing data + /// Thrown when internal IO error happens + /// 5 + public void Send(Port p) + { + var r = Interop.LibRPCPort.Parcel.Send(_handle, p.Handle); + if (r != Interop.LibRPCPort.ErrorCode.None) + throw new InvalidIOException(); + } + + /// + /// Writes 'byte' type value into Parcel object + /// + /// 'byte' type data + /// 5 + public void WriteByte(byte b) + { + Interop.LibRPCPort.Parcel.WriteByte(_handle, b); + } + + /// + /// Writes 'short' type value into Parcel object + /// + /// 'short' type data + /// 5 + public void WriteShort(short b) + { + Interop.LibRPCPort.Parcel.WriteInt16(_handle, b); + } + + /// + /// Writes 'int' type value into Parcel object + /// + /// 'int' type data + /// 5 + public void WriteInt(int b) + { + Interop.LibRPCPort.Parcel.WriteInt32(_handle, b); + } + + /// + /// Writes a Long type value into Parcel handle + /// + /// 'long' type data + /// 5 + public void WriteLong(long b) + { + Interop.LibRPCPort.Parcel.WriteInt64(_handle, b); + } + + /// + /// Writes a 'float' type value into Parcel handle + /// + /// 'float' type data + /// 5 + public void WriteFloat(float b) + { + Interop.LibRPCPort.Parcel.WriteFloat(_handle, b); + } + + /// + /// Writes a 'double' type value into Parcel handle + /// + /// 'double' type data + /// 5 + public void WriteDouble(double b) + { + Interop.LibRPCPort.Parcel.WriteDouble(_handle, b); + } + + /// + /// Writes a 'string' type value into Parcel handle + /// + /// 'string' type data + /// 5 + public void WriteString(string b) + { + Interop.LibRPCPort.Parcel.WriteString(_handle, b); + } + + /// + /// Writes a 'bool' type value into Parcel handle + /// + /// 'bool' type data + /// 5 + public void WriteBool(bool b) + { + Interop.LibRPCPort.Parcel.WriteBool(_handle, b); + } + + /// + /// Writes a 'Bundle' type value into Parcel handle + /// + /// 'Bundle' type data + /// 5 + public void WriteBundle(Bundle b) + { + Interop.LibRPCPort.Parcel.WriteBundle(_handle, b.SafeBundleHandle.DangerousGetHandle()); + } + + /// + /// Writes a count for array into Parcel object + /// + /// Array count + /// 5 + public void WriteArrayCount(int cnt) + { + Interop.LibRPCPort.Parcel.WriteArrayCount(_handle, cnt); + } + + /// + /// Reads 'byte' type value from Parcel object + /// + /// 'byte' type data + /// 5 + public byte ReadByte() + { + Interop.LibRPCPort.Parcel.ReadByte(_handle, out byte b); + return b; + } + + /// + /// Reads 'short' type value from Parcel object + /// + /// 'short' type data + /// 5 + public short ReadShort() + { + Interop.LibRPCPort.Parcel.ReadInt16(_handle, out short b); + return b; + } + + /// + /// Reads 'int' type value from Parcel object + /// + /// 'int' type data + /// 5 + public int ReadInt() + { + Interop.LibRPCPort.Parcel.ReadInt32(_handle, out int b); + return b; + } + + /// + /// Reads 'long' type value from Parcel object + /// + /// 'long' type data + /// 5 + public long ReadLong() + { + Interop.LibRPCPort.Parcel.ReadInt64(_handle, out long b); + return b; + } + + /// + /// Reads 'float' type value from Parcel object + /// + /// 'float' type data + /// 5 + public float ReadFloat() + { + Interop.LibRPCPort.Parcel.ReadFloat(_handle, out float b); + return b; + } + + /// + /// Reads 'double' type value from Parcel object + /// + /// 'double' type data + /// 5 + public double ReadDouble() + { + Interop.LibRPCPort.Parcel.ReadDouble(_handle, out double b); + return b; + } + + /// + /// Reads 'string' type value from Parcel object + /// + /// 'string' type data + /// 5 + public string ReadString() + { + Interop.LibRPCPort.Parcel.ReadString(_handle, out string b); + return b; + } + + /// + /// Reads 'bool' type value from Parcel object + /// + /// 'bool' type data + /// 5 + public bool ReadBool() + { + Interop.LibRPCPort.Parcel.ReadBool(_handle, out bool b); + return b; + } + + /// + /// Reads 'Bundle' type value from Parcel object + /// + /// 'Bundle' type data + /// 5 + public Bundle ReadBundle() + { + Interop.LibRPCPort.Parcel.ReadBundle(_handle, out IntPtr b); + + return new Bundle(new SafeBundleHandle(b, true)); + } + + /// + /// Reads a count for array from Parcel object + /// + /// Array count + /// 5 + public int ReadArrayCount() + { + Interop.LibRPCPort.Parcel.ReadArrayCount(_handle, out int b); + return b; + } + + /// + /// Writes bytes into Parcel object + /// + /// Array of bytes + /// 5 + public void Write(byte[] bytes) + { + Interop.LibRPCPort.Parcel.Write(_handle, bytes, bytes.Length); + } + + /// + /// Reads bytes from Parcel object + /// + /// Bytes to read + /// Array of bytes + /// 5 + public byte[] Read(int size) + { + var ret = new byte[size]; + Interop.LibRPCPort.Parcel.Read(_handle, ret, size); + return ret; + } + + #region IDisposable Support + private bool disposedValue = false; + + private void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + } + + if (_handle != IntPtr.Zero) + { + Interop.LibRPCPort.Parcel.Destroy(_handle); + _handle = IntPtr.Zero; + } + + disposedValue = true; + } + } + + /// + /// Finalizer of the class Parcel + /// + ~Parcel() + { + Dispose(false); + } + + /// + /// Release all resources used by the class Parcel + /// + /// 5 + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + #endregion + } +} diff --git a/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Port.cs b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Port.cs new file mode 100755 index 0000000..c33e2c4 --- /dev/null +++ b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Port.cs @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2018 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; + +namespace Tizen.Applications.RPCPort +{ + /// + /// The class for communicating each other between proxy and stub + /// + /// 5 + public class Port + { + /// + /// Enumeration for RPC port types + /// + public enum Type + { + /// + /// Main channel to communicate + /// + Main, + + /// + /// Sub channel for callbacks + /// + Callback + } + + internal IntPtr Handle { get; set; } + internal Port() { } + } +} diff --git a/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/ProxyBase.cs b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/ProxyBase.cs new file mode 100755 index 0000000..e904444 --- /dev/null +++ b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/ProxyBase.cs @@ -0,0 +1,204 @@ +/* + * Copyright (c) 2018 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; + +namespace Tizen.Applications.RPCPort +{ + /// + /// Abstract class for making a proxy class for RPC + /// + /// 5 + public abstract class ProxyBase : IDisposable + { + private Interop.LibRPCPort.Proxy.ConnectedEventCallback _connectedEventCallback; + private Interop.LibRPCPort.Proxy.DisconnectedEventCallback _disconnectedEventCallback; + private Interop.LibRPCPort.Proxy.RejectedEventCallback _rejectedEventCallback; + private Interop.LibRPCPort.Proxy.ReceivedEventCallback _receivedEventCallback; + private IntPtr _proxy; + + /// + /// Gets Port object + /// + /// 5 + protected Port Port { get; private set; } + + /// + /// Gets Port object for asynchronous events + /// + /// 5 + protected Port CallbackPort { get; private set; } + + /// + /// Constructor for this class + /// + /// Thrown when internal IO error happens + /// 5 + public ProxyBase() + { + if (Interop.LibRPCPort.Proxy.Create(out _proxy) != Interop.LibRPCPort.ErrorCode.None) + throw new InvalidIOException(); + _connectedEventCallback = new Interop.LibRPCPort.Proxy.ConnectedEventCallback(OnConnectedEvent); + _disconnectedEventCallback = new Interop.LibRPCPort.Proxy.DisconnectedEventCallback(OnDisconnectedEvent); + _rejectedEventCallback = new Interop.LibRPCPort.Proxy.RejectedEventCallback(OnRejectedEvent); + _receivedEventCallback = new Interop.LibRPCPort.Proxy.ReceivedEventCallback(OnReceivedEvent); + Interop.LibRPCPort.Proxy.AddConnectedEventCb(_proxy, _connectedEventCallback, IntPtr.Zero); + Interop.LibRPCPort.Proxy.AddDisconnectedEventCb(_proxy, _disconnectedEventCallback, IntPtr.Zero); + Interop.LibRPCPort.Proxy.AddRejectedEventCb(_proxy, _rejectedEventCallback, IntPtr.Zero); + Interop.LibRPCPort.Proxy.AddReceivedEventCb(_proxy, _receivedEventCallback, IntPtr.Zero); + } + + /// + /// Connects to port + /// + /// The target stub app ID + /// The name of rpc port + /// Thrown when not available app ID is used + /// Thrown when internal IO error happens + /// Thrown when the permission is denied + /// http://tizen.org/privilege/datasharing + /// http://tizen.org/privilege/appmanager.launch + /// 5 + protected void Connect(string appid, string port) + { + var err = Interop.LibRPCPort.Proxy.Connect(_proxy, appid, port); + switch (err) + { + case Interop.LibRPCPort.ErrorCode.InvalidParameter: + throw new InvalidIDException(); + case Interop.LibRPCPort.ErrorCode.PermissionDenied: + throw new PermissionDeniedException(); + case Interop.LibRPCPort.ErrorCode.IoError: + throw new InvalidIOException(); + } + } + + /// + /// Gets a port + /// + /// The type of port + /// Port object + /// Thrown when internal IO error happens + /// 5 + protected Port GetPort(Port.Type t) + { + var err = Interop.LibRPCPort.Proxy.GetPort(_proxy, + (Interop.LibRPCPort.PortType)t, out IntPtr port); + switch (err) + { + case Interop.LibRPCPort.ErrorCode.InvalidParameter: + case Interop.LibRPCPort.ErrorCode.IoError: + throw new InvalidIOException(); + } + + return new Port() { Handle = port }; + } + + /// + /// Abstract method for receiving connected event + /// + /// The target stub app ID + /// The name of the port + /// Port object for reading and writing + /// 5 + protected abstract void OnConnectedEvent(string endPoint, string portName, Port port); + + /// + /// Abstract method for receiving disconnected event + /// + /// The target stub app ID + /// The name of the port + /// 5 + protected abstract void OnDisconnectedEvent(string endPoint, string portName); + + /// + /// Abstract method called when the proxy received data from stub + /// + /// The target stub app ID + /// The name of the port + /// 5 + protected abstract void OnReceivedEvent(string endPoint, string portName); + + /// + /// Abstract method for receiving rejected event + /// + /// The target stub app ID + /// The name of the port + /// 5 + protected abstract void OnRejectedEvent(string endPoint, string portName); + + private void OnConnectedEvent(string endPoint, string portName, IntPtr port, IntPtr data) + { + Port = new Port() { Handle = port }; + CallbackPort = GetPort(Port.Type.Callback); + OnConnectedEvent(endPoint, portName, Port); + } + + private void OnDisconnectedEvent(string endPoint, string portName, IntPtr data) + { + Port = null; + CallbackPort = null; + OnDisconnectedEvent(endPoint, portName); + } + + private void OnReceivedEvent(string endPoint, string portName, IntPtr data) + { + OnReceivedEvent(endPoint, portName); + } + + private void OnRejectedEvent(string endPoint, string portName, IntPtr data) + { + OnRejectedEvent(endPoint, portName); + } + + #region IDisposable Support + private bool disposedValue = false; + + private void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + } + if (_proxy != IntPtr.Zero) + Interop.LibRPCPort.Proxy.Destroy(_proxy); + _proxy = IntPtr.Zero; + + disposedValue = true; + } + } + + /// + /// Finalizer of the class ProxyBase + /// + ~ProxyBase() + { + Dispose(false); + } + + /// + /// Release all resources used by the class ProxyBase + /// + /// 5 + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + #endregion + } +} diff --git a/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/StubBase.cs b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/StubBase.cs new file mode 100755 index 0000000..c9cc668 --- /dev/null +++ b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/StubBase.cs @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2018 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; + +namespace Tizen.Applications.RPCPort +{ + /// + /// Abstract class for making a stub class for RPC + /// + /// 5 + public abstract class StubBase : IDisposable + { + private Interop.LibRPCPort.Stub.ConnectedEventCallback _connectedEventCallback; + private Interop.LibRPCPort.Stub.DisconnectedEventCallback _disconnectedEventCallback; + private Interop.LibRPCPort.Stub.ReceivedEventCallback _receivedEventCallback; + private IntPtr _stub; + + /// + /// Gets port name + /// + /// 5 + public string PortName { get; } + + /// + /// Constructor for this class + /// + /// The name of the port which want to listen + /// Thrown when internal IO error happens + /// 5 + public StubBase(string portName) + { + if (Interop.LibRPCPort.Stub.Create(out _stub, portName) != Interop.LibRPCPort.ErrorCode.None) + throw new InvalidIOException(); + PortName = portName; + _connectedEventCallback = new Interop.LibRPCPort.Stub.ConnectedEventCallback(OnConnectedEvent); + _disconnectedEventCallback = new Interop.LibRPCPort.Stub.DisconnectedEventCallback(OnDisconnectedEvent); + _receivedEventCallback = new Interop.LibRPCPort.Stub.ReceivedEventCallback(OnReceivedEvent); + Interop.LibRPCPort.Stub.AddReceivedEventCb(_stub, _receivedEventCallback, IntPtr.Zero); + Interop.LibRPCPort.Stub.AddConnectedEventCb(_stub, _connectedEventCallback, IntPtr.Zero); + Interop.LibRPCPort.Stub.AddDisconnectedEventCb(_stub, _disconnectedEventCallback, IntPtr.Zero); + } + + /// + /// Listens to the requests for connections + /// + /// Thrown when internal IO error happens + /// 5 + protected void Listen() + { + var err = Interop.LibRPCPort.Stub.Listen(_stub); + switch (err) + { + case Interop.LibRPCPort.ErrorCode.InvalidParameter: + case Interop.LibRPCPort.ErrorCode.IoError: + throw new InvalidIOException(); + } + } + + /// + /// Adds a privilege to the stub + /// + /// The privilege to access this stub + /// Thrown when the privilege is null + /// 5 + protected void AddPrivilege(string privilege) + { + if (privilege == null) + throw new ArgumentNullException(); + Interop.LibRPCPort.Stub.AddPrivilege(_stub, privilege); + } + + /// + /// Sets trusted to the stub + /// + /// Whether stub allows only trusted proxy or not + /// 5 + protected void SetTrusted(bool trusted) + { + Interop.LibRPCPort.Stub.SetTrusted(_stub, trusted); + } + + /// + /// Gets s port + /// + /// The type of port + /// The ID of the instance which is connected + /// Port object + /// Thrown when invalid instance was used + /// Thrown when internal IO error happens + /// 5 + protected Port GetPort(Port.Type t, string instance) + { + var err = Interop.LibRPCPort.Stub.GetPort(_stub, + (Interop.LibRPCPort.PortType)t, instance, out IntPtr port); + switch (err) + { + case Interop.LibRPCPort.ErrorCode.InvalidParameter: + throw new InvalidIDException(); + case Interop.LibRPCPort.ErrorCode.IoError: + throw new InvalidIOException(); + } + + return new Port() { Handle = port }; + } + + /// + /// Abstract method for receiving connected event + /// + /// The target proxy app ID + /// The information of the request + /// 5 + protected abstract void OnConnectedEvent(string sender, string instance); + + /// + /// Abstract method for receiving disconnected event + /// + /// The target proxy app ID + /// The information of the request + /// 5 + protected abstract void OnDisconnectedEvent(string sender, string instance); + + /// + /// Abstract method called when the stub received data from proxy + /// + /// The target proxy app ID + /// The information of the request + /// Port object for reading and writing + /// 'true' to continue receiving data, otherwise 'false' to disconnect from the port + /// 5 + protected abstract bool OnReceivedEvent(string sender, string instance, Port port); + + /// + /// Abstract method called right before disposing object + /// + /// 5 + protected abstract void OnTerminatedEvent(); + + private void OnConnectedEvent(string sender, string instance, IntPtr data) + { + OnConnectedEvent(sender, instance); + } + + private void OnDisconnectedEvent(string sender, string instance, IntPtr data) + { + OnDisconnectedEvent(sender, instance); + } + + private int OnReceivedEvent(string sender, string instance, IntPtr port, IntPtr data) + { + bool b = OnReceivedEvent(sender, instance, new Port() { Handle = port }); + if (b) + return 0; + return -1; + } + + #region IDisposable Support + private bool disposedValue = false; + + private void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + } + + OnTerminatedEvent(); + + if (_stub != IntPtr.Zero) + Interop.LibRPCPort.Proxy.Destroy(_stub); + _stub = IntPtr.Zero; + + disposedValue = true; + } + } + + /// + /// Finalizer of the class StubBase + /// + ~StubBase() + { + Dispose(false); + } + + /// + /// Release all resources used by the class StubBase + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + #endregion + } +} -- 2.7.4