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 { FormatCommand } from '../hdcclient/FormatCommand';
17import { warn } from '../../log/Log';
18
19export class Utils {
20  private static localId = 1;
21
22  public static getSessionId(): number {
23    return Math.round(Math.random() * 100000000);
24  }
25
26  public static getLocalId(): number {
27    if (this.localId === 4294967295) {
28      this.localId = 1;
29    }
30    return this.localId++;
31  }
32
33  public static formatCommand(cmd: string): FormatCommand {
34    let command = cmd;
35    if (cmd.startsWith('hdc_std')) {
36      command = command.substring('hdc_std '.length);
37    } else if (cmd.startsWith('hdc')) {
38      command = command.substring('hdc '.length);
39    }
40    let formatCommand = FormatCommand.string2FormatCommand(command);
41    // formatCommand Success
42    if (formatCommand.cmdFlag <= -1) {
43      warn(`command : ${command} is not Support`);
44    }
45    return formatCommand;
46  }
47
48  public static numToHexString(num: number): string {
49    if (num === undefined || num === null || isNaN(num)) {
50      return '0x0';
51    }
52    if (num < 0) {
53      return `0x${(num >>> 0).toString(16)}`;
54    } else {
55      return `0x${num.toString(16)}`;
56    }
57  }
58}
59