Add DSchedEvent Listener

issue:https://gitee.com/openharmony/ability_dmsfwk/issues/I8JF3K

Signed-off-by: m30043719 <maxiaodong25@huawei.com>
This commit is contained in:
m30043719 2023-11-25 15:00:06 +08:00
parent 80dfdfa234
commit 5b41fc0b8a
27 changed files with 1240 additions and 3 deletions

View File

@ -71,6 +71,8 @@
"fwk_group": [
"//foundation/ability/dmsfwk/interfaces/innerkits/common:common_sdk",
"//foundation/ability/dmsfwk/interfaces/innerkits/continuation_manager:continuation_manager",
"//foundation/ability/dmsfwk/interfaces/innerkits/distributed_event:distributed_sdk",
"//foundation/ability/dmsfwk/interfaces/innerkits/tests:dms_sdk_demo",
"//foundation/ability/dmsfwk/interfaces/kits/napi:napi_packages"
],
"service_group": [

View File

@ -12,6 +12,7 @@
# limitations under the License.
declare_args() {
ams_path = "//foundation/ability/ability_runtime"
dms_path = "//foundation/ability/dmsfwk"
module_output_path = "dmsfwk/dmsfwk"
communication_path = "//foundation/communication"

View File

@ -17,5 +17,7 @@ group("innerkits_target") {
deps = [
"common:common_sdk",
"continuation_manager:continuation_manager",
"distributed_event:distributed_sdk",
"tests:dms_sdk_demo",
]
}

View File

@ -0,0 +1,64 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
import("../../../dmsfwk.gni")
config("dms_sdk_public_config") {
visibility = [ ":*" ]
include_dirs = [
"${ams_path}/interfaces/inner_api/ability_manager/include/",
"${ams_path}/services/common/include/",
"${dms_path}/interfaces/innerkits/distributed_event/include/",
"${dms_path}/services/dtbabilitymgr/include/",
"${dms_path}/services/dtbschedmgr/include/",
"${dms_path}/utils/native/include/",
]
cflags = []
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
}
ohos_shared_library("distributed_sdk") {
branch_protector_ret = "pac_ret"
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
sources = [
"${dms_path}/interfaces/innerkits/distributed_event/src/dms_client.cpp",
"${dms_path}/interfaces/innerkits/distributed_event/src/dms_handler.cpp",
"${dms_path}/interfaces/innerkits/distributed_event/src/dms_listener_stub.cpp",
"${dms_path}/interfaces/innerkits/distributed_event/src/dms_sa_client.cpp",
]
public_configs = [
":dms_sdk_public_config",
"${dms_path}/services/dtbschedmgr/test/resource:coverage_flags",
]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"ipc:ipc_core",
"samgr:samgr_proxy",
]
subsystem_name = "ability"
innerapi_tags = [ "platformsdk" ]
part_name = "dmsfwk"
}

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_DISTRIBUTED_EVENT_LISTENER_H
#define OHOS_DISTRIBUTED_EVENT_LISTENER_H
#include <memory>
#include "iremote_broker.h"
namespace OHOS {
namespace DistributedSchedule {
class EventNotify {
public:
int32_t eventResult_ = -1;
std::string srcNetworkId_;
std::string dstNetworkId_;
std::string bundleName_;
std::string moduleName_;
std::string abilityName_;
};
class IDSchedEventListener : public OHOS::IRemoteBroker {
public:
DECLARE_INTERFACE_DESCRIPTOR(u"ohos.distributedSchedule.dschedeventlistener");
virtual ~IDSchedEventListener() {};
virtual void DSchedEventNotify(EventNotify &notify) = 0;
enum class Message {
// ipc id for DSchedEventCallback
DSCHED_EVENT_CALLBACK = 0,
};
};
} // namespace DistributedSchedule
} // namespace OHOS
#endif // OHOS_DISTRIBUTED_EVENT_LISTENER_H

View File

@ -0,0 +1,112 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_DISTRIBUTED_PARCEL_HELPER_H
#define OHOS_DISTRIBUTED_PARCEL_HELPER_H
#include <cinttypes>
#include "hilog_wrapper.h"
namespace OHOS {
namespace DistributedSchedule {
#define PARCEL_WRITE_HELPER(parcel, type, value) \
do { \
bool ret = parcel.Write##type((value)); \
if (!ret) { \
HILOG_ERROR("%{public}s write value failed!", __func__); \
return ERR_FLATTEN_OBJECT; \
} \
} while (0)
#define PARCEL_WRITE_HELPER_NORET(parcel, type, value) \
do { \
bool ret = parcel.Write##type((value)); \
if (!ret) { \
HILOG_ERROR("%{public}s write value failed!", __func__); \
return; \
} \
} while (0)
#define PARCEL_WRITE_HELPER_RET(parcel, type, value, failRet) \
do { \
bool ret = parcel.Write##type((value)); \
if (!ret) { \
HILOG_ERROR("%{public}s write value failed!", __func__); \
return failRet; \
} \
} while (0)
#define PARCEL_READ_HELPER(parcel, type, out) \
do { \
bool ret = parcel.Read##type((out)); \
if (!ret) { \
HILOG_ERROR("%{public}s read value failed!", __func__); \
return ERR_FLATTEN_OBJECT; \
} \
} while (0)
#define PARCEL_READ_HELPER_RET(parcel, type, out, failRet) \
do { \
bool ret = parcel.Read##type((out)); \
if (!ret) { \
HILOG_ERROR("%{public}s read value failed!", __func__); \
return failRet; \
} \
} while (0)
#define PARCEL_READ_HELPER_NORET(parcel, type, out) \
do { \
bool ret = parcel.Read##type((out)); \
if (!ret) { \
HILOG_WARN("%{public}s read value failed!", __func__); \
} \
} while (0)
#define PARCEL_TRANSACT_SYNC_RET_INT(remote, code, data, reply) \
do { \
MessageOption option; \
int32_t error = remote->SendRequest(code, data, reply, option); \
if (error != ERR_NONE) { \
HILOG_ERROR("%{public}s transact failed, error: %{public}d", __func__, error); \
return error; \
} \
int32_t result = reply.ReadInt32(); \
HILOG_INFO("%{public}s get result from server data = %{public}d", __func__, result); \
return result; \
} while (0)
#define PARCEL_TRANSACT_SYNC_NORET(remote, code, data, reply) \
do { \
MessageOption option; \
int32_t result = remote->SendRequest(code, data, reply, option); \
if (result != ERR_NONE) { \
HILOG_ERROR("%{public}s transact failed, result: %{public}d", __func__, result); \
return; \
} \
HILOG_DEBUG("%{public}s transact success!", __func__); \
} while (0)
#define PARCEL_WRITE_REPLY_NOERROR(reply, type, result) \
do { \
bool ret = reply.Write##type(result); \
if (!ret) { \
HILOG_WARN("%{public}s write reply failed.", __func__); \
} \
return ERR_NONE; \
} while (0)
} // namespace DistributedSchedule
} // namespace OHOS
#endif /* OHOS_DISTRIBUTED_PARCEL_HELPER_H */

View File

@ -0,0 +1,40 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_DMS_CLIENT_H
#define OHOS_DMS_CLIENT_H
#include <string>
#include "ability_manager_errors.h"
#include "distributed_event_listener.h"
#include "iremote_broker.h"
namespace OHOS {
namespace DistributedSchedule {
class DistributedClient {
public:
int32_t RegisterDSchedEventListener(const std::string& type, const sptr<IDSchedEventListener>& obj);
int32_t UnRegisterDSchedEventListener(const std::string& type, const sptr<IDSchedEventListener>& obj);
enum {
REGISTER_DSCHED_EVENT_LISTENER = 262,
UNREGISTER_DSCHED_EVENT_LISTENER = 263,
};
private:
sptr<IRemoteObject> GetDmsProxy();
};
} // namespace DistributedSchedule
} // namespace OHOS
#endif

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_DMS_HANDLER_H
#define OHOS_DMS_HANDLER_H
#include <condition_variable>
#include <map>
#include <mutex>
#include <string>
#include <vector>
#include "distributed_event_listener.h"
#include "dms_sa_client.h"
#include "refbase.h"
#include "single_instance.h"
namespace OHOS {
namespace DistributedSchedule {
class DmsHandler {
DECLARE_SINGLE_INSTANCE(DmsHandler);
public:
int32_t RegisterDSchedEventListener(std::string type, sptr<IDSchedEventListener> &listener);
int32_t UnRegisterDSchedEventListener(std::string type, sptr<IDSchedEventListener> &listener);
};
} // namespace DistributedSchedule
} // namespace OHOS
#endif // OHOS_DMS_HANDLER_H

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_DMS_LISTENER_STUB_H
#define OHOS_DMS_LISTENER_STUB_H
#include <cstdint>
#include <map>
#include "distributed_event_listener.h"
#include "dtbschedmgr_log.h"
#include "iremote_stub.h"
#include "message_parcel.h"
#include "refbase.h"
#include "sam_log.h"
namespace OHOS {
namespace DistributedSchedule {
class DSchedEventListenerStub : public IRemoteStub<IDSchedEventListener> {
public:
DSchedEventListenerStub();
virtual ~DSchedEventListenerStub();
int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
private:
void DSchedEventNotifyInner(MessageParcel &data, MessageParcel &reply);
using DistributedScheduleFunc = void (DSchedEventListenerStub::*)(MessageParcel &data, MessageParcel &reply);
std::map<uint32_t, DistributedScheduleFunc> memberFuncMap_;
};
} // namespace DistributedSchedule
} // namespace OHOS
#endif // OHOS_DMS_LISTENER_STUB_H

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_DMS_SA_CLIENT_H
#define OHOS_DMS_SA_CLIENT_H
#include <mutex>
#include "distributed_event_listener.h"
#include "dms_client.h"
#include "dms_handler.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "sam_log.h"
#include "system_ability_status_change_stub.h"
#include "system_ability_definition.h"
namespace OHOS {
namespace DistributedSchedule {
class DmsSaClient : public SystemAbilityStatusChangeStub {
public:
static DmsSaClient &GetInstance();
bool SubscribeDmsSA();
int32_t AddDSchedEventListener(const std::string& type, const sptr<IDSchedEventListener>& listener);
int32_t DelDSchedEventListener(const std::string& type, const sptr<IDSchedEventListener>& listener);
void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
private:
DmsSaClient() {};
~DmsSaClient() {};
bool hasSubscribeDmsSA_ = false;
OHOS::sptr<ISystemAbilityManager> saMgrProxy_;
std::map<std::string, sptr<IDSchedEventListener>> listeners_;
std::mutex eventMutex_;
};
class DmsSystemAbilityStatusChange : public SystemAbilityStatusChangeStub {
public:
DmsSystemAbilityStatusChange();
~DmsSystemAbilityStatusChange();
void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
};
} // namespace DistributedSchedule
} // namespace OHOS
#endif

View File

@ -0,0 +1,80 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dms_client.h"
#include "ability_manager_errors.h"
#include "distributed_parcel_helper.h"
#include "if_system_ability_manager.h"
#include "ipc_skeleton.h"
#include "iservice_registry.h"
#include "string_ex.h"
#include "system_ability_definition.h"
namespace OHOS {
namespace DistributedSchedule {
namespace {
const std::u16string DMS_PROXY_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
}
sptr<IRemoteObject> DistributedClient::GetDmsProxy()
{
auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (samgrProxy == nullptr) {
HILOG_ERROR("fail to get samgr.");
return nullptr;
}
return samgrProxy->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID);
}
int32_t DistributedClient::RegisterDSchedEventListener(const std::string& type,
const sptr<IDSchedEventListener>& obj)
{
HILOG_INFO("RegisterDSchedEventListener called");
sptr<IRemoteObject> remote = GetDmsProxy();
if (remote == nullptr) {
HILOG_ERROR("remote system ablity is nullptr");
return AAFwk::INVALID_PARAMETERS_ERR;
}
MessageParcel data;
MessageParcel reply;
if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
return ERR_FLATTEN_OBJECT;
}
PARCEL_WRITE_HELPER(data, String, type);
PARCEL_WRITE_HELPER(data, RemoteObject, obj->AsObject());
PARCEL_TRANSACT_SYNC_RET_INT(remote, REGISTER_DSCHED_EVENT_LISTENER, data, reply);
}
int32_t DistributedClient::UnRegisterDSchedEventListener(const std::string& type,
const sptr<IDSchedEventListener>& obj)
{
HILOG_INFO("UnRegisterDSchedEventListener called");
sptr<IRemoteObject> remote = GetDmsProxy();
if (remote == nullptr) {
HILOG_ERROR("remote system ablity is null");
return AAFwk::INVALID_PARAMETERS_ERR;
}
MessageParcel data;
MessageParcel reply;
if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
HILOG_DEBUG("write interface token failed.");
return ERR_FLATTEN_OBJECT;
}
PARCEL_WRITE_HELPER(data, String, type);
PARCEL_WRITE_HELPER(data, RemoteObject, obj->AsObject());
PARCEL_TRANSACT_SYNC_RET_INT(remote, UNREGISTER_DSCHED_EVENT_LISTENER, data, reply);
}
} // namespace DistributedSchedule
} // namespace OHOS

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dms_handler.h"
#include "dtbschedmgr_log.h"
#include "single_instance.h"
namespace OHOS {
namespace DistributedSchedule {
IMPLEMENT_SINGLE_INSTANCE(DmsHandler);
namespace {
const std::string TAG = "DmsHandle";
}
int32_t DmsHandler::RegisterDSchedEventListener(std::string type, sptr<IDSchedEventListener>& listener)
{
HILOGI("RegisterListener type:%s", type.c_str());
if (listener == nullptr) {
HILOGE("RegisterListener is null.");
return GET_REMOTE_DMS_FAIL;
}
int32_t ret = DmsSaClient::GetInstance().AddDSchedEventListener(type, listener);
if (ret != 0) {
HILOGE("DmsSaClient AddListener fail");
return GET_REMOTE_DMS_FAIL;
}
return 0;
}
int32_t DmsHandler::UnRegisterDSchedEventListener(std::string type, sptr<IDSchedEventListener> &listener)
{
HILOGI("UnRegisterListener");
if (listener == nullptr) {
HILOGI("UnRegisterListener is null.");
return GET_REMOTE_DMS_FAIL;
}
int32_t ret = DmsSaClient::GetInstance().DelDSchedEventListener(type, listener);
if (ret != 0) {
HILOGE("DmsSaClient AddListener fail");
return GET_REMOTE_DMS_FAIL;
}
return 0;
}
} // namespace DistributedSchedule
} // namespace OHOS

View File

@ -0,0 +1,89 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dms_listener_stub.h"
#include <cstring>
#include <memory>
#include <string>
#include "ipc_object_stub.h"
#include "ipc_types.h"
#include "message_option.h"
#include "message_parcel.h"
#include "securec.h"
namespace OHOS {
namespace DistributedSchedule {
namespace {
const std::string TAG = "DmsListenerStub";
}
DSchedEventListenerStub::DSchedEventListenerStub()
{
memberFuncMap_[static_cast<uint32_t>(DSchedEventListenerStub::Message::DSCHED_EVENT_CALLBACK)] =
&DSchedEventListenerStub::DSchedEventNotifyInner;
}
DSchedEventListenerStub::~DSchedEventListenerStub()
{}
int32_t DSchedEventListenerStub::OnRemoteRequest(uint32_t code,
MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
HILOGI("DSchedEventListenerStub OnRemoteRequest code: %d", code);
std::u16string desc = DSchedEventListenerStub::GetDescriptor();
std::u16string remoteDesc = data.ReadInterfaceToken();
if (desc != remoteDesc) {
HILOGE("remoteDesc is invalid!");
return GET_REMOTE_DMS_FAIL;
}
auto itFunc = memberFuncMap_.find(code);
if (itFunc == memberFuncMap_.end()) {
HILOGE("memberFuncMap_ not found");
return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}
auto memberFunc = itFunc->second;
(this->*memberFunc)(data, reply);
return 0;
}
void DSchedEventListenerStub::DSchedEventNotifyInner(MessageParcel &data, MessageParcel &reply)
{
HILOGI("DSchedEventListenerStub DSchedEventNotifyInner");
int32_t ret = 0;
do {
int32_t eventResult = data.ReadInt32();
std::string srcNetworkId = data.ReadString();
std::string dstNetworkId = data.ReadString();
std::string bundleName = data.ReadString();
std::string moduleName = data.ReadString();
std::string abilityName = data.ReadString();
EventNotify eventNotify;
eventNotify.eventResult_ = eventResult;
eventNotify.srcNetworkId_ = srcNetworkId;
eventNotify.dstNetworkId_ = dstNetworkId;
eventNotify.bundleName_ = bundleName;
eventNotify.moduleName_ = moduleName;
eventNotify.abilityName_ = abilityName;
DSchedEventNotify(eventNotify);
} while (0);
if (!reply.WriteInt32(ret)) {
HILOGE("DSchedEventNotifyInner write ret failed, ret = %d", ret);
}
}
} // namespace DistributedSchedule
} // namespace OHOS

View File

@ -0,0 +1,126 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dms_sa_client.h"
namespace OHOS {
namespace DistributedSchedule {
DmsSaClient &DmsSaClient::GetInstance()
{
HILOGI("%{public}s called.", __func__);
static DmsSaClient instance;
return instance;
}
bool DmsSaClient::SubscribeDmsSA()
{
HILOGI("%{public}s called.", __func__);
sptr<DmsSystemAbilityStatusChange> callback(new DmsSystemAbilityStatusChange());
int32_t ret = saMgrProxy_->SubscribeSystemAbility(DISTRIBUTED_SCHED_SA_ID, callback);
if (ret != ERR_OK) {
HILOGE("Failed to subscribe system ability DISTRIBUTED_SCHED_SA_ID ret:%{public}d", ret);
return false;
}
return true;
}
int32_t DmsSaClient::AddDSchedEventListener(const std::string& type, const sptr<IDSchedEventListener>& listener)
{
HILOGI("%{public}s called, the type is %{public}s", __func__, type.c_str());
saMgrProxy_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (saMgrProxy_ == nullptr) {
HILOGE("fail to get saMgrProxy.");
return AAFwk::INNER_ERR;
}
if (saMgrProxy_->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID)) {
DistributedClient distributedClient;
distributedClient.RegisterDSchedEventListener(type, listener);
}
std::lock_guard<std::mutex> lock(eventMutex_);
if (!hasSubscribeDmsSA_) {
if (SubscribeDmsSA()) {
hasSubscribeDmsSA_ = true;
listeners_[type] = listener;
} else {
return AAFwk::INNER_ERR;
}
}
return NO_ERROR;
}
int32_t DmsSaClient::DelDSchedEventListener(const std::string& type, const sptr<IDSchedEventListener>& listener)
{
HILOGI("%{public}s called, the type is %{public}s", __func__, type.c_str());
if (saMgrProxy_->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID)) {
DistributedClient distributedClient;
distributedClient.UnRegisterDSchedEventListener(type, listener);
}
std::lock_guard<std::mutex> lock(eventMutex_);
listeners_.erase(type);
return NO_ERROR;
}
void DmsSaClient::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
{
HILOGI("%{public}s called, the systemAbilityId is %{public}d", __func__, systemAbilityId);
std::lock_guard<std::mutex> lock(eventMutex_);
if (systemAbilityId == DISTRIBUTED_SCHED_SA_ID) {
HILOGI("%{public}s listeners size: %{public}zu .", __func__, listeners_.size());
for (auto& listener : listeners_) {
DistributedClient distributedClient;
distributedClient.RegisterDSchedEventListener(listener.first, listener.second);
}
} else {
HILOGE("SystemAbilityId must be DISTRIBUTED_SCHED_SA_ID,but it is %{public}d", systemAbilityId);
}
}
void DmsSaClient::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
{
}
DmsSystemAbilityStatusChange::DmsSystemAbilityStatusChange()
{
HILOGI("DmsSystemAbilityStatusChange create");
}
DmsSystemAbilityStatusChange::~DmsSystemAbilityStatusChange()
{
HILOGI("DmsSystemAbilityStatusChange delete");
}
void DmsSystemAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
{
HILOGI("OnAddSystemAbility called, the systemAbilityId is %d", systemAbilityId);
if (systemAbilityId != DISTRIBUTED_SCHED_SA_ID) {
HILOGE("SystemAbilityId must be DISTRIBUTED_SCHED_SA_ID,but it is %d", systemAbilityId);
return;
}
DmsSaClient::GetInstance().OnAddSystemAbility(systemAbilityId, deviceId);
HILOGI("OnAddSystemAbility called end, the systemAbilityId is %d", systemAbilityId);
}
void DmsSystemAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
{
HILOGI("OnRemoveSystemAbility called, the systemAbilityId is %d", systemAbilityId);
if (systemAbilityId != DISTRIBUTED_SCHED_SA_ID) {
HILOGE("SystemAbilityId must be DISTRIBUTED_SCHED_SA_ID,but it is %d", systemAbilityId);
return;
}
// notify listener
}
} // namespace DistributedSchedule
} // namespace OHOS

View File

@ -0,0 +1,71 @@
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
import("../../../dmsfwk.gni")
config("tests_public_config") {
visibility = [ ":*" ]
include_dirs = [
"${dms_path}/interfaces/innerkits/distributed_event/include",
"${dms_path}/interfaces/innerkits/tests/",
"${dms_path}/services/dtbabilitymgr/include/",
"${dms_path}/utils/native/include/",
]
cflags = []
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
}
ohos_shared_library("dms_sdk_demo") {
branch_protector_ret = "pac_ret"
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
deps =
[ "${dms_path}/interfaces/innerkits/distributed_event:distributed_sdk" ]
defines = [
"HI_LOG_ENABLE",
"LOG_TAG=\"dms_sdk_demo\"",
"LOG_DOMAIN=0xD004100",
"DH_LOG_TAG=\"dms_sdk_demo\"",
"VERSION_STR=\"1.0.0.302\"",
"DMS_SOURCE",
]
sources = [
"${dms_path}/interfaces/innerkits/distributed_event/src/dms_listener_stub.cpp",
"${dms_path}/interfaces/innerkits/tests/dms_sdk_demo.cpp",
]
public_configs = [
":tests_public_config",
"${dms_path}/services/dtbschedmgr/test/resource:coverage_flags",
]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"ipc:ipc_core",
"samgr:samgr_proxy",
]
subsystem_name = "ability"
part_name = "dmsfwk"
}

View File

@ -0,0 +1,89 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dms_sdk_demo.h"
#include <iostream>
#include <string>
#include <vector>
using namespace OHOS;
using namespace DistributedSchedule;
using namespace std;
Business g_business;
DmsHandler &dmsSourceHandlerdemo = DmsHandler::GetInstance();
std::string g_type = "IDSchedEventListener";
sptr<IDSchedEventListener> listener = sptr<IDSchedEventListener>(new Business());
void Business::Register()
{
int32_t result = 0;
result = dmsSourceHandlerdemo.RegisterDSchedEventListener(g_type, listener);
if (result < 0) {
cout << "RegisterDSchedEventListener failed.CODE = " << result << endl;
} else {
cout << "RegisterDSchedEventListener succeed.CODE = " << result << endl;
}
}
void Business::UnRegister()
{
int32_t result = 0;
result = dmsSourceHandlerdemo.UnRegisterDSchedEventListener(g_type, listener);
if (result < 0) {
cout << "UnRegisterDSchedEventListener failed.CODE = " << result << endl;
} else {
cout << "UnRegisterDSchedEventListener succeed.CODE = " << result << endl;
}
}
void Business::DSchedEventNotify(EventNotify& notify)
{
cout << endl << "DSchedEventNotify Start." << endl;
cout << "eventResult: " << notify.eventResult_ << endl;
cout << "srcNetworkId: " << notify.srcNetworkId_ << endl;
cout << "dstNetworkId: " << notify.dstNetworkId_ << endl;
cout << "bundleName: " << notify.bundleName_ << endl;
cout << "moduleName: " << notify.moduleName_ << endl;
cout << "abilityName: " << notify.abilityName_ << endl;
cout << "DSchedEventNotify Success." << endl;
}
int main()
{
cout << "Please select an option to test the interface:" << endl;
cout << "A.RegisterDSchedEventListener B.UnRegisterDSchedEventListener X.exit" << endl;
cout << "\n" << endl;
char cmd;
while (cin >> cmd) {
if (cmd <= 'z' && cmd >= 'a') {
cmd = cmd + 'A' - 'a';
}
switch (cmd) {
case 'A' : g_business.Register();
break;
case 'B' : g_business.UnRegister();
break;
case 'X' : g_business.UnRegister();
return 0;
default:
cout << "unknown cmd, please input again" << endl;
}
}
return 0;
}

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_DMS_SDK_DEMO_H
#define OHOS_DMS_SDK_DEMO_H
#include "dms_handler.h"
#include "distributed_event_listener.h"
#include "dms_sa_client.h"
#include "dms_listener_stub.h"
namespace OHOS {
namespace DistributedSchedule {
class Business : public DSchedEventListenerStub {
public:
void DSchedEventNotify(EventNotify &notify) override;
void Register();
void UnRegister();
};
}
}
#endif // OHOS_DMS_SDK_DEMO_H

View File

@ -28,6 +28,13 @@ namespace OHOS {
namespace DistributedSchedule {
using FuncContinuationCallback = std::function<void(int32_t missionId)>;
struct ContinueEvent {
std::string srcNetworkId;
std::string dstNetworkId;
std::string bundleName;
std::string moduleName;
std::string abilityName;
};
class DSchedContinuation : public std::enable_shared_from_this<DSchedContinuation> {
public:
void Init(const FuncContinuationCallback& contCallback);
@ -39,12 +46,17 @@ public:
void RemoveTimeOut(int32_t missionId);
bool PushCallback(int32_t missionId, const sptr<IRemoteObject>& callback,
std::string deviceId, bool isFreeInstall);
bool PushCallback(const std::string& type, const sptr<IRemoteObject>& callback);
sptr<IRemoteObject> CleanupCallback(const std::string& type);
sptr<IRemoteObject> GetCallback(const std::string& type);
sptr<IRemoteObject> PopCallback(int32_t missionId);
int32_t NotifyMissionCenterResult(int32_t missionId, int32_t resultCode);
int32_t NotifyDSchedEventResult(const std::string& type, int32_t resultCode);
bool IsFreeInstall(int32_t missionId);
std::string GetTargetDevice(int32_t missionId);
bool IsCleanMission(int32_t missionId);
void SetCleanMissionFlag(int32_t missionId, bool isCleanMission);
ContinueEvent continueEvent_;
private:
class ContinuationHandler : public AppExecFwk::EventHandler {
@ -65,6 +77,7 @@ private:
std::mutex continuationLock_;
int32_t currSessionId_ = 1;
std::map<int32_t, sptr<IRemoteObject>> continuationMap_;
std::map<std::string, sptr<IRemoteObject>> continuationCallbackMap_;
std::map<int32_t, sptr<IRemoteObject>> callbackMap_;
std::map<int32_t, bool> freeInstall_;
std::map<int32_t, bool> cleanMission_;

View File

@ -79,6 +79,10 @@ public:
int32_t status, uint32_t accessToken) = 0;
virtual void NotifyCompleteContinuation(const std::u16string& devId, int32_t sessionId, bool isSuccess) = 0;
virtual int32_t NotifyContinuationResultFromRemote(int32_t sessionId, bool isSuccess) = 0;
virtual int32_t NotifyDSchedEventResultFromRemote(const std::string type, int32_t dSchedEventResult)
{
return 0;
}
virtual int32_t ConnectRemoteAbility(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
int32_t callerUid, int32_t callerPid, uint32_t accessToken) = 0;
virtual int32_t DisconnectRemoteAbility(const sptr<IRemoteObject>& connect, int32_t callerUid,
@ -95,6 +99,14 @@ public:
virtual int32_t StopSyncRemoteMissions(const std::string& devId) = 0;
virtual int32_t StopSyncMissionsFromRemote(const CallerInfo& callerInfo) = 0;
virtual int32_t RegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj) = 0;
virtual int32_t RegisterDSchedEventListener(const std::string& type, const sptr<IRemoteObject>& obj)
{
return 0;
}
virtual int32_t UnRegisterDSchedEventListener(const std::string& type, const sptr<IRemoteObject>& obj)
{
return 0;
}
virtual int32_t RegisterOnListener(const std::string& type, const sptr<IRemoteObject>& obj)
{
return 0;

View File

@ -46,6 +46,7 @@ public:
int32_t status, uint32_t accessToken) override;
void NotifyCompleteContinuation(const std::u16string& devId, int32_t sessionId, bool isSuccess) override;
int32_t NotifyContinuationResultFromRemote(int32_t sessionId, bool isSuccess) override;
int32_t NotifyDSchedEventResultFromRemote(const std::string type, int32_t dSchedEventResult) override;
int32_t ConnectRemoteAbility(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
int32_t callerUid, int32_t callerPid, uint32_t accessToken) override;
int32_t DisconnectRemoteAbility(const sptr<IRemoteObject>& connect, int32_t callerUid,
@ -59,6 +60,7 @@ public:
int32_t StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag) override;
int32_t StopSyncRemoteMissions(const std::string& devId) override;
int32_t RegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj) override;
int32_t RegisterDSchedEventListener(const std::string& type, const sptr<IRemoteObject>& obj) override;
int32_t RegisterOnListener(const std::string& type, const sptr<IRemoteObject>& obj) override;
int32_t RegisterOffListener(const std::string& type, const sptr<IRemoteObject>& obj) override;
int32_t UnRegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj) override;

View File

@ -102,11 +102,14 @@ public:
int32_t ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId,
const std::string& bundleName, const sptr<IRemoteObject>& callback,
const OHOS::AAFwk::WantParams& wantParams) override;
int32_t DealDSchedEventResult(const OHOS::AAFwk::Want& want, int32_t status);
int32_t StartContinuation(const OHOS::AAFwk::Want& want, int32_t missionId, int32_t callerUid,
int32_t status, uint32_t accessToken) override;
void NotifyCompleteContinuation(const std::u16string& devId, int32_t sessionId, bool isSuccess) override;
int32_t NotifyContinuationResultFromRemote(int32_t sessionId, bool isSuccess) override;
int32_t NotifyDSchedEventResultFromRemote(const std::string type, int32_t dSchedEventResult) override;
void NotifyContinuationCallbackResult(int32_t missionId, int32_t resultCode);
void NotifyDSchedEventCallbackResult(const std::string type, int32_t resultCode);
int32_t NotifyFreeInstallResult(const CallbackTaskItem item, int32_t resultCode);
int32_t ConnectRemoteAbility(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
int32_t callerUid, int32_t callerPid, uint32_t accessToken) override;
@ -130,6 +133,8 @@ public:
int32_t StopSyncRemoteMissions(const std::string& devId) override;
int32_t StopSyncMissionsFromRemote(const CallerInfo& callerInfo) override;
int32_t RegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj) override;
int32_t RegisterDSchedEventListener(const std::string& type, const sptr<IRemoteObject>& obj) override;
int32_t UnRegisterDSchedEventListener(const std::string& type, const sptr<IRemoteObject>& obj) override;
int32_t RegisterOnListener(const std::string& type, const sptr<IRemoteObject>& obj) override;
int32_t RegisterOffListener(const std::string& type, const sptr<IRemoteObject>& obj) override;
int32_t UnRegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj) override;

View File

@ -40,6 +40,7 @@ private:
int32_t StartContinuationInner(MessageParcel& data, MessageParcel& reply);
int32_t NotifyCompleteContinuationInner(MessageParcel& data, MessageParcel& reply);
int32_t NotifyContinuationResultFromRemoteInner(MessageParcel& data, MessageParcel& reply);
int32_t NotifyDSchedEventResultFromRemoteInner(MessageParcel& data, MessageParcel& reply);
int32_t ConnectRemoteAbilityInner(MessageParcel& data, MessageParcel& reply);
int32_t DisconnectRemoteAbilityInner(MessageParcel& data, MessageParcel& reply);
int32_t ConnectAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply);
@ -50,6 +51,8 @@ private:
int32_t GetMissionInfosInner(MessageParcel& data, MessageParcel& reply);
int32_t GetRemoteMissionSnapshotInfoInner(MessageParcel& data, MessageParcel& reply);
int32_t RegisterMissionListenerInner(MessageParcel& data, MessageParcel& reply);
int32_t RegisterDSchedEventListenerInner(MessageParcel& data, MessageParcel& reply);
int32_t UnRegisterDSchedEventListenerInner(MessageParcel& data, MessageParcel& reply);
int32_t RegisterOnListenerInner(MessageParcel& data, MessageParcel& reply);
int32_t RegisterOffListenerInner(MessageParcel& data, MessageParcel& reply);
int32_t UnRegisterMissionListenerInner(MessageParcel& data, MessageParcel& reply);

View File

@ -35,6 +35,7 @@ enum class IDSchedInterfaceCode : uint32_t {
UNREGISTER_ABILITY_TOKEN = 15,
CONTINUE_ABILITY = 16,
NOTIFY_PROCESS_DIED_FROM_REMOTE = 17,
NOTIFY_DSCHED_EVENT_RESULT_FROM_REMOTE = 18,
SEND_RESULT_FROM_REMOTE = 20,
GET_REMOTE_APPTHREAD = 35,
CONTINUE_MISSION = 36,
@ -83,6 +84,8 @@ enum class IDSchedInterfaceCode : uint32_t {
STOP_EXTERNSION_ABILITY_FROM_REMOTE = 226,
REGISTER_ON_LISTENER = 260,
REGISTER_OFF_LISTENER = 261,
REGISTER_DSCHED_EVENT_LISTENER = 262,
UNREGISTER_DSCHED_EVENT_LISTENER = 263,
// request code for set continue state
SET_MISSION_CONTINUE_STATE = 300,

View File

@ -26,6 +26,8 @@ constexpr int64_t CONTINUATION_DELAY_TIME = 20000;
const std::string TAG = "DSchedContinuation";
const std::u16string NAPI_MISSION_CENTER_INTERFACE_TOKEN = u"ohos.DistributedSchedule.IMissionCallback";
constexpr int32_t NOTIFY_MISSION_CENTER_RESULT = 4;
const std::u16string DSCHED_EVENT_TOKEN = u"ohos.distributedSchedule.dschedeventlistener";
constexpr int32_t DSCHED_EVENT_CALLBACK = 0;
}
void DSchedContinuation::Init(const FuncContinuationCallback& contCallback)
@ -150,6 +152,29 @@ std::string DSchedContinuation::GetTargetDevice(int32_t missionId)
return "";
}
bool DSchedContinuation::PushCallback(const std::string& type, const sptr<IRemoteObject>& callback)
{
HILOGI("DSchedContinuation PushCallback start!");
if (callback == nullptr) {
HILOGE("callback null!");
return false;
}
if (continuationHandler_ == nullptr) {
HILOGE("not initialized!");
return false;
}
std::lock_guard<std::mutex> autoLock(continuationLock_);
auto iterSession = continuationCallbackMap_.find(type);
if (iterSession != continuationCallbackMap_.end()) {
HILOGE("type:%{public}s exist!", type.c_str());
return false;
}
(void)continuationCallbackMap_.emplace(type, callback);
return true;
}
bool DSchedContinuation::PushCallback(int32_t missionId, const sptr<IRemoteObject>& callback,
std::string deviceId, bool isFreeInstall)
{
@ -178,6 +203,31 @@ bool DSchedContinuation::PushCallback(int32_t missionId, const sptr<IRemoteObjec
return true;
}
sptr<IRemoteObject> DSchedContinuation::GetCallback(const std::string& type)
{
std::lock_guard<std::mutex> autoLock(continuationLock_);
auto iter = continuationCallbackMap_.find(type);
if (iter == continuationCallbackMap_.end()) {
HILOGW("PopCallback not found, type:%{public}s", type.c_str());
return nullptr;
}
sptr<IRemoteObject> callback = iter->second;
return callback;
}
sptr<IRemoteObject> DSchedContinuation::CleanupCallback(const std::string& type)
{
std::lock_guard<std::mutex> autoLock(continuationLock_);
auto iter = continuationCallbackMap_.find(type);
if (iter == continuationCallbackMap_.end()) {
HILOGW("PopCallback not found, type:%{public}s", type.c_str());
return nullptr;
}
sptr<IRemoteObject> callback = iter->second;
continuationCallbackMap_.erase(iter);
return callback;
}
sptr<IRemoteObject> DSchedContinuation::PopCallback(int32_t missionId)
{
std::lock_guard<std::mutex> autoLock(continuationLock_);
@ -204,6 +254,32 @@ sptr<IRemoteObject> DSchedContinuation::PopCallback(int32_t missionId)
return callback;
}
int32_t DSchedContinuation::NotifyDSchedEventResult(const std::string& type, int32_t resultCode)
{
HILOGI("GetCallback IDSchedEventListener");
sptr<IRemoteObject> callback = GetCallback(type);
if (callback == nullptr) {
HILOGE("NotifyMissionCenterResult IDSchedEventListener is null");
return INVALID_PARAMETERS_ERR;
}
MessageParcel data;
if (!data.WriteInterfaceToken(DSCHED_EVENT_TOKEN)) {
HILOGE("NotifyMissionCenterResult write token failed");
return INVALID_PARAMETERS_ERR;
}
PARCEL_WRITE_HELPER_RET(data, Int32, resultCode, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.srcNetworkId, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.dstNetworkId, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.bundleName, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.moduleName, false);
PARCEL_WRITE_HELPER_RET(data, String, continueEvent_.abilityName, false);
MessageParcel reply;
MessageOption option;
int32_t error = callback->SendRequest(DSCHED_EVENT_CALLBACK, data, reply, option);
HILOGI("NotifyDSchedEventListenerResult transact result: %{public}d", error);
return error;
}
int32_t DSchedContinuation::NotifyMissionCenterResult(int32_t missionId, int32_t resultCode)
{
sptr<IRemoteObject> callback = PopCallback(missionId);

View File

@ -219,6 +219,24 @@ void DistributedSchedProxy::NotifyCompleteContinuation(const std::u16string& dev
data, reply);
}
int32_t DistributedSchedProxy::NotifyDSchedEventResultFromRemote(const std::string type, int32_t dSchedEventResult)
{
sptr<IRemoteObject> remote = Remote();
if (remote == nullptr) {
HILOGE("NotifyDSchedEventResultFromRemote remote service null");
return ERR_NULL_OBJECT;
}
MessageParcel data;
if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
return ERR_FLATTEN_OBJECT;
}
PARCEL_WRITE_HELPER(data, String, type);
PARCEL_WRITE_HELPER(data, Int32, dSchedEventResult);
MessageParcel reply;
PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>
(IDSchedInterfaceCode::NOTIFY_DSCHED_EVENT_RESULT_FROM_REMOTE), data, reply);
}
int32_t DistributedSchedProxy::NotifyContinuationResultFromRemote(int32_t sessionId, bool isSuccess)
{
sptr<IRemoteObject> remote = Remote();
@ -491,6 +509,26 @@ int32_t DistributedSchedProxy::RegisterMissionListener(const std::u16string& dev
data, reply);
}
int32_t DistributedSchedProxy::RegisterDSchedEventListener(const std::string& type,
const sptr<IRemoteObject>& obj)
{
HILOGI("RegisterDSchedEventListener called");
sptr<IRemoteObject> remote = Remote();
if (remote == nullptr) {
HILOGE("remote system ability is null");
return ERR_NULL_OBJECT;
}
MessageParcel data;
MessageParcel reply;
if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
return ERR_FLATTEN_OBJECT;
}
PARCEL_WRITE_HELPER(data, String, type);
PARCEL_WRITE_HELPER(data, RemoteObject, obj);
PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_DSCHED_EVENT_LISTENER),
data, reply);
}
int32_t DistributedSchedProxy::RegisterOnListener(const std::string& type,
const sptr<IRemoteObject>& obj)
{

View File

@ -89,6 +89,7 @@ const std::string DMS_MISSION_ID = "dmsMissionId";
const std::string SUPPORT_CONTINUE_PAGE_STACK_KEY = "ohos.extra.param.key.supportContinuePageStack";
const std::string SUPPORT_CONTINUE_SOURCE_EXIT_KEY = "ohos.extra.param.key.supportContinueSourceExit";
const std::string SUPPORT_CONTINUE_MODULE_NAME_UPDATE_KEY = "ohos.extra.param.key.supportContinueModuleNameUpdate";
const std::string DSCHED_EVENT_KEY = "IDSchedEventListener";
constexpr int32_t DEFAULT_DMS_MISSION_ID = -1;
constexpr int32_t DEFAULT_DMS_CONNECT_TOKEN = -1;
constexpr int32_t BIND_CONNECT_RETRY_TIMES = 3;
@ -514,11 +515,17 @@ int32_t DistributedSchedService::ContinueMission(const std::string& srcDeviceId,
return ret;
}
#endif
dschedContinuation_->continueEvent_.srcNetworkId = srcDeviceId;
dschedContinuation_->continueEvent_.dstNetworkId = dstDeviceId;
dschedContinuation_->continueEvent_.bundleName = bundleName;
return ContinueLocalMission(dstDeviceId, missionId, callback, wantParams);
} else if (dstDeviceId == localDevId) {
if (DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceInfoById(srcDeviceId) == nullptr) {
return INVALID_REMOTE_PARAMETERS_ERR;
}
dschedContinuation_->continueEvent_.srcNetworkId = dstDeviceId;
dschedContinuation_->continueEvent_.dstNetworkId = srcDeviceId;
dschedContinuation_->continueEvent_.bundleName = bundleName;
return ContinueRemoteMission(srcDeviceId, dstDeviceId, bundleName, callback, wantParams);
} else {
HILOGE("source or target device must be local!");
@ -554,10 +561,33 @@ int32_t DistributedSchedService::SetWantForContinuation(AAFwk::Want& newWant, in
return ERR_OK;
}
int32_t DistributedSchedService::DealDSchedEventResult(const OHOS::AAFwk::Want& want, int32_t status)
{
dschedContinuation_->continueEvent_.moduleName = want.GetElement().GetModuleName();
HILOGI("dschedContinuation_->continueEvent_.moduleName %{public}s",
dschedContinuation_->continueEvent_.moduleName.c_str());
dschedContinuation_->continueEvent_.abilityName = want.GetElement().GetAbilityName();
HILOGI("dschedContinuation_->continueEvent_.abilityName %{public}s",
dschedContinuation_->continueEvent_.abilityName.c_str());
if (status != ERR_OK) {
HILOGD("want.GetElement().GetDeviceId result:%{public}s", want.GetElement().GetDeviceID().c_str());
std::string deviceId = want.GetElement().GetDeviceID();
sptr<IDistributedSched> remoteDms = GetRemoteDms(deviceId);
if (remoteDms == nullptr) {
HILOGE("NotifyCompleteContinuation get remote dms null!");
return INVALID_REMOTE_PARAMETERS_ERR;
}
int dSchedEventresult = remoteDms->NotifyDSchedEventResultFromRemote(DSCHED_EVENT_KEY, status);
HILOGI("NotifyDSchedEventResultFromRemote result:%{public}d", dSchedEventresult);
}
return ERR_OK;
}
int32_t DistributedSchedService::StartContinuation(const OHOS::AAFwk::Want& want, int32_t missionId,
int32_t callerUid, int32_t status, uint32_t accessToken)
{
HILOGD("[PerformanceTest] StartContinuation begin");
DealDSchedEventResult(want, status);
if (status != ERR_OK) {
HILOGE("continuation has been rejected, status: %{public}d", status);
NotifyContinuationCallbackResult(missionId, status);
@ -571,7 +601,6 @@ int32_t DistributedSchedService::StartContinuation(const OHOS::AAFwk::Want& want
HILOGD("StartContinuation: devId = %{private}s, bundleName = %{private}s, abilityName = %{private}s",
want.GetElement().GetDeviceID().c_str(), want.GetElement().GetBundleName().c_str(),
want.GetElement().GetAbilityName().c_str());
if (dschedContinuation_ == nullptr) {
HILOGE("StartContinuation continuation object null!");
return INVALID_REMOTE_PARAMETERS_ERR;
@ -579,7 +608,6 @@ int32_t DistributedSchedService::StartContinuation(const OHOS::AAFwk::Want& want
if (!dschedContinuation_->IsInContinuationProgress(missionId)) {
dschedContinuation_->SetTimeOut(missionId, CONTINUATION_TIMEOUT);
}
AAFwk::Want newWant = want;
int result = SetWantForContinuation(newWant, missionId);
if (result != ERR_OK) {
@ -601,7 +629,6 @@ int32_t DistributedSchedService::StartContinuation(const OHOS::AAFwk::Want& want
return result;
}
}
HILOGD("[PerformanceTest] StartContinuation end");
return result;
}
@ -622,6 +649,8 @@ void DistributedSchedService::NotifyCompleteContinuation(const std::u16string& d
HILOGE("NotifyCompleteContinuation get remote dms null!");
return;
}
int dSchedEventresult = dschedContinuation_->NotifyDSchedEventResult(DSCHED_EVENT_KEY, ERR_OK);
HILOGI("NotifyDSchedEventResult result:%{public}d", dSchedEventresult);
remoteDms->NotifyContinuationResultFromRemote(sessionId, isSuccess);
}
@ -637,6 +666,12 @@ int32_t DistributedSchedService::NotifyContinuationResultFromRemote(int32_t sess
return ERR_OK;
}
int32_t DistributedSchedService::NotifyDSchedEventResultFromRemote(const std::string type, int32_t dSchedEventResult)
{
NotifyDSchedEventCallbackResult(DSCHED_EVENT_KEY, dSchedEventResult);
return ERR_OK;
}
#ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
sptr<IFormMgr> DistributedSchedService::GetFormMgrProxy()
{
@ -712,6 +747,17 @@ void DistributedSchedService::NotifyContinuationCallbackResult(int32_t missionId
HILOGD("NotifyContinuationCallbackResult result:%{public}d", result);
}
void DistributedSchedService::NotifyDSchedEventCallbackResult(const std::string type, int32_t resultCode)
{
HILOGD("Continuation result is: %{public}d", resultCode);
if (dschedContinuation_ == nullptr) {
HILOGE("continuation object null!");
return;
}
int dSchedEventresult = dschedContinuation_->NotifyDSchedEventResult(DSCHED_EVENT_KEY, resultCode);
HILOGD("NotifyDSchedEventResult result:%{public}d", dSchedEventresult);
}
void DistributedSchedService::RemoteConnectAbilityMappingLocked(const sptr<IRemoteObject>& connect,
const std::string& localDeviceId, const std::string& remoteDeviceId, const AppExecFwk::ElementName& element,
const CallerInfo& callerInfo, TargetComponent targetComponent)
@ -1993,6 +2039,29 @@ int32_t DistributedSchedService::RegisterMissionListener(const std::u16string& d
return DistributedSchedMissionManager::GetInstance().RegisterMissionListener(devId, obj);
}
int32_t DistributedSchedService::RegisterDSchedEventListener(const std::string& type,
const sptr<IRemoteObject>& callback)
{
int32_t result = dschedContinuation_->PushCallback(type, callback);
HILOGI("result: %{public}d!", result);
if (result == ERR_INVALID_VALUE) {
return MISSION_FOR_CONTINUING_IS_NOT_ALIVE;
}
return result;
}
int32_t DistributedSchedService::UnRegisterDSchedEventListener(const std::string& type,
const sptr<IRemoteObject>& callback)
{
sptr<IRemoteObject> result = dschedContinuation_->CleanupCallback(type);
if (result == nullptr) {
HILOGI("The callback does not exist.");
} else {
HILOGI("Clearing the callback succeeded.");
}
return 0;
}
int32_t DistributedSchedService::RegisterOnListener(const std::string& type,
const sptr<IRemoteObject>& obj)
{

View File

@ -92,6 +92,10 @@ void DistributedSchedStub::InitLocalFuncsInner()
&DistributedSchedStub::GetRemoteMissionSnapshotInfoInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_MISSION_LISTENER)] =
&DistributedSchedStub::RegisterMissionListenerInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_DSCHED_EVENT_LISTENER)] =
&DistributedSchedStub::RegisterDSchedEventListenerInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::UNREGISTER_DSCHED_EVENT_LISTENER)] =
&DistributedSchedStub::UnRegisterDSchedEventListenerInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_ON_LISTENER)] =
&DistributedSchedStub::RegisterOnListenerInner;
localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_OFF_LISTENER)] =
@ -131,6 +135,8 @@ void DistributedSchedStub::InitRemoteFuncsInner()
&DistributedSchedStub::SendResultFromRemoteInner;
remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_CONTINUATION_RESULT_FROM_REMOTE)] =
&DistributedSchedStub::NotifyContinuationResultFromRemoteInner;
remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_DSCHED_EVENT_RESULT_FROM_REMOTE)] =
&DistributedSchedStub::NotifyDSchedEventResultFromRemoteInner;
remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::CONNECT_ABILITY_FROM_REMOTE)] =
&DistributedSchedStub::ConnectAbilityFromRemoteInner;
remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::DISCONNECT_ABILITY_FROM_REMOTE)] =
@ -476,6 +482,21 @@ int32_t DistributedSchedStub::NotifyCompleteContinuationInner(MessageParcel& dat
return ERR_OK;
}
int32_t DistributedSchedStub::NotifyDSchedEventResultFromRemoteInner(MessageParcel& data,
[[maybe_unused]] MessageParcel& reply)
{
if (!CheckCallingUid()) {
HILOGW("request DENIED!");
return DMS_PERMISSION_DENIED;
}
std::string type = "";
PARCEL_READ_HELPER(data, String, type);
int32_t dSchedEventResult = -1;
PARCEL_READ_HELPER(data, Int32, dSchedEventResult);
return NotifyDSchedEventResultFromRemote(type, dSchedEventResult);
}
int32_t DistributedSchedStub::NotifyContinuationResultFromRemoteInner(MessageParcel& data,
[[maybe_unused]] MessageParcel& reply)
{
@ -743,6 +764,40 @@ int32_t DistributedSchedStub::RegisterMissionListenerInner(MessageParcel& data,
PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
}
int32_t DistributedSchedStub::RegisterDSchedEventListenerInner(MessageParcel& data, MessageParcel& reply)
{
HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
string type = data.ReadString();
if (type.empty()) {
HILOGW("read type failed!");
return ERR_FLATTEN_OBJECT;
}
sptr<IRemoteObject> dSchedEventListener = data.ReadRemoteObject();
if (dSchedEventListener == nullptr) {
HILOGW("read IRemoteObject failed!");
return ERR_FLATTEN_OBJECT;
}
int32_t result = RegisterDSchedEventListener(type, dSchedEventListener);
PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
}
int32_t DistributedSchedStub::UnRegisterDSchedEventListenerInner(MessageParcel& data, MessageParcel& reply)
{
HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
string type = data.ReadString();
if (type.empty()) {
HILOGW("read type failed!");
return ERR_FLATTEN_OBJECT;
}
sptr<IRemoteObject> dSchedEventListener = data.ReadRemoteObject();
if (dSchedEventListener == nullptr) {
HILOGW("read IRemoteObject failed!");
return ERR_FLATTEN_OBJECT;
}
int32_t result = UnRegisterDSchedEventListener(type, dSchedEventListener);
PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
}
int32_t DistributedSchedStub::RegisterOnListenerInner(MessageParcel& data, MessageParcel& reply)
{
if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {