1 /*
2 * Copyright (c) 2025 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 "scan_context.h"
17 #include "scan_manager_client.h"
18 #include "scan_callback.h"
19 #include "scan_log.h"
20 #include "securec.h"
21 #include "scan_option_value.h"
22 #include "scan_util.h"
23
24 namespace OHOS::Scan {
25 constexpr int32_t YES_VALUE = 1;
26 constexpr int32_t NO_VALUE = 0;
27
ScanContext()28 ScanContext::ScanContext() {}
29
~ScanContext()30 ScanContext::~ScanContext()
31 {
32 Clear();
33 }
34
GetInstance()35 ScanContext& ScanContext::GetInstance()
36 {
37 static ScanContext instance;
38 return instance;
39 }
40
ExecuteCallback(const std::vector<ScanDeviceInfo> & infos)41 void ScanContext::ExecuteCallback(const std::vector<ScanDeviceInfo>& infos)
42 {
43 std::lock_guard<std::mutex> lock(mutex_);
44 if (discoverCallback_ == nullptr) {
45 SCAN_HILOGE("discoverCallback_ is a nullptr");
46 return;
47 }
48 int32_t deviceCount = static_cast<int32_t>(infos.size());
49 if (deviceCount == 0) {
50 SCAN_HILOGD("not found devices");
51 discoverCallback_(nullptr, 0);
52 return;
53 }
54 constexpr int32_t maxDeviceCount = 1000;
55 if (deviceCount > maxDeviceCount) {
56 SCAN_HILOGE("deviceCount [%{public}d] exceeded the maximum value", deviceCount);
57 }
58 Scan_ScannerDevice** devices = new (std::nothrow) Scan_ScannerDevice* [deviceCount]{};
59 if (devices == nullptr) {
60 SCAN_HILOGE("devices is a nullptr");
61 discoverCallback_(nullptr, 0);
62 }
63 for (int i = 0; i < deviceCount; i++) {
64 Scan_ScannerDevice* device = new (std::nothrow) Scan_ScannerDevice();
65 if (device == nullptr) {
66 SCAN_HILOGE("devices is a nullptr");
67 deviceCount = i;
68 break;
69 }
70 device->scannerId = infos[i].GetDeviceId().c_str();
71 device->manufacturer = infos[i].GetManufacturer().c_str();
72 device->model = infos[i].GetModel().c_str();
73 device->serialNumber = infos[i].GetSerialNumber().c_str();
74 device->discoverMode = infos[i].GetDiscoverMode().c_str();
75 devices[i] = device;
76 }
77 discoverCallback_(devices, deviceCount);
78 for (int32_t i = 0; i < deviceCount; i++) {
79 DELETE_AND_NULLIFY(devices[i]);
80 }
81 DELETE_ARRAY_AND_NULLIFY(devices);
82 }
83
GetScannerParaCount(const std::string & deviceId,int32_t & scannerParaCount)84 int32_t ScanContext::GetScannerParaCount(const std::string& deviceId, int32_t& scannerParaCount)
85 {
86 auto client = ScanManagerClient::GetInstance();
87 if (client == nullptr) {
88 SCAN_HILOGE("client is a nullptr");
89 return SCAN_ERROR_GENERIC_FAILURE;
90 }
91 ScanOptionDescriptor desc;
92 int32_t ret = client->GetScanOptionDesc(deviceId, 0, desc);
93 if (ret != E_SCAN_NONE) {
94 SCAN_HILOGE("GetScanOptionDesc failed, ErrorCode: [%{public}d]", ret);
95 return StatusConvert(ret);
96 }
97 uint32_t optionType = desc.GetOptionType();
98 ScanOptionValue value;
99 value.SetScanOptionValueType(static_cast<ScanOptionValueType>(optionType));
100 ret = client->OpScanOptionValue(deviceId, 0, SCAN_ACTION_GET_VALUE, value);
101 if (ret != E_SCAN_NONE) {
102 SCAN_HILOGE("OpScanOptionValue failed, ErrorCode: [%{public}d]", ret);
103 return StatusConvert(ret);
104 }
105 scannerParaCount = value.GetNumValue();
106 return SCAN_ERROR_NONE;
107 }
108
ParaIndexConvert(const int32_t option,int32_t & innerOption,const std::string & deviceId)109 bool ScanContext::ParaIndexConvert(const int32_t option, int32_t& innerOption, const std::string& deviceId)
110 {
111 std::lock_guard<std::mutex> lock(mutex_);
112 auto it = innerScanParaTables_.find(deviceId);
113 if (it == innerScanParaTables_.end()) {
114 SCAN_HILOGE("cannot find deviceId [%{public}s] in innerScanParaTables", deviceId.c_str());
115 return false;
116 }
117 auto& tablePtr = it->second;
118 if (tablePtr == nullptr) {
119 SCAN_HILOGE("tablePtr is a nullptr");
120 return false;
121 }
122 auto& indexMap = tablePtr->indexMap;
123 auto indexMapIt = indexMap.find(option);
124 if (indexMapIt == indexMap.end()) {
125 SCAN_HILOGE("cannot find option [%{public}d] in indexMap", option);
126 return false;
127 }
128 innerOption = indexMapIt->second;
129 return true;
130 }
131
GetScannerParameter(const std::string & deviceId,int32_t scannerParaCount,ScanParaTable & paraTable)132 int32_t ScanContext::GetScannerParameter(const std::string &deviceId,
133 int32_t scannerParaCount, ScanParaTable ¶Table)
134 {
135 int32_t buffLength = 0;
136 for (int i = 1; i < scannerParaCount; i++) {
137 ScanOptionDescriptor desc;
138 auto client = ScanManagerClient::GetInstance();
139 if (client == nullptr) {
140 SCAN_HILOGE("client is a nullptr");
141 return SCAN_ERROR_GENERIC_FAILURE;
142 }
143 int32_t ret = client->GetScanOptionDesc(deviceId, i, desc);
144 if (ret != SCAN_ERROR_NONE) {
145 SCAN_HILOGE("GetScanOptionDesc failed, ErrorCode: [%{public}d]", ret);
146 return StatusConvert(ret);
147 }
148 if (SetParaTable(desc, paraTable, buffLength)) {
149 paraTable.indexMap.insert({buffLength, i});
150 buffLength++;
151 }
152 }
153 paraTable.buffLength = buffLength;
154 return SCAN_ERROR_NONE;
155 }
156
SetRangeStrInParaTable(const ScanOptionDescriptor & desc,ScanParaTable & paraTable,const int32_t & buffLength)157 std::string ScanContext::SetRangeStrInParaTable(const ScanOptionDescriptor& desc,
158 ScanParaTable& paraTable, const int32_t& buffLength)
159 {
160 std::string rangeStr;
161 uint32_t constraintType = desc.GetOptionConstraintType();
162 uint32_t optionType = desc.GetOptionType();
163 if (constraintType == SCAN_CONSTRAINT_WORD_LIST) {
164 std::vector<std::int32_t> optionConstraintNumber;
165 desc.GetOptionConstraintNumber(optionConstraintNumber);
166 for (auto t : optionConstraintNumber) {
167 std::string numStr = std::to_string(t);
168 rangeStr.append(numStr).append(",");
169 }
170 rangeStr.pop_back();
171 } else if (constraintType == SCAN_CONSTRAINT_STRING_LIST) {
172 std::vector<std::string> optionConstraintString;
173 desc.GetOptionConstraintString(optionConstraintString);
174 for (auto t : optionConstraintString) {
175 rangeStr.append(t).append(",");
176 }
177 rangeStr.pop_back();
178 } else if (constraintType == SCAN_CONSTRAINT_RANGE) {
179 ScanRange range;
180 desc.GetOptionConstraintRange(range);
181 int32_t minValue = range.GetMinValue();
182 int32_t maxValue = range.GetMaxValue();
183 int32_t quantValue = range.GetQuantValue();
184 if (quantValue != 0) {
185 minValue /= quantValue;
186 maxValue /= quantValue;
187 paraTable.quantMap[buffLength] = quantValue;
188 }
189 const std::string symbol = "..";
190 rangeStr.append(std::to_string(minValue).append(symbol).append(std::to_string(maxValue)));
191 } else if (constraintType == SCAN_CONSTRAINT_NONE && optionType == SCAN_VALUE_BOOL) {
192 rangeStr = "yes,no";
193 } else {
194 SCAN_HILOGD("not support ConstraintType[%{public}u], optionType[%{public}u]", constraintType, optionType);
195 return "";
196 }
197 return rangeStr;
198 }
199
GetPhysicalUnitDesc(uint32_t physicalUnit)200 std::string ScanContext::GetPhysicalUnitDesc(uint32_t physicalUnit)
201 {
202 static const std::map<uint32_t, std::string> physicalUnitDescMap = {
203 {SCANNER_UNIT_PIXEL, "(Physical unit is number of pixels)"},
204 {SCANNER_UNIT_BIT, "(Physical unit is number of bits)"},
205 {SCANNER_UNIT_MM, "(Physical unit is millimeters)"},
206 {SCANNER_UNIT_DPI, "(Physical unit is resolution in dots/inch)"},
207 {SCANNER_UNIT_PERCENT, "(Physical unit is a percentage)"},
208 {SCANNER_UNIT_MICROSECOND, "(Physical unit is micro seconds)"},
209 };
210 auto it = physicalUnitDescMap.find(physicalUnit);
211 if (it == physicalUnitDescMap.end()) {
212 SCAN_HILOGW("physicalUnit [%{public}u] is not exist", physicalUnit);
213 return "";
214 }
215 return it->second;
216 }
217
SetParaTable(ScanOptionDescriptor & desc,ScanParaTable & paraTable,int32_t & buffLength)218 bool ScanContext::SetParaTable(ScanOptionDescriptor& desc, ScanParaTable& paraTable, int32_t& buffLength)
219 {
220 std::string rangeStr = SetRangeStrInParaTable(desc, paraTable, buffLength);
221 if (rangeStr == "") {
222 SCAN_HILOGE("SetRangeStrInParaTable fail");
223 return false;
224 }
225 uint32_t optionType = desc.GetOptionType();
226 paraTable.optionTypeBuff.emplace_back(optionType);
227 paraTable.rangesBuff.emplace_back(rangeStr);
228 paraTable.titBuff.emplace_back(desc.GetOptionTitle());
229 std::string optionDesc = desc.GetOptionDesc();
230 uint32_t physicalUnit = desc.GetOptionUnit();
231 if (physicalUnit != SCANNER_UNIT_NONE) {
232 optionDesc.append(GetPhysicalUnitDesc(physicalUnit));
233 }
234 paraTable.desBuff.emplace_back(optionDesc);
235 return true;
236 }
237
GetOptionValueFromTable(const std::string & deviceId,int32_t option,const char * value,ScanOptionValue & optionValue)238 int32_t ScanContext::GetOptionValueFromTable(const std::string& deviceId, int32_t option,
239 const char* value, ScanOptionValue& optionValue)
240 {
241 auto* paraTable = GetScanParaTable(deviceId);
242 std::lock_guard<std::mutex> lock(mutex_);
243 if (paraTable == nullptr) {
244 SCAN_HILOGE("cannot find scannerId [%{private}s] in parameter tables.", deviceId.c_str());
245 return SCAN_ERROR_INVALID_PARAMETER;
246 }
247 if (value == nullptr) {
248 SCAN_HILOGE("value is a nullptr");
249 return SCAN_ERROR_INVALID_PARAMETER;
250 }
251 if (option < 0 || option >= paraTable->buffLength) {
252 SCAN_HILOGE("cannot find option [%{private}d] in parameter tables.", option);
253 return SCAN_ERROR_INVALID_PARAMETER;
254 }
255 uint32_t optionType = paraTable->optionTypeBuff[option];
256 optionValue.SetScanOptionValueType(static_cast<ScanOptionValueType>(optionType));
257 if (optionType == SCAN_VALUE_STR) {
258 optionValue.SetStrValue(std::string(value));
259 } else if (optionType == SCAN_VALUE_BOOL) {
260 if (strcmp(value, "yes") == 0) {
261 optionValue.SetNumValue(YES_VALUE);
262 } else if (strcmp(value, "no") == 0) {
263 optionValue.SetNumValue(NO_VALUE);
264 } else {
265 SCAN_HILOGE("option [%{private}d] is invalid.", option);
266 return SCAN_ERROR_INVALID_PARAMETER;
267 }
268 } else {
269 int32_t numValue = 0;
270 if (!ScanUtil::ConvertToInt(std::string(value), numValue)) {
271 SCAN_HILOGE("option [%{private}d] is invalid.", option);
272 return SCAN_ERROR_INVALID_PARAMETER;
273 }
274 auto quantIt = paraTable->quantMap.find(option);
275 if (quantIt != paraTable->quantMap.end()) {
276 numValue *= quantIt->second;
277 }
278 optionValue.SetNumValue(numValue);
279 }
280 return SCAN_ERROR_NONE;
281 }
282
FreeScannerOptionsMemory(Scan_ScannerOptions * scannerOptions)283 void ScanContext::FreeScannerOptionsMemory(Scan_ScannerOptions* scannerOptions)
284 {
285 if (scannerOptions == nullptr) {
286 SCAN_HILOGW("scannerOptions is a nullptr.");
287 return;
288 }
289
290 if (scannerOptions->titles != nullptr) {
291 for (int i = 0; i < scannerOptions->optionCount; i++) {
292 DELETE_AND_NULLIFY(scannerOptions->titles[i])
293 }
294 DELETE_ARRAY_AND_NULLIFY(scannerOptions->titles)
295 }
296
297 if (scannerOptions->descriptions != nullptr) {
298 for (int i = 0; i < scannerOptions->optionCount; i++) {
299 DELETE_AND_NULLIFY(scannerOptions->descriptions[i])
300 }
301 DELETE_ARRAY_AND_NULLIFY(scannerOptions->descriptions)
302 }
303
304 if (scannerOptions->ranges != nullptr) {
305 for (int i = 0; i < scannerOptions->optionCount; i++) {
306 DELETE_AND_NULLIFY(scannerOptions->ranges[i])
307 }
308 DELETE_ARRAY_AND_NULLIFY(scannerOptions->ranges)
309 }
310
311 DELETE_AND_NULLIFY(scannerOptions)
312 }
313
CreateScannerOptions(int32_t & optionCount)314 Scan_ScannerOptions* ScanContext::CreateScannerOptions(int32_t &optionCount)
315 {
316 constexpr int32_t maxOptionCount = 1000;
317 if (optionCount > maxOptionCount) {
318 SCAN_HILOGE("optionCount [%{public}d] exceeded the maximum value", optionCount);
319 return nullptr;
320 }
321 Scan_ScannerOptions* scannerOptions = new (std::nothrow) Scan_ScannerOptions();
322 if (scannerOptions == nullptr) {
323 SCAN_HILOGE("scannerOptions is a nullptr");
324 return nullptr;
325 }
326 int32_t scannerOptionsMemSize = sizeof(Scan_ScannerOptions);
327 if (memset_s(scannerOptions, scannerOptionsMemSize, 0, scannerOptionsMemSize) != 0) {
328 SCAN_HILOGW("memset_s fail");
329 FreeScannerOptionsMemory(scannerOptions);
330 return nullptr;
331 }
332 scannerOptions->titles = new (std::nothrow) char* [optionCount];
333 scannerOptions->descriptions = new (std::nothrow) char* [optionCount];
334 scannerOptions->ranges = new (std::nothrow) char* [optionCount];
335 scannerOptions->optionCount = optionCount;
336 if (scannerOptions->titles == nullptr || scannerOptions->descriptions == nullptr ||
337 scannerOptions->ranges == nullptr) {
338 FreeScannerOptionsMemory(scannerOptions);
339 return nullptr;
340 }
341 int32_t stringMemSize = optionCount * sizeof(char*);
342 if (memset_s(scannerOptions->titles, stringMemSize, 0, stringMemSize) != 0 ||
343 memset_s(scannerOptions->descriptions, stringMemSize, 0, stringMemSize) != 0 ||
344 memset_s(scannerOptions->ranges, stringMemSize, 0, stringMemSize) != 0) {
345 SCAN_HILOGW("memset_s fail");
346 FreeScannerOptionsMemory(scannerOptions);
347 return nullptr;
348 }
349 return scannerOptions;
350 }
351
CopySingleBuf(char * destBuf,const char * srcBuf,size_t bufferSize)352 bool ScanContext::CopySingleBuf(char* destBuf, const char* srcBuf, size_t bufferSize)
353 {
354 if (destBuf == nullptr || srcBuf == nullptr) {
355 SCAN_HILOGW("CopySingleBuf new fail");
356 return false;
357 }
358 if (memset_s(destBuf, bufferSize, 0, bufferSize) != 0) {
359 SCAN_HILOGE("CopySingleBuf memset_s fail");
360 return false;
361 }
362 if (strncpy_s(destBuf, bufferSize, srcBuf, bufferSize) != 0) {
363 SCAN_HILOGE("CopySingleBuf strncpy_s fail");
364 return false;
365 }
366
367 return true;
368 }
369
MemSetScannerOptions(Scan_ScannerOptions * scannerOptions,int32_t & optionCount,ScanParaTable & paraTable)370 bool ScanContext::MemSetScannerOptions(Scan_ScannerOptions* scannerOptions,
371 int32_t &optionCount, ScanParaTable ¶Table)
372 {
373 for (int i = 0; i < optionCount; i++) {
374 auto bufferSize = paraTable.titBuff[i].length() + 1;
375 char* titBuff = new (std::nothrow) char[bufferSize];
376 if (!CopySingleBuf(titBuff, paraTable.titBuff[i].c_str(), bufferSize)) {
377 if (titBuff != nullptr) {
378 delete[] titBuff;
379 }
380 return false;
381 }
382 scannerOptions->titles[i] = titBuff;
383
384 bufferSize = paraTable.desBuff[i].length() + 1;
385 char* desBuff = new (std::nothrow) char[bufferSize];
386 if (!CopySingleBuf(desBuff, paraTable.desBuff[i].c_str(), bufferSize)) {
387 if (desBuff != nullptr) {
388 delete[] desBuff;
389 }
390 return false;
391 }
392 scannerOptions->descriptions[i] = desBuff;
393
394 bufferSize = paraTable.rangesBuff[i].length() + 1;
395 char* rangesBuff = new (std::nothrow) char[bufferSize];
396 if (!CopySingleBuf(rangesBuff, paraTable.rangesBuff[i].c_str(), bufferSize)) {
397 if (rangesBuff != nullptr) {
398 delete[] rangesBuff;
399 }
400 return false;
401 }
402 scannerOptions->ranges[i] = rangesBuff;
403 }
404 return true;
405 }
406
ConvertToScannerOptions(ScanParaTable & paraTable)407 Scan_ScannerOptions* ScanContext::ConvertToScannerOptions(ScanParaTable ¶Table)
408 {
409 int32_t optionCount = paraTable.buffLength;
410 if (optionCount <= 0) {
411 SCAN_HILOGE("optionCount is less than 0");
412 return nullptr;
413 }
414 Scan_ScannerOptions* scannerOptions = CreateScannerOptions(optionCount);
415 if (scannerOptions == nullptr) {
416 SCAN_HILOGE("scannerOptions is a nullptr");
417 return nullptr;
418 }
419 if (!MemSetScannerOptions(scannerOptions, optionCount, paraTable)) {
420 SCAN_HILOGE("MemSetScannerOptions error");
421 FreeScannerOptionsMemory(scannerOptions);
422 return nullptr;
423 }
424 return scannerOptions;
425 }
426
StatusConvert(int32_t status)427 int32_t ScanContext::StatusConvert(int32_t status)
428 {
429 static const std::map<uint32_t, uint32_t> errorCodeMap = {
430 {E_SCAN_GENERIC_FAILURE, SCAN_ERROR_GENERIC_FAILURE},
431 {E_SCAN_RPC_FAILURE, SCAN_ERROR_RPC_FAILURE},
432 {E_SCAN_SERVER_FAILURE, SCAN_ERROR_SERVER_FAILURE},
433 {E_SCAN_UNSUPPORTED, SCAN_ERROR_UNSUPPORTED},
434 {E_SCAN_CANCELLED, SCAN_ERROR_CANCELED},
435 {E_SCAN_DEVICE_BUSY, SCAN_ERROR_DEVICE_BUSY},
436 {E_SCAN_INVAL, SCAN_ERROR_INVALID},
437 {E_SCAN_JAMMED, SCAN_ERROR_JAMMED},
438 {E_SCAN_NO_DOCS, SCAN_ERROR_NO_DOCS},
439 {E_SCAN_COVER_OPEN, SCAN_ERROR_COVER_OPEN},
440 {E_SCAN_IO_ERROR, SCAN_ERROR_IO_ERROR},
441 {E_SCAN_NO_MEM, SCAN_ERROR_NO_MEMORY},
442 {E_SCAN_ACCESS_DENIED, SCAN_ERROR_INVALID}
443 };
444 auto it = errorCodeMap.find(status);
445 if (it != errorCodeMap.end()) {
446 return it->second;
447 } else {
448 SCAN_HILOGE("StatusConvert failed, status: [%{public}d]", status);
449 return E_SCAN_GENERIC_FAILURE;
450 }
451 }
452
Clear()453 void ScanContext::Clear()
454 {
455 std::lock_guard<std::mutex> lock(mutex_);
456 for (auto& pair : exScanParaTables_) {
457 FreeScannerOptionsMemory(pair.second);
458 }
459 exScanParaTables_.clear();
460 innerScanParaTables_.clear();
461 discoverCallback_ = nullptr;
462 }
463
GetScanParaTable(const std::string & scannerId)464 ScanParaTable* ScanContext::GetScanParaTable(const std::string& scannerId)
465 {
466 std::lock_guard<std::mutex> lock(mutex_);
467 auto it = innerScanParaTables_.find(scannerId);
468 return it != innerScanParaTables_.end() ? it->second.get() : nullptr;
469 }
470
SetScanParaTable(const std::string & scannerId,std::unique_ptr<ScanParaTable> table)471 void ScanContext::SetScanParaTable(const std::string& scannerId, std::unique_ptr<ScanParaTable> table)
472 {
473 std::lock_guard<std::mutex> lock(mutex_);
474 innerScanParaTables_[scannerId] = std::move(table);
475 }
476
GetScannerOptions(const std::string & scannerId)477 Scan_ScannerOptions* ScanContext::GetScannerOptions(const std::string& scannerId)
478 {
479 std::lock_guard<std::mutex> lock(mutex_);
480 auto it = exScanParaTables_.find(scannerId);
481 return it != exScanParaTables_.end() ? it->second : nullptr;
482 }
483
SetScannerOptions(const std::string & scannerId,Scan_ScannerOptions * options)484 void ScanContext::SetScannerOptions(const std::string& scannerId, Scan_ScannerOptions* options)
485 {
486 std::lock_guard<std::mutex> lock(mutex_);
487 exScanParaTables_[scannerId] = options;
488 }
489
SetDiscoverCallback(Scan_ScannerDiscoveryCallback callback)490 void ScanContext::SetDiscoverCallback(Scan_ScannerDiscoveryCallback callback)
491 {
492 std::lock_guard<std::mutex> lock(mutex_);
493 discoverCallback_ = callback;
494 }
495
GetRegisterType()496 const std::string& ScanContext::GetRegisterType()
497 {
498 static const std::string discoveryRegistryType = "GET_SCANNER_DEVICE_LIST";
499 return discoveryRegistryType;
500 }
501 } // namespace OHOS::Scan