Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Startup Init Lite
提交
09b41e46
S
Startup Init Lite
项目概览
OpenHarmony
/
Startup Init Lite
接近 2 年 前同步成功
通知
3
Star
37
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Startup Init Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
09b41e46
编写于
8月 30, 2022
作者:
C
cheng_jinsong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add multi callback for watcher
Signed-off-by:
N
cheng_jinsong
<
chengjinsong2@huawei.com
>
上级
7cf030e7
变更
20
隐藏空白更改
内联
并排
Showing
20 changed file
with
295 addition
and
90 deletion
+295
-90
interfaces/innerkits/include/syspara/parameter.h
interfaces/innerkits/include/syspara/parameter.h
+19
-3
services/param/trigger/trigger_manager.c
services/param/trigger/trigger_manager.c
+2
-2
services/param/watcher/BUILD.gn
services/param/watcher/BUILD.gn
+0
-1
services/param/watcher/agent/watcher.cpp
services/param/watcher/agent/watcher.cpp
+1
-1
services/param/watcher/agent/watcher.h
services/param/watcher/agent/watcher.h
+2
-2
services/param/watcher/agent/watcher_manager_kits.cpp
services/param/watcher/agent/watcher_manager_kits.cpp
+141
-34
services/param/watcher/agent/watcher_manager_kits.h
services/param/watcher/agent/watcher_manager_kits.h
+38
-9
services/param/watcher/agent/watcher_manager_proxy.cpp
services/param/watcher/agent/watcher_manager_proxy.cpp
+15
-0
services/param/watcher/agent/watcher_manager_proxy.h
services/param/watcher/agent/watcher_manager_proxy.h
+1
-0
services/param/watcher/agent/watcher_stub.cpp
services/param/watcher/agent/watcher_stub.cpp
+1
-1
services/param/watcher/include/iwatcher.h
services/param/watcher/include/iwatcher.h
+1
-1
services/param/watcher/include/iwatcher_manager.h
services/param/watcher/include/iwatcher_manager.h
+3
-1
services/param/watcher/proxy/watcher_manager.cpp
services/param/watcher/proxy/watcher_manager.cpp
+20
-9
services/param/watcher/proxy/watcher_manager.h
services/param/watcher/proxy/watcher_manager.h
+4
-2
services/param/watcher/proxy/watcher_manager_stub.cpp
services/param/watcher/proxy/watcher_manager_stub.cpp
+7
-0
services/param/watcher/proxy/watcher_proxy.cpp
services/param/watcher/proxy/watcher_proxy.cpp
+2
-2
services/param/watcher/proxy/watcher_proxy.h
services/param/watcher/proxy/watcher_proxy.h
+1
-1
test/moduletest/param_test_cmds.c
test/moduletest/param_test_cmds.c
+7
-15
test/unittest/param/watcher_agent_unittest.cpp
test/unittest/param/watcher_agent_unittest.cpp
+27
-3
test/unittest/param/watcher_proxy_unittest.cpp
test/unittest/param/watcher_proxy_unittest.cpp
+3
-3
未找到文件。
interfaces/innerkits/include/syspara/parameter.h
浏览文件 @
09b41e46
...
...
@@ -94,8 +94,8 @@ int WaitParameter(const char *key, const char *value, int timeout);
*
* You can use this function to watch system parameter values.\n
*
* @param key
p
refix Indicates the key prefix for the parameter to be watched.
* If key
p
refix is not a full name, "A.B." for example, it means to watch for all parameter started with "A.B.".
* @param key
P
refix Indicates the key prefix for the parameter to be watched.
* If key
P
refix is not a full name, "A.B." for example, it means to watch for all parameter started with "A.B.".
* @param callback Indicates value change callback.
* If callback is NULL, it means to cancel the watch.
* @return Returns <b>0</b> if the operation is successful;
...
...
@@ -104,7 +104,23 @@ int WaitParameter(const char *key, const char *value, int timeout);
* @version 1.1
*/
typedef
void
(
*
ParameterChgPtr
)(
const
char
*
key
,
const
char
*
value
,
void
*
context
);
int
WatchParameter
(
const
char
*
keyprefix
,
ParameterChgPtr
callback
,
void
*
context
);
int
WatchParameter
(
const
char
*
keyPrefix
,
ParameterChgPtr
callback
,
void
*
context
);
/**
* @brief Remove parameter watcher.
*
* You can use this function to remove system parameter watcher.\n
*
* @param keyPrefix Indicates the key prefix for the parameter to be watched.
* If keyPrefix is not a full name, "A.B." for example, it means to watch for all parameter started with "A.B.".
* @param callback Indicates value change callback.
* If callback is NULL, it means to cancel the watch.
* @return Returns <b>0</b> if the operation is successful;
* returns <b>-1</b> in other scenarios.
* @since 1.1
* @version 1.1
*/
int
RemoveParameterWatcher
(
const
char
*
keyPrefix
,
ParameterChgPtr
callback
,
void
*
context
);
const
char
*
GetSecurityPatchTag
(
void
);
const
char
*
GetOSFullName
(
void
);
...
...
services/param/trigger/trigger_manager.c
浏览文件 @
09b41e46
...
...
@@ -550,10 +550,10 @@ static int32_t CompareData_(const struct tagTriggerNode_ *trigger, const void *d
PARAM_CHECK
(
trigger
!=
NULL
&&
data
!=
NULL
,
return
-
1
,
"Invalid trigger"
);
if
(
trigger
->
type
==
TRIGGER_PARAM_WAIT
)
{
WaitNode
*
node
=
(
WaitNode
*
)
trigger
;
return
node
->
waitId
==
*
(
uint32_t
*
)
data
;
return
node
->
waitId
-
*
(
uint32_t
*
)
data
;
}
else
if
(
trigger
->
type
==
TRIGGER_PARAM_WATCH
)
{
WatchNode
*
node
=
(
WatchNode
*
)
trigger
;
return
node
->
watchId
==
*
(
uint32_t
*
)
data
;
return
node
->
watchId
-
*
(
uint32_t
*
)
data
;
}
return
-
1
;
}
...
...
services/param/watcher/BUILD.gn
浏览文件 @
09b41e46
...
...
@@ -20,7 +20,6 @@ ohos_prebuilt_etc("param_watcher.rc") {
ohos_shared_library("param_watcher") {
sources = [
"//base/startup/init/services/utils/list.c",
"proxy/watcher_manager.cpp",
"proxy/watcher_manager_stub.cpp",
"proxy/watcher_proxy.cpp",
...
...
services/param/watcher/agent/watcher.cpp
浏览文件 @
09b41e46
...
...
@@ -17,7 +17,7 @@
namespace
OHOS
{
namespace
init_param
{
void
Watcher
::
OnParame
r
terChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
void
Watcher
::
OnParameterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
{
UNUSED
(
name
);
UNUSED
(
value
);
...
...
services/param/watcher/agent/watcher.h
浏览文件 @
09b41e46
...
...
@@ -24,9 +24,9 @@ namespace init_param {
class
Watcher
:
public
WatcherStub
{
public:
Watcher
()
=
default
;
~
Watcher
()
=
default
;
virtual
~
Watcher
()
=
default
;
void
OnParame
r
terChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
override
;
void
OnParameterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
override
;
};
}
// namespace init_param
}
// namespace OHOS
...
...
services/param/watcher/agent/watcher_manager_kits.cpp
浏览文件 @
09b41e46
...
...
@@ -21,6 +21,7 @@
#include "iwatcher_manager.h"
#include "system_ability_definition.h"
#include "watcher_utils.h"
#include "parameter.h"
namespace
OHOS
{
namespace
init_param
{
...
...
@@ -92,14 +93,6 @@ WatcherManagerKits::ParamWatcherKitPtr WatcherManagerKits::GetParamWatcher(const
return
watchers_
[
keyPrefix
];
}
void
WatcherManagerKits
::
SetParamWatcher
(
const
std
::
string
&
keyPrefix
,
ParamWatcherKitPtr
watcher
)
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex_
);
if
(
watchers_
.
find
(
keyPrefix
)
==
watchers_
.
end
())
{
watchers_
[
keyPrefix
]
=
watcher
;
}
}
void
WatcherManagerKits
::
ReAddWatcher
()
{
WATCHER_LOGV
(
"ReAddWatcher "
);
...
...
@@ -127,45 +120,142 @@ void WatcherManagerKits::ReAddWatcher()
int32_t
WatcherManagerKits
::
AddWatcher
(
const
std
::
string
&
keyPrefix
,
ParameterChangePtr
callback
,
void
*
context
)
{
WATCHER_LOGI
(
"AddWatcher keyPrefix %s"
,
keyPrefix
.
c_str
());
ParamWatcherKitPtr
watcher
=
GetParamWatcher
(
keyPrefix
);
WATCHER_CHECK
(
watcher
==
nullptr
,
return
-
1
,
"Has been watched by keyPrefix %s"
,
keyPrefix
.
c_str
());
watcher
=
new
ParamWatcher
(
keyPrefix
,
callback
,
context
);
WATCHER_CHECK
(
watcher
!=
nullptr
,
return
-
1
,
"Failed to create watcher for %s"
,
keyPrefix
.
c_str
());
WATCHER_LOGV
(
"AddWatcher keyPrefix %s"
,
keyPrefix
.
c_str
());
auto
watcherManager
=
GetService
();
WATCHER_CHECK
(
watcherManager
!=
nullptr
,
return
-
1
,
"Failed to get watcher manager"
);
uint32_t
watcherId
=
watcherManager
->
AddWatcher
(
keyPrefix
,
watcher
);
WATCHER_CHECK
(
watcherId
!=
0
,
return
-
1
,
"Failed to add watcher for %s"
,
keyPrefix
.
c_str
());
watcher
->
SetWatcherId
(
watcherId
);
SetParamWatcher
(
keyPrefix
,
watcher
);
return
watcher
->
GetWatcherId
();
bool
newWatcher
=
false
;
ParamWatcherKitPtr
watcher
=
nullptr
;
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex_
);
if
(
watchers_
.
find
(
keyPrefix
)
==
watchers_
.
end
())
{
watcher
=
new
ParamWatcher
(
keyPrefix
);
WATCHER_CHECK
(
watcher
!=
nullptr
,
return
-
1
,
"Failed to create watcher for %s"
,
keyPrefix
.
c_str
());
watchers_
[
keyPrefix
]
=
watcher
;
newWatcher
=
true
;
int
ret
=
watcher
->
AddParameterListener
(
callback
,
context
);
WATCHER_CHECK
(
ret
==
0
,
return
-
1
,
"Failed to add callback for %s "
,
keyPrefix
.
c_str
());
uint32_t
watcherId
=
watcherManager
->
AddWatcher
(
keyPrefix
,
watcher
);
WATCHER_CHECK
(
watcherId
!=
0
,
return
-
1
,
"Failed to add watcher for %s"
,
keyPrefix
.
c_str
());
watcher
->
SetWatcherId
(
watcherId
);
}
else
{
watcher
=
watchers_
[
keyPrefix
];
}
}
// save callback
if
(
!
newWatcher
)
{
// refresh
int
ret
=
watcher
->
AddParameterListener
(
callback
,
context
);
WATCHER_CHECK
(
ret
==
0
,
return
-
1
,
"Failed to add callback for %s "
,
keyPrefix
.
c_str
());
ret
=
watcherManager
->
RefreshWatcher
(
keyPrefix
,
watcher
->
GetWatcherId
());
WATCHER_CHECK
(
ret
==
0
,
return
-
1
,
"Failed to refresh watcher for %s"
,
keyPrefix
.
c_str
());
}
WATCHER_LOGI
(
"AddWatcher keyPrefix %s watcherId %u success"
,
keyPrefix
.
c_str
(),
watcher
->
GetWatcherId
());
return
0
;
}
int32_t
WatcherManagerKits
::
DelWatcher
(
const
std
::
string
&
keyPrefix
)
int32_t
WatcherManagerKits
::
DelWatcher
(
const
std
::
string
&
keyPrefix
,
ParameterChangePtr
callback
,
void
*
context
)
{
ParamWatcherKitPtr
watcher
=
GetParamWatcher
(
keyPrefix
);
WATCHER_CHECK
(
watcher
!=
nullptr
,
return
0
,
"Can not find watcher for keyPrefix %s"
,
keyPrefix
.
c_str
());
auto
watcherManager
=
GetService
();
WATCHER_CHECK
(
watcherManager
!=
nullptr
,
return
-
1
,
"Failed to get watcher manager"
);
WATCHER_LOGV
(
"DelWatcher keyPrefix_ %s "
,
keyPrefix
.
c_str
());
int
ret
=
watcherManager
->
DelWatcher
(
keyPrefix
,
watcher
->
GetWatcherId
());
WATCHER_CHECK
(
ret
==
0
,
return
-
1
,
"Failed to delete watcher for %s"
,
keyPrefix
.
c_str
());
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex_
);
auto
iter
=
watchers_
.
find
(
keyPrefix
);
if
(
iter
!=
watchers_
.
end
())
{
watchers_
.
erase
(
iter
);
int
count
=
watcher
->
DelParameterListener
(
callback
,
context
);
WATCHER_LOGI
(
"DelWatcher keyPrefix_ %s count %d"
,
keyPrefix
.
c_str
(),
count
);
if
(
count
==
0
)
{
int
ret
=
watcherManager
->
DelWatcher
(
keyPrefix
,
watcher
->
GetWatcherId
());
WATCHER_CHECK
(
ret
==
0
,
return
-
1
,
"Failed to delete watcher for %s"
,
keyPrefix
.
c_str
());
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex_
);
auto
iter
=
watchers_
.
find
(
keyPrefix
);
if
(
iter
!=
watchers_
.
end
())
{
watchers_
.
erase
(
iter
);
}
}
}
return
0
;
}
WatcherManagerKits
::
ParameterChangeListener
*
WatcherManagerKits
::
ParamWatcher
::
GetParameterListener
(
uint32_t
*
idx
)
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex_
);
uint32_t
index
=
*
idx
;
while
(
index
<
static_cast
<
uint32_t
>
(
parameterChangeListeners
.
size
()))
{
auto
it
=
parameterChangeListeners
.
find
(
index
);
if
(
it
!=
parameterChangeListeners
.
end
())
{
*
idx
=
index
;
return
parameterChangeListeners
[
index
].
get
();
}
index
++
;
}
return
nullptr
;
}
void
WatcherManagerKits
::
ParamWatcher
::
RemoveParameterListener
(
uint32_t
idx
)
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex_
);
if
(
idx
>=
static_cast
<
uint32_t
>
(
parameterChangeListeners
.
size
()))
{
return
;
}
parameterChangeListeners
.
erase
(
idx
);
}
void
WatcherManagerKits
::
ParamWatcher
::
OnParameterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
{
Watcher
::
OnParameterChange
(
name
,
value
);
WATCHER_LOGI
(
"OnParameterChange name %s value %s"
,
name
.
c_str
(),
value
.
c_str
());
uint32_t
index
=
0
;
ParameterChangeListener
*
listener
=
GetParameterListener
(
&
index
);
while
(
listener
!=
NULL
)
{
if
(
!
listener
->
CheckValueChange
(
value
))
{
listener
->
OnParameterChange
(
name
,
value
);
}
index
++
;
listener
=
GetParameterListener
(
&
index
);
}
}
int
WatcherManagerKits
::
ParamWatcher
::
AddParameterListener
(
ParameterChangePtr
callback
,
void
*
context
)
{
WATCHER_CHECK
(
callback
!=
nullptr
,
return
-
1
,
"Invalid callback "
);
WATCHER_LOGV
(
"AddParameterListener listenerId_ %d callback %p context %p"
,
listenerId_
,
callback
,
context
);
std
::
lock_guard
<
std
::
mutex
>
lock
(
mutex_
);
for
(
auto
it
=
parameterChangeListeners
.
begin
();
it
!=
parameterChangeListeners
.
end
();
it
++
)
{
if
(
it
->
second
==
nullptr
)
{
continue
;
}
if
(
it
->
second
->
IsEqual
(
callback
,
context
))
{
WATCHER_LOGI
(
"AddParameterListener callback %p context %p exist"
,
callback
,
context
);
return
-
1
;
}
}
std
::
shared_ptr
<
ParameterChangeListener
>
changeNode
=
std
::
make_shared
<
ParameterChangeListener
>
(
callback
,
context
);
WATCHER_CHECK
(
changeNode
!=
nullptr
,
return
-
1
,
"Failed to create listener"
);
parameterChangeListeners
[
listenerId_
]
=
changeNode
;
listenerId_
++
;
return
0
;
}
void
WatcherManagerKits
::
ParamWatcher
::
OnParamerterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
int
WatcherManagerKits
::
ParamWatcher
::
DelParameterListener
(
ParameterChangePtr
callback
,
void
*
context
)
{
uint32_t
index
=
0
;
ParameterChangeListener
*
listener
=
GetParameterListener
(
&
index
);
while
(
listener
!=
NULL
)
{
if
((
callback
==
nullptr
&&
context
==
nullptr
))
{
RemoveParameterListener
(
index
);
}
else
if
(
listener
->
IsEqual
(
callback
,
context
))
{
WATCHER_LOGI
(
"DelParameterListener callback %p context %p"
,
callback
,
context
);
RemoveParameterListener
(
index
);
break
;
}
index
++
;
listener
=
GetParameterListener
(
&
index
);
}
return
static_cast
<
int
>
(
parameterChangeListeners
.
size
());
}
void
WatcherManagerKits
::
ParameterChangeListener
::
OnParameterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
{
Watcher
::
OnParamerterChange
(
name
,
value
);
WATCHER_LOGV
(
"OnParamerterChange name %s value %s"
,
name
.
c_str
(),
value
.
c_str
());
if
(
callback_
!=
nullptr
)
{
callback_
(
name
.
c_str
(),
value
.
c_str
(),
context_
);
}
...
...
@@ -188,9 +278,26 @@ int SystemWatchParameter(const char *keyPrefix, ParameterChangePtr callback, voi
}
OHOS
::
init_param
::
WatcherManagerKits
&
instance
=
OHOS
::
init_param
::
WatcherManagerKits
::
GetInstance
();
if
(
callback
!=
nullptr
)
{
ret
=
(
instance
.
AddWatcher
(
keyPrefix
,
callback
,
context
)
>
0
)
?
0
:
-
1
;
ret
=
instance
.
AddWatcher
(
keyPrefix
,
callback
,
context
)
;
}
else
{
ret
=
instance
.
DelWatcher
(
keyPrefix
);
ret
=
instance
.
DelWatcher
(
keyPrefix
,
nullptr
,
nullptr
);
}
return
ret
;
}
int
RemoveParameterWatcher
(
const
char
*
keyPrefix
,
ParameterChgPtr
callback
,
void
*
context
)
{
WATCHER_CHECK
(
keyPrefix
!=
nullptr
,
return
PARAM_CODE_INVALID_PARAM
,
"Invalid prefix"
);
int
ret
=
0
;
std
::
string
key
(
keyPrefix
);
if
(
key
.
rfind
(
"*"
)
==
key
.
length
()
-
1
)
{
ret
=
WatchParamCheck
(
key
.
substr
(
0
,
key
.
length
()
-
1
).
c_str
());
}
else
{
ret
=
WatchParamCheck
(
keyPrefix
);
}
if
(
ret
!=
0
)
{
return
ret
;
}
OHOS
::
init_param
::
WatcherManagerKits
&
instance
=
OHOS
::
init_param
::
WatcherManagerKits
::
GetInstance
();
return
instance
.
DelWatcher
(
keyPrefix
,
(
ParameterChangePtr
)
callback
,
context
);
}
\ No newline at end of file
services/param/watcher/agent/watcher_manager_kits.h
浏览文件 @
09b41e46
...
...
@@ -35,18 +35,43 @@ public:
static
WatcherManagerKits
&
GetInstance
();
int32_t
AddWatcher
(
const
std
::
string
&
keyPrefix
,
ParameterChangePtr
callback
,
void
*
context
);
int32_t
DelWatcher
(
const
std
::
string
&
keyPrefix
);
int32_t
DelWatcher
(
const
std
::
string
&
keyPrefix
,
ParameterChangePtr
callback
,
void
*
context
);
void
ReAddWatcher
();
#ifndef STARTUP_INIT_TEST
private:
#endif
class
ParameterChangeListener
{
public:
ParameterChangeListener
(
ParameterChangePtr
callback
,
void
*
context
)
:
callback_
(
callback
),
context_
(
context
)
{}
~
ParameterChangeListener
()
=
default
;
bool
IsEqual
(
ParameterChangePtr
callback
,
void
*
context
)
{
return
(
callback
==
callback_
&&
context
==
context_
);
}
void
OnParameterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
);
bool
CheckValueChange
(
const
std
::
string
&
value
)
{
bool
ret
=
(
value_
==
value
);
value_
=
value
;
return
ret
;
}
private:
std
::
string
value_
{};
ParameterChangePtr
callback_
{
nullptr
};
void
*
context_
{
nullptr
};
};
class
ParamWatcher
final
:
public
Watcher
{
public:
ParamWatcher
(
const
std
::
string
&
key
,
ParameterChangePtr
callback
,
void
*
context
)
:
keyPrefix_
(
key
),
callback_
(
callback
),
context_
(
context
)
{}
~
ParamWatcher
()
=
default
;
explicit
ParamWatcher
(
const
std
::
string
&
key
)
:
keyPrefix_
(
key
)
{}
virtual
~
ParamWatcher
()
{
parameterChangeListeners
.
clear
();
};
void
OnParame
r
terChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
override
;
void
OnParameterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
override
;
void
SetWatcherId
(
uint32_t
watcherId
)
{
...
...
@@ -56,12 +81,17 @@ private:
{
return
watcherId_
;
}
int
AddParameterListener
(
ParameterChangePtr
callback
,
void
*
context
);
int
DelParameterListener
(
ParameterChangePtr
callback
,
void
*
context
);
private:
ParameterChangeListener
*
GetParameterListener
(
uint32_t
*
idx
);
void
RemoveParameterListener
(
uint32_t
idx
);
uint32_t
watcherId_
{
0
};
std
::
string
keyPrefix_
{};
ParameterChangePtr
callback_
{
nullptr
};
void
*
context_
{
nullptr
};
std
::
mutex
mutex_
;
uint32_t
listenerId_
{
0
};
std
::
map
<
uint32_t
,
std
::
shared_ptr
<
ParameterChangeListener
>>
parameterChangeListeners
;
};
using
ParamWatcherKitPtr
=
sptr
<
WatcherManagerKits
::
ParamWatcher
>
;
ParamWatcherKitPtr
GetParamWatcher
(
const
std
::
string
&
keyPrefix
);
...
...
@@ -80,7 +110,6 @@ private:
}
private:
void
SetParamWatcher
(
const
std
::
string
&
keyPrefix
,
ParamWatcherKitPtr
watcher
);
void
ResetService
(
const
wptr
<
IRemoteObject
>
&
remote
);
sptr
<
IWatcherManager
>
GetService
();
std
::
mutex
lock_
;
...
...
services/param/watcher/agent/watcher_manager_proxy.cpp
浏览文件 @
09b41e46
...
...
@@ -52,5 +52,20 @@ int32_t WatcherManagerProxy::DelWatcher(const std::string &keyPrefix, uint32_t w
WATCHER_CHECK
(
res
==
ERR_OK
,
return
ERR_FLATTEN_OBJECT
,
"Transact error"
);
return
reply
.
ReadInt32
();
}
int32_t
WatcherManagerProxy
::
RefreshWatcher
(
const
std
::
string
&
keyPrefix
,
uint32_t
watcherId
)
{
auto
remote
=
Remote
();
WATCHER_CHECK
(
remote
!=
nullptr
,
return
ERR_FLATTEN_OBJECT
,
"Can not get remote"
);
MessageParcel
data
;
data
.
WriteInterfaceToken
(
WatcherManagerProxy
::
GetDescriptor
());
data
.
WriteString
(
keyPrefix
);
data
.
WriteUint32
(
watcherId
);
MessageParcel
reply
;
MessageOption
option
{
MessageOption
::
TF_SYNC
};
int32_t
res
=
remote
->
SendRequest
(
REFRESH_WATCHER
,
data
,
reply
,
option
);
WATCHER_CHECK
(
res
==
ERR_OK
,
return
ERR_FLATTEN_OBJECT
,
"Transact error"
);
return
0
;
}
}
}
// namespace OHOS
services/param/watcher/agent/watcher_manager_proxy.h
浏览文件 @
09b41e46
...
...
@@ -27,6 +27,7 @@ public:
uint32_t
AddWatcher
(
const
std
::
string
&
keyPrefix
,
const
sptr
<
IWatcher
>
&
watcher
)
override
;
int32_t
DelWatcher
(
const
std
::
string
&
keyPrefix
,
uint32_t
watcherId
)
override
;
int32_t
RefreshWatcher
(
const
std
::
string
&
keyPrefix
,
uint32_t
watcherId
)
override
;
private:
static
inline
BrokerDelegator
<
WatcherManagerProxy
>
delegator_
;
};
...
...
services/param/watcher/agent/watcher_stub.cpp
浏览文件 @
09b41e46
...
...
@@ -30,7 +30,7 @@ int32_t WatcherStub::OnRemoteRequest(uint32_t code, MessageParcel &data, Message
case
PARAM_CHANGE
:
{
std
::
string
name
=
data
.
ReadString
();
std
::
string
value
=
data
.
ReadString
();
OnParame
r
terChange
(
name
,
value
);
OnParameterChange
(
name
,
value
);
break
;
}
default:
{
...
...
services/param/watcher/include/iwatcher.h
浏览文件 @
09b41e46
...
...
@@ -31,7 +31,7 @@ public:
DECLARE_INTERFACE_DESCRIPTOR
(
u"OHOS.Startup.IWatcher"
);
public:
virtual
void
OnParame
r
terChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
=
0
;
virtual
void
OnParameterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
=
0
;
};
}
// namespace init_param
}
// namespace OHOS
...
...
services/param/watcher/include/iwatcher_manager.h
浏览文件 @
09b41e46
...
...
@@ -27,13 +27,15 @@ class IWatcherManager : public OHOS::IRemoteBroker {
public:
enum
{
ADD_WATCHER
,
DEL_WATCHER
DEL_WATCHER
,
REFRESH_WATCHER
};
DECLARE_INTERFACE_DESCRIPTOR
(
u"OHOS.Startup.IWatcherManager"
);
public:
virtual
uint32_t
AddWatcher
(
const
std
::
string
&
keyPrefix
,
const
sptr
<
IWatcher
>
&
watcher
)
=
0
;
virtual
int32_t
DelWatcher
(
const
std
::
string
&
keyPrefix
,
uint32_t
watcherId
)
=
0
;
virtual
int32_t
RefreshWatcher
(
const
std
::
string
&
keyPrefix
,
uint32_t
watcherId
)
=
0
;
};
}
// namespace update_engine
}
// namespace OHOS
...
...
services/param/watcher/proxy/watcher_manager.cpp
浏览文件 @
09b41e46
...
...
@@ -64,17 +64,17 @@ uint32_t WatcherManager::AddWatcher(const std::string &keyPrefix, const sptr<IWa
group
=
std
::
make_shared
<
WatcherGroup
>
(
groupId
,
keyPrefix
);
WATCHER_CHECK
(
group
!=
nullptr
,
return
0
,
"Failed to create group for %s"
,
keyPrefix
.
c_str
());
}
else
{
newGroup
=
group
->
Empt
r
y
();
newGroup
=
group
->
Empty
();
}
ParamWatcherPtr
paramWather
=
std
::
make_shared
<
ParamWatcher
>
(
watcherId
,
watcher
,
group
);
WATCHER_CHECK
(
paramWather
!=
nullptr
,
return
0
,
"Failed to create watcher for %s"
,
keyPrefix
.
c_str
());
AddParamWatcher
(
keyPrefix
,
group
,
paramWather
);
ParamWatcherPtr
paramWat
c
her
=
std
::
make_shared
<
ParamWatcher
>
(
watcherId
,
watcher
,
group
);
WATCHER_CHECK
(
paramWat
c
her
!=
nullptr
,
return
0
,
"Failed to create watcher for %s"
,
keyPrefix
.
c_str
());
AddParamWatcher
(
keyPrefix
,
group
,
paramWat
c
her
);
if
(
newGroup
)
{
StartLoop
();
SendMessage
(
group
,
MSG_ADD_WATCHER
);
}
SendLocalChange
(
keyPrefix
,
paramWather
);
WATCHER_LOGI
(
"AddWatcher %s watcherId: %u
success"
,
keyPrefix
.
c_str
(),
watcherId
);
SendLocalChange
(
keyPrefix
,
paramWat
c
her
);
WATCHER_LOGI
(
"AddWatcher %s watcherId: %u
groupId %u success"
,
keyPrefix
.
c_str
(),
watcherId
,
group
->
GetGroupId
()
);
return
watcherId
;
}
...
...
@@ -83,7 +83,7 @@ int32_t WatcherManager::DelWatcher(const std::string &keyPrefix, uint32_t watche
auto
group
=
GetWatcherGroup
(
keyPrefix
);
WATCHER_CHECK
(
group
!=
nullptr
,
return
0
,
"Can not find group %s"
,
keyPrefix
.
c_str
());
auto
paramWatcher
=
GetWatcher
(
watcherId
);
WATCHER_CHECK
(
group
!=
nullptr
,
return
0
,
"Can not find watcher %s %d"
,
keyPrefix
.
c_str
(),
watcherId
);
WATCHER_CHECK
(
paramWatcher
!=
nullptr
,
return
0
,
"Can not find watcher %s %d"
,
keyPrefix
.
c_str
(),
watcherId
);
WATCHER_LOGV
(
"DelWatcher prefix %s watcherId %u"
,
keyPrefix
.
c_str
(),
watcherId
);
return
DelWatcher
(
group
,
paramWatcher
);
}
...
...
@@ -97,7 +97,7 @@ int32_t WatcherManager::DelWatcher(WatcherGroupPtr group, ParamWatcherPtr watche
}
WATCHER_LOGI
(
"DelWatcher watcherId %u prefix %s"
,
watcher
->
GetWatcherId
(),
group
->
GetKeyPrefix
().
c_str
());
DelParamWatcher
(
watcher
);
if
(
group
->
Empt
r
y
())
{
if
(
group
->
Empty
())
{
SendMessage
(
group
,
MSG_DEL_WATCHER
);
DelWatcherGroup
(
group
);
}
...
...
@@ -112,7 +112,7 @@ int WatcherManager::SendMessage(WatcherGroupPtr group, int type)
request
->
id
.
watcherId
=
group
->
GetGroupId
();
request
->
msgSize
=
sizeof
(
ParamMessage
);
WATCHER_LOGV
(
"SendMessage %s
"
,
group
->
GetKeyPrefix
().
c_str
()
);
WATCHER_LOGV
(
"SendMessage %s
groupId %d type %d"
,
group
->
GetKeyPrefix
().
c_str
(),
group
->
GetGroupId
(),
type
);
int
ret
=
PARAM_CODE_ERROR
;
int
fd
=
GetServerFd
(
false
);
if
(
fd
!=
INVALID_SOCKET
)
{
...
...
@@ -423,5 +423,16 @@ int WatcherManager::GetGroupId(uint32_t &groupId)
groupId
=
groupId_
;
return
0
;
}
int32_t
WatcherManager
::
RefreshWatcher
(
const
std
::
string
&
keyPrefix
,
uint32_t
watcherId
)
{
WATCHER_LOGV
(
"RefreshWatcher %s watcherId: %u"
,
keyPrefix
.
c_str
(),
watcherId
);
auto
group
=
GetWatcherGroup
(
keyPrefix
);
WATCHER_CHECK
(
group
!=
nullptr
,
return
0
,
"Can not find group %s"
,
keyPrefix
.
c_str
());
auto
paramWatcher
=
GetWatcher
(
watcherId
);
WATCHER_CHECK
(
paramWatcher
!=
nullptr
,
return
0
,
"Can not find watcher %s %d"
,
keyPrefix
.
c_str
(),
watcherId
);
SendLocalChange
(
keyPrefix
,
paramWatcher
);
return
0
;
}
}
// namespace init_param
}
// namespace OHOS
services/param/watcher/proxy/watcher_manager.h
浏览文件 @
09b41e46
...
...
@@ -90,7 +90,7 @@ public:
void
ProcessParameterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
{
#ifndef STARTUP_INIT_TEST
watcher_
->
OnParame
r
terChange
(
name
,
value
);
watcher_
->
OnParameterChange
(
name
,
value
);
#endif
}
private:
...
...
@@ -114,7 +114,7 @@ public:
{
return
keyPrefix_
;
}
bool
Empt
r
y
()
const
bool
Empty
()
const
{
return
ListEmpty
(
watchers_
);
}
...
...
@@ -134,6 +134,7 @@ public:
uint32_t
AddWatcher
(
const
std
::
string
&
keyPrefix
,
const
sptr
<
IWatcher
>
&
watcher
)
override
;
int32_t
DelWatcher
(
const
std
::
string
&
keyPrefix
,
uint32_t
watcherId
)
override
;
int32_t
RefreshWatcher
(
const
std
::
string
&
keyPrefix
,
uint32_t
watcherId
)
override
;
int32_t
DelWatcher
(
WatcherGroupPtr
group
,
ParamWatcherPtr
watcher
);
ParamWatcherPtr
GetWatcher
(
uint32_t
watcherId
);
ParamWatcherPtr
GetWatcher
(
const
wptr
<
IRemoteObject
>
&
remote
);
...
...
@@ -160,6 +161,7 @@ private:
int
GetServerFd
(
bool
retry
);
int
GetWatcherId
(
uint32_t
&
watcherId
);
int
GetGroupId
(
uint32_t
&
groupId
);
private:
std
::
atomic
<
uint32_t
>
watcherId_
{
0
};
std
::
atomic
<
uint32_t
>
groupId_
{
0
};
...
...
services/param/watcher/proxy/watcher_manager_stub.cpp
浏览文件 @
09b41e46
...
...
@@ -43,6 +43,13 @@ int32_t WatcherManagerStub::OnRemoteRequest(uint32_t code,
reply
.
WriteInt32
(
ret
);
break
;
}
case
REFRESH_WATCHER
:
{
std
::
string
key
=
data
.
ReadString
();
uint32_t
watcherId
=
data
.
ReadUint32
();
int
ret
=
RefreshWatcher
(
key
,
watcherId
);
reply
.
WriteInt32
(
ret
);
break
;
}
default:
{
return
IPCObjectStub
::
OnRemoteRequest
(
code
,
data
,
reply
,
option
);
}
...
...
services/param/watcher/proxy/watcher_proxy.cpp
浏览文件 @
09b41e46
...
...
@@ -23,9 +23,9 @@
namespace
OHOS
{
namespace
init_param
{
void
WatcherProxy
::
OnParame
r
terChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
void
WatcherProxy
::
OnParameterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
{
WATCHER_LOGV
(
"WatcherProxy::OnParame
r
terChange %s %s"
,
name
.
c_str
(),
value
.
c_str
());
WATCHER_LOGV
(
"WatcherProxy::OnParameterChange %s %s"
,
name
.
c_str
(),
value
.
c_str
());
MessageParcel
data
;
MessageParcel
reply
;
MessageOption
option
{
MessageOption
::
TF_ASYNC
};
...
...
services/param/watcher/proxy/watcher_proxy.h
浏览文件 @
09b41e46
...
...
@@ -27,7 +27,7 @@ public:
virtual
~
WatcherProxy
()
=
default
;
void
OnParame
r
terChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
override
;
void
OnParameterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
override
;
private:
static
inline
BrokerDelegator
<
WatcherProxy
>
delegator_
;
...
...
test/moduletest/param_test_cmds.c
浏览文件 @
09b41e46
...
...
@@ -84,25 +84,17 @@ static void HandleParamChange(const char *key, const char *value, void *context)
printf
(
"Receive parameter commit %lld change %s %s
\n
"
,
commit
,
key
,
value
);
}
static
void
*
CmdWatcher
(
void
*
args
)
{
char
*
name
=
(
char
*
)
args
;
int
ret
=
SystemWatchParameter
(
name
,
HandleParamChange
,
NULL
);
if
(
ret
!=
0
)
{
return
0
;
}
while
(
1
)
{
pause
();
}
return
NULL
;
}
static
int32_t
BShellParamCmdWatch
(
BShellHandle
shell
,
int32_t
argc
,
char
*
argv
[])
{
PLUGIN_CHECK
(
argc
>=
1
,
return
-
1
,
"Invalid parameter"
);
PLUGIN_LOGV
(
"BShellParamCmdWatch %s"
,
argv
[
1
]);
pthread_t
thread
;
pthread_create
(
&
thread
,
NULL
,
CmdWatcher
,
argv
[
1
]);
static
int
index
=
0
;
int
ret
=
SystemWatchParameter
(
argv
[
1
],
HandleParamChange
,
(
void
*
)
index
);
if
(
ret
!=
0
)
{
PLUGIN_LOGE
(
"Failed to watch %s"
,
argv
[
1
]);
return
0
;
}
index
++
;
return
0
;
}
...
...
test/unittest/param/watcher_agent_unittest.cpp
浏览文件 @
09b41e46
...
...
@@ -20,6 +20,7 @@
#include "iwatcher.h"
#include "iwatcher_manager.h"
#include "message_parcel.h"
#include "parameter.h"
#include "param_manager.h"
#include "param_utils.h"
#include "system_ability_definition.h"
...
...
@@ -53,10 +54,33 @@ public:
int
TestAddWatcher
()
{
int
ret
=
SystemWatchParameter
(
"test.permission.watcher.test1"
,
TestParameterChange
,
nullptr
);
size_t
index
=
1
;
int
ret
=
SystemWatchParameter
(
"test.permission.watcher.test1"
,
TestParameterChange
,
(
void
*
)
index
);
EXPECT_EQ
(
ret
,
0
);
ret
=
SystemWatchParameter
(
"test.permission.watcher.test1*"
,
TestParameterChange
,
nullptr
);
ret
=
SystemWatchParameter
(
"test.permission.watcher.test1*"
,
TestParameterChange
,
(
void
*
)
index
);
EXPECT_EQ
(
ret
,
0
);
// repeat add, return fail
ret
=
SystemWatchParameter
(
"test.permission.watcher.test1"
,
TestParameterChange
,
(
void
*
)
index
);
EXPECT_NE
(
ret
,
0
);
index
++
;
ret
=
SystemWatchParameter
(
"test.permission.watcher.test1"
,
TestParameterChange
,
(
void
*
)
index
);
EXPECT_EQ
(
ret
,
0
);
index
++
;
ret
=
SystemWatchParameter
(
"test.permission.watcher.test1"
,
TestParameterChange
,
(
void
*
)
index
);
EXPECT_EQ
(
ret
,
0
);
// delete
ret
=
RemoveParameterWatcher
(
"test.permission.watcher.test1"
,
TestParameterChange
,
(
void
*
)
index
);
EXPECT_EQ
(
ret
,
0
);
index
--
;
ret
=
RemoveParameterWatcher
(
"test.permission.watcher.test1"
,
TestParameterChange
,
(
void
*
)
index
);
EXPECT_EQ
(
ret
,
0
);
index
--
;
ret
=
RemoveParameterWatcher
(
"test.permission.watcher.test1"
,
TestParameterChange
,
(
void
*
)
index
);
EXPECT_EQ
(
ret
,
0
);
ret
=
RemoveParameterWatcher
(
"test.permission.watcher.test1"
,
TestParameterChange
,
(
void
*
)
index
);
EXPECT_EQ
(
ret
,
0
);
// 非法
ret
=
SystemWatchParameter
(
"test.permission.watcher.tes^^^^t1*"
,
TestParameterChange
,
nullptr
);
EXPECT_NE
(
ret
,
0
);
...
...
@@ -98,7 +122,7 @@ public:
if
(
watcher
!=
nullptr
)
{
watcher
->
OnRemoteRequest
(
IWatcher
::
PARAM_CHANGE
,
data
,
reply
,
option
);
watcher
->
OnRemoteRequest
(
IWatcher
::
PARAM_CHANGE
+
1
,
data
,
reply
,
option
);
watcher
->
OnParame
r
terChange
(
"testname"
,
"testvalue"
);
watcher
->
OnParameterChange
(
"testname"
,
"testvalue"
);
}
return
0
;
}
...
...
test/unittest/param/watcher_proxy_unittest.cpp
浏览文件 @
09b41e46
...
...
@@ -41,9 +41,9 @@ public:
TestWatcher
()
{}
~
TestWatcher
()
=
default
;
void
OnParame
r
terChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
override
void
OnParameterChange
(
const
std
::
string
&
name
,
const
std
::
string
&
value
)
override
{
printf
(
"TestWatcher::OnParame
r
terChange name %s %s
\n
"
,
name
.
c_str
(),
value
.
c_str
());
printf
(
"TestWatcher::OnParameterChange name %s %s
\n
"
,
name
.
c_str
(),
value
.
c_str
());
}
};
...
...
@@ -124,7 +124,7 @@ public:
EXPECT_NE
(
remoteObj
,
nullptr
);
WatcherProxy
*
watcher
=
new
WatcherProxy
(
remoteObj
);
if
(
watcher
!=
nullptr
)
{
watcher
->
OnParame
r
terChange
(
name
,
value
);
watcher
->
OnParameterChange
(
name
,
value
);
delete
watcher
;
}
return
0
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录