1 /*
2  * Copyright (c) 2023 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 
17 #ifndef PRINT_USB_MANAGER_H_
18 #define PRINT_USB_MANAGER_H_
19 #ifdef IPPOVERUSB_ENABLE
20 
21 #include <map>
22 #include <unordered_map>
23 #include <string>
24 #include <utility>
25 #include "singleton.h"
26 #include "usb_srv_client.h"
27 #include "httplib.h"
28 #include "print_ipp_over_usb_constant.h"
29 
30 namespace OHOS::Print {
31 
32 struct PrinterTranIndex {
33     int32_t sendDocConfigIndex = INVAILD_VALUE;
34     int32_t sendDocInterfaceIndex = INVAILD_VALUE;
35     int32_t commonConfigIndex = INVAILD_VALUE;
36     int32_t commonInterfaceIndex = INVAILD_VALUE;
37 };
38 
39 enum class Operation {
40     Get_Printer_Attributes,
41     Send_Document,
42     Common
43 };
44 
45 class PrintUsbManager final : public DelayedSingleton<PrintUsbManager> {
46 public:
47     PrintUsbManager();
48     ~PrintUsbManager();
49 
50     void Init();
51     bool isExistIppOverUsbPrinter(std::string printerName);
52     bool ConnectUsbPinter(const std::string &printerName);
53     void DisConnectUsbPinter(const std::string &printerName);
54     int32_t BulkTransferWrite(std::string printerName, const Operation operation,
55         std::vector<uint8_t> &vectorRequestBuffer);
56     int32_t BulkTransferRead(std::string printerName, const Operation operation,
57         std::vector<uint8_t> &vectorRequestBuffer);
58 
59     void DealUsbDevStatusChange(const std::string &devStr, bool isAttach);
60     std::string GetPrinterName(const std::string &name);
61 
62 private:
63     void RefreshUsbPrinterDevice();
64     bool isPrintDevice(USB::UsbDevice &usbdevice, std::string &printerName);
65     std::string GetProductName(USB::UsbDevice &usbDevice);
66     std::string QueryPrinterInfoFromStringDescriptor(
67         USB::USBDevicePipe &usbDevicePipe, uint16_t indexInStringDescriptor);
68     void DisConnectLastUsbPinter(const std::string &printerName);
69     bool AllocateInterface(const std::string &printerName, USB::UsbDevice &usbdevice,
70         USB::USBDevicePipe &usbDevicePipe);
71 
72 private:
73     bool isInit = false;
74     bool isUsbEnable = true;
75     std::unordered_map<std::string, USB::UsbDevice> printDeviceMap;
76     std::map<std::string, std::vector<std::pair<int32_t, int32_t>>> printerIndexMap;
77     std::map<std::string, USB::USBDevicePipe> printPipeMap;
78     std::map<std::string, PrinterTranIndex> printTranIndexMap;
79     std::string currentPrinterName;
80 };
81 
82 }
83 
84 #endif // IPPOVERUSB_ENABLE
85 #endif // PRINT_USB_MANAGER_H_