1/** 2 * Copyright 2022 The Pigweed Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * https://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17/** @see https://wicg.github.io/serial/#paritytype-enum */ 18type ParityType = 'none' | 'even' | 'odd'; 19 20/** @see https://wicg.github.io/serial/#serialoptions-dictionary */ 21interface SerialOptions { 22 baudrate: number; 23 databits?: number; 24 stopbits?: number; 25 parity?: ParityType; 26 buffersize?: number; 27 rtscts?: boolean; 28} 29 30/** @see https://wicg.github.io/serial/#serialport-interface */ 31declare class SerialPort { 32 readonly readable: ReadableStream<Uint8Array>; 33 readonly writable: WritableStream<Uint8Array>; 34 35 open(options?: SerialOptions): Promise<void>; 36 close(): void; 37} 38 39/** 40 * @see https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/serial/serial_port_filter.idl 41 */ 42interface SerialPortFilter { 43 usbVendorId?: number; 44 usbProductId?: number; 45} 46 47/** 48 * @see https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/serial/serial_port_request_options.idl 49 */ 50interface SerialPortRequestOptions { 51 filters?: SerialPortFilter[]; 52} 53 54/** 55 * @see https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/serial/serial_connection_event_init.idl 56 */ 57interface SerialConnectionEventInit extends EventInit { 58 port: SerialPort; 59} 60 61/** 62 * @see https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/modules/serial/serial_connection_event.idl 63 */ 64declare class SerialConnectionEvent extends Event { 65 constructor(type: string, eventInitDict: SerialConnectionEventInit); 66 readonly port: SerialPort; 67} 68 69/** @see https://wicg.github.io/serial/#serial-interface */ 70declare class Serial extends EventTarget { 71 onconnect(): ((this: this, ev: SerialConnectionEvent) => any) | null; 72 ondisconnect(): ((this: this, ev: SerialConnectionEvent) => any) | null; 73 getPorts(): Promise<SerialPort[]>; 74 requestPort(options?: SerialPortRequestOptions): Promise<SerialPort>; 75 addEventListener( 76 type: 'connect' | 'disconnect', 77 listener: (this: this, ev: SerialConnectionEvent) => any, 78 useCapture?: boolean, 79 ): void; 80 addEventListener( 81 type: string, 82 listener: EventListenerOrEventListenerObject | null, 83 options?: boolean | AddEventListenerOptions, 84 ): void; 85 removeEventListener( 86 type: 'connect' | 'disconnect', 87 callback: (this: this, ev: SerialConnectionEvent) => any, 88 useCapture?: boolean, 89 ): void; 90 removeEventListener( 91 type: string, 92 callback: EventListenerOrEventListenerObject | null, 93 options?: EventListenerOptions | boolean, 94 ): void; 95} 96 97/** @see https://wicg.github.io/serial/#extensions-to-the-navigator-interface */ 98interface Navigator { 99 readonly serial: Serial; 100} 101 102/** 103 * @see https://wicg.github.io/serial/#extensions-to-workernavigator-interface 104 */ 105interface WorkerNavigator { 106 readonly serial: Serial; 107} 108 109export type { 110 Navigator, 111 SerialPortFilter, 112 Serial, 113 SerialOptions, 114 SerialConnectionEvent, 115 SerialPortRequestOptions, 116 SerialPort, 117}; 118