1 /*
2  * Copyright (C) 2022-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 #ifndef PASTE_BOARD_DISTRIBUTE_MODULE_CONFIG_H
17 #define PASTE_BOARD_DISTRIBUTE_MODULE_CONFIG_H
18 
19 #include "api/visibility.h"
20 #include <atomic>
21 #include <functional>
22 #include "device/dm_adapter.h"
23 
24 namespace OHOS {
25 namespace MiscServices {
26 class API_EXPORT DistributedModuleConfig : protected DMAdapter::DMObserver {
27 public:
28     using Observer = std::function<void(bool isOn)>;
29     bool IsOn();
30     void Watch(Observer observer);
31     void Init();
32     void DeInit();
33 protected:
34     void Online(const std::string &device) override;
35     void Offline(const std::string &device) override;
36     void OnReady(const std::string &device) override;
37 private:
38     int32_t GetEnabledStatus();
39     void Notify();
40     void GetRetryTask();
41     size_t GetDeviceNum();
42     Observer observer_ = nullptr;
43     bool status_ = false;
44     std::atomic<bool> retrying_ = false;
45     static constexpr uint32_t RETRY_TIMES = 30;
46     static constexpr uint32_t RETRY_INTERVAL = 1000; //milliseconds
47     static constexpr uint32_t RANDOM_MAX = 500; //milliseconds
48     static constexpr uint32_t RANDOM_MIN = 5; //milliseconds
49     static constexpr const char* SUPPORT_STATUS = "1";
50 };
51 } // namespace MiscServices
52 } // namespace OHOS
53 #endif // PASTE_BOARD_DISTRIBUTE_MODULE_CONFIG_H
54