Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Convergence / Tizen.Convergence / InternalDevice.cs
1 /*
2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Runtime.InteropServices;
20 using System.ComponentModel;
21
22 namespace Tizen.Convergence
23 {
24     /// <summary>
25     /// The class encapsulates a D2D convergence compliant device information
26     /// </summary>
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public class InternalDevice : IDisposable
29     {
30         internal const string DeviceIdKey = "device_id";
31         internal const string DeviceNameKey = "device_name";
32         internal const string DeviceTypeKey = "device_type";
33         internal Interop.Internal.ConvDeviceHandle _deviceHandle;
34         internal List<InternalService> _services = new List<InternalService>();
35
36         /// <summary>
37         /// The Unique ID of the device
38         /// </summary>
39         public string Id { get;}
40
41         /// <summary>
42         /// Name of the device
43         /// </summary>
44         public string Name { get;}
45
46         /// <summary>
47         /// The profile of the device
48         /// </summary>
49         public string Type { get; }
50
51         /// <summary>
52         /// List of services supported by the device
53         /// </summary>
54         public IEnumerable<InternalService> Services
55         {
56             get
57             {
58                 return _services;
59             }
60         }
61
62         internal InternalDevice(IntPtr deviceHandle)
63         {
64             int ret = Interop.Internal.ConvDevice.Clone(deviceHandle, out _deviceHandle);
65             if (ret != (int)ConvErrorCode.None)
66             {
67                 Log.Error(ErrorFactory.LogTag, "Failed to clone device");
68                 throw ErrorFactory.GetException(ret);
69             }
70
71             IntPtr stringPtr = IntPtr.Zero;
72             ret = Interop.Internal.ConvDevice.GetPropertyString(_deviceHandle, DeviceIdKey, out stringPtr);
73             if (ret == (int)ConvErrorCode.None)
74             {
75                 Id = Marshal.PtrToStringAnsi(stringPtr);
76                 Interop.Libc.Free(stringPtr);
77             }
78
79             ret = Interop.Internal.ConvDevice.GetPropertyString(_deviceHandle, DeviceNameKey, out stringPtr);
80             if (ret == (int)ConvErrorCode.None)
81             {
82                 Name = Marshal.PtrToStringAnsi(stringPtr);
83                 Interop.Libc.Free(stringPtr);
84             }
85
86             ret = Interop.Internal.ConvDevice.GetPropertyString(_deviceHandle, DeviceTypeKey, out stringPtr);
87             if (ret != (int)ConvErrorCode.None)
88             {
89                 Type = Marshal.PtrToStringAnsi(stringPtr);
90                 Interop.Libc.Free(stringPtr);
91             }
92
93             Log.Debug(ErrorFactory.LogTag, "Device ID ,Name, and Type:[" + Id +"," + Name +"," + Type +"]");
94
95             Interop.Internal.ConvDevice.ConvServiceForeachCallback cb = (IntPtr serviceHandle, IntPtr userData) =>
96             {
97                     _services.Add(InternalService.GetService(serviceHandle));
98             };
99
100             ret = Interop.Internal.ConvDevice.ForeachService(_deviceHandle, cb, IntPtr.Zero);
101             if (ret != (int)ConvErrorCode.None)
102             {
103                 Log.Error(ErrorFactory.LogTag, "Failed to get device services");
104                 throw ErrorFactory.GetException(ret);
105             }
106         }
107
108         /// <summary>
109         /// The dispose method
110         /// </summary>
111         public void Dispose()
112         {
113             _deviceHandle.Dispose();
114         }
115     }
116 }