1 /*
2  * Copyright (C) 2023 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 MDNS_CLIENT_H
17 #define MDNS_CLIENT_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "parcel.h"
23 #include "singleton.h"
24 #include "system_ability_load_callback_stub.h"
25 
26 #include "i_mdns_event.h"
27 #include "i_mdns_service.h"
28 #include "mdns_service_info.h"
29 
30 namespace OHOS {
31 namespace NetManagerStandard {
32 class OnDemandLoadCallback : public SystemAbilityLoadCallbackStub {
33 public:
34     void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override;
35     void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
36     const sptr<IRemoteObject> &GetRemoteObject() const;
37 
38 private:
39     sptr<IRemoteObject> remoteObject_ = nullptr;
40 };
41 
42 class MDnsClient {
43     DECLARE_DELAYED_SINGLETON(MDnsClient);
44 
45 public:
46 /**
47      * Register mDNS service instance
48      *
49      * @param serviceInfo.name Service instance name
50      * @param serviceInfo.type Service instance type
51      * @param serviceInfo.port Service instance port
52      * @param cb callback object
53      * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error
54      * @permission ohos.permission.CONNECTIVITY_INTERNAL
55      * @systemapi Hide this for inner system use.
56      */
57     int32_t RegisterService(const MDnsServiceInfo &serviceInfo, const sptr<IRegistrationCallback> &cb);
58 
59     /**
60      * UnRegister mDNS service instance
61      *
62      * @param cb callback object used in RegisterService
63      * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error
64      * @permission ohos.permission.CONNECTIVITY_INTERNAL
65      * @systemapi Hide this for inner system use.
66      */
67     int32_t UnRegisterService(const sptr<IRegistrationCallback> &cb);
68 
69     /**
70      * Browse mDNS service instance by service type
71      *
72      * @param serviceType Service instance type
73      * @param cb callback object
74      * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error
75      * @permission ohos.permission.CONNECTIVITY_INTERNAL
76      * @systemapi Hide this for inner system use.
77      */
78     int32_t StartDiscoverService(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb);
79 
80     /**
81      * Stop browse mDNS service instance by service type
82      *
83      * @param cb callback object used in StartDiscoverService
84      * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error
85      * @permission ohos.permission.CONNECTIVITY_INTERNAL
86      * @systemapi Hide this for inner system use.
87      */
88     int32_t StopDiscoverService(const sptr<IDiscoveryCallback> &cb);
89 
90     /**
91      * Resolve browse mDNS service instance by service type and name
92      *
93      * @param serviceInfo.name Service instance name
94      * @param serviceInfo.type Service instance type
95      * @param serviceInfo.port Service instance port
96      * @param cb callback object
97      * @return Return NETMANAGER_EXT_SUCCESS if process normal, others is error
98      * @permission ohos.permission.CONNECTIVITY_INTERNAL
99      * @systemapi Hide this for inner system use.
100      */
101     int32_t ResolveService(const MDnsServiceInfo &serviceInfo, const sptr<IResolveCallback> &cb);
102 
103     void RestartResume();
104 
105 private:
106     class MDnsDeathRecipient : public IRemoteObject::DeathRecipient {
107     public:
MDnsDeathRecipient(MDnsClient & client)108         explicit MDnsDeathRecipient(MDnsClient &client) : client_(client) {}
109         ~MDnsDeathRecipient() = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)110         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
111         {
112             client_.OnRemoteDied(remote);
113         }
114 
115     private:
116         MDnsClient &client_;
117     };
118 
119     sptr<IRemoteObject> LoadSaOnDemand();
120     sptr<IMDnsService> GetProxy();
121     void OnRemoteDied(const wptr<IRemoteObject> &remote);
122 
123     std::mutex mutex_;
124     sptr<IMDnsService> mdnsService_ = nullptr;
125     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
126     sptr<OnDemandLoadCallback> loadCallback_ = nullptr;
127 };
128 } // namespace NetManagerStandard
129 } // namespace OHOS
130 #endif // MDNS_CLIENT_H
131