Release 4.0.0-preview1-00051
[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         ~MtpDevice()
119         {
120             Dispose(false);
121         }
122
123         public void Dispose()
124         {
125             Dispose(true);
126             GC.SuppressFinalize(this);
127         }
128
129         private void Dispose(bool disposing)
130         {
131             if (disposed)
132                 return;
133
134             if (disposing)
135             {
136                 // Free managed objects.
137                 foreach (MtpStorage storage in _storageList)
138                 {
139                     storage.Dispose();
140                     _storageList.Remove(storage);
141                 }
142             }
143             //Free unmanaged objects
144             disposed = true;
145         }
146
147         internal int GetHandle()
148         {
149             return _deviceHandle;
150         }
151
152         /// <summary>
153         /// Gets the list of storages.
154         /// </summary>
155         /// <returns>List of storage objects.</returns>
156         /// <feature>http://tizen.org/feature/network.mtp</feature>
157         /// <exception cref="NotSupportedException">Thrown when Mtp is not supported.</exception>
158         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
159         /// <since_tizen> 5 </since_tizen>
160         public IEnumerable<MtpStorage> GetStorages()
161         {
162             IntPtr storagePtr;
163             int count = 0;
164
165             int ret = Interop.Mtp.GetStorages(_deviceHandle, out storagePtr, out count);
166             if (ret != (int)MtpError.None)
167             {
168                 Log.Error(Globals.LogTag, "Failed to get storage list, Error - " + (MtpError)ret);
169                 MtpErrorFactory.ThrowMtpException(ret);
170             }
171
172             for (int i = 0; i < count; i++)
173             {
174                 int storageID = Marshal.ReadInt32(storagePtr);
175
176                 MtpStorage storageItem = new MtpStorage(_deviceHandle, storageID);
177                 _storageList.Add(storageItem);
178                 storagePtr += sizeof(int);
179             }
180
181             return _storageList;
182         }
183     }
184 }