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 #ifndef SCAN_MDNS_SERVICE_H
17 #define SCAN_MDNS_SERVICE_H
18 
19 #include <unordered_set>
20 #include "i_mdns_event.h"
21 #include "mdns_event_stub.h"
22 #include "mdns_client.h"
23 #include "mdns_common.h"
24 #include "scanner_info.h"
25 #include "scan_log.h"
26 #ifdef SANE_ENABLE
27 #include "sane/sane.h"
28 #include "sane/saneopts.h"
29 #endif
30 #include "scan_service_ability.h"
31 
32 namespace OHOS::Scan {
33 using namespace OHOS::NetManagerStandard;
34 
35 class ScanMDnsDiscoveryObserver : public DiscoveryCallbackStub {
36 public:
ScanMDnsDiscoveryObserver(const MDnsServiceInfo &info)37     explicit ScanMDnsDiscoveryObserver(const MDnsServiceInfo &info) : expected_(info) {}
38     virtual ~ScanMDnsDiscoveryObserver() = default;
39     void HandleStartDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode) override{}
40     void HandleStopDiscover(const MDnsServiceInfo &serviceInfo, int32_t retCode) override;
41     void HandleServiceLost(const MDnsServiceInfo &serviceInfo, int32_t retCode) override{}
42     void HandleServiceFound(const MDnsServiceInfo &info, int32_t retCode) override;
43     int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override
44     {
45         SCAN_HILOGD("GetScannerList HandleSerieFound OnRemoteRequest");
46         return DiscoveryCallbackStub::OnRemoteRequest(code, data, reply, option);
47     }
48 public:
49     MDnsServiceInfo expected_;
50 };
51 
52 class ScanMDnsResolveObserver : public ResolveCallbackStub {
53 public:
ScanMDnsResolveObserver(const MDnsServiceInfo &info)54     explicit ScanMDnsResolveObserver(const MDnsServiceInfo &info):_serviceInfo(info) {}
55     virtual ~ScanMDnsResolveObserver() = default;
56     int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption& option) override
57     {
58         SCAN_HILOGD("GetScannerList ScanMDnsResolveObserver OnRemoteRequest");
59         return ResolveCallbackStub::OnRemoteRequest(code, data, reply, option);
60     }
61     void HandleResolveResult(const MDnsServiceInfo& info, int32_t retCode) override;
62 public:
63     MDnsServiceInfo _serviceInfo;
64 };
65 
66 class ScanMdnsService {
67 public:
68     ScanMdnsService(const ScanMdnsService&) = delete;
69     ScanMdnsService& operator=(const ScanMdnsService&) = delete;
GetInstance()70     static ScanMdnsService& GetInstance()
71     {
72         static ScanMdnsService instance;
73         return instance;
74     }
75     void SetServiceInfo(const MDnsServiceInfo& info);
76     void SetMDnsResolveCallBack(sptr<ScanMDnsResolveObserver>& cb);
77     void SetMDnsDiscoveryCallBack(sptr<ScanMDnsDiscoveryObserver>& cb);
78     void SetServiceType(std::string stype);
79     sptr<ScanMDnsResolveObserver> GetMDnsResolveCallBack();
80     sptr<ScanMDnsDiscoveryObserver> GetMDnsDiscoveryCallBack();
81     MDnsServiceInfo& GetServiceInfo();
82     static std::string GetServiceAttribute(MDnsServiceInfo& serviceInfo, std::string keyStr);
83 
84     bool onStartDiscoverService();
85     bool onStopDiscoverService();
86     bool onResolveService(MDnsServiceInfo& serviceInfo);
87     void ToMDnsScaner(MDnsServiceInfo& serviceInfo);
88 
89 public:
90     sptr<ScanMDnsDiscoveryObserver> _scanMDnsDiscoveryCallBack;
91     sptr<ScanMDnsResolveObserver> _scanMDnsResolveCallBack;
92 
93 private:
ScanMdnsService()94     ScanMdnsService(){};
95     MDnsServiceInfo _serviceInfo;
96 };
97 }
98 #endif // !SCAN_MDNS_SERVICE_H
99