• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef WATCHER_MANAGER_H_
16 #define WATCHER_MANAGER_H_
17 #include <atomic>
18 #include <iostream>
19 #include <map>
20 #include <mutex>
21 #include <thread>
22 #include <vector>
23 
24 #include "iremote_stub.h"
25 #include "iwatcher.h"
26 #include "list.h"
27 #include "message_parcel.h"
28 #include "param_utils.h"
29 #include "parcel.h"
30 #include "param_message.h"
31 #include "system_ability.h"
32 #include "watcher_manager_stub.h"
33 
34 namespace OHOS {
35 namespace init_param {
36 class WatcherNode;
37 class ParamWatcherList;
38 class WatcherGroup;
39 class RemoteWatcher;
40 using WatcherGroupPtr = WatcherGroup *;
41 using RemoteWatcherPtr = RemoteWatcher *;
42 using WatcherNodePtr = WatcherNode *;
43 using ListNodePtr = ListNode *;
44 using ListHeadPtr = ListHead *;
45 using ParamWatcherListPtr = ParamWatcherList *;
46 using ParamWatcherProcessor = std::function<void(ParamWatcherListPtr list, WatcherNodePtr node, uint32_t index)>;
47 
48 class WatcherManager : public SystemAbility, public WatcherManagerStub {
49 public:
50     friend class WatcherGroup;
51     DECLARE_SYSTEM_ABILITY(WatcherManager);
52     DISALLOW_COPY_AND_MOVE(WatcherManager);
53     explicit WatcherManager(int32_t systemAbilityId, bool runOnCreate = true)
SystemAbility(systemAbilityId,runOnCreate)54         : SystemAbility(systemAbilityId, runOnCreate)
55     {
56     }
57     ~WatcherManager() override;
58 
59     // For death event procession
60     class DeathRecipient final : public IRemoteObject::DeathRecipient {
61     public:
DeathRecipient(WatcherManager * manager)62         explicit DeathRecipient(WatcherManager *manager) : manager_(manager) {}
63         ~DeathRecipient() final = default;
64         DISALLOW_COPY_AND_MOVE(DeathRecipient);
OnRemoteDied(const wptr<IRemoteObject> & remote)65         void OnRemoteDied(const wptr<IRemoteObject> &remote) final
66         {
67             manager_->OnRemoteDied(remote);
68         }
69     private:
70         WatcherManager *manager_;
71     };
GetDeathRecipient(void)72     sptr<IRemoteObject::DeathRecipient> GetDeathRecipient(void)
73     {
74         return deathRecipient_;
75     }
76     uint32_t AddRemoteWatcher(uint32_t id, const sptr<IWatcher> &watcher) override;
77     int32_t DelRemoteWatcher(uint32_t remoteWatcherId) override;
78     int32_t AddWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) override;
79     int32_t DelWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) override;
80     int32_t RefreshWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) override;
81 #ifndef STARTUP_INIT_TEST
82 protected:
83 #endif
84     void OnStart() override;
85     void OnStop() override;
86     int Dump(int fd, const std::vector<std::u16string>& args) override;
87 #ifndef STARTUP_INIT_TEST
88 private:
89 #endif
90     void RunLoop();
91     void StartLoop();
92     void StopLoop();
93     void SendLocalChange(const std::string &keyPrefix, RemoteWatcherPtr &remoteWatcher);
94     int SendMessage(WatcherGroupPtr group, int type);
95     int GetServerFd(bool retry);
96     int GetRemoteWatcherId(uint32_t &remoteWatcherId);
97     int GetGroupId(uint32_t &groupId);
98     // for remote watcher
99     int AddRemoteWatcher(RemoteWatcherPtr remoteWatcher);
100     RemoteWatcherPtr GetRemoteWatcher(uint32_t remoteWatcherId);
101     RemoteWatcherPtr GetRemoteWatcher(const wptr<IRemoteObject> &remote);
102     void DelRemoteWatcher(RemoteWatcherPtr remoteWatcher);
103     // for group
104     WatcherGroupPtr AddWatcherGroup(const std::string &keyPrefix);
105     WatcherGroupPtr GetWatcherGroup(const std::string &keyPrefix);
106     WatcherGroupPtr GetWatcherGroup(uint32_t groupId);
107     void DelWatcherGroup(WatcherGroupPtr group);
108     // for param watcher
109     int AddParamWatcher(WatcherGroupPtr group, RemoteWatcherPtr remoteWatcher);
110     int DelParamWatcher(WatcherGroupPtr group, RemoteWatcherPtr remoteWatcher);
111     // for process message form init
112     void ProcessWatcherMessage(const ParamMessage *msg);
113     // for client died
114     void OnRemoteDied(const wptr<IRemoteObject> &remote);
115     void OnRemoteDied(RemoteWatcherPtr remoteWatcher);
116     // clear
117     void Clear(void);
118     // dump
119     void DumpAllGroup(int fd, ParamWatcherProcessor dumpHandle);
120 private:
121     std::atomic<uint32_t> remoteWatcherId_ { 0 };
122     std::atomic<uint32_t> groupId_ { 0 };
123     std::mutex mutex_;
124     std::mutex watcherMutex_;
125     int serverFd_ { -1 };
126     std::thread *pRecvThread_ { nullptr };
127     std::atomic<bool> stop_ { false };
128     std::map<std::string, WatcherGroupPtr> groupMap_ {};
129     sptr<IRemoteObject::DeathRecipient> deathRecipient_ {};
130     ParamWatcherListPtr watcherGroups_ {};
131     ParamWatcherListPtr remoteWatchers_ {};
132 };
133 
134 class WatcherNode {
135 public:
WatcherNode(uint32_t nodeId)136     explicit WatcherNode(uint32_t nodeId) : nodeId_(nodeId)
137     {
138         OH_ListInit(&node_);
139     }
140     virtual ~WatcherNode(void) = default;
GetNodeId(void)141     uint32_t GetNodeId(void) const
142     {
143         return nodeId_;
144     }
145 
146     void AddToList(ListHeadPtr list);
147     void RemoveFromList(ListHeadPtr list);
148     WatcherNodePtr GetNext(ListHeadPtr list);
149     static WatcherNodePtr GetFromList(ListHeadPtr list, uint32_t nodeId);
150     static WatcherNodePtr GetNextFromList(ListHeadPtr list, uint32_t nodeId);
ConvertNodeToBase(ListNodePtr node)151     static WatcherNodePtr ConvertNodeToBase(ListNodePtr node)
152     {
153         return reinterpret_cast<WatcherNodePtr>((char *)node - (char*)(&(((WatcherNodePtr)0)->node_)));
154     }
GetListNode()155     ListNodePtr GetListNode()
156     {
157         return &node_;
158     }
159 private:
160     static int CompareNode(ListNodePtr node, ListNodePtr newNode);
161     static int CompareData(ListNodePtr node, void *data);
162     static int Greater(ListNodePtr node, void *data);
163     ListNode node_;
164     uint32_t nodeId_;
165 };
166 
167 template<typename T>
ConvertTo(WatcherNodePtr node)168 inline T *ConvertTo(WatcherNodePtr node)
169 {
170 #ifdef PARAM_WATCHER_RTTI_ENABLE
171     // when -frtti is enabled, we use dynamic cast directly
172     // to achieve the correct base class side-to-side conversion.
173     T *obj = dynamic_cast<T *>(node);
174 #else
175     // adjust pointer position when multiple inheritance.
176     void *tmp = reinterpret_cast<void *>(node);
177     // when -frtti is not enable, we use static cast.
178     // static cast is not safe enough, but we have checked before we get here.
179     T *obj = static_cast<T *>(tmp);
180 #endif
181     return obj;
182 }
183 
184 class ParamWatcher : public WatcherNode {
185 public:
ParamWatcher(uint32_t watcherId)186     explicit ParamWatcher(uint32_t watcherId) : WatcherNode(watcherId) {}
~ParamWatcher(void)187     ~ParamWatcher(void) override  {}
188 };
189 
190 class ParamWatcherList {
191 public:
ParamWatcherList(void)192     ParamWatcherList(void)
193     {
194         OH_ListInit(&nodeList_);
195     }
196     ~ParamWatcherList(void) = default;
197 
Empty(void)198     bool Empty(void) const
199     {
200         return nodeList_.next == &nodeList_;
201     }
GetNodeCount(void)202     uint32_t GetNodeCount(void) const
203     {
204         return nodeCount_;
205     }
206     int AddNode(WatcherNodePtr node);
207     int RemoveNode(WatcherNodePtr node);
208     WatcherNodePtr GetNode(uint32_t nodeId);
209     WatcherNodePtr GetNextNodeSafe(WatcherNodePtr node);
210     WatcherNodePtr GetNextNode(WatcherNodePtr node);
211     void TraversalNode(ParamWatcherProcessor handle);
212     void TraversalNodeSafe(ParamWatcherProcessor processor);
213 public:
214     ListHead nodeList_ {};
215     uint32_t nodeCount_ = 0;
216 };
217 
218 class RemoteWatcher : public WatcherNode, public ParamWatcherList {
219 public:
RemoteWatcher(uint32_t watcherId,const sptr<IWatcher> & watcher)220     RemoteWatcher(uint32_t watcherId, const sptr<IWatcher> &watcher)
221         : WatcherNode(watcherId), ParamWatcherList(), watcher_(watcher) {}
~RemoteWatcher(void)222     ~RemoteWatcher(void) override
223     {
224         watcher_ = nullptr;
225         TraversalNodeSafe([](ParamWatcherListPtr list, WatcherNodePtr node, uint32_t index) {
226             list->RemoveNode(node);
227             ParamWatcher *watcher = ConvertTo<ParamWatcher>(node);
228             delete watcher;
229         });
230     }
231 
GetRemoteWatcherId(void)232     uint32_t GetRemoteWatcherId(void) const
233     {
234         return GetNodeId();
235     }
GetAgentId(void)236     uint32_t GetAgentId(void) const
237     {
238         return id_;
239     }
SetAgentId(uint32_t id)240     void SetAgentId(uint32_t id)
241     {
242         id_ = id;
243     }
ProcessParameterChange(const std::string & prefix,const std::string & name,const std::string & value)244     void ProcessParameterChange(const std::string &prefix, const std::string &name, const std::string &value)
245     {
246         watcher_->OnParameterChange(prefix, name, value);
247     }
GetWatcher(void)248     sptr<IWatcher> GetWatcher(void)
249     {
250         return watcher_;
251     }
252 private:
253     uint32_t id_ = { 0 };
254     sptr<IWatcher> watcher_ {};
255 };
256 
257 class WatcherGroup : public WatcherNode, public ParamWatcherList {
258 public:
WatcherGroup(uint32_t groupId,const std::string & key)259     WatcherGroup(uint32_t groupId, const std::string &key)
260         : WatcherNode(groupId), ParamWatcherList(), keyPrefix_(key) {}
~WatcherGroup()261     ~WatcherGroup() override
262     {
263         TraversalNodeSafe([](ParamWatcherListPtr list, WatcherNodePtr node, uint32_t index) {
264             list->RemoveNode(node);
265             ParamWatcher *watcher = ConvertTo<ParamWatcher>(node);
266             delete watcher;
267         });
268     }
269     void ProcessParameterChange(WatcherManager *mananger, const std::string &name, const std::string &value);
GetKeyPrefix()270     const std::string GetKeyPrefix() const
271     {
272         return keyPrefix_;
273     }
GetGroupId()274     uint32_t GetGroupId() const
275     {
276         return GetNodeId();
277     }
278 private:
279     std::string keyPrefix_ { };
280 };
281 } // namespace init_param
282 } // namespace OHOS
283 #endif // WATCHER_MANAGER_H_