• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 
16 #ifndef USB_IPP_MANAGER_H
17 #define USB_IPP_MANAGER_H
18 
19 #include <map>
20 #include <vector>
21 #include <string>
22 #include <mutex>
23 #include <atomic>
24 #include "usb_srv_client.h"
25 #include "v1_0/iusb_interface.h"
26 #include "usb_monitor.h"
27 
28 namespace OHOS::CUPS {
29 using namespace OHOS::USB;
30 constexpr int32_t INVAILD_VALUE = -1;
31 constexpr uint8_t USB_CLASS_PRINTER = 0x07;
32 constexpr uint8_t USB_PRINTER_SUBCLASS_IPP = 0x01;
33 constexpr uint8_t USB_PRINTER_PROTOCOL_IPP = 0x04;
34 constexpr int32_t WRITE_RETRY_MAX_TIMES = 20;
35 constexpr int32_t USB_WRITE_INTERVAL = 50;
36 constexpr int32_t EORROR_HDF_DEV_ERR_TIME_OUT = -7;
37 constexpr int32_t EORROR_HDF_DEV_ERR_NO_DEVICE = -202;
38 constexpr int32_t USB_DEVICE_CLASS_PRINT = 7;
39 constexpr int32_t USB_DEVICE_SUBCLASS_PRINT = 1;
40 constexpr int32_t USB_BULKTRANSFER_READ_TIMEOUT = 500;
41 constexpr int32_t USB_BULKTRANSFER_WRITE_TIMEOUT = 500;
42 constexpr int32_t USB_DEVICE_PROTOCOL_PRINT = 4;
43 constexpr int32_t USB_READ_INTERVAL = 50;
44 constexpr int32_t INDEX_0 = 0;
45 constexpr int32_t INDEX_1 = 1;
46 constexpr int32_t INDEX_4 = 4;
47 constexpr int32_t CLAIM_INTERFACE_RETRY_MAX_TIMES = 5;
48 constexpr int32_t EORROR_HDF_DEV_ERR_IO_FAILURE = -1;
49 
50 struct PrinterTranIndex {
51     int32_t configIndex = INVAILD_VALUE;
52     int32_t interfaceIndex = INVAILD_VALUE;
53 };
54 
55 struct BufferContext {
56     uint8_t* data = nullptr;
57     size_t size = INDEX_0;
58     size_t pos = INDEX_0;
59 };
60 
61 struct UsbPrinter {
62     USBDevicePipe ippPipe;
63     PrinterTranIndex tranIndex; // allocated ipp interface
64     std::vector<PrinterTranIndex> indexVec;
65     UsbDevice device;
66     bool isOpened = false;
67     bool isSupportIpp = false;
68 };
69 
70 class IppUsbManager {
71 public:
72     static IppUsbManager& GetInstance();
73     IppUsbManager(const IppUsbManager&) = delete;
74     IppUsbManager& operator=(const IppUsbManager&) = delete;
75     IppUsbManager(IppUsbManager&&) = delete;
76     IppUsbManager& operator=(IppUsbManager&&) = delete;
77 
78     bool IsSupportIppOverUsb(const std::string& uri);
79     bool ConnectUsbPinter(const std::string& uri);
80     bool DisConnectUsbPinter(const std::string& uri);
81     bool ProcessMonitorPrinter(const std::string& uri, MonitorPrinterCallback callback);
82     void SetTerminalSingal();
83 private:
84     IppUsbManager();
85     ~IppUsbManager();
86 
87     bool FindUsbPrinter(UsbDevice& device, const std::string& uri);
88     bool ProcessDataFromDevice(const std::string& uri, PrinterStatus& printerStatus);
89     int32_t BulkTransferRead(const std::string& uri, std::vector<uint8_t>& readTempBUffer);
90     int32_t BulkTransferWrite(const std::string& uri, std::vector<uint8_t>& vectorRequestBuffer);
91     bool AllocateInterface(const std::string &uri, UsbDevice& usbdevice,
92         USBDevicePipe &usbDevicePipe);
93     bool ParseIppResponse(std::vector<uint8_t>& responseData, PrinterStatus& printerStatus);
94     std::vector<uint8_t> BuildIppRequest();
95     static ssize_t ReadFromBuffer(void* ctx, void* data, size_t len);
96     static ssize_t WriteToBuffer(void* ctx, const void* data, size_t len);
97     void RemoveHttpHeader(std::vector<uint8_t>& readTempBuffer);
98     void ReportPrinterState(bool& isPrinterStarted, PrinterStatus& printerStatus, MonitorPrinterCallback callback);
99     void SetPrinterStateReasons(PrinterStatus& printerStatus);
100 
101     std::map<std::string, UsbPrinter> ippPrinterMap_;
102     std::atomic<bool> isTerminated_;
103     std::mutex lock_;
104 };
105 } // namespace OHOS::CUPS
106 #endif // USB_IPP_MANAGER_H