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