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 #include <vector>
16 #include "scan_manager_client.h"
17 #include "napi_scan_helper.h"
18 #include "scan_constant.h"
19 #include "scan_log.h"
20
21 namespace OHOS::Scan {
GetScannerAllPara(const std::string & deviceId,std::vector<ScanOptionDescriptor> & allDesc)22 int32_t NapiScanHelper::GetScannerAllPara(const std::string &deviceId, std::vector<ScanOptionDescriptor> &allDesc)
23 {
24 int32_t scannerParaCount = 0;
25 int32_t ret = GetScannerParaCount(deviceId, scannerParaCount);
26 if (ret != E_SCAN_NONE) {
27 SCAN_HILOGE("Failed to get the scan para count");
28 return ret;
29 }
30 ret = GetScannerParameter(deviceId, scannerParaCount, allDesc);
31 if (ret != E_SCAN_NONE) {
32 SCAN_HILOGE("Failed to get the scan para count");
33 return ret;
34 }
35 return E_SCAN_NONE;
36 }
37
GetScannerParaCount(const std::string & deviceId,int32_t & scannerParaCount)38 int32_t NapiScanHelper::GetScannerParaCount(const std::string& deviceId, int32_t& scannerParaCount)
39 {
40 auto client = ScanManagerClient::GetInstance();
41 if (client == nullptr) {
42 SCAN_HILOGE("client is a nullptr");
43 return E_SCAN_GENERIC_FAILURE;
44 }
45 ScanOptionDescriptor desc;
46 int32_t ret = client->GetScanOptionDesc(deviceId, 0, desc);
47 if (ret != E_SCAN_NONE) {
48 SCAN_HILOGE("GetScanOptionDesc failed, ErrorCode: [%{public}d]", ret);
49 return ret;
50 }
51 uint32_t optionType = desc.GetOptionType();
52 ScanOptionValue value;
53 value.SetScanOptionValueType(static_cast<ScanOptionValueType>(optionType));
54 ret = client->OpScanOptionValue(deviceId, 0, SCAN_ACTION_GET_VALUE, value);
55 if (ret != E_SCAN_NONE) {
56 SCAN_HILOGE("OpScanOptionValue failed, ErrorCode: [%{public}d]", ret);
57 return ret;
58 }
59 scannerParaCount = value.GetNumValue();
60 return E_SCAN_NONE;
61 }
62
GetScannerParameter(const std::string & deviceId,int32_t scannerParaCount,std::vector<ScanOptionDescriptor> & allDesc)63 int32_t NapiScanHelper::GetScannerParameter(const std::string &deviceId,
64 int32_t scannerParaCount, std::vector<ScanOptionDescriptor> &allDesc)
65 {
66 for (int i = 1; i < scannerParaCount; i++) {
67 ScanOptionDescriptor desc;
68 auto client = ScanManagerClient::GetInstance();
69 if (client == nullptr) {
70 SCAN_HILOGE("client is a nullptr");
71 return E_SCAN_GENERIC_FAILURE;
72 }
73 int32_t ret = client->GetScanOptionDesc(deviceId, i, desc);
74 if (ret != E_SCAN_NONE) {
75 SCAN_HILOGE("GetScanOptionDesc failed, ErrorCode: [%{public}d]", ret);
76 return ret;
77 }
78 desc.SetOptionIndex(i);
79 allDesc.push_back(desc);
80 }
81 return E_SCAN_NONE;
82 }
83
84 } // namespace OHOS::Scan