• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import {TransmissionInterface} from "./TransmissionInterface.js";
17import {PACKET_FLAG, USB_PACKET_FLAG} from "../common/ConstantType.js";
18import {USBHead} from "../message/USBHead.js";
19import {DataMessage} from "../message/DataMessage.js";
20import {DataListener} from "../hdcclient/DataListener.js";
21import {PayloadProtect} from "../message/PayloadProtect.js";
22import {Serialize} from "../common/Serialize.js";
23import {PayloadHead} from "../message/PayloadHead.js";
24import {UsbProtocolOption} from "../hdcclient/UsbProtocolOption.js";
25import {toHex16} from "../common/BaseConversion.js";
26import {error, log} from "../../log/Log.js";
27
28export class DataProcessing {
29    private readonly transmissionChannel: TransmissionInterface;
30    private dataListener: DataListener;
31    public readData = true;
32    private static usbHeadSize: number = 11;
33    private static vCode: number = 0x09;
34    private static checkSum: number = 0;
35
36    constructor(dataListener: DataListener, transmissionChannel: TransmissionInterface) {
37        this.dataListener = dataListener;
38        this.transmissionChannel = transmissionChannel;
39    }
40
41    public async startReadData(): Promise<void> {
42        try {
43            while (this.readData) {
44                let usbHead = await this.readUsbHead();
45                if (usbHead != null) {
46                    let dataSize = usbHead!.dataSize;
47                    if (dataSize > 0) {
48                        let body = await this.readBody(dataSize);
49                        let message = new DataMessage(usbHead, body);
50                        this.dataListener.createDataMessage(message);
51                    } else {
52                        let message = new DataMessage(usbHead);
53                        this.dataListener.createDataMessage(message);
54                    }
55                } else {
56                    log("head is null")
57                }
58            }
59        } catch (e) {
60            let ubsHead = new USBHead([-1, -1], -1, -1, -1);
61            let message = new DataMessage(ubsHead);
62            this.dataListener.createDataMessage(message);
63            error("error", e)
64        }
65    }
66
67    public async readUsbHead(): Promise<USBHead | null> {
68        let res = await this.transmissionChannel.readData(USBHead.getUSBHeadLength());
69        if (res) {
70            let useHead: USBHead = USBHead.parseHeadData(res);
71            return useHead;
72        }
73        return null;
74    }
75
76    public async readBody(dataSize: number): Promise<DataView> {
77        let data = await this.transmissionChannel.readData(dataSize);
78        return data;
79    }
80
81    public async send(sessionId: number, channelId: number, commandFlag: number, data: Uint8Array, dataSize: number): Promise<boolean> {
82        let protectBuf: PayloadProtect = new PayloadProtect(channelId, commandFlag, DataProcessing.checkSum, DataProcessing.vCode);
83        let pbs = Serialize.serializePayloadProtect(protectBuf);
84        let payloadHead: PayloadHead = new PayloadHead([PACKET_FLAG.charCodeAt(0), PACKET_FLAG.charCodeAt(1)], [0, 0], 1, pbs.byteLength, dataSize)
85        let dataView = payloadHead.getDataView();
86        let playHeadArray = new Uint8Array(dataView.buffer);
87        let finalBufSize = dataView.byteLength + pbs.byteLength + dataSize;
88        let finalBuf = new Uint8Array(finalBufSize);
89        finalBuf.set(playHeadArray);
90        finalBuf.set(pbs, dataView.byteLength)
91        finalBuf.set(data, dataView.byteLength + pbs.byteLength);
92        if (this.transmissionChannel) {
93            let header = this.buildPacketHeader(sessionId, UsbProtocolOption.USB_OPTION_HEADER, finalBufSize);
94            await this.transmissionChannel.writeData(header);
95            await this.transmissionChannel.writeData(finalBuf);
96            return true;
97        } else {
98            this.stopReadData();
99            return false;
100        }
101    }
102
103    private buildPacketHeader(sessionId: number, option: number, dataSize: number): Uint8Array {
104        let head: USBHead = new USBHead([USB_PACKET_FLAG.charCodeAt(0), USB_PACKET_FLAG.charCodeAt(1)], option, sessionId, dataSize);
105        let dataView = head.getDataView();
106        return new Uint8Array(dataView.buffer);
107    }
108
109    public stopReadData() {
110        this.readData = false;
111    }
112
113}