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 OHOS_HDI_LIGHT_V1_0_LIGHTINTERFACEPROXY_H
17#define OHOS_HDI_LIGHT_V1_0_LIGHTINTERFACEPROXY_H
18
19#include "v1_0/ilight_interface.h"
20#include <unistd.h>
21#include <iproxy_broker.h>
22
23namespace OHOS {
24namespace HDI {
25namespace Light {
26namespace V1_0 {
27
28class LightInterfaceProxy : public IProxyBroker<OHOS::HDI::Light::V1_0::ILightInterface> {
29public:
30    class IServiceManagerDeathRecipient : public IRemoteObject::DeathRecipient {
31    public:
32        IServiceManagerDeathRecipient(wptr<OHOS::HDI::Light::V1_0::LightInterfaceProxy> proxy) : proxy_(proxy) {}
33        ~IServiceManagerDeathRecipient() override = default;
34        void OnRemoteDied(const wptr<IRemoteObject> &remote) override
35        {
36            int32_t result = HDF_FAILURE;
37            const int sleepInterval = 500000;
38            const int waitTimes = 10;
39            int currentTime = waitTimes;
40            do {
41                usleep(sleepInterval);
42                auto proxy = proxy_.promote();
43                if (proxy != nullptr) {
44                    result = OHOS::HDI::Light::V1_0::LightInterfaceProxy::Reconnect(proxy);
45                }
46                --currentTime;
47            } while (result != HDF_SUCCESS && currentTime >0);
48        }
49    private:
50        wptr<OHOS::HDI::Light::V1_0::LightInterfaceProxy> proxy_;
51    };
52
53    explicit LightInterfaceProxy(const sptr<IRemoteObject>& remote) : IProxyBroker<OHOS::HDI::Light::V1_0::ILightInterface>(remote) {
54        reconnectRemote_ = nullptr;
55        servMgr_ = nullptr;
56        deathRecipient_ = nullptr;
57        isReconnected_ = false;
58    }
59    virtual ~LightInterfaceProxy() {
60        if (servMgr_ != nullptr && deathRecipient_ != nullptr) {
61            servMgr_->RemoveDeathRecipient(deathRecipient_);
62        }
63    }
64
65    inline bool IsProxy() override
66    {
67        return true;
68    }
69
70    int32_t GetLightInfo(std::vector<OHOS::HDI::Light::V1_0::HdfLightInfo>& info) override;
71
72    int32_t TurnOnLight(int32_t lightId, const OHOS::HDI::Light::V1_0::HdfLightEffect& effect) override;
73
74    int32_t TurnOnMultiLights(int32_t lightId,
75         const std::vector<OHOS::HDI::Light::V1_0::HdfLightColor>& colors) override;
76
77    int32_t TurnOffLight(int32_t lightId) override;
78
79    int32_t GetVersion(uint32_t& majorVer, uint32_t& minorVer) override;
80
81    static int32_t GetLightInfo_(std::vector<OHOS::HDI::Light::V1_0::HdfLightInfo>& info,
82         const sptr<IRemoteObject> remote);
83
84    static int32_t TurnOnLight_(int32_t lightId, const OHOS::HDI::Light::V1_0::HdfLightEffect& effect,
85         const sptr<IRemoteObject> remote);
86
87    static int32_t TurnOnMultiLights_(int32_t lightId, const std::vector<OHOS::HDI::Light::V1_0::HdfLightColor>& colors,
88         const sptr<IRemoteObject> remote);
89
90    static int32_t TurnOffLight_(int32_t lightId, const sptr<IRemoteObject> remote);
91
92    static int32_t GetVersion_(uint32_t& majorVer, uint32_t& minorVer, const sptr<IRemoteObject> remote);
93
94    static int32_t Reconnect(sptr<OHOS::HDI::Light::V1_0::LightInterfaceProxy> proxy);
95
96    sptr<IRemoteObject> GetCurrentRemote() {
97        return isReconnected_ ? reconnectRemote_ : Remote();
98    }
99
100    bool isReconnected_;
101    std::string serviceName_;
102    sptr<IRemoteObject> servMgr_;
103    sptr<OHOS::HDI::Light::V1_0::LightInterfaceProxy::IServiceManagerDeathRecipient> deathRecipient_;
104    sptr<IRemoteObject> reconnectRemote_;
105private:
106    static inline BrokerDelegator<OHOS::HDI::Light::V1_0::LightInterfaceProxy> delegator_;
107};
108
109} // V1_0
110} // Light
111} // HDI
112} // OHOS
113
114#endif // OHOS_HDI_LIGHT_V1_0_LIGHTINTERFACEPROXY_H
115
116