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 <iostream>
16 #include <cstring>
17 #include <memory>
18 #include <arpa/inet.h>
19 #include <vector>
20 #include <thread>
21 #include <utility>
22 #include <unordered_set>
23 #include "usb_monitor.h"
24 #include "usb_ipp_manager.h"
25
26 using namespace OHOS::CUPS;
27
28 static IppUsbManager& usbManager = IppUsbManager::GetInstance();
29 static std::pair<std::string, std::string> GetComparedStateReasons(const char* oldReasons, const char* newReasons);
30
IsSupportIppOverUsb(const char * uri)31 bool IsSupportIppOverUsb(const char* uri)
32 {
33 fprintf(stderr, "DEBUG: USB_MONITOR enter IsSupportIppOverUsb\n");
34 if (uri == nullptr) {
35 fprintf(stderr, "DEBUG: USB_MONITOR uri is a nullptr\n");
36 return false;
37 }
38 return usbManager.IsSupportIppOverUsb(std::string(uri));
39 }
40
StartMonitorIppPrinter(MonitorPrinterCallback callback,const char * uri)41 bool StartMonitorIppPrinter(MonitorPrinterCallback callback, const char* uri)
42 {
43 fprintf(stderr, "DEBUG: USB_MONITOR enter MonitorPrinter\n");
44 if (callback == nullptr || uri == nullptr) {
45 fprintf(stderr, "DEBUG: USB_MONITOR callback or uri is nullptr\n");
46 return false;
47 }
48 std::string uriStr(uri);
49 if (!usbManager.ConnectUsbPinter(uriStr)) {
50 fprintf(stderr, "DEBUG: USB_MONITOR ConnectUsbPinter fail\n");
51 return false;
52 }
53 if (!usbManager.ProcessMonitorPrinter(uriStr, callback)) {
54 fprintf(stderr, "DEBUG: USB_MONITOR ProcessMonitorPrinter fail\n");
55 usbManager.DisConnectUsbPinter(uriStr);
56 return false;
57 }
58 usbManager.DisConnectUsbPinter(uriStr);
59 fprintf(stderr, "DEBUG: USB_MONITOR end MonitorPrinter\n");
60 return true;
61 }
62
SetTerminalSingal()63 void SetTerminalSingal()
64 {
65 fprintf(stderr, "DEBUG: USB_MONITOR enter SetTerminalSingal\n");
66 usbManager.SetTerminalSingal();
67 }
68
GetComparedStateReasons(const char * oldReasons,const char * newReasons)69 std::pair<std::string, std::string> GetComparedStateReasons(const char* oldReasons, const char* newReasons)
70 {
71 std::vector<std::string> oldReasonsVec;
72 std::vector<std::string> newReasonsVec;
73 std::stringstream ssOldReasons(oldReasons);
74 std::stringstream ssNewReasons(newReasons);
75 std::string token;
76 while (std::getline(ssOldReasons, token, ',')) {
77 oldReasonsVec.push_back(token);
78 }
79 while (std::getline(ssNewReasons, token, ',')) {
80 newReasonsVec.push_back(token);
81 }
82 std::unordered_set<std::string> setOldReasons(oldReasonsVec.begin(), oldReasonsVec.end());
83 std::unordered_set<std::string> setNewReasons(newReasonsVec.begin(), newReasonsVec.end());
84 std::vector<std::string> addedVec;
85 std::vector<std::string> deletedVec;
86 for (const auto& item : setNewReasons) {
87 if (setOldReasons.find(item) == setOldReasons.end()) {
88 addedVec.push_back(item);
89 }
90 }
91 for (const auto& item : setOldReasons) {
92 if (setNewReasons.find(item) == setNewReasons.end()) {
93 deletedVec.push_back(item);
94 }
95 }
96 std::string addedStr;
97 std::string deletedStr;
98 for (size_t i = 0; i < addedVec.size(); ++i) {
99 if (i != 0) {
100 addedStr += ",";
101 }
102 addedStr += addedVec[i];
103 }
104 for (size_t i = 0; i < deletedVec.size(); ++i) {
105 if (i != 0) {
106 deletedStr += ",";
107 }
108 deletedStr += deletedVec[i];
109 }
110 return std::make_pair(addedStr, deletedStr);
111 }
112
ComparePrinterStateReasons(const char * oldReasons,const char * newReasons,char ** addedReasons,char ** deletedReasons)113 void ComparePrinterStateReasons(const char* oldReasons, const char* newReasons,
114 char** addedReasons, char** deletedReasons)
115 {
116 if (oldReasons == nullptr || newReasons == nullptr ||
117 addedReasons == nullptr || deletedReasons == nullptr) {
118 fprintf(stderr, "DEBUG: USB_MONITOR ComparePrinterStateReasons nullptr\n");
119 return;
120 }
121 if (strcmp(oldReasons, newReasons) == 0) {
122 return;
123 }
124 auto comparedStr = GetComparedStateReasons(oldReasons, newReasons);
125 std::string addedStr = comparedStr.first;
126 std::string deletedStr = comparedStr.second;
127 auto getComparedStr = [](const std::string& str) -> char* {
128 int32_t len = static_cast<int32_t>(str.size()) + 1;
129 if (len > PRINTER_STATE_REASONS_SIZE) {
130 fprintf(stderr, "DEBUG: USB_MONITOR The length exceeds the maximum value.\n");
131 return nullptr;
132 }
133 char* cStr = new (std::nothrow) char[len]{};
134 if (cStr == nullptr) {
135 fprintf(stderr, "DEBUG: USB_MONITOR cStr new fail\n");
136 return nullptr;
137 }
138 if (strcpy_s(cStr, len, str.c_str()) != 0) {
139 fprintf(stderr, "DEBUG: USB_MONITOR ComparePrinterStateReasons strcpy_s fail\n");
140 delete[] cStr;
141 return nullptr;
142 }
143 return cStr;
144 };
145
146 *addedReasons = getComparedStr(addedStr);
147 *deletedReasons = getComparedStr(deletedStr);
148 }
149
FreeCompareStringsResult(char ** addedReasons,char ** deletedReasons)150 void FreeCompareStringsResult(char** addedReasons, char** deletedReasons)
151 {
152 if (addedReasons != nullptr && *addedReasons != nullptr) {
153 delete[] *addedReasons;
154 *addedReasons = nullptr;
155 }
156 if (deletedReasons != nullptr && *deletedReasons != nullptr) {
157 delete[] *deletedReasons;
158 *deletedReasons = nullptr;
159 }
160 }