• 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 #include "config.h"
17 #include <thread>
18 #include "common/common_macro.h"
19 #include "common/event_comm.h"
20 #include "common/media_log.h"
21 #include "json_parser.h"
22 
23 namespace OHOS {
24 namespace Sharing {
GetConfig(SharingData::Ptr & datas)25 int32_t Config::GetConfig(SharingData::Ptr &datas)
26 {
27     SHARING_LOGD("trace.");
28     datas = datas_;
29     return CONFIGURE_ERROR_NONE;
30 }
31 
GetConfig(const std::string & module,SharingDataGroupByModule::Ptr & values)32 int32_t Config::GetConfig(const std::string &module, SharingDataGroupByModule::Ptr &values)
33 {
34     SHARING_LOGD("trace.");
35     std::shared_lock<std::shared_mutex> lk(mutex_);
36     if (datas_ == nullptr || !datas_->HasModule(module)) {
37         SHARING_LOGE("data_s is null or has no module named %{public}s.", module.c_str());
38         return CONFIGURE_ERROR_NOT_FIND;
39     }
40 
41     return datas_->GetSharingValues(values, module);
42 }
43 
GetConfig(const std::string & module,const std::string & tag,SharingDataGroupByTag::Ptr & values)44 int32_t Config::GetConfig(const std::string &module, const std::string &tag, SharingDataGroupByTag::Ptr &values)
45 {
46     SHARING_LOGD("trace.");
47     SharingDataGroupByModule::Ptr modelValue = nullptr;
48     if (GetConfig(module, modelValue) != CONFIGURE_ERROR_NONE) {
49         SHARING_LOGE("module %{public}s error.", module.c_str());
50         return CONFIGURE_ERROR_NOT_FIND;
51     }
52 
53     std::shared_lock<std::shared_mutex> lk(mutex_);
54     return datas_->GetSharingValues(values, module, tag);
55 }
56 
GetConfig(const std::string & module,const std::string & tag,const std::string & key,SharingValue::Ptr & value)57 int32_t Config::GetConfig(const std::string &module, const std::string &tag, const std::string &key,
58                           SharingValue::Ptr &value)
59 {
60     SHARING_LOGD("trace.");
61     SharingDataGroupByTag::Ptr tagValue = nullptr;
62     if (GetConfig(module, tag, tagValue) != CONFIGURE_ERROR_NONE) {
63         SHARING_LOGE("module %{public}s, tag %{public}s error.", module.c_str(), tag.c_str());
64         return CONFIGURE_ERROR_NOT_FIND;
65     }
66 
67     std::shared_lock<std::shared_mutex> lk(mutex_);
68     SharingValue::Ptr valueTmp = datas_->GetSharingValue(key, module, tag);
69     if (valueTmp == nullptr) {
70         SHARING_LOGE("sharing value is null.");
71         return CONFIGURE_ERROR_NOT_FIND;
72     }
73 
74     value = valueTmp;
75     return CONFIGURE_ERROR_NONE;
76 }
77 
78 
SetConfig(const std::string & module,const SharingDataGroupByModule::Ptr & values)79 int32_t Config::SetConfig(const std::string &module, const SharingDataGroupByModule::Ptr &values)
80 {
81     SHARING_LOGD("trace.");
82     std::unique_lock<std::shared_mutex> lk(mutex_);
83     RETURN_INVALID_IF_NULL(datas_);
84     auto err = datas_->PutSharingValues(values, module);
85     if (err == CONFIGURE_ERROR_NONE) {
86         SaveConfig();
87     }
88 
89     return err;
90 }
91 
SetConfig(const std::string & module,const std::string & tag,const SharingDataGroupByTag::Ptr & values)92 int32_t Config::SetConfig(const std::string &module, const std::string &tag, const SharingDataGroupByTag::Ptr &values)
93 {
94     SHARING_LOGD("trace.");
95     SharingDataGroupByModule::Ptr modelValue = nullptr;
96     if (GetConfig(module, modelValue) != CONFIGURE_ERROR_NONE) {
97         modelValue = std::make_shared<SharingDataGroupByModule>(module);
98     }
99 
100     if (modelValue == nullptr) {
101         SHARING_LOGE("modelValue value is null.");
102         return CONFIGURE_ERROR_NOT_FIND;
103     }
104 
105     std::unique_lock<std::shared_mutex> lk(mutex_);
106     auto err = modelValue->PutSharingValues(tag, values);
107     if (err == CONFIGURE_ERROR_NONE) {
108         SaveConfig();
109     }
110 
111     return err;
112 }
113 
SetConfig(const std::string & module,const std::string & tag,const std::string & key,const SharingValue::Ptr & value)114 int32_t Config::SetConfig(const std::string &module, const std::string &tag, const std::string &key,
115                           const SharingValue::Ptr &value)
116 {
117     SHARING_LOGD("trace.");
118     RETURN_INVALID_IF_NULL(datas_);
119     if (!datas_->HasModule(module)) {
120         auto modelValue = std::make_shared<SharingDataGroupByModule>(module);
121         modelValue->PutSharingValue(tag, key, value);
122         return SetConfig(module, modelValue);
123     }
124 
125     if (!datas_->HasTag(module, tag)) {
126         auto tagValue = std::make_shared<SharingDataGroupByTag>(tag);
127         tagValue->PutSharingValue(key, value);
128         return SetConfig(module, tag, tagValue);
129     }
130 
131     datas_->PutSharingValue(key, value, module, tag);
132     SaveConfig();
133     return CONFIGURE_ERROR_NONE;
134 }
135 
ReadConfig(void)136 bool Config::ReadConfig(void)
137 {
138     SHARING_LOGD("trace.");
139     std::thread read([this] {
140         std::unique_lock<std::shared_mutex> lock(mutex_);
141         status_ = ConfigStatus::CONFIG_STATUS_READING;
142         JsonParser parser;
143         parser.GetConfig(datas_);
144         status_ = ConfigStatus::CONFIG_STATUS_READY;
145         EmitEvent();
146     });
147 
148     if (read.joinable()) {
149         read.join();
150     }
151 
152     return true;
153 }
154 
SaveConfig(void)155 bool Config::SaveConfig(void)
156 {
157     SHARING_LOGD("trace.");
158     std::thread read([this] {
159         std::unique_lock<std::shared_mutex> lock(mutex_);
160         status_ = ConfigStatus::CONFIG_STATUS_WRITING;
161         JsonParser parser;
162         parser.SaveConfig(datas_);
163         status_ = ConfigStatus::CONFIG_STATUS_READY;
164     });
165 
166     read.detach();
167     return true;
168 }
169 
EmitEvent(const ConfigStatus type,const ModuleType toModule)170 void Config::EmitEvent(const ConfigStatus type, const ModuleType toModule)
171 {
172     SHARING_LOGD("trace.");
173     SharingEvent evt;
174     evt.eventMsg = std::make_shared<ConfigEventMsg>(type, toModule);
175     evt.eventMsg->type = EventType::EVENT_CONFIGURE_READY;
176     emiter_.SendEvent(evt);
177 }
178 
EmitEvent(const EventType type,const ModuleType toModule,const SharingDataGroupByModule::Ptr & data)179 void Config::EmitEvent(const EventType type, const ModuleType toModule, const SharingDataGroupByModule::Ptr &data)
180 {
181     SHARING_LOGD("trace.");
182     SharingEvent evt;
183     evt.eventMsg = std::make_shared<ConfigEventMsg>(data, toModule);
184     evt.eventMsg->type = type;
185     emiter_.SendEvent(evt);
186 }
187 
Init(void)188 void Config::Init(void)
189 {
190     SHARING_LOGD("trace.");
191     {
192         std::unique_lock<std::shared_mutex> lock(mutex_);
193         if (status_ != ConfigStatus::CONFIG_STATUS_INVALID) {
194             SHARING_LOGE("init CONFIG_STATUS_INVALID.");
195             return;
196         }
197         status_ = ConfigStatus::CONFIG_STATUS_INITED;
198     }
199 
200     ReadConfig();
201 }
202 
203 } // namespace Sharing
204 } // namespace OHOS