@psdk/ohos-usb 0.5.13

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.
@@ -0,0 +1,21 @@
1
+ import { ConnectedDevice, ConnectionState, ReadOptions } from "@psdk/frame-father";
2
+ import { OhosUsb } from "./provider";
3
+ import usb from '@ohos.usbManager';
4
+ export interface OusbOptions {
5
+ ohosUsb: OhosUsb;
6
+ device: usb.USBDevice;
7
+ }
8
+ export declare class OhosUsbConnectedDevice implements ConnectedDevice {
9
+ private readonly options;
10
+ constructor(options: OusbOptions);
11
+ get device(): usb.USBDevice;
12
+ canRead(): boolean;
13
+ connectionState(): ConnectionState;
14
+ deviceName(): string;
15
+ disconnect(): Promise<void>;
16
+ flush(): void;
17
+ origin(): any;
18
+ read(options?: ReadOptions): Promise<Uint8Array>;
19
+ write(data: Uint8Array): Promise<void>;
20
+ }
21
+ //# sourceMappingURL=connected.d.ts.map
@@ -0,0 +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,EAAC,OAAO,EAAC,MAAM,YAAY,CAAC;AACnC,OAAO,GAAG,MAAM,kBAAkB,CAAC;AAEnC,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,OAAO,CAAA;IAChB,MAAM,EAAE,GAAG,CAAC,SAAS,CAAA;CACtB;AAED,qBAAa,sBAAuB,YAAW,eAAe;IAC5D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAc;gBAEnB,OAAO,EAAE,WAAW;IAIvC,IAAI,MAAM,IAAI,GAAG,CAAC,SAAS,CAE1B;IAED,OAAO,IAAI,OAAO;IAIlB,eAAe,IAAI,eAAe;IAIlC,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"}
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OhosUsbConnectedDevice = void 0;
4
+ // @ts-nocheck
5
+ const frame_father_1 = require("@psdk/frame-father");
6
+ class OhosUsbConnectedDevice {
7
+ constructor(options) {
8
+ this.options = options;
9
+ }
10
+ get device() {
11
+ return this.options.device;
12
+ }
13
+ canRead() {
14
+ return true;
15
+ }
16
+ connectionState() {
17
+ return this.options.ohosUsb.isOpened() ? frame_father_1.ConnectionState.CONNECTED : frame_father_1.ConnectionState.DISCONNECTED;
18
+ }
19
+ deviceName() {
20
+ var _a;
21
+ return (_a = this.options.ohosUsb.deviceName()) !== null && _a !== void 0 ? _a : '';
22
+ }
23
+ disconnect() {
24
+ this.options.ohosUsb.closeUsb();
25
+ }
26
+ flush() { }
27
+ origin() {
28
+ return this.device;
29
+ }
30
+ read(options) {
31
+ const temp = new Uint8Array(512);
32
+ const length = this.options.ohosUsb.read_usb(temp);
33
+ const receive = temp.slice(0, length);
34
+ return Promise.resolve(receive);
35
+ }
36
+ async write(data) {
37
+ if (!this.options) {
38
+ throw new Error('can not found OusbOptions.');
39
+ }
40
+ await this.options.ohosUsb.send_usb(data);
41
+ }
42
+ }
43
+ exports.OhosUsbConnectedDevice = OhosUsbConnectedDevice;
@@ -0,0 +1,3 @@
1
+ export * from './connected';
2
+ export * from './provider';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA"}
package/build/index.js ADDED
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
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);
@@ -0,0 +1,34 @@
1
+ import { ConnectedDevice } from "@psdk/frame-father";
2
+ import usb from '@ohos.usbManager';
3
+ export declare class OhosUsb {
4
+ private VIDQR;
5
+ private VIDMY;
6
+ private VIDIP;
7
+ private isOpen;
8
+ private connectedDevice?;
9
+ deviceName(): string;
10
+ /**
11
+ * 打开USB端口
12
+ */
13
+ openUsb(): ConnectedDevice;
14
+ initCommunication(device: usb.USBDevice): boolean;
15
+ /**
16
+ * 获得usb打开状态
17
+ *
18
+ * @return true:USB打开 false:USB关闭
19
+ */
20
+ isOpened(): boolean;
21
+ /**
22
+ * 关闭usb
23
+ */
24
+ closeUsb(): void;
25
+ send_usb(sendData: Uint8Array): Promise<boolean>;
26
+ /**
27
+ * 读取数据
28
+ *
29
+ * @param tempBytes 数据缓冲区
30
+ * @return 返回读取数据的长度
31
+ */
32
+ read_usb(tempBytes: Uint8Array): number;
33
+ }
34
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +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;IACH,OAAO,IAAI,eAAe;IAwB1B,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"}
@@ -0,0 +1,195 @@
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
+ // @ts-nocheck
8
+ const connected_1 = require("./connected");
9
+ const _ohos_usbManager_1 = __importDefault(require("@ohos.usbManager"));
10
+ let currentDevice;
11
+ let mUsbInterface;
12
+ let mUsbEndpointOut;
13
+ let mUsbEndpointIn;
14
+ let mUsbDeviceConnection;
15
+ class OhosUsb {
16
+ constructor() {
17
+ this.VIDQR = 0x09C5;
18
+ this.VIDMY = 0x09C6;
19
+ this.VIDIP = 0x353D;
20
+ this.isOpen = false;
21
+ }
22
+ deviceName() {
23
+ var _a;
24
+ return (_a = currentDevice.name) !== null && _a !== void 0 ? _a : '';
25
+ }
26
+ /**
27
+ * 打开USB端口
28
+ */
29
+ openUsb() {
30
+ let devicesList = _ohos_usbManager_1.default.getDevices();
31
+ for (let i = 0; i < devicesList.length; i++) {
32
+ if ((devicesList[i].vendorId == this.VIDQR) || (devicesList[i].vendorId == this.VIDMY) || (devicesList[i].vendorId == this.VIDIP)) {
33
+ currentDevice = devicesList[i];
34
+ if (!_ohos_usbManager_1.default.hasRight(devicesList[i].name)) {
35
+ _ohos_usbManager_1.default.requestRight(devicesList[i].name);
36
+ }
37
+ try {
38
+ if (this.initCommunication(devicesList[i])) {
39
+ this.connectedDevice = new connected_1.OhosUsbConnectedDevice({
40
+ ...this,
41
+ device: currentDevice,
42
+ });
43
+ return this.connectedDevice;
44
+ }
45
+ }
46
+ catch (e) {
47
+ return null;
48
+ }
49
+ }
50
+ }
51
+ return null;
52
+ }
53
+ initCommunication(device) {
54
+ let interfaceCount = device.configs[0].interfaces.length;
55
+ for (let interfaceIndex = 0; interfaceIndex < interfaceCount; interfaceIndex++) {
56
+ let usbInterface = device.configs[0].interfaces[interfaceIndex];
57
+ if (7 != usbInterface.clazz) {
58
+ continue;
59
+ }
60
+ mUsbInterface = usbInterface;
61
+ for (let i = 0; i < usbInterface.endpoints.length; i++) {
62
+ let ep = usbInterface.endpoints[i];
63
+ if (ep.type == 2) {
64
+ if (ep.direction == _ohos_usbManager_1.default.USBRequestDirection.USB_REQUEST_DIR_TO_DEVICE) {
65
+ mUsbEndpointOut = ep;
66
+ }
67
+ else if (ep.direction == _ohos_usbManager_1.default.USBRequestDirection.USB_REQUEST_DIR_FROM_DEVICE) {
68
+ mUsbEndpointIn = ep;
69
+ }
70
+ }
71
+ }
72
+ if ((null == mUsbEndpointIn) || (null == mUsbEndpointOut)) {
73
+ mUsbEndpointIn = null;
74
+ mUsbEndpointOut = null;
75
+ this.isOpen = false;
76
+ return false;
77
+ }
78
+ else {
79
+ if (!_ohos_usbManager_1.default.hasRight(currentDevice.name)) {
80
+ this.isOpen = false;
81
+ return false;
82
+ }
83
+ else {
84
+ this.isOpen = true;
85
+ return true;
86
+ }
87
+ }
88
+ }
89
+ return false;
90
+ }
91
+ /**
92
+ * 获得usb打开状态
93
+ *
94
+ * @return true:USB打开 false:USB关闭
95
+ */
96
+ isOpened() {
97
+ return this.isOpen;
98
+ }
99
+ /**
100
+ * 关闭usb
101
+ */
102
+ closeUsb() {
103
+ mUsbEndpointIn = null;
104
+ mUsbEndpointOut = null;
105
+ if (mUsbDeviceConnection != null) {
106
+ _ohos_usbManager_1.default.releaseInterface(mUsbDeviceConnection, mUsbInterface);
107
+ _ohos_usbManager_1.default.closePipe(mUsbDeviceConnection);
108
+ mUsbDeviceConnection = null;
109
+ this.isOpen = false;
110
+ }
111
+ }
112
+ async send_usb(sendData) {
113
+ let length = sendData.length;
114
+ let size = 512;
115
+ let count = 0;
116
+ let yushu = length % size;
117
+ count = (length + size - 1) / size;
118
+ let index = 0;
119
+ if (currentDevice != null) {
120
+ if (mUsbDeviceConnection == null) {
121
+ _ohos_usbManager_1.default.requestRight(currentDevice.name);
122
+ mUsbDeviceConnection = _ohos_usbManager_1.default.connectDevice(currentDevice);
123
+ let ret = _ohos_usbManager_1.default.claimInterface(mUsbDeviceConnection, mUsbInterface, true);
124
+ }
125
+ if (mUsbEndpointOut != null) {
126
+ if (count == 1) {
127
+ let result = await _ohos_usbManager_1.default.bulkTransfer(mUsbDeviceConnection, mUsbEndpointOut, sendData, 1500);
128
+ if (result == -1) {
129
+ return false;
130
+ }
131
+ }
132
+ else {
133
+ for (let i = 0; i < count; i++) {
134
+ if (yushu != 0 && i == (count - 1)) {
135
+ let temp = new Uint8Array(yushu);
136
+ for (let j = 0; j < yushu; j++) {
137
+ temp[j] = sendData[index + j];
138
+ }
139
+ let result = await _ohos_usbManager_1.default.bulkTransfer(mUsbDeviceConnection, mUsbEndpointOut, temp, 1500);
140
+ if (result == -1) {
141
+ return false;
142
+ }
143
+ index = 0;
144
+ }
145
+ else {
146
+ let temp = new Uint8Array(size);
147
+ for (let j = 0; j < size; j++) {
148
+ temp[j] = sendData[index + j];
149
+ }
150
+ let result = await _ohos_usbManager_1.default.bulkTransfer(mUsbDeviceConnection, mUsbEndpointOut, temp, 1500);
151
+ index += size;
152
+ if (result == -1) {
153
+ return false;
154
+ }
155
+ }
156
+ }
157
+ }
158
+ }
159
+ else {
160
+ console.log("mUsbEndpointOut == null需先获得usb权限");
161
+ return false;
162
+ }
163
+ }
164
+ else {
165
+ console.log("未检测到打印机设备");
166
+ return false;
167
+ }
168
+ return true;
169
+ }
170
+ /**
171
+ * 读取数据
172
+ *
173
+ * @param tempBytes 数据缓冲区
174
+ * @return 返回读取数据的长度
175
+ */
176
+ read_usb(tempBytes) {
177
+ if (mUsbDeviceConnection == null) {
178
+ _ohos_usbManager_1.default.requestRight(currentDevice.name);
179
+ mUsbDeviceConnection = _ohos_usbManager_1.default.connectDevice(currentDevice);
180
+ let ret = _ohos_usbManager_1.default.claimInterface(mUsbDeviceConnection, mUsbInterface, true);
181
+ }
182
+ _ohos_usbManager_1.default.bulkTransfer(mUsbDeviceConnection, mUsbEndpointIn, tempBytes, 15000).then((dataLength) => {
183
+ if (dataLength >= 0) {
184
+ return dataLength;
185
+ }
186
+ else {
187
+ return 0;
188
+ }
189
+ }).catch((error) => {
190
+ return 0;
191
+ });
192
+ return 0;
193
+ }
194
+ }
195
+ exports.OhosUsb = OhosUsb;
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@psdk/ohos-usb",
3
+ "version": "0.5.13",
4
+ "description": "psdk",
5
+ "main": "build/index.js",
6
+ "types": "build/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "package": "tsc",
10
+ "test": "jest",
11
+ "docs": "npx typedoc --out dist/docs index.ts"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "author": "",
17
+ "devDependencies": {
18
+ "@types/jest": "^29.5.12",
19
+ "@vercel/ncc": "^0.38.1",
20
+ "jest": "^29.7.0",
21
+ "ts-jest": "^29.1.5",
22
+ "ts-node": "^10.9.2",
23
+ "typedoc": "^0.26.4",
24
+ "typescript": "^5.5.3"
25
+ },
26
+ "dependencies": {
27
+ "@psdk/frame-father": "0.5.13",
28
+ "await-timeout": "^1.1.1"
29
+ },
30
+ "files": [
31
+ "build",
32
+ "!build/browser/psdk*"
33
+ ],
34
+ "gitHead": "79e71d930b142e5849dd7d6061562530060ead31"
35
+ }