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.js'; 17import { debug, log } from '../../log/Log.js'; 18import { PayloadHead } from './PayloadHead.js'; 19import { Serialize } from '../common/Serialize.js'; 20import { Utils } from '../common/Utils.js'; 21import { HdcCommand } from '../hdcclient/HdcCommand.js'; 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() { 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 let payloadProtect = Serialize.parsePayloadProtect(resultPlayProtectBuffer); 49 this._channelId = payloadProtect.channelId; 50 this._commandFlag = payloadProtect.commandFlag; 51 if (payloadProtect.commandFlag == HdcCommand.CMD_KERNEL_CHANNEL_CLOSE) { 52 log('commandFlag: ' + payloadProtect.commandFlag); 53 this._channelClose = true; 54 } else { 55 if (dataSize > 0) { 56 this._resArrayBuffer = this.body!.buffer.slice(11 + headSize, 11 + headSize + dataSize); 57 } 58 } 59 } 60 61 public getChannelId(): number { 62 return this._channelId; 63 } 64 65 public getData(): ArrayBuffer | undefined { 66 return this._resArrayBuffer; 67 } 68 69 public getDataToString(): string { 70 let textDecoder = new TextDecoder(); 71 this._result = textDecoder.decode(this._resArrayBuffer); 72 return this._result; 73 } 74 75 get usbHead(): USBHead { 76 return this._usbHead; 77 } 78 79 set usbHead(value: USBHead) { 80 this._usbHead = value; 81 } 82 83 get channelId(): number { 84 return this._channelId; 85 } 86 87 set channelId(value: number) { 88 this._channelId = value; 89 } 90 91 get result(): string { 92 return this._result; 93 } 94 95 set result(value: string) { 96 this._result = value; 97 } 98 99 get channelClose(): boolean { 100 return this._channelClose; 101 } 102 103 set channelClose(value: boolean) { 104 this._channelClose = value; 105 } 106 107 get resArrayBuffer(): ArrayBuffer | undefined { 108 return this._resArrayBuffer; 109 } 110 111 set resArrayBuffer(value: ArrayBuffer | undefined) { 112 this._resArrayBuffer = value; 113 } 114 115 get commandFlag(): number { 116 return this._commandFlag; 117 } 118 119 set commandFlag(value: number) { 120 this._commandFlag = value; 121 } 122 123 toString(): string { 124 return ( 125 'usbHead: ' + 126 this._usbHead + 127 ' channelId: ' + 128 this._channelId + 129 ' result: ' + 130 this._result + 131 ' channelClose: ' + 132 this._channelClose + 133 ' commandFlag: ' + 134 this._commandFlag 135 ); 136 } 137} 138