Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MonoUsbDeviceHandle.cs « MonoLibUsb « LibWinUsb - github.com/ClusterM/hakchi2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a9b977dfe970dfd11df3fb70361558e2de529d29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright © 2006-2010 Travis Robinson. All rights reserved.
// 
// website: http://sourceforge.net/projects/libusbdotnet
// e-mail:  libusbdotnet@gmail.com
// 
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or 
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. or 
// visit www.gnu.org.
// 
// 
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using LibUsbDotNet.Main;
using MonoLibUsb.Profile;

namespace MonoLibUsb
{
    /// <summary>
    /// Represents a Libusb-1.0 device handle.
    /// </summary>
    /// <remarks>
    /// <para>To close a device, see the <see cref="Close"/> method.</para>
    /// <note title="Libusb-1.0 API Note:" type="cpp">A <see cref="MonoUsbDeviceHandle"/> is roughly equivalent to a <a href="http://libusb.sourceforge.net/api-1.0/group__dev.html#ga7df95821d20d27b5597f1d783749d6a4">libusb_device_handle</a>.</note>
    /// </remarks>
    /// <code>
    /// MonoUsbDeviceHandle deviceHandle = new MonoUsbDeviceHandle(profileHandle);
    /// if (deviceHandle.IsInvalid) throw new Exception("Invalid device context.");
    /// </code>
    public class MonoUsbDeviceHandle : SafeContextHandle
    {
        private static Object handleLOCK = new object();
        private static MonoUsbError mLastReturnCode;
        private static String mLastReturnString = String.Empty;

        /// <summary>
        /// If the device handle is <see cref="SafeContextHandle.IsInvalid"/>, gets a descriptive string for the <see cref="LastErrorCode"/>.
        /// </summary>
        public static string LastErrorString
        {
            get
            {
                lock (handleLOCK)
                {
                    return mLastReturnString;
                }
            }
        }
        /// <summary>
        /// If the device handle is <see cref="SafeContextHandle.IsInvalid"/>, gets the <see cref="MonoUsbError"/> status code indicating the reason.
        /// </summary>
        public static MonoUsbError LastErrorCode
        {
            get
            {
                lock (handleLOCK)
                {
                    return mLastReturnCode;
                }
            }
        }
        /// <summary>Open a device handle from <paramref name="profileHandle"/>.</summary>
        /// <remarks>
        /// <para>A handle allows you to perform I/O on the device in question.</para>
        /// <para>To close a device handle call its <see cref="SafeHandle.Close"/> method.</para>
        /// <para>This is a non-blocking function; no requests are sent over the bus.</para>
        /// <note title="Libusb-1.0 API Note:" type="cpp">The <see cref="MonoUsbDeviceHandle(MonoUsbProfileHandle)"/> constructor is roughly equivalent to <a href="http://libusb.sourceforge.net/api-1.0/group__dev.html#ga8163100afdf933fabed0db7fa81c89d1">libusb_open()</a>.</note>
        /// </remarks>
        /// <param name="profileHandle">A device profile handle.</param>
        public MonoUsbDeviceHandle(MonoUsbProfileHandle profileHandle)
            : base(IntPtr.Zero) 
        {
            IntPtr pDeviceHandle = IntPtr.Zero;
            int ret = MonoUsbApi.Open(profileHandle, ref pDeviceHandle);
            if (ret < 0 || pDeviceHandle==IntPtr.Zero)
            {
                lock (handleLOCK)
                {
                    mLastReturnCode = (MonoUsbError) ret;
                    mLastReturnString = MonoUsbApi.StrError(mLastReturnCode);
                }
                SetHandleAsInvalid();
            }
            else
            {
                SetHandle(pDeviceHandle);
            }

        }

        internal MonoUsbDeviceHandle(IntPtr pDeviceHandle)
            : base(pDeviceHandle)
        {
        }
        ///<summary>
        ///Closes the <see cref="MonoUsbDeviceHandle"/>.
        ///</summary>
        ///<returns>
        ///true if the <see cref="MonoUsbDeviceHandle"/> is released successfully; otherwise, in the event of a catastrophic failure, false. In this case, it generates a ReleaseHandleFailed Managed Debugging Assistant.
        ///</returns>
        protected override bool ReleaseHandle()
        {
            if (!IsInvalid)
            {
                Debug.WriteLine(GetType().Name + ".ReleaseHandle() Before", "Libusb-1.0");
                MonoUsbApi.Close(handle);
                Debug.WriteLine(GetType().Name + ".ReleaseHandle() After", "Libusb-1.0");
                SetHandleAsInvalid();
            }
            return true;
        }

        /// <summary>
        /// Closes the <see cref="MonoUsbDeviceHandle"/> reference.  When all references are no longer is use, the device
        /// is closed in the <see cref="ReleaseHandle"/> finalizer.
        /// </summary>
        /// <remarks>
        /// <note title="Libusb-1.0 API Note:" type="cpp">The <see cref="Close"/> method is roughly equivalent to <a href="http://libusb.sourceforge.net/api-1.0/group__dev.html#ga779bc4f1316bdb0ac383bddbd538620e">libusb_close()</a>.</note>
        /// </remarks>
        public new void Close()
        {
            base.Close();
        }
    }
}