@psdk/ohos-usb 0.5.17 → 0.5.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -6
- package/build/connected.d.ts +1 -0
- package/build/connected.d.ts.map +1 -1
- package/build/connected.js +11 -12
- package/build/index.js +2 -18
- package/build/provider.d.ts +1 -1
- package/build/provider.d.ts.map +1 -1
- package/build/provider.js +33 -39
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -10,11 +10,14 @@ npm i @psdk/ohos-usb
|
|
|
10
10
|
## How to use
|
|
11
11
|
|
|
12
12
|
```js
|
|
13
|
-
|
|
14
|
-
usb
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
//open usb
|
|
14
|
+
const usb = new OhosUsb();
|
|
15
|
+
const connectDevice = usb.openUsb();
|
|
16
|
+
// init device
|
|
17
|
+
PrinterUtil.getInstance().init(connectDevice);
|
|
18
|
+
// send data
|
|
19
|
+
const report = await PrinterUtil.getInstance().cpcl()
|
|
20
|
+
.raw(Raw.binary(Uint8Array.from([0x10, 0xff, 0xbf])))
|
|
21
|
+
.write();
|
|
19
22
|
```
|
|
20
23
|
|
package/build/connected.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export declare class OhosUsbConnectedDevice implements ConnectedDevice {
|
|
|
9
9
|
private readonly options;
|
|
10
10
|
constructor(options: OusbOptions);
|
|
11
11
|
get device(): usb.USBDevice;
|
|
12
|
+
get ohosUsb(): OhosUsb;
|
|
12
13
|
canRead(): boolean;
|
|
13
14
|
connectionState(): ConnectionState;
|
|
14
15
|
deviceName(): string;
|
package/build/connected.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connected.d.ts","sourceRoot":"","sources":["../src/connected.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,eAAe,EAAE,eAAe,EAAE,WAAW,EAAC,MAAM,oBAAoB,CAAC;AAEjF,OAAO,E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UAAU,IAAI,MAAM;IAIpB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAI3B,KAAK,IAAI,IAAI;IAEb,MAAM,IAAI,GAAG;IAIb,IAAI,CAAC,OAAO,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;IAO1C,KAAK,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;CAO7C"}
|
package/build/connected.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OhosUsbConnectedDevice = void 0;
|
|
4
1
|
// @ts-nocheck
|
|
5
|
-
|
|
6
|
-
class OhosUsbConnectedDevice {
|
|
2
|
+
import { ConnectionState } from "@psdk/frame-father";
|
|
3
|
+
export class OhosUsbConnectedDevice {
|
|
4
|
+
options;
|
|
7
5
|
constructor(options) {
|
|
8
6
|
this.options = options;
|
|
9
7
|
}
|
|
10
8
|
get device() {
|
|
11
9
|
return this.options.device;
|
|
12
10
|
}
|
|
11
|
+
get ohosUsb() {
|
|
12
|
+
return this.options.ohosUsb;
|
|
13
|
+
}
|
|
13
14
|
canRead() {
|
|
14
15
|
return true;
|
|
15
16
|
}
|
|
16
17
|
connectionState() {
|
|
17
|
-
return this.
|
|
18
|
+
return this.ohosUsb.isOpened() ? ConnectionState.CONNECTED : ConnectionState.DISCONNECTED;
|
|
18
19
|
}
|
|
19
20
|
deviceName() {
|
|
20
|
-
|
|
21
|
-
return (_a = this.options.ohosUsb.deviceName()) !== null && _a !== void 0 ? _a : '';
|
|
21
|
+
return this.ohosUsb.deviceName() ?? '';
|
|
22
22
|
}
|
|
23
23
|
disconnect() {
|
|
24
|
-
this.
|
|
24
|
+
this.ohosUsb.closeUsb();
|
|
25
25
|
}
|
|
26
26
|
flush() { }
|
|
27
27
|
origin() {
|
|
@@ -29,7 +29,7 @@ class OhosUsbConnectedDevice {
|
|
|
29
29
|
}
|
|
30
30
|
read(options) {
|
|
31
31
|
const temp = new Uint8Array(512);
|
|
32
|
-
const length = this.
|
|
32
|
+
const length = this.ohosUsb.read_usb(temp);
|
|
33
33
|
const receive = temp.slice(0, length);
|
|
34
34
|
return Promise.resolve(receive);
|
|
35
35
|
}
|
|
@@ -37,7 +37,6 @@ class OhosUsbConnectedDevice {
|
|
|
37
37
|
if (!this.options) {
|
|
38
38
|
throw new Error('can not found OusbOptions.');
|
|
39
39
|
}
|
|
40
|
-
await this.
|
|
40
|
+
await this.ohosUsb.send_usb(data);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
-
exports.OhosUsbConnectedDevice = OhosUsbConnectedDevice;
|
package/build/index.js
CHANGED
|
@@ -1,18 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./connected"), exports);
|
|
18
|
-
__exportStar(require("./provider"), exports);
|
|
1
|
+
export * from './connected';
|
|
2
|
+
export * from './provider';
|
package/build/provider.d.ts
CHANGED
package/build/provider.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,eAAe,EAAC,MAAM,oBAAoB,CAAC;AAEnD,OAAO,GAAG,MAAM,kBAAkB,CAAC;AASnC,qBAAa,OAAO;IAClB,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,MAAM,CAAkB;IAChC,OAAO,CAAC,eAAe,CAAC,CAAyB;IAEjD,UAAU,IAAI,MAAM;IAIpB;;OAEG;
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAEA,OAAO,EAAC,eAAe,EAAC,MAAM,oBAAoB,CAAC;AAEnD,OAAO,GAAG,MAAM,kBAAkB,CAAC;AASnC,qBAAa,OAAO;IAClB,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,MAAM,CAAkB;IAChC,OAAO,CAAC,eAAe,CAAC,CAAyB;IAEjD,UAAU,IAAI,MAAM;IAIpB;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,eAAe,CAAC;IA2BzC,iBAAiB,CAAC,MAAM,EAAE,GAAG,CAAC,SAAS,GAAG,OAAO;IAoCjD;;;;OAIG;IACH,QAAQ,IAAI,OAAO;IAInB;;OAEG;IACH,QAAQ;IAWF,QAAQ,CAAC,QAAQ,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC;IAuDtD;;;;;OAKG;IACH,QAAQ,CAAC,SAAS,EAAE,UAAU,GAAG,MAAM;CAiBxC"}
|
package/build/provider.js
CHANGED
|
@@ -1,43 +1,38 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.OhosUsb = void 0;
|
|
7
1
|
// @ts-nocheck
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
import { OhosUsbConnectedDevice } from "./connected";
|
|
3
|
+
import usb from '@ohos.usbManager';
|
|
10
4
|
let currentDevice;
|
|
11
5
|
let mUsbInterface;
|
|
12
6
|
let mUsbEndpointOut;
|
|
13
7
|
let mUsbEndpointIn;
|
|
14
8
|
let mUsbDeviceConnection;
|
|
15
|
-
class OhosUsb {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
9
|
+
export class OhosUsb {
|
|
10
|
+
VIDQR = 0x09C5;
|
|
11
|
+
VIDMY = 0x09C6;
|
|
12
|
+
VIDIP = 0x353D;
|
|
13
|
+
isOpen = false;
|
|
14
|
+
connectedDevice;
|
|
22
15
|
deviceName() {
|
|
23
|
-
|
|
24
|
-
return (_a = currentDevice.name) !== null && _a !== void 0 ? _a : '';
|
|
16
|
+
return currentDevice.name ?? '';
|
|
25
17
|
}
|
|
26
18
|
/**
|
|
27
19
|
* 打开USB端口
|
|
28
20
|
*/
|
|
29
|
-
openUsb() {
|
|
30
|
-
let devicesList =
|
|
21
|
+
async openUsb() {
|
|
22
|
+
let devicesList = usb.getDevices();
|
|
31
23
|
for (let i = 0; i < devicesList.length; i++) {
|
|
32
24
|
if ((devicesList[i].vendorId == this.VIDQR) || (devicesList[i].vendorId == this.VIDMY) || (devicesList[i].vendorId == this.VIDIP)) {
|
|
33
25
|
currentDevice = devicesList[i];
|
|
34
|
-
if (!
|
|
35
|
-
|
|
26
|
+
if (!usb.hasRight(devicesList[i].name)) {
|
|
27
|
+
const hasRight = await usb.requestRight(devicesList[i].name);
|
|
28
|
+
if (!hasRight) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
36
31
|
}
|
|
37
32
|
try {
|
|
38
33
|
if (this.initCommunication(devicesList[i])) {
|
|
39
|
-
this.connectedDevice = new
|
|
40
|
-
|
|
34
|
+
this.connectedDevice = new OhosUsbConnectedDevice({
|
|
35
|
+
ohosUsb: this,
|
|
41
36
|
device: currentDevice,
|
|
42
37
|
});
|
|
43
38
|
return this.connectedDevice;
|
|
@@ -61,10 +56,10 @@ class OhosUsb {
|
|
|
61
56
|
for (let i = 0; i < usbInterface.endpoints.length; i++) {
|
|
62
57
|
let ep = usbInterface.endpoints[i];
|
|
63
58
|
if (ep.type == 2) {
|
|
64
|
-
if (ep.direction ==
|
|
59
|
+
if (ep.direction == usb.USBRequestDirection.USB_REQUEST_DIR_TO_DEVICE) {
|
|
65
60
|
mUsbEndpointOut = ep;
|
|
66
61
|
}
|
|
67
|
-
else if (ep.direction ==
|
|
62
|
+
else if (ep.direction == usb.USBRequestDirection.USB_REQUEST_DIR_FROM_DEVICE) {
|
|
68
63
|
mUsbEndpointIn = ep;
|
|
69
64
|
}
|
|
70
65
|
}
|
|
@@ -76,7 +71,7 @@ class OhosUsb {
|
|
|
76
71
|
return false;
|
|
77
72
|
}
|
|
78
73
|
else {
|
|
79
|
-
if (!
|
|
74
|
+
if (!usb.hasRight(currentDevice.name)) {
|
|
80
75
|
this.isOpen = false;
|
|
81
76
|
return false;
|
|
82
77
|
}
|
|
@@ -103,8 +98,8 @@ class OhosUsb {
|
|
|
103
98
|
mUsbEndpointIn = null;
|
|
104
99
|
mUsbEndpointOut = null;
|
|
105
100
|
if (mUsbDeviceConnection != null) {
|
|
106
|
-
|
|
107
|
-
|
|
101
|
+
usb.releaseInterface(mUsbDeviceConnection, mUsbInterface);
|
|
102
|
+
usb.closePipe(mUsbDeviceConnection);
|
|
108
103
|
mUsbDeviceConnection = null;
|
|
109
104
|
this.isOpen = false;
|
|
110
105
|
}
|
|
@@ -118,13 +113,13 @@ class OhosUsb {
|
|
|
118
113
|
let index = 0;
|
|
119
114
|
if (currentDevice != null) {
|
|
120
115
|
if (mUsbDeviceConnection == null) {
|
|
121
|
-
|
|
122
|
-
mUsbDeviceConnection =
|
|
123
|
-
let ret =
|
|
116
|
+
await usb.requestRight(currentDevice.name);
|
|
117
|
+
mUsbDeviceConnection = usb.connectDevice(currentDevice);
|
|
118
|
+
let ret = usb.claimInterface(mUsbDeviceConnection, mUsbInterface, true);
|
|
124
119
|
}
|
|
125
120
|
if (mUsbEndpointOut != null) {
|
|
126
121
|
if (count == 1) {
|
|
127
|
-
let result = await
|
|
122
|
+
let result = await usb.bulkTransfer(mUsbDeviceConnection, mUsbEndpointOut, sendData, 1500);
|
|
128
123
|
if (result == -1) {
|
|
129
124
|
return false;
|
|
130
125
|
}
|
|
@@ -136,7 +131,7 @@ class OhosUsb {
|
|
|
136
131
|
for (let j = 0; j < yushu; j++) {
|
|
137
132
|
temp[j] = sendData[index + j];
|
|
138
133
|
}
|
|
139
|
-
let result = await
|
|
134
|
+
let result = await usb.bulkTransfer(mUsbDeviceConnection, mUsbEndpointOut, temp, 1500);
|
|
140
135
|
if (result == -1) {
|
|
141
136
|
return false;
|
|
142
137
|
}
|
|
@@ -147,7 +142,7 @@ class OhosUsb {
|
|
|
147
142
|
for (let j = 0; j < size; j++) {
|
|
148
143
|
temp[j] = sendData[index + j];
|
|
149
144
|
}
|
|
150
|
-
let result = await
|
|
145
|
+
let result = await usb.bulkTransfer(mUsbDeviceConnection, mUsbEndpointOut, temp, 1500);
|
|
151
146
|
index += size;
|
|
152
147
|
if (result == -1) {
|
|
153
148
|
return false;
|
|
@@ -175,11 +170,11 @@ class OhosUsb {
|
|
|
175
170
|
*/
|
|
176
171
|
read_usb(tempBytes) {
|
|
177
172
|
if (mUsbDeviceConnection == null) {
|
|
178
|
-
|
|
179
|
-
mUsbDeviceConnection =
|
|
180
|
-
let ret =
|
|
173
|
+
await usb.requestRight(currentDevice.name);
|
|
174
|
+
mUsbDeviceConnection = usb.connectDevice(currentDevice);
|
|
175
|
+
let ret = usb.claimInterface(mUsbDeviceConnection, mUsbInterface, true);
|
|
181
176
|
}
|
|
182
|
-
|
|
177
|
+
usb.bulkTransfer(mUsbDeviceConnection, mUsbEndpointIn, tempBytes, 15000).then((dataLength) => {
|
|
183
178
|
if (dataLength >= 0) {
|
|
184
179
|
return dataLength;
|
|
185
180
|
}
|
|
@@ -192,4 +187,3 @@ class OhosUsb {
|
|
|
192
187
|
return 0;
|
|
193
188
|
}
|
|
194
189
|
}
|
|
195
|
-
exports.OhosUsb = OhosUsb;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@psdk/ohos-usb",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.19",
|
|
4
4
|
"description": "psdk - ohos usb device support",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -24,12 +24,12 @@
|
|
|
24
24
|
"typescript": "^5.5.3"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@psdk/frame-father": "0.5.
|
|
27
|
+
"@psdk/frame-father": "0.5.19",
|
|
28
28
|
"await-timeout": "^1.1.1"
|
|
29
29
|
},
|
|
30
30
|
"files": [
|
|
31
31
|
"build",
|
|
32
32
|
"!build/browser/psdk*"
|
|
33
33
|
],
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "506be0edc5199fa87cb69af7c32b84f9e106b426"
|
|
35
35
|
}
|