1 /*
2  * Copyright (c) 2024 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 VENDOR_DRIVER_BASE_H
17 #define VENDOR_DRIVER_BASE_H
18 
19 #include <string>
20 #include <map>
21 #include <mutex>
22 #include <chrono>
23 #include "vendor_extension.h"
24 #include "printer_info.h"
25 #include "print_constant.h"
26 #include "thread_sync_wait.h"
27 
28 namespace OHOS {
29 namespace Print {
30 struct PrinterVendorStatus {
31     Print_PrinterState state;
32     uint64_t lastUpdateTime;
33     uint64_t lastCheckTime;
PrinterVendorStatusPrinterVendorStatus34     PrinterVendorStatus() : state(PRINTER_UNAVAILABLE), lastUpdateTime(0), lastCheckTime(0) {}
35 };
36 
GetNowTime()37 static inline uint64_t GetNowTime()
38 {
39     return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch())
40         .count();
41 }
42 
43 enum ConnectMethod { ID_AUTO = 0, IP_AUTO, IP_IPP, IP_LPD, IP_SOCKET };
44 
45 class IPrinterVendorManager {
46 public:
47     virtual int32_t AddPrinterToDiscovery(const std::string &vendorName, const PrinterInfo &printerInfo) = 0;
48     virtual int32_t UpdatePrinterToDiscovery(const std::string &vendorName, const PrinterInfo &printerInfo) = 0;
49     virtual int32_t RemovePrinterFromDiscovery(const std::string &vendorName, const std::string &printerId) = 0;
50     virtual int32_t AddPrinterToCupsWithPpd(const std::string &vendorName, const std::string &printerId,
51                                             const std::string &ppdData) = 0;
52     virtual int32_t RemovePrinterFromCups(const std::string &vendorName, const std::string &printerId) = 0;
53     virtual bool OnPrinterStatusChanged(const std::string &vendorName, const std::string &printerId,
54                                         const PrinterVendorStatus &status) = 0;
55     virtual bool OnPrinterPpdQueried(const std::string &vendorName, const std::string &printerId,
56                                      const std::string &ppdData) = 0;
57     virtual bool IsConnectingPrinter(const std::string &id, const std::string &uri) = 0;
58     virtual void SetConnectingPrinter(ConnectMethod method, const std::string &printer) = 0;
59     virtual void ClearConnectingPrinter() = 0;
60     virtual bool QueryPrinterCapabilityByUri(const std::string &uri, PrinterCapability &printerCap) = 0;
61     virtual bool QueryPrinterStatusByUri(const std::string &uri, PrinterStatus &status) = 0;
62     virtual std::shared_ptr<PrinterInfo> QueryDiscoveredPrinterInfoById(const std::string &vendorName,
63         const std::string &printerId) = 0;
64     virtual int32_t QueryPrinterInfoByPrinterId(const std::string &vendorName, const std::string &printerId,
65         PrinterInfo &info) = 0;
66     virtual bool QueryPPDInformation(const char *makeModel, std::vector<std::string> &ppds) = 0;
67 };
68 
69 class VendorDriverBase {
70 public:
71     VendorDriverBase();
72     virtual ~VendorDriverBase();
73     virtual bool Init(IPrinterVendorManager *manager);
74     virtual void UnInit();
75     virtual void OnCreate();
76     virtual void OnDestroy();
77     virtual void OnStartDiscovery();
78     virtual void OnStopDiscovery();
79     virtual bool OnQueryCapability(const std::string &printerId, int timeout);
80     virtual bool OnQueryCapabilityByIp(const std::string &printerIp, const std::string &protocol);
81     virtual bool OnQueryProperties(const std::string &printerId, const std::vector<std::string> &propertyKeys);
82     virtual std::string GetVendorName() = 0;
83     virtual int32_t OnPrinterDiscovered(const std::string &vendorName, const PrinterInfo &printerInfo);
84 
85     void UpdateAllPrinterStatus();
86     bool MonitorPrinterStatus(const std::string &printerId, bool on);
87     std::shared_ptr<PrinterVendorStatus> GetMonitorVendorStatus(const std::string &printerId);
88     std::string GetGlobalVendorName();
89     std::string GetGlobalPrinterId(const std::string &printerId);
90     void OnPrinterStateQueried(const std::string &printerId, Print_PrinterState state);
91 
92 protected:
93     IPrinterVendorManager *vendorManager = nullptr;
94     std::mutex statusMapMutex;
95     std::map<std::string, std::shared_ptr<PrinterVendorStatus>> vendorStatusMap;
96     ThreadSyncWait syncWait;
97 };
98 }  // namespace Print
99 }  // namespace OHOS
100 #endif  // VENDOR_DRIVER_BASE_H
101