Adding C# API for D2D Convevergence
[platform/core/csapi/tizenfx.git] / src / Tizen.Convergence / Tizen.Convergence / Device.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
21 namespace Tizen.Convergence
22 {
23     /// <summary>
24     /// The class encapsulates a D2D convergence compliant device information
25     /// </summary>
26     public class Device : IDisposable
27     {
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>();
33
34         /// <summary>
35         /// The Unique ID of the device
36         /// </summary>
37         public string Id { get;}
38
39         /// <summary>
40         /// Name of the device
41         /// </summary>
42         public string Name { get;}
43
44         /// <summary>
45         /// The profile of the device
46         /// </summary>
47         public string Type { get; }
48
49         /// <summary>
50         /// List of services supported by the device
51         /// </summary>
52         public IEnumerable<Service> Services
53         {
54             get
55             {
56                 return _services;
57             }
58         }
59
60         internal Device(IntPtr deviceHandle)
61         {
62             int ret = Interop.ConvDevice.Clone(deviceHandle, out _deviceHandle);
63             if (ret != (int)ConvErrorCode.None)
64             {
65                 Log.Error(ErrorFactory.LogTag, "Failed to clone device");
66                 throw ErrorFactory.GetException(ret);
67             }
68
69             IntPtr stringPtr = IntPtr.Zero;
70             ret = Interop.ConvDevice.GetPropertyString(_deviceHandle, DeviceIdKey, out stringPtr);
71             if (ret == (int)ConvErrorCode.None)
72             {
73                 Id = Marshal.PtrToStringAnsi(stringPtr);
74                 Interop.Libc.Free(stringPtr);
75             }
76
77             ret = Interop.ConvDevice.GetPropertyString(_deviceHandle, DeviceNameKey, out stringPtr);
78             if (ret == (int)ConvErrorCode.None)
79             {
80                 Name = Marshal.PtrToStringAnsi(stringPtr);
81                 Interop.Libc.Free(stringPtr);
82             }
83
84             ret = Interop.ConvDevice.GetPropertyString(_deviceHandle, DeviceTypeKey, out stringPtr);
85             if (ret != (int)ConvErrorCode.None)
86             {
87                 Type = Marshal.PtrToStringAnsi(stringPtr);
88                 Interop.Libc.Free(stringPtr);
89             }
90
91             Log.Debug(ErrorFactory.LogTag, "Device ID ,Name, and Type:[" + Id +"," + Name +"," + Type +"]");
92
93             Interop.ConvDevice.ConvServiceForeachCallback cb = (IntPtr serviceHandle, IntPtr userData) =>
94             {
95                     _services.Add(Service.GetService(serviceHandle));
96             };
97
98             ret = Interop.ConvDevice.ForeachService(_deviceHandle, cb, IntPtr.Zero);
99             if (ret != (int)ConvErrorCode.None)
100             {
101                 Log.Error(ErrorFactory.LogTag, "Failed to get device services");
102                 throw ErrorFactory.GetException(ret);
103             }
104         }
105
106         /// <summary>
107         /// The dispose method
108         /// </summary>
109         public void Dispose()
110         {
111             _deviceHandle.Dispose();
112         }
113     }
114 }