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_ipp_everywhere.h"
17 #include "print_log.h"
18 #include <json/json.h>
19 #include "print_json_util.h"
20
21 using namespace OHOS::Print;
22 namespace {
23 const std::string VENDOR_IPP_START = "ipp://";
24 const std::string VENDOR_IPP_END = ":631/ipp/print";
25 }
26
27 static const std::string VENDOR_NAME = "driver.ipp.everywhere";
28
VendorIppEveryWhere()29 VendorIppEveryWhere::VendorIppEveryWhere() {}
30
~VendorIppEveryWhere()31 VendorIppEveryWhere::~VendorIppEveryWhere() {}
32
GetVendorName()33 std::string VendorIppEveryWhere::GetVendorName()
34 {
35 return VENDOR_IPP_EVERYWHERE;
36 }
37
Init(IPrinterVendorManager * manager)38 bool VendorIppEveryWhere::Init(IPrinterVendorManager *manager)
39 {
40 if (!VendorDriverBase::Init(manager)) {
41 PRINT_HILOGD("VendorDriverBase init fail");
42 return false;
43 }
44 return true;
45 }
UnInit()46 void VendorIppEveryWhere::UnInit()
47 {
48 VendorDriverBase::UnInit();
49 }
OnCreate()50 void VendorIppEveryWhere::OnCreate()
51 {
52 opQueue.Run();
53 }
OnDestroy()54 void VendorIppEveryWhere::OnDestroy()
55 {
56 opQueue.Stop();
57 }
OnStartDiscovery()58 void VendorIppEveryWhere::OnStartDiscovery() {}
OnStopDiscovery()59 void VendorIppEveryWhere::OnStopDiscovery() {}
60
OnQueryCapability(const std::string & printerId,int timeout)61 bool VendorIppEveryWhere::OnQueryCapability(const std::string &printerId, int timeout)
62 {
63 if (vendorManager == nullptr) {
64 PRINT_HILOGW("vendorManager is null");
65 return false;
66 }
67 auto printerInfo = vendorManager->QueryDiscoveredPrinterInfoById(GetVendorName(), printerId);
68 if (printerInfo == nullptr) {
69 PRINT_HILOGW("invalid printerId");
70 return false;
71 }
72 std::string printerUri(printerInfo->GetUri());
73 if (printerUri.find(VENDOR_IPP_START) == std::string::npos) {
74 PRINT_HILOGW("ipp everywhere not support");
75 return false;
76 }
77 auto op = std::bind(&VendorIppEveryWhere::ConnectPrinterByPrinterIdAndUri, this, printerId, printerUri);
78 return opQueue.Push(op);
79 }
80
OnQueryCapabilityByIp(const std::string & printerIp,const std::string & protocol)81 bool VendorIppEveryWhere::OnQueryCapabilityByIp(const std::string &printerIp, const std::string &protocol)
82 {
83 if (protocol != "auto" && protocol != "ipp") {
84 PRINT_HILOGW("protocol not support");
85 return false;
86 }
87 PRINT_HILOGI("QueryCapabilityByIp begin");
88 std::string uri = "ipp://" + printerIp + ":631/ipp/print";
89 auto op = std::bind(&VendorIppEveryWhere::ConnectPrinterByUri, this, uri);
90 return opQueue.Push(op);
91 }
92
OnQueryProperties(const std::string & printerId,const std::vector<std::string> & propertyKeys)93 bool VendorIppEveryWhere::OnQueryProperties(const std::string &printerId, const std::vector<std::string> &propertyKeys)
94 {
95 bool ret = false;
96 for (auto const &key : propertyKeys) {
97 if (key == PRINTER_PROPERTY_KEY_DEVICE_STATE) {
98 auto op = std::bind(&VendorIppEveryWhere::QueryPrinterStatusByUri, this, printerId);
99 if (opQueue.Push(op)) {
100 ret = true;
101 }
102 }
103 }
104 return ret;
105 }
106
QueryCapabilityByUri(const std::string & uri)107 void VendorIppEveryWhere::QueryCapabilityByUri(const std::string &uri)
108 {
109 PRINT_HILOGI("QueryCapabilityByUri enter");
110 auto printerInfo = QueryPrinterInfoByUri(uri);
111 if (!UpdateCapability(printerInfo)) {
112 PRINT_HILOGW("update capability fail");
113 return;
114 }
115 PRINT_HILOGI("QueryCapabilityByUri quit");
116 }
117
ConnectPrinterByUri(const std::string & uri)118 void VendorIppEveryWhere::ConnectPrinterByUri(const std::string &uri)
119 {
120 PRINT_HILOGI("ConnectPrinterByUri enter");
121 auto printerInfo = QueryPrinterInfoByUri(uri);
122 if (!ConnectPrinter(printerInfo)) {
123 PRINT_HILOGW("connect fail");
124 return;
125 }
126 PRINT_HILOGI("ConnectPrinterByUri quit");
127 }
128
ConnectPrinterByPrinterIdAndUri(const std::string & printerId,const std::string & uri)129 void VendorIppEveryWhere::ConnectPrinterByPrinterIdAndUri(const std::string &printerId, const std::string &uri)
130 {
131 PRINT_HILOGI("ConnectPrinterByPrinterIdAndUri enter");
132 auto printerInfo = QueryPrinterInfoByUri(uri);
133 if (printerInfo == nullptr) {
134 PRINT_HILOGW("connect fail");
135 return;
136 }
137 printerInfo->SetPrinterId(printerId);
138 if (!ConnectPrinter(printerInfo)) {
139 PRINT_HILOGW("connect fail");
140 return;
141 }
142 PRINT_HILOGI("ConnectPrinterByPrinterIdAndUri quit");
143 }
144
UpdateCapability(std::shared_ptr<PrinterInfo> printerInfo)145 bool VendorIppEveryWhere::UpdateCapability(std::shared_ptr<PrinterInfo> printerInfo)
146 {
147 if (vendorManager == nullptr) {
148 PRINT_HILOGW("vendorManager is null");
149 return false;
150 }
151 if (printerInfo == nullptr) {
152 PRINT_HILOGW("printerInfo fail");
153 return false;
154 }
155 PRINT_HILOGI("get printer info success");
156 if (vendorManager->UpdatePrinterToDiscovery(GetVendorName(), *printerInfo) != EXTENSION_ERROR_NONE) {
157 PRINT_HILOGW("UpdatePrinterToDiscovery fail");
158 return false;
159 }
160 return true;
161 }
162
ConnectPrinter(std::shared_ptr<PrinterInfo> printerInfo)163 bool VendorIppEveryWhere::ConnectPrinter(std::shared_ptr<PrinterInfo> printerInfo)
164 {
165 if (vendorManager == nullptr) {
166 PRINT_HILOGW("vendorManager is null");
167 return false;
168 }
169 if (printerInfo == nullptr) {
170 PRINT_HILOGW("printer info fail");
171 return false;
172 }
173 PRINT_HILOGI("get printer info success");
174 auto discoveredInfo = vendorManager->QueryDiscoveredPrinterInfoById(GetVendorName(), printerInfo->GetPrinterId());
175 if (discoveredInfo != nullptr) {
176 printerInfo->SetPrinterName(discoveredInfo->GetPrinterName());
177 }
178 if (vendorManager->UpdatePrinterToDiscovery(GetVendorName(), *printerInfo) != EXTENSION_ERROR_NONE) {
179 PRINT_HILOGW("UpdatePrinterToDiscovery fail");
180 return false;
181 }
182 PRINT_HILOGI("UpdatePrinterToDiscovery success");
183 if (vendorManager->AddPrinterToCupsWithPpd(GetVendorName(), printerInfo->GetPrinterId(), DEFAULT_PPD_NAME, "") !=
184 EXTENSION_ERROR_NONE) {
185 PRINT_HILOGW("AddPrinterToCupsWithPpd fail");
186 return false;
187 }
188 return true;
189 }
190
QueryPrinterInfoByUri(const std::string & uri)191 std::shared_ptr<PrinterInfo> VendorIppEveryWhere::QueryPrinterInfoByUri(const std::string &uri)
192 {
193 if (vendorManager == nullptr) {
194 PRINT_HILOGW("vendorManager is null");
195 return nullptr;
196 }
197 PrinterCapability printerCap;
198 if (!vendorManager->QueryPrinterCapabilityByUri(uri, printerCap)) {
199 PRINT_HILOGW("QueryPrinterCapabilityByUri fail");
200 return nullptr;
201 }
202 PRINT_HILOGI("QueryPrinterCapabilityByUri success");
203 return ConvertCapabilityToInfo(printerCap, uri);
204 }
205
ConvertCapabilityToInfo(const PrinterCapability & printerCap,const std::string & printerUri)206 std::shared_ptr<PrinterInfo> VendorIppEveryWhere::ConvertCapabilityToInfo(const PrinterCapability &printerCap,
207 const std::string &printerUri)
208 {
209 if (!printerCap.HasOption()) {
210 PRINT_HILOGW("empty option");
211 return nullptr;
212 }
213 std::string capOption = printerCap.GetOption();
214 Json::Value capJson;
215 if (!PrintJsonUtil::Parse(capOption, capJson)) {
216 PRINT_HILOGW("invalid option");
217 return nullptr;
218 }
219 if (!PrintJsonUtil::IsMember(capJson, "printerName") || !capJson["printerName"].isString()) {
220 PRINT_HILOGW("printerName invalid");
221 return nullptr;
222 }
223 std::string printerName = capJson["printerName"].asString();
224 if (!PrintJsonUtil::IsMember(capJson, "make") || !capJson["make"].isString()) {
225 PRINT_HILOGW("make invalid");
226 return nullptr;
227 }
228 std::string printerMaker = capJson["make"].asString();
229 std::shared_ptr<PrinterInfo> printerInfo = std::make_shared<PrinterInfo>();
230 printerInfo->SetPrinterName(printerName);
231 printerInfo->SetUri(printerUri);
232 printerInfo->SetPrinterMake(printerMaker);
233 printerInfo->SetPrinterId(printerUri);
234 printerInfo->SetPrinterState(PRINTER_UPDATE_CAP);
235 printerInfo->SetCapability(printerCap);
236 Json::Value option;
237 option["printerName"] = printerName;
238 option["printerUri"] = printerUri;
239 option["make"] = printerMaker;
240 printerInfo->SetOption(PrintJsonUtil::WriteString(option));
241 return printerInfo;
242 }
243
QueryPrinterStatusByUri(const std::string & uri)244 void VendorIppEveryWhere::QueryPrinterStatusByUri(const std::string &uri)
245 {
246 if (vendorManager == nullptr) {
247 PRINT_HILOGW("vendorManager is null");
248 return;
249 }
250 PrinterStatus status = PRINTER_STATUS_UNAVAILABLE;
251 if (!vendorManager->QueryPrinterStatusByUri(uri, status)) {
252 return;
253 }
254 OnPrinterStateQueried(uri, static_cast<Print_PrinterState>(status));
255 }
256
ConvertPrinterIdByUri(std::string & uri)257 bool VendorIppEveryWhere::ConvertPrinterIdByUri(std::string &uri)
258 {
259 if (uri.empty()) {
260 return false;
261 }
262 auto pos_start = uri.find(VENDOR_IPP_START);
263 auto pos_end = uri.find(VENDOR_IPP_END);
264 if (pos_start == std::string::npos || uri.length() <= pos_start + VENDOR_IPP_START.length() ||
265 pos_end == std::string::npos || pos_end - pos_start <= VENDOR_IPP_START.length()) {
266 return false;
267 }
268 uri = uri.substr(pos_start + VENDOR_IPP_START.length(), pos_end - pos_start - VENDOR_IPP_START.length());
269 PRINT_HILOGD("ConvertPrinterIdByUri seccess");
270 return true;
271 }
272