• 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 #include <sstream>
16 #include <iomanip>
17 #include "scan_log.h"
18 #include "scan_service_utils.h"
19 
20 namespace OHOS::Scan {
21 const std::string TASK_EVENT_DELIMITER = "-";
22 std::unordered_map<SaneStatus, ScanErrorCode> ScanServiceUtils::saneStatusToScanErrorCodeMap_ = {
23     {SANE_STATUS_GOOD, E_SCAN_NONE},
24     {SANE_STATUS_UNSUPPORTED, E_SCAN_UNSUPPORTED},
25     {SANE_STATUS_CANCELLED, E_SCAN_CANCELLED},
26     {SANE_STATUS_DEVICE_BUSY, E_SCAN_DEVICE_BUSY},
27     {SANE_STATUS_INVAL, E_SCAN_INVAL},
28     {SANE_STATUS_EOF, E_SCAN_EOF},
29     {SANE_STATUS_JAMMED, E_SCAN_JAMMED},
30     {SANE_STATUS_NO_DOCS, E_SCAN_NO_DOCS},
31     {SANE_STATUS_COVER_OPEN, E_SCAN_COVER_OPEN},
32     {SANE_STATUS_IO_ERROR, E_SCAN_IO_ERROR},
33     {SANE_STATUS_NO_MEM, E_SCAN_NO_MEM},
34     {SANE_STATUS_ACCESS_DENIED, E_SCAN_ACCESS_DENIED},
35     {SANE_STATUS_NO_PERMISSION, E_SCAN_NO_PERMISSION},
36     {SANE_STATUS_INVALID_PARAMETER, E_SCAN_INVALID_PARAMETER},
37     {SANE_STATUS_GENERIC_FAILURE, E_SCAN_GENERIC_FAILURE},
38     {SANE_STATUS_RPC_FAILURE, E_SCAN_RPC_FAILURE},
39     {SANE_STATUS_SERVER_FAILURE, E_SCAN_SERVER_FAILURE}
40 };
ReplaceDeviceIdUsbPort(const std::string & deviceId,const std::string & usbPort)41 std::string ScanServiceUtils::ReplaceDeviceIdUsbPort(const std::string& deviceId, const std::string& usbPort)
42 {
43     constexpr int32_t invalidPort = -1;
44     int32_t start = invalidPort;
45     int32_t end = invalidPort;
46     char dash;
47     std::istringstream(usbPort) >> start >> dash >> end;
48     if (start < 0 || end < 0 || dash != '-') {
49         SCAN_HILOGE("usbPort format is error");
50         return "";
51     }
52     std::ostringstream oss;
53     char zero = '0';
54     constexpr int32_t portWidth = 3;
55     oss << std::setw(portWidth) << std::setfill(zero) << start;
56     std::string formattedStart = oss.str();
57     oss.str("");
58     oss << std::setw(portWidth) << std::setfill(zero) << end;
59     std::string formattedEnd = oss.str();
60     size_t pos1 = deviceId.rfind(':');
61     if (pos1 == std::string::npos) {
62         SCAN_HILOGE("deviceId format is error");
63         return "";
64     }
65     size_t pos2 = deviceId.rfind(':', pos1 - 1);
66     if (pos2 == std::string::npos) {
67         SCAN_HILOGE("deviceId format is error");
68         return "";
69     }
70     std::string newDeviceId = deviceId.substr(0, pos2 + 1).append(formattedStart).append(":").append(formattedEnd);
71     SCAN_HILOGD("new deviceId = %{private}s", newDeviceId.c_str());
72     return newDeviceId;
73 }
74 
EncodeTaskEventId(const std::string & eventType,std::string & type)75 bool ScanServiceUtils::EncodeTaskEventId(const std::string &eventType, std::string &type)
76 {
77     size_t pos = eventType.find_last_of(TASK_EVENT_DELIMITER);
78     if (pos != std::string::npos) {
79         type = eventType.substr(pos + 1);
80         return true;
81     }
82     return false;
83 }
84 
GetTaskEventId(const std::string & taskId,const std::string & type,int32_t userId,int32_t callerPid)85 std::string ScanServiceUtils::GetTaskEventId(const std::string &taskId, const std::string &type,
86     int32_t userId, int32_t callerPid)
87 {
88     std::ostringstream eventTypeStream;
89     eventTypeStream << userId << TASK_EVENT_DELIMITER << callerPid << TASK_EVENT_DELIMITER;
90     if (!taskId.empty()) {
91         eventTypeStream << taskId << TASK_EVENT_DELIMITER;
92     }
93     eventTypeStream << type;
94     return eventTypeStream.str();
95 }
96 
ConvertErro(const SaneStatus status)97 ScanErrorCode ScanServiceUtils::ConvertErro(const SaneStatus status)
98 {
99     auto it = saneStatusToScanErrorCodeMap_.find(status);
100     if (it != saneStatusToScanErrorCodeMap_.end()) {
101         return it->second;
102     }
103     return E_SCAN_GENERIC_FAILURE;
104 }
105 }  // namespace OHOS::Scan