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;
123 public void Dispose()
126 GC.SuppressFinalize(this);
129 private void Dispose(bool disposing)
136 // Free managed objects.
137 foreach (MtpStorage storage in _storageList)
140 _storageList.Remove(storage);
143 //Free unmanaged objects
147 internal int GetHandle()
149 return _deviceHandle;
153 /// Gets the list of storages.
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()
165 int ret = Interop.Mtp.GetStorages(_deviceHandle, out storagePtr, out count);
166 if (ret != (int)MtpError.None)
168 Log.Error(Globals.LogTag, "Failed to get storage list, Error - " + (MtpError)ret);
169 MtpErrorFactory.ThrowMtpException(ret);
172 for (int i = 0; i < count; i++)
174 int storageID = Marshal.ReadInt32(storagePtr);
176 MtpStorage storageItem = new MtpStorage(_deviceHandle, storageID);
177 _storageList.Add(storageItem);
178 storagePtr += sizeof(int);