1 /* 2 * Copyright (c) 2021 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 16 package ohos.devtools.datasources.transport.hdc; 17 18 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager; 19 import ohos.devtools.datasources.utils.session.service.SessionManager; 20 import org.apache.commons.lang3.StringUtils; 21 import org.apache.logging.log4j.LogManager; 22 import org.apache.logging.log4j.Logger; 23 24 import java.io.BufferedReader; 25 import java.io.Closeable; 26 import java.io.IOException; 27 import java.io.InputStream; 28 import java.io.InputStreamReader; 29 import java.nio.charset.Charset; 30 import java.util.ArrayList; 31 import java.util.HashMap; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.Objects; 35 36 import static ohos.devtools.datasources.utils.common.Constant.TIME_OUT; 37 38 /** 39 * Interact with commands on the device side 40 * 41 * @since 2021/5/19 16:39 42 */ 43 public class HdcWrapper { 44 private static final Logger LOGGER = LogManager.getLogger(HdcWrapper.class); 45 private static final HdcWrapper INSTANCE = new HdcWrapper(); 46 HdcWrapper()47 private HdcWrapper() { 48 } 49 50 /** 51 * Get an instance 52 * 53 * @return HdcWrapper 54 */ getInstance()55 public static HdcWrapper getInstance() { 56 return INSTANCE; 57 } 58 59 /** 60 * generate Cmd Head 61 * 62 * @param isLeakOhos judge use hdc or hdc_std 63 * @param deviceId select device id 64 * @return ArrayList <String> 65 */ generateDeviceCmdHead(boolean isLeakOhos, String deviceId)66 public ArrayList<String> generateDeviceCmdHead(boolean isLeakOhos, String deviceId) { 67 ArrayList<String> headCommand = new ArrayList<>(); 68 String pluginPath; 69 if (isLeakOhos) { 70 pluginPath = SessionManager.getInstance().getHdcStdPath(); 71 } else { 72 pluginPath = SessionManager.getInstance().getHdcPath(); 73 } 74 headCommand.add(pluginPath); 75 headCommand.add("-t"); 76 headCommand.add(deviceId); 77 headCommand.add("shell"); 78 return headCommand; 79 } 80 81 /** 82 * Get hdc string result 83 * 84 * @param hdcCmd hdc command 85 * @return String 86 */ getHdcStringResult(ArrayList<String> hdcCmd)87 public String getHdcStringResult(ArrayList<String> hdcCmd) { 88 if (ProfilerLogManager.isInfoEnabled()) { 89 LOGGER.info("getHdcStringResult"); 90 } 91 StringBuilder cmdStrResult = new StringBuilder(); 92 try { 93 Process process = new ProcessBuilder(hdcCmd).start(); 94 InputStream inputStream = null; 95 InputStream errorStream = null; 96 BufferedReader brInputStream = null; 97 BufferedReader brErrorStream = null; 98 try { 99 inputStream = process.getInputStream(); 100 errorStream = process.getErrorStream(); 101 brInputStream = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("gbk"))); 102 brErrorStream = new BufferedReader(new InputStreamReader(errorStream)); 103 String line; 104 while ((line = brInputStream.readLine()) != null || (line = brErrorStream.readLine()) != null) { 105 cmdStrResult.append(line).append(System.getProperty("line.separator")); 106 } 107 } catch (IOException ioException) { 108 if (ProfilerLogManager.isErrorEnabled()) { 109 LOGGER.error("ioException error: " + ioException); 110 } 111 } finally { 112 close(inputStream); 113 close(errorStream); 114 close(brErrorStream); 115 close(brInputStream); 116 if (Objects.nonNull(process)) { 117 process.destroy(); 118 } 119 } 120 } catch (IOException ioException) { 121 if (ProfilerLogManager.isErrorEnabled()) { 122 LOGGER.error("ioException error: " + ioException); 123 } 124 } 125 return cmdStrResult.toString(); 126 } 127 128 /** 129 * Get hdc string result 130 * 131 * @param hdcCmd hdc command 132 * @param timeout timeout 133 * @return String 134 */ getHdcStringResult(ArrayList<String> hdcCmd, long timeout)135 public String getHdcStringResult(ArrayList<String> hdcCmd, long timeout) { 136 if (ProfilerLogManager.isInfoEnabled()) { 137 LOGGER.info("getHdcStringResult"); 138 } 139 long time = timeout; 140 if (timeout <= 0) { 141 time = 5; 142 } 143 ExecResult execResult = new CmdExecutors().executeCommand(hdcCmd, time); 144 StringBuilder cmdStrResult = new StringBuilder(); 145 int exitCode = execResult.getExitCode(); 146 if (exitCode == 0) { 147 ArrayList<String> executeOut = execResult.getExecuteOut(); 148 executeOut.forEach(line -> { 149 cmdStrResult.append(line).append(System.getProperty("line.separator")); 150 }); 151 } 152 if (exitCode == -2) { 153 return TIME_OUT; 154 } 155 return cmdStrResult.toString(); 156 } 157 158 /** 159 * Get hdc string result 160 * 161 * @param hdcCmd hdc command 162 * @return String 163 */ getHdcResult(ArrayList<String> hdcCmd)164 public String getHdcResult(ArrayList<String> hdcCmd) { 165 if (ProfilerLogManager.isInfoEnabled()) { 166 LOGGER.info("getHdcStringResult"); 167 } 168 StringBuilder cmdStrResult = new StringBuilder(); 169 try { 170 Process process = new ProcessBuilder(hdcCmd).start(); 171 InputStream inputStream = null; 172 InputStream errorStream = null; 173 BufferedReader brInputStream = null; 174 BufferedReader brErrorStream = null; 175 try { 176 inputStream = process.getInputStream(); 177 errorStream = process.getErrorStream(); 178 brInputStream = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("gbk"))); 179 brErrorStream = new BufferedReader(new InputStreamReader(errorStream)); 180 String line; 181 while ((line = brInputStream.readLine()) != null || (line = brErrorStream.readLine()) != null) { 182 cmdStrResult.append(line); 183 } 184 } catch (IOException ioException) { 185 if (ProfilerLogManager.isErrorEnabled()) { 186 LOGGER.error("ioException error: " + ioException); 187 } 188 } finally { 189 close(inputStream); 190 close(errorStream); 191 close(brErrorStream); 192 close(brInputStream); 193 if (Objects.nonNull(process)) { 194 process.destroy(); 195 } 196 } 197 } catch (IOException ioException) { 198 if (ProfilerLogManager.isErrorEnabled()) { 199 LOGGER.error("ioException error: " + ioException); 200 } 201 } 202 return cmdStrResult.toString(); 203 } 204 205 /** 206 * execCmdBy 207 * 208 * @param hdcCmd hdc command 209 * @return String 210 */ execCmdBy(ArrayList<String> hdcCmd)211 public String execCmdBy(ArrayList<String> hdcCmd) { 212 if (ProfilerLogManager.isInfoEnabled()) { 213 LOGGER.info("execCmdBy"); 214 } 215 Process process; 216 StringBuilder cmdStrResult = new StringBuilder(); 217 try { 218 process = new ProcessBuilder(hdcCmd).start(); 219 InputStream inputStream = null; 220 InputStream errorStream = null; 221 BufferedReader brInputStream = null; 222 BufferedReader brErrorStream = null; 223 try { 224 inputStream = process.getInputStream(); 225 errorStream = process.getErrorStream(); 226 brInputStream = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("gbk"))); 227 brErrorStream = new BufferedReader(new InputStreamReader(errorStream)); 228 String line; 229 while ((line = brInputStream.readLine()) != null || (line = brErrorStream.readLine()) != null) { 230 cmdStrResult.append(line); 231 if ("StartDaemonSuccess".equals(line)) { 232 break; 233 } 234 } 235 } catch (IOException ioException) { 236 if (ProfilerLogManager.isErrorEnabled()) { 237 LOGGER.error("ioException error: " + ioException); 238 } 239 } finally { 240 close(inputStream); 241 close(errorStream); 242 close(brErrorStream); 243 close(brInputStream); 244 if (Objects.nonNull(process)) { 245 process.destroy(); 246 } 247 } 248 } catch (IOException ioException) { 249 if (ProfilerLogManager.isErrorEnabled()) { 250 LOGGER.error("ioException error: " + ioException); 251 } 252 } 253 return cmdStrResult.toString(); 254 } 255 256 /** 257 * execCmdBy 258 * 259 * @param hdcCmd hdc command 260 * @param timeout timeout 261 * @return String 262 */ execCmdBy(ArrayList<String> hdcCmd, long timeout)263 public String execCmdBy(ArrayList<String> hdcCmd, long timeout) { 264 if (ProfilerLogManager.isInfoEnabled()) { 265 LOGGER.info("execCmdBy"); 266 } 267 if (hdcCmd.isEmpty()) { 268 return ""; 269 } 270 StringBuilder cmdStrResult = new StringBuilder(); 271 ExecResult execResult = new CmdExecutors().executeCommand(hdcCmd, timeout); 272 int exitCode = execResult.getExitCode(); 273 if (exitCode == 0) { 274 ArrayList<String> executeOut = execResult.getExecuteOut(); 275 for (String excuteResult : executeOut) { 276 if (StringUtils.isNotBlank(excuteResult)) { 277 cmdStrResult.append(excuteResult); 278 } 279 } 280 } 281 return cmdStrResult.toString(); 282 } 283 284 /** 285 * execCmd 286 * 287 * @param hdcCmd hdcCmd 288 * @return List <String> 289 */ execCmd(ArrayList<String> hdcCmd)290 public List<String> execCmd(ArrayList<String> hdcCmd) { 291 List<String> result = new ArrayList<>(); 292 if (ProfilerLogManager.isInfoEnabled()) { 293 LOGGER.info("execCmdBy"); 294 } 295 Process process; 296 String line; 297 try { 298 process = new ProcessBuilder(hdcCmd).start(); 299 InputStream inputStream = null; 300 InputStream errorStream = null; 301 BufferedReader brInputStream = null; 302 BufferedReader brErrorStream = null; 303 try { 304 inputStream = process.getInputStream(); 305 errorStream = process.getErrorStream(); 306 brInputStream = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("gbk"))); 307 brErrorStream = new BufferedReader(new InputStreamReader(errorStream)); 308 while ((line = brInputStream.readLine()) != null || (line = brErrorStream.readLine()) != null) { 309 if (ProfilerLogManager.isInfoEnabled()) { 310 LOGGER.info("cmd result line {}", line); 311 } 312 result.add(line); 313 if ("StartDaemonSuccess".equals(line)) { 314 break; 315 } 316 } 317 } catch (IOException ioException) { 318 if (ProfilerLogManager.isErrorEnabled()) { 319 LOGGER.error("ioException error: " + ioException); 320 } 321 } finally { 322 close(inputStream); 323 close(errorStream); 324 close(brErrorStream); 325 close(brInputStream); 326 if (Objects.nonNull(process)) { 327 process.destroy(); 328 } 329 } 330 } catch (IOException ioException) { 331 if (ProfilerLogManager.isErrorEnabled()) { 332 LOGGER.error("ioException error: " + ioException); 333 } 334 } 335 return result; 336 } 337 338 /** 339 * getCliResult 340 * 341 * @param hdcStr hdc String 342 * @return ArrayList <ArrayList < String>> 343 */ getCliResult(ArrayList<String> hdcStr)344 public ArrayList<ArrayList<String>> getCliResult(ArrayList<String> hdcStr) { 345 if (ProfilerLogManager.isInfoEnabled()) { 346 LOGGER.info("getCliResult"); 347 } 348 ArrayList<ArrayList<String>> result = new ArrayList<>(); 349 ExecResult execResult = new CmdExecutors().executeCommand(hdcStr, 8); 350 int exitCode = execResult.getExitCode(); 351 if (exitCode == 0) { 352 ArrayList<String> executeOut = execResult.getExecuteOut(); 353 for (String excuteResult : executeOut) { 354 if (StringUtils.isNotBlank(excuteResult)) { 355 ArrayList<String> list = new ArrayList<>(); 356 String[] newLine = excuteResult.split(":"); 357 for (String str : newLine) { 358 String value = str.trim(); 359 if (!"".equals(value)) { 360 list.add(value); 361 } 362 } 363 result.add(list); 364 } 365 } 366 } 367 if (exitCode == -2) { 368 ArrayList<String> list = new ArrayList<>(); 369 list.add(TIME_OUT); 370 result.add(list); 371 } 372 return result; 373 } 374 375 /** 376 * Get result list 377 * 378 * @param hdcStr hdc String 379 * @return Map<String, String> 380 */ getCmdResultMap(ArrayList<String> hdcStr)381 public Map<String, String> getCmdResultMap(ArrayList<String> hdcStr) { 382 if (ProfilerLogManager.isInfoEnabled()) { 383 LOGGER.info("getCmdResultMap"); 384 } 385 Map<String, String> resultMap = new HashMap<>(); 386 ExecResult execResult = new CmdExecutors().executeCommandByLine(hdcStr, 5); 387 int exitCode = execResult.getExitCode(); 388 if (exitCode == 0) { 389 ArrayList<String> executeOut = execResult.getExecuteOut(); 390 for (String excuteResult : executeOut) { 391 if (StringUtils.isNotBlank(excuteResult)) { 392 String[] strings = excuteResult.trim().split(" "); 393 resultMap.put(strings[1], strings[0]); 394 } 395 } 396 } 397 return resultMap; 398 } 399 400 /** 401 * Get result list 402 * 403 * @param hdcStr hdc String 404 * @return ArrayList <ArrayList < String>> 405 */ getListResult(ArrayList<String> hdcStr)406 public ArrayList<ArrayList<String>> getListResult(ArrayList<String> hdcStr) { 407 if (ProfilerLogManager.isInfoEnabled()) { 408 LOGGER.info("getListResult"); 409 } 410 ArrayList<ArrayList<String>> devices = new ArrayList<>(); 411 ExecResult execResult = new CmdExecutors().executeCommand(hdcStr, 8); 412 int exitCode = execResult.getExitCode(); 413 if (exitCode == 0) { 414 ArrayList<String> executeOut = execResult.getExecuteOut(); 415 for (String excuteResult : executeOut) { 416 if (StringUtils.isNotBlank(excuteResult)) { 417 ArrayList<String> list = new ArrayList<>(); 418 String[] newLine = excuteResult.split(" "); 419 for (String str : newLine) { 420 String value = str.trim(); 421 if (!"".equals(value)) { 422 list.add(value); 423 } 424 } 425 devices.add(list); 426 } 427 } 428 } 429 if (exitCode == -2) { 430 ArrayList<String> list = new ArrayList<>(); 431 list.add(TIME_OUT); 432 devices.add(list); 433 } 434 return devices; 435 } 436 437 /** 438 * Get result list 439 * 440 * @param hdcStr hdc String 441 * @return ArrayList <ArrayList < String>> 442 */ getListHdcStdResult(ArrayList<String> hdcStr)443 public ArrayList<ArrayList<String>> getListHdcStdResult(ArrayList<String> hdcStr) { 444 if (ProfilerLogManager.isInfoEnabled()) { 445 LOGGER.info("getListHdcStdResult"); 446 } 447 String temp; 448 ArrayList<ArrayList<String>> devices = new ArrayList<>(); 449 ExecResult execResult = new CmdExecutors().executeCommand(hdcStr, 8); 450 int exitCode = execResult.getExitCode(); 451 if (exitCode == 0) { 452 ArrayList<String> executeOut = execResult.getExecuteOut(); 453 for (String excuteResult : executeOut) { 454 if (StringUtils.isNotBlank(excuteResult)) { 455 ArrayList<String> list = new ArrayList<>(); 456 String[] newLine = excuteResult.split("\t"); 457 for (String str : newLine) { 458 if (StringUtils.isNotBlank(str)) { 459 String value = str.trim(); 460 list.add(value); 461 } 462 } 463 devices.add(list); 464 } 465 } 466 } 467 if (exitCode == -2) { 468 ArrayList<String> list = new ArrayList<>(); 469 list.add(TIME_OUT); 470 devices.add(list); 471 } 472 return devices; 473 } 474 475 /** 476 * conversionCommand 477 * 478 * @param list list 479 * @param args args 480 * @return ArrayList 481 */ conversionCommand(ArrayList<String> list, String... args)482 public static ArrayList<String> conversionCommand(ArrayList<String> list, String... args) { 483 if (ProfilerLogManager.isInfoEnabled()) { 484 LOGGER.info("conversionCommand"); 485 } 486 int index = 0; 487 ArrayList<String> cmdlist = new ArrayList<>(); 488 for (String string : list) { 489 if (StringUtils.equals(string, "%s")) { 490 cmdlist.add(args[index]); 491 index = index + 1; 492 continue; 493 } 494 495 if (string.contains("%s") && !StringUtils.equals(string, "%s")) { 496 if (args.length <= index) { 497 cmdlist.add(string); 498 continue; 499 } else { 500 String replace = string.replace("%s", args[index]); 501 cmdlist.add(replace); 502 } 503 index = index + 1; 504 continue; 505 } 506 cmdlist.add(string); 507 } 508 if (ProfilerLogManager.isInfoEnabled()) { 509 LOGGER.info("conversionCommand cmd is {}", cmdlist); 510 } 511 return cmdlist; 512 } 513 close(Closeable closeable)514 private void close(Closeable closeable) { 515 if (closeable != null) { 516 try { 517 if (ProfilerLogManager.isInfoEnabled()) { 518 LOGGER.info("close"); 519 } 520 closeable.close(); 521 } catch (IOException exception) { 522 if (ProfilerLogManager.isErrorEnabled()) { 523 LOGGER.error("IOException ", exception); 524 } 525 } 526 } 527 } 528 } 529