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