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_SERVICE_H 17 #define MDNS_SERVICE_H 18 19 #include <cstdint> 20 #include <iosfwd> 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include "refbase.h" 26 #include "singleton.h" 27 #include "system_ability.h" 28 29 #include "i_mdns_event.h" 30 #include "mdns_service_stub.h" 31 #include "mdns_manager.h" 32 #include "net_interface_callback.h" 33 34 namespace OHOS { 35 namespace NetManagerStandard { 36 37 class MDnsService : public SystemAbility, public MDnsServiceStub { 38 DECLARE_DELAYED_SINGLETON(MDnsService) 39 DECLARE_SYSTEM_ABILITY(MDnsService) 40 41 public: 42 enum ServiceRunningState { 43 STATE_STOPPED = 0, 44 STATE_RUNNING, 45 }; 46 47 int32_t RegisterService(const MDnsServiceInfo &serviceInfo, const sptr<IRegistrationCallback> &cb) override; 48 int32_t UnRegisterService(const sptr<IRegistrationCallback> &cb) override; 49 50 int32_t StartDiscoverService(const std::string &serviceType, const sptr<IDiscoveryCallback> &cb) override; 51 int32_t StopDiscoverService(const sptr<IDiscoveryCallback> &cb) override; 52 53 int32_t ResolveService(const MDnsServiceInfo &serviceInfo, const sptr<IResolveCallback> &cb) override; 54 55 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 56 57 protected: 58 void OnStart() override; 59 void OnStop() override; 60 int32_t OnIdle(const SystemAbilityOnDemandReason &idleReason) override; 61 62 private: 63 bool Init(); 64 65 bool isRegistered_; 66 ServiceRunningState state_; 67 sptr<INetInterfaceStateCallback> netStateCallback_; 68 69 private: 70 class MdnsCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 71 public: MdnsCallbackDeathRecipient(MDnsService & client)72 explicit MdnsCallbackDeathRecipient(MDnsService &client) : client_(client) {} 73 ~MdnsCallbackDeathRecipient() override = default; OnRemoteDied(const wptr<IRemoteObject> & remote)74 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 75 { 76 client_.OnRemoteDied(remote); 77 } 78 79 private: 80 MDnsService &client_; 81 }; 82 void OnRemoteDied(const wptr<IRemoteObject> &remoteObject); 83 void AddClientDeathRecipient(const sptr<IDiscoveryCallback> &cb); 84 void RemoveClientDeathRecipient(const sptr<IDiscoveryCallback> &cb); 85 void UnloadSystemAbility(); 86 void RemoveALLClientDeathRecipient(); 87 std::mutex remoteMutex_; 88 sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr; 89 std::vector<sptr<IDiscoveryCallback>> remoteCallback_; 90 }; 91 } // namespace NetManagerStandard 92 } // namespace OHOS 93 94 #endif // MDNS_SERVICE_H