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 #include <set>
16 #include <map>
17 #include <cstring>
18 #include <string>
19 #include <thread>
20 #include "securec.h"
21 #include "refbase.h"
22 #include "scan_callback.h"
23 #include "scan_manager_client.h"
24 #include "scan_constant.h"
25 #include "scan_log.h"
26 #include "scanner_info.h"
27 #include "scan_option_value.h"
28 #include "ohscan.h"
29
30 using namespace OHOS::Scan;
31
32 struct ValueMap {
33 uint32_t valueType;
34 int32_t optionIndex;
35 std::set<std::string> numList;
36 std::set<std::string> strList;
37 };
38
39 struct ScanParaTable {
40 std::vector<std::string> titBuff;
41 std::vector<std::string> desBuff;
42 std::vector<std::string> rangesBuff;
43 int32_t lengthBuff;
44 };
45
46 static constexpr int32_t SCAN_INT_TYPE = 1;
47 static constexpr int32_t SCAN_STRING_TYPE = 3;
48 static constexpr int32_t SCAN_NUM_LIST = 2;
49 static constexpr int32_t SCAN_STRING_LIST = 3;
50 static std::map<std::string, std::map<int, ValueMap>> g_valueMap;
51 static std::map<std::string, Scan_ScannerOptions* > g_scanParaTables;
52 static bool g_isListening = false;
53 static const char* GET_SCANNER_DEVICE_LIST = "GET_SCANNER_DEVICE_LIST";
54 static Scan_ScannerDiscoveryCallback g_discoverCallback = nullptr;
55
56
FreeDeviceListMemory(Scan_ScannerDevice ** devices,int32_t deviceCount)57 static inline void FreeDeviceListMemory(Scan_ScannerDevice** devices, int32_t deviceCount)
58 {
59 for (int32_t i = 0; i < deviceCount; i++) {
60 DELETE_AND_NULLIFY(devices[i])
61 }
62 DELETE_ARRAY_AND_NULLIFY(devices)
63 }
64
__anona70cfa310102(std::vector<ScanDeviceInfo> &infos) 65 auto callbackFunction = [](std::vector<ScanDeviceInfo> &infos) {
66 int32_t deviceCount = infos.size();
67 SCAN_HILOGI("deviceCount : [%{public}d]", deviceCount);
68 if (deviceCount == 0) {
69 SCAN_HILOGE("not found");
70 g_discoverCallback(nullptr, 0);
71 return;
72 }
73 Scan_ScannerDevice** devices = new (std::nothrow) Scan_ScannerDevice* [deviceCount];
74 if (devices == nullptr) {
75 SCAN_HILOGE("devices is a nullptr");
76 g_discoverCallback(nullptr, 0);
77 return;
78 }
79 int32_t devicesMemSize = deviceCount * sizeof(Scan_ScannerDevice*);
80 if (memset_s(devices, devicesMemSize, 0, devicesMemSize) != 0) {
81 SCAN_HILOGW("memset_s fail");
82 FreeDeviceListMemory(devices, 0);
83 g_discoverCallback(nullptr, 0);
84 return;
85 }
86 for (int i = 0; i < deviceCount; i++) {
87 Scan_ScannerDevice* device = new (std::nothrow) Scan_ScannerDevice();
88 if (device == nullptr) {
89 SCAN_HILOGE("devices is a nullptr");
90 deviceCount = i;
91 break;
92 }
93 if (memset_s(device, sizeof(Scan_ScannerDevice), 0, sizeof(Scan_ScannerDevice)) != 0) {
94 SCAN_HILOGW("memset_s fail");
95 deviceCount = i;
96 break;
97 }
98 device->scannerId = infos[i].GetDeviceId().c_str();
99 device->manufacturer = infos[i].GetManufacturer().c_str();
100 device->model = infos[i].GetModel().c_str();
101 device->serialNumber = infos[i].GetSerialNumber().c_str();
102 device->discoverMode = infos[i].GetDiscoverMode().c_str();
103 devices[i] = device;
104 }
105 g_discoverCallback(devices, deviceCount);
106 FreeDeviceListMemory(devices, deviceCount);
107 };
108
109 namespace {
GetScanParaDesc(const std::string & deviceId,ScanOptionValue & value)110 int32_t GetScanParaDesc(const std::string &deviceId, ScanOptionValue &value)
111 {
112 auto client = ScanManagerClient::GetInstance();
113 ScanOptionDescriptor desc;
114 int32_t ret = client->GetScanOptionDesc(deviceId, 0, desc);
115 uint32_t optionType = desc.GetOptionType();
116 int32_t optionSize = desc.GetOptionSize();
117 value.SetScanOptionValueType(static_cast<ScanOptionValueType>(optionType));
118 value.SetValueSize(optionSize);
119 int32_t info = 0;
120 ret = client->OpScanOptionValue(deviceId, 0, SCAN_ACTION_GET_VALUE, value, info);
121 return ret;
122 }
123
GetScanParaValues(const std::string & deviceId,ScanOptionValue & value,ScanParaTable & paraTable)124 int32_t GetScanParaValues(const std::string &deviceId, ScanOptionValue &value, ScanParaTable ¶Table)
125 {
126 std::set<uint32_t> dataType = {SCAN_INT_TYPE, SCAN_STRING_TYPE};
127 int32_t lengthBuff = 0;
128 for (int i = 1 ; i < value.GetNumValue(); i++) {
129 ScanOptionDescriptor desc;
130 auto client = ScanManagerClient::GetInstance();
131 int32_t ret = client->GetScanOptionDesc(deviceId, i, desc);
132 if (ret != SCAN_ERROR_NONE) {
133 SCAN_HILOGE("Failed to get scanner parameters.");
134 return ret;
135 }
136 if (!dataType.count(desc.GetOptionType())) {
137 continue;
138 }
139 if (desc.GetOptionConstraintType() == SCAN_NUM_LIST) {
140 std::string tmp;
141 std::vector<std::int32_t> optionConstraintNumber;
142 desc.GetOptionConstraintNumber(optionConstraintNumber);
143 for (auto t : optionConstraintNumber) {
144 std::string numStr = std::to_string(t);
145 tmp.append(numStr).append(",");
146 g_valueMap[deviceId][lengthBuff].numList.insert(numStr);
147 g_valueMap[deviceId][lengthBuff].valueType = SCAN_INT_TYPE;
148 }
149 tmp.pop_back();
150 paraTable.rangesBuff.emplace_back(tmp);
151 } else if (desc.GetOptionConstraintType() == SCAN_STRING_LIST) {
152 std::string tmp;
153 std::vector<std::string> optionConstraintString;
154 desc.GetOptionConstraintString(optionConstraintString);
155 for (auto t : optionConstraintString) {
156 tmp.append(t).append(",");
157 g_valueMap[deviceId][lengthBuff].strList.insert(t);
158 g_valueMap[deviceId][lengthBuff].valueType = SCAN_STRING_TYPE;
159 }
160 tmp.pop_back();
161 paraTable.rangesBuff.emplace_back(tmp);
162 } else {
163 continue;
164 }
165 paraTable.titBuff.emplace_back(desc.GetOptionTitle());
166 paraTable.desBuff.emplace_back(desc.GetOptionDesc());
167 g_valueMap[deviceId][lengthBuff].optionIndex = i;
168 lengthBuff++;
169 }
170 paraTable.lengthBuff = lengthBuff;
171 return SCAN_ERROR_NONE;
172 }
173
FreeScannerOptionsMemory(Scan_ScannerOptions * scannerOptions)174 void FreeScannerOptionsMemory(Scan_ScannerOptions* scannerOptions)
175 {
176 if (scannerOptions == nullptr) {
177 SCAN_HILOGW("scannerOptions is a nullptr.");
178 return;
179 }
180
181 for (int i = 0; i < scannerOptions->optionCount; i++) {
182 DELETE_AND_NULLIFY(scannerOptions->titles[i])
183 }
184 DELETE_ARRAY_AND_NULLIFY(scannerOptions->titles)
185
186 for (int i = 0; i < scannerOptions->optionCount; i++) {
187 DELETE_AND_NULLIFY(scannerOptions->descriptions[i])
188 }
189 DELETE_ARRAY_AND_NULLIFY(scannerOptions->descriptions)
190
191 for (int i = 0; i < scannerOptions->optionCount; i++) {
192 DELETE_AND_NULLIFY(scannerOptions->ranges[i])
193 }
194 DELETE_ARRAY_AND_NULLIFY(scannerOptions->ranges)
195 DELETE_AND_NULLIFY(scannerOptions)
196 }
197
CreateScannerOptions(int32_t & optionCount)198 Scan_ScannerOptions* CreateScannerOptions(int32_t &optionCount)
199 {
200 Scan_ScannerOptions* scannerOptions = new (std::nothrow) Scan_ScannerOptions();
201 if (scannerOptions == nullptr) {
202 SCAN_HILOGE("scannerOptions is a nullptr");
203 return nullptr;
204 }
205 int32_t scannerOptionsMemSize = sizeof(Scan_ScannerOptions);
206 if (memset_s(scannerOptions, scannerOptionsMemSize, 0, scannerOptionsMemSize) != 0) {
207 SCAN_HILOGW("memset_s fail");
208 FreeScannerOptionsMemory(scannerOptions);
209 return nullptr;
210 }
211 scannerOptions->titles = new (std::nothrow) char* [optionCount];
212 scannerOptions->descriptions = new (std::nothrow) char* [optionCount];
213 scannerOptions->ranges = new (std::nothrow) char* [optionCount];
214 scannerOptions->optionCount = optionCount;
215 if (scannerOptions->titles == nullptr || scannerOptions->descriptions == nullptr ||
216 scannerOptions->ranges == nullptr) {
217 FreeScannerOptionsMemory(scannerOptions);
218 return nullptr;
219 }
220 int32_t stringMemSize = optionCount * sizeof(char**);
221 if (memset_s(scannerOptions->titles, stringMemSize, 0, stringMemSize) != 0 ||
222 memset_s(scannerOptions->descriptions, stringMemSize, 0, stringMemSize) != 0 ||
223 memset_s(scannerOptions->ranges, stringMemSize, 0, stringMemSize) != 0) {
224 SCAN_HILOGW("memset_s fail");
225 FreeScannerOptionsMemory(scannerOptions);
226 return nullptr;
227 }
228 return scannerOptions;
229 }
230
CopySingleBuf(char * destBuf,const char * srcBuf,size_t bufferSize)231 bool CopySingleBuf(char* destBuf, const char* srcBuf, size_t bufferSize)
232 {
233 if (destBuf == nullptr || srcBuf == nullptr) {
234 SCAN_HILOGW("CopySingleBuf new fail");
235 return false;
236 }
237 if (memset_s(destBuf, bufferSize, 0, bufferSize) != 0) {
238 SCAN_HILOGE("CopySingleBuf memset_s fail");
239 return false;
240 }
241 if (strncpy_s(destBuf, bufferSize, srcBuf, bufferSize) != 0) {
242 SCAN_HILOGE("CopySingleBuf strncpy_s fail");
243 return false;
244 }
245
246 return true;
247 }
248
MemSetScannerOptions(Scan_ScannerOptions * scannerOptions,int32_t & optionCount,ScanParaTable & paraTable)249 bool MemSetScannerOptions(Scan_ScannerOptions* scannerOptions, int32_t &optionCount, ScanParaTable ¶Table)
250 {
251 for (int i = 0; i < optionCount; i++) {
252 auto bufferSize = paraTable.titBuff[i].length() + 1;
253 char* titBuff = new (std::nothrow) char[bufferSize];
254 if (!CopySingleBuf(titBuff, paraTable.titBuff[i].c_str(), bufferSize)) {
255 if (titBuff != nullptr) {
256 delete[] titBuff;
257 }
258 return false;
259 }
260 scannerOptions->titles[i] = titBuff;
261
262 bufferSize = paraTable.desBuff[i].length() + 1;
263 char* desBuff = new (std::nothrow) char[bufferSize];
264 if (!CopySingleBuf(desBuff, paraTable.desBuff[i].c_str(), bufferSize)) {
265 if (desBuff != nullptr) {
266 delete[] desBuff;
267 }
268 return false;
269 }
270 scannerOptions->descriptions[i] = desBuff;
271
272 bufferSize = paraTable.rangesBuff[i].length() + 1;
273 char* rangesBuff = new (std::nothrow) char[bufferSize];
274 if (!CopySingleBuf(rangesBuff, paraTable.rangesBuff[i].c_str(), bufferSize)) {
275 if (rangesBuff != nullptr) {
276 delete[] rangesBuff;
277 }
278 return false;
279 }
280 scannerOptions->ranges[i] = rangesBuff;
281 }
282 return true;
283 }
284
GetScanParaValue(ScanParaTable & paraTable)285 Scan_ScannerOptions* GetScanParaValue(ScanParaTable ¶Table)
286 {
287 int32_t optionCount = paraTable.lengthBuff;
288 if (optionCount <= 0) {
289 SCAN_HILOGE("optionCount <= 0");
290 return nullptr;
291 }
292 Scan_ScannerOptions* scannerOptions = CreateScannerOptions(optionCount);
293 if (scannerOptions == nullptr) {
294 SCAN_HILOGE("scannerOptions is a nullptr");
295 return nullptr;
296 }
297 if (!MemSetScannerOptions(scannerOptions, optionCount, paraTable)) {
298 SCAN_HILOGE("MemSetScannerOptions error");
299 FreeScannerOptionsMemory(scannerOptions);
300 return nullptr;
301 }
302 return scannerOptions;
303 }
304 }
305
OH_Scan_Init()306 int32_t OH_Scan_Init()
307 {
308 SCAN_HILOGI("Enter OH_Scan_Init");
309 auto client = ScanManagerClient::GetInstance();
310 int32_t scanVersion = 0;
311 int32_t ret = client->InitScan(scanVersion);
312 if (ret != SCAN_ERROR_NONE) {
313 SCAN_HILOGE("InitScan failed, ErrorCode: [%{public}d]", ret);
314 return ret;
315 } else {
316 SCAN_HILOGI("InitScan successfully");
317 return SCAN_ERROR_NONE;
318 }
319 }
320
OH_Scan_StartScannerDiscovery(Scan_ScannerDiscoveryCallback callback)321 int32_t OH_Scan_StartScannerDiscovery(Scan_ScannerDiscoveryCallback callback)
322 {
323 g_discoverCallback = callback;
324 auto client = ScanManagerClient::GetInstance();
325 int32_t ret = SCAN_ERROR_NONE;
326 if (!g_isListening) {
327 OHOS::sptr<IScanCallback> call = new (std::nothrow) ScanCallback(callbackFunction);
328 if (call == nullptr) {
329 SCAN_HILOGE("call is null");
330 return SCAN_ERROR_GENERIC_FAILURE;
331 }
332 ret = client->On("", std::string(GET_SCANNER_DEVICE_LIST), call);
333 if (ret != SCAN_ERROR_NONE) {
334 SCAN_HILOGE("Failed to register event");
335 return ret;
336 }
337 g_isListening = true;
338 }
339 ret = client->GetScannerList();
340 if (ret != SCAN_ERROR_NONE) {
341 SCAN_HILOGE("Failed to GetScannerList");
342 return ret;
343 }
344 return SCAN_ERROR_NONE;
345 }
346
OH_Scan_OpenScanner(const char * scannerId)347 int32_t OH_Scan_OpenScanner(const char* scannerId)
348 {
349 if (scannerId == nullptr) {
350 SCAN_HILOGE("Invalid parameter.");
351 return SCAN_ERROR_INVALID_PARAMETER;
352 }
353 auto client = ScanManagerClient::GetInstance();
354 int32_t ret = client->OpenScanner(std::string(scannerId));
355 if (ret != SCAN_ERROR_NONE) {
356 SCAN_HILOGE("OpenScanner failed, ErrorCode: [%{public}d]", ret);
357 return ret;
358 } else {
359 SCAN_HILOGI("OpenScanner successfully");
360 return SCAN_ERROR_NONE;
361 }
362 }
363
OH_Scan_CloseScanner(const char * scannerId)364 int32_t OH_Scan_CloseScanner(const char* scannerId)
365 {
366 if (scannerId == nullptr) {
367 SCAN_HILOGE("Invalid parameter.");
368 return SCAN_ERROR_INVALID_PARAMETER;
369 }
370 auto client = ScanManagerClient::GetInstance();
371 int32_t ret = client->CloseScanner(std::string(scannerId));
372 if (ret != SCAN_ERROR_NONE) {
373 SCAN_HILOGE("CloseScanner failed, ErrorCode: [%{public}d]", ret);
374 return ret;
375 } else {
376 SCAN_HILOGI("CloseScanner successfully");
377 return SCAN_ERROR_NONE;
378 }
379 }
380
OH_Scan_GetScannerParameter(const char * scannerId,int32_t * errorCode)381 Scan_ScannerOptions* OH_Scan_GetScannerParameter(const char* scannerId, int32_t* errorCode)
382 {
383 if (scannerId == nullptr || errorCode == nullptr) {
384 SCAN_HILOGE("Invalid parameter.");
385 return nullptr;
386 }
387 std::string deviceId = std::string(scannerId);
388 if (g_scanParaTables.find(deviceId) != g_scanParaTables.end()) {
389 SCAN_HILOGW("Device parameters have been obtained.");
390 *errorCode = SCAN_ERROR_NONE;
391 return g_scanParaTables[deviceId];
392 }
393 int32_t status = SCAN_ERROR_NONE;
394 ScanOptionValue value;
395 status = GetScanParaDesc(deviceId, value);
396 if (status != SCAN_ERROR_NONE) {
397 SCAN_HILOGE("Failed to get scanner ScanOptionValue value.");
398 *errorCode = status;
399 return nullptr;
400 }
401 ScanParaTable paraTable;
402 status = GetScanParaValues(deviceId, value, paraTable);
403 if (status != SCAN_ERROR_NONE) {
404 SCAN_HILOGE("Failed to get scanner ScanParaTable paraTable.");
405 *errorCode = status;
406 return nullptr;
407 }
408
409 Scan_ScannerOptions* scaParaOptions = GetScanParaValue(paraTable);
410 if (scaParaOptions == nullptr) {
411 *errorCode = SCAN_ERROR_GENERIC_FAILURE;
412 return nullptr;
413 }
414 g_scanParaTables[scannerId] = scaParaOptions;
415 *errorCode = SCAN_ERROR_NONE;
416 return scaParaOptions;
417 }
418
OH_Scan_SetScannerParameter(const char * scannerId,const int32_t option,const char * value)419 int32_t OH_Scan_SetScannerParameter(const char* scannerId, const int32_t option, const char* value)
420 {
421 if (scannerId == nullptr || value == nullptr) {
422 SCAN_HILOGE("Invalid parameter.");
423 return SCAN_ERROR_INVALID_PARAMETER;
424 }
425 auto client = ScanManagerClient::GetInstance();
426 if (g_valueMap.find(scannerId) == g_valueMap.end() ||
427 g_valueMap[scannerId].find(option) == g_valueMap[scannerId].end()) {
428 SCAN_HILOGE("not exit this option: [%{public}d]", option);
429 return SCAN_ERROR_INVALID_PARAMETER;
430 }
431 auto t = g_valueMap[scannerId].find(option);
432 uint32_t valueType = g_valueMap[scannerId][option].valueType;
433 std::string strvalue = std::string(value);
434 ScanOptionValue optionValue;
435
436 if (valueType == SCAN_INT_TYPE) {
437 if (!t->second.numList.count(strvalue)) {
438 SCAN_HILOGE("not exit this value: [%{public}s]", strvalue.c_str());
439 return SCAN_ERROR_INVALID_PARAMETER;
440 }
441 optionValue.SetNumValue(std::stoi(strvalue));
442 optionValue.SetScanOptionValueType(SCAN_VALUE_NUM);
443 } else if (valueType == SCAN_STRING_TYPE) {
444 if (!t->second.strList.count(strvalue)) {
445 SCAN_HILOGE("not exit this value: [%{public}s]", strvalue.c_str());
446 return SCAN_ERROR_INVALID_PARAMETER;
447 }
448 optionValue.SetStrValue(strvalue);
449 optionValue.SetScanOptionValueType(SCAN_VALUE_STR);
450 } else {
451 SCAN_HILOGI("not exist this type ");
452 return SCAN_ERROR_GENERIC_FAILURE;
453 }
454
455 int32_t optionIndex = t->second.optionIndex;
456 int32_t info;
457 int32_t ret = client->OpScanOptionValue(std::string(scannerId),
458 optionIndex, SCAN_ACTION_SET_VALUE, optionValue, info);
459 if (ret != SCAN_ERROR_NONE) {
460 SCAN_HILOGE("SetScannerParameter failed, ErxrorCode: [%{public}d]", ret);
461 return ret;
462 } else {
463 SCAN_HILOGI("SetScannerParameter successfully");
464 return SCAN_ERROR_NONE;
465 }
466 return SCAN_ERROR_NONE;
467 }
468
OH_Scan_StartScan(const char * scannerId,bool batchMode)469 int32_t OH_Scan_StartScan(const char* scannerId, bool batchMode)
470 {
471 if (scannerId == nullptr) {
472 SCAN_HILOGE("Invalid parameter.");
473 return SCAN_ERROR_INVALID_PARAMETER;
474 }
475 auto client = ScanManagerClient::GetInstance();
476 int32_t ret = client->StartScan(std::string(scannerId), batchMode);
477 if (ret != SCAN_ERROR_NONE) {
478 SCAN_HILOGE("StartScan failed, ErxrorCode: [%{public}d]", ret);
479 return ret;
480 } else {
481 SCAN_HILOGI("StartScan successfully");
482 return SCAN_ERROR_NONE;
483 }
484 }
485
OH_Scan_CancelScan(const char * scannerId)486 int32_t OH_Scan_CancelScan(const char* scannerId)
487 {
488 if (scannerId == nullptr) {
489 SCAN_HILOGE("Invalid parameter.");
490 return SCAN_ERROR_INVALID_PARAMETER;
491 }
492 auto client = ScanManagerClient::GetInstance();
493 int32_t ret = client->CancelScan(std::string(scannerId));
494 if (ret != SCAN_ERROR_NONE) {
495 SCAN_HILOGE("CancelScan failed, ErxrorCode: [%{public}d]", ret);
496 return ret;
497 } else {
498 SCAN_HILOGI("CancelScan successfully");
499 return SCAN_ERROR_NONE;
500 }
501 }
502
OH_Scan_GetPictureScanProgress(const char * scannerId,Scan_PictureScanProgress * prog)503 int32_t OH_Scan_GetPictureScanProgress(const char* scannerId, Scan_PictureScanProgress* prog)
504 {
505 if (prog == nullptr) {
506 SCAN_HILOGE("Invalid parameter.");
507 return SCAN_ERROR_INVALID_PARAMETER;
508 }
509 ScanProgress scanProg;
510 auto client = ScanManagerClient::GetInstance();
511 int32_t ret = client->GetScanProgress(std::string(scannerId), scanProg);
512 if (ret != SCAN_ERROR_NONE) {
513 SCAN_HILOGE("GetScanProgress failed, ErrorCode: [%{public}d]", ret);
514 return ret;
515 } else {
516 prog->progress = scanProg.GetScanProgress();
517 prog->fd = scanProg.GetScanPictureFd();
518 prog->isFinal = scanProg.GetIsFinal();
519 SCAN_HILOGI("GetScanProgress successfully");
520 return SCAN_ERROR_NONE;
521 }
522 }
523
OH_Scan_Exit()524 int32_t OH_Scan_Exit()
525 {
526 auto client = ScanManagerClient::GetInstance();
527 int32_t ret = client->ExitScan();
528 if (ret != SCAN_ERROR_NONE) {
529 SCAN_HILOGE("ExitScan failed, ErrorCode: [%{public}d]", ret);
530 return ret;
531 }
532 for (auto table : g_scanParaTables) {
533 FreeScannerOptionsMemory(table.second);
534 }
535 g_scanParaTables.clear();
536 if (g_isListening) {
537 client->Off("", std::string(GET_SCANNER_DEVICE_LIST));
538 }
539 SCAN_HILOGI("ExitScan successfully");
540 return SCAN_ERROR_NONE;
541 }