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 { USBHead } from './USBHead'; 17import { debug, log } from '../../log/Log'; 18import { PayloadHead } from './PayloadHead'; 19import { Serialize } from '../common/Serialize'; 20import { Utils } from '../common/Utils'; 21import { HdcCommand } from '../hdcclient/HdcCommand'; 22 23export class DataMessage extends Object { 24 body?: DataView; 25 26 private _usbHead: USBHead; 27 private _channelId: number = -1; 28 private _result: string = ''; 29 private _channelClose: boolean = false; 30 private _resArrayBuffer: ArrayBuffer | undefined; 31 private _commandFlag: number = -1; 32 33 constructor(usbHead: USBHead, body?: DataView) { 34 super(); 35 this._usbHead = usbHead; 36 this.body = body; 37 if (this.body) { 38 this.splitData(); 39 } 40 } 41 42 splitData(): void { 43 let playHeadArray = this.body!.buffer.slice(0, 11); 44 let resultPayloadHead: PayloadHead = PayloadHead.parsePlayHead(new DataView(playHeadArray)); 45 let headSize = resultPayloadHead.headSize; 46 let dataSize = resultPayloadHead.dataSize; 47 let resultPlayProtectBuffer = this.body!.buffer.slice(11, 11 + headSize); 48 // @ts-ignore 49 let payloadProtect = Serialize.parsePayloadProtect(resultPlayProtectBuffer); 50 this._channelId = payloadProtect.channelId; 51 this._commandFlag = payloadProtect.commandFlag; 52 if (payloadProtect.commandFlag === HdcCommand.CMD_KERNEL_CHANNEL_CLOSE) { 53 log(`commandFlag: ${payloadProtect.commandFlag}`); 54 this._channelClose = true; 55 } else { 56 if (dataSize > 0) { 57 // @ts-ignore 58 this._resArrayBuffer = this.body!.buffer.slice(11 + headSize, 11 + headSize + dataSize); 59 } 60 } 61 } 62 63 public getChannelId(): number { 64 return this._channelId; 65 } 66 67 public getData(): ArrayBuffer | undefined { 68 return this._resArrayBuffer; 69 } 70 71 public getDataToString(): string { 72 let textDecoder = new TextDecoder(); 73 this._result = textDecoder.decode(this._resArrayBuffer); 74 return this._result; 75 } 76 77 get usbHead(): USBHead { 78 return this._usbHead; 79 } 80 81 set usbHead(value: USBHead) { 82 this._usbHead = value; 83 } 84 85 get channelId(): number { 86 return this._channelId; 87 } 88 89 set channelId(value: number) { 90 this._channelId = value; 91 } 92 93 get result(): string { 94 return this._result; 95 } 96 97 set result(value: string) { 98 this._result = value; 99 } 100 101 get channelClose(): boolean { 102 return this._channelClose; 103 } 104 105 set channelClose(value: boolean) { 106 this._channelClose = value; 107 } 108 109 get resArrayBuffer(): ArrayBuffer | undefined { 110 return this._resArrayBuffer; 111 } 112 113 set resArrayBuffer(value: ArrayBuffer | undefined) { 114 this._resArrayBuffer = value; 115 } 116 117 get commandFlag(): number { 118 return this._commandFlag; 119 } 120 121 set commandFlag(value: number) { 122 this._commandFlag = value; 123 } 124 125 toString(): string { 126 return `usbHead: ${this._usbHead} channelId: ${this._channelId} result: ${this._result} channelClose: ${this._channelClose} commandFlag: ${this._commandFlag}`; 127 } 128} 129