• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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_SHARING_CONFIG_H
17 #define OHOS_SHARING_CONFIG_H
18 
19 #include <shared_mutex>
20 #include <singleton.h>
21 #include <string>
22 #include "event/event_base.h"
23 #include "sharing_data.h"
24 
25 namespace OHOS {
26 namespace Sharing {
27 
28 enum class ConfigStatus {
29     CONFIG_STATUS_INVALID,
30     CONFIG_STATUS_INITED,
31     CONFIG_STATUS_READING,
32     CONFIG_STATUS_WRITING,
33     CONFIG_STATUS_READY
34 };
35 
36 struct ConfigEventMsg : EventMsg {
ConfigEventMsgConfigEventMsg37     ConfigEventMsg(const ConfigStatus status, const ModuleType toMudule)
38     {
39         this->status = status;
40         this->toMgr = toMudule;
41         this->fromMgr = ModuleType::MODULE_CONFIGURE;
42         this->data = nullptr;
43     }
44 
ConfigEventMsgConfigEventMsg45     ConfigEventMsg(const SharingDataGroupByModule::Ptr &data, const ModuleType toMudule)
46     {
47         this->status = ConfigStatus::CONFIG_STATUS_READY;
48         this->data = data;
49         this->fromMgr = ModuleType::MODULE_CONFIGURE;
50         this->toMgr = toMudule;
51     }
52 
53     ConfigStatus status;
54     SharingDataGroupByModule::Ptr data;
55 };
56 
57 class Config : public Singleton<Config> {
58 public:
59     Config() = default;
60     ~Config() = default;
61 
62     void Init(void);
63     int32_t GetConfig(SharingData::Ptr &datas);
64     int32_t GetConfig(const std::string &module, SharingDataGroupByModule::Ptr &values);
65     int32_t GetConfig(const std::string &module, const std::string &tag, SharingDataGroupByTag::Ptr &values);
66     int32_t GetConfig(const std::string &module, const std::string &tag, const std::string &key,
67                       SharingValue::Ptr &value);
68 
69     int32_t SetConfig(const SharingData::Ptr &datas);
70     int32_t SetConfig(const std::string &module, const SharingDataGroupByModule::Ptr &values);
71     int32_t SetConfig(const std::string &module, const std::string &tag, const SharingDataGroupByTag::Ptr &values);
72     int32_t SetConfig(const std::string &module, const std::string &tag, const std::string &key,
73                       const SharingValue::Ptr &value);
74 
75 private:
76     bool ReadConfig(void);
77     bool SaveConfig(void);
78     void EmitEvent(const EventType type, const ModuleType toModule, const SharingDataGroupByModule::Ptr &data);
79     void EmitEvent(const ConfigStatus type = ConfigStatus::CONFIG_STATUS_READY,
80                    const ModuleType toModule = ModuleType::MODULE_DATACENTER);
81 
82 private:
83     std::shared_mutex mutex_;
84     std::string fileName_ = "/etc/sharing_config.json";
85 
86     EventEmitter emiter_;
87     SharingData::Ptr datas_ = std::make_shared<SharingData>();
88     ConfigStatus status_ = ConfigStatus::CONFIG_STATUS_INVALID;
89 };
90 
91 } // namespace Sharing
92 } // namespace OHOS
93 #endif