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.Runtime.InteropServices;
19 using System.Collections.Generic;
21 namespace Tizen.Network.Mtp
24 /// A class for Mtp Device information. It allows applications to handle device information.
26 /// <since_tizen> 5 </since_tizen>
27 public class MtpDevice : IDisposable
29 private int _deviceHandle = -1;
30 private bool disposed = false;
31 private List<MtpStorage> _storageList = new List<MtpStorage>();
34 /// Gets the manufacturer name of the device information.
36 /// <value>Manufacture name of device.</value>
37 /// <since_tizen> 5 </since_tizen>
38 public string ManufacturerName
43 int ret = Interop.Mtp.DeviceInfomation.GetManufacturerName(_deviceHandle, out strPtr);
44 if (ret != (int)MtpError.None)
46 Log.Error(Globals.LogTag, "Failed to get manufacturer name, Error - " + (MtpError)ret);
49 return Marshal.PtrToStringAnsi(strPtr);
54 /// Gets the model name of the device information.
56 /// <value>Model name of device.</value>
57 /// <since_tizen> 5 </since_tizen>
58 public string ModelName
63 int ret = Interop.Mtp.DeviceInfomation.GetModelName(_deviceHandle, out strPtr);
64 if (ret != (int)MtpError.None)
66 Log.Error(Globals.LogTag, "Failed to get model name, Error - " + (MtpError)ret);
69 return Marshal.PtrToStringAnsi(strPtr);
74 /// Gets the serial number of the device information.
76 /// <value>Serial number of device.</value>
77 /// <since_tizen> 5 </since_tizen>
78 public string SerialNumber
83 int ret = Interop.Mtp.DeviceInfomation.GetSerialNumber(_deviceHandle, out strPtr);
84 if (ret != (int)MtpError.None)
86 Log.Error(Globals.LogTag, "Failed to get serial number, Error - " + (MtpError)ret);
89 return Marshal.PtrToStringAnsi(strPtr);
94 /// Gets the device version of the device information.
96 /// <value>Version number of device.</value>
97 /// <since_tizen> 5 </since_tizen>
98 public string DeviceVersion
103 int ret = Interop.Mtp.DeviceInfomation.GetDeviceVersion(_deviceHandle, out strPtr);
104 if (ret != (int)MtpError.None)
106 Log.Error(Globals.LogTag, "Failed to get device version, Error - " + (MtpError)ret);
109 return Marshal.PtrToStringAnsi(strPtr);
113 internal MtpDevice(int handle)
115 _deviceHandle = handle;
119 /// MtpDevice destructor.
129 public void Dispose()
132 GC.SuppressFinalize(this);
135 private void Dispose(bool disposing)
142 // Free managed objects.
143 foreach (MtpStorage storage in _storageList)
146 _storageList.Remove(storage);
149 //Free unmanaged objects
153 internal int GetHandle()
155 return _deviceHandle;
159 /// Gets the list of storages.
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()
171 int ret = Interop.Mtp.GetStorages(_deviceHandle, out storagePtr, out count);
172 if (ret != (int)MtpError.None)
174 Log.Error(Globals.LogTag, "Failed to get storage list, Error - " + (MtpError)ret);
175 MtpErrorFactory.ThrowMtpException(ret);
178 for (int i = 0; i < count; i++)
180 int storageID = Marshal.ReadInt32(storagePtr);
182 MtpStorage storageItem = new MtpStorage(_deviceHandle, storageID);
183 _storageList.Add(storageItem);
184 storagePtr += sizeof(int);