fix(usb):inner review issue clear

Signed-off-by: wu-chengwen <wuchengwen4@huawei.com>
This commit is contained in:
wu-chengwen 2022-03-21 16:43:59 +08:00
parent 530e690cd7
commit 4c1fd2a5d1
44 changed files with 493 additions and 502 deletions

View File

@ -1,4 +1,3 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

2
interfaces/innerkits/BUILD.gn Executable file → Normal file
View File

@ -1,4 +1,4 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -67,7 +67,7 @@ public:
virtual int32_t GetDevices(std::vector<UsbDevice> &deviceList) = 0;
virtual int32_t GetCurrentFunctions(int32_t &funcs) = 0;
virtual int32_t SetCurrentFunctions(int32_t funcs) = 0;
virtual int32_t UsbFunctionsFromString(std::string funcs) = 0;
virtual int32_t UsbFunctionsFromString(std::string_view funcs) = 0;
virtual std::string UsbFunctionsToString(int32_t funcs) = 0;
virtual int32_t GetPorts(std::vector<UsbPort> &ports) = 0;
virtual int32_t GetSupportedModes(int32_t portId, int32_t &supportedModes) = 0;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -66,6 +66,7 @@ public:
int32_t GetMaxPower() const
{
// 2 represent maxPower units
return maxPower * 2;
}
@ -88,22 +89,27 @@ public:
{
this->interfaces = interfaces;
}
std::vector<UsbInterface> &GetInterfaces()
{
return interfaces;
}
void SetId(int32_t id)
{
this->id = id;
}
void SetAttribute(int32_t attributes)
{
this->attributes = attributes;
}
void SetMaxPower(int32_t maxPower)
{
this->maxPower = maxPower;
}
std::string ToString() const
{
std::ostringstream ss;
@ -120,14 +126,17 @@ public:
}
return str;
}
void SetName(std::string Name)
void SetName(const std::string &name)
{
this->name = Name;
this->name = name;
}
void SetiConfiguration(uint8_t idx)
{
this->iConfiguration = idx;
}
uint8_t GetiConfiguration()
{
return this->iConfiguration;
@ -139,7 +148,7 @@ private:
std::vector<UsbInterface> interfaces;
int32_t maxPower = INVALID_USB_INT_VALUE;
std::string name;
uint8_t iConfiguration;
uint8_t iConfiguration = UINT8_MAX;
};
} // namespace USB
} // namespace OHOS

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -130,19 +130,19 @@ public:
{
busNum = Num;
}
void SetName(std::string Name)
void SetName(const std::string &Name)
{
mName = Name;
}
void SetManufacturerName(std::string Name)
void SetManufacturerName(const std::string &Name)
{
mManufacturerName = Name;
}
void SetProductName(std::string Name)
void SetProductName(const std::string &Name)
{
mProductName = Name;
}
void SetVersion(std::string Name)
void SetVersion(const std::string &Name)
{
mVersion = Name;
}
@ -267,14 +267,14 @@ private:
std::string mSerial;
uint8_t devAddr;
uint8_t busNum;
uint8_t descConfigCount;
uint8_t descConfigCount = UINT8_MAX;
uint8_t bMaxPacketSize0;
uint16_t bcdUSB;
uint16_t bcdDevice;
uint8_t iManufacturer;
uint8_t iProduct;
uint8_t iSerialNumber;
uint8_t bMaxPacketSize0 = UINT8_MAX;
uint16_t bcdUSB = UINT16_MAX;
uint16_t bcdDevice = UINT16_MAX;
uint8_t iManufacturer = UINT8_MAX;
uint8_t iProduct = UINT8_MAX;
uint8_t iSerialNumber = UINT8_MAX;
int mVendorId;
int mProductId;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -13,17 +13,18 @@
* limitations under the License.
*/
#ifndef USB_PIPE_H
#define USB_PIPE_H
#ifndef USB_DEVICE_PIPE_H
#define USB_DEVICE_PIPE_H
#include "iusb_srv.h"
#include "usb_param.h"
#include "usb_config.h"
namespace OHOS {
namespace USB {
class USBDevicePipe {
public:
USBDevicePipe();
~USBDevicePipe() {};
~USBDevicePipe() {}
USBDevicePipe(uint8_t busNum, uint8_t devAddr);
int32_t ClaimInterface(const UsbInterface &interface, bool force);
int32_t ReleaseInterface(const UsbInterface &interface);
@ -41,9 +42,8 @@ public:
private:
uint8_t busNum;
uint8_t devAddr;
void *usbClient;
};
} // namespace USB
} // namespace OHOS
#endif // USB_PIPE_H
#endif // USB_DEVICE_PIPE_H

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -123,7 +123,7 @@ private:
int32_t attributes = INVALID_USB_INT_VALUE;
int32_t interval = INVALID_USB_INT_VALUE;
int32_t maxPacketSize = INVALID_USB_INT_VALUE;
uint8_t interfaceId;
uint8_t interfaceId = UINT8_MAX;
};
} // namespace USB
} // namespace OHOS

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -41,74 +41,93 @@ public:
this->mAlternateSetting = mAlternateSetting;
this->mEndpoints = mEndpoints;
}
UsbInterface() {}
const std::string &GetName() const
{
return mName;
}
int32_t GetId() const
{
return mId;
}
int32_t GetClass() const
{
return mClass;
}
int32_t GetSubClass() const
{
return mSubClass;
}
int32_t GetAlternateSetting() const
{
return mAlternateSetting;
}
int32_t GetProtocol() const
{
return mProtocol;
}
int32_t GetEndpointCount() const
{
return mEndpoints.size();
}
void GetEndpoint(uint32_t index, USBEndpoint &ep) const
{
if (index < mEndpoints.size()) {
ep = mEndpoints[index];
}
}
std::vector<USBEndpoint> &GetEndpoints()
{
return mEndpoints;
}
void SetEndpoints(const std::vector<USBEndpoint> &eps)
{
mEndpoints = eps;
}
void SetId(int Val)
{
mId = Val;
}
void SetProtocol(int Val)
{
mProtocol = Val;
}
void SetClass(int Val)
{
mClass = Val;
}
void SetSubClass(int Val)
{
mSubClass = Val;
}
void SetAlternateSetting(int Val)
{
mAlternateSetting = Val;
}
void SetName(std::string Name)
void SetName(const std::string &Name)
{
mName = Name;
}
~UsbInterface() {}
std::string ToString() const
{
std::ostringstream ss;
@ -127,10 +146,12 @@ public:
}
return str;
}
void SetiInterface(uint8_t idx)
{
this->iInterface = idx;
}
uint8_t GetiInterface()
{
return this->iInterface;
@ -144,7 +165,7 @@ private:
int mAlternateSetting;
std::string mName;
std::vector<USBEndpoint> mEndpoints;
uint8_t iInterface;
uint8_t iInterface = UINT8_MAX;
};
} // namespace USB
} // namespace OHOS

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -24,7 +24,7 @@ namespace OHOS {
namespace USB {
class UsbRequest {
public:
UsbRequest();
UsbRequest() {}
~UsbRequest() {}
int32_t Initialize(const USBDevicePipe &pipe, const USBEndpoint &endpoint);
int32_t Queue();
@ -68,9 +68,6 @@ public:
USBEndpoint endpoint;
std::vector<uint8_t> clientData;
std::vector<uint8_t> reqData;
private:
uint8_t *usbClient;
};
} // namespace USB
} // namespace OHOS

View File

@ -1,6 +1,6 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -37,6 +37,7 @@ const std::string SEVVERSION = MAXVERSION + "." + SUBVERSION + "." + DLPVERSION;
class UsbSrvClient final : public DelayedRefSingleton<UsbSrvClient> {
DECLARE_DELAYED_REF_SINGLETON(UsbSrvClient)
public:
DISALLOW_COPY_AND_MOVE(UsbSrvClient);
@ -50,7 +51,7 @@ public:
int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole);
int32_t GetCurrentFunctions(int32_t &funcs);
int32_t SetCurrentFunctions(int32_t funcs);
int32_t UsbFunctionsFromString(std::string funcs);
int32_t UsbFunctionsFromString(std::string_view funcs);
std::string UsbFunctionsToString(int32_t funcs);
int32_t ClaimInterface(USBDevicePipe &pip, const UsbInterface &interface, bool force);
int32_t ReleaseInterface(USBDevicePipe &pip, const UsbInterface &interface);
@ -91,9 +92,9 @@ private:
};
int32_t Connect();
void ResetProxy(const wptr<IRemoteObject> &remote);
sptr<IUsbSrv> proxy_ = nullptr;
sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
void ResetProxy(const wptr<IRemoteObject> &remote);
std::mutex mutex_;
};
} // namespace USB

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -19,14 +19,14 @@
#include "ipc_object_stub.h"
namespace OHOS::USB {
enum {
CMD_USBD_BULK_CALLBACK_READ,
CMD_USBD_BULK_CALLBACK_WRITE,
};
class UsbdBulkCallBack : public OHOS::IPCObjectStub {
public:
explicit UsbdBulkCallBack() : OHOS::IPCObjectStub(u"UsbdBulkCallback.V1_0") {};
enum {
CMD_USBD_BULK_CALLBACK_READ,
CMD_USBD_BULK_CALLBACK_WRITE,
};
explicit UsbdBulkCallBack() : OHOS::IPCObjectStub(u"UsbdBulkCallback.V1_0") {}
virtual ~UsbdBulkCallBack() = default;
int32_t OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply,
OHOS::MessageOption &option) override;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -23,66 +23,67 @@
namespace OHOS {
namespace USB {
USBDevicePipe::USBDevicePipe() : usbClient(&UsbSrvClient::GetInstance()) {}
USBDevicePipe::USBDevicePipe() {}
USBDevicePipe::USBDevicePipe(uint8_t busNum, uint8_t devAddr)
: busNum(busNum), devAddr(devAddr), usbClient(&UsbSrvClient::GetInstance())
: busNum(busNum), devAddr(devAddr)
{
}
int32_t USBDevicePipe::Close()
{
RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT);
int32_t ret = ERR_OK;
bool bSuccess = ((UsbSrvClient *)usbClient)->Close(*this);
bool bSuccess = UsbSrvClient::GetInstance().Close(*this);
if (!bSuccess) {
ret = ERR_INVALID_VALUE;
}
return ret;
}
int32_t USBDevicePipe::ClaimInterface(const UsbInterface &interface, bool force)
{
RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT);
return ((UsbSrvClient *)usbClient)->ClaimInterface(*this, interface, force);
return UsbSrvClient::GetInstance().ClaimInterface(*this, interface, force);
}
int32_t USBDevicePipe::ReleaseInterface(const UsbInterface &interface)
{
RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT);
return ((UsbSrvClient *)usbClient)->ReleaseInterface(*this, interface);
return UsbSrvClient::GetInstance().ReleaseInterface(*this, interface);
}
int32_t USBDevicePipe::BulkTransfer(const USBEndpoint &endpoint, std::vector<uint8_t> &bufferData, int32_t timeOut)
{
RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT);
return ((UsbSrvClient *)usbClient)->BulkTransfer(*this, endpoint, bufferData, timeOut);
return UsbSrvClient::GetInstance().BulkTransfer(*this, endpoint, bufferData, timeOut);
}
int32_t USBDevicePipe::ControlTransfer(const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData)
{
RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT);
return ((UsbSrvClient *)usbClient)->ControlTransfer(*this, ctrl, bufferData);
return UsbSrvClient::GetInstance().ControlTransfer(*this, ctrl, bufferData);
}
int32_t USBDevicePipe::SetConfiguration(const USBConfig &config)
{
RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT);
return ((UsbSrvClient *)usbClient)->SetConfiguration(*this, config);
return UsbSrvClient::GetInstance().SetConfiguration(*this, config);
}
int32_t USBDevicePipe::SetInterface(const UsbInterface &interface)
{
RETURN_IF_WITH_RET(usbClient == nullptr, ERR_NO_INIT);
return ((UsbSrvClient *)usbClient)->SetInterface(*this, interface);
return UsbSrvClient::GetInstance().SetInterface(*this, interface);
}
void USBDevicePipe::SetBusNum(uint8_t busNum)
{
this->busNum = busNum;
}
void USBDevicePipe::SetDevAddr(uint8_t devAddr)
{
this->devAddr = devAddr;
}
uint8_t USBDevicePipe::GetBusNum() const
{
return busNum;
}
uint8_t USBDevicePipe::GetDevAddr() const
{
return devAddr;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -19,14 +19,13 @@
namespace OHOS {
namespace USB {
UsbRequest::UsbRequest() : usbClient((uint8_t *)(&UsbSrvClient::GetInstance())) {}
int32_t UsbRequest::Initialize(const USBDevicePipe &pipe, const USBEndpoint &endpoint)
{
this->pipe = pipe;
this->endpoint = endpoint;
int32_t ret = ((UsbSrvClient *)usbClient)->RequestInitialize(*this);
int32_t ret = UsbSrvClient::GetInstance().RequestInitialize(*this);
if (ERR_OK != ret) {
USB_HILOGE(MODULE_USB_INNERKIT, "UsbRequest::%{public}s:%{public}d failed width ret = %{public}d.", __func__,
USB_HILOGE(MODULE_USB_INNERKIT, "UsbRequest::%{public}s:%{public}d failed with ret = %{public}d.", __func__,
__LINE__, ret);
}
return ret;
@ -34,9 +33,9 @@ int32_t UsbRequest::Initialize(const USBDevicePipe &pipe, const USBEndpoint &end
int32_t UsbRequest::Queue()
{
int32_t ret = ((UsbSrvClient *)usbClient)->RequestQueue(*this);
int32_t ret = UsbSrvClient::GetInstance().RequestQueue(*this);
if (ERR_OK != ret) {
USB_HILOGE(MODULE_USB_INNERKIT, "UsbRequest::%{public}s:%{public}d failed width ret = %{public}d.", __func__,
USB_HILOGE(MODULE_USB_INNERKIT, "UsbRequest::%{public}s:%{public}d failed with ret = %{public}d.", __func__,
__LINE__, ret);
}
return ret;
@ -44,9 +43,9 @@ int32_t UsbRequest::Queue()
int32_t UsbRequest::Free()
{
int32_t ret = ((UsbSrvClient *)usbClient)->RequestFree(*this);
int32_t ret = UsbSrvClient::GetInstance().RequestFree(*this);
if (ERR_OK != ret) {
USB_HILOGE(MODULE_USB_INNERKIT, "UsbRequest::%{public}s:%{public}d failed width ret = %{public}d.", __func__,
USB_HILOGE(MODULE_USB_INNERKIT, "UsbRequest::%{public}s:%{public}d failed with ret = %{public}d.", __func__,
__LINE__, ret);
}
return ret;
@ -54,9 +53,9 @@ int32_t UsbRequest::Free()
int32_t UsbRequest::Abort()
{
int32_t ret = ((UsbSrvClient *)usbClient)->RequestAbort(*this);
int32_t ret = UsbSrvClient::GetInstance().RequestAbort(*this);
if (ERR_OK != ret) {
USB_HILOGE(MODULE_USB_INNERKIT, "UsbRequest::%{public}s:%{public}d failed width ret = %{public}d.", __func__,
USB_HILOGE(MODULE_USB_INNERKIT, "UsbRequest::%{public}s:%{public}d failed with ret = %{public}d.", __func__,
__LINE__, ret);
}
return ret;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -27,7 +27,6 @@
namespace OHOS {
namespace USB {
#define USB_MAX_REQUEST_DATA_SIZE 1024
const uint8_t CLAIM_FORCE_1 = 1;
UsbSrvClient::UsbSrvClient()
{
@ -144,6 +143,7 @@ int32_t UsbSrvClient::GetCurrentFunctions(int32_t &funcs)
USB_HILOGI(MODULE_USB_INNERKIT, " Calling GetCurrentFunctions Success!");
return ret;
}
int32_t UsbSrvClient::SetCurrentFunctions(int32_t funcs)
{
RETURN_IF_WITH_RET(Connect() != UEC_OK, false);
@ -156,7 +156,7 @@ int32_t UsbSrvClient::SetCurrentFunctions(int32_t funcs)
return ret;
}
int32_t UsbSrvClient::UsbFunctionsFromString(std::string funcs)
int32_t UsbSrvClient::UsbFunctionsFromString(std::string_view funcs)
{
RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
int32_t result = proxy_->UsbFunctionsFromString(funcs);
@ -215,6 +215,7 @@ int32_t UsbSrvClient::ClaimInterface(USBDevicePipe &pipe, const UsbInterface &in
}
return ret;
}
int32_t UsbSrvClient::ReleaseInterface(USBDevicePipe &pipe, const UsbInterface &interface)
{
RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
@ -224,6 +225,7 @@ int32_t UsbSrvClient::ReleaseInterface(USBDevicePipe &pipe, const UsbInterface &
}
return ret;
}
int32_t UsbSrvClient::BulkTransfer(USBDevicePipe &pipe, const USBEndpoint &endpoint, std::vector<uint8_t> &bufferData,
int32_t timeOut)
{
@ -241,6 +243,7 @@ int32_t UsbSrvClient::BulkTransfer(USBDevicePipe &pipe, const USBEndpoint &endpo
}
return ret;
}
int32_t UsbSrvClient::ControlTransfer(USBDevicePipe &pipe, const UsbCtrlTransfer &ctrl,
std::vector<uint8_t> &bufferData)
{
@ -253,18 +256,21 @@ int32_t UsbSrvClient::ControlTransfer(USBDevicePipe &pipe, const UsbCtrlTransfer
return ret;
}
int32_t UsbSrvClient::SetConfiguration(USBDevicePipe &pipe, const USBConfig &config)
{
RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
int32_t ret = proxy_->SetActiveConfig(pipe.GetBusNum(), pipe.GetDevAddr(), config.GetId());
return ret;
}
int32_t UsbSrvClient::SetInterface(USBDevicePipe &pipe, const UsbInterface &interface)
{
RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
return proxy_->SetInterface(pipe.GetBusNum(), pipe.GetDevAddr(), interface.GetId(),
interface.GetAlternateSetting());
}
int32_t UsbSrvClient::GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t> &bufferData)
{
RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
@ -274,6 +280,7 @@ int32_t UsbSrvClient::GetRawDescriptors(USBDevicePipe &pipe, std::vector<uint8_t
}
return ret;
}
int32_t UsbSrvClient::GetFileDescriptor(USBDevicePipe &pipe, int32_t &fd)
{
RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT);
@ -343,6 +350,7 @@ int32_t UsbSrvClient::RequestQueue(UsbRequest &request)
const UsbPipe tpipe = {ep.GetInterfaceId(), ep.GetAddress()};
return proxy_->RequestQueue(tdev, tpipe, request.GetClientData(), request.GetReqData());
}
int32_t UsbSrvClient::RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint, const sptr<IRemoteObject> &cb)
{
RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
@ -354,6 +362,7 @@ int32_t UsbSrvClient::RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &end
}
return ret;
}
int32_t UsbSrvClient::UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint)
{
RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
@ -365,6 +374,7 @@ int32_t UsbSrvClient::UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &e
}
return ret;
}
int32_t UsbSrvClient::BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem)
{
RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
@ -376,6 +386,7 @@ int32_t UsbSrvClient::BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint,
}
return ret;
}
int32_t UsbSrvClient::BulkWrite(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr<Ashmem> &ashmem)
{
RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);
@ -387,6 +398,7 @@ int32_t UsbSrvClient::BulkWrite(USBDevicePipe &pip, const USBEndpoint &endpoint,
}
return ret;
}
int32_t UsbSrvClient::BulkCancel(USBDevicePipe &pip, const USBEndpoint &endpoint)
{
RETURN_IF_WITH_RET(proxy_ == nullptr, UEC_INTERFACE_NO_INIT);

2
interfaces/kits/js/napi/BUILD.gn Executable file → Normal file
View File

@ -1,4 +1,4 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -108,4 +108,4 @@ struct USBQueueAsyncContext : USBAsyncContext {
};
} // namespace USB
} // namespace OHOS
#endif // USB_ASYNC_CONTEXT_H
#endif // USB_ASYNC_CONTEXT_H

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -36,7 +36,10 @@ void NapiUtil::JsValueToString(const napi_env &env, const napi_value &value, con
USB_HILOGE(MODULE_JS_NAPI, "%{public}s nullptr js object to string malloc failed", __func__);
return;
}
(void)memset_s(buf.get(), bufLen, 0, bufLen);
errno_t ret = memset_s(buf.get(), bufLen, 0, bufLen);
NAPI_ASSERT_RETURN_VOID(env, ret == EOK, "JsValueToString memset_s failed.");
size_t result = 0;
napi_get_value_string_utf8(env, value, buf.get(), bufLen, &result);
target = buf.get();
@ -58,10 +61,14 @@ void NapiUtil::JsObjectToString(const napi_env &env, const napi_value &object, s
napi_typeof(env, field, &valueType);
NAPI_ASSERT_RETURN_VOID(env, valueType == napi_string, "Wrong argument type. String expected.");
std::unique_ptr<char[]> buf = std::make_unique<char[]>(bufLen);
if (buf.get() == nullptr) {
if (buf == nullptr) {
USB_HILOGE(MODULE_JS_NAPI, "%{public}s nullptr js object to string malloc failed", __func__);
return;
}
(void)memset_s(buf.get(), bufLen, 0, bufLen);
errno_t ret = memset_s(buf.get(), bufLen, 0, bufLen);
NAPI_ASSERT_RETURN_VOID(env, ret == EOK, "JsObjectToString memset_s failed.");
size_t result = 0;
napi_get_value_string_utf8(env, field, buf.get(), bufLen, &result);
fieldRef = buf.get();
@ -112,8 +119,8 @@ bool NapiUtil::JsUint8ArrayParse(const napi_env &env, const napi_value &object,
napi_typedarray_type type;
napi_value buffer;
napi_status infoStatus =
napi_get_typedarray_info(env, object, &type, &bufferSize, (void **)uint8Buffer, &buffer, &offset);
napi_status infoStatus = napi_get_typedarray_info(env, object, &type, &bufferSize,
reinterpret_cast<void **>(uint8Buffer), &buffer, &offset);
if (infoStatus != napi_ok) {
USB_HILOGW(MODULE_JS_NAPI, "%{public}s get typedarray info failed, status: %{public}d", __func__, infoStatus);
return false;
@ -124,21 +131,27 @@ bool NapiUtil::JsUint8ArrayParse(const napi_env &env, const napi_value &object,
return false;
}
if (bufferSize == 0) {
USB_HILOGW(MODULE_JS_NAPI, "%{public}s:%{public}d bufferSize error", __func__, __LINE__);
return false;
}
return true;
}
void NapiUtil::Uint8ArrayToJsValue(const napi_env &env, std::vector<uint8_t> &uint8Buffer, size_t bufferSize,
napi_value &result)
{
if (bufferSize < 0) {
napi_get_undefined(env, &result);
return;
}
uint8_t *nativeArraybuffer = nullptr;
napi_value nativeValue = nullptr;
napi_create_arraybuffer(env, bufferSize, (void **)&nativeArraybuffer, &nativeValue);
memcpy_s(nativeArraybuffer, bufferSize, (const char *)uint8Buffer.data(), bufferSize);
errno_t ret = memcpy_s(nativeArraybuffer, bufferSize, uint8Buffer.data(), bufferSize);
if (ret != EOK) {
USB_HILOGE(MODULE_JS_NAPI, "%{public}s:%{public}d memcpy_s failed\n", __func__, __LINE__);
return;
}
napi_create_typedarray(env, napi_uint8_array, bufferSize, nativeValue, 0, &result);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -33,7 +33,7 @@
using namespace OHOS;
using namespace OHOS::USB;
const int32_t MSEC_TIME = 1000;
const int32_t INDEX_0 = 0;
const int32_t INDEX_1 = 1;
const int32_t INDEX_2 = 2;
@ -185,29 +185,31 @@ static bool ParseEndpointsObjs(const napi_env env, const napi_value interfaceObj
// Get the array.
bool result = false;
napi_status status = napi_is_array(env, interfaceObj, &result);
if (result && status == napi_ok) {
napi_value endpointsObjs;
bool isGetObjSuccess = NapiUtil::JsObjectGetProperty(env, interfaceObj, "endpoints", endpointsObjs);
if (!isGetObjSuccess) {
return false;
}
uint32_t endpointCount = 0;
status = napi_get_array_length(env, endpointsObjs, &endpointCount);
for (uint32_t k = 0; k < endpointCount; ++k) {
napi_value endpointObj;
napi_status status = napi_get_element(env, endpointsObjs, k, &endpointObj);
if (status != napi_ok) {
return false;
}
USBEndpoint ep;
ParseEndpointObj(env, endpointObj, ep);
eps.push_back(ep);
}
if (result == false || status != napi_ok) {
return false;
}
return false;
napi_value endpointsObjs;
bool isGetObjSuccess = NapiUtil::JsObjectGetProperty(env, interfaceObj, "endpoints", endpointsObjs);
if (!isGetObjSuccess) {
return false;
}
uint32_t endpointCount = 0;
status = napi_get_array_length(env, endpointsObjs, &endpointCount);
for (uint32_t k = 0; k < endpointCount; ++k) {
napi_value endpointObj;
napi_status status = napi_get_element(env, endpointsObjs, k, &endpointObj);
if (status != napi_ok) {
return false;
}
USBEndpoint ep;
ParseEndpointObj(env, endpointObj, ep);
eps.push_back(ep);
}
return true;
}
struct PipeControlParam {
@ -360,19 +362,10 @@ static void ParseDeviceObj(const napi_env env, const napi_value deviceObj, UsbDe
protocol, configs);
}
static long getTimeDiff(timeval startTime, timeval endTime)
{
return (endTime.tv_sec - startTime.tv_sec) * MSEC_TIME + (endTime.tv_usec - startTime.tv_usec) / MSEC_TIME;
}
/* ============================================= Usb Core ============================================= */
static napi_value CoreGetDevices(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value argv[PARAM_COUNT_1] = {0};
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
@ -397,17 +390,12 @@ static napi_value CoreGetDevices(napi_env env, napi_callback_info info)
napi_set_element(env, result, i, device);
++i;
}
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms", __func__, getTimeDiff(start, end));
return result;
}
static napi_value CoreConnectDevice(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value argv[PARAM_COUNT_1] = {0};
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
@ -430,18 +418,12 @@ static napi_value CoreConnectDevice(napi_env env, napi_callback_info info)
} else {
napi_get_undefined(env, &pipObj);
}
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms, ret : %{public}d", __func__,
getTimeDiff(start, end), ret);
return pipObj;
}
static napi_value CoreHasRight(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value args[PARAM_COUNT_1] = {0};
napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
@ -458,30 +440,22 @@ static napi_value CoreHasRight(napi_env env, napi_callback_info info)
napi_value napiValue = nullptr;
napi_get_boolean(env, result == UEC_OK, &napiValue);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms", __func__, getTimeDiff(start, end));
return napiValue;
}
static auto g_requestRightExecute = [](napi_env env, void *data) {
struct timeval beginAsync = {0};
gettimeofday(&beginAsync, nullptr);
USBRightAsyncContext *asyncContext = (USBRightAsyncContext *)data;
USBRightAsyncContext *asyncContext = reinterpret_cast<USBRightAsyncContext *>(data);
int32_t ret = g_usbClient.RequestRight(asyncContext->deviceName);
if (ret == UEC_OK) {
asyncContext->status = napi_ok;
} else {
asyncContext->status = napi_generic_failure;
}
struct timeval endAsync = {0};
gettimeofday(&endAsync, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call CoreRequestRight async work finished, takes : %{public}ld ms",
getTimeDiff(beginAsync, endAsync));
};
static auto g_requestRightComplete = [](napi_env env, napi_status status, void *data) {
USBRightAsyncContext *asyncContext = (USBRightAsyncContext *)data;
USBRightAsyncContext *asyncContext = reinterpret_cast<USBRightAsyncContext *>(data);
napi_value queryResult = nullptr;
napi_get_boolean(env, asyncContext->status == napi_ok, &queryResult);
@ -492,9 +466,6 @@ static auto g_requestRightComplete = [](napi_env env, napi_status status, void *
static napi_value CoreRequestRight(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value args[PARAM_COUNT_1] = {0};
napi_status status = napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
@ -506,7 +477,9 @@ static napi_value CoreRequestRight(napi_env env, napi_callback_info info)
std::string deviceName;
NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, deviceName);
auto asyncContext = new USBRightAsyncContext();
auto asyncContext = new(std::nothrow) USBRightAsyncContext();
NAPI_ASSERT(env, asyncContext != nullptr, "Create USBRightAsyncContext failed.");
asyncContext->env = env;
asyncContext->deviceName = deviceName;
@ -516,20 +489,15 @@ static napi_value CoreRequestRight(napi_env env, napi_callback_info info)
napi_value resource = nullptr;
napi_create_string_utf8(env, "RequestRight", NAPI_AUTO_LENGTH, &resource);
napi_create_async_work(env, nullptr, resource, g_requestRightExecute, g_requestRightComplete, (void *)asyncContext,
&asyncContext->work);
napi_create_async_work(env, nullptr, resource, g_requestRightExecute, g_requestRightComplete,
reinterpret_cast<void *>(asyncContext), &asyncContext->work);
napi_queue_async_work(env, asyncContext->work);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms", __func__, getTimeDiff(start, end));
return result;
}
static napi_value CoreUsbFunctionsFromString(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value argv[PARAM_COUNT_1] = {0};
@ -548,17 +516,12 @@ static napi_value CoreUsbFunctionsFromString(napi_env env, napi_callback_info in
napi_value result;
napi_create_int32(env, numFuncs, &result);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms", __func__, getTimeDiff(start, end));
return result;
}
static napi_value CoreUsbFunctionsToString(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value argv[PARAM_COUNT_1] = {0};
@ -576,30 +539,21 @@ static napi_value CoreUsbFunctionsToString(napi_env env, napi_callback_info info
napi_value result;
napi_create_string_utf8(env, strFuncs.c_str(), NAPI_AUTO_LENGTH, &result);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms", __func__, getTimeDiff(start, end));
return result;
}
static auto g_setCurrentFunctionExecute = [](napi_env env, void *data) {
struct timeval beginAsync = {0};
gettimeofday(&beginAsync, nullptr);
USBFunctionAsyncContext *asyncContext = (USBFunctionAsyncContext *)data;
USBFunctionAsyncContext *asyncContext = reinterpret_cast<USBFunctionAsyncContext *>(data);
int32_t ret = g_usbClient.SetCurrentFunctions(asyncContext->functions);
if (ret == UEC_OK) {
asyncContext->status = napi_ok;
} else {
asyncContext->status = napi_generic_failure;
}
struct timeval endAsync = {0};
gettimeofday(&endAsync, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call CoreSetCurrentFunctions async work finished, takes : %{public}ld ms",
getTimeDiff(beginAsync, endAsync));
};
static auto g_setCurrentFunctionComplete = [](napi_env env, napi_status status, void *data) {
USBFunctionAsyncContext *asyncContext = (USBFunctionAsyncContext *)data;
USBFunctionAsyncContext *asyncContext = reinterpret_cast<USBFunctionAsyncContext *>(data);
napi_value queryResult = nullptr;
napi_get_boolean(env, asyncContext->status == napi_ok, &queryResult);
@ -610,9 +564,6 @@ static auto g_setCurrentFunctionComplete = [](napi_env env, napi_status status,
static napi_value CoreSetCurrentFunctions(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value argv[PARAM_COUNT_1] = {0};
@ -626,7 +577,9 @@ static napi_value CoreSetCurrentFunctions(napi_env env, napi_callback_info info)
int32_t funcs = 0;
napi_get_value_int32(env, argv[INDEX_0], &funcs);
auto asyncContext = new USBFunctionAsyncContext();
auto asyncContext = new(std::nothrow) USBFunctionAsyncContext();
NAPI_ASSERT(env, asyncContext != nullptr, "Create USBFunctionAsyncContext failed.");
asyncContext->env = env;
asyncContext->functions = funcs;
napi_value result = nullptr;
@ -636,20 +589,14 @@ static napi_value CoreSetCurrentFunctions(napi_env env, napi_callback_info info)
napi_create_string_utf8(env, "SetCurrentFunctions", NAPI_AUTO_LENGTH, &resource);
napi_create_async_work(env, nullptr, resource, g_setCurrentFunctionExecute, g_setCurrentFunctionComplete,
(void *)asyncContext, &asyncContext->work);
reinterpret_cast<void *>(asyncContext), &asyncContext->work);
napi_queue_async_work(env, asyncContext->work);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms", __func__, getTimeDiff(start, end));
return result;
}
static napi_value CoreGetCurrentFunctions(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value argv[PARAM_COUNT_1] = {0};
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
@ -664,18 +611,12 @@ static napi_value CoreGetCurrentFunctions(napi_env env, napi_callback_info info)
return result;
}
napi_create_int32(env, cfuncs, &result);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms", __func__, getTimeDiff(start, end));
return result;
}
static napi_value CoreGetPorts(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value argv[PARAM_COUNT_1] = {0};
napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
@ -705,9 +646,7 @@ static napi_value CoreGetPorts(napi_env env, napi_callback_info info)
napi_set_named_property(env, port, "status", usbPortStatus);
napi_set_element(env, result, i, port);
}
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms", __func__, getTimeDiff(start, end));
return result;
}
@ -715,9 +654,6 @@ static napi_value CoreGetPorts(napi_env env, napi_callback_info info)
static napi_value PortGetSupportedModes(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value args[PARAM_COUNT_1] = {0};
@ -737,31 +673,22 @@ static napi_value PortGetSupportedModes(napi_env env, napi_callback_info info)
}
napi_value napiValue = nullptr;
NAPI_CALL(env, napi_create_int32(env, result, &napiValue));
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, result : %{public}d, takes : %{public}ld ms", __func__, result,
getTimeDiff(start, end));
return napiValue;
}
static auto g_setPortRoleExecute = [](napi_env env, void *data) {
struct timeval beginAsync = {0};
gettimeofday(&beginAsync, nullptr);
USBPortRoleAsyncContext *asyncContext = (USBPortRoleAsyncContext *)data;
USBPortRoleAsyncContext *asyncContext = reinterpret_cast<USBPortRoleAsyncContext *>(data);
int32_t ret = g_usbClient.SetPortRole(asyncContext->portId, asyncContext->powerRole, asyncContext->dataRole);
if (ret == UEC_OK) {
asyncContext->status = napi_ok;
} else {
asyncContext->status = napi_generic_failure;
}
struct timeval endAsync = {0};
gettimeofday(&endAsync, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call PortSetPortRole async work finished, takes : %{public}ld ms",
getTimeDiff(beginAsync, endAsync));
};
static auto g_setPortRoleComplete = [](napi_env env, napi_status status, void *data) {
USBPortRoleAsyncContext *asyncContext = (USBPortRoleAsyncContext *)data;
USBPortRoleAsyncContext *asyncContext = reinterpret_cast<USBPortRoleAsyncContext *>(data);
napi_value queryResult = nullptr;
napi_get_boolean(env, asyncContext->status == napi_ok, &queryResult);
@ -773,9 +700,6 @@ static auto g_setPortRoleComplete = [](napi_env env, napi_status status, void *d
static napi_value PortSetPortRole(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_3;
napi_value args[PARAM_COUNT_3] = {0};
@ -797,7 +721,9 @@ static napi_value PortSetPortRole(napi_env env, napi_callback_info info)
int32_t dataRole = 0;
napi_get_value_int32(env, args[INDEX_2], &dataRole);
auto asyncContext = new USBPortRoleAsyncContext();
auto asyncContext = new(std::nothrow) USBPortRoleAsyncContext();
NAPI_ASSERT(env, asyncContext != nullptr, "Create USBPortRoleAsyncContext failed.");
asyncContext->env = env;
asyncContext->portId = id;
asyncContext->dataRole = dataRole;
@ -809,20 +735,15 @@ static napi_value PortSetPortRole(napi_env env, napi_callback_info info)
napi_value resource = nullptr;
napi_create_string_utf8(env, "PortSetPortRole", NAPI_AUTO_LENGTH, &resource);
napi_create_async_work(env, nullptr, resource, g_setPortRoleExecute, g_setPortRoleComplete, (void *)asyncContext,
&asyncContext->work);
napi_create_async_work(env, nullptr, resource, g_setPortRoleExecute, g_setPortRoleComplete,
reinterpret_cast<void *>(asyncContext), &asyncContext->work);
napi_queue_async_work(env, asyncContext->work);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms", __func__, getTimeDiff(start, end));
return result;
}
static napi_value PipeClaimInterface(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_3;
napi_value argv[PARAM_COUNT_3] = {0};
@ -854,18 +775,12 @@ static napi_value PipeClaimInterface(napi_env env, napi_callback_info info)
USB_HILOGD(MODULE_JS_NAPI, "pipe call ClaimInterface ret: %{public}d", ret);
napi_value result;
napi_create_int32(env, ret, &result);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, ret: %{public}d takes : %{public}ld ms", __func__, ret,
getTimeDiff(start, end));
return result;
}
static napi_value PipeReleaseInterface(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_2;
napi_value argv[PARAM_COUNT_2] = {0};
@ -887,18 +802,12 @@ static napi_value PipeReleaseInterface(napi_env env, napi_callback_info info)
USB_HILOGD(MODULE_JS_NAPI, "pipe call PipeReleaseInterface ret: %{public}d", ret);
napi_value result;
napi_create_int32(env, ret, &result);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, ret: %{public}d takes : %{public}ld ms", __func__, ret,
getTimeDiff(start, end));
return result;
}
static napi_value PipeSetInterface(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_2;
napi_value argv[PARAM_COUNT_2] = {0};
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
@ -921,18 +830,12 @@ static napi_value PipeSetInterface(napi_env env, napi_callback_info info)
int32_t ret = g_usbClient.SetInterface(pipe, interface);
napi_value result;
napi_create_int32(env, ret, &result);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, ret: %{public}d takes : %{public}ld ms", __func__, ret,
getTimeDiff(start, end));
return result;
}
static napi_value PipeSetConfiguration(napi_env env, napi_callback_info info)
{
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_2;
napi_value argv[PARAM_COUNT_2] = {0};
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
@ -955,18 +858,12 @@ static napi_value PipeSetConfiguration(napi_env env, napi_callback_info info)
int32_t ret = g_usbClient.SetConfiguration(pipe, config);
napi_value result;
napi_create_int32(env, ret, &result);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, ret: %{public}d, takes : %{public}ld ms", __func__, ret,
getTimeDiff(start, end));
return result;
}
static napi_value PipeGetRawDescriptors(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value argv[PARAM_COUNT_1] = {0};
@ -988,18 +885,12 @@ static napi_value PipeGetRawDescriptors(napi_env env, napi_callback_info info)
} else {
napi_get_undefined(env, &result);
}
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, ret: %{public}d takes : %{public}ld ms", __func__, ret,
getTimeDiff(start, end));
return result;
}
static napi_value PipeGetFileDescriptor(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value argv[PARAM_COUNT_1] = {0};
@ -1016,43 +907,41 @@ static napi_value PipeGetFileDescriptor(napi_env env, napi_callback_info info)
int32_t fd = -1;
napi_value result;
int32_t ret = g_usbClient.GetFileDescriptor(pipe, fd);
NAPI_ASSERT(env, ret == UEC_OK, "Get File Descriptor failed.");
napi_create_int32(env, fd, &result);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, ret : %{public}d, takes : %{public}ld ms",
__func__, ret, getTimeDiff(start, end));
return result;
}
static auto g_controlTransferExecute = [](napi_env env, void *data) {
struct timeval beginAsync = {0};
gettimeofday(&beginAsync, nullptr);
USBControlTransferAsyncContext *asyncContext = (USBControlTransferAsyncContext *)data;
std::vector<uint8_t> bufferData(asyncContext->buffer, asyncContext->buffer + asyncContext->bufferLength);
const UsbCtrlTransfer tctrl = {asyncContext->reqType, asyncContext->request, asyncContext->value,
asyncContext->index, asyncContext->timeOut};
int32_t ret = asyncContext->pipe.ControlTransfer(tctrl, bufferData);
if ((asyncContext->reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT) {
delete [] asyncContext->buffer;
asyncContext->buffer = nullptr;
} else {
(bufferData.size() > asyncContext->bufferLength) ? asyncContext->bufferLength = asyncContext->bufferLength
: asyncContext->bufferLength = bufferData.size();
memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(), asyncContext->bufferLength);
if (bufferData.size() < asyncContext->bufferLength) {
asyncContext->bufferLength = bufferData.size();
}
ret = memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(),
asyncContext->bufferLength);
NAPI_ASSERT_RETURN_VOID(env, ret == EOK, "ControlTransferExecute memcpy_s failed.");
}
if (ret == UEC_OK) {
asyncContext->status = napi_ok;
} else {
asyncContext->status = napi_generic_failure;
}
struct timeval endAsync = {0};
gettimeofday(&endAsync, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call PipeControlTransfer async work finished, takes : %{public}ld ms",
getTimeDiff(beginAsync, endAsync));
};
static auto g_controlTransferComplete = [](napi_env env, napi_status status, void *data) {
USBControlTransferAsyncContext *asyncContext = (USBControlTransferAsyncContext *)data;
USBControlTransferAsyncContext *asyncContext = reinterpret_cast<USBControlTransferAsyncContext *>(data);
napi_value queryResult = nullptr;
if (asyncContext->status == napi_ok) {
@ -1066,24 +955,27 @@ static auto g_controlTransferComplete = [](napi_env env, napi_status status, voi
delete asyncContext;
};
static napi_value PipeControlTransfer(napi_env env, napi_callback_info info)
static std::tuple<bool, USBDevicePipe, PipeControlParam, int32_t> GetControlTransferParam(napi_env env,
napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_3;
napi_value argv[PARAM_COUNT_3] = {0};
napi_status status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
NAPI_ASSERT(env, (status == napi_ok) && (argc >= PARAM_COUNT_2), "ControlTransfer failed to get cb info");
if (status != napi_ok || argc < PARAM_COUNT_2) {
USB_HILOGE(MODULE_JS_NAPI, "ControlTransfer failed to get cb info\n");
return {false, {}, {}, {} };
}
// pipe param
napi_value pipeObj = argv[INDEX_0];
napi_valuetype type;
napi_typeof(env, pipeObj, &type);
NAPI_ASSERT(env, type == napi_object, "index 0 wrong argument type, object expected.");
napi_typeof(env, argv[INDEX_0], &type);
if (type != napi_object) {
USB_HILOGE(MODULE_JS_NAPI, "index 0 wrong argument type, object expected.\n");
return {false, {}, {}, {} };
}
USBDevicePipe pipe;
ParseUsbDevicePipe(env, pipeObj, pipe);
ParseUsbDevicePipe(env, argv[INDEX_0], pipe);
// control params
PipeControlParam controlParam = {0};
@ -1095,7 +987,17 @@ static napi_value PipeControlTransfer(napi_env env, napi_callback_info info)
napi_get_value_int32(env, argv[INDEX_2], &timeOut);
}
auto asyncContext = new USBControlTransferAsyncContext();
return { true, pipe, controlParam, timeOut };
}
static napi_value PipeControlTransfer(napi_env env, napi_callback_info info)
{
auto [res, pipe, controlParam, timeOut] = GetControlTransferParam(env, info);
NAPI_ASSERT(env, res == true, "GetControlTransferParam failed.");
auto asyncContext = new(std::nothrow) USBControlTransferAsyncContext();
NAPI_ASSERT(env, asyncContext != nullptr, "New USBControlTransferAsyncContext failed.");
asyncContext->env = env;
asyncContext->pipe = pipe;
asyncContext->request = controlParam.request;
@ -1105,8 +1007,20 @@ static napi_value PipeControlTransfer(napi_env env, napi_callback_info info)
asyncContext->index = controlParam.index;
if ((asyncContext->reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT) {
uint8_t *nativeArraybuffer = new uint8_t[controlParam.dataLength];
memcpy_s(nativeArraybuffer, controlParam.dataLength, controlParam.data, controlParam.dataLength);
uint8_t *nativeArraybuffer = new(std::nothrow) uint8_t[controlParam.dataLength];
if (nativeArraybuffer == nullptr) {
USB_HILOGE(MODULE_JS_NAPI, "%{public}s:%{public}d, new failed", __func__, __LINE__);
delete asyncContext;
return nullptr;
}
errno_t ret = memcpy_s(nativeArraybuffer, controlParam.dataLength, controlParam.data, controlParam.dataLength);
if (ret != EOK) {
USB_HILOGE(MODULE_JS_NAPI, "%{public}s:%{public}d, memcpy_s failed\n", __func__, __LINE__);
delete asyncContext;
delete []nativeArraybuffer;
return nullptr;
}
asyncContext->buffer = nativeArraybuffer;
} else {
asyncContext->buffer = controlParam.data;
@ -1121,19 +1035,14 @@ static napi_value PipeControlTransfer(napi_env env, napi_callback_info info)
napi_create_string_utf8(env, "PipeControlTransfer", NAPI_AUTO_LENGTH, &resource);
napi_create_async_work(env, nullptr, resource, g_controlTransferExecute, g_controlTransferComplete,
(void *)asyncContext, &asyncContext->work);
reinterpret_cast<void *>(asyncContext), &asyncContext->work);
napi_queue_async_work(env, asyncContext->work);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms", __func__, getTimeDiff(start, end));
return result;
}
static auto g_bulkTransferExecute = [](napi_env env, void *data) {
struct timeval beginAsync = {0};
gettimeofday(&beginAsync, nullptr);
USBBulkTransferAsyncContext *asyncContext = (USBBulkTransferAsyncContext *)data;
USBBulkTransferAsyncContext *asyncContext = reinterpret_cast<USBBulkTransferAsyncContext *>(data);
std::vector<uint8_t> bufferData(asyncContext->buffer, asyncContext->buffer + asyncContext->bufferLength);
int32_t ret = asyncContext->pipe.BulkTransfer(asyncContext->endpoint, bufferData, asyncContext->timeOut);
@ -1141,9 +1050,12 @@ static auto g_bulkTransferExecute = [](napi_env env, void *data) {
delete [] asyncContext->buffer;
asyncContext->buffer = nullptr;
} else {
(bufferData.size() > asyncContext->bufferLength) ? asyncContext->bufferLength = asyncContext->bufferLength
: asyncContext->bufferLength = bufferData.size();
memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(), asyncContext->bufferLength);
if (bufferData.size() < asyncContext->bufferLength) {
asyncContext->bufferLength = bufferData.size();
}
ret = memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(), asyncContext->bufferLength);
NAPI_ASSERT_RETURN_VOID(env, ret == EOK, "BulkTransferExecute memcpy_s failed.");
}
USB_HILOGD(MODULE_JS_NAPI, "call pipe result %{public}d", ret);
@ -1152,14 +1064,10 @@ static auto g_bulkTransferExecute = [](napi_env env, void *data) {
} else {
asyncContext->status = napi_generic_failure;
}
struct timeval endAsync = {0};
gettimeofday(&endAsync, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call PipeBulkTransfer async work finished, takes : %{public}ld ms",
getTimeDiff(beginAsync, endAsync));
};
static auto g_bulkTransferComplete = [](napi_env env, napi_status status, void *data) {
USBBulkTransferAsyncContext *asyncContext = (USBBulkTransferAsyncContext *)data;
USBBulkTransferAsyncContext *asyncContext = reinterpret_cast<USBBulkTransferAsyncContext *>(data);
napi_value queryResult = nullptr;
if (asyncContext->status == napi_ok) {
napi_create_int32(env, asyncContext->bufferLength, &queryResult);
@ -1197,8 +1105,8 @@ static bool GetBulkTransferParams(napi_env env, napi_callback_info info, USBBulk
}
uint8_t *buffer = nullptr;
size_t offset;
size_t bufferSize;
size_t offset = 0;
size_t bufferSize = 0;
bool hasBuffer = NapiUtil::JsUint8ArrayParse(env, argv[INDEX_2], &buffer, bufferSize, offset);
if (!hasBuffer) {
USB_HILOGE(MODULE_JS_NAPI, "BulkTransfer wrong argument, buffer is null");
@ -1209,8 +1117,16 @@ static bool GetBulkTransferParams(napi_env env, napi_callback_info info, USBBulk
asyncContext.endpoint = ep;
if (ep.GetDirection() == USB_ENDPOINT_DIR_OUT) {
uint8_t *nativeArraybuffer = new uint8_t[bufferSize];
memcpy_s(nativeArraybuffer, bufferSize, buffer, bufferSize);
uint8_t *nativeArraybuffer = new(std::nothrow) uint8_t[bufferSize];
NAPI_ASSERT(env, nativeArraybuffer != nullptr, "nativeArraybuffer create failed.");
errno_t ret = memcpy_s(nativeArraybuffer, bufferSize, buffer, bufferSize);
if (ret != EOK) {
USB_HILOGE(MODULE_JS_NAPI, "%{public}s:%{public}d, memcpy_s failed\n", __func__, __LINE__);
delete []nativeArraybuffer;
return false;
}
asyncContext.buffer = nativeArraybuffer;
} else {
asyncContext.buffer = buffer;
@ -1222,11 +1138,9 @@ static bool GetBulkTransferParams(napi_env env, napi_callback_info info, USBBulk
static napi_value PipeBulkTransfer(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
auto asyncContext = new(std::nothrow) USBBulkTransferAsyncContext();
NAPI_ASSERT(env, asyncContext != nullptr, "Create USBBulkTransferAsyncContext failed.");
auto asyncContext = new USBBulkTransferAsyncContext();
napi_value result = nullptr;
napi_create_promise(env, &asyncContext->deferred, &result);
if (!GetBulkTransferParams(env, info, *asyncContext)) {
@ -1243,26 +1157,18 @@ static napi_value PipeBulkTransfer(napi_env env, napi_callback_info info)
napi_create_string_utf8(env, "PipeBulkTransfer", NAPI_AUTO_LENGTH, &resource);
napi_status status = napi_create_async_work(env, nullptr, resource, g_bulkTransferExecute, g_bulkTransferComplete,
(void *)asyncContext, &asyncContext->work);
struct timeval end = {0};
gettimeofday(&end, nullptr);
reinterpret_cast<void *>(asyncContext), &asyncContext->work);
if (status != napi_ok) {
USB_HILOGE(MODULE_JS_NAPI, "end call %{public}s, create async work failed, takes : %{public}ld ms", __func__,
getTimeDiff(start, end));
USB_HILOGE(MODULE_JS_NAPI, "%{public}s, create async work failed", __func__);
return result;
}
napi_queue_async_work(env, asyncContext->work);
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, takes : %{public}ld ms", __func__, getTimeDiff(start, end));
return result;
}
static napi_value PipeClose(napi_env env, napi_callback_info info)
{
USB_HILOGD(MODULE_JS_NAPI, "begin call %{public}s", __func__);
struct timeval start = {0};
gettimeofday(&start, nullptr);
size_t argc = PARAM_COUNT_1;
napi_value argv[PARAM_COUNT_1] = {0};
@ -1279,16 +1185,13 @@ static napi_value PipeClose(napi_env env, napi_callback_info info)
int32_t ret = pipe.Close();
napi_value result;
napi_create_int32(env, ret, &result);
struct timeval end = {0};
gettimeofday(&end, nullptr);
USB_HILOGD(MODULE_JS_NAPI, "end call %{public}s, result %{public}d, takes : %{public}ld ms", __func__, ret,
getTimeDiff(start, end));
return result;
}
static napi_value GetVersion(napi_env env, napi_callback_info info)
{
std::string version = g_usbClient.GetVersion();
auto version = g_usbClient.GetVersion();
USB_HILOGD(MODULE_JS_NAPI, "call %{public}s, version is %{public}s", __func__, version.c_str());
napi_value result;
napi_create_string_utf8(env, version.c_str(), NAPI_AUTO_LENGTH, &result);

2
services/BUILD.gn Executable file → Normal file
View File

@ -1,4 +1,4 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -27,24 +27,24 @@ class UsbFunctionManager {
public:
static bool AreSettableFunctions(int32_t funcs);
static int32_t FromStringFunctions(std::string funcs);
static int32_t FromStringFunctions(std::string_view funcs);
static std::string ToStringFunctions(int32_t func);
static void updateFunctions(int32_t func);
static int32_t getCurrentFunctions();
static const std::string FUNCTION_NAME_NONE;
static const std::string FUNCTION_NAME_HDC;
static const std::string FUNCTION_NAME_ACM;
static const std::string FUNCTION_NAME_ECM;
static constexpr std::string_view FUNCTION_NAME_NONE = "none";
static constexpr std::string_view FUNCTION_NAME_HDC = "hdc";
static constexpr std::string_view FUNCTION_NAME_ACM = "acm";
static constexpr std::string_view FUNCTION_NAME_ECM = "ecm";
static const int32_t FUNCTION_NONE;
static const int32_t FUNCTION_ACM;
static const int32_t FUNCTION_ECM;
static const int32_t FUNCTION_HDC;
static constexpr int32_t FUNCTION_NONE = 0;
static constexpr int32_t FUNCTION_ACM = 1;
static constexpr int32_t FUNCTION_ECM = 2;
static constexpr int32_t FUNCTION_HDC = 4;
private:
static const int32_t FUNCTION_SETTABLE;
static const std::map<std::string, int32_t> FUNCTION_MAPPING_N2C;
static constexpr int32_t FUNCTION_SETTABLE = FUNCTION_HDC | FUNCTION_ACM | FUNCTION_ECM;
static const std::map<std::string_view, int32_t> FUNCTION_MAPPING_N2C;
static int32_t currentFunctions;
};
} // namespace USB

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -29,7 +29,7 @@ typedef std::map<std::string, UsbDevice *> MAP_STR_DEVICE;
class UsbHostManager {
public:
UsbHostManager(SystemAbility *systemAbility);
explicit UsbHostManager(SystemAbility *systemAbility);
~UsbHostManager();
void GetDevices(MAP_STR_DEVICE &devices);
bool DelDevice(uint8_t busNum, uint8_t devNum);

View File

@ -32,10 +32,10 @@ public:
RightMap rightMap;
void Init();
int32_t HasRight(std::string deviceName, std::string bundleName);
int32_t RequestRight(std::string deviceName, std::string bundleName);
bool AddDeviceRight(std::string deviceName, std::string bundleName);
bool RemoveDeviceRight(std::string deviceName);
int32_t HasRight(const std::string &deviceName, const std::string &bundleName);
int32_t RequestRight(const std::string &deviceName, const std::string &bundleName);
bool AddDeviceRight(const std::string &deviceName, const std::string &bundleName);
bool RemoveDeviceRight(const std::string &deviceName);
};
} // namespace USB
} // namespace OHOS

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -40,8 +40,8 @@ class UsbService : public SystemAbility, public UsbServerStub {
DECLARE_DELAYED_SP_SINGLETON(UsbService);
public:
virtual void OnStart() override;
virtual void OnStop() override;
void OnStart() override;
void OnStop() override;
bool IsServiceReady() const
{
@ -59,7 +59,7 @@ public:
int32_t GetDevices(std::vector<UsbDevice> &deviceList) override;
int32_t GetCurrentFunctions(int32_t &funcs) override;
int32_t SetCurrentFunctions(int32_t funcs) override;
int32_t UsbFunctionsFromString(std::string funcs) override;
int32_t UsbFunctionsFromString(std::string_view funcs) override;
std::string UsbFunctionsToString(int32_t funcs) override;
int32_t GetPorts(std::vector<UsbPort> &ports) override;
int32_t GetSupportedModes(int32_t portId, int32_t &result) override;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -44,8 +44,8 @@ int32_t UsbDescriptorParser::ParseDeviceDescriptor(const uint8_t *buffer, uint32
return UEC_SERVICE_INVALID_VALUE;
}
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: parse begin buffer=%{public}p, length=%{public}d, cursor=%{public}d",
__func__, buffer, length, cursor);
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: parse begin length=%{public}u, cursor=%{public}u",
__func__, length, cursor);
uint32_t deviceDescriptorSize = sizeof(UsbdDeviceDescriptor);
if (length < deviceDescriptorSize) {
USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: buffer size error", __func__);
@ -81,8 +81,8 @@ int32_t UsbDescriptorParser::ParseConfigDescriptor(const uint8_t *buffer, uint32
return UEC_SERVICE_INVALID_VALUE;
}
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: parse begin buffer=%{public}p, length=%{public}d, cursor=%{public}d",
__func__, buffer, length, cursor);
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: parse begin length=%{public}u, cursor=%{public}u",
__func__, length, cursor);
uint32_t configDescriptorSize = sizeof(UsbdConfigDescriptor);
if (length < configDescriptorSize) {
USB_HILOGE(MODULE_USB_SERVICE, "%{public}s: buffer size error", __func__);
@ -170,11 +170,12 @@ int32_t UsbDescriptorParser::ParseInterfaceDescriptor(const uint8_t *buffer, uin
interface.SetEndpoints(eps);
return UEC_OK;
}
int32_t UsbDescriptorParser::ParseEndpointDescriptor(const uint8_t *buffer, uint32_t length, uint32_t &cursor,
USBEndpoint &ep)
{
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: parse begin buffer=%{public}p, length=%{public}d, cursor=%{public}d",
__func__, buffer, length, cursor);
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s: parse begin, length=%{public}u, cursor=%{public}u",
__func__, length, cursor);
if (buffer == nullptr || length == 0) {
return UEC_SERVICE_INVALID_VALUE;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -18,33 +18,21 @@
namespace OHOS {
namespace USB {
const std::string UsbFunctionManager::FUNCTION_NAME_NONE = "none";
const std::string UsbFunctionManager::FUNCTION_NAME_HDC = "hdc";
const std::string UsbFunctionManager::FUNCTION_NAME_ACM = "acm";
const std::string UsbFunctionManager::FUNCTION_NAME_ECM = "ecm";
const int32_t UsbFunctionManager::FUNCTION_NONE = 0;
const int32_t UsbFunctionManager::FUNCTION_ACM = 1;
const int32_t UsbFunctionManager::FUNCTION_ECM = 2;
const int32_t UsbFunctionManager::FUNCTION_HDC = 4;
int32_t UsbFunctionManager::currentFunctions = 0;
const int32_t UsbFunctionManager::FUNCTION_SETTABLE = FUNCTION_HDC | FUNCTION_ACM | FUNCTION_ECM;
const std::map<std::string, int32_t> UsbFunctionManager::FUNCTION_MAPPING_N2C = {
const std::map<std::string_view, int32_t> UsbFunctionManager::FUNCTION_MAPPING_N2C = {
{FUNCTION_NAME_NONE, FUNCTION_NONE},
{FUNCTION_NAME_ACM, FUNCTION_ACM},
{FUNCTION_NAME_ECM, FUNCTION_ECM},
{FUNCTION_NAME_HDC, FUNCTION_HDC},
};
int32_t UsbFunctionManager::currentFunctions = 0;
bool UsbFunctionManager::AreSettableFunctions(int32_t funcs)
{
return funcs == FUNCTION_NONE || ((~FUNCTION_SETTABLE & funcs) == 0);
}
int32_t UsbFunctionManager::FromStringFunctions(std::string strFun)
int32_t UsbFunctionManager::FromStringFunctions(std::string_view strFun)
{
if (strFun.compare(FUNCTION_NAME_NONE) == 0) {
return FUNCTION_NONE;
@ -55,11 +43,11 @@ int32_t UsbFunctionManager::FromStringFunctions(std::string strFun)
return UEC_SERVICE_INVALID_VALUE;
}
std::vector<std::string> vModeStr;
std::vector<std::string_view> vModeStr;
size_t pos = 0;
while (pos < len) {
int32_t find_pos = strFun.find(",", pos);
if (find_pos < 0) {
size_t find_pos = strFun.find(",", pos);
if (find_pos == strFun.npos) {
vModeStr.push_back(strFun.substr(pos, len - pos));
break;
}
@ -85,27 +73,27 @@ std::string UsbFunctionManager::ToStringFunctions(int32_t intFun)
{
std::string stream;
if (intFun <= FUNCTION_NONE || intFun > FUNCTION_SETTABLE) {
stream = FUNCTION_NAME_NONE;
stream = std::string { FUNCTION_NAME_NONE };
return stream;
}
bool flag = false;
if ((intFun & FUNCTION_HDC) != 0) {
stream = FUNCTION_NAME_HDC;
stream = std::string { FUNCTION_NAME_HDC };
flag = true;
}
if ((intFun & FUNCTION_ACM) != 0) {
if (flag) {
stream = stream + ",";
}
stream = stream + FUNCTION_NAME_ACM;
stream = stream + std::string { FUNCTION_NAME_ACM };
flag = true;
}
if ((intFun & FUNCTION_ECM) != 0) {
if (flag) {
stream = stream + ",";
}
stream = stream + FUNCTION_NAME_ECM;
stream = stream + std::string { FUNCTION_NAME_ECM };
flag = true;
}
USB_HILOGI(MODULE_USB_SERVICE, "UsbFunctionManager::ToStringFunctions success");

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -36,12 +36,12 @@ bool UsbHostManager::DelDevice(uint8_t busNum, uint8_t devNum)
std::string name = std::to_string(busNum) + "-" + std::to_string(devNum);
MAP_STR_DEVICE::iterator iter = devices_.find(name);
if (iter == devices_.end()) {
USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d device:%{public}s bus:%{public}d dev:%{public}d not exist",
__func__, __LINE__, name.c_str(), busNum, devNum);
USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d name:%{public}s bus:%{public}hhu dev:%{public}hhu not exist",
__func__, __LINE__, name.c_str(), busNum, devNum);
return false;
}
USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d device:%{public}s bus:%{public}d dev:%{public}d erase ", __func__,
__LINE__, name.c_str(), busNum, devNum);
USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d device:%{public}s bus:%{public}hhu dev:%{public}hhu erase ",
__func__, __LINE__, name.c_str(), busNum, devNum);
UsbDevice *devOld = iter->second;
devices_.erase(iter);
if (devOld)
@ -50,7 +50,7 @@ bool UsbHostManager::DelDevice(uint8_t busNum, uint8_t devNum)
}
bool UsbHostManager::AddDevice(UsbDevice *dev)
{
if (dev == NULL) {
if (dev == nullptr) {
USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d device is NULL", __func__, __LINE__);
return false;
}
@ -60,14 +60,14 @@ bool UsbHostManager::AddDevice(UsbDevice *dev)
MAP_STR_DEVICE::iterator iter = devices_.find(name);
if (iter != devices_.end()) {
USB_HILOGF(MODULE_SERVICE,
"%{public}s:%{public}d device:%{public}s bus:%{public}d dev:%{public}d already exist", __func__,
"%{public}s:%{public}d device:%{public}s bus:%{public}hhu dev:%{public}hhu already exist", __func__,
__LINE__, name.c_str(), busNum, devNum);
UsbDevice *devOld = iter->second;
devices_.erase(iter);
if (devOld)
delete devOld;
}
USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d device:%{public}s bus:%{public}d dev:%{public}d insert",
USB_HILOGF(MODULE_SERVICE, "%{public}s:%{public}d device:%{public}s bus:%{public}hhu dev:%{public}hhu insert",
__func__, __LINE__, name.c_str(), busNum, devNum);
devices_.insert(std::pair<std::string, UsbDevice *>(name, dev));
return true;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -20,7 +20,7 @@ namespace OHOS {
namespace USB {
void UsbRightManager::Init() {}
int32_t UsbRightManager::HasRight(std::string deviceName, std::string bundleName)
int32_t UsbRightManager::HasRight(const std::string &deviceName, const std::string &bundleName)
{
auto itMap = rightMap.find(deviceName);
if (itMap == rightMap.end()) {
@ -38,7 +38,7 @@ int32_t UsbRightManager::HasRight(std::string deviceName, std::string bundleName
return UEC_OK;
}
int32_t UsbRightManager::RequestRight(std::string deviceName, std::string bundleName)
int32_t UsbRightManager::RequestRight(const std::string &deviceName, const std::string &bundleName)
{
if (HasRight(deviceName, bundleName) == 0) {
USB_HILOGE(MODULE_USB_SERVICE, "device has Right ");
@ -55,7 +55,7 @@ int32_t UsbRightManager::RequestRight(std::string deviceName, std::string bundle
return UEC_SERVICE_INVALID_VALUE;
}
bool UsbRightManager::AddDeviceRight(std::string deviceName, std::string bundleName)
bool UsbRightManager::AddDeviceRight(const std::string &deviceName, const std::string &bundleName)
{
auto itMap = rightMap.find(deviceName);
if (itMap != rightMap.end()) {
@ -75,7 +75,7 @@ bool UsbRightManager::AddDeviceRight(std::string deviceName, std::string bundleN
return true;
}
bool UsbRightManager::RemoveDeviceRight(std::string deviceName)
bool UsbRightManager::RemoveDeviceRight(const std::string &deviceName)
{
auto it = rightMap.find(deviceName);
if (it != rightMap.end()) {
@ -86,4 +86,4 @@ bool UsbRightManager::RemoveDeviceRight(std::string deviceName)
return false;
}
} // namespace USB
} // namespace OHOS
} // namespace OHOS

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -37,17 +37,17 @@
namespace OHOS {
namespace USB {
namespace {
const std::string USB_SERVICE_NAME = "UsbService";
constexpr const char *USB_SERVICE_NAME = "UsbService";
constexpr int32_t COMMEVENT_REGISTER_RETRY_TIMES = 10;
constexpr int32_t COMMEVENT_REGISTER_WAIT_DELAY_US = 20000;
const uint32_t CURSOR_INIT = 18;
const int32_t DESCRIPTOR_TYPE_STRING = 3;
const int32_t DESCRIPTOR_VALUE_START_OFFSET = 2;
const int32_t HALF = 2;
const int32_t BIT_SHIFT_4 = 4;
const int32_t BIT_HIGH_4 = 0xF0;
const int32_t BIT_LOW_4 = 0x0F;
const int32_t SERVICE_STARTUP_MAX_TIME = 30;
constexpr uint32_t CURSOR_INIT = 18;
constexpr int32_t DESCRIPTOR_TYPE_STRING = 3;
constexpr int32_t DESCRIPTOR_VALUE_START_OFFSET = 2;
constexpr int32_t HALF = 2;
constexpr int32_t BIT_SHIFT_4 = 4;
constexpr int32_t BIT_HIGH_4 = 0xF0;
constexpr int32_t BIT_LOW_4 = 0x0F;
constexpr int32_t SERVICE_STARTUP_MAX_TIME = 30;
} // namespace
auto pms = DelayedSpSingleton<UsbService>::GetInstance();
@ -111,6 +111,7 @@ bool UsbService::Init()
USB_HILOGE(MODULE_USB_SERVICE, "Init failed due to create handler error");
return false;
}
if (!Publish(pms)) {
USB_HILOGE(MODULE_USB_SERVICE, "OnStart register to system ability manager failed.");
return false;
@ -131,7 +132,12 @@ bool UsbService::Init()
bool UsbService::InitUsbd()
{
usbdSubscriber_ = new UsbServiceSubscriber();
usbdSubscriber_ = new(std::nothrow) UsbServiceSubscriber();
if (usbdSubscriber_ == nullptr) {
USB_HILOGE(MODULE_USB_SERVICE, " %{public}s:%{public}d Init failed\n", __func__, __LINE__);
return false;
}
ErrCode ret = UsbdClient::GetInstance().BindUsbdSubscriber(usbdSubscriber_);
USB_HILOGE(MODULE_USB_SERVICE, " entry InitUsbd ret: %{public}d", ret);
return SUCCEEDED(ret);
@ -165,6 +171,7 @@ bool UsbService::IsCommonEventServiceAbilityExist()
}
return true;
}
int32_t UsbService::OpenDevice(uint8_t busNum, uint8_t devAddr)
{
std::string name = std::to_string(busNum) + "-" + std::to_string(devAddr);
@ -182,6 +189,7 @@ int32_t UsbService::OpenDevice(uint8_t busNum, uint8_t devAddr)
}
return ret;
}
int32_t UsbService::HasRight(std::string deviceName)
{
USB_HILOGI(MODULE_USB_SERVICE, "calling usbRightManager HasRight");
@ -236,7 +244,7 @@ int32_t UsbService::SetCurrentFunctions(int32_t funcs)
return UsbdClient::GetInstance().SetCurrentFunctions(funcs);
}
int32_t UsbService::UsbFunctionsFromString(std::string funcs)
int32_t UsbService::UsbFunctionsFromString(std::string_view funcs)
{
USB_HILOGI(MODULE_USB_SERVICE, "calling UsbFunctionsFromString");
return UsbFunctionManager::FromStringFunctions(funcs);
@ -271,11 +279,13 @@ int32_t UsbService::ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t inte
const UsbDev dev = {busNum, devAddr};
return UsbdClient::GetInstance().ClaimInterface(dev, interface, force);
}
int32_t UsbService::ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interface)
{
const UsbDev dev = {busNum, devAddr};
return UsbdClient::GetInstance().ReleaseInterface(dev, interface);
}
int32_t UsbService::BulkTransferRead(const UsbDev &devInfo, const UsbPipe &pipe, std::vector<uint8_t> &bufferData,
int32_t timeOut)
{
@ -286,6 +296,7 @@ int32_t UsbService::BulkTransferRead(const UsbDev &devInfo, const UsbPipe &pipe,
}
return ret;
}
int32_t UsbService::BulkTransferWrite(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &bufferData,
int32_t timeOut)
{
@ -296,6 +307,7 @@ int32_t UsbService::BulkTransferWrite(const UsbDev &dev, const UsbPipe &pipe, co
}
return ret;
}
int32_t UsbService::ControlTransfer(const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData)
{
int32_t ret = UsbdClient::GetInstance().ControlTransfer(dev, ctrl, bufferData);
@ -304,21 +316,25 @@ int32_t UsbService::ControlTransfer(const UsbDev &dev, const UsbCtrlTransfer &ct
}
return ret;
}
int32_t UsbService::SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex)
{
const UsbDev dev = {busNum, devAddr};
return UsbdClient::GetInstance().SetConfig(dev, configIndex);
}
int32_t UsbService::GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex)
{
const UsbDev dev = {busNum, devAddr};
return UsbdClient::GetInstance().GetConfig(dev, configIndex);
}
int32_t UsbService::SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex)
{
const UsbDev dev = {busNum, devAddr};
return UsbdClient::GetInstance().SetInterface(dev, interfaceid, altIndex);
}
int32_t UsbService::GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vector<uint8_t> &bufferData)
{
const UsbDev dev = {busNum, devAddr};
@ -328,6 +344,7 @@ int32_t UsbService::GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vecto
}
return ret;
}
int32_t UsbService::GetFileDescriptor(uint8_t busNum, uint8_t devAddr, int32_t &fd)
{
const UsbDev dev = {busNum, devAddr};
@ -347,6 +364,7 @@ int32_t UsbService::RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const s
}
return ret;
}
int32_t UsbService::RequestWait(const UsbDev &dev, int32_t timeOut, std::vector<uint8_t> &clientData,
std::vector<uint8_t> &bufferData)
{
@ -374,34 +392,40 @@ static std::string GetDevStringValFromIdx(uint8_t busNum, uint8_t devAddr, uint8
{
const UsbDev dev = {busNum, devAddr};
std::vector<uint8_t> strV;
std::string string = " ";
std::string strDesc = " ";
if (idx == 0) {
return string;
return strDesc;
}
int32_t ret = UsbdClient::GetInstance().GetStringDescriptor(dev, idx, strV);
if (ret != UEC_OK) {
USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:%{public}d get string[%{public}d] failed ret:%{public}d", __func__,
USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:%{public}d get string[%{public}hhu] failed ret:%{public}d", __func__,
__LINE__, idx, ret);
return string;
return strDesc;
}
uint32_t length = strV.size();
if ((length < DESCRIPTOR_VALUE_START_OFFSET) || (strV[1] != DESCRIPTOR_TYPE_STRING)) {
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d type or length error, len:%{public}d", __func__, __LINE__,
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d type or length error, len:%{public}u", __func__, __LINE__,
length);
return string;
return strDesc;
}
uint16_t *tbuf = new uint16_t[length + 1]();
uint16_t *tbuf = new(std::nothrow) uint16_t[length + 1]();
if (tbuf == nullptr) {
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d new failed\n", __func__, __LINE__);
return strDesc;
}
for (uint32_t i = 0; i < length - DESCRIPTOR_VALUE_START_OFFSET; ++i) {
tbuf[i] = strV[i + DESCRIPTOR_VALUE_START_OFFSET];
}
std::wstring wstr((wchar_t *)(tbuf), (length - DESCRIPTOR_VALUE_START_OFFSET) / HALF);
string = std::string(wstr.begin(), wstr.end());
strDesc = std::string(wstr.begin(), wstr.end());
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d getString idx:%{public}d String:%{public}s length:%{public}d",
__func__, __LINE__, idx, string.c_str(), length);
__func__, __LINE__, idx, strDesc.c_str(), length);
delete[] tbuf;
return string;
return strDesc;
}
static std::string BcdToString(const std::vector<uint8_t> &bcd)
@ -443,8 +467,8 @@ static int32_t FillDevStrings(UsbDevice &dev)
std::vector<UsbInterface> interfaces = it->GetInterfaces();
for (auto itIF = interfaces.begin(); itIF != interfaces.end(); ++itIF) {
itIF->SetName(GetDevStringValFromIdx(busNum, devAddr, itIF->GetiInterface()));
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d interface:%{public}d %{public}s", __func__, __LINE__,
itIF->GetiInterface(), itIF->GetName().c_str());
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d interface:%{public}hhu %{public}s", __func__,
__LINE__, itIF->GetiInterface(), itIF->GetName().c_str());
}
it->SetInterfaces(interfaces);
}
@ -546,8 +570,18 @@ int32_t UsbService::GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice &de
bool UsbService::AddDevice(uint8_t busNum, uint8_t devAddr)
{
UsbDevice *devInfo = new UsbDevice();
memset_s(devInfo, sizeof(UsbDevice), 0, sizeof(UsbDevice));
UsbDevice *devInfo = new(std::nothrow) UsbDevice();
if (devInfo == nullptr) {
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d new failed", __func__, __LINE__);
return false;
}
errno_t retSafe = memset_s(devInfo, sizeof(UsbDevice), 0, sizeof(UsbDevice));
if (retSafe != EOK) {
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d memset_s failed", __func__, __LINE__);
return false;
}
int32_t ret = GetDeviceInfo(busNum, devAddr, *devInfo);
USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d GetDeviceInfo ret=%{public}d", __func__, __LINE__, ret);
if (ret == UEC_OK) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -100,8 +100,8 @@ int32_t UsbServiceSubscriber::DeviceEvent(const UsbInfo &info)
}
struct timeval end;
gettimeofday(&end, NULL);
long tackTime = (end.tv_sec - start.tv_sec) * MSEC_TIME + (end.tv_usec - start.tv_usec) / MSEC_TIME;
USB_HILOGD(MODULE_USB_SERVICE, "end call subscriber usb device tached event, takes : %{public}ld ms", tackTime);
int64_t tackTime = (end.tv_sec - start.tv_sec) * MSEC_TIME + (end.tv_usec - start.tv_usec) / MSEC_TIME;
USB_HILOGD(MODULE_USB_SERVICE, "end call subscriber usb device tached event, takes : %{public}lld ms", tackTime);
if ((ACT_UPDEVICE == status) || (ACT_DOWNDEVICE == status)) {
return ret;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -31,45 +31,44 @@ public:
~UsbServerProxy() = default;
DISALLOW_COPY_AND_MOVE(UsbServerProxy);
virtual int32_t GetDevices(std::vector<UsbDevice> &deviceList) override;
virtual int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) override;
virtual int32_t HasRight(std::string deviceName) override;
virtual int32_t RequestRight(std::string deviceName) override;
virtual int32_t RemoveRight(std::string deviceName) override;
virtual int32_t GetCurrentFunctions(int32_t &funcs) override;
virtual int32_t SetCurrentFunctions(int32_t funcs) override;
virtual int32_t UsbFunctionsFromString(std::string funcs) override;
virtual std::string UsbFunctionsToString(int32_t funcs) override;
virtual int32_t GetPorts(std::vector<UsbPort> &ports) override;
virtual int32_t GetSupportedModes(int32_t portId, int32_t &supportedModes) override;
virtual int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) override;
int32_t GetDevices(std::vector<UsbDevice> &deviceList) override;
int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) override;
int32_t HasRight(std::string deviceName) override;
int32_t RequestRight(std::string deviceName) override;
int32_t RemoveRight(std::string deviceName) override;
int32_t GetCurrentFunctions(int32_t &funcs) override;
int32_t SetCurrentFunctions(int32_t funcs) override;
int32_t UsbFunctionsFromString(std::string_view funcs) override;
std::string UsbFunctionsToString(int32_t funcs) override;
int32_t GetPorts(std::vector<UsbPort> &ports) override;
int32_t GetSupportedModes(int32_t portId, int32_t &supportedModes) override;
int32_t SetPortRole(int32_t portId, int32_t powerRole, int32_t dataRole) override;
virtual int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t force) override;
virtual int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override;
virtual int32_t BulkTransferRead(const UsbDev &dev, const UsbPipe &pipe, std::vector<uint8_t> &bufferData,
int32_t ClaimInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t force) override;
int32_t ReleaseInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid) override;
int32_t BulkTransferRead(const UsbDev &dev, const UsbPipe &pipe, std::vector<uint8_t> &bufferData,
int32_t timeOut) override;
virtual int32_t BulkTransferWrite(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &bufferData,
int32_t BulkTransferWrite(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &bufferData,
int32_t timeOut) override;
virtual int32_t ControlTransfer(const UsbDev &dev, const UsbCtrlTransfer &ctrl,
std::vector<uint8_t> &bufferData) override;
virtual int32_t SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) override;
virtual int32_t GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex) override;
virtual int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) override;
virtual int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vector<uint8_t> &bufferData) override;
virtual int32_t GetFileDescriptor(uint8_t busNum, uint8_t devAddr, int32_t &fd) override;
virtual int32_t RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData,
int32_t ControlTransfer(const UsbDev &dev, const UsbCtrlTransfer &ctrl, std::vector<uint8_t> &bufferData) override;
int32_t SetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t configIndex) override;
int32_t GetActiveConfig(uint8_t busNum, uint8_t devAddr, uint8_t &configIndex) override;
int32_t SetInterface(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t altIndex) override;
int32_t GetRawDescriptor(uint8_t busNum, uint8_t devAddr, std::vector<uint8_t> &bufferData) override;
int32_t GetFileDescriptor(uint8_t busNum, uint8_t devAddr, int32_t &fd) override;
int32_t RequestQueue(const UsbDev &dev, const UsbPipe &pipe, const std::vector<uint8_t> &clientData,
const std::vector<uint8_t> &bufferData) override;
virtual int32_t RequestWait(const UsbDev &dev, int32_t timeOut, std::vector<uint8_t> &clientData,
int32_t RequestWait(const UsbDev &dev, int32_t timeOut, std::vector<uint8_t> &clientData,
std::vector<uint8_t> &bufferData) override;
virtual int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId) override;
virtual int32_t Close(uint8_t busNum, uint8_t devAddr) override;
int32_t RequestCancel(uint8_t busNum, uint8_t devAddr, uint8_t interfaceid, uint8_t endpointId) override;
int32_t Close(uint8_t busNum, uint8_t devAddr) override;
virtual int32_t RegBulkCallback(const UsbDev &dev, const UsbPipe &pipe, const sptr<IRemoteObject> &cb) override;
virtual int32_t UnRegBulkCallback(const UsbDev &dev, const UsbPipe &pipe) override;
virtual int32_t BulkRead(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem) override;
virtual int32_t BulkWrite(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem) override;
virtual int32_t BulkCancel(const UsbDev &dev, const UsbPipe &pipe) override;
int32_t RegBulkCallback(const UsbDev &dev, const UsbPipe &pipe, const sptr<IRemoteObject> &cb) override;
int32_t UnRegBulkCallback(const UsbDev &dev, const UsbPipe &pipe) override;
int32_t BulkRead(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem) override;
int32_t BulkWrite(const UsbDev &dev, const UsbPipe &pipe, sptr<Ashmem> &ashmem) override;
int32_t BulkCancel(const UsbDev &dev, const UsbPipe &pipe) override;
private:
static inline BrokerDelegator<UsbServerProxy> delegator_;

View File

@ -1,6 +1,5 @@
/*
* Copyright 2021 Huawei Device Co., Ltd.
*
* Copyright 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -17,3 +16,4 @@
interface OHOS.Usb.IUsbServer {
/* the function about UsbService */
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -41,15 +41,14 @@ int32_t UsbServerProxy::SetBufferMessage(MessageParcel &data, const std::vector<
}
if (!data.WriteUint32(length)) {
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed length:%{public}d", __func__, __LINE__, length);
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed length:%{public}u", __func__, __LINE__, length);
return UEC_SERVICE_WRITE_PARCEL_ERROR;
}
if ((ptr) && (length > 0) && !data.WriteBuffer((const void *)ptr, length)) {
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed length:%{public}d", __func__, __LINE__, length);
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed length:%{public}u", __func__, __LINE__, length);
return UEC_SERVICE_WRITE_PARCEL_ERROR;
} else {
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d success length:%{public}d ptr:%{public}p", __func__, __LINE__,
length, ptr);
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d success length:%{public}u", __func__, __LINE__, length);
}
return UEC_OK;
}
@ -63,13 +62,13 @@ int32_t UsbServerProxy::GetBufferMessage(MessageParcel &data, std::vector<uint8_
return UEC_SERVICE_READ_PARCEL_ERROR;
}
if (dataSize == 0) {
USB_HILOGI(MODULE_USBD, "%{public}s:%{public}d size:%{public}d", __func__, __LINE__, dataSize);
USB_HILOGI(MODULE_USBD, "%{public}s:%{public}d size:%{public}u", __func__, __LINE__, dataSize);
return UEC_OK;
}
const uint8_t *readData = data.ReadUnpadBuffer(dataSize);
if (readData == nullptr) {
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed size:%{public}d", __func__, __LINE__, dataSize);
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed size:%{public}u", __func__, __LINE__, dataSize);
return UEC_SERVICE_READ_PARCEL_ERROR;
}
std::vector<uint8_t> tdata(readData, readData + dataSize);
@ -107,7 +106,6 @@ int32_t UsbServerProxy::GetDeviceListMessageParcel(MessageParcel &data, std::vec
{
int32_t count;
data.ReadInt32(count);
USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s device size : %{public}d", __func__, count);
for (int32_t i = 0; i < count; ++i) {
UsbDevice devInfo;
@ -389,7 +387,7 @@ int32_t UsbServerProxy::SetCurrentFunctions(int32_t funcs)
return ret;
}
int32_t UsbServerProxy::UsbFunctionsFromString(std::string funcs)
int32_t UsbServerProxy::UsbFunctionsFromString(std::string_view funcs)
{
sptr<IRemoteObject> remote = Remote();
RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE);
@ -401,7 +399,7 @@ int32_t UsbServerProxy::UsbFunctionsFromString(std::string funcs)
USB_HILOGE(MODULE_USB_SERVICE, "UsbServerProxy::%{public}s write descriptor failed!", __func__);
return UEC_INTERFACE_WRITE_PARCEL_ERROR;
}
WRITE_PARCEL_WITH_RET(data, String, funcs, UEC_INTERFACE_WRITE_PARCEL_ERROR);
WRITE_PARCEL_WITH_RET(data, String, std::string { funcs }, UEC_INTERFACE_WRITE_PARCEL_ERROR);
int32_t ret =
remote->SendRequest(static_cast<int32_t>(IUsbSrv::USB_FUN_USB_FUNCTIONS_FROM_STRING), data, reply, option);
if (ret != UEC_OK) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -45,11 +45,11 @@ int32_t UsbServerStub::SetBufferMessage(MessageParcel &data, const std::vector<u
}
if (!data.WriteUint32(length)) {
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed length:%{public}d", __func__, __LINE__, length);
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed length:%{public}u", __func__, __LINE__, length);
return UEC_SERVICE_WRITE_PARCEL_ERROR;
}
if ((ptr) && (length > 0) && !data.WriteBuffer((const void *)ptr, length)) {
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed length:%{public}d", __func__, __LINE__, length);
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed length:%{public}u", __func__, __LINE__, length);
return UEC_SERVICE_WRITE_PARCEL_ERROR;
}
return UEC_OK;
@ -64,13 +64,13 @@ int32_t UsbServerStub::GetBufferMessage(MessageParcel &data, std::vector<uint8_t
return UEC_SERVICE_READ_PARCEL_ERROR;
}
if (dataSize == 0) {
USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d size:%{public}d", __func__, __LINE__, dataSize);
USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d size:%{public}u", __func__, __LINE__, dataSize);
return UEC_OK;
}
const uint8_t *readData = data.ReadUnpadBuffer(dataSize);
if (readData == nullptr) {
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed size:%{public}d", __func__, __LINE__, dataSize);
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d failed size:%{public}u", __func__, __LINE__, dataSize);
return UEC_SERVICE_READ_PARCEL_ERROR;
}
std::vector<uint8_t> tdata(readData, readData + dataSize);
@ -188,7 +188,7 @@ bool UsbServerStub::StubHost(uint32_t code, int32_t &result, MessageParcel &data
int32_t UsbServerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
USB_HILOGD(MODULE_USB_SERVICE, "UsbServerStub::OnRemoteRequest, cmd = %{public}d, flags = %{public}d", code,
USB_HILOGD(MODULE_USB_SERVICE, "UsbServerStub::OnRemoteRequest, cmd = %{public}u, flags = %{public}d", code,
option.GetFlags());
std::u16string descriptor = UsbServerStub::GetDescriptor();
std::u16string remoteDescriptor = data.ReadInterfaceToken();
@ -437,7 +437,7 @@ int32_t UsbServerStub::DoControlTransfer(MessageParcel &data, MessageParcel &rep
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d get error ret:%{public}d", __func__, __LINE__, ret);
return ret;
}
// 写数据
bool bWrite = ((requestType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_OUT);
const UsbDev tmpDev = {busNum, devAddr};
const UsbCtrlTransfer tctrl = {requestType, request, value, index, timeOut};
@ -446,7 +446,7 @@ int32_t UsbServerStub::DoControlTransfer(MessageParcel &data, MessageParcel &rep
USB_HILOGE(MODULE_USBD, "%{public}s:%{public}d ControlTransfer error ret:%{public}d", __func__, __LINE__, ret);
return ret;
}
// 读数据
if (!bWrite) {
ret = SetBufferMessage(reply, bufferData);
if (UEC_OK != ret) {

View File

@ -46,12 +46,12 @@ export default {
return arr.length == 0;
},
// ArrayBuffer转为字符串参数为ArrayBuffer对象
// Convert ArrayBuffer to String buff is ArrayBuffer
ab2str(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
},
// 字符串转为ArrayBuffer对象参数为字符串
// Convert String to ArrayBuffer
str2ab(str) {
var arr = [];
for (var i = 0, j = str.length; i < j; ++i) {
@ -61,7 +61,6 @@ export default {
return tmpUint8Array;
},
// 简单阻塞的粗暴实现
sleep(delay) {
for (var t = Date.now(); Date.now() - t <= delay;);
}

2
test/native/service_unittest/BUILD.gn Executable file → Normal file
View File

@ -1,4 +1,4 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -25,4 +25,5 @@ public:
void SetUp();
void TearDown();
};
#endif
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -25,4 +25,5 @@ public:
void SetUp();
void TearDown();
};
#endif
#endif

View File

@ -39,16 +39,23 @@ const uint32_t MEM_DATA = 1024 * 1024;
int32_t InitAshmemOne(sptr<Ashmem> &asmptr, int32_t asmSize, uint8_t rflg)
{
int32_t ret = 0;
asmptr = Ashmem::CreateAshmem("ttashmem000", asmSize);
if (asmptr == nullptr) {
USB_HILOGE(MODULE_USB_SERVICE, "InitAshmemOne CreateAshmem failed\n");
return UEC_SERVICE_NO_MEMORY;
}
asmptr->MapReadAndWriteAshmem();
if (rflg == 0) {
uint8_t tdata[ASHMEM_MAX_SIZE];
int32_t offset = 0;
int32_t tlen = 0;
if (memset_s(tdata, sizeof(tdata), 'Y', ASHMEM_MAX_SIZE) != EOK) {
return -1;
int32_t retSafe = memset_s(tdata, sizeof(tdata), 'Y', ASHMEM_MAX_SIZE);
if (retSafe != EOK) {
USB_HILOGE(MODULE_USB_SERVICE, "InitAshmemOne memset_s failed\n");
return UEC_SERVICE_NO_MEMORY;
}
while (offset < asmSize) {
tlen = (asmSize - offset) < ASHMEM_MAX_SIZE ? (asmSize - offset) : ASHMEM_MAX_SIZE;
@ -57,7 +64,7 @@ int32_t InitAshmemOne(sptr<Ashmem> &asmptr, int32_t asmSize, uint8_t rflg)
}
}
return ret;
return 0;
}
void UsbBulkcallbackTest::SetUpTestCase(void)

0
test/native/service_unittest/src/usb_callback_test.cpp Executable file → Normal file
View File

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -749,4 +749,4 @@ HWTEST_F(UsbCoreTest, SetPortRole008, TestSize.Level1)
USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::status=%{public}d", ret);
ASSERT_TRUE(ret == 0);
USB_HILOGI(MODULE_USB_SERVICE, "Case End : SetPortRole008 : SetPortRole");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -1510,4 +1510,5 @@ HWTEST_F(UsbRequestTest, PipeRequestWait006, TestSize.Level1)
USB_HILOGI(MODULE_USB_SERVICE, "UsbRequestTest::PipeRequestWait006 %{public}d close=%{public}d", __LINE__, close);
EXPECT_TRUE(close);
USB_HILOGI(MODULE_USB_SERVICE, "Case End : PipeRequestWait006 : Initialize");
}
}

2
test/tool/BUILD.gn Executable file → Normal file
View File

@ -1,4 +1,4 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -116,13 +116,14 @@ int32_t main(int32_t argc, char *argv[])
}
if ((!strcmp(argv[1], "-f"))) {
int32_t mode = atoi(argv[2]);
int32_t mode = stoi(argv[2]);
FunctionSwitch(g_usbClient, mode);
} else if (!strcmp(argv[1], "-p")) {
int32_t mode = atoi(argv[2]);
int32_t mode = stoi(argv[2]);
PortSwitch(g_usbClient, mode);
} else {
printf("param incorrect: please input -h for help\n");
}
return 0;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Copyright (c) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@ -20,6 +20,7 @@
#include <mutex>
#include <refbase.h>
#include "nocopyable.h"
#include "hilog_wrapper.h"
namespace OHOS {
namespace USB {
@ -47,10 +48,15 @@ template <typename T> std::mutex DelayedSpSingleton<T>::mutex_;
template <typename T> sptr<T> DelayedSpSingleton<T>::GetInstance()
{
if (!instance_) {
std::lock_guard<std::mutex> lock(mutex_);
if (instance_ != nullptr) {
return instance_;
}
std::lock_guard<std::mutex> lock(mutex_);
if (instance_ == nullptr) {
instance_ = new(std::nothrow) T();
if (instance_ == nullptr) {
instance_ = new T();
USB_HILOGI(MODULE_USB_SERVICE, " %{public}s:%{public}d get instance failed\n", __func__, __LINE__);
}
}