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.Collections.Generic;
19 using System.Runtime.InteropServices;
21 namespace Tizen.Convergence
24 /// The class encapsulates a D2D convergence compliant device information
26 public class Device : IDisposable
28 internal const string DeviceIdKey = "device_id";
29 internal const string DeviceNameKey = "device_name";
30 internal const string DeviceTypeKey = "device_type";
31 internal Interop.ConvDeviceHandle _deviceHandle;
32 internal List<Service> _services = new List<Service>();
35 /// The Unique ID of the device
37 public string Id { get;}
40 /// Name of the device
42 public string Name { get;}
45 /// The profile of the device
47 public string Type { get; }
50 /// List of services supported by the device
52 public IEnumerable<Service> Services
60 internal Device(IntPtr deviceHandle)
62 int ret = Interop.ConvDevice.Clone(deviceHandle, out _deviceHandle);
63 if (ret != (int)ConvErrorCode.None)
65 Log.Error(ErrorFactory.LogTag, "Failed to clone device");
66 throw ErrorFactory.GetException(ret);
69 IntPtr stringPtr = IntPtr.Zero;
70 ret = Interop.ConvDevice.GetPropertyString(_deviceHandle, DeviceIdKey, out stringPtr);
71 if (ret == (int)ConvErrorCode.None)
73 Id = Marshal.PtrToStringAnsi(stringPtr);
74 Interop.Libc.Free(stringPtr);
77 ret = Interop.ConvDevice.GetPropertyString(_deviceHandle, DeviceNameKey, out stringPtr);
78 if (ret == (int)ConvErrorCode.None)
80 Name = Marshal.PtrToStringAnsi(stringPtr);
81 Interop.Libc.Free(stringPtr);
84 ret = Interop.ConvDevice.GetPropertyString(_deviceHandle, DeviceTypeKey, out stringPtr);
85 if (ret != (int)ConvErrorCode.None)
87 Type = Marshal.PtrToStringAnsi(stringPtr);
88 Interop.Libc.Free(stringPtr);
91 Log.Debug(ErrorFactory.LogTag, "Device ID ,Name, and Type:[" + Id +"," + Name +"," + Type +"]");
93 Interop.ConvDevice.ConvServiceForeachCallback cb = (IntPtr serviceHandle, IntPtr userData) =>
95 _services.Add(Service.GetService(serviceHandle));
98 ret = Interop.ConvDevice.ForeachService(_deviceHandle, cb, IntPtr.Zero);
99 if (ret != (int)ConvErrorCode.None)
101 Log.Error(ErrorFactory.LogTag, "Failed to get device services");
102 throw ErrorFactory.GetException(ret);
107 /// The dispose method
109 public void Dispose()
111 _deviceHandle.Dispose();