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 // Implementation of client side of IPC.
17 
18 #ifndef TUNNEL_CLIENT_H
19 #define TUNNEL_CLIENT_H
20 
21 #include <memory>
22 
23 #include "i_tunnel_client.h"
24 #include "i_intention.h"
25 
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29 class TunnelClient final : public ITunnelClient, public std::enable_shared_from_this<TunnelClient> {
30 public:
31     TunnelClient() = default;
32     ~TunnelClient();
33     DISALLOW_COPY_AND_MOVE(TunnelClient);
34 
35     // Request to enable the service identified by [`intention`].
36     int32_t Enable(Intention intention, ParamBase &data, ParamBase &reply) override;
37     // Request to disable the service identified by [`intention`].
38     int32_t Disable(Intention intention, ParamBase &data, ParamBase &reply) override;
39     // Request to start the service identified by [`intention`].
40     int32_t Start(Intention intention, ParamBase &data, ParamBase &reply) override;
41     // Request to stop the service identified by [`intention`].
42     int32_t Stop(Intention intention, ParamBase &data, ParamBase &reply) override;
43     // Request to add a watch of state of service, with the service identified by
44     // [`intention`], the state to watch identified by [`id`], parameters packed in
45     // [`data`] parcel.
46     int32_t AddWatch(Intention intention, uint32_t id, ParamBase &data, ParamBase &reply) override;
47     // Request to remove a watch of state of service.
48     int32_t RemoveWatch(Intention intention, uint32_t id, ParamBase &data, ParamBase &reply) override;
49     // Request to set a parameter of service, with the service identified by
50     // [`intention`], the parameter identified by [`id`], and values packed in
51     // [`data`] parcel.
52     int32_t SetParam(Intention intention, uint32_t id, ParamBase &data, ParamBase &reply) override;
53     // Request to get a parameter of service, with the service identified by
54     // [`intention`], the parameter identified by [`id`].
55     int32_t GetParam(Intention intention, uint32_t id, ParamBase &data, ParamBase &reply) override;
56     // Request to interact with service identified by [`intention`] for general purpose.
57     // This interface supplements functions of previous intefaces. Functionalities of
58     // this interface is service spicific.
59     int32_t Control(Intention intention, uint32_t id, ParamBase &data, ParamBase &reply) override;
60 
61 private:
62     class DeathRecipient : public IRemoteObject::DeathRecipient {
63     public:
64         DeathRecipient(std::shared_ptr<TunnelClient> parent);
65         ~DeathRecipient() = default;
66         void OnRemoteDied(const wptr<IRemoteObject> &remote);
67 
68     private:
69         std::weak_ptr<TunnelClient> parent_;
70     };
71 
72     ErrCode Connect();
73     void ResetProxy(const wptr<IRemoteObject> &remote);
74 
75 private:
76     std::mutex mutex_;
77     sptr<IIntention> devicestatusProxy_ { nullptr };
78     sptr<IRemoteObject::DeathRecipient> deathRecipient_ { nullptr };
79 };
80 } // namespace DeviceStatus
81 } // namespace Msdp
82 } // namespace OHOS
83 #endif // TUNNEL_CLIENT_H
84