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 {
51 public:
52     static LocatorAgentManager* GetInstance();
53     explicit LocatorAgentManager();
54     ~LocatorAgentManager();
55 
56     /**
57      * @brief Subscribe location changed.
58      *
59      * @param callback Indicates the callback for reporting the location result.
60      */
61     void StartGnssLocating(const LocationCallbackIfaces& callback);
62 
63     /**
64      * @brief Subscribe satellite status changed.
65      *
66      * @param callback Indicates the callback for reporting the satellite status.
67      */
68     void RegisterGnssStatusCallback(const SvStatusCallbackIfaces& callback);
69 
70     /**
71      * @brief Subscribe nmea message changed.
72      *
73      * @param callback Indicates the callback for reporting the nmea message.
74      */
75     void RegisterNmeaMessageCallback(const GnssNmeaCallbackIfaces& callback);
76 
77     /**
78      * @brief Unsubscribe location changed.
79      */
80     void StopGnssLocating();
81 
82     /**
83      * @brief Unsubscribe nmea message changed.
84      */
85     void UnregisterNmeaMessageCallback();
86 
87     /**
88      * @brief Unsubscribe satellite status changed.
89      */
90     void UnregisterGnssStatusCallback();
91     void ResetLocatorAgent(const wptr<IRemoteObject> &remote);
92 private:
93     class LocatorAgentDeathRecipient : public IRemoteObject::DeathRecipient {
94     public:
LocatorAgentDeathRecipient(LocatorAgentManager & impl)95         explicit LocatorAgentDeathRecipient(LocatorAgentManager &impl) : impl_(impl) {}
96         ~LocatorAgentDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)97         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
98         {
99             impl_.ResetLocatorAgent(remote);
100         }
101     private:
102         LocatorAgentManager &impl_;
103     };
104 
105     sptr<LocatorAgent> GetLocatorAgent();
106     sptr<IRemoteObject> CheckLocatorSystemAbilityLoaded();
107     bool TryLoadLocatorSystemAbility();
108     sptr<LocatorAgent> InitLocatorAgent(sptr<IRemoteObject>& saObject);
109 
110     sptr<LocatorAgent> client_ { nullptr };
111     sptr<IRemoteObject::DeathRecipient> recipient_ { nullptr };
112     std::mutex mutex_;
113     sptr<NativeLocationCallbackHost> locationCallbackHost_;
114     sptr<NativeNmeaCallbackHost> nmeaCallbackHost_;
115     sptr<NativeSvCallbackHost> gnssCallbackHost_;
116 };
117 }  // namespace Location
118 }  // namespace OHOS
119 #endif // LOCATOR_AGENT_H