• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #ifndef USBMGR_USB_SERVICE_H
17 #define USBMGR_USB_SERVICE_H
18 
19 #include <map>
20 #include <vector>
21 
22 #include "delayed_sp_singleton.h"
23 #include "iremote_object.h"
24 #include "iusb_srv.h"
25 #include "system_ability.h"
26 #include "system_ability_status_change_stub.h"
27 #include "timer.h"
28 #include "usb_device_manager.h"
29 #include "usb_host_manager.h"
30 #include "usb_port_manager.h"
31 #include "usb_right_manager.h"
32 #include "usb_server_event_handler.h"
33 #include "usb_server_stub.h"
34 #include "usb_service_subscriber.h"
35 #include "usbd_type.h"
36 #include "v1_0/iusb_interface.h"
37 #include "v1_0/iusbd_bulk_callback.h"
38 #include "v1_0/iusbd_subscriber.h"
39 #include "v1_0/usb_types.h"
40 
41 namespace OHOS {
42 namespace USB {
43 const std::string USB_HOST = "usb_host";
44 const std::string USB_DEVICE = "usb_device";
45 const std::string USB_PORT = "usb_port";
46 const std::string USB_HELP = "-h";
47 class UsbService : public SystemAbility, public UsbServerStub {
48     DECLARE_SYSTEM_ABILITY(UsbService)
49     DECLARE_DELAYED_SP_SINGLETON(UsbService);
50 
51 public:
52     enum UnLoadSaType { UNLOAD_SA_DELAY, UNLOAD_SA_IMMEDIATELY };
53     void OnStart() override;
54     void OnStop() override;
55     int Dump(int fd, const std::vector<std::u16string> &args) override;
56 
IsServiceReady()57     bool IsServiceReady() const
58     {
59         return ready_;
60     }
61 
GetHandler()62     std::shared_ptr<UsbServerEventHandler> GetHandler() const
63     {
64         return handler_;
65     }
66 
67     int32_t SetUsbd(const sptr<HDI::Usb::V1_0::IUsbInterface> &usbd);
68     int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) override;
69     bool HasRight(std::string deviceName) override;
70     int32_t RequestRight(std::string deviceName) override;
71     int32_t RemoveRight(std::string deviceName) override;
72     int32_t GetDevices(std::vector<UsbDevice> &deviceList) override;
73     int32_t GetCurrentFunctions(int32_t &funcs) override;
74     int32_t SetCurrentFunctions(int32_t funcs) override;
75     int32_t UsbFunctionsFromString(std::string_view funcs) override;
76     std::string UsbFunctionsToString(int32_t funcs) override;
77     int32_t GetPorts(std::vector<UsbPort> &ports) override;
78     int32_t GetSupportedModes(int32_t portId, int32_t &result) override;
79     int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) override;
80 
81     int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t force) override;
82     int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override;
83     int32_t BulkTransferRead(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe,
84         std::vector<uint8_t> &bufferData, int32_t timeOut) override;
85     int32_t BulkTransferWrite(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe,
86         const std::vector<uint8_t> &bufferData, int32_t timeOut) override;
87     int32_t ControlTransfer(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbCtrlTransfer &ctrl,
88         std::vector<uint8_t> &bufferData) override;
89     int32_t SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) override;
90     int32_t GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex) override;
91     int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) override;
92     int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vector<uint8_t> &bufferData) override;
93     int32_t GetFileDescriptor(uint8_t busNum, uint8_t devAddr, int32_t &fd) override;
94     int32_t RequestQueue(const HDI::Usb::V1_0::UsbDev &dev, const HDI::Usb::V1_0::UsbPipe &pipe,
95         const std::vector<uint8_t> &clientData, const std::vector<uint8_t> &bufferData) override;
96     int32_t RequestWait(const HDI::Usb::V1_0::UsbDev &dev, int32_t timeOut, std::vector<uint8_t> &clientData,
97         std::vector<uint8_t> &bufferData) override;
98     int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId) override;
99     int32_t Close(uint8_t busNum, uint8_t devAddr) override;
100     bool AddDevice(uint8_t busNum, uint8_t devAddr);
101     bool DelDevice(uint8_t busNum, uint8_t devAddr);
102     void UpdateUsbPort(int32_t portId, int32_t powerRole, int32_t dataRole, int32_t mode);
103     void UpdateDeviceState(int32_t status);
104     int32_t GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice &dev);
105     int32_t GetDeviceInfoDescriptor(
106         const HDI::Usb::V1_0::UsbDev &uDev, std::vector<uint8_t> &descriptor, UsbDevice &dev);
107     int32_t GetConfigDescriptor(UsbDevice &dev, std::vector<uint8_t> &descriptor);
108 
109     int32_t RegBulkCallback(const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe,
110         const sptr<IRemoteObject> &cb) override;
111     int32_t UnRegBulkCallback(const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe) override;
112     int32_t BulkRead(
113         const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe, sptr<Ashmem> &ashmem) override;
114     int32_t BulkWrite(
115         const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe, sptr<Ashmem> &ashmem) override;
116     int32_t BulkCancel(const HDI::Usb::V1_0::UsbDev &devInfo, const HDI::Usb::V1_0::UsbPipe &pipe) override;
117     int32_t AddRight(const std::string &bundleName, const std::string &deviceName) override;
118     void UnLoadSelf(UnLoadSaType type);
119 
120 private:
121     class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub {
122     public:
123         explicit SystemAbilityStatusChangeListener(sptr<UsbServiceSubscriber> usbdSubscriber);
124         ~SystemAbilityStatusChangeListener() = default;
125         void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
126         void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
127 
128     private:
129         sptr<UsbServiceSubscriber> usbdSubscriber_;
130     };
131 
132     class UsbdDeathRecipient : public IRemoteObject::DeathRecipient {
133     public:
134         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
135     };
136 
137 private:
138     bool Init();
139     bool InitUsbd();
140     bool IsCommonEventServiceAbilityExist();
141     bool GetBundleName(std::string &bundleName);
142     std::string GetDeviceVidPidSerialNumber(std::string deviceName);
143     int32_t FillDevStrings(UsbDevice &dev);
144     std::string GetDevStringValFromIdx(uint8_t busNum, uint8_t devAddr, uint8_t idx);
145     int32_t InitUsbRight();
146     void DumpHelp(int32_t fd);
147     bool ready_ = false;
148     int32_t commEventRetryTimes_ = 0;
149     std::mutex mutex_;
150     std::shared_ptr<UsbHostManager> usbHostManger_;
151     std::shared_ptr<UsbRightManager> usbRightManager_;
152     std::shared_ptr<UsbPortManager> usbPortManager_;
153     std::shared_ptr<UsbDeviceManager> usbDeviceManager_;
154     std::shared_ptr<AppExecFwk::EventRunner> eventRunner_;
155     std::shared_ptr<UsbServerEventHandler> handler_;
156     sptr<UsbServiceSubscriber> usbdSubscriber_;
157     sptr<HDI::Usb::V1_0::IUsbdBulkCallback> hdiCb_ = nullptr;
158     sptr<HDI::Usb::V1_0::IUsbInterface> usbd_ = nullptr;
159     std::map<std::string, std::string> deviceVidPidMap_;
160     Utils::Timer unloadSelfTimer_ {"unLoadTimer"};
161     uint32_t unloadSelfTimerId_ {UINT32_MAX};
162     sptr<IRemoteObject::DeathRecipient> recipient_;
163 };
164 } // namespace USB
165 } // namespace OHOS
166 #endif // USBMGR_USB_SERVICE_H
167