diff --git a/zh-cn/device-dev/subsystems/Readme-CN.md b/zh-cn/device-dev/subsystems/Readme-CN.md index 80716e72840992e378b8b3fa7fa12d2d5a2506da..3d2b66695349dc2e21737fed1ece0d8574a15126 100755 --- a/zh-cn/device-dev/subsystems/Readme-CN.md +++ b/zh-cn/device-dev/subsystems/Readme-CN.md @@ -43,10 +43,10 @@ - [Sensor服务子系概述](subsys-sensor-overview.md) - [Sensor服务子系使用指导](subsys-sensor-guide.md) - [Sensor服务子系使用实例](subsys-sensor-demo.md) -- [Usb服务子系统指南](subsys-usbservice.md) - - [USB服务子系概述](subsys-usbservice-overview.md) - - [USB服务子系使用指导](subsys-usbservice-guide.md) - - [USB服务子系使用实例](subsys-usbservice-demo.md) +- [Usb服务子系统](subsys-usbservice.md) + - [USB服务子系统概述](subsys-usbservice-overview.md) + - [USB服务子系统使用指导](subsys-usbservice-guide.md) + - [USB服务子系统使用实例](subsys-usbservice-demo.md) - [用户程序框架](subsys-application-framework.md) - [概述](subsys-application-framework-overview.md) - [搭建环境](subsys-application-framework-envbuild.md) diff --git a/zh-cn/device-dev/subsystems/subsys-usbservice-demo.md b/zh-cn/device-dev/subsystems/subsys-usbservice-demo.md index 5ba094ce370f0585ed359206c0d93d56bc2a759b..80c5d631338d5aca4aabe73347d902ccc0a018a7 100644 --- a/zh-cn/device-dev/subsystems/subsys-usbservice-demo.md +++ b/zh-cn/device-dev/subsystems/subsys-usbservice-demo.md @@ -1,4 +1,4 @@ -# USB服务子系使用实例 +# USB服务子系统使用实例 ``` @@ -7,6 +7,7 @@ #include #include #include +#include #include "if_system_ability_manager.h" #include "ipc_skeleton.h" #include "iremote_object.h" @@ -16,138 +17,164 @@ #include "system_ability_definition.h" #include "usb_common.h" #include "usb_device.h" +#include "usb_errors.h" #include "usb_request.h" #include "usb_server_proxy.h" #include "usb_srv_client.h" -#include "usb_errors.h" -#include -//需要修改/base/usb/usb_manager/service/BUILD.gn和/base/usb/usb_manager/ohos.build文件 -//usb_manager/servrice/BUILD.gn是因为.cpp文件在service目录下,可根据实际情况来调整gn的指定路径 +#define REQUESTYPE ((1 << 7) | (0 << 5) | (0 & 0x1f)) +#define REQUESTCMD 6 +#define VALUE (2 << 8) + 0 +#define TIMEOUT 5000 +#define ITFCLASS 10 +#define ITFPROTOCOL 2 -int main(int argc, char **argv) +void GetType(OHOS::USB::USBEndpoint tep, OHOS::USB::USBEndpoint &outEp, bool &outEpFlg) { - int32_t ret; - static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance(); - // GetDevices - std::vector deviceList; - ret = g_usbClient.GetDevices(deviceList); - if (ret != 0) { - std::cout << "GetDevices failed width ret = " << ret << std::endl; - return -5; - } - if (deviceList.empty()) { - std::cout << "device list is empty." << std::endl; - return -5; - } - for (auto it = deviceList.begin(); it != deviceList.end(); ++it) { - std::cout << "device info :" << it->ToString() << "\n" << std::endl; + if ((tep.GetType() == 2)) { + if (tep.GetDirection() == 0) { + outEp = tep; + outEpFlg = true; + } } - std::cout << "usb_device_test : << GetDevices >> test end ok <-" << std::endl; - OHOS::USB::UsbDevice device = deviceList[0]; - std::vector configs = device.GetConfigs(); - OHOS::USB::USBConfig config = configs[0]; - std::vector interfaces = config.GetInterfaces(); - OHOS::USB::UsbInterface interface; - OHOS::USB::USBEndpoint inEp; - OHOS::USB::USBEndpoint outEp; - bool interfaceFlg = false; - bool inEpFlg = false; - bool outEpFlg = false; - std::string hstr = "\t"; - std::cout << "test device Info:" << device.ToString() << std::endl; - std::cout << hstr << "config Info:" << config.ToString() << std::endl; - hstr += "\t"; +} + +bool selectEndpoint(OHOS::USB::USBConfig config, + std::vector interfaces, + OHOS::USB::UsbInterface &interface, + OHOS::USB::USBEndpoint &outEp, + bool &outEpFlg) +{ for (int32_t i = 0; i < config.GetInterfaceCount(); ++i) { OHOS::USB::UsbInterface tif = interfaces[i]; - std::cout << hstr << "Interfaces[" << i << "]:" << std::endl; - std::cout << hstr << tif.ToString() << std::endl; - std::string hestr = hstr + "\t"; std::vector mEndpoints = tif.GetEndpoints(); for (int32_t j = 0; j < tif.GetEndpointCount(); ++j) { OHOS::USB::USBEndpoint tep = mEndpoints[j]; - std::cout << hestr << "mEndpoints[" << j << "]:" << std::endl; - std::cout << hestr << tep.ToString() << std::endl; - if ((tif.GetClass() == 10) && (tif.GetSubClass() == 0) && (tif.GetProtocol() == 2)) { - if ((!interfaceFlg) && (tep.GetType() == 2)) { - if (tep.GetDirection()) { - inEp = tep; - inEpFlg = true; - } else { - outEp = tep; - outEpFlg = true; - } - } + if ((tif.GetClass() == ITFCLASS) && (tif.GetSubClass() == 0) && (tif.GetProtocol() == ITFPROTOCOL)) { + GetType(tep, outEp, outEpFlg); } } - if (!interfaceFlg) { - if ((outEpFlg) && (inEpFlg)) { - interface = interfaces[i]; - interfaceFlg = true; - } else { - inEpFlg = false; - outEpFlg = false; - } + if (outEpFlg) { + interface = interfaces[i]; + return true; } std::cout << std::endl; } -// OpenDevice - std::cout << "usb_device_test : << OpenDevice >> test begin -> " << std::endl; - OHOS::USB::USBDevicePipe pip; - ret = g_usbClient.RequestRight(device.GetName()); - std::cout << "设备请求权限结果 ret = " << ret << std::endl; + return false; +} + +int OpenDeviceTest(OHOS::USB::UsbSrvClient &g_usbClient, OHOS::USB::UsbDevice device, OHOS::USB::USBDevicePipe &pip) +{ + int ret = g_usbClient.RequestRight(device.GetName()); + std::cout << "设备请求权限结果 RequestRight = " << ret << std::endl; if (0 != ret) { - std::cout << "设备请求权限失败 ret = " << ret << std::endl; + std::cout << "设备请求权限失败 RequestRight = " << ret << std::endl; } ret = g_usbClient.OpenDevice(device, pip); - if (ret != 0) { - std::cout << "OpenDevice failed width ret = " << ret << std::endl; - return -5; - } + return ret; +} + +int CtrTransferTest(OHOS::USB::UsbSrvClient &g_usbClient, OHOS::USB::USBDevicePipe &pip) +{ std::cout << "usb_device_test : << Control Transfer >> " << std::endl; - int32_t requestCmd = 6; - int32_t requestType = ((1 << 7) | (0 << 5) | (0 & 0x1f)); - int32_t value = (2 << 8) + 0; // descriptor type << 8 + descriptor index - int32_t index = 0; - int32_t timeout = 5000; std::vector vData; - const OHOS::USB::UsbCtrlTransfer tctrl = {requestType, requestCmd, value, index, timeout}; - ret = g_usbClient.ControlTransfer(pip, tctrl, vData); + const OHOS::USB::UsbCtrlTransfer tctrl = {REQUESTYPE, REQUESTCMD, VALUE, 0, TIMEOUT}; + int ret = g_usbClient.ControlTransfer(pip, tctrl, vData); if (ret != 0) { std::cout << "control message read failed width ret = " << ret << std::endl; } else { std::cout << "control message read success" << std::endl; } + + return ret; +} + +int ClaimTest(OHOS::USB::UsbSrvClient &g_usbClient, + OHOS::USB::USBDevicePipe &pip, + OHOS::USB::UsbInterface interface, + bool interfaceFlg) +{ if (interfaceFlg) { std::cout << "ClaimInterface InterfaceInfo:" << interface.ToString() << std::endl; - ret = g_usbClient.ClaimInterface(pip, interface, true); + int ret = g_usbClient.ClaimInterface(pip, interface, true); if (ret != 0) { std::cout << "ClaimInterface failed width ret = " << ret << std::endl; } else { std::cout << "ClaimInterface success" << std::endl; } } - // BulkTransfer + return 0; +} + +int BulkTransferTest(OHOS::USB::UsbSrvClient &g_usbClient, + OHOS::USB::USBDevicePipe &pip, + OHOS::USB::USBEndpoint outEp, + bool interfaceFlg, + bool outEpFlg) +{ if (interfaceFlg) { std::cout << "usb_device_test : << Bulk transfer start >> " << std::endl; if (outEpFlg) { - std::cout << "usb_device_test : << Bulk transfer write >> " << std::endl; uint8_t buffer[50] = "hello world 123456789"; std::vector vData(buffer, buffer + 21); - ret = g_usbClient.BulkTransfer(pip, outEp, vData, 5000); + int ret = g_usbClient.BulkTransfer(pip, outEp, vData, 5000); if (ret != 0) { std::cout << "Bulk transfer write failed width ret = " << ret << std::endl; } else { std::cout << "Bulk transfer write success" << std::endl; } + return ret; } } + return 0; +} + +int main(int argc, char **argv) +{ + static OHOS::USB::UsbSrvClient &g_usbClient = OHOS::USB::UsbSrvClient::GetInstance(); + // GetDevices + std::vector deviceList; + int32_t ret = g_usbClient.GetDevices(deviceList); + if (ret != 0) { + return OHOS::USB::UEC_SERVICE_INVALID_VALUE; + } + if (deviceList.empty()) { + return OHOS::USB::UEC_SERVICE_INVALID_VALUE; + } + + OHOS::USB::UsbDevice device = deviceList[0]; + std::vector configs = device.GetConfigs(); + OHOS::USB::USBConfig config = configs[0]; + std::vector interfaces = config.GetInterfaces(); + OHOS::USB::UsbInterface interface; + OHOS::USB::USBEndpoint outEp; + bool interfaceFlg = false; + bool outEpFlg = false; + interfaceFlg = selectEndpoint(config, interfaces, interface, outEp, outEpFlg); + + // OpenDevice + std::cout << "usb_device_test : << OpenDevice >> test begin -> " << std::endl; + OHOS::USB::USBDevicePipe pip; + ret = OpenDeviceTest(g_usbClient, device, pip); + if (ret != 0) { + return OHOS::USB::UEC_SERVICE_INVALID_VALUE; + } + + // ControlTransfer + CtrTransferTest(g_usbClient, pip); + + // ClaimInterface + ClaimTest(g_usbClient, pip, interface, interfaceFlg); + + // BulkTransferWrite + BulkTransferTest(g_usbClient, pip, outEp, interfaceFlg, outEpFlg); + // CloseDevice std::cout << "usb_device_test : << Close Device >> " << std::endl; ret = g_usbClient.Close(pip); if (ret == 0) { std::cout << "Close device failed width ret = " << ret << std::endl; - return -5; + return OHOS::USB::UEC_SERVICE_INVALID_VALUE; } else { std::cout << "Close device success" << std::endl; } diff --git a/zh-cn/device-dev/subsystems/subsys-usbservice-guide.md b/zh-cn/device-dev/subsystems/subsys-usbservice-guide.md index 268b03e8b435a83e4361d65c1a8aa321e0ed5994..c5cd8179243fdd10ff5b7b6b90c1271e6b4bbe46 100644 --- a/zh-cn/device-dev/subsystems/subsys-usbservice-guide.md +++ b/zh-cn/device-dev/subsystems/subsys-usbservice-guide.md @@ -1,4 +1,4 @@ -# USB服务子系使用指导 +# USB服务子系统使用指导 - [使用步骤](#section18816105182315) diff --git a/zh-cn/device-dev/subsystems/subsys-usbservice-overview.md b/zh-cn/device-dev/subsystems/subsys-usbservice-overview.md index bbcc2d700906e3ca707b802f7e2b6b4a839733b6..9475d486a900b330dc65b4da1098146bcee85cb9 100644 --- a/zh-cn/device-dev/subsystems/subsys-usbservice-overview.md +++ b/zh-cn/device-dev/subsystems/subsys-usbservice-overview.md @@ -1,4 +1,4 @@ -# USB服务子系概述 +# USB服务子系统概述 - [概述](#section175431838101617) - [接口说明](#section83365421647)