2 * Copyright (c) 2016 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 System.Runtime.InteropServices;
19 using System.ComponentModel;
21 namespace Tizen.Convergence
24 /// The class abstracts the features provided by Tizen D2D Convergence.
26 /// <seealso cref="AppCommunicationService"/>
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 public abstract class InternalService : IDisposable
30 internal Interop.Internal.ConvServiceHandle _serviceHandle;
31 internal const string ServiceIdKey = "service_id";
32 internal const string ServiceVersionKey = "service_version";
34 private event EventHandler<InternalServiceEventOccuredEventArgs> _serviceEventOccured;
36 internal InternalService(Interop.Internal.ServiceType type)
38 int ret = Interop.Internal.ConvService.Create(out _serviceHandle);
39 if (ret != (int)ConvErrorCode.None)
41 Log.Error(ErrorFactory.LogTag, "Failed to create service handle");
42 throw ErrorFactory.GetException(ret);
45 ret = Interop.Internal.ConvService.SetType(_serviceHandle, (int)type);
46 if (ret != (int)ConvErrorCode.None)
48 Log.Error(ErrorFactory.LogTag, "Failed to create service handle");
49 throw ErrorFactory.GetException(ret);
53 internal InternalService(IntPtr existingServiceHandle)
55 int ret = Interop.Internal.ConvService.Clone(existingServiceHandle, out _serviceHandle);
56 if (ret != (int)ConvErrorCode.None)
58 Log.Error(ErrorFactory.LogTag, "Failed to clone device");
59 throw ErrorFactory.GetException(ret);
62 IntPtr stringPtr = IntPtr.Zero;
63 ret = Interop.Internal.ConvService.GetPropertyString(_serviceHandle, ServiceIdKey, out stringPtr);
64 if (ret != (int)ConvErrorCode.None)
66 Log.Error(ErrorFactory.LogTag, "Failed to get service Id");
67 throw ErrorFactory.GetException(ret);
70 Id = Marshal.PtrToStringAnsi(stringPtr);
71 Interop.Libc.Free(stringPtr);
73 ret = Interop.Internal.ConvService.GetPropertyString(_serviceHandle, ServiceVersionKey, out stringPtr);
74 if (ret != (int)ConvErrorCode.None)
76 Log.Error(ErrorFactory.LogTag, "Failed to get service version");
77 throw ErrorFactory.GetException(ret);
80 Version = Marshal.PtrToStringAnsi(stringPtr);
81 Interop.Libc.Free(stringPtr);
85 /// The ID of the service
87 public string Id { get; }
90 /// The Version of the service
92 public string Version { get; }
95 /// The event handler for Service Events(Start/Publish/Read/Stop)
97 public event EventHandler<InternalServiceEventOccuredEventArgs> ServiceEventOccurred
101 if (_serviceEventOccured == null)
103 RegisterServiceEventListener();
105 _serviceEventOccured += value;
109 _serviceEventOccured -= value;
110 if (_serviceEventOccured == null)
112 UnregisterServiceEventListener();
118 /// The event handler for service error events
120 public event EventHandler<InternalServiceErrorOccuredEventArgs> ServiceErrorOccured;
123 /// The dispose method
125 public void Dispose()
127 _serviceHandle.Dispose();
130 internal static InternalService GetService(IntPtr serviceHandle)
132 InternalService service = null;
134 int ret = Interop.Internal.ConvService.GetType(serviceHandle, out serviceType);
135 if (ret != (int)ConvErrorCode.None)
137 Log.Error(ErrorFactory.LogTag, "Failed to get service version");
138 throw ErrorFactory.GetException(ret);
140 if (serviceType == (int)Interop.Internal.ServiceType.AppCommunication)
142 service = new InternalAppCommunicationService(serviceHandle);
148 private void RegisterServiceEventListener()
150 int ret = Interop.Internal.ConvService.SetListenerCb(_serviceHandle, serviceEventsCb, IntPtr.Zero);
151 if (ret != (int)ConvErrorCode.None)
153 Log.Error(ErrorFactory.LogTag, "Failed to set listener for service events");
154 throw ErrorFactory.GetException(ret);
158 private void UnregisterServiceEventListener()
160 int ret = Interop.Internal.ConvService.UnsetListenerCb(_serviceHandle);
161 if (ret != (int)ConvErrorCode.None)
163 Log.Error(ErrorFactory.LogTag, "Failed to unset listener for service events");
164 throw ErrorFactory.GetException(ret);
168 private void serviceEventsCb(IntPtr serviceHandle, IntPtr channelHandle, int error, IntPtr resultPayloadHandle, IntPtr userData)
170 Log.Debug(ErrorFactory.LogTag, "service event occured. error code :[" + error + "]");
172 if (error == (int)ConvErrorCode.None)
174 var channel = (channelHandle == IntPtr.Zero) ? null : new InternalChannel(channelHandle);
175 var payload = (resultPayloadHandle == IntPtr.Zero) ? null : new InternalPayload(resultPayloadHandle);
176 _serviceEventOccured?.Invoke(this, new InternalServiceEventOccuredEventArgs()
184 var eventArgs = new InternalServiceErrorOccuredEventArgs()
186 Exception = ErrorFactory.GetException(error)
188 ServiceErrorOccured?.Invoke(this, eventArgs);