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 */ 15export class Cmd { 16 /** 17 * exec objdump to disassembling binary and find addr to show 100 line 18 * @param command obj dump command 19 * @param addr addr of select line 20 * @param callback result callback 21 */ 22 static execObjDump(command: string, addr: string, callback: Function) { 23 const data = {cmd: command, addr: addr}; 24 let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/exec`; 25 fetch(uri, { 26 method: 'POST', 27 headers: { 28 'Content-Type': 'application/json', 29 }, 30 body: JSON.stringify(data), 31 }).then(response => { 32 if (response.ok) { 33 let result = response.text(); 34 result.then(output => { 35 callback(output); 36 }); 37 } 38 }); 39 } 40 41 static execHdcCmd(command: string, callback: Function) { 42 const data = { 43 cmd: command, 44 tag: "shell" 45 }; 46 let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/hdcCmd`; 47 fetch(uri, { 48 method: 'POST', 49 headers: { 50 'Content-Type': 'application/json', 51 }, 52 body: JSON.stringify(data), 53 }).then(response => { 54 if (response.ok) { 55 let result = response.text(); 56 result.then(output => { 57 callback(output); 58 }); 59 } 60 }); 61 } 62 63 static async execFileRecv(command: string, filePath: string, callback: Function) { 64 let fileName = filePath.substring(filePath.lastIndexOf("/") + 1) 65 const data = { 66 cmd: command, 67 tag: "file", 68 fileName: fileName 69 }; 70 let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/hdcCmd`; 71 let buf = await fetch(uri, { 72 method: 'POST', headers: { 73 'Content-Type': 'application/json', 74 }, 75 body: JSON.stringify(data), 76 }).then(res => res.arrayBuffer()); 77 callback(buf); 78 } 79 80 static execHdcTraceCmd(command: string, serialNumber: string, callback: Function) { 81 const data = { 82 cmd: command, 83 tag: "hiprofiler_cmd", 84 serialNumber: serialNumber 85 }; 86 let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/hdcCmd`; 87 fetch(uri, { 88 method: 'POST', 89 headers: { 90 'Content-Type': 'application/json', 91 }, 92 body: JSON.stringify(data), 93 }).then(response => { 94 if (response.ok) { 95 let result = response.text(); 96 result.then(output => { 97 callback(output); 98 }); 99 } 100 }); 101 } 102 103 static formatString(string: string, params: string[]) { 104 if (params.length == 0) { 105 return string; 106 } 107 for (let i = 0; i < params.length; i++) { 108 string = string.replace(new RegExp('\\{' + i + '\\}', 'g'), params[i]); 109 } 110 return string; 111 } 112 113 static showSaveFile(callback: Function) { 114 let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/showSaveDialog`; 115 fetch(uri, { 116 method: 'GET' 117 }).then(response => { 118 if (response.ok) { 119 let result = response.text(); 120 result.then(output => { 121 callback(output); 122 }); 123 } 124 }); 125 } 126 127 static uploadFile(fd: FormData, callback: Function) { 128 let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/upload`; 129 fetch(uri, { 130 method: 'POST', 131 body: fd, 132 }).then(response => { 133 callback(response); 134 }); 135 } 136 137 static copyFile(fileName: string, distFile: string, callback: Function) { 138 const data = { 139 filename: fileName, 140 distfile: distFile, 141 }; 142 let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/copyfile`; 143 fetch(uri, { 144 method: 'POST', 145 headers: { 146 'Content-Type': 'application/json', 147 }, 148 body: JSON.stringify(data), 149 }).then(response => { 150 callback(response); 151 }); 152 } 153 154 static async openFileDialog() { 155 let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/showOpenDialog`; 156 let res = await fetch(uri, {method: 'POST'}) 157 let result = res.ok ? await res.text() : ""; 158 return result; 159 } 160}