• 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 
16 #include "usb_impl_mock.h"
17 #include "if_system_ability_manager.h"
18 #include "iproxy_broker.h"
19 #include "iremote_object.h"
20 #include "iservice_registry.h"
21 #include "system_ability.h"
22 #include "system_ability_definition.h"
23 #include "system_ability_status_change_stub.h"
24 
25 namespace OHOS {
26 namespace USB {
27 using namespace OHOS;
28 using namespace OHOS::HDI;
29 
30 std::vector<uint8_t> g_descBuf {
31     0x12, 0x01, 0x20, 0x03, 0x00, 0x00, 0x00, 0x09, 0x07, 0x22, 0x18, 0x00, 0x23, 0x02, 0x01, 0x02, 0x03, 0x01, 0x09,
32     0x02, 0x5D, 0x00, 0x02, 0x01, 0x04, 0xC0, 0x3E, 0x08, 0x0B, 0x00, 0x02, 0x02, 0x02, 0x01, 0x07, 0x09, 0x04, 0x00,
33     0x00, 0x01, 0x02, 0x02, 0x01, 0x05, 0x05, 0x24, 0x00, 0x10, 0x01, 0x05, 0x24, 0x01, 0x00, 0x01, 0x04, 0x24, 0x02,
34     0x02, 0x05, 0x24, 0x06, 0x00, 0x01, 0x07, 0x05, 0x81, 0x03, 0x0A, 0x00, 0x09, 0x06, 0x30, 0x00, 0x00, 0x00, 0x00,
35     0x09, 0x04, 0x01, 0x00, 0x02, 0x0A, 0x00, 0x02, 0x06, 0x07, 0x05, 0x82, 0x02, 0x00, 0x04, 0x00, 0x06, 0x30, 0x00,
36     0x00, 0x00, 0x00, 0x07, 0x05, 0x01, 0x02, 0x00, 0x04, 0x00, 0x06, 0x30, 0x00, 0x00, 0x00, 0x00
37 };
38 
MockUsbImpl()39 MockUsbImpl::MockUsbImpl() {}
40 
~MockUsbImpl()41 MockUsbImpl::~MockUsbImpl() {}
42 
FindDeviceInfo(std::vector<UsbDevice> & devi)43 UsbDevice MockUsbImpl::FindDeviceInfo(std::vector<UsbDevice> &devi)
44 {
45     size_t devLen = devi.size();
46     UsbDevice info;
47     for (size_t i = 0; i < devLen; i++) {
48         info = devi.at(i);
49         if ((info.GetDevAddr() == DEV_ADDR_OK) && (info.GetBusNum() == BUS_NUM_OK)) {
50             break;
51         }
52     }
53     return info;
54 }
55 
GetRawDescriptor(const UsbDev & dev,std::vector<uint8_t> & decriptor)56 int32_t MockUsbImpl::GetRawDescriptor(const UsbDev &dev, std::vector<uint8_t> &decriptor)
57 {
58     if ((BUS_NUM_OK != dev.busNum) || (DEV_ADDR_OK != dev.devAddr)) {
59         return HDF_DEV_ERR_NO_DEVICE;
60     }
61     decriptor = g_descBuf;
62     return HDF_SUCCESS;
63 }
64 
GetStringDescriptor(const UsbDev & dev,uint8_t descId,std::vector<uint8_t> & decriptor)65 int32_t MockUsbImpl::GetStringDescriptor(const UsbDev &dev, uint8_t descId, std::vector<uint8_t> &decriptor)
66 {
67     (void)descId;
68     if ((BUS_NUM_OK != dev.busNum) || (DEV_ADDR_OK != dev.devAddr)) {
69         return HDF_DEV_ERR_NO_DEVICE;
70     }
71     decriptor = g_descBuf;
72     return HDF_SUCCESS;
73 }
74 
QueryPort(int32_t & portId,int32_t & powerRole,int32_t & dataRole,int32_t & mode)75 int32_t MockUsbImpl::QueryPort(int32_t &portId, int32_t &powerRole, int32_t &dataRole, int32_t &mode)
76 {
77     USB_HILOGI(MODULE_USB_SERVICE, "pob usb 1 MockUsbImpl::QueryPort enter");
78     portId = DEFAULT_PORT_ID;
79     powerRole = UsbSrvSupport::POWER_ROLE_SINK;
80     dataRole = UsbSrvSupport::DATA_ROLE_DEVICE;
81     mode = UsbSrvSupport::PORT_MODE_DEVICE;
82     return HDF_SUCCESS;
83 }
84 
BindUsbdSubscriber(const sptr<IUsbdSubscriber> & subscriber)85 int32_t MockUsbImpl::BindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber)
86 {
87     subscriber_ = subscriber;
88     return HDF_SUCCESS;
89 }
90 
UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> & subscriber)91 int32_t MockUsbImpl::UnbindUsbdSubscriber(const sptr<IUsbdSubscriber> &subscriber)
92 {
93     (void)subscriber;
94     subscriber_ = nullptr;
95     return HDF_SUCCESS;
96 }
97 
SetPortRole(int32_t portId,int32_t powerRole,int32_t dataRole)98 int32_t MockUsbImpl::SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole)
99 {
100     int32_t mode = UsbSrvSupport::PORT_MODE_DEVICE;
101 
102     if (portId != DEFAULT_PORT_ID) {
103         return HDF_FAILURE;
104     }
105     if (powerRole <= UsbSrvSupport::POWER_ROLE_NONE || powerRole > UsbSrvSupport::POWER_ROLE_SINK) {
106         return HDF_FAILURE;
107     }
108     if (dataRole <= UsbSrvSupport::DTA_ROLE_NONE || dataRole > UsbSrvSupport::DATA_ROLE_DEVICE) {
109         return HDF_FAILURE;
110     }
111 
112     if (powerRole == UsbSrvSupport::POWER_ROLE_SOURCE && dataRole == UsbSrvSupport::DATA_ROLE_HOST) {
113         mode = UsbSrvSupport::PORT_MODE_HOST;
114     }
115 
116     if (powerRole == UsbSrvSupport::POWER_ROLE_SINK && dataRole == UsbSrvSupport::DATA_ROLE_DEVICE) {
117         mode = UsbSrvSupport::PORT_MODE_DEVICE;
118     }
119 
120     portInfo_.portId = portId;
121     portInfo_.powerRole = powerRole;
122     portInfo_.dataRole = dataRole;
123     portInfo_.mode = mode;
124     auto ret = subscriber_->PortChangedEvent(portInfo_);
125 
126     return ret ? HDF_SUCCESS : HDF_FAILURE;
127 }
128 
SubscriberDeviceEvent(const USBDeviceInfo & info)129 int32_t MockUsbImpl::SubscriberDeviceEvent(const USBDeviceInfo &info)
130 {
131     auto ret = subscriber_->DeviceEvent(info);
132     return ret;
133 }
134 } // namespace USB
135 } // namespace OHOS
136