• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 usbManagerService from '@ohos.usbManager';
17import systemParameterEnhance from '@ohos.systemParameterEnhance';
18
19const TAG: string = 'usbfunctionswitchwindow_UsbServiceSwitch';
20const USBFUNCTION_NONE: number = 0;
21const USBFUNCTION_HDC: number = 4;
22const USBFUNCTION_MTP: number = 8;
23const USBFUNCTION_PTP: number = 16;
24const USBFUNCTION_STORAGE: number = 512;
25const USBFUNCTION_DEVMODE_AUTH: number = 4096;
26const supportedCompositeFuncs: Array<number> = [
27  USBFUNCTION_HDC
28];
29
30class UsbServiceSwitch {
31  private curFunc: number = USBFUNCTION_NONE;
32  private tarFunc: number = USBFUNCTION_NONE;
33
34  getCurrentFunctions(): number {
35    this.curFunc = usbManagerService.getCurrentFunctions();
36    console.log(TAG + 'current' + this.curFunc);
37    return this.curFunc;
38  }
39
40  async serviceChoose(chooseId: number): Promise<void> {
41    this.curFunc = usbManagerService.getCurrentFunctions();
42    console.log(TAG + 'choose ' + chooseId + 'current' + this.curFunc);
43    if (typeof (this.curFunc) === undefined) {
44      console.error(TAG + 'undefined, error');
45      return;
46    }
47    this.tarFunc = ~(USBFUNCTION_MTP | USBFUNCTION_PTP);
48    /* signed number to unsigned number */
49    this.tarFunc = this.tarFunc >>> 0;
50    this.tarFunc = this.tarFunc & this.curFunc;
51
52    if (chooseId === -1) {
53      console.log(TAG + 'choose: charge only');
54      return;
55    } else if (chooseId === USBFUNCTION_NONE) {
56      this.tarFunc = (this.tarFunc | USBFUNCTION_NONE) & (~USBFUNCTION_STORAGE);
57      if (this.tarFunc === USBFUNCTION_NONE) {
58        this.tarFunc = USBFUNCTION_STORAGE;
59      }
60      console.log(TAG + 'choose: xfer file(NONE)');
61    } else if (chooseId === USBFUNCTION_MTP) {
62      this.tarFunc = supportedCompositeFuncs.includes(this.tarFunc) ? (this.tarFunc | USBFUNCTION_MTP) : USBFUNCTION_MTP;
63      console.log(TAG + 'choose: xfer file(MTP)');
64    } else if (chooseId === USBFUNCTION_PTP) {
65      this.tarFunc = supportedCompositeFuncs.includes(this.tarFunc) ? (this.tarFunc | USBFUNCTION_PTP) : USBFUNCTION_PTP;
66      console.log(TAG + 'choose: xfer pic(PTP)');
67    } else {
68      console.log(TAG + 'choose error');
69      return;
70    }
71
72    this.tarFunc = this.tarFunc & (~USBFUNCTION_DEVMODE_AUTH); // remove dev auth
73    console.log(TAG + 'setFunctions: current ' + JSON.stringify(this.curFunc) + 'target: ' + JSON.stringify(this.tarFunc));
74    if (this.tarFunc !== this.curFunc) {
75      usbManagerService.setCurrentFunctions(this.tarFunc).then((data) => {
76        console.log(TAG + 'setCurrentFunctions success: ' + JSON.stringify(data));
77      }).catch((err) => {
78        console.error(TAG + 'setCurrentFunctions failed: ' + JSON.stringify(err));
79      });
80    }
81    return;
82  }
83
84  setUSBChargeDialog(inhibited: string): void {
85    try {
86      systemParameterEnhance.set('persist.usb.setting.gadget_conn_prompt', inhibited).then((value: void) => {
87        console.log(TAG, 'setUSBChargeDialog set success');
88      }).catch((err) => {
89        console.log(TAG, 'setUSBChargeDialog set error: ', err.code);
90      });
91    } catch (error) {
92      console.log(TAG, 'setUSBChargeDialog set unexpected error: ', error.code);
93    }
94  }
95
96  setPortRole(portId: number, powerRole: number, dataRole: number): Promise<void> {
97    console.log(TAG + 'setPortRoles portId ' + portId + ' powerRole ' + powerRole + ' dataRole ' + dataRole);
98    usbManagerService.setPortRoles(portId, powerRole, dataRole).then(() => {
99      console.log(TAG + 'setPortRoles success: ');
100      return;
101    }).catch((err) => {
102      console.error(TAG + 'setPortRoles failed: ' + JSON.stringify(err));
103      return;
104    });
105  }
106  getPorts(): Array<usbManagerService.USBPort> {
107    return usbManagerService.getPorts();
108  }
109}
110export let usbServiceSwitch = new UsbServiceSwitch();
111