• 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 { SpRecordTrace } from '../trace/component/SpRecordTrace.js';
17import { CmdConstant } from './CmdConstant.js';
18import { HdcDeviceManager } from '../hdc/HdcDeviceManager.js';
19
20export class Cmd {
21  static CmdSendPostUtils(uri: string, callback: Function, requestData: any) {
22    // @ts-ignore
23    if (window.useWb) {
24      return;
25    }
26    fetch(uri, {
27      method: 'POST',
28      headers: {
29        'Content-Type': 'application/json',
30      },
31      body: JSON.stringify(requestData),
32    }).then((response) => {
33      if (response.ok) {
34        let result = response.text();
35        result.then((output) => {
36          callback(output);
37        });
38      }
39    });
40  }
41
42  /**
43   * exec objdump to disassembling binary and find addr to show 100 line
44   * @param command obj dump command
45   * @param addr addr of select line
46   * @param callback result callback
47   */
48  static execObjDump(command: string, addr: string, callback: Function) {
49    // @ts-ignore
50    if (window.useWb) {
51      return;
52    }
53    const data = { cmd: command, addr: addr };
54    let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/exec`;
55    Cmd.CmdSendPostUtils(uri, callback, data);
56  }
57
58  static execHdcCmd(command: string, callback: Function) {
59    // @ts-ignore
60    if (window.useWb) {
61      return;
62    }
63    const data = {
64      cmd: command,
65      tag: 'shell',
66    };
67    let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/hdcCmd`;
68    Cmd.CmdSendPostUtils(uri, callback, data);
69  }
70
71  static async execFileRecv(command: string, filePath: string, callback: Function) {
72    // @ts-ignore
73    if (window.useWb) {
74      return;
75    }
76    let fileName = filePath.substring(filePath.lastIndexOf('/') + 1);
77    const data = {
78      cmd: command,
79      tag: 'file',
80      fileName: fileName,
81    };
82    let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/hdcCmd`;
83    let buf = await fetch(uri, {
84      method: 'POST',
85      headers: {
86        'Content-Type': 'application/json',
87      },
88      body: JSON.stringify(data),
89    }).then((res) => res.arrayBuffer());
90    callback(buf);
91  }
92
93  static execHdcTraceCmd(command: string, serialNumber: string, callback: Function) {
94    // @ts-ignore
95    if (window.useWb) {
96      return;
97    }
98    const data = {
99      cmd: command,
100      tag: 'hiprofiler_cmd',
101      serialNumber: serialNumber,
102    };
103    let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/hdcCmd`;
104    Cmd.CmdSendPostUtils(uri, callback, data);
105  }
106
107  static formatString(string: string, params: string[]) {
108    if (params.length == 0) {
109      return string;
110    }
111    for (let i = 0; i < params.length; i++) {
112      string = string.replace(new RegExp('\\{' + i + '\\}', 'g'), params[i]);
113    }
114    return string;
115  }
116
117  static showSaveFile(callback: Function) {
118    // @ts-ignore
119    if (window.useWb) {
120      return;
121    }
122    let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/showSaveDialog`;
123    fetch(uri, {
124      method: 'GET',
125    }).then((response) => {
126      if (response.ok) {
127        let result = response.text();
128        result.then((output) => {
129          callback(output);
130        });
131      }
132    });
133  }
134
135  static uploadFile(fd: FormData, callback: Function) {
136    // @ts-ignore
137    if (window.useWb) {
138      return;
139    }
140    let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/upload`;
141    fetch(uri, {
142      method: 'POST',
143      body: fd,
144    }).then((response) => {
145      callback(response);
146    });
147  }
148
149  static copyFile(fileName: string, distFile: string, callback: Function) {
150    // @ts-ignore
151    if (window.useWb) {
152      return;
153    }
154    const data = {
155      filename: fileName,
156      distfile: distFile,
157    };
158    let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/copyfile`;
159    fetch(uri, {
160      method: 'POST',
161      headers: {
162        'Content-Type': 'application/json',
163      },
164      body: JSON.stringify(data),
165    }).then((response) => {
166      callback(response);
167    });
168  }
169
170  static async openFileDialog() {
171    // @ts-ignore
172    if (window.useWb) {
173      return '';
174    }
175    let uri = `http://${window.location.host.split(':')[0]}:${window.location.port}/showOpenDialog`;
176    let res = await fetch(uri, { method: 'POST' });
177    let result = res.ok ? await res.text() : '';
178    return result;
179  }
180
181  static convertOutProcessList(res: string): string[] {
182    let processData: string[] = [];
183    if (res) {
184      let lineValues: string[] = res.replace(/\r\n/g, '\r').replace(/\n/g, '\r').split(/\r/);
185      for (let lineVal of lineValues) {
186        lineVal = lineVal.trim();
187        if (lineVal.indexOf('__progname') != -1 || lineVal.indexOf('CMD') != -1 || lineVal.length === 0) {
188          continue;
189        } else {
190          let process: string[] = lineVal.split(' ');
191          if (process.length == 2) {
192            processData.push(process[1] + '(' + process[0] + ')');
193          }
194        }
195      }
196    }
197    return processData;
198  }
199  static getDebugProcess(): Promise<string[]> {
200    return new Promise((resolve, reject) => {
201      if (SpRecordTrace.isVscode) {
202        let cmd = Cmd.formatString(CmdConstant.CMD_GET_DEBUG_PROCESS_DEVICES, [SpRecordTrace.serialNumber]);
203        Cmd.execHdcCmd(cmd, (res: string) => {
204          resolve(Cmd.convertOutProcessList(res));
205        });
206      } else {
207        HdcDeviceManager.connect(SpRecordTrace.serialNumber).then((conn) => {
208          if (conn) {
209            HdcDeviceManager.shellResultAsString(CmdConstant.CMD_GET_DEBUG_PROCESS, false).then((res) => {
210              resolve(Cmd.convertOutProcessList(res));
211            });
212          } else {
213            reject(-1);
214          }
215        });
216      }
217    });
218  }
219
220  static getProcess(): Promise<string[]> {
221    return new Promise((resolve, reject) => {
222      if (SpRecordTrace.isVscode) {
223        let cmd = Cmd.formatString(CmdConstant.CMD_GET_PROCESS_DEVICES, [SpRecordTrace.serialNumber]);
224        Cmd.execHdcCmd(cmd, (res: string) => {
225          resolve(Cmd.convertOutProcessList(res));
226        });
227      } else {
228        HdcDeviceManager.connect(SpRecordTrace.serialNumber).then((conn) => {
229          if (conn) {
230            HdcDeviceManager.shellResultAsString(CmdConstant.CMD_GET_PROCESS, false).then((res) => {
231              resolve(Cmd.convertOutProcessList(res));
232            });
233          } else {
234            reject(-1);
235          }
236        });
237      }
238    });
239  }
240}
241