1/*
2 * Copyright (C) 2024 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 GEOFENCE_SDK_H
16#define GEOFENCE_SDK_H
17
18#include "iremote_object.h"
19#include "iremote_proxy.h"
20#include "iremote_broker.h"
21#include "i_locator.h"
22#include "geofence_request.h"
23#include "constant_definition.h"
24#include "locationhub_ipc_interface_code.h"
25#include "constant_definition.h"
26
27namespace OHOS {
28namespace Location {
29class GeofenceSdk : public IRemoteProxy<ILocator> {
30public:
31    explicit GeofenceSdk(const sptr<IRemoteObject> &impl);
32    ~GeofenceSdk() = default;
33    LocationErrCode AddFenceV9(std::shared_ptr<GeofenceRequest>& request);
34    LocationErrCode RemoveFenceV9(std::shared_ptr<GeofenceRequest>& request);
35    LocationErrCode AddGnssGeofence(std::shared_ptr<GeofenceRequest>& request);
36    LocationErrCode RemoveGnssGeofence(std::shared_ptr<GeofenceRequest>& request);
37    LocationErrCode GetGeofenceSupportedCoordTypes(
38        std::vector<CoordinateSystemType>& coordinateSystemTypes);
39    LocationErrCode HandleGnssfenceRequest(LocatorInterfaceCode code,
40        std::shared_ptr<GeofenceRequest>& request);
41private:
42	LocationErrCode SendMsgWithDataReplyV9(const int msgId, MessageParcel& data, MessageParcel& reply);
43    static inline BrokerDelegator<GeofenceSdk> delegator_;
44};
45
46class GeofenceManager {
47public:
48    static GeofenceManager* GetInstance();
49    explicit GeofenceManager();
50    ~GeofenceManager();
51
52    /**
53     * @brief Add a geofence and subscribe geo fence status changed.
54     *
55     * @param request Indicates the Geofence configuration parameters.
56     * @return Returns ERRCODE_SUCCESS if the fence has been added successfully.
57     */
58    LocationErrCode AddFenceV9(std::shared_ptr<GeofenceRequest>& request);
59    /**
60     * @brief Remove a geofence and unsubscribe geo fence status changed.
61     *
62     * @param request Indicates the Geofence configuration parameters.
63     * @return Returns ERRCODE_SUCCESS if the fence has been removed successfully.
64     */
65    LocationErrCode RemoveFenceV9(std::shared_ptr<GeofenceRequest>& request);
66    LocationErrCode AddGnssGeofence(std::shared_ptr<GeofenceRequest>& request);
67    LocationErrCode RemoveGnssGeofence(std::shared_ptr<GeofenceRequest>& request);
68    LocationErrCode GetGeofenceSupportedCoordTypes(
69        std::vector<CoordinateSystemType>& coordinateSystemTypes);
70    void ResetGeofenceSdkProxy(const wptr<IRemoteObject> &remote);
71private:
72    sptr<GeofenceSdk> GetProxy();
73
74    sptr<GeofenceSdk> client_ { nullptr };
75    sptr<IRemoteObject::DeathRecipient> recipient_ { nullptr };
76    std::mutex mutex_;
77    bool isServerExist_ = false;
78
79private:
80    class GeofenceManagerDeathRecipient : public IRemoteObject::DeathRecipient {
81    public:
82        explicit GeofenceManagerDeathRecipient(GeofenceManager &impl) : impl_(impl) {}
83        ~GeofenceManagerDeathRecipient() override = default;
84        void OnRemoteDied(const wptr<IRemoteObject> &remote) override
85        {
86            impl_.ResetGeofenceSdkProxy(remote);
87        }
88    private:
89        GeofenceManager &impl_;
90    };
91};
92}  // namespace Location
93}  // namespace OHOS
94#endif // GEOFENCE_SDK_H