-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUsbWrapper.cpp
More file actions
106 lines (96 loc) · 2.66 KB
/
UsbWrapper.cpp
File metadata and controls
106 lines (96 loc) · 2.66 KB
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
#include "BasicMethods.h"
using namespace System;
namespace AsrClientWrapper {
public ref class AsrNativeAdbDevice : IDisposable
{
private:
unsigned long _timeout = 5000;
void* _nativeHandles;
AsrNativeAdbDevice(Handles* handles)
{
_nativeHandles = handles;
}
public:
//timeout = -1 -----> infinite loop
static AsrNativeAdbDevice^ FindAndOpen(unsigned long timeout)
{
Handles* nativeHandles = find_and_open_handles(timeout);
if (nativeHandles == nullptr) {
throw gcnew InvalidOperationException("Open usb handles failed");
}
else if (nativeHandles == (Handles*)-1) {
throw gcnew TimeoutException("Open usb handles timeout");
}
AsrNativeAdbDevice^ d = gcnew AsrNativeAdbDevice(nativeHandles);
return d;
}
property bool IsOpen
{
bool get() {
return _nativeHandles != nullptr;
}
}
property unsigned long Timeout
{
unsigned long get() {
return _timeout;
}
void set(unsigned long value) {
_timeout = value;
}
}
property unsigned long LastErrorCode
{
unsigned long get() {
if (_nativeHandles == nullptr) return 0;
return GetLastError();
}
}
unsigned long Read(array<System::Byte>^ buffer, int offset, int count)
{
if (!IsOpen) return -1;
if (buffer == nullptr) throw gcnew ArgumentNullException("buffer");
if (offset < 0 || count < 0 || offset + count > buffer->Length) throw gcnew ArgumentOutOfRangeException();
pin_ptr<System::Byte> pinned = &buffer[offset];
void* nativePtr = pinned;
return usb_read(((Handles*)_nativeHandles)->read_endpoint, nativePtr, count, _timeout);;
}
unsigned long Write(array<System::Byte>^ buffer, int offset, int count)
{
if (!IsOpen) return -1;
if (buffer == nullptr) throw gcnew ArgumentNullException("buffer");
if (offset < 0 || count < 0 || offset + count > buffer->Length) throw gcnew ArgumentOutOfRangeException();
pin_ptr<System::Byte> pinned = &buffer[offset];
void* nativePtr = pinned;
return usb_write(((Handles*)_nativeHandles)->write_endpoint, nativePtr, (size_t)count, _timeout);
}
// ÏÔʽ¹Ø±Õ
void Close()
{
if (_nativeHandles != nullptr) {
Handles* handles = (Handles*)_nativeHandles;
if (handles->read_endpoint != nullptr) {
AdbCloseHandle(handles->read_endpoint);
}
if (handles->write_endpoint != nullptr) {
AdbCloseHandle(handles->write_endpoint);
}
if (handles->interface_handle != nullptr) {
AdbCloseHandle(handles->interface_handle);
}
delete handles;
_nativeHandles = nullptr;
}
}
// IDisposable & finalizer
~AsrNativeAdbDevice()
{
Close();
GC::SuppressFinalize(this);
}
!AsrNativeAdbDevice()
{
Close();
}
};
}