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 #include <vector>
17 #include "vendor_driver_base.h"
18 #include "vendor_helper.h"
19 #include "vendor_manager.h"
20 #include "print_log.h"
21 
22 namespace {
23 const int MONITOR_CHECK_INTERVAL_MS = 15000;
24 const int MONITOR_OFFLINE_TIMEOUT_MS = 60000;
25 }
26 
27 using namespace OHOS::Print;
28 
VendorDriverBase()29 VendorDriverBase::VendorDriverBase() {}
30 
~VendorDriverBase()31 VendorDriverBase::~VendorDriverBase() {}
32 
Init(IPrinterVendorManager *manager)33 bool VendorDriverBase::Init(IPrinterVendorManager *manager)
34 {
35     vendorManager = manager;
36     return true;
37 }
UnInit()38 void VendorDriverBase::UnInit()
39 {
40     vendorManager = nullptr;
41 }
42 
OnCreate()43 void VendorDriverBase::OnCreate() {}
OnDestroy()44 void VendorDriverBase::OnDestroy() {}
45 
OnStartDiscovery()46 void VendorDriverBase::OnStartDiscovery() {}
OnStopDiscovery()47 void VendorDriverBase::OnStopDiscovery() {}
48 
OnQueryCapability(const std::string &printerId, int timeout)49 bool VendorDriverBase::OnQueryCapability(const std::string &printerId, int timeout)
50 {
51     return false;
52 }
OnQueryCapabilityByIp(const std::string &printerIp, const std::string &protocol)53 bool VendorDriverBase::OnQueryCapabilityByIp(const std::string &printerIp, const std::string &protocol)
54 {
55     return false;
56 }
OnQueryProperties(const std::string &printerId, const std::vector<std::string> &propertyKeys)57 bool VendorDriverBase::OnQueryProperties(const std::string &printerId, const std::vector<std::string> &propertyKeys)
58 {
59     return false;
60 }
61 
UpdateAllPrinterStatus()62 void VendorDriverBase::UpdateAllPrinterStatus()
63 {
64     std::lock_guard<std::mutex> lock(statusMapMutex);
65     uint64_t currentTime = GetNowTime();
66     for (auto const &pair : vendorStatusMap) {
67         auto vendorStatus = pair.second;
68         if (vendorStatus == nullptr) {
69             PRINT_HILOGW("status is null, %{private}s", pair.first.c_str());
70             continue;
71         }
72         if (currentTime < vendorStatus->lastCheckTime + MONITOR_CHECK_INTERVAL_MS) {
73             continue;
74         }
75         if (currentTime < vendorStatus->lastUpdateTime + MONITOR_CHECK_INTERVAL_MS) {
76             continue;
77         }
78         vendorStatus->lastCheckTime = currentTime;
79         std::vector<std::string> list;
80         list.push_back(PRINTER_PROPERTY_KEY_DEVICE_STATE);
81         OnQueryProperties(pair.first, list);
82         if (currentTime < vendorStatus->lastUpdateTime + MONITOR_OFFLINE_TIMEOUT_MS) {
83             continue;
84         }
85         vendorStatus->state = PRINTER_UNAVAILABLE;
86         if (vendorManager != nullptr) {
87             vendorManager->OnPrinterStatusChanged(GetVendorName(), pair.first, *vendorStatus);
88         }
89     }
90 }
91 
MonitorPrinterStatus(const std::string &printerId, bool on)92 bool VendorDriverBase::MonitorPrinterStatus(const std::string &printerId, bool on)
93 {
94     std::lock_guard<std::mutex> lock(statusMapMutex);
95     auto iter = vendorStatusMap.find(printerId);
96     if (iter == vendorStatusMap.end()) {
97         if (on) {
98             vendorStatusMap[printerId] = std::make_shared<PrinterVendorStatus>();
99             PRINT_HILOGD("monitor on: %{public}s", printerId.c_str());
100             return true;
101         }
102     } else {
103         if (!on) {
104             vendorStatusMap.erase(iter);
105             PRINT_HILOGD("monitor off: %{public}s", printerId.c_str());
106             return true;
107         }
108     }
109     return false;
110 }
111 
GetMonitorVendorStatus(const std::string &printerId)112 std::shared_ptr<PrinterVendorStatus> VendorDriverBase::GetMonitorVendorStatus(const std::string &printerId)
113 {
114     std::lock_guard<std::mutex> lock(statusMapMutex);
115     auto iter = vendorStatusMap.find(printerId);
116     if (iter != vendorStatusMap.end()) {
117         return iter->second;
118     }
119     return nullptr;
120 }
121 
GetGlobalVendorName()122 std::string VendorDriverBase::GetGlobalVendorName()
123 {
124     return VendorManager::GetGlobalVendorName(GetVendorName());
125 }
126 
GetGlobalPrinterId(const std::string &printerId)127 std::string VendorDriverBase::GetGlobalPrinterId(const std::string &printerId)
128 {
129     return VendorManager::GetGlobalPrinterId(GetGlobalVendorName(), printerId);
130 }
131 
OnPrinterStateQueried(const std::string &printerId, Print_PrinterState state)132 void VendorDriverBase::OnPrinterStateQueried(const std::string &printerId, Print_PrinterState state)
133 {
134     PRINT_HILOGD("state queried: %{public}d for %{public}s", static_cast<int>(state), printerId.c_str());
135     if (vendorManager == nullptr) {
136         PRINT_HILOGW("vendorManager is null");
137         return;
138     }
139     auto vendorStatus = GetMonitorVendorStatus(printerId);
140     if (vendorStatus != nullptr) {
141         vendorStatus->state = state;
142         bool updated = vendorManager->OnPrinterStatusChanged(GetVendorName(), printerId, *vendorStatus);
143         vendorStatus->lastUpdateTime = GetNowTime();
144         if (state == PRINTER_IDLE && !updated) {
145             OnQueryCapability(printerId, 0);
146         }
147     } else {
148         PRINT_HILOGW("vendorStatus is null");
149     }
150 }