• 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 #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 
IsStatusMonitoring(const std::string & printerId)121 bool VendorDriverBase::IsStatusMonitoring(const std::string &printerId)
122 {
123     std::lock_guard<std::mutex> lock(statusMapMutex);
124     return vendorStatusMap.find(printerId) != vendorStatusMap.end();
125 }
126 
QueryProperty(const std::string & printerId,const std::string & key,std::string & value)127 bool VendorDriverBase::QueryProperty(const std::string &printerId, const std::string &key, std::string &value)
128 {
129     value = std::string();
130     return false;
131 }
132 
GetMonitorVendorStatus(const std::string & printerId)133 std::shared_ptr<PrinterVendorStatus> VendorDriverBase::GetMonitorVendorStatus(const std::string &printerId)
134 {
135     std::lock_guard<std::mutex> lock(statusMapMutex);
136     auto iter = vendorStatusMap.find(printerId);
137     if (iter != vendorStatusMap.end()) {
138         return iter->second;
139     }
140     return nullptr;
141 }
142 
GetGlobalVendorName()143 std::string VendorDriverBase::GetGlobalVendorName()
144 {
145     return VendorManager::GetGlobalVendorName(GetVendorName());
146 }
147 
GetGlobalPrinterId(const std::string & printerId)148 std::string VendorDriverBase::GetGlobalPrinterId(const std::string &printerId)
149 {
150     return VendorManager::GetGlobalPrinterId(GetGlobalVendorName(), printerId);
151 }
152 
OnPrinterStateQueried(const std::string & printerId,Print_PrinterState state)153 void VendorDriverBase::OnPrinterStateQueried(const std::string &printerId, Print_PrinterState state)
154 {
155     PRINT_HILOGD("state queried: %{public}d for %{public}s", static_cast<int>(state), printerId.c_str());
156     if (vendorManager == nullptr) {
157         PRINT_HILOGW("vendorManager is null");
158         return;
159     }
160     auto vendorStatus = GetMonitorVendorStatus(printerId);
161     if (vendorStatus != nullptr) {
162         vendorStatus->state = state;
163         bool updated = vendorManager->OnPrinterStatusChanged(GetVendorName(), printerId, *vendorStatus);
164         vendorStatus->lastUpdateTime = GetNowTime();
165         if (state == PRINTER_IDLE && !updated) {
166             OnQueryCapability(printerId, 0);
167         }
168     } else {
169         PRINT_HILOGW("vendorStatus is null");
170     }
171 }