• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# USB服务子系使用实例<a name="ZH-CN_TOPIC_0000001092792986"></a>
2
3
4```cpp
5#include <cstdio>
6#include <iostream>
7#include <map>
8#include <mutex>
9#include <sstream>
10#include <sys/time.h>
11#include "if_system_ability_manager.h"
12#include "ipc_skeleton.h"
13#include "iremote_object.h"
14#include "iservice_registry.h"
15#include "iusb_srv.h"
16#include "string_ex.h"
17#include "system_ability_definition.h"
18#include "usb_common.h"
19#include "usb_device.h"
20#include "usb_errors.h"
21#include "usb_request.h"
22#include "usb_server_proxy.h"
23#include "usb_srv_client.h"
24
25const int32_t REQUESTYPE = ((1 << 7) | (0 << 5) | (0 & 0x1f));
26const int32_t REQUESTCMD = 6;
27const int32_t VALUE = (2 << 8) + 0;
28const int32_t TIMEOUT = 5000;
29const int32_t ITFCLASS = 10;
30const int32_t PRAMATYPE = 2;
31const int32_t BUFFERLENGTH = 21;
32
33void GetType(OHOS::USB::USBEndpoint &tep, OHOS::USB::USBEndpoint &outEp, bool &outEpFlg)
34{
35    if ((tep.GetType() == PRAMATYPE)) {
36        if (tep.GetDirection() == 0) {
37            outEp = tep;
38            outEpFlg = true;
39        }
40    }
41}
42
43bool SelectEndpoint(OHOS::USB::USBConfig config,
44                    std::vector<OHOS::USB::UsbInterface> interfaces,
45                    OHOS::USB::UsbInterface &interface,
46                    OHOS::USB::USBEndpoint &outEp,
47                    bool &outEpFlg)
48{
49    for (int32_t i = 0; i < config.GetInterfaceCount(); ++i) {
50        OHOS::USB::UsbInterface tif = interfaces[i];
51        std::vector<OHOS::USB::USBEndpoint> mEndpoints = tif.GetEndpoints();
52        for (int32_t j = 0; j < tif.GetEndpointCount(); ++j) {
53            OHOS::USB::USBEndpoint tep = mEndpoints[j];
54            if ((tif.GetClass() == ITFCLASS) && (tif.GetSubClass() == 0) && (tif.GetProtocol() == PRAMATYPE)) {
55                GetType(tep, outEp, outEpFlg);
56            }
57        }
58        if (outEpFlg) {
59            interface = interfaces[i];
60            return true;
61        }
62        std::cout << std::endl;
63    }
64    return false;
65}
66
67int OpenDeviceTest(OHOS::USB::UsbSrvClient &Instran, OHOS::USB::UsbDevice device, OHOS::USB::USBDevicePipe &pip)
68{
69    int ret = Instran.RequestRight(device.GetName());
70    std::cout << "device RequestRight ret = " << ret << std::endl;
71    if (0 != ret) {
72        std::cout << "device RequestRight failed = " << ret << std::endl;
73    }
74    ret = Instran.OpenDevice(device, pip);
75    return ret;
76}
77
78int CtrTransferTest(OHOS::USB::UsbSrvClient &Instran, OHOS::USB::USBDevicePipe &pip)
79{
80    std::cout << "usb_device_test : << Control Transfer >> " << std::endl;
81    std::vector<uint8_t> vData;
82    const OHOS::USB::UsbCtrlTransfer tctrl = {REQUESTYPE, REQUESTCMD, VALUE, 0, TIMEOUT};
83    int ret = Instran.ControlTransfer(pip, tctrl, vData);
84    if (ret != 0) {
85        std::cout << "control message read failed width ret = " << ret << std::endl;
86    } else {
87    }
88        std::cout << "control message read success" << std::endl;
89
90    return ret;
91}
92
93int ClaimTest(OHOS::USB::UsbSrvClient &Instran,
94              OHOS::USB::USBDevicePipe &pip,
95              OHOS::USB::UsbInterface &interface,
96              bool interfaceFlg)
97{
98    if (interfaceFlg) {
99        std::cout << "ClaimInterface InterfaceInfo:" << interface.ToString() << std::endl;
100        int ret = Instran.ClaimInterface(pip, interface, true);
101        if (ret != 0) {
102            std::cout << "ClaimInterface failed width ret = " << ret << std::endl;
103        } else {
104            std::cout << "ClaimInterface success" << std::endl;
105        }
106    }
107    return 0;
108}
109
110int BulkTransferTest(OHOS::USB::UsbSrvClient &Instran,
111                     OHOS::USB::USBDevicePipe &pip,
112                     OHOS::USB::USBEndpoint &outEp,
113                     bool interfaceFlg,
114                     bool outEpFlg)
115{
116    if (interfaceFlg) {
117        std::cout << "usb_device_test : << Bulk transfer start >> " << std::endl;
118        if (outEpFlg) {
119            uint8_t buffer[50] = "hello world 123456789";
120            std::vector<uint8_t> vData(buffer, buffer + BUFFERLENGTH);
121            int ret = Instran.BulkTransfer(pip, outEp, vData, TIMEOUT);
122            if (ret != 0) {
123                std::cout << "Bulk transfer write failed width ret = " << ret << std::endl;
124            } else {
125                std::cout << "Bulk transfer write success" << std::endl;
126            }
127            return ret;
128        }
129    }
130    return 0;
131}
132
133int main(int argc, char **argv)
134{
135    std::cout << "usb_device_test " << std::endl;
136    static OHOS::USB::UsbSrvClient &Instran = OHOS::USB::UsbSrvClient::GetInstance();
137    // GetDevices
138    std::vector<OHOS::USB::UsbDevice> deviceList;
139    int32_t ret = Instran.GetDevices(deviceList);
140    if (ret != 0) {
141        return OHOS::USB::UEC_SERVICE_INVALID_VALUE;
142    }
143    if (deviceList.empty()) {
144        return OHOS::USB::UEC_SERVICE_INVALID_VALUE;
145    }
146
147    OHOS::USB::UsbDevice device = deviceList[0];
148    std::vector<OHOS::USB::USBConfig> configs = device.GetConfigs();
149    OHOS::USB::USBConfig config = configs[0];
150    std::vector<OHOS::USB::UsbInterface> interfaces = config.GetInterfaces();
151    OHOS::USB::UsbInterface interface;
152    OHOS::USB::USBEndpoint outEp;
153    bool interfaceFlg = false;
154    bool outEpFlg = false;
155    interfaceFlg = SelectEndpoint(config, interfaces, interface, outEp, outEpFlg);
156
157    // OpenDevice
158    std::cout << "usb_device_test : << OpenDevice >> test begin -> " << std::endl;
159    OHOS::USB::USBDevicePipe pip;
160    ret = OpenDeviceTest(Instran, device, pip);
161    if (ret != 0) {
162        return OHOS::USB::UEC_SERVICE_INVALID_VALUE;
163    }
164
165    // ControlTransfer
166    CtrTransferTest(Instran, pip);
167
168    // ClaimInterface
169    ClaimTest(Instran, pip, interface, interfaceFlg);
170
171    // BulkTransferWrite
172    BulkTransferTest(Instran, pip, outEp, interfaceFlg, outEpFlg);
173
174    // CloseDevice
175    std::cout << "usb_device_test : << Close Device >> " << std::endl;
176    ret = Instran.Close(pip);
177    if (ret == 0) {
178        std::cout << "Close device failed width ret = " << ret << std::endl;
179        return OHOS::USB::UEC_SERVICE_INVALID_VALUE;
180    } else {
181        std::cout << "Close Device success" << std::endl;
182    }
183    return 0;
184}
185```
186
187
188