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