• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GNSS_ABILITY_H
17 #define GNSS_ABILITY_H
18 #ifdef FEATURE_GNSS_SUPPORT
19 
20 #include <mutex>
21 #include <singleton.h>
22 #include <v1_0/ignss_interface.h>
23 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE
24 #include <v1_0/ia_gnss_interface.h>
25 #endif
26 
27 #include "event_handler.h"
28 #include "system_ability.h"
29 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE
30 #include "agnss_event_callback.h"
31 #endif
32 #include "common_utils.h"
33 #include "constant_definition.h"
34 #include "gnss_ability_skeleton.h"
35 #include "i_gnss_status_callback.h"
36 #include "i_nmea_message_callback.h"
37 #include "subability_common.h"
38 
39 namespace OHOS {
40 namespace Location {
41 #ifdef __aarch64__
42 #define VENDOR_GNSS_ADAPTER_SO_PATH "/system/lib64/vendorGnssAdapter.so"
43 #else
44 #define VENDOR_GNSS_ADAPTER_SO_PATH "/system/lib/vendorGnssAdapter.so"
45 #endif
46 
47 using HDI::Location::Gnss::V1_0::IGnssInterface;
48 using HDI::Location::Gnss::V1_0::IGnssCallback;
49 using HDI::Location::Gnss::V1_0::GNSS_START_TYPE_NORMAL;
50 using HDI::Location::Gnss::V1_0::GNSS_STATUS_NONE;
51 using HDI::Location::Gnss::V1_0::GNSS_STATUS_SESSION_BEGIN;
52 using HDI::Location::Gnss::V1_0::GNSS_STATUS_SESSION_END;
53 using HDI::Location::Gnss::V1_0::GNSS_STATUS_ENGINE_ON;
54 using HDI::Location::Gnss::V1_0::GNSS_STATUS_ENGINE_OFF;
55 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE
56 using HDI::Location::Agnss::V1_0::IAGnssInterface;
57 using HDI::Location::Agnss::V1_0::IAGnssCallback;
58 using HDI::Location::Agnss::V1_0::AGNSS_TYPE_SUPL;
59 using HDI::Location::Agnss::V1_0::AGnssServerInfo;
60 #endif
61 
62 class GnssHandler : public AppExecFwk::EventHandler {
63 public:
64     explicit GnssHandler(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
65     ~GnssHandler() override;
66     void ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) override;
67 };
68 
69 class GnssAbility : public SystemAbility, public GnssAbilityStub, public SubAbility, DelayedSingleton<GnssAbility> {
70 DECLEAR_SYSTEM_ABILITY(GnssAbility);
71 
72 public:
73     DISALLOW_COPY_AND_MOVE(GnssAbility);
74     GnssAbility();
75     ~GnssAbility() override;
76     void OnStart() override;
77     void OnStop() override;
QueryServiceState()78     ServiceRunningState QueryServiceState() const
79     {
80         return state_;
81     }
82     LocationErrCode SendLocationRequest(WorkRecord &workrecord) override;
83     LocationErrCode SetEnable(bool state) override;
84     LocationErrCode RefrashRequirements() override;
85     LocationErrCode RegisterGnssStatusCallback(const sptr<IRemoteObject>& callback, pid_t uid) override;
86     LocationErrCode UnregisterGnssStatusCallback(const sptr<IRemoteObject>& callback) override;
87     LocationErrCode RegisterNmeaMessageCallback(const sptr<IRemoteObject>& callback, pid_t uid) override;
88     LocationErrCode UnregisterNmeaMessageCallback(const sptr<IRemoteObject>& callback) override;
89     LocationErrCode RegisterCachedCallback(const std::unique_ptr<CachedGnssLocationsRequest>& request,
90         const sptr<IRemoteObject>& callback) override;
91     LocationErrCode UnregisterCachedCallback(const sptr<IRemoteObject>& callback) override;
92     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
93     LocationErrCode GetCachedGnssLocationsSize(int &size) override;
94     LocationErrCode FlushCachedGnssLocations() override;
95     LocationErrCode SendCommand(std::unique_ptr<LocationCommand>& commands) override;
96     LocationErrCode AddFence(std::unique_ptr<GeofenceRequest>& request) override;
97     LocationErrCode RemoveFence(std::unique_ptr<GeofenceRequest>& request) override;
98     void ReportGnssSessionStatus(int status);
99     void ReportNmea(int64_t timestamp, const std::string &nmea);
100     void ReportSv(const std::unique_ptr<SatelliteStatus> &sv);
101     LocationErrCode EnableMock() override;
102     LocationErrCode DisableMock() override;
103     LocationErrCode SetMocked(const int timeInterval, const std::vector<std::shared_ptr<Location>> &location) override;
104     void RequestRecord(WorkRecord &workRecord, bool isAdded) override;
105     void SendReportMockLocationEvent() override;
106     void SendMessage(uint32_t code, MessageParcel &data, MessageParcel &reply) override;
107     void UnloadGnssSystemAbility() override;
108     void StartGnss();
109     void StopGnss();
110     bool EnableGnss();
111     void DisableGnss();
112     bool ConnectHdi();
113     bool RemoveHdi();
114 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE
115     void SetAgnssServer();
116     void SetAgnssCallback();
117     void SetSetId(const SubscriberSetId& id);
118     void SetRefInfo(const AGnssRefInfo& refInfo);
119 #endif
120     bool IsMockEnabled();
121     void ProcessReportLocationMock();
122     void ReConnectHdi();
123 private:
124     bool Init();
125     static void SaDumpInfo(std::string& result);
126     bool IsGnssEnabled();
127     int32_t ReportMockedLocation(const std::shared_ptr<Location> location);
128     bool CheckIfGnssConnecting();
129     bool IsMockProcessing();
130     void RegisterLocationHdiDeathRecipient();
131 
132     bool isHdiConnected_;
133     size_t mockLocationIndex_ = 0;
134     bool registerToAbility_ = false;
135     int gnssWorkingStatus_ = 0;
136     std::shared_ptr<GnssHandler> gnssHandler_;
137     ServiceRunningState state_ = ServiceRunningState::STATE_NOT_START;
138     std::mutex gnssMutex_;
139     std::mutex nmeaMutex_;
140     std::mutex hdiMutex_;
141     std::unique_ptr<std::map<pid_t, sptr<IGnssStatusCallback>>> gnssStatusCallback_;
142     std::unique_ptr<std::map<pid_t, sptr<INmeaMessageCallback>>> nmeaCallback_;
143     sptr<IGnssInterface> gnssInterface_;
144     sptr<IGnssCallback> gnssCallback_;
145 #ifdef HDF_DRIVERS_INTERFACE_AGNSS_ENABLE
146     sptr<IAGnssCallback> agnssCallback_;
147     sptr<IAGnssInterface> agnssInterface_;
148 #endif
149 };
150 
151 class LocationHdiDeathRecipient : public IRemoteObject::DeathRecipient {
152 public:
153     void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
154     LocationHdiDeathRecipient();
155     ~LocationHdiDeathRecipient() override;
156 };
157 } // namespace Location
158 } // namespace OHOS
159 #endif // FEATURE_GNSS_SUPPORT
160 #endif // GNSS_ABILITY_H
161