@psdk/device-webusb 0.7.15 → 0.7.17

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/build/index.d.ts CHANGED
@@ -9,6 +9,7 @@ export declare class Webusb {
9
9
  private inputWriteEndpoint?;
10
10
  private state;
11
11
  private device?;
12
+ private claimedInterfaceNumber?;
12
13
  constructor(options: {
13
14
  /**
14
15
  * filters
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,eAAe,EAAmB,WAAW,EAAC,MAAM,oBAAoB,CAAC;AAEjF,qBAAa,MAAM;IACjB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsB;IAC9C,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;IACvC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,aAAa,CAAS;IAC9B,OAAO,CAAC,iBAAiB,CAAC,CAAS;IACnC,OAAO,CAAC,kBAAkB,CAAC,CAAS;IAEpC,OAAO,CAAC,KAAK,CAAkB;IAC/B,OAAO,CAAC,MAAM,CAAC,CAAM;gBAEF,OAAO,EAAE;QAC1B;;WAEG;QACH,OAAO,EAAE,KAAK,CAAC,YAAY,CAAC,CAAC;QAC7B;;WAEG;QACH,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB;;WAEG;QACH,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB;;WAEG;QACH,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB;;WAEG;QACH,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB;WAYa,UAAU,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM;WAIxC,WAAW,CAAC,OAAO,EAAE,KAAK,CAAC,YAAY,CAAC,GAAG,MAAM;IAIlD,OAAO,IAAI,OAAO,CAAC,eAAe,CAAC;YAkClC,eAAe;CA2D9B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vB;;WAEG;QACH,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB;WAYa,UAAU,CAAC,MAAM,EAAE,YAAY,GAAG,MAAM;WAIxC,WAAW,CAAC,OAAO,EAAE,KAAK,CAAC,YAAY,CAAC,GAAG,MAAM;IAIlD,OAAO,IAAI,OAAO,CAAC,eAAe,CAAC;YAoClC,eAAe;CA8H9B;AAED,qBAAa,iBAAkB,SAAQ,WAAW;IAChD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAErB,OAAO,CAAC,EAAE;QACpB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB;;WAEG;QACH,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB;WAKa,GAAG,IAAI,iBAAiB;IAItC,IAAI,UAAU,IAAI,MAAM,CAEvB;CACF;AAED,qBAAa,YAAY;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;gBAEP,OAAO,EAAE;QAC1B;;WAEG;QACH,QAAQ,EAAE,MAAM,CAAC;QACjB;;WAEG;QACH,SAAS,CAAC,EAAE,MAAM,CAAC;KACpB;CAIF"}
package/build/index.js CHANGED
@@ -12,6 +12,7 @@ class Webusb {
12
12
  inputWriteEndpoint;
13
13
  state;
14
14
  device;
15
+ claimedInterfaceNumber;
15
16
  constructor(options) {
16
17
  if (options.filters.length == 0) {
17
18
  throw new Error('Missing filters');
@@ -42,8 +43,10 @@ class Webusb {
42
43
  throw new Error('Failed to get device');
43
44
  await device.open();
44
45
  await device.selectConfiguration(this.configuration);
45
- await device.claimInterface(device.configuration.interfaces[this.interface].interfaceNumber);
46
- const alternates = device.configuration.interfaces[0].alternates[0];
46
+ const claimedInterface = device.configuration.interfaces[this.interface];
47
+ this.claimedInterfaceNumber = claimedInterface.interfaceNumber;
48
+ await device.claimInterface(this.claimedInterfaceNumber);
49
+ const alternates = claimedInterface.alternates[0];
47
50
  const inEndpoints = alternates.endpoints.filter(endpoint => endpoint.direction === 'in');
48
51
  const outEndpoints = alternates.endpoints.filter(endpoint => endpoint.direction === 'out');
49
52
  this.readEndpoint = inEndpoints && inEndpoints.length ? inEndpoints[0].endpointNumber : this.inputReadEndpoint;
@@ -63,17 +66,71 @@ class Webusb {
63
66
  async connectedDevice() {
64
67
  const _device = () => this.device;
65
68
  const _connectionState = () => this.state;
69
+ const _setConnectionState = (state) => this.state = state;
66
70
  const _readEndpoint = () => this.readEndpoint;
67
71
  const _writeEndpoint = () => this.writeEndpoint;
72
+ const _claimedInterfaceNumber = () => this.claimedInterfaceNumber;
73
+ const readBuffer = [];
74
+ let notifyCallback;
75
+ let notifyRunning = false;
76
+ let notifyStopped = false;
77
+ const toUint8Array = (data) => {
78
+ const arrs = [];
79
+ const len = data?.byteLength ?? 0;
80
+ for (let i = 0; i < len; i++) {
81
+ arrs.push(data.getUint8(i));
82
+ }
83
+ return new Uint8Array(arrs);
84
+ };
85
+ const transferIn = async (readLength) => {
86
+ const device = _device();
87
+ const result = await device.transferIn(_readEndpoint(), readLength);
88
+ const { status, data } = result;
89
+ if (status != 'ok') {
90
+ throw new Error(`[${status}]: Failed to read data. about status code you can read this https://developer.mozilla.org/en-US/docs/Web/API/USBInTransferResult`);
91
+ }
92
+ return toUint8Array(data);
93
+ };
94
+ const startNotifyLoop = () => {
95
+ if (notifyRunning) {
96
+ return;
97
+ }
98
+ notifyStopped = false;
99
+ notifyRunning = true;
100
+ (async () => {
101
+ try {
102
+ while (!notifyStopped && _connectionState() == frame_father_1.ConnectionState.CONNECTED) {
103
+ const data = await transferIn(WebusbReadOptions.def().readLength);
104
+ if (notifyStopped || _connectionState() != frame_father_1.ConnectionState.CONNECTED) {
105
+ break;
106
+ }
107
+ if (data.length == 0) {
108
+ continue;
109
+ }
110
+ if (notifyCallback) {
111
+ await notifyCallback(data);
112
+ }
113
+ readBuffer.push(data);
114
+ }
115
+ }
116
+ catch (e) {
117
+ }
118
+ finally {
119
+ notifyRunning = false;
120
+ }
121
+ })();
122
+ };
68
123
  return new class {
69
124
  notify(callback) {
125
+ notifyCallback = callback;
126
+ startNotifyLoop();
70
127
  }
71
128
  origin() {
72
129
  return _device();
73
130
  }
74
131
  connectionState() {
75
132
  const device = _device();
76
- if (!device.opened) {
133
+ if (!device || !device.opened) {
77
134
  return frame_father_1.ConnectionState.DISCONNECTED;
78
135
  }
79
136
  return _connectionState();
@@ -83,31 +140,44 @@ class Webusb {
83
140
  return device.productName;
84
141
  }
85
142
  async disconnect() {
86
- // todo: disconnect webusb
143
+ const device = _device();
144
+ notifyStopped = true;
145
+ _setConnectionState(frame_father_1.ConnectionState.DISCONNECTED);
146
+ if (!device) {
147
+ return;
148
+ }
149
+ const claimedInterfaceNumber = _claimedInterfaceNumber();
150
+ if (claimedInterfaceNumber !== undefined && device.releaseInterface) {
151
+ try {
152
+ await device.releaseInterface(claimedInterfaceNumber);
153
+ }
154
+ catch (e) {
155
+ }
156
+ }
157
+ if (device.close) {
158
+ try {
159
+ await device.close();
160
+ }
161
+ catch (e) {
162
+ }
163
+ }
87
164
  }
88
165
  canRead() {
89
166
  return true;
90
167
  }
91
168
  async read(options) {
92
- const device = _device();
93
- const _options = options ?? WebusbReadOptions.def();
94
- const result = await device.transferIn(_readEndpoint(), _options.readLength);
95
- const { status, data } = result;
96
- if (status != 'ok') {
97
- throw new Error(`[${status}]: Failed to read data. about status code you can read this https://developer.mozilla.org/en-US/docs/Web/API/USBInTransferResult`);
169
+ if (readBuffer.length > 0) {
170
+ return readBuffer.shift();
98
171
  }
99
- const arrs = [];
100
- const len = data.byteLength;
101
- for (let i = 0; i < len; i++) {
102
- arrs.push(data.getUint8(i));
103
- }
104
- return new Uint8Array(arrs);
172
+ const _options = options ?? WebusbReadOptions.def();
173
+ return transferIn(_options.readLength);
105
174
  }
106
175
  async write(data) {
107
176
  const device = _device();
108
177
  await device.transferOut(_writeEndpoint(), data);
109
178
  }
110
179
  flush() {
180
+ readBuffer.splice(0, readBuffer.length);
111
181
  }
112
182
  };
113
183
  }