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  
16  #ifndef OHOS_HDI_USB_V1_1_USBD_PORT_H
17  #define OHOS_HDI_USB_V1_1_USBD_PORT_H
18  
19  #include <fcntl.h>
20  #include <sys/types.h>
21  #include <unistd.h>
22  
23  #include "usbd.h"
24  #include "v1_0/iusbd_subscriber.h"
25  #include "v1_0/usb_types.h"
26  
27  #define DEFAULT_PORT_ID 1
28  
29  #define PORT_MODE_HOST_STR   "host"
30  #define PORT_MODE_DEVICE_STR "device"
31  
32  #define POWER_ROLE_NONE   0
33  #define POWER_ROLE_SOURCE 1
34  #define POWER_ROLE_SINK   2
35  #define POWER_ROLE_MAX    3
36  
37  #define DATA_ROLE_NONE   0
38  #define DATA_ROLE_HOST   1
39  #define DATA_ROLE_DEVICE 2
40  #define DATA_ROLE_MAX    3
41  
42  #define PORT_MODE_NONE   0
43  #define PORT_MODE_DEVICE 1
44  #define PORT_MODE_HOST   2
45  
46  using OHOS::HDI::Usb::V1_1::PortInfo;
47  
48  using OHOS::HDI::Usb::V1_1::IUsbdSubscriber;
49  
50  namespace OHOS {
51  namespace HDI {
52  namespace Usb {
53  namespace V1_1 {
54  class UsbdPort {
55  public:
56      static UsbdPort &GetInstance();
57      int32_t SetPort(int32_t portId, int32_t powerRole, int32_t dataRole,
58          UsbdSubscriber *usbdSubscribers, uint32_t len);
59      int32_t QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode);
60      int32_t UpdatePort(int32_t mode, const sptr<HDI::Usb::V1_1::IUsbdSubscriber> &subscriber);
61      void setPortPath(const std::string &path);
62  
63  private:
64      UsbdPort() = default;
65      ~UsbdPort() = default;
66      UsbdPort(const UsbdPort &) = delete;
67      UsbdPort(UsbdPort &&) = delete;
68      UsbdPort &operator=(const UsbdPort &) = delete;
69      UsbdPort &operator=(UsbdPort &&) = delete;
70  
71      int32_t IfCanSwitch(int32_t portId, int32_t powerRole, int32_t dataRole);
72      int32_t OpenPortFile(int32_t flags);
73      int32_t WritePortFile(int32_t powerRole, int32_t dataRole, int32_t mode);
74      int32_t ReadPortFile(int32_t &powerRole, int32_t &dataRole, int32_t &mode);
75      int32_t SetPortInit(int32_t portId, int32_t powerRole, int32_t dataRole);
76      HDI::Usb::V1_1::PortInfo currentPortInfo_ = {DEFAULT_PORT_ID, POWER_ROLE_SINK, DATA_ROLE_DEVICE, PORT_MODE_DEVICE};
77      std::string path_;
78  };
79  } // namespace V1_1
80  } // namespace Usb
81  } // namespace HDI
82  } // namespace OHOS
83  #endif // OHOS_HDI_USB_V1_1_USBD_PORT_H
84