Fix XML Doc Build Warning for Mtp
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Mtp / Tizen.Network.Mtp / MtpDevice.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.Runtime.InteropServices;
19 using System.Collections.Generic;
20
21 namespace Tizen.Network.Mtp
22 {
23     /// <summary>
24     /// A class for Mtp Device information. It allows applications to handle device information.
25     /// </summary>
26     /// <since_tizen> 5 </since_tizen>
27     public class MtpDevice : IDisposable
28     {
29         private int _deviceHandle = -1;
30         private bool disposed = false;
31         private List<MtpStorage> _storageList = new List<MtpStorage>();
32
33         /// <summary>
34         /// Gets the manufacturer name of the device information.
35         /// </summary>
36         /// <value>Manufacture name of device.</value>
37         /// <since_tizen> 5 </since_tizen>
38         public string ManufacturerName
39         {
40             get
41             {
42                 IntPtr strPtr;
43                 int ret = Interop.Mtp.DeviceInfomation.GetManufacturerName(_deviceHandle, out strPtr);
44                 if (ret != (int)MtpError.None)
45                 {
46                     Log.Error(Globals.LogTag, "Failed to get manufacturer name, Error - " + (MtpError)ret);
47                     return "";
48                 }
49                 return Marshal.PtrToStringAnsi(strPtr);
50             }
51         }
52
53         /// <summary>
54         /// Gets the model name of the device information.
55         /// </summary>
56         /// <value>Model name of device.</value>
57         /// <since_tizen> 5 </since_tizen>
58         public string ModelName
59         {
60             get
61             {
62                 IntPtr strPtr;
63                 int ret = Interop.Mtp.DeviceInfomation.GetModelName(_deviceHandle, out strPtr);
64                 if (ret != (int)MtpError.None)
65                 {
66                     Log.Error(Globals.LogTag, "Failed to get model name, Error - " + (MtpError)ret);
67                     return "";
68                 }
69                 return Marshal.PtrToStringAnsi(strPtr);
70             }
71         }
72
73         /// <summary>
74         /// Gets the serial number of the device information.
75         /// </summary>
76         /// <value>Serial number of device.</value>
77         /// <since_tizen> 5 </since_tizen>
78         public string SerialNumber
79         {
80             get
81             {
82                 IntPtr strPtr;
83                 int ret = Interop.Mtp.DeviceInfomation.GetSerialNumber(_deviceHandle, out strPtr);
84                 if (ret != (int)MtpError.None)
85                 {
86                     Log.Error(Globals.LogTag, "Failed to get serial number, Error - " + (MtpError)ret);
87                     return "";
88                 }
89                 return Marshal.PtrToStringAnsi(strPtr);
90             }
91         }
92
93         /// <summary>
94         /// Gets the device version of the device information.
95         /// </summary>
96         /// <value>Version number of device.</value>
97         /// <since_tizen> 5 </since_tizen>
98         public string DeviceVersion
99         {
100             get
101             {
102                 IntPtr strPtr;
103                 int ret = Interop.Mtp.DeviceInfomation.GetDeviceVersion(_deviceHandle, out strPtr);
104                 if (ret != (int)MtpError.None)
105                 {
106                     Log.Error(Globals.LogTag, "Failed to get device version, Error - " + (MtpError)ret);
107                     return "";
108                 }
109                 return Marshal.PtrToStringAnsi(strPtr);
110             }
111         }
112
113         internal MtpDevice(int handle)
114         {
115             _deviceHandle = handle;
116         }
117
118         /// <summary>
119         /// MtpDevice destructor.
120         /// </summary>
121         ~MtpDevice()
122         {
123             Dispose(false);
124         }
125
126         /// <summary>
127         /// Dispose
128         /// </summary>
129         public void Dispose()
130         {
131             Dispose(true);
132             GC.SuppressFinalize(this);
133         }
134
135         private void Dispose(bool disposing)
136         {
137             if (disposed)
138                 return;
139
140             if (disposing)
141             {
142                 // Free managed objects.
143                 foreach (MtpStorage storage in _storageList)
144                 {
145                     storage.Dispose();
146                     _storageList.Remove(storage);
147                 }
148             }
149             //Free unmanaged objects
150             disposed = true;
151         }
152
153         internal int GetHandle()
154         {
155             return _deviceHandle;
156         }
157
158         /// <summary>
159         /// Gets the list of storages.
160         /// </summary>
161         /// <returns>List of storage objects.</returns>
162         /// <feature>http://tizen.org/feature/network.mtp</feature>
163         /// <exception cref="NotSupportedException">Thrown when Mtp is not supported.</exception>
164         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
165         /// <since_tizen> 5 </since_tizen>
166         public IEnumerable<MtpStorage> GetStorages()
167         {
168             IntPtr storagePtr;
169             int count = 0;
170
171             int ret = Interop.Mtp.GetStorages(_deviceHandle, out storagePtr, out count);
172             if (ret != (int)MtpError.None)
173             {
174                 Log.Error(Globals.LogTag, "Failed to get storage list, Error - " + (MtpError)ret);
175                 MtpErrorFactory.ThrowMtpException(ret);
176             }
177
178             for (int i = 0; i < count; i++)
179             {
180                 int storageID = Marshal.ReadInt32(storagePtr);
181
182                 MtpStorage storageItem = new MtpStorage(_deviceHandle, storageID);
183                 _storageList.Add(storageItem);
184                 storagePtr += sizeof(int);
185             }
186
187             return _storageList;
188         }
189     }
190 }