• 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 "vendor_wlan_group.h"
17 #include "print_log.h"
18 #include "print_utils.h"
19 #include "file_ex.h"
20 
21 using namespace OHOS::Print;
22 namespace {
23     const std::string VENDOR_BSUNI_URI_START = "://";
24     const std::string VENDOR_BSUNI_URI_END = ":";
25     const std::string VENDOR_CONVERTED_PRINTERID = "uuid";
26 }
27 
VendorWlanGroup(VendorManager * vendorManager)28 VendorWlanGroup::VendorWlanGroup(VendorManager *vendorManager) : parentVendorManager(vendorManager)
29 {
30     hasGs = FileExists("/system/bin/uni_print_driver/ghostscript/bin/gs");
31 }
32 
GetVendorName()33 std::string VendorWlanGroup::GetVendorName()
34 {
35     return VENDOR_WLAN_GROUP;
36 }
37 
OnQueryCapability(const std::string & printerId,int timeout)38 bool VendorWlanGroup::OnQueryCapability(const std::string &printerId, int timeout)
39 {
40     PRINT_HILOGI("OnQueryCapability enter.");
41     if (parentVendorManager == nullptr) {
42         PRINT_HILOGE("VendorManager is null.");
43         return false;
44     }
45     auto ppdDriver = parentVendorManager->FindDriverByVendorName(VENDOR_PPD_DRIVER);
46     if (ppdDriver != nullptr) {
47         printerVendorGroupList_[printerId] = VENDOR_PPD_DRIVER;
48         auto printerInfo = parentVendorManager->QueryDiscoveredPrinterInfoById(GetVendorName(), printerId);
49         if (printerInfo != nullptr && ppdDriver->OnQueryProperties(printerId, std::vector<std::string>(1,
50             printerInfo->GetPrinterMake()))) {
51             if (ppdDriver->OnQueryCapability(printerId, timeout)) {
52                 PRINT_HILOGI("on query capability on ppd vendor seccess.");
53                 return true;
54             }
55         }
56         RemoveGroupPrinterFromVendorGroupList(printerId);
57     }
58     if (IsBsunidriverSupport(printerId)) {
59         printerVendorGroupList_[printerId] = VENDOR_BSUNI_DRIVER;
60         auto bsuniDriver = parentVendorManager->FindDriverByVendorName(VENDOR_BSUNI_DRIVER);
61         auto printerInfo = parentVendorManager->QueryDiscoveredPrinterInfoById(GetVendorName(), printerId);
62         if (bsuniDriver != nullptr && printerInfo != nullptr &&
63             bsuniDriver->OnQueryCapability(ExtractPrinterIdByPrinterInfo(*printerInfo), timeout)) {
64             PRINT_HILOGI("on query capability on bsuni vendor seccess.");
65             return true;
66         }
67         RemoveGroupPrinterFromVendorGroupList(printerId);
68     } else {
69         printerVendorGroupList_[printerId] = VENDOR_IPP_EVERYWHERE;
70         auto ippEverywhereDriver = parentVendorManager->FindDriverByVendorName(VENDOR_IPP_EVERYWHERE);
71         if (ippEverywhereDriver != nullptr && ippEverywhereDriver->OnQueryCapability(printerId, 0)) {
72             PRINT_HILOGI("on query capability on ipp everywhere seccess.");
73             return true;
74         }
75         RemoveGroupPrinterFromVendorGroupList(printerId);
76     }
77     PRINT_HILOGE("no vendor can query capability.");
78     return false;
79 }
80 
OnQueryCapabilityByIp(const std::string & printerIp,const std::string & protocol)81 bool VendorWlanGroup::OnQueryCapabilityByIp(const std::string &printerIp, const std::string &protocol)
82 {
83     PRINT_HILOGI("OnQueryCapabilityByIp enter.");
84     if (parentVendorManager == nullptr) {
85         PRINT_HILOGE("VendorManager is null.");
86         return false;
87     }
88     auto bsuniDriver = parentVendorManager->FindDriverByVendorName(VENDOR_BSUNI_DRIVER);
89     printerVendorGroupList_[printerIp] = VENDOR_BSUNI_DRIVER;
90     if (bsuniDriver != nullptr && bsuniDriver->OnQueryCapabilityByIp(printerIp, protocol)) {
91         PRINT_HILOGI("on query capability by ip on bsuni vendor seccess.");
92         return true;
93     }
94     RemoveGroupPrinterFromVendorGroupList(printerIp);
95     printerVendorGroupList_[printerIp] = VENDOR_IPP_EVERYWHERE;
96     auto ippEverywhereDriver = parentVendorManager->FindDriverByVendorName(VENDOR_IPP_EVERYWHERE);
97     if (ippEverywhereDriver != nullptr && ippEverywhereDriver->OnQueryCapabilityByIp(printerIp, protocol)) {
98         PRINT_HILOGI("on query capability by ip on ipp everywhere seccess.");
99         return true;
100     }
101     RemoveGroupPrinterFromVendorGroupList(printerIp);
102     PRINT_HILOGE("no vendor can query capability by ip.");
103     return false;
104 }
105 
OnPrinterDiscovered(const std::string & vendorName,const PrinterInfo & printerInfo)106 int32_t VendorWlanGroup::OnPrinterDiscovered(const std::string &vendorName, const PrinterInfo &printerInfo)
107 {
108     if (parentVendorManager == nullptr) {
109         PRINT_HILOGE("VendorManager is null.");
110         return EXTENSION_ERROR_CALLBACK_NULL;
111     }
112     return parentVendorManager->AddPrinterToDiscovery(GetVendorName(), ConvertPrinterInfoId(printerInfo));
113 }
114 
OnUpdatePrinterToDiscovery(const std::string & vendorName,const PrinterInfo & printerInfo)115 int32_t VendorWlanGroup::OnUpdatePrinterToDiscovery(const std::string &vendorName, const PrinterInfo &printerInfo)
116 {
117     if (parentVendorManager == nullptr) {
118         PRINT_HILOGE("VendorManager is null.");
119         return EXTENSION_ERROR_CALLBACK_NULL;
120     }
121     if (CheckPrinterAddedByIp(printerInfo.GetPrinterId())) {
122         PRINT_HILOGI("OnUpdatePrinterToDiscovery PrinterAddedByIp");
123         return parentVendorManager->UpdatePrinterToDiscovery(GetVendorName(), ConvertIpPrinterName(printerInfo));
124     }
125     return parentVendorManager->UpdatePrinterToDiscovery(GetVendorName(), ConvertPrinterInfoId(printerInfo));
126 }
127 
OnPrinterRemoved(const std::string & vendorName,const std::string & printerId)128 int32_t VendorWlanGroup::OnPrinterRemoved(const std::string &vendorName, const std::string &printerId)
129 {
130     if (parentVendorManager == nullptr) {
131         PRINT_HILOGE("VendorManager is null.");
132         return EXTENSION_ERROR_CALLBACK_NULL;
133     }
134     std::string groupPrinterId = GetGroupPrinterId(printerId);
135     RemoveGroupPrinterFromVendorGroupList(printerId);
136     RemovedGroupPrinter(printerId);
137     return parentVendorManager->RemovePrinterFromDiscovery(GetVendorName(), groupPrinterId);
138 }
139 
IsConnectingPrinter(const std::string & globalPrinterIdOrIp,const std::string & uri)140 bool VendorWlanGroup::IsConnectingPrinter(const std::string &globalPrinterIdOrIp, const std::string &uri)
141 {
142     if (parentVendorManager == nullptr) {
143         PRINT_HILOGE("VendorManager is null.");
144         return false;
145     }
146     std::string printerId(VendorManager::ExtractPrinterId(globalPrinterIdOrIp));
147     std::string groupPrinterId = CheckPrinterAddedByIp(printerId) ? printerId : GetGroupPrinterId(printerId);
148     std::string globalPrinterId = GetGlobalPrinterId(groupPrinterId);
149     return parentVendorManager->IsConnectingPrinter(globalPrinterId, uri);
150 }
151 
GetConnectingMethod(const std::string & globalPrinterIdOrIp)152 ConnectMethod VendorWlanGroup::GetConnectingMethod(const std::string &globalPrinterIdOrIp)
153 {
154     if (parentVendorManager == nullptr) {
155         PRINT_HILOGE("VendorManager is null.");
156         return ID_AUTO;
157     }
158     std::string printerId(VendorManager::ExtractPrinterId(globalPrinterIdOrIp));
159     std::string groupPrinterId = CheckPrinterAddedByIp(printerId) ? printerId : GetGroupPrinterId(printerId);
160     std::string globalPrinterId = GetGlobalPrinterId(groupPrinterId);
161     return parentVendorManager->GetConnectingMethod(globalPrinterId);
162 }
163 
SetConnectingPrinter(ConnectMethod method,const std::string & globalPrinterIdOrIp)164 void VendorWlanGroup::SetConnectingPrinter(ConnectMethod method, const std::string &globalPrinterIdOrIp)
165 {
166     if (parentVendorManager == nullptr) {
167         PRINT_HILOGE("VendorManager is null.");
168         return;
169     }
170     std::string printerId(VendorManager::ExtractPrinterId(globalPrinterIdOrIp));
171     std::string groupPrinterId = CheckPrinterAddedByIp(printerId) ? printerId : GetGroupPrinterId(printerId);
172     std::string globalPrinterId = GetGlobalPrinterId(groupPrinterId);
173     parentVendorManager->SetConnectingPrinter(method, globalPrinterId);
174 }
175 
OnPrinterPpdQueried(const std::string & vendorName,const std::string & printerId,const std::string & ppdData)176 bool VendorWlanGroup::OnPrinterPpdQueried(const std::string &vendorName, const std::string &printerId,
177                                           const std::string &ppdData)
178 {
179     if (parentVendorManager == nullptr) {
180         PRINT_HILOGE("VendorManager is null.");
181         return false;
182     }
183     std::string groupPrinterId = CheckPrinterAddedByIp(printerId) ? printerId : GetGroupPrinterId(printerId);
184     return parentVendorManager->OnPrinterPpdQueried(GetVendorName(), groupPrinterId, ppdData);
185 }
186 
187 
IsGroupDriver(const std::string & bothPrinterId)188 bool VendorWlanGroup::IsGroupDriver(const std::string &bothPrinterId)
189 {
190     if (bothPrinterId.find(VENDOR_CONVERTED_PRINTERID) != std::string::npos) {
191         PRINT_HILOGD("printerId has be converted whit uuid, is group driver!.");
192         return true;
193     }
194     std::string printerId(VendorManager::ExtractPrinterId(bothPrinterId));
195     auto iter = printerVendorGroupList_.find(printerId);
196     return (iter != printerVendorGroupList_.end() && !iter->second.empty());
197 }
198 
ConvertGroupDriver(std::string & printerId,std::string & vendorName)199 bool VendorWlanGroup::ConvertGroupDriver(std::string &printerId, std::string &vendorName)
200 {
201     printerId = VendorManager::ExtractPrinterId(printerId);
202     if (vendorName == VENDOR_BSUNI_DRIVER) {
203         printerId = GetGroupPrinterId(printerId);
204         return false;
205     }
206     auto iter = printerVendorGroupList_.find(printerId);
207     if (iter != printerVendorGroupList_.end() && !iter->second.empty()) {
208         vendorName = VENDOR_WLAN_GROUP;
209         return true;
210     }
211     return false;
212 }
213 
IsBsunidriverSupport(const std::string & groupPrinterId)214 bool VendorWlanGroup::IsBsunidriverSupport(const std::string &groupPrinterId)
215 {
216     PRINT_HILOGD("IsBsunidriverSupport enter");
217     if (parentVendorManager == nullptr) {
218         PRINT_HILOGE("VendorManager is null.");
219         return false;
220     }
221     auto printerInfo = parentVendorManager->QueryDiscoveredPrinterInfoById(GetVendorName(), groupPrinterId);
222     if (printerInfo == nullptr) {
223         return false;
224     }
225     std::string supportValue;
226     Json::Value option;
227     if (printerInfo->HasOption() && PrintJsonUtil::Parse(printerInfo->GetOption(), option)) {
228         if (PrintJsonUtil::IsMember(option, "bsunidriverSupport") && option["bsunidriverSupport"].isString()) {
229             supportValue = option["bsunidriverSupport"].asString();
230         }
231     }
232     PRINT_HILOGD("IsBsunidriverSupport bsunidriverSupport=%{public}s", supportValue.c_str());
233     if (supportValue == "true") {
234         return true;
235     } else if (supportValue == "need_gs") {
236         return hasGs;
237     } else {
238         return false;
239     }
240 }
241 
RemoveGroupPrinterFromVendorGroupList(const std::string & groupPrinterId)242 void VendorWlanGroup::RemoveGroupPrinterFromVendorGroupList(const std::string &groupPrinterId)
243 {
244     auto iter = printerVendorGroupList_.find(groupPrinterId);
245     if (iter != printerVendorGroupList_.end()) {
246         PRINT_HILOGD("remove printer from vendor group list");
247         printerVendorGroupList_.erase(groupPrinterId);
248     }
249 }
250 
QueryVendorDriverByGroupPrinterId(const std::string & groupPrinterId)251 std::string VendorWlanGroup::QueryVendorDriverByGroupPrinterId(const std::string &groupPrinterId)
252 {
253     auto iter = printerVendorGroupList_.find(groupPrinterId);
254     if (iter != printerVendorGroupList_.end()) {
255         return iter->second;
256     }
257     PRINT_HILOGE("query printer vendor driver failed");
258     return "";
259 }
260 
ConvertGroupGlobalPrinterId(const std::string & bothPrinterId)261 std::string VendorWlanGroup::ConvertGroupGlobalPrinterId(const std::string &bothPrinterId)
262 {
263     std::string printerId(VendorManager::ExtractPrinterId(bothPrinterId));
264     return PrintUtils::GetGlobalId(VendorManager::GetGlobalVendorName(GetVendorName()), printerId);
265 }
266 
GetGroupPrinterId(const std::string & printerId)267 std::string VendorWlanGroup::GetGroupPrinterId(const std::string &printerId)
268 {
269     std::lock_guard<std::mutex> lock(groupPrinterIdMapMutex);
270     auto item = groupPrinterIdMap_.find(printerId);
271     if (item != groupPrinterIdMap_.end() && !item->second.empty()) {
272         return item->second;
273     }
274     PRINT_HILOGW("groupPrinterId is not exited, use printerId");
275     return printerId;
276 }
277 
CheckPrinterAddedByIp(const std::string & printerId)278 bool VendorWlanGroup::CheckPrinterAddedByIp(const std::string &printerId)
279 {
280     if (parentVendorManager == nullptr) {
281         PRINT_HILOGE("VendorManager is null.");
282         return false;
283     }
284     ConnectMethod connectingMethod = parentVendorManager->GetConnectingMethod(printerId);
285     PRINT_HILOGI("CheckPrinterAddedByIp connectingMethod : %{public}d", connectingMethod);
286     if (connectingMethod == IP_AUTO) {
287         return true;
288     }
289     return false;
290 }
291 
UpdateGroupPrinter(const std::string & printerId,const std::string & groupPrinterId)292 void VendorWlanGroup::UpdateGroupPrinter(const std::string &printerId, const std::string &groupPrinterId)
293 {
294     std::lock_guard<std::mutex> lock(groupPrinterIdMapMutex);
295     auto retPair = groupPrinterIdMap_.try_emplace(printerId, groupPrinterId);
296     if (retPair.second) {
297         PRINT_HILOGI("add new groupPrinterId");
298         return;
299     }
300     if (retPair.first->second == groupPrinterId) {
301         PRINT_HILOGD("not need update groupPrinterId");
302         return;
303     }
304     PRINT_HILOGI("update groupPrinterId");
305     parentVendorManager->RemovePrinterFromDiscovery(GetVendorName(), retPair.first->second);
306     retPair.first->second = groupPrinterId;
307 }
308 
HasGroupPrinter(const std::string & printerId)309 bool VendorWlanGroup::HasGroupPrinter(const std::string &printerId)
310 {
311     std::lock_guard<std::mutex> lock(groupPrinterIdMapMutex);
312     auto item = groupPrinterIdMap_.find(printerId);
313     if (item != groupPrinterIdMap_.end()) {
314         return true;
315     }
316     return false;
317 }
318 
RemovedGroupPrinter(const std::string & printerId)319 void VendorWlanGroup::RemovedGroupPrinter(const std::string &printerId)
320 {
321     std::lock_guard<std::mutex> lock(groupPrinterIdMapMutex);
322     groupPrinterIdMap_.erase(printerId);
323 }
324 
ConvertPrinterInfoId(const PrinterInfo & printerInfo)325 PrinterInfo VendorWlanGroup::ConvertPrinterInfoId(const PrinterInfo &printerInfo)
326 {
327     PrinterInfo info(printerInfo);
328     if (info.HasPrinterUuid()) {
329         PRINT_HILOGD("convert printerId by uuid: %{private}s", info.GetPrinterUuid().c_str());
330         UpdateGroupPrinter(info.GetPrinterId(), info.GetPrinterUuid());
331         info.SetPrinterId(info.GetPrinterUuid());
332         return info;
333     }
334     PRINT_HILOGW("uuid is not exited, not convert printerId");
335     UpdateGroupPrinter(printerInfo.GetPrinterId(), printerInfo.GetPrinterId());
336     return printerInfo;
337 }
338 
ConvertIpPrinterName(const PrinterInfo & printerInfo)339 PrinterInfo VendorWlanGroup::ConvertIpPrinterName(const PrinterInfo &printerInfo)
340 {
341     PrinterInfo info(printerInfo);
342     PRINT_HILOGI("ConvertIpPrinterName printerId : %{private}s, printerName : %{private}s",
343         info.GetPrinterId().c_str(), info.GetPrinterName().c_str());
344     info.SetPrinterName(info.GetPrinterId());
345     return info;
346 }
347 
ExtractPrinterIdByPrinterInfo(const PrinterInfo & printerInfo)348 std::string VendorWlanGroup::ExtractPrinterIdByPrinterInfo(const PrinterInfo &printerInfo)
349 {
350     std::string uri(printerInfo.GetUri());
351     if (uri.empty()) {
352         return "";
353     }
354     auto pos_start = uri.find_first_of(VENDOR_BSUNI_URI_START);
355     auto pos_end = uri.find_last_of(VENDOR_BSUNI_URI_END);
356     if (pos_start == std::string::npos || uri.length() <= pos_start + VENDOR_BSUNI_URI_START.length() ||
357         pos_end - pos_start <= VENDOR_BSUNI_URI_START.length()) {
358         return "";
359     }
360     std::string printerId = uri.substr(pos_start + VENDOR_BSUNI_URI_START.length(),
361         pos_end - pos_start - VENDOR_BSUNI_URI_START.length());
362     return printerId;
363 }
364 
MonitorPrinterStatus(const std::string & groupPrinterId,bool on)365 bool VendorWlanGroup::MonitorPrinterStatus(const std::string &groupPrinterId, bool on)
366 {
367     if (parentVendorManager == nullptr) {
368         PRINT_HILOGE("VendorManager is null.");
369         return false;
370     }
371     if (QueryVendorDriverByGroupPrinterId(groupPrinterId) == VENDOR_IPP_EVERYWHERE) {
372         auto ippEverywhereDriver = parentVendorManager->FindDriverByVendorName(VENDOR_IPP_EVERYWHERE);
373         if (ippEverywhereDriver != nullptr) {
374             PRINT_HILOGI("start MonitorPrinterStatus by ippEverywhere");
375             return ippEverywhereDriver->MonitorPrinterStatus(groupPrinterId, on);
376         }
377     } else {
378         PrinterInfo printerInfo;
379         auto ret = parentVendorManager->QueryPrinterInfoByPrinterId(GetVendorName(), groupPrinterId, printerInfo);
380         if (ret != E_PRINT_NONE) {
381             PRINT_HILOGE("get printerInfo failed.");
382             return false;
383         }
384         auto bsuniDriver = parentVendorManager->FindDriverByVendorName(VENDOR_BSUNI_DRIVER);
385         if (bsuniDriver != nullptr) {
386             PRINT_HILOGI("start MonitorPrinterStatus by bsuni");
387             return bsuniDriver->MonitorPrinterStatus(ExtractPrinterIdByPrinterInfo(printerInfo), on);
388         }
389     }
390     return false;
391 }
392 
OnPrinterStatusChanged(const std::string & vendorName,const std::string & printerId,const PrinterVendorStatus & status)393 bool VendorWlanGroup::OnPrinterStatusChanged(const std::string &vendorName, const std::string &printerId,
394                                              const PrinterVendorStatus &status)
395 {
396     if (parentVendorManager == nullptr) {
397         PRINT_HILOGE("VendorManager is null.");
398         return false;
399     }
400     std::string groupPrinterId = GetGroupPrinterId(printerId);
401     return parentVendorManager->OnPrinterStatusChanged(GetVendorName(), groupPrinterId, status);
402 }
403 
404