• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_mdns_service.h"
17 #include "mdns_common.h"
18 
19 #define SCAN_MDNS_PORT 5353
20 
21 namespace OHOS::Scan {
22 using namespace OHOS::NetManagerStandard;
23 
SetServiceType(std::string stype)24 void ScanMdnsService::SetServiceType(std::string stype)
25 {
26     _serviceInfo.type = stype;
27     _serviceInfo.port = SCAN_MDNS_PORT;
28     _scanMDnsDiscoveryCallBack = new (std::nothrow) ScanMDnsDiscoveryObserver(_serviceInfo);
29     _scanMDnsResolveCallBack = new (std::nothrow) ScanMDnsResolveObserver(_serviceInfo);
30 }
31 
GetServiceInfo()32 MDnsServiceInfo &ScanMdnsService::GetServiceInfo()
33 {
34     return _serviceInfo;
35 }
36 
SetServiceInfo(const MDnsServiceInfo & info)37 void ScanMdnsService::SetServiceInfo(const MDnsServiceInfo &info)
38 {
39     _serviceInfo = info;
40 }
41 
GetMDnsDiscoveryCallBack()42 sptr<ScanMDnsDiscoveryObserver> ScanMdnsService::GetMDnsDiscoveryCallBack()
43 {
44     return _scanMDnsDiscoveryCallBack;
45 }
46 
SetMDnsDiscoveryCallBack(sptr<ScanMDnsDiscoveryObserver> & cb)47 void ScanMdnsService::SetMDnsDiscoveryCallBack(sptr<ScanMDnsDiscoveryObserver> &cb)
48 {
49     _scanMDnsDiscoveryCallBack = cb;
50 }
51 
GetMDnsResolveCallBack()52 sptr<ScanMDnsResolveObserver> ScanMdnsService::GetMDnsResolveCallBack()
53 {
54     return _scanMDnsResolveCallBack;
55 }
56 
SetMDnsResolveCallBack(sptr<ScanMDnsResolveObserver> & cb)57 void ScanMdnsService::SetMDnsResolveCallBack(sptr<ScanMDnsResolveObserver> &cb)
58 {
59     _scanMDnsResolveCallBack = cb;
60 }
61 
onStartDiscoverService()62 bool ScanMdnsService::onStartDiscoverService()
63 {
64     if (_scanMDnsDiscoveryCallBack == nullptr) {
65         SCAN_HILOGE("GetScannerList onStartDiscoverService1 nullptr");
66         return false;
67     }
68     SCAN_HILOGI("GetScannerList onStartDiscoverService begin");
69     int32_t ret = DelayedSingleton<MDnsClient>::GetInstance()->StartDiscoverService(
70         _serviceInfo.type, _scanMDnsDiscoveryCallBack);
71     if (ret != NETMANAGER_EXT_SUCCESS) {
72         return false;
73     }
74     SCAN_HILOGI("GetScannerList onStartDiscoverService end");
75     return true;
76 }
77 
onResolveService(MDnsServiceInfo & serviceInfo)78 bool ScanMdnsService::onResolveService(MDnsServiceInfo &serviceInfo)
79 {
80     SCAN_HILOGI("GetScannerList onResolveService");
81     if (_scanMDnsResolveCallBack == nullptr) {
82         SCAN_HILOGE("GetScannerList _scanMDnsResolveCallBack null fail");
83         return false;
84     }
85     int32_t ret = DelayedSingleton<MDnsClient>::GetInstance()->ResolveService(serviceInfo, _scanMDnsResolveCallBack);
86     if (ret != NETMANAGER_EXT_SUCCESS) {
87         SCAN_HILOGE("GetScannerList onResolveService false");
88         return false;
89     }
90     SCAN_HILOGI("GetScannerList onResolveService1 success");
91     return true;
92 }
93 
onStopDiscoverService()94 bool ScanMdnsService::onStopDiscoverService()
95 {
96     SCAN_HILOGI("GetScannerList onStopDiscoverService");
97     if (_scanMDnsDiscoveryCallBack == nullptr) {
98         SCAN_HILOGE("GetScannerList _scanMDnsDiscoveryCallBack null fail");
99         return false;
100     }
101     int32_t ret = DelayedSingleton<MDnsClient>::GetInstance()->StopDiscoverService(_scanMDnsDiscoveryCallBack);
102     if (ret != NETMANAGER_EXT_SUCCESS) {
103         SCAN_HILOGE("GetScannerList onStopDiscoverService false");
104         return false;
105     }
106     SCAN_HILOGI("GetScannerList onStopDiscoverService success");
107     return true;
108 }
109 
HandleServiceFound(const MDnsServiceInfo & info,int32_t retCode)110 void ScanMDnsDiscoveryObserver::HandleServiceFound(const MDnsServiceInfo &info, int32_t retCode)
111 {
112     MDnsServiceInfo tempInfo = info;
113     SCAN_HILOGD("GetScannerList HandleServiceFound [%{public}s][%{public}s][%{public}d][%{public}s]",
114         info.name.c_str(),
115         info.type.c_str(),
116         info.port,
117         info.addr.c_str());
118     ScanMdnsService::GetInstance().onResolveService(tempInfo);
119 }
120 
HandleResolveResult(const MDnsServiceInfo & info,int32_t retCode)121 void ScanMDnsResolveObserver::HandleResolveResult(const MDnsServiceInfo &info, int32_t retCode)
122 {
123     _serviceInfo = info;
124     SCAN_HILOGD("GetScannerList HandleResolveResult [%{public}s][%{public}s][%{public}d][%{public}s]",
125         info.name.c_str(),
126         info.type.c_str(),
127         info.port,
128         info.addr.c_str());
129     MDnsServiceInfo tempInfo = info;
130     auto texRecord = tempInfo.GetAttrMap();
131     auto textIt = texRecord.begin();
132     for (; textIt != texRecord.end(); textIt++) {
133         SCAN_HILOGD("GetScannerList startHandleServiceResolve keys [%{public}s]", textIt->first.c_str());
134     }
135 
136     ScanMdnsService::GetInstance().ToMDnsScaner(tempInfo);
137 }
138 
HandleStopDiscover(const MDnsServiceInfo & serviceInfo,int32_t retCode)139 void ScanMDnsDiscoveryObserver::HandleStopDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode)
140 {
141     MDnsServiceInfo info = serviceInfo;
142     SCAN_HILOGD("GetScannerList HandleStopDiscover [%{public}s][%{public}s][%{public}d][%{public}s]",
143         info.name.c_str(),
144         info.type.c_str(),
145         info.port,
146         info.addr.c_str());
147 }
148 
ToMDnsScaner(MDnsServiceInfo & serviceInfo)149 void ScanMdnsService::ToMDnsScaner(MDnsServiceInfo &serviceInfo)
150 {
151     std::unique_ptr<ScanDeviceInfoTCP> scannerInfo = std::make_unique<ScanDeviceInfoTCP>();
152     scannerInfo->addr = serviceInfo.addr;
153     scannerInfo->deviceName = serviceInfo.name;
154     scannerInfo->port = std::to_string(serviceInfo.port);
155     std::vector<std::string> keys = {"UUID", "adminur", "button", "feeder", "mdl", "mfg", "mote", "txtvers", "ty"};
156     for (auto key : keys) {
157         std::string value = ScanMdnsService::GetServiceAttribute(serviceInfo, key);
158         if (value.empty()) {
159             SCAN_HILOGW("GetScannerList key [%{public}s] is empty", key.c_str());
160             continue;
161         }
162         SCAN_HILOGD("GetScannerList key:[%{public}s] value:[%{public}s]", key.c_str(), value.c_str());
163         if (key == "UUID") {
164             scannerInfo->uuid = value;
165         } else if (key == "mdl") {
166             scannerInfo->model = value;
167         } else if (key == "mfg") {
168             scannerInfo->manufacturer = value;
169         } else if (key == "ty") {
170             scannerInfo->deviceType = value;
171         } else if (key == "port") {
172             scannerInfo->port = value;
173         } else if (key == "button") {
174             scannerInfo->button = value;
175         } else if (key == "feeder") {
176             scannerInfo->feeder = value;
177         }
178     }
179     SCAN_HILOGI("mdns scanner's deviceName:[%{public}s]", scannerInfo->deviceName.c_str());
180     SCAN_HILOGD("mdns scanner's addr:[%{public}s]", scannerInfo->addr.c_str());
181     SCAN_HILOGD("mdns scanner's port:[%{public}s]", scannerInfo->port.c_str());
182     ScanServiceAbility::scanDeviceInfoTCPMap_[scannerInfo->addr] = *scannerInfo;
183 }
184 
GetServiceAttribute(MDnsServiceInfo & serviceInfo,std::string keyStr)185 std::string ScanMdnsService::GetServiceAttribute(MDnsServiceInfo &serviceInfo, std::string keyStr)
186 {
187     SCAN_HILOGD("GetScannerList GetServiceAttribute keyStr [%{public}s]", keyStr.c_str());
188     TxtRecord attrMap = serviceInfo.GetAttrMap();
189     auto attrArrSize = attrMap.size();
190     if (attrArrSize < 1) {
191         SCAN_HILOGE("can not get attr");
192         return "";
193     }
194     auto attrIt = attrMap.find(keyStr);
195     if (attrIt == attrMap.end()) {
196         SCAN_HILOGE("can not find key");
197         return "";
198     }
199     std::string value = "";
200     if (!attrMap[keyStr].empty()) {
201         value += std::accumulate(attrMap[keyStr].begin(), attrMap[keyStr].end(), "");
202     }
203 
204     return value;
205 }
206 
207 
208 }  // namespace OHOS::Scan
209