From 8c1bc389e8d49af4a7dcbbc49a4515dd80a0ccf8 Mon Sep 17 00:00:00 2001 From: wu-chengwen Date: Thu, 31 Mar 2022 10:51:54 +0800 Subject: [PATCH] fix:usb inner review issue clear Signed-off-by: wu-chengwen --- OAT.xml | 104 +++++++ README.md | 4 +- README_zh.md | 4 +- .../usb-manager-architecture.png | Bin .../usb-manager-architecture_zh.png | Bin interfaces/innerkits/BUILD.gn | 2 - .../innerkits/native/include/iusb_srv.h | 2 +- .../innerkits/native/include/usb_config.h | 72 ++--- .../innerkits/native/include/usb_device.h | 280 ++++++++++-------- .../native/include/usb_device_pipe.h | 4 +- .../innerkits/native/include/usb_endpoint.h | 60 ++-- .../innerkits/native/include/usb_interface.h | 116 ++++---- .../innerkits/native/include/usb_request.h | 26 +- .../innerkits/native/include/usb_srv_client.h | 3 +- .../innerkits/native/src/usb_device_pipe.cpp | 10 +- .../innerkits/native/src/usb_request.cpp | 4 +- .../innerkits/native/src/usb_srv_client.cpp | 10 +- .../native/src/usbd_bulk_callback.cpp | 32 +- .../kits/js/napi/include/usb_async_context.h | 8 +- interfaces/kits/js/napi/src/napi_util.cpp | 12 +- interfaces/kits/js/napi/src/usb_info.cpp | 178 ++++++----- services/native/include/usb_right_manager.h | 2 +- services/native/include/usb_service.h | 6 +- services/native/src/usb_descriptor_parser.cpp | 1 + services/native/src/usb_host_manager.cpp | 1 + services/native/src/usb_right_manager.cpp | 12 +- services/native/src/usb_service.cpp | 53 ++-- .../native/src/usb_service_subscriber.cpp | 12 +- services/usb_service.cfg | 4 +- services/zidl/include/usb_server_proxy.h | 2 +- services/zidl/src/usb_srv_proxy.cpp | 25 +- services/zidl/src/usb_srv_stub.cpp | 6 +- .../src/usb_bulkcallback_test.cpp | 1 + .../service_unittest/src/usb_core_test.cpp | 107 +++---- .../src/usb_device_pipe_test.cpp | 2 +- test/tool/usb_function_port_test.cpp | 34 ++- 36 files changed, 686 insertions(+), 513 deletions(-) create mode 100644 OAT.xml rename usb.png => figures/usb-manager-architecture.png (100%) rename usb服务子系统.png => figures/usb-manager-architecture_zh.png (100%) diff --git a/OAT.xml b/OAT.xml new file mode 100644 index 0000000..a861e77 --- /dev/null +++ b/OAT.xml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/README.md b/README.md index 568861a..3f6ee6e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ The following figure shows the USB service architecture. **Figure 1** USB service architecture -![](usb.png "USB_service_architecture") +![](figures/usb-manager-architecture.png "USB_service_architecture") The architecture logically consists of three layers: 1. USB API: provides USB APIs that implement various basic functions, for example, query of the USB device list, USB device plug notification, USB host and device mode switching, bulk transfer, control transfer, right management, and function switching in device mode. @@ -52,7 +52,7 @@ base/usb/usb_manager | int32_t PipeRequestWait(USBDevicePipe &pip, int64_t timeout, UsbRequest &req); | Waits for the operation result of the isochronous transfer request in RequestQueue.| | int32_t RequestAbort(UsbRequest &request); | Cancels the data transfer requests to be processed.| | int32_t RequestFree(UsbRequest &request); | Requests for releasing data.| -| int32_t HasRight(std::string deviceName); | Checks whether the application has permission to access the USB device.| +| bool HasRight(std::string deviceName); | Checks whether the application has permission to access the USB device.| | int32_t RequestRight(std::string deviceName); | Requests for permission to access the USB device.| #### Device APIs diff --git a/README_zh.md b/README_zh.md index df7df76..602d1bd 100644 --- a/README_zh.md +++ b/README_zh.md @@ -10,7 +10,7 @@ USB服务框架如下图所示: **图 1** USB服务框架架图 -![](usb服务子系统.png "USB服务框架架图") +![](figures/usb-manager-architecture_zh.png "USB服务框架架图") 架构及功能介绍: 1. USB API:提供USB的基础API,主要包含查询USB设备的列表、设备插拔通知、USB HOST/DEVICE 功能切换、批量数据传输、控制命令传输、USB设备打开的权限控制及USB device模式下的function功能切换等。 @@ -52,7 +52,7 @@ base/usb/usb_manager | int32_t PipeRequestWait(USBDevicePipe &pip, int64_t timeout, UsbRequest &req); | 等待RequestQueue异步请求的操作结果 | | int32_t RequestAbort(UsbRequest &request); | 取消待处理的数据请求 | | int32_t RequestFree(UsbRequest &request); | 释放数据请求 | -| int32_t HasRight(std::string deviceName); | 判断设备是否有权限 | +| bool HasRight(std::string deviceName); | 判断设备是否有权限 | | int32_t RequestRight(std::string deviceName); | 给指定设备申请权限 | #### Device 功能接口 diff --git a/usb.png b/figures/usb-manager-architecture.png similarity index 100% rename from usb.png rename to figures/usb-manager-architecture.png diff --git a/usb服务子系统.png b/figures/usb-manager-architecture_zh.png similarity index 100% rename from usb服务子系统.png rename to figures/usb-manager-architecture_zh.png diff --git a/interfaces/innerkits/BUILD.gn b/interfaces/innerkits/BUILD.gn index a960c79..b447042 100644 --- a/interfaces/innerkits/BUILD.gn +++ b/interfaces/innerkits/BUILD.gn @@ -41,8 +41,6 @@ ohos_shared_library("usbsrv_client") { deps = [] external_deps = [ - "device_driver_framework:libhdf_ipc_adapter", - "device_driver_framework:libhdf_utils", "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/interfaces/innerkits/native/include/iusb_srv.h b/interfaces/innerkits/native/include/iusb_srv.h index 492531b..0143e51 100644 --- a/interfaces/innerkits/native/include/iusb_srv.h +++ b/interfaces/innerkits/native/include/iusb_srv.h @@ -61,7 +61,7 @@ public: }; virtual int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) = 0; - virtual int32_t HasRight(std::string deviceName) = 0; + virtual bool HasRight(std::string deviceName) = 0; virtual int32_t RequestRight(std::string deviceName) = 0; virtual int32_t RemoveRight(std::string deviceName) = 0; virtual int32_t GetDevices(std::vector &deviceList) = 0; diff --git a/interfaces/innerkits/native/include/usb_config.h b/interfaces/innerkits/native/include/usb_config.h index b3adf55..094139b 100644 --- a/interfaces/innerkits/native/include/usb_config.h +++ b/interfaces/innerkits/native/include/usb_config.h @@ -31,97 +31,97 @@ public: USBConfig(uint32_t id, uint32_t attributes, std::string name, uint32_t maxPower, std::vector interfaces) { - this->id = id; - this->attributes = attributes; - this->maxPower = maxPower; - this->name = name; - this->interfaces = interfaces; + this->id_ = id; + this->attributes_ = attributes; + this->maxPower_ = maxPower; + this->name_ = name; + this->interfaces_ = interfaces; } USBConfig() {} ~USBConfig() {} const int32_t &GetId() const { - return id; + return id_; } const int32_t &GetAttributes() const { - return attributes; + return attributes_; } bool GetInterface(uint32_t index, UsbInterface &interface) const { - if (index >= interfaces.size()) { + if (index >= interfaces_.size()) { return false; } - interface = interfaces[index]; + interface = interfaces_[index]; return true; } - int32_t GetInterfaceCount() const + uint32_t GetInterfaceCount() const { - return interfaces.size(); + return interfaces_.size(); } int32_t GetMaxPower() const { // 2 represent maxPower units - return maxPower * 2; + return maxPower_ * 2; } const std::string &GetName() const { - return name; + return name_; } bool IsRemoteWakeup() const { - return (attributes & USB_CFG_REMOTE_WAKEUP) != 0; + return (attributes_ & USB_CFG_REMOTE_WAKEUP) != 0; } bool IsSelfPowered() const { - return (attributes & USB_CFG_SELF_POWERED) != 0; + return (attributes_ & USB_CFG_SELF_POWERED) != 0; } void SetInterfaces(const std::vector &interfaces) { - this->interfaces = interfaces; + this->interfaces_ = interfaces; } std::vector &GetInterfaces() { - return interfaces; + return interfaces_; } void SetId(int32_t id) { - this->id = id; + this->id_ = id; } void SetAttribute(int32_t attributes) { - this->attributes = attributes; + this->attributes_ = attributes; } void SetMaxPower(int32_t maxPower) { - this->maxPower = maxPower; + this->maxPower_ = maxPower; } std::string ToString() const { std::ostringstream ss; - ss << "name=" << name << "," - << "id=" << id << "," - << "iConfiguration=" << (int32_t)iConfiguration << "," - << "attributes=" << attributes << "," - << "maxPower=" << maxPower << "; "; + ss << "name=" << name_ << "," + << "id=" << id_ << "," + << "iConfiguration=" << (int32_t)iConfiguration_ << "," + << "attributes=" << attributes_ << "," + << "maxPower=" << maxPower_ << "; "; std::string str = "USBConfig[" + ss.str() + "]"; ss.str(""); - for (size_t i = 0; i < interfaces.size(); ++i) { - const UsbInterface &interface = interfaces[i]; + for (size_t i = 0; i < interfaces_.size(); ++i) { + const UsbInterface &interface = interfaces_[i]; str += interface.ToString(); } return str; @@ -129,26 +129,26 @@ public: void SetName(const std::string &name) { - this->name = name; + this->name_ = name; } void SetiConfiguration(uint8_t idx) { - this->iConfiguration = idx; + this->iConfiguration_ = idx; } uint8_t GetiConfiguration() { - return this->iConfiguration; + return this->iConfiguration_; } private: - int32_t id = INVALID_USB_INT_VALUE; - int32_t attributes = INVALID_USB_INT_VALUE; - std::vector interfaces; - int32_t maxPower = INVALID_USB_INT_VALUE; - std::string name; - uint8_t iConfiguration = UINT8_MAX; + int32_t id_ = INVALID_USB_INT_VALUE; + int32_t attributes_ = INVALID_USB_INT_VALUE; + std::vector interfaces_; + int32_t maxPower_ = INVALID_USB_INT_VALUE; + std::string name_; + uint8_t iConfiguration_ = UINT8_MAX; }; } // namespace USB } // namespace OHOS diff --git a/interfaces/innerkits/native/include/usb_device.h b/interfaces/innerkits/native/include/usb_device.h index 8706489..8a61f6e 100644 --- a/interfaces/innerkits/native/include/usb_device.h +++ b/interfaces/innerkits/native/include/usb_device.h @@ -28,260 +28,290 @@ namespace OHOS { namespace USB { class UsbDevice { public: - UsbDevice(std::string mName, - std::string mManufacturerName, - std::string mProductName, - std::string mVersion, - uint8_t devAddr, - uint8_t busNum, - int mVendorId, - int mProductId, - int mClass, - int mSubclass, - int mProtocol, - std::vector configs) + UsbDevice(std::string name, std::string manufacturerName, std::string productName, std::string version, + uint8_t devAddr, uint8_t busNum, int32_t vendorId, int32_t productId, int32_t klass, int32_t subClass, + int32_t protocol, std::vector configs) { - this->mName = mName; - this->mManufacturerName = mManufacturerName; - this->mProductName = mProductName; - this->mVersion = mVersion; - this->devAddr = devAddr; - this->busNum = busNum; - this->mVendorId = mVendorId; - this->mProductId = mProductId; - this->mClass = mClass; - this->mSubclass = mSubclass; - this->mProtocol = mProtocol; - this->configs = configs; + this->name_ = name; + this->manufacturerName_ = manufacturerName; + this->productName_ = productName; + this->version_ = version; + this->devAddr_ = devAddr; + this->busNum_ = busNum; + this->vendorId_ = vendorId; + this->productId_ = productId; + this->klass_ = klass; + this->subClass_ = subClass; + this->protocol_ = protocol; + this->configs_ = configs; } + UsbDevice() {} ~UsbDevice() {} + const std::string &GetName() const { - return mName; + return name_; } + const std::string &GetManufacturerName() const { - return mManufacturerName; + return manufacturerName_; } + const std::string &GetProductName() const { - return mProductName; + return productName_; } + const std::string &GetVersion() const { - return mVersion; + return version_; } + int32_t GetVendorId() const { - return mVendorId; + return vendorId_; } + int32_t GetProductId() const { - return mProductId; + return productId_; } + int32_t GetClass() const { - return mClass; + return klass_; } + int32_t GetSubclass() const { - return mSubclass; + return subClass_; } + int32_t GetProtocol() const { - return mProtocol; + return protocol_; } + int32_t GetConfigCount() const { - return configs.size(); + return configs_.size(); } + int32_t GetConfig(uint32_t index, USBConfig &config) const { - if (index >= configs.size()) { + if (index >= configs_.size()) { return ERR_INVALID_VALUE; } - config = configs[index]; + config = configs_[index]; return ERR_OK; } + void SetConfigs(const std::vector &configs) { - this->configs = configs; + this->configs_ = configs; } uint8_t GetDevAddr() const { - return devAddr; - } - uint8_t GetBusNum() const - { - return busNum; - } - bool GetDescConfigCount() - { - return descConfigCount; + return devAddr_; } - void SetDevAddr(uint8_t Num) + uint8_t GetBusNum() const { - devAddr = Num; + return busNum_; } - void SetBusNum(uint8_t Num) + + bool GetDescConfigCount() { - busNum = Num; + return descConfigCount_; } - void SetName(const std::string &Name) + + void SetDevAddr(uint8_t addr) { - mName = Name; + devAddr_ = addr; } - void SetManufacturerName(const std::string &Name) + + void SetBusNum(uint8_t num) { - mManufacturerName = Name; + busNum_ = num; } - void SetProductName(const std::string &Name) + + void SetName(const std::string &name) { - mProductName = Name; + name_ = name; } - void SetVersion(const std::string &Name) + + void SetManufacturerName(const std::string &manufacturerName) { - mVersion = Name; + manufacturerName_ = manufacturerName; } - void SetVendorId(int Id) + + void SetProductName(const std::string &productName) { - mVendorId = Id; + productName_ = productName; } - void SetProductId(int Id) + + void SetVersion(const std::string &version) { - mProductId = Id; + version_ = version; } - void SetClass(int Id) + + void SetVendorId(int32_t vendorId) { - mClass = Id; + vendorId_ = vendorId; } - void SetSubclass(int Id) + + void SetProductId(int32_t productId) { - mSubclass = Id; + productId_ = productId; } - void SetProtocol(int Id) + + void SetClass(int32_t deviceClass) { - mProtocol = Id; + klass_ = deviceClass; } + + void SetSubclass(int32_t subClass) + { + subClass_ = subClass; + } + + void SetProtocol(int32_t protocol) + { + protocol_ = protocol; + } + void SetDescConfigCount(uint8_t count) { - descConfigCount = count; + descConfigCount_ = count; } std::vector &GetConfigs() { - return configs; + return configs_; } std::string ToString() const { std::ostringstream ss; - ss << "mName=" << mName << "," - << "mManufacturerName=" << mManufacturerName << "," - << "mProductName=" << mProductName << "," - << "mVersion=" << mVersion << "," - << "mSerial=" << mSerial << "," - << "busNum=" << (int32_t)busNum << "," - << "devAddr=" << (int32_t)devAddr << "," - << "mVendorId=" << mVendorId << "," - << "mProductId=" << mProductId << "," - << "mClass=" << mClass << "," - << "mSubclass=" << mSubclass << "," - << "mProtocol=" << mProtocol << ""; + ss << "name_=" << name_ << "," + << "manufacturerName_=" << manufacturerName_ << "," + << "productName_=" << productName_ << "," + << "version_=" << version_ << "," + << "serial_=" << serial_ << "," + << "busNum_=" << (int32_t)busNum_ << "," + << "devAddr_=" << (int32_t)devAddr_ << "," + << "vendorId_=" << vendorId_ << "," + << "productId_=" << productId_ << "," + << "klass_=" << klass_ << "," + << "subClass_=" << subClass_ << "," + << "protocol_=" << protocol_ << ""; std::string str = "UsbDevice[" + ss.str() + "]; "; ss.str(""); std::string strConfigs; - for (size_t i = 0; i < configs.size(); ++i) { - const USBConfig &config = configs[i]; + for (size_t i = 0; i < configs_.size(); ++i) { + const USBConfig &config = configs_[i]; strConfigs += config.ToString(); } str += strConfigs; return str; } - void SetiManufacturer(uint8_t idx) + void SetiManufacturer(uint8_t manufacturer) { - this->iManufacturer = idx; + this->iManufacturer_ = manufacturer; } + uint8_t GetiManufacturer() { - return this->iManufacturer; + return this->iManufacturer_; } - void SetiProduct(uint8_t idx) + + void SetiProduct(uint8_t product) { - this->iProduct = idx; + this->iProduct_ = product; } + uint8_t GetiProduct() { - return this->iProduct; + return this->iProduct_; } - void SetiSerialNumber(uint8_t idx) + + void SetiSerialNumber(uint8_t sn) { - this->iSerialNumber = idx; + this->iSerialNumber_ = sn; } + uint8_t GetiSerialNumber() { - return this->iSerialNumber; + return this->iSerialNumber_; } - void SetmSerial(std::string Name) + + void SetmSerial(std::string serial) { - this->mSerial = Name; + this->serial_ = serial; } + const std::string GetmSerial() const { - return this->mSerial; + return this->serial_; } - void SetbMaxPacketSize0(uint8_t val) + + void SetbMaxPacketSize0(uint8_t maxSize) { - this->bMaxPacketSize0 = val; + this->bMaxPacketSize0_ = maxSize; } + uint8_t GetbMaxPacketSize0() { - return this->bMaxPacketSize0; + return this->bMaxPacketSize0_; } - void SetbcdUSB(uint16_t val) + + void SetbcdUSB(uint16_t bcdUSB) { - this->bcdUSB = val; + this->bcdUSB_ = bcdUSB; } + uint16_t GetbcdUSB() { - return this->bcdUSB; + return this->bcdUSB_; } - void SetbcdDevice(uint16_t val) + + void SetbcdDevice(uint16_t bcdDevice) { - this->bcdDevice = val; + this->bcdDevice_ = bcdDevice; } + uint16_t GetbcdDevice() { - return this->bcdDevice; + return this->bcdDevice_; } private: - std::string mName; - std::string mManufacturerName; - std::string mProductName; - std::string mVersion; - std::string mSerial; - uint8_t devAddr; - uint8_t busNum; - uint8_t descConfigCount = UINT8_MAX; + std::string name_; + std::string manufacturerName_; + std::string productName_; + std::string version_; + std::string serial_; + uint8_t devAddr_; + uint8_t busNum_; + uint8_t descConfigCount_ = UINT8_MAX; - 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; + 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; - int mClass; - int mSubclass; - int mProtocol; - std::vector configs; + int32_t vendorId_; + int32_t productId_; + int32_t klass_; + int32_t subClass_; + int32_t protocol_; + std::vector configs_; }; } // namespace USB } // namespace OHOS diff --git a/interfaces/innerkits/native/include/usb_device_pipe.h b/interfaces/innerkits/native/include/usb_device_pipe.h index d0c1e93..0519fd6 100644 --- a/interfaces/innerkits/native/include/usb_device_pipe.h +++ b/interfaces/innerkits/native/include/usb_device_pipe.h @@ -40,8 +40,8 @@ public: uint8_t GetDevAddr() const; private: - uint8_t busNum; - uint8_t devAddr; + uint8_t busNum_; + uint8_t devAddr_; }; } // namespace USB } // namespace OHOS diff --git a/interfaces/innerkits/native/include/usb_endpoint.h b/interfaces/innerkits/native/include/usb_endpoint.h index 2c52d1d..0a07963 100644 --- a/interfaces/innerkits/native/include/usb_endpoint.h +++ b/interfaces/innerkits/native/include/usb_endpoint.h @@ -27,103 +27,109 @@ class USBEndpoint { public: USBEndpoint(uint32_t address, uint32_t attributes, uint32_t interval, uint32_t maxPacketSize) { - this->address = address; - this->attributes = attributes; - this->interval = interval; - this->maxPacketSize = maxPacketSize; + this->address_ = address; + this->attributes_ = attributes; + this->interval_ = interval; + this->maxPacketSize_ = maxPacketSize; } USBEndpoint() {} ~USBEndpoint() {} uint8_t GetNumber() const { - return address & USB_ENDPOINT_NUMBER_MASK; + return address_ & USB_ENDPOINT_NUMBER_MASK; } const int32_t &GetAddress() const { - return address; + return address_; } int32_t GetDirection() const { - return address & USB_ENDPOINT_DIR_MASK; + return address_ & USB_ENDPOINT_DIR_MASK; } const int32_t &GetAttributes() const { - return attributes; + return attributes_; } int32_t GetEndpointNumber() const { - return address & USB_ENDPOINT_NUMBER_MASK; + return address_ & USB_ENDPOINT_NUMBER_MASK; } const int32_t &GetInterval() const { - return interval; + return interval_; } const int32_t &GetMaxPacketSize() const { - return maxPacketSize; + return maxPacketSize_; } int32_t GetType() const { - return (attributes & USB_ENDPOINT_XFERTYPE_MASK); + return (attributes_ & USB_ENDPOINT_XFERTYPE_MASK); } std::string ToString() const { std::string ret = "USBEndpoint:[Address:"; - ret.append(std::to_string(address)) + ret.append(std::to_string(address_)) .append(", Direction:") .append(std::to_string(GetDirection())) .append(", Attributes:") - .append(std::to_string(attributes)) + .append(std::to_string(attributes_)) .append(", EndpointNumber:") .append(std::to_string(GetEndpointNumber())) .append(", Interval:") - .append(std::to_string(interval)) + .append(std::to_string(interval_)) .append(", MaxPacketSize:") - .append(std::to_string(maxPacketSize)) + .append(std::to_string(maxPacketSize_)) .append(", Type:") .append(std::to_string(GetType())) .append("]"); return ret; } + void SetAddr(int32_t val) { - address = val; + address_ = val; } + void SetAttr(int32_t val) { - attributes = val; + attributes_ = val; } + void SetInterval(int32_t val) { - interval = val; + interval_ = val; } + void SetMaxPacketSize(int32_t val) { - maxPacketSize = val; + maxPacketSize_ = val; } + void SetInterfaceId(uint8_t interfaceId) { - this->interfaceId = interfaceId; + this->interfaceId_ = interfaceId; } + int8_t GetInterfaceId() const { - return interfaceId; + return interfaceId_; } private: - int32_t address = INVALID_USB_INT_VALUE; - 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_MAX; + int32_t address_ = INVALID_USB_INT_VALUE; + 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_MAX; }; } // namespace USB } // namespace OHOS diff --git a/interfaces/innerkits/native/include/usb_interface.h b/interfaces/innerkits/native/include/usb_interface.h index d002d02..cc445f5 100644 --- a/interfaces/innerkits/native/include/usb_interface.h +++ b/interfaces/innerkits/native/include/usb_interface.h @@ -20,110 +20,114 @@ #include #include #include +#include #include "usb_endpoint.h" namespace OHOS { namespace USB { class UsbInterface { public: - UsbInterface(int32_t mId, - int32_t mProtocol, - int32_t mClass, - int32_t mSubClass, - int32_t mAlternateSetting, - std::string mName, - std::vector mEndpoints) + UsbInterface(int32_t id, + int32_t protocol, + int32_t interfaceClass, + int32_t subClass, + int32_t alternateSetting, + std::string name, + std::vector endpoints) { - this->mId = mId; - this->mProtocol = mProtocol; - this->mClass = mClass; - this->mSubClass = mSubClass; - this->mAlternateSetting = mAlternateSetting; - this->mEndpoints = mEndpoints; + this->id_ = id; + this->protocol_ = protocol; + this->klass_ = interfaceClass; + this->subClass_ = subClass; + this->alternateSetting_ = alternateSetting; + this->endpoints_ = endpoints; } UsbInterface() {} const std::string &GetName() const { - return mName; + return name_; } int32_t GetId() const { - return mId; + return id_; } int32_t GetClass() const { - return mClass; + return klass_; } int32_t GetSubClass() const { - return mSubClass; + return subClass_; } int32_t GetAlternateSetting() const { - return mAlternateSetting; + return alternateSetting_; } int32_t GetProtocol() const { - return mProtocol; + return protocol_; } int32_t GetEndpointCount() const { - return mEndpoints.size(); + return endpoints_.size(); } - void GetEndpoint(uint32_t index, USBEndpoint &ep) const + std::optional GetEndpoint(uint32_t index) const { - if (index < mEndpoints.size()) { - ep = mEndpoints[index]; + if (index >= endpoints_.size()) { + USB_HILOGE(MODULE_USB_INNERKIT, "invalid index=%{public}u !", index); + return std::nullopt; } + + return endpoints_[index]; } std::vector &GetEndpoints() { - return mEndpoints; + return endpoints_; } void SetEndpoints(const std::vector &eps) { - mEndpoints = eps; + endpoints_ = eps; } - void SetId(int Val) + void SetId(int32_t id) { - mId = Val; + id_ = id; } - void SetProtocol(int Val) + void SetProtocol(int32_t protocol) { - mProtocol = Val; + protocol_ = protocol; } - void SetClass(int Val) + void SetClass(int32_t klass) { - mClass = Val; + klass_ = klass; } - void SetSubClass(int Val) + void SetSubClass(int32_t subClass) { - mSubClass = Val; + subClass_ = subClass; } - void SetAlternateSetting(int Val) + void SetAlternateSetting(int32_t alternateSetting) { - mAlternateSetting = Val; + alternateSetting_ = alternateSetting; } - void SetName(const std::string &Name) + void SetName(const std::string &name) { - mName = Name; + name_ = name; } ~UsbInterface() {} @@ -131,17 +135,17 @@ public: std::string ToString() const { std::ostringstream ss; - ss << "id=" << mId << "," - << "mName=" << mName << "," - << "iInterface=" << (int32_t)iInterface << "," - << "mClass=" << mClass << "," - << "mSubClass=" << mSubClass << "," - << "mProtocol=" << mProtocol << "," - << "mAlternateSetting=" << mAlternateSetting << ""; + ss << "id=" << id_ << "," + << "name_=" << name_ << "," + << "iInterface_=" << (int32_t)iInterface_ << "," + << "klass_=" << klass_ << "," + << "subClass_=" << subClass_ << "," + << "protocol_=" << protocol_ << "," + << "alternateSetting_=" << alternateSetting_ << ""; std::string str = "UsbInterface[" + ss.str() + "]; "; ss.str(""); - for (size_t i = 0; i < mEndpoints.size(); ++i) { - const USBEndpoint &endpoint = mEndpoints[i]; + for (size_t i = 0; i < endpoints_.size(); ++i) { + const USBEndpoint &endpoint = endpoints_[i]; str += endpoint.ToString(); } return str; @@ -149,23 +153,23 @@ public: void SetiInterface(uint8_t idx) { - this->iInterface = idx; + this->iInterface_ = idx; } uint8_t GetiInterface() { - return this->iInterface; + return this->iInterface_; } private: - int mId; - int mProtocol; - int mClass; - int mSubClass; - int mAlternateSetting; - std::string mName; - std::vector mEndpoints; - uint8_t iInterface = UINT8_MAX; + int32_t id_ = INT32_MAX; + int32_t protocol_ = INT32_MAX; + int32_t klass_ = INT32_MAX; + int32_t subClass_ = INT32_MAX; + int32_t alternateSetting_ = INT32_MAX; + std::string name_; + std::vector endpoints_; + uint8_t iInterface_ = UINT8_MAX; }; } // namespace USB } // namespace OHOS diff --git a/interfaces/innerkits/native/include/usb_request.h b/interfaces/innerkits/native/include/usb_request.h index 929f0f6..c8e1a0c 100644 --- a/interfaces/innerkits/native/include/usb_request.h +++ b/interfaces/innerkits/native/include/usb_request.h @@ -32,42 +32,42 @@ public: int32_t Abort(); void SetPipe(const USBDevicePipe &pipe) { - this->pipe = pipe; + this->pipe_ = pipe; } void SetEndpoint(const USBEndpoint &endpoint) { - this->endpoint = endpoint; + this->endpoint_ = endpoint; } void SetClientData(std::vector bufferData) { - clientData = bufferData; + clientData_ = bufferData; } void SetReqData(std::vector bufferData) { - reqData = bufferData; + reqData_ = bufferData; } std::vector &GetClientData() { - return clientData; + return clientData_; } std::vector &GetReqData() { - return reqData; + return reqData_; } const USBDevicePipe &GetPipe() const { - return pipe; + return pipe_; } const USBEndpoint &GetEndpoint() const { - return endpoint; + return endpoint_; } -public: - USBDevicePipe pipe; - USBEndpoint endpoint; - std::vector clientData; - std::vector reqData; +private: + USBDevicePipe pipe_; + USBEndpoint endpoint_; + std::vector clientData_; + std::vector reqData_; }; } // namespace USB } // namespace OHOS diff --git a/interfaces/innerkits/native/include/usb_srv_client.h b/interfaces/innerkits/native/include/usb_srv_client.h index 8307082..8550947 100644 --- a/interfaces/innerkits/native/include/usb_srv_client.h +++ b/interfaces/innerkits/native/include/usb_srv_client.h @@ -42,7 +42,7 @@ public: DISALLOW_COPY_AND_MOVE(UsbSrvClient); int32_t OpenDevice(const UsbDevice &device, USBDevicePipe &pip); - int32_t HasRight(std::string deviceName); + bool HasRight(std::string deviceName); int32_t RequestRight(std::string deviceName); int32_t RemoveRight(std::string deviceName); int32_t GetDevices(std::vector &deviceList); @@ -74,6 +74,7 @@ public: { return SEVVERSION; } + int32_t RegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint, const sptr &cb); int32_t UnRegBulkCallback(USBDevicePipe &pip, const USBEndpoint &endpoint); int32_t BulkRead(USBDevicePipe &pip, const USBEndpoint &endpoint, sptr &ashmem); diff --git a/interfaces/innerkits/native/src/usb_device_pipe.cpp b/interfaces/innerkits/native/src/usb_device_pipe.cpp index ead72f5..409897c 100644 --- a/interfaces/innerkits/native/src/usb_device_pipe.cpp +++ b/interfaces/innerkits/native/src/usb_device_pipe.cpp @@ -25,7 +25,7 @@ namespace OHOS { namespace USB { USBDevicePipe::USBDevicePipe() {} USBDevicePipe::USBDevicePipe(uint8_t busNum, uint8_t devAddr) - : busNum(busNum), devAddr(devAddr) + : busNum_(busNum), devAddr_(devAddr) { } @@ -71,22 +71,22 @@ int32_t USBDevicePipe::SetInterface(const UsbInterface &interface) void USBDevicePipe::SetBusNum(uint8_t busNum) { - this->busNum = busNum; + this->busNum_ = busNum; } void USBDevicePipe::SetDevAddr(uint8_t devAddr) { - this->devAddr = devAddr; + this->devAddr_ = devAddr; } uint8_t USBDevicePipe::GetBusNum() const { - return busNum; + return busNum_; } uint8_t USBDevicePipe::GetDevAddr() const { - return devAddr; + return devAddr_; } } // namespace USB } // namespace OHOS diff --git a/interfaces/innerkits/native/src/usb_request.cpp b/interfaces/innerkits/native/src/usb_request.cpp index 33adfc2..9a50011 100644 --- a/interfaces/innerkits/native/src/usb_request.cpp +++ b/interfaces/innerkits/native/src/usb_request.cpp @@ -21,8 +21,8 @@ namespace OHOS { namespace USB { int32_t UsbRequest::Initialize(const USBDevicePipe &pipe, const USBEndpoint &endpoint) { - this->pipe = pipe; - this->endpoint = endpoint; + this->pipe_ = pipe; + this->endpoint_ = endpoint; int32_t ret = UsbSrvClient::GetInstance().RequestInitialize(*this); if (ERR_OK != ret) { USB_HILOGE(MODULE_USB_INNERKIT, "UsbRequest::%{public}s:%{public}d failed with ret = %{public}d.", __func__, diff --git a/interfaces/innerkits/native/src/usb_srv_client.cpp b/interfaces/innerkits/native/src/usb_srv_client.cpp index ffc8223..8f6383f 100644 --- a/interfaces/innerkits/native/src/usb_srv_client.cpp +++ b/interfaces/innerkits/native/src/usb_srv_client.cpp @@ -91,15 +91,11 @@ int32_t UsbSrvClient::OpenDevice(const UsbDevice &device, USBDevicePipe &pipe) return UEC_OK; } -int32_t UsbSrvClient::HasRight(std::string deviceName) +bool UsbSrvClient::HasRight(std::string deviceName) { USB_HILOGI(MODULE_USB_INNERKIT, " Calling HasRight Start!"); - RETURN_IF_WITH_RET(Connect() != UEC_OK, UEC_INTERFACE_NO_INIT); - int32_t ret = proxy_->HasRight(deviceName); - if (ret != UEC_OK) { - USB_HILOGE(MODULE_USB_INNERKIT, " Calling HasRight False!"); - } - return ret; + RETURN_IF_WITH_RET(Connect() != UEC_OK, false); + return proxy_->HasRight(deviceName); } int32_t UsbSrvClient::RequestRight(std::string deviceName) diff --git a/interfaces/innerkits/native/src/usbd_bulk_callback.cpp b/interfaces/innerkits/native/src/usbd_bulk_callback.cpp index 482664a..e0528f5 100644 --- a/interfaces/innerkits/native/src/usbd_bulk_callback.cpp +++ b/interfaces/innerkits/native/src/usbd_bulk_callback.cpp @@ -13,58 +13,56 @@ * limitations under the License. */ - #include "usbd_bulk_callback.h" -#include -#include -#include #include "usb_errors.h" - -#define HDF_LOG_TAG USBD_BULK_CALLBACK +#include "hilog_wrapper.h" namespace OHOS::USB { int32_t UsbdBulkCallBack::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { - HDF_LOGI("%{public}s:%{public}d OnRemoteRequest entry! code:%{public}d", __func__, __LINE__, code); switch (code) { case CMD_USBD_BULK_CALLBACK_WRITE: { if (data.ReadInterfaceToken() != GetObjectDescriptor()) { - HDF_LOGE("%{public}s: checkout interface descriptor failed!", __func__); + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s: checkout interface descriptor failed!", __func__); return UEC_SERVICE_WRITE_PARCEL_ERROR; } + int32_t status; int32_t actLength; if (!data.ReadInt32(status)) { - HDF_LOGE("%{public}s:%{public}d get status error", __func__, __LINE__); + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s:%{public}d get status error", __func__, __LINE__); return UEC_SERVICE_WRITE_PARCEL_ERROR; } if (!data.ReadInt32(actLength)) { - HDF_LOGE("%{public}s:%{public}d get actLength error", __func__, __LINE__); + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s:%{public}d get actLength error", __func__, __LINE__); return UEC_SERVICE_WRITE_PARCEL_ERROR; } - HDF_LOGI("%{public}s:%{public}d status:%{public}d actLength:%{public}d", __func__, __LINE__, status, - actLength); + + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s:%{public}d status:%{public}d actLength:%{public}d", __func__, + __LINE__, status, actLength); OnBulkWriteCallback(status, actLength); break; } case CMD_USBD_BULK_CALLBACK_READ: { if (data.ReadInterfaceToken() != GetObjectDescriptor()) { - HDF_LOGE("%{public}s: checkout interface descriptor failed!", __func__); + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s: checkout interface descriptor failed!", __func__); return UEC_SERVICE_WRITE_PARCEL_ERROR; } + int32_t status; int32_t actLength; if (!data.ReadInt32(status)) { - HDF_LOGE("%{public}s:%{public}d get status error", __func__, __LINE__); + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s:%{public}d get status error", __func__, __LINE__); return UEC_SERVICE_WRITE_PARCEL_ERROR; } if (!data.ReadInt32(actLength)) { - HDF_LOGE("%{public}s:%{public}d get actLength error", __func__, __LINE__); + USB_HILOGE(MODULE_USB_INNERKIT, "%{public}s:%{public}d get actLength error", __func__, __LINE__); return UEC_SERVICE_WRITE_PARCEL_ERROR; } - HDF_LOGI("%{public}s:%{public}d status:%{public}d actLength:%{public}d", __func__, __LINE__, status, - actLength); + + USB_HILOGI(MODULE_USB_INNERKIT, "%{public}s:%{public}d status:%{public}d actLength:%{public}d", __func__, + __LINE__, status, actLength); OnBulkReadCallback(status, actLength); break; } diff --git a/interfaces/kits/js/napi/include/usb_async_context.h b/interfaces/kits/js/napi/include/usb_async_context.h index a8e5286..2fc19b1 100644 --- a/interfaces/kits/js/napi/include/usb_async_context.h +++ b/interfaces/kits/js/napi/include/usb_async_context.h @@ -90,22 +90,18 @@ struct USBControlTransferAsyncContext : USBAsyncContext { int32_t index; uint8_t *buffer; uint32_t bufferLength; + uint32_t dataSize; int32_t timeOut = 0; }; struct USBBulkTransferAsyncContext : USBAsyncContext { uint8_t *buffer; uint32_t bufferLength; + uint32_t dataSize; int32_t timeOut = 0; USBDevicePipe pipe; USBEndpoint endpoint; }; - -struct USBQueueAsyncContext : USBAsyncContext { - UsbRequest req; - uint8_t *buffer; - uint32_t bufferLength; -}; } // namespace USB } // namespace OHOS #endif // USB_ASYNC_CONTEXT_H diff --git a/interfaces/kits/js/napi/src/napi_util.cpp b/interfaces/kits/js/napi/src/napi_util.cpp index 6a9c797..dac2fab 100644 --- a/interfaces/kits/js/napi/src/napi_util.cpp +++ b/interfaces/kits/js/napi/src/napi_util.cpp @@ -32,10 +32,6 @@ void NapiUtil::JsValueToString(const napi_env &env, const napi_value &value, con } std::unique_ptr buf = std::make_unique(bufLen); - if (buf.get() == nullptr) { - USB_HILOGE(MODULE_JS_NAPI, "%{public}s nullptr js object to string malloc failed", __func__); - return; - } errno_t ret = memset_s(buf.get(), bufLen, 0, bufLen); NAPI_ASSERT_RETURN_VOID(env, ret == EOK, "JsValueToString memset_s failed."); @@ -48,8 +44,10 @@ void NapiUtil::JsValueToString(const napi_env &env, const napi_value &value, con void NapiUtil::JsObjectToString(const napi_env &env, const napi_value &object, std::string fieldStr, const int32_t bufLen, std::string &fieldRef) { - if (bufLen <= 0) + if (bufLen <= 0) { + USB_HILOGE(MODULE_JS_NAPI, "invalid bufLen=%{public}d", bufLen); return; + } bool hasProperty = false; napi_has_named_property(env, object, fieldStr.c_str(), &hasProperty); @@ -61,10 +59,6 @@ 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 buf = std::make_unique(bufLen); - if (buf == nullptr) { - USB_HILOGE(MODULE_JS_NAPI, "%{public}s nullptr js object to string malloc failed", __func__); - return; - } errno_t ret = memset_s(buf.get(), bufLen, 0, bufLen); NAPI_ASSERT_RETURN_VOID(env, ret == EOK, "JsObjectToString memset_s failed."); diff --git a/interfaces/kits/js/napi/src/usb_info.cpp b/interfaces/kits/js/napi/src/usb_info.cpp index 3e429e6..bcd25d0 100644 --- a/interfaces/kits/js/napi/src/usb_info.cpp +++ b/interfaces/kits/js/napi/src/usb_info.cpp @@ -100,14 +100,19 @@ static void CtoJSUsbInterface(const napi_env &env, napi_value &obj, const UsbInt NapiUtil::SetValueInt32(env, "subclass", usbInterface.GetSubClass(), obj); NapiUtil::SetValueInt32(env, "alternateSetting", usbInterface.GetAlternateSetting(), obj); NapiUtil::SetValueUtf8String(env, "name", usbInterface.GetName(), obj); + napi_value arr; napi_create_array(env, &arr); for (int32_t i = 0; i < usbInterface.GetEndpointCount(); ++i) { - USBEndpoint usbEndpoint; - usbInterface.GetEndpoint(i, usbEndpoint); - napi_value obj; - CtoJSUsbEndpoint(env, obj, usbEndpoint); - napi_set_element(env, arr, i, obj); + auto usbEndpoint = usbInterface.GetEndpoint(i); + if (!usbEndpoint.has_value()) { + USB_HILOGE(MODULE_JS_NAPI, "GetEndpoint failed i=%{public}d", i); + return; + } + + napi_value objTmp; + CtoJSUsbEndpoint(env, objTmp, usbEndpoint.value()); + napi_set_element(env, arr, i, objTmp); } napi_set_named_property(env, obj, "endpoints", arr); } @@ -123,12 +128,12 @@ static void CtoJSUsbConfig(const napi_env &env, napi_value &obj, const USBConfig NapiUtil::SetValueUtf8String(env, "name", usbConfig.GetName(), obj); napi_value arr; napi_create_array(env, &arr); - for (int32_t i = 0; i < usbConfig.GetInterfaceCount(); ++i) { + for (uint32_t i = 0; i < usbConfig.GetInterfaceCount(); ++i) { UsbInterface usbInterface; usbConfig.GetInterface(i, usbInterface); - napi_value obj; - CtoJSUsbInterface(env, obj, usbInterface); - napi_set_element(env, arr, i, obj); + napi_value objTmp; + CtoJSUsbInterface(env, objTmp, usbInterface); + napi_set_element(env, arr, i, objTmp); } napi_set_named_property(env, obj, "interfaces", arr); } @@ -153,9 +158,9 @@ static void CtoJSUsbDevice(const napi_env &env, napi_value &obj, const UsbDevice for (int32_t i = 0; i < usbDevice.GetConfigCount(); ++i) { USBConfig usbConfig; usbDevice.GetConfig(i, usbConfig); - napi_value obj; - CtoJSUsbConfig(env, obj, usbConfig); - napi_set_element(env, arr, i, obj); + napi_value objTmp; + CtoJSUsbConfig(env, objTmp, usbConfig); + napi_set_element(env, arr, i, objTmp); } napi_set_named_property(env, obj, "configs", arr); } @@ -182,26 +187,32 @@ static void ParseEndpointObj(const napi_env env, const napi_value endpointObj, U static bool ParseEndpointsObjs(const napi_env env, const napi_value interfaceObj, std::vector &eps) { - // Get the array. - bool result = false; - napi_status status = napi_is_array(env, interfaceObj, &result); - if (result == false || status != napi_ok) { - return false; - } - napi_value endpointsObjs; bool isGetObjSuccess = NapiUtil::JsObjectGetProperty(env, interfaceObj, "endpoints", endpointsObjs); if (!isGetObjSuccess) { + USB_HILOGE(MODULE_JS_NAPI, "%{public}s, get endpoints failed.", __func__); + return false; + } + + bool result = false; + napi_status status = napi_is_array(env, endpointsObjs, &result); + if (!result || status != napi_ok) { + USB_HILOGE(MODULE_JS_NAPI, "%{public}s, invalid endpoints.", __func__); return false; } uint32_t endpointCount = 0; status = napi_get_array_length(env, endpointsObjs, &endpointCount); + if (status != napi_ok) { + USB_HILOGE(MODULE_JS_NAPI, "get len failed."); + return false; + } for (uint32_t k = 0; k < endpointCount; ++k) { napi_value endpointObj; - napi_status status = napi_get_element(env, endpointsObjs, k, &endpointObj); + status = napi_get_element(env, endpointsObjs, k, &endpointObj); if (status != napi_ok) { + USB_HILOGE(MODULE_JS_NAPI, "%{public}s, get endpoints element failed k=%u.", __func__, k); return false; } USBEndpoint ep; @@ -266,35 +277,49 @@ static void ParseInterfaceObj(const napi_env env, const napi_value interfaceObj, std::string name; NapiUtil::JsObjectToString(env, interfaceObj, "name", DEFAULT_DESCRIPTION_SIZE, name); std::vector eps; - ParseEndpointsObjs(env, interfaceObj, eps); + + bool ret = ParseEndpointsObjs(env, interfaceObj, eps); + NAPI_ASSERT_RETURN_VOID(env, ret == true, "Parse endpointers error."); + interface = UsbInterface(id, protocol, clzz, subClass, alternateSetting, name, eps); } static bool ParseInterfacesObjs(const napi_env env, const napi_value configObj, std::vector &interfaces) { - bool result = false; - napi_status status = napi_is_array(env, configObj, &result); - if (result && status == napi_ok) { - napi_value interfacesObjs; - bool isGetObjSuccess = NapiUtil::JsObjectGetProperty(env, configObj, "interfaces", interfacesObjs); - if (!isGetObjSuccess) { - return false; - } - uint32_t interfaceCount = 0; - status = napi_get_array_length(env, interfacesObjs, &interfaceCount); - for (uint32_t i = 0; i < interfaceCount; ++i) { - napi_value interfaceObj; - napi_status status = napi_get_element(env, interfacesObjs, i, &interfaceObj); - if (status != napi_ok) { - return false; - } - UsbInterface interface; - ParseInterfaceObj(env, interfaceObj, interface); - interfaces.push_back(interface); - } + napi_value interfacesObjs; + bool isGetObjSuccess = NapiUtil::JsObjectGetProperty(env, configObj, "interfaces", interfacesObjs); + if (!isGetObjSuccess) { + USB_HILOGE(MODULE_JS_NAPI, "%{public}s, get interfaces failed.", __func__); + return false; } - return false; + bool result = false; + napi_status status = napi_is_array(env, interfacesObjs, &result); + if (!result || status != napi_ok) { + USB_HILOGE(MODULE_JS_NAPI, "%{public}s, invalid param.", __func__); + return false; + } + + uint32_t interfaceCount = 0; + status = napi_get_array_length(env, interfacesObjs, &interfaceCount); + if (status != napi_ok) { + USB_HILOGE(MODULE_JS_NAPI, "%{public}s, get len failed.", __func__); + return false; + } + + for (uint32_t i = 0; i < interfaceCount; ++i) { + napi_value interfaceObj; + status = napi_get_element(env, interfacesObjs, i, &interfaceObj); + if (status != napi_ok) { + USB_HILOGE(MODULE_JS_NAPI, "%{public}s, get element failed,i=%{public}u.", __func__, i); + return false; + } + UsbInterface interface; + ParseInterfaceObj(env, interfaceObj, interface); + interfaces.push_back(interface); + } + + return true; } static void ParseConfigObj(const napi_env env, const napi_value configObj, USBConfig &config) @@ -309,7 +334,9 @@ static void ParseConfigObj(const napi_env env, const napi_value configObj, USBCo NapiUtil::JsObjectToString(env, configObj, "name", DEFAULT_DESCRIPTION_SIZE, name); std::vector interfaces; - ParseInterfacesObjs(env, configObj, interfaces); + bool ret = ParseInterfacesObjs(env, configObj, interfaces); + NAPI_ASSERT_RETURN_VOID(env, ret == true, "Parse interfaces error."); + config = USBConfig(id, attributes, name, maxPower, interfaces); } @@ -376,7 +403,7 @@ static napi_value CoreGetDevices(napi_env env, napi_callback_info info) napi_value result; if (ret != UEC_OK) { napi_get_undefined(env, &result); - USB_HILOGE(MODULE_JS_NAPI, "end call %{public}s, get device faild ret : %{public}d", __func__, ret); + USB_HILOGE(MODULE_JS_NAPI, "end call %{public}s, get device failed ret : %{public}d", __func__, ret); return result; } @@ -435,11 +462,11 @@ static napi_value CoreHasRight(napi_env env, napi_callback_info info) std::string deviceName; NapiUtil::JsValueToString(env, args[INDEX_0], STR_DEFAULT_SIZE, deviceName); - int32_t result = g_usbClient.HasRight(deviceName); + bool result = g_usbClient.HasRight(deviceName); USB_HILOGD(MODULE_JS_NAPI, "client called result %{public}d", result); napi_value napiValue = nullptr; - napi_get_boolean(env, result == UEC_OK, &napiValue); + napi_get_boolean(env, result, &napiValue); return napiValue; } @@ -607,7 +634,7 @@ static napi_value CoreGetCurrentFunctions(napi_env env, napi_callback_info info) napi_value result; if (ret != UEC_OK) { napi_get_undefined(env, &result); - USB_HILOGE(MODULE_JS_NAPI, "end call %{public}s, get ports faild ret : %{public}d", __func__, ret); + USB_HILOGE(MODULE_JS_NAPI, "end call %{public}s, get ports failed ret : %{public}d", __func__, ret); return result; } napi_create_int32(env, cfuncs, &result); @@ -628,7 +655,7 @@ static napi_value CoreGetPorts(napi_env env, napi_callback_info info) napi_value result; if (ret != UEC_OK) { napi_get_undefined(env, &result); - USB_HILOGE(MODULE_JS_NAPI, "end call %{public}s, get ports faild ret : %{public}d", __func__, ret); + USB_HILOGE(MODULE_JS_NAPI, "end call %{public}s, get ports failed ret : %{public}d", __func__, ret); return result; } @@ -916,27 +943,32 @@ static napi_value PipeGetFileDescriptor(napi_env env, napi_callback_info info) static auto g_controlTransferExecute = [](napi_env env, void *data) { USBControlTransferAsyncContext *asyncContext = (USBControlTransferAsyncContext *)data; std::vector 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 { - if (bufferData.size() < asyncContext->bufferLength) { - asyncContext->bufferLength = bufferData.size(); + } + + const UsbCtrlTransfer tctrl = {asyncContext->reqType, asyncContext->request, asyncContext->value, + asyncContext->index, asyncContext->timeOut}; + int32_t ret; + do { + ret = asyncContext->pipe.ControlTransfer(tctrl, bufferData); + if (ret != UEC_OK) { + USB_HILOGE(MODULE_JS_NAPI, "ControlTransferExecute failed"); + break; } - ret = memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(), - asyncContext->bufferLength); - NAPI_ASSERT_RETURN_VOID(env, ret == EOK, "ControlTransferExecute memcpy_s failed."); - } + if ((asyncContext->reqType & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_DIR_IN) { + ret = memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(), bufferData.size()); + } + } while (0); if (ret == UEC_OK) { asyncContext->status = napi_ok; + asyncContext->dataSize = bufferData.size(); } else { asyncContext->status = napi_generic_failure; + asyncContext->dataSize = 0; } }; @@ -945,7 +977,7 @@ static auto g_controlTransferComplete = [](napi_env env, napi_status status, voi napi_value queryResult = nullptr; if (asyncContext->status == napi_ok) { - napi_create_int32(env, asyncContext->bufferLength, &queryResult); + napi_create_int32(env, asyncContext->dataSize, &queryResult); } else { USB_HILOGD(MODULE_JS_NAPI, "ControlTransfer failed"); napi_create_int32(env, -1, &queryResult); @@ -1044,25 +1076,31 @@ static napi_value PipeControlTransfer(napi_env env, napi_callback_info info) static auto g_bulkTransferExecute = [](napi_env env, void *data) { USBBulkTransferAsyncContext *asyncContext = reinterpret_cast(data); std::vector bufferData(asyncContext->buffer, asyncContext->buffer + asyncContext->bufferLength); - int32_t ret = asyncContext->pipe.BulkTransfer(asyncContext->endpoint, bufferData, asyncContext->timeOut); - if (asyncContext->endpoint.GetDirection() == USB_ENDPOINT_DIR_OUT) { delete [] asyncContext->buffer; asyncContext->buffer = nullptr; - } else { - if (bufferData.size() < asyncContext->bufferLength) { - asyncContext->bufferLength = bufferData.size(); + } + + int32_t ret; + do { + ret = asyncContext->pipe.BulkTransfer(asyncContext->endpoint, bufferData, asyncContext->timeOut); + if (ret != UEC_OK) { + USB_HILOGE(MODULE_JS_NAPI, "ControlTransferExecute failed"); + break; } - ret = memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(), asyncContext->bufferLength); - NAPI_ASSERT_RETURN_VOID(env, ret == EOK, "BulkTransferExecute memcpy_s failed."); - } + if (asyncContext->endpoint.GetDirection() == USB_ENDPOINT_DIR_IN) { + ret = memcpy_s(asyncContext->buffer, asyncContext->bufferLength, bufferData.data(), bufferData.size()); + } + } while (0); USB_HILOGD(MODULE_JS_NAPI, "call pipe result %{public}d", ret); if (ret == UEC_OK) { asyncContext->status = napi_ok; + asyncContext->dataSize = bufferData.size(); } else { asyncContext->status = napi_generic_failure; + asyncContext->dataSize = 0; } }; @@ -1070,9 +1108,9 @@ static auto g_bulkTransferComplete = [](napi_env env, napi_status status, void * USBBulkTransferAsyncContext *asyncContext = reinterpret_cast(data); napi_value queryResult = nullptr; if (asyncContext->status == napi_ok) { - napi_create_int32(env, asyncContext->bufferLength, &queryResult); + napi_create_int32(env, asyncContext->dataSize, &queryResult); } else { - USB_HILOGD(MODULE_JS_NAPI, "BulkTransfer failed"); + USB_HILOGE(MODULE_JS_NAPI, "BulkTransfer failed"); napi_create_int32(env, -1, &queryResult); } ProcessPromise(env, *asyncContext, queryResult); diff --git a/services/native/include/usb_right_manager.h b/services/native/include/usb_right_manager.h index 86e016b..e22c182 100644 --- a/services/native/include/usb_right_manager.h +++ b/services/native/include/usb_right_manager.h @@ -32,7 +32,7 @@ public: RightMap rightMap; void Init(); - int32_t HasRight(const std::string &deviceName, const std::string &bundleName); + bool 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); diff --git a/services/native/include/usb_service.h b/services/native/include/usb_service.h index 002bd6f..09cdffa 100644 --- a/services/native/include/usb_service.h +++ b/services/native/include/usb_service.h @@ -53,7 +53,7 @@ public: } int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) override; - int32_t HasRight(std::string deviceName) override; + bool HasRight(std::string deviceName) override; int32_t RequestRight(std::string deviceName) override; int32_t RemoveRight(std::string deviceName) override; int32_t GetDevices(std::vector &deviceList) override; @@ -87,8 +87,8 @@ public: bool DelDevice(uint8_t busNum, uint8_t devAddr); void UpdateUsbPort(int32_t portId, int32_t powerRole, int32_t dataRole, int32_t mode); int32_t GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice &dev); - int32_t GetDeviceInfoDescriptor(const UsbDev &uDev, std::vector &decriptor, UsbDevice &dev); - int32_t GetConfigDescriptor(UsbDevice &dev, std::vector &decriptor); + int32_t GetDeviceInfoDescriptor(const UsbDev &uDev, std::vector &descriptor, UsbDevice &dev); + int32_t GetConfigDescriptor(UsbDevice &dev, std::vector &descriptor); int32_t RegBulkCallback(const UsbDev &devInfo, const UsbPipe &pipe, const sptr &cb) override; int32_t UnRegBulkCallback(const UsbDev &devInfo, const UsbPipe &pipe) override; diff --git a/services/native/src/usb_descriptor_parser.cpp b/services/native/src/usb_descriptor_parser.cpp index 68b3ee0..6aa1abe 100644 --- a/services/native/src/usb_descriptor_parser.cpp +++ b/services/native/src/usb_descriptor_parser.cpp @@ -110,6 +110,7 @@ int32_t UsbDescriptorParser::ParseConfigDescriptor(const uint8_t *buffer, uint32 if (interface.GetEndpointCount() > 0) { interfaces.push_back(interface); } else { + // retry --i; } cursor += interfaceCursor; diff --git a/services/native/src/usb_host_manager.cpp b/services/native/src/usb_host_manager.cpp index e51adf9..f6d88f8 100644 --- a/services/native/src/usb_host_manager.cpp +++ b/services/native/src/usb_host_manager.cpp @@ -48,6 +48,7 @@ bool UsbHostManager::DelDevice(uint8_t busNum, uint8_t devNum) delete devOld; return true; } + bool UsbHostManager::AddDevice(UsbDevice *dev) { if (dev == nullptr) { diff --git a/services/native/src/usb_right_manager.cpp b/services/native/src/usb_right_manager.cpp index 9ac9e9c..b422ac3 100644 --- a/services/native/src/usb_right_manager.cpp +++ b/services/native/src/usb_right_manager.cpp @@ -20,34 +20,34 @@ namespace OHOS { namespace USB { void UsbRightManager::Init() {} -int32_t UsbRightManager::HasRight(const std::string &deviceName, const std::string &bundleName) +bool UsbRightManager::HasRight(const std::string &deviceName, const std::string &bundleName) { auto itMap = rightMap.find(deviceName); if (itMap == rightMap.end()) { USB_HILOGE(MODULE_USB_SERVICE, "hasRight deviceName false"); - return UEC_SERVICE_INVALID_VALUE; + return false; } else { BundleNameList bundleNameList = itMap->second; auto itVevtor = std::find(bundleNameList.begin(), bundleNameList.end(), bundleName); if (itVevtor == bundleNameList.end()) { USB_HILOGE(MODULE_USB_SERVICE, "hasRight bundleName false"); - return UEC_SERVICE_INVALID_VALUE; + return false; } } USB_HILOGI(MODULE_USB_SERVICE, "Request Right Success"); - return UEC_OK; + return true; } int32_t UsbRightManager::RequestRight(const std::string &deviceName, const std::string &bundleName) { - if (HasRight(deviceName, bundleName) == 0) { + if (HasRight(deviceName, bundleName)) { USB_HILOGE(MODULE_USB_SERVICE, "device has Right "); return UEC_OK; } if (!AddDeviceRight(deviceName, bundleName)) { return UEC_SERVICE_INVALID_VALUE; } - if (HasRight(deviceName, bundleName) == 0) { + if (HasRight(deviceName, bundleName)) { USB_HILOGI(MODULE_USB_SERVICE, "requestRight Success"); return UEC_OK; } diff --git a/services/native/src/usb_service.cpp b/services/native/src/usb_service.cpp index f72b0ca..4175af6 100644 --- a/services/native/src/usb_service.cpp +++ b/services/native/src/usb_service.cpp @@ -105,12 +105,8 @@ bool UsbService::Init() return false; } } - if (!handler_) { + if (handler_ == nullptr) { handler_ = std::make_shared(eventRunner_, pms); - if (handler_ == nullptr) { - 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."); @@ -175,22 +171,22 @@ bool UsbService::IsCommonEventServiceAbilityExist() int32_t UsbService::OpenDevice(uint8_t busNum, uint8_t devAddr) { std::string name = std::to_string(busNum) + "-" + std::to_string(devAddr); - int32_t ret = UsbService::HasRight(name); - if (UEC_OK == ret) { - const UsbDev dev = {busNum, devAddr}; - ret = UsbdClient::GetInstance().OpenDevice(dev); - if (UEC_OK != ret) { - USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:%{public}d OpenDevice failed ret:%{public}d", __func__, __LINE__, - ret); - } - } else { - USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:%{public}d No permission ret:%{public}d", __func__, __LINE__, ret); - ret = UEC_SERVICE_PERMISSION_DENIED; + if (!UsbService::HasRight(name)) { + USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:%{public}d No permission", __func__, __LINE__); + return UEC_SERVICE_PERMISSION_DENIED; } + + const UsbDev dev = {busNum, devAddr}; + int32_t ret = UsbdClient::GetInstance().OpenDevice(dev); + if (ret != UEC_OK) { + USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:%{public}d OpenDevice failed ret:%{public}d", __func__, __LINE__, + ret); + } + return ret; } -int32_t UsbService::HasRight(std::string deviceName) +bool UsbService::HasRight(std::string deviceName) { USB_HILOGI(MODULE_USB_SERVICE, "calling usbRightManager HasRight"); std::string bundleName; @@ -199,7 +195,7 @@ int32_t UsbService::HasRight(std::string deviceName) return usbRightManager_->HasRight(deviceName, bundleName); } USB_HILOGE(MODULE_USB_SERVICE, "HasRight GetBundleName false"); - return UEC_SERVICE_INNER_ERR; + return false; } int32_t UsbService::RequestRight(std::string deviceName) @@ -477,9 +473,9 @@ static int32_t FillDevStrings(UsbDevice &dev) return UEC_OK; } -int32_t UsbService::GetDeviceInfoDescriptor(const UsbDev &uDev, std::vector &decriptor, UsbDevice &dev) +int32_t UsbService::GetDeviceInfoDescriptor(const UsbDev &uDev, std::vector &descriptor, UsbDevice &dev) { - int32_t ret = UsbdClient::GetInstance().GetRawDescriptor(uDev, decriptor); + int32_t ret = UsbdClient::GetInstance().GetRawDescriptor(uDev, descriptor); if (ret != UEC_OK) { UsbdClient::GetInstance().CloseDevice(uDev); USB_HILOGE(MODULE_USB_SERVICE, @@ -488,8 +484,8 @@ int32_t UsbService::GetDeviceInfoDescriptor(const UsbDev &uDev, std::vector &decriptor) +int32_t UsbService::GetConfigDescriptor(UsbDevice &dev, std::vector &descriptor) { std::vector configs; - uint8_t *buffer = decriptor.data(); - uint32_t length = decriptor.size(); + uint8_t *buffer = descriptor.data(); + uint32_t length = descriptor.size(); uint32_t cursor = CURSOR_INIT; int32_t ret = UEC_OK; for (uint8_t i = 0; i < dev.GetDescConfigCount(); ++i) { @@ -542,7 +538,7 @@ int32_t UsbService::GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice &de USB_HILOGI(MODULE_USB_SERVICE, "%{public}s:%{public}d busNum:%{public}d devAddr:%{public}d", __func__, __LINE__, busNum, devAddr); const UsbDev uDev = {busNum, devAddr}; - std::vector decriptor; + std::vector descriptor; int32_t ret = UsbdClient::GetInstance().OpenDevice(uDev); if (ret != UEC_OK) { @@ -550,12 +546,12 @@ int32_t UsbService::GetDeviceInfo(uint8_t busNum, uint8_t devAddr, UsbDevice &de return ret; } - ret = GetDeviceInfoDescriptor(uDev, decriptor, dev); + ret = GetDeviceInfoDescriptor(uDev, descriptor, dev); if (ret != UEC_OK) { USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:%{public}d GetDeviceInfoDescriptor ret=%{public}d", __func__, __LINE__, ret); } - ret = GetConfigDescriptor(dev, decriptor); + ret = GetConfigDescriptor(dev, descriptor); if (ret != UEC_OK) { USB_HILOGE(MODULE_USB_SERVICE, "%{public}s:%{public}d GetConfigDescriptor ret=%{public}d", __func__, __LINE__, ret); @@ -591,6 +587,7 @@ bool UsbService::AddDevice(uint8_t busNum, uint8_t devAddr) delete devInfo; return false; } + bool UsbService::DelDevice(uint8_t busNum, uint8_t devAddr) { USB_HILOGI(MODULE_USBD, "%{public}s:%{public}d entry", __func__, __LINE__); diff --git a/services/native/src/usb_service_subscriber.cpp b/services/native/src/usb_service_subscriber.cpp index c55418c..8374e49 100644 --- a/services/native/src/usb_service_subscriber.cpp +++ b/services/native/src/usb_service_subscriber.cpp @@ -38,6 +38,11 @@ UsbServiceSubscriber::UsbServiceSubscriber() {} int32_t UsbServiceSubscriber::PortChangedEvent(int32_t portId, int32_t powerRole, int32_t dataRole, int32_t mode) { auto pms = DelayedSpSingleton::GetInstance(); + if (pms == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "failed to GetInstance"); + return UEC_SERVICE_GET_USB_SERVICE_FAILED; + } + Want want; want.SetAction(CommonEventSupport::COMMON_EVENT_USB_PORT_CHANGED); pms->UpdateUsbPort(portId, powerRole, dataRole, mode); @@ -111,8 +116,11 @@ int32_t UsbServiceSubscriber::DeviceEvent(const UsbInfo &info) USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d status:%{public}d bus:%{public}d dev:%{public}d", __func__, __LINE__, status, busNum, devAddr); auto pms = DelayedSpSingleton::GetInstance(); - USB_HILOGW(MODULE_USBD, "%{public}s:%{public}d status:%{public}d pms:%{public}s", __func__, __LINE__, status, - pms == nullptr ? "NULL" : "OK"); + if (pms == nullptr) { + USB_HILOGE(MODULE_USB_SERVICE, "failed to GetInstance"); + return UEC_SERVICE_GET_USB_SERVICE_FAILED; + } + if (status == ACT_DEVUP) { USB_HILOGE(MODULE_USB_SERVICE, "usb attached"); pms->AddDevice(busNum, devAddr); diff --git a/services/usb_service.cfg b/services/usb_service.cfg index fb07c47..5b9999c 100644 --- a/services/usb_service.cfg +++ b/services/usb_service.cfg @@ -2,8 +2,8 @@ "services" : [{ "name" : "usb_service", "path" : ["/system/bin/sa_main", "/system/profile/usb_service.xml"], - "uid" : "system", - "gid" : ["system", "shell"] + "uid" : "usb", + "gid" : ["usb", "shell"] } ] } diff --git a/services/zidl/include/usb_server_proxy.h b/services/zidl/include/usb_server_proxy.h index ee2b072..66dc0c0 100644 --- a/services/zidl/include/usb_server_proxy.h +++ b/services/zidl/include/usb_server_proxy.h @@ -33,7 +33,7 @@ public: int32_t GetDevices(std::vector &deviceList) override; int32_t OpenDevice(uint8_t busNum, uint8_t devAddr) override; - int32_t HasRight(std::string deviceName) override; + bool 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; diff --git a/services/zidl/src/usb_srv_proxy.cpp b/services/zidl/src/usb_srv_proxy.cpp index fdc2444..71c7c8f 100644 --- a/services/zidl/src/usb_srv_proxy.cpp +++ b/services/zidl/src/usb_srv_proxy.cpp @@ -170,12 +170,13 @@ int32_t UsbServerProxy::GetDeviceMessageParcel(MessageParcel &data, UsbDevice &d int32_t UsbServerProxy::GetDeviceConfigsMessageParcel(MessageParcel &data, std::vector &configs) { - int32_t tmp, configCount; + uint32_t configCount; uint8_t tui8; std::string tstr; - data.ReadInt32(tmp); - configCount = tmp; - for (int32_t i = 0; i < configCount; ++i) { + data.ReadUint32(configCount); + + int32_t tmp; + for (uint32_t i = 0; i < configCount; ++i) { USBConfig config; data.ReadInt32(tmp); config.SetId(tmp); @@ -279,24 +280,30 @@ int32_t UsbServerProxy::OpenDevice(uint8_t busNum, uint8_t devAddr) return ret; } -int32_t UsbServerProxy::HasRight(std::string deviceName) +bool UsbServerProxy::HasRight(std::string deviceName) { MessageParcel data; MessageOption option; MessageParcel reply; sptr remote = Remote(); - RETURN_IF_WITH_RET(remote == nullptr, UEC_INTERFACE_INVALID_VALUE); + RETURN_IF_WITH_RET(remote == nullptr, false); if (!data.WriteInterfaceToken(UsbServerProxy::GetDescriptor())) { USB_HILOGE(MODULE_INNERKIT, "UsbServerProxy::%{public}s write descriptor failed!", __func__); - return UEC_INTERFACE_WRITE_PARCEL_ERROR; + return false; } - WRITE_PARCEL_WITH_RET(data, String, deviceName, UEC_INTERFACE_WRITE_PARCEL_ERROR); + + WRITE_PARCEL_WITH_RET(data, String, deviceName, false); int32_t ret = remote->SendRequest(static_cast(IUsbSrv::USB_FUN_HAS_RIGHT), data, reply, option); if (ret != UEC_OK) { USB_HILOGE(MODULE_USB_INNERKIT, "UsbServerProxy::%{public}s SendRequest is failed, error code: %{public}d", __func__, ret); + return false; } - return ret; + + bool result = false; + READ_PARCEL_WITH_RET(reply, Bool, result, false); + + return result; } int32_t UsbServerProxy::RequestRight(std::string deviceName) diff --git a/services/zidl/src/usb_srv_stub.cpp b/services/zidl/src/usb_srv_stub.cpp index 48b7fd1..f41f1f0 100644 --- a/services/zidl/src/usb_srv_stub.cpp +++ b/services/zidl/src/usb_srv_stub.cpp @@ -263,7 +263,9 @@ int32_t UsbServerStub::DoHasRight(MessageParcel &data, MessageParcel &reply, Mes { std::string deviceName = ""; READ_PARCEL_WITH_RET(data, String, deviceName, UEC_SERVICE_READ_PARCEL_ERROR); - return HasRight(deviceName); + WRITE_PARCEL_WITH_RET(reply, Bool, HasRight(deviceName), UEC_SERVICE_WRITE_PARCEL_ERROR); + + return UEC_OK; } int32_t UsbServerStub::DoRequestRight(MessageParcel &data, MessageParcel &reply, MessageOption &option) @@ -695,7 +697,7 @@ int32_t UsbServerStub::SetDeviceConfigsMessageParcel(std::vector &con WRITE_PARCEL_WITH_RET(data, Uint8, config.GetiConfiguration(), UEC_SERVICE_WRITE_PARCEL_ERROR); WRITE_PARCEL_WITH_RET(data, String, config.GetName(), UEC_SERVICE_WRITE_PARCEL_ERROR); - WRITE_PARCEL_WITH_RET(data, Int32, config.GetInterfaceCount(), UEC_SERVICE_WRITE_PARCEL_ERROR); + WRITE_PARCEL_WITH_RET(data, Uint32, config.GetInterfaceCount(), UEC_SERVICE_WRITE_PARCEL_ERROR); USB_HILOGI(MODULE_USB_SERVICE, "%{public}s devInfo=%{public}s", __func__, config.ToString().c_str()); int32_t ret = SetDeviceInterfacesMessageParcel(config.GetInterfaces(), data); if (ret) { diff --git a/test/native/service_unittest/src/usb_bulkcallback_test.cpp b/test/native/service_unittest/src/usb_bulkcallback_test.cpp index 8066bc3..e88cfa1 100644 --- a/test/native/service_unittest/src/usb_bulkcallback_test.cpp +++ b/test/native/service_unittest/src/usb_bulkcallback_test.cpp @@ -24,6 +24,7 @@ #include "usb_srv_client.h" #include "ashmem.h" #include "securec.h" +#include "usb_errors.h" #include "iservice_registry.h" #include "usb_callback_test.h" diff --git a/test/native/service_unittest/src/usb_core_test.cpp b/test/native/service_unittest/src/usb_core_test.cpp index 6ac86cf..e41210b 100644 --- a/test/native/service_unittest/src/usb_core_test.cpp +++ b/test/native/service_unittest/src/usb_core_test.cpp @@ -353,8 +353,8 @@ HWTEST_F(UsbCoreTest, UsbFunctionsToString005, TestSize.Level1) /** * @tc.name: UsbHasRight001 * @tc.desc: Test functions of HasRight - * @tc.desc: int32_t HasRight(std::string deviceName) - * @tc.desc: 正向测试:代码正常运行,返回结果为-1 + * @tc.desc: bool HasRight(std::string deviceName) + * @tc.desc: Before RequestRight, HasRight return false. * @tc.type: FUNC */ HWTEST_F(UsbCoreTest, UsbHasRight001, TestSize.Level1) @@ -362,17 +362,17 @@ HWTEST_F(UsbCoreTest, UsbHasRight001, TestSize.Level1) USB_HILOGI(MODULE_USB_SERVICE, "Case Start : UsbHasRight001: SetConfig"); auto &UsbSrvClient = UsbSrvClient::GetInstance(); std::string deviceName = "device_80"; - int32_t result = UsbSrvClient.HasRight(deviceName); + bool result = UsbSrvClient.HasRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight001 HasRight=%{public}d", result); - ASSERT_TRUE(result != 0); + ASSERT_FALSE(result); USB_HILOGI(MODULE_USB_SERVICE, "Case End : UsbHasRight001: SetConfig"); } /** * @tc.name: UsbHasRight002 * @tc.desc: Test functions of HasRight - * @tc.desc: int32_t HasRight(std::string deviceName) - * @tc.desc: 反向测试:代码正常运行,返回结果为0 + * @tc.desc: bool HasRight(std::string deviceName) + * @tc.desc: After RequestRight, HasRight return true. * @tc.type: FUNC */ HWTEST_F(UsbCoreTest, UsbHasRight002, TestSize.Level1) @@ -380,23 +380,23 @@ HWTEST_F(UsbCoreTest, UsbHasRight002, TestSize.Level1) USB_HILOGI(MODULE_USB_SERVICE, "Case Start : UsbHasRight002: SetConfig"); auto &UsbSrvClient = UsbSrvClient::GetInstance(); std::string deviceName = "device_80"; - int32_t result = UsbSrvClient.HasRight(deviceName); + bool result = UsbSrvClient.HasRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight002 HasRight=%{public}d", result); - ASSERT_TRUE(result != 0); - result = UsbSrvClient.RequestRight(deviceName); + ASSERT_FALSE(result); + int32_t ret = UsbSrvClient.RequestRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight002 RequestRight=%{public}d", result); - ASSERT_TRUE(result == 0); + ASSERT_EQ(ret, 0); result = UsbSrvClient.HasRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight002 HasRight=%{public}d", result); - ASSERT_TRUE(result == 0); + ASSERT_TRUE(result); USB_HILOGI(MODULE_USB_SERVICE, "Case End : UsbHasRight002: SetConfig"); } /** * @tc.name: UsbHasRight003 * @tc.desc: Test functions of HasRight - * @tc.desc: int32_t HasRight(std::string deviceName) - * @tc.desc: 反向测试:代码正常运行,返回结果为-1 + * @tc.desc: bool HasRight(std::string deviceName) + * @tc.desc: After RemoveRight, HasRight return false. * @tc.type: FUNC */ HWTEST_F(UsbCoreTest, UsbHasRight003, TestSize.Level1) @@ -404,24 +404,24 @@ HWTEST_F(UsbCoreTest, UsbHasRight003, TestSize.Level1) USB_HILOGI(MODULE_USB_SERVICE, "Case Start : UsbHasRight003: SetConfig"); auto &UsbSrvClient = UsbSrvClient::GetInstance(); std::string deviceName = "device_80"; - int32_t result = UsbSrvClient.HasRight(deviceName); + bool result = UsbSrvClient.HasRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight003 HasRight=%{public}d", result); - ASSERT_TRUE(result == 0); - result = UsbSrvClient.RemoveRight(deviceName); + ASSERT_TRUE(result); + int32_t ret = UsbSrvClient.RemoveRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight003 RemoveRight=%{public}d", result); - ASSERT_TRUE(result == 0); + ASSERT_EQ(ret, 0); deviceName = "device_81"; result = UsbSrvClient.HasRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight003 HasRight=%{public}d", result); - ASSERT_TRUE(result != 0); + ASSERT_FALSE(result); USB_HILOGI(MODULE_USB_SERVICE, "Case End : UsbHasRight003: SetConfig"); } /** * @tc.name: UsbHasRight004 * @tc.desc: Test functions of HasRight - * @tc.desc: int32_t HasRight(std::string deviceName) - * @tc.desc: 反向测试:代码正常运行,返回结果为0 + * @tc.desc: bool HasRight(std::string deviceName) + * @tc.desc: RequestRight then RemoveRight * @tc.type: FUNC */ HWTEST_F(UsbCoreTest, UsbHasRight004, TestSize.Level1) @@ -429,18 +429,18 @@ HWTEST_F(UsbCoreTest, UsbHasRight004, TestSize.Level1) USB_HILOGI(MODULE_USB_SERVICE, "Case Start : UsbHasRight004: SetConfig"); auto &UsbSrvClient = UsbSrvClient::GetInstance(); std::string deviceName = "device_82"; - int32_t result = UsbSrvClient.HasRight(deviceName); + bool result = UsbSrvClient.HasRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight004 HasRight=%{public}d", result); - ASSERT_TRUE(result != 0); - result = UsbSrvClient.RequestRight(deviceName); + ASSERT_FALSE(result); + int32_t ret = UsbSrvClient.RequestRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight004 RequestRight=%{public}d", result); - ASSERT_TRUE(result == 0); + ASSERT_EQ(ret, 0); result = UsbSrvClient.HasRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::UsbHasRight004 HasRight=%{public}d", result); - ASSERT_TRUE(result == 0); - result = UsbSrvClient.RemoveRight(deviceName); + ASSERT_TRUE(result); + ret = UsbSrvClient.RemoveRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright003 RemoveRight=%{public}d", result); - ASSERT_TRUE(result == 0); + ASSERT_EQ(ret, 0); USB_HILOGI(MODULE_USB_SERVICE, "Case End : UsbHasRight004: SetConfig"); } @@ -448,7 +448,7 @@ HWTEST_F(UsbCoreTest, UsbHasRight004, TestSize.Level1) * @tc.name: Usbrequestright001 * @tc.desc: Test functions of requestright * @tc.desc: int32_t requestright(std::string deviceName) - * @tc.desc: 正向测试:代码正常运行 + * @tc.desc: RequestRight then RemoveRight * @tc.type: FUNC */ HWTEST_F(UsbCoreTest, Usbrequestright001, TestSize.Level1) @@ -456,40 +456,17 @@ HWTEST_F(UsbCoreTest, Usbrequestright001, TestSize.Level1) USB_HILOGI(MODULE_USB_SERVICE, "Case Start : Usbrequestright001: SetConfig"); auto &UsbSrvClient = UsbSrvClient::GetInstance(); std::string deviceName = "device_83"; - int32_t result = UsbSrvClient.RequestRight(deviceName); - USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright001 RequestRight=%{public}d", result); - ASSERT_TRUE(result == 0); - result = UsbSrvClient.HasRight(deviceName); + int32_t ret = UsbSrvClient.RequestRight(deviceName); + USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright001 RequestRight=%{public}d", ret); + ASSERT_EQ(ret, 0); + bool result = UsbSrvClient.HasRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright001 HasRight=%{public}d", result); - ASSERT_TRUE(result == 0); - result = UsbSrvClient.RemoveRight(deviceName); + ASSERT_TRUE(result); + ret = UsbSrvClient.RemoveRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright003 RemoveRight=%{public}d", result); - ASSERT_TRUE(result == 0); + ASSERT_EQ(ret, 0); USB_HILOGI(MODULE_USB_SERVICE, "Case End : Usbrequestright001: SetConfig"); } -/** - * @tc.name: Usbrequestright002 - * @tc.desc: Test functions of requestright - * @tc.desc: int32_t requestright(std::string deviceName) - * @tc.desc: 正向测试:代码正常运行 - * @tc.type: FUNC - */ -HWTEST_F(UsbCoreTest, Usbrequestright002, TestSize.Level1) -{ - USB_HILOGI(MODULE_USB_SERVICE, "Case Start : Usbrequestright002: SetConfig"); - auto &UsbSrvClient = UsbSrvClient::GetInstance(); - std::string deviceName = "device_84"; - int32_t result = UsbSrvClient.RequestRight(deviceName); - USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright002 RequestRight=%{public}d", result); - ASSERT_TRUE(result == 0); - result = UsbSrvClient.HasRight(deviceName); - USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright002 HasRight=%{public}d", result); - ASSERT_TRUE(result == 0); - result = UsbSrvClient.RemoveRight(deviceName); - USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright003 RemoveRight=%{public}d", result); - ASSERT_TRUE(result == 0); - USB_HILOGI(MODULE_USB_SERVICE, "Case End : Usbrequestright002: SetConfig"); -} /** * @tc.name: Usbrequestright003 @@ -502,18 +479,18 @@ HWTEST_F(UsbCoreTest, Usbrequestright003, TestSize.Level1) USB_HILOGI(MODULE_USB_SERVICE, "Case Start : Usbrequestright003: SetConfig"); auto &UsbSrvClient = UsbSrvClient::GetInstance(); std::string deviceName = "device_81"; - int32_t result = UsbSrvClient.HasRight(deviceName); + bool result = UsbSrvClient.HasRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright003 HasRight=%{public}d", result); - ASSERT_TRUE(result != 0); - result = UsbSrvClient.RequestRight(deviceName); + ASSERT_FALSE(result); + int32_t ret = UsbSrvClient.RequestRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright003 RequestRight=%{public}d", result); - ASSERT_TRUE(result == 0); + ASSERT_EQ(ret, 0); result = UsbSrvClient.HasRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright003 HasRight=%{public}d", result); - ASSERT_TRUE(result == 0); - result = UsbSrvClient.RemoveRight(deviceName); + ASSERT_TRUE(result); + ret = UsbSrvClient.RemoveRight(deviceName); USB_HILOGI(MODULE_USB_SERVICE, "UsbCoreTest::Usbrequestright003 RemoveRight=%{public}d", result); - ASSERT_TRUE(result == 0); + ASSERT_EQ(ret, 0); USB_HILOGI(MODULE_USB_SERVICE, "Case End : Usbrequestright003: SetConfig"); } diff --git a/test/native/service_unittest/src/usb_device_pipe_test.cpp b/test/native/service_unittest/src/usb_device_pipe_test.cpp index a861039..ae466bc 100644 --- a/test/native/service_unittest/src/usb_device_pipe_test.cpp +++ b/test/native/service_unittest/src/usb_device_pipe_test.cpp @@ -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 diff --git a/test/tool/usb_function_port_test.cpp b/test/tool/usb_function_port_test.cpp index eb1a915..d2ef698 100644 --- a/test/tool/usb_function_port_test.cpp +++ b/test/tool/usb_function_port_test.cpp @@ -19,11 +19,14 @@ using namespace std; using namespace OHOS; using namespace OHOS::USB; -static const int32_t DEFAULT_PORT_ID = 1; -static const int32_t DEFAULT_ROLE_HOST = 1; -static const int32_t DEFAULT_ROLE_DEVICE = 2; +static constexpr int32_t DEFAULT_PORT_ID = 1; +static constexpr int32_t DEFAULT_ROLE_HOST = 1; +static constexpr int32_t DEFAULT_ROLE_DEVICE = 2; +static constexpr int32_t MIN_ARG_NUM = 3; +static constexpr uint32_t CMD_INDEX = 1; +static constexpr uint32_t PARAM_INDEX = 2; -static const int32_t HOST_MODE = 2; +static constexpr int32_t HOST_MODE = 2; static UsbSrvClient &g_usbClient = UsbSrvClient::GetInstance(); @@ -33,7 +36,7 @@ static void PrintHelp() printf("-p 0: Query Port\n"); printf("-p 1: Switch to host\n"); printf("-p 2: Switch to device:\n"); - printf("-f 0: Query fucntion\n"); + printf("-f 0: Query function\n"); printf("-f 1: Switch to function:acm\n"); printf("-f 2: Switch to function:ecm\n"); printf("-f 3: Switch to function:acm&ecm\n"); @@ -108,18 +111,29 @@ static void PortSwitch(UsbSrvClient &g_usbClient, int32_t mode) } } +static inline bool isNumber(string_view strv) +{ + return (strv.find_first_not_of("0123456789") == strv.npos); +} + int32_t main(int32_t argc, char *argv[]) { - if ((argc < 1) || (strcmp(argv[1], "-h") == 0)) { + if (argc < MIN_ARG_NUM) { PrintHelp(); return 0; } - if ((!strcmp(argv[1], "-f"))) { - int32_t mode = stoi(argv[2]); + if (!isNumber(argv[PARAM_INDEX])) { + PrintHelp(); + return 0; + } + + int32_t mode; + if ((!strcmp(argv[CMD_INDEX], "-f"))) { + mode = stoi(argv[PARAM_INDEX]); FunctionSwitch(g_usbClient, mode); - } else if (!strcmp(argv[1], "-p")) { - int32_t mode = stoi(argv[2]); + } else if (!strcmp(argv[CMD_INDEX], "-p")) { + mode = stoi(argv[PARAM_INDEX]); PortSwitch(g_usbClient, mode); } else { printf("param incorrect: please input -h for help\n");