1 /*
2 * Copyright (c) 2025 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 <cinttypes>
17 #include "usbserialcommonfunction_fuzzer.h"
18 #include "UsbSubscriberTest.h"
19 #include "hdf_log.h"
20
21 using namespace OHOS::HDI::Usb::UsbSerialDdk::V1_0;
22
23 namespace OHOS {
24 namespace USBSerial {
25
26 constexpr uint32_t SHIFT_32 = 32;
27
ToDdkDeviceId(const uint8_t busNum,const uint8_t devAddr)28 static uint64_t ToDdkDeviceId(const uint8_t busNum, const uint8_t devAddr)
29 {
30 return (static_cast<uint64_t>(busNum) << SHIFT_32) + devAddr;
31 }
32
UsbSerialFuzzTestHostModeInit(const sptr<IUsbSerialDdk> & usbSerialInterface,UsbSerialDeviceHandle * device)33 int32_t UsbSerialFuzzTestHostModeInit(const sptr<IUsbSerialDdk> &usbSerialInterface, UsbSerialDeviceHandle *device)
34 {
35 sptr<OHOS::HDI::Usb::V1_0::IUsbInterface> usbInterface = OHOS::HDI::Usb::V1_0::IUsbInterface::Get();
36 sleep(SLEEP_TIME);
37
38 sptr<OHOS::USB::UsbSubscriberTest> subscriber = new OHOS::USB::UsbSubscriberTest();
39 int32_t ret = usbInterface->BindUsbdSubscriber(subscriber);
40 if (ret != HDF_SUCCESS) {
41 HDF_LOGE("%{public}s: bind usbd subscriber failed", __func__);
42 return ret;
43 }
44
45 uint8_t busNum = subscriber->busNum_;
46 uint8_t devAddr = subscriber->devAddr_;
47 uint64_t interfaceIndex = 0x00;
48
49 HDF_LOGI("%{public}s: busNum:%{public}d, devAddr:%{public}d, deviceID:%{public}016" PRIX64, __func__,
50 subscriber->busNum_, subscriber->devAddr_, ToDdkDeviceId(busNum, devAddr));
51 ret = usbSerialInterface->Open(ToDdkDeviceId(busNum, devAddr), interfaceIndex, *device);
52 if (ret != HDF_SUCCESS) {
53 HDF_LOGE("%{public}s: open device failed", __func__);
54 return ret;
55 }
56 return ret;
57 }
58 } // namespace V1_0
59 } // namespace UsbSerialDdk
60