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 #ifndef LOCATOR_AGENT_H 16 #define LOCATOR_AGENT_H 17 18 #include "request_config.h" 19 #include "i_locator_callback.h" 20 #include "constant_definition.h" 21 22 #include <singleton.h> 23 #include "iremote_object.h" 24 #include "iremote_proxy.h" 25 #include "iremote_broker.h" 26 #include "i_locator.h" 27 28 #include "native_location_callback_host.h" 29 #include "native_sv_callback_host.h" 30 #include "native_nmea_callback_host.h" 31 32 namespace OHOS { 33 namespace Location { 34 class LocatorAgent : public IRemoteProxy<ILocator> { 35 public: 36 explicit LocatorAgent(const sptr<IRemoteObject> &impl); 37 ~LocatorAgent() = default; 38 LocationErrCode StartGnssLocating(sptr<ILocatorCallback>& callback); 39 LocationErrCode StopGnssLocating(sptr<ILocatorCallback>& callback); 40 LocationErrCode RegisterNmeaMessageCallback(const sptr<INmeaMessageCallback>& callback); 41 LocationErrCode UnregisterNmeaMessageCallback(const sptr<INmeaMessageCallback>& callback); 42 LocationErrCode RegisterGnssStatusCallback(const sptr<IGnssStatusCallback>& callback); 43 LocationErrCode UnregisterGnssStatusCallback(const sptr<IGnssStatusCallback>& callback); 44 private: 45 LocationErrCode SendRequestToStub(const int msgId, MessageParcel& data, MessageParcel& reply); 46 47 static inline BrokerDelegator<LocatorAgent> delegator_; 48 }; 49 50 class LocatorAgentManager : DelayedSingleton<LocatorAgentManager> { 51 public: 52 explicit LocatorAgentManager(); 53 ~LocatorAgentManager(); 54 55 /** 56 * @brief Subscribe location changed. 57 * 58 * @param callback Indicates the callback for reporting the location result. 59 */ 60 void StartGnssLocating(const LocationCallbackIfaces& callback); 61 62 /** 63 * @brief Subscribe satellite status changed. 64 * 65 * @param callback Indicates the callback for reporting the satellite status. 66 */ 67 void RegisterGnssStatusCallback(const SvStatusCallbackIfaces& callback); 68 69 /** 70 * @brief Subscribe nmea message changed. 71 * 72 * @param callback Indicates the callback for reporting the nmea message. 73 */ 74 void RegisterNmeaMessageCallback(const GnssNmeaCallbackIfaces& callback); 75 76 /** 77 * @brief Unsubscribe location changed. 78 */ 79 void StopGnssLocating(); 80 81 /** 82 * @brief Unsubscribe nmea message changed. 83 */ 84 void UnregisterNmeaMessageCallback(); 85 86 /** 87 * @brief Unsubscribe satellite status changed. 88 */ 89 void UnregisterGnssStatusCallback(); 90 void ResetLocatorAgent(const wptr<IRemoteObject> &remote); 91 private: 92 class LocatorAgentDeathRecipient : public IRemoteObject::DeathRecipient { 93 public: LocatorAgentDeathRecipient(LocatorAgentManager & impl)94 explicit LocatorAgentDeathRecipient(LocatorAgentManager &impl) : impl_(impl) {} 95 ~LocatorAgentDeathRecipient() override = default; OnRemoteDied(const wptr<IRemoteObject> & remote)96 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 97 { 98 impl_.ResetLocatorAgent(remote); 99 } 100 private: 101 LocatorAgentManager &impl_; 102 }; 103 104 sptr<LocatorAgent> GetLocatorAgent(); 105 sptr<IRemoteObject> CheckLocatorSystemAbilityLoaded(); 106 bool TryLoadLocatorSystemAbility(); 107 sptr<LocatorAgent> InitLocatorAgent(sptr<IRemoteObject>& saObject); 108 109 sptr<LocatorAgent> client_ { nullptr }; 110 sptr<IRemoteObject::DeathRecipient> recipient_ { nullptr }; 111 std::mutex mutex_; 112 sptr<NativeLocationCallbackHost> locationCallbackHost_; 113 sptr<NativeNmeaCallbackHost> nmeaCallbackHost_; 114 sptr<NativeSvCallbackHost> gnssCallbackHost_; 115 }; 116 } // namespace Location 117 } // namespace OHOS 118 #endif // LOCATOR_AGENT_H