Interface for submitting and receiving broadcasts

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

Signed-off-by: m30043719 <maxiaodong25@huawei.com>
This commit is contained in:
maxiaodong (D) 2023-05-12 15:52:47 +08:00 committed by m30043719
parent 12e8c47055
commit a5f8edaf08
9 changed files with 430 additions and 2 deletions

View File

@ -1,4 +1,4 @@
# Copyright (c) 2022 Huawei Device Co., Ltd.
# Copyright (c) 2021-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
@ -13,11 +13,13 @@
declare_args() {
dms_path = "//foundation/ability/dmsfwk"
communication_path = "//foundation/communication"
dmsfwk_standard_form_share = true
dmsfwk_mission_manager = false
efficiency_manager_service_enable = false
dmsfwk_report_memmgr = false
dmsfwk_report_memmgr_plugins = false
distributed_sched_softbus_adapter_common = true
if (defined(global_parts_info)) {
if (defined(global_parts_info.multimedia_multimedia_image_framework)) {
dmsfwk_mission_manager = true

View File

@ -23,7 +23,11 @@ group("unittest") {
config("distributed_sched_config") {
visibility = [ ":*" ]
visibility += [ "./test/*" ]
include_dirs = [ "include" ]
include_dirs = [
"include",
"${communication_path}/dsoftbus/dsoftbus_enhance/interfaces/kits/broadcast",
"${communication_path}/dsoftbus/interfaces/kits/common",
]
defines = []
if (dmsfwk_mission_manager) {
defines += [ "SUPPORT_DISTRIBUTED_MISSION_MANAGER" ]
@ -69,11 +73,19 @@ ohos_shared_library("distributedschedsvr") {
"src/dtbschedmgr_device_info_storage.cpp",
]
if (!distributed_sched_softbus_adapter_common) {
sources += [ "src/softbus_adapter/softbus_adapter.cpp" ]
} else {
sources += [ "src/softbus_adapter/softbus_adapter_common.cpp" ]
}
configs = [
":distributed_sched_config",
"//foundation/ability/dmsfwk/services/dtbschedmgr/test/resource:coverage_flags",
]
deps = [ "${communication_path}/dsoftbus/sdk:softbus_client" ]
external_deps = [
"ability_base:want",
"ability_base:zuri",

View File

@ -0,0 +1,44 @@
/*
* 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_DMS_SOFTBUSADAPTER_H
#define OHOS_DISTRIBUTED_DMS_SOFTBUSADAPTER_H
#include <string>
#include "single_instance.h"
#include "softbus_adapter_listener.h"
namespace OHOS {
namespace DistributedSchedule {
class SoftbusAdapter {
public:
DECLARE_SINGLE_INSTANCE_BASE(SoftbusAdapter);
public:
int32_t SendSoftbusEvent(uint8_t* sendData, uint32_t sendDataLen);
int32_t StopSoftbusEvent();
int32_t RegisterSoftbusEventListener(std::shared_ptr<SoftbusAdapterListener>& listener);
int32_t UnregisterSoftbusEventListener(std::shared_ptr<SoftbusAdapterListener>& listener);
void OnBroadCastRecv(std::string& networkId, uint8_t* data, uint32_t dataLen);
public:
std::shared_ptr<SoftbusAdapterListener> softbusAdapterListener_;
private:
SoftbusAdapter() {}
std::string pkgName_ = "ohos.ability.distributedsched";
};
} // namespace DistributedSchedule
} // namespace OHOS
#endif /* OHOS_DISTRIBUTED_DMS_SOFTBUSADAPTER_H */

View File

@ -0,0 +1,35 @@
/*
* 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_SOFTBUS_ADAPTER_LISTENER_H
#define OHOS_DISTRIBUTED_SOFTBUS_ADAPTER_LISTENER_H
#include <string>
namespace OHOS {
namespace DistributedSchedule {
/**
* @class SoftbusAdapterListener
* SoftbusAdapterListener is used to notify broadcast.
*/
class SoftbusAdapterListener {
public:
virtual ~SoftbusAdapterListener() = default;
virtual void OnDataRecv(std::string& senderNetworkId, uint8_t* payload, uint32_t dataLen) = 0;
};
} // namespace DistributedSchedule
} // namespace OHOS
#endif // OHOS_DISTRIBUTED_SOFTBUS_ADAPTER_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.
*/
#include "softbus_adapter/softbus_adapter.h"
#include "broadcast.h"
#include "dtbschedmgr_log.h"
#include "softbus_error_code.h"
namespace OHOS {
namespace DistributedSchedule {
namespace {
const std::string TAG = "SoftbusAdapter";
}
IMPLEMENT_SINGLE_INSTANCE(SoftbusAdapter);
int32_t SoftbusAdapter::SendSoftbusEvent(uint8_t* sendData, uint32_t sendDataLen)
{
HILOGI("SendSoftbusEvent pkgName: %{public}s.", pkgName_.c_str());
EventData eventData;
eventData.event = FOREGROUND_APP;
eventData.freq = EVENT_HIGH_FREQ;
eventData.data = sendData;
eventData.dataLen = sendDataLen;
int32_t ret = SendEvent(pkgName_.c_str(), BROADCAST_TARGET_AREA, &eventData);
if (ret != SOFTBUS_OK) {
HILOGE("SendEvent failed, ret:%{public}d.", ret);
return ret;
}
return SOFTBUS_OK;
}
int32_t SoftbusAdapter::StopSoftbusEvent()
{
HILOGI("StopSoftbusEvent pkgName: %{public}s.", pkgName_.c_str());
int32_t ret = StopEvent(pkgName_.c_str(), BROADCAST_TARGET_AREA, FOREGROUND_APP);
if (ret != SOFTBUS_OK) {
HILOGE("StopEvent failed, ret:%{public}d.", ret);
return ret;
}
return SOFTBUS_OK;
}
static void EventListenerReceived(const EventNotify *eventNotify)
{
HILOG_INFO("%{public}s called.", __func__);
std::string networkId(eventNotify->senderNetworkId);
SoftbusAdapter::GetInstance().OnBroadCastRecv(networkId, eventNotify->data, eventNotify->dataLen);
}
void SoftbusAdapter::OnBroadCastRecv(std::string& networkId, uint8_t* data, uint32_t dataLen)
{
if (softbusAdapterListener_ != nullptr) {
softbusAdapterListener_->OnDataRecv(networkId, data, dataLen);
} else {
HILOGE("softbusAdapterListener_ is nullptr");
}
}
int32_t SoftbusAdapter::RegisterSoftbusEventListener(std::shared_ptr<SoftbusAdapterListener>& listener)
{
if (listener == nullptr) {
HILOGE("Registering listener failed");
return -1;
}
softbusAdapterListener_ = listener;
EventListener eventListener;
eventListener.event = FOREGROUND_APP;
eventListener.freq = EVENT_HIGH_FREQ;
eventListener.OnEventReceived = EventListenerReceived;
HILOGI("RegisterSoftbusEventListener pkgName: %s.", pkgName_.c_str());
int32_t ret = RegisterEventListener(pkgName_.c_str(), &eventListener);
if (ret != SOFTBUS_OK) {
HILOGE("RegisterSoftbusEventListener failed, ret:%d.", ret);
return ret;
}
return SOFTBUS_OK;
}
int32_t SoftbusAdapter::UnregisterSoftbusEventListener(std::shared_ptr<SoftbusAdapterListener>& listener)
{
if (listener == nullptr) {
HILOGE("Unregistering listener failed");
return -1;
}
softbusAdapterListener_ = listener;
EventListener eventListener;
eventListener.event = FOREGROUND_APP;
eventListener.freq = EVENT_HIGH_FREQ;
eventListener.OnEventReceived = EventListenerReceived;
HILOGI("UnregisterSoftbusEventListener pkgName: %s.", pkgName_.c_str());
int32_t ret = UnregisterEventListener(pkgName_.c_str(), &eventListener);
if (ret != SOFTBUS_OK) {
HILOGE("UnregisterSoftbusEventListener failed, ret:%d.", ret);
return ret;
}
return SOFTBUS_OK;
}
} // namespace DistributedSchedule
} // namespace OHOS

View File

@ -0,0 +1,51 @@
/*
* 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 "softbus_adapter/softbus_adapter.h"
namespace OHOS {
namespace DistributedSchedule {
namespace {
const std::string TAG = "SoftbusAdapter";
}
IMPLEMENT_SINGLE_INSTANCE(SoftbusAdapter);
int32_t SoftbusAdapter::SendSoftbusEvent(uint8_t* sendData, uint32_t sendDataLen)
{
return 0;
}
int32_t SoftbusAdapter::StopSoftbusEvent()
{
return 0;
}
void SoftbusAdapter::OnBroadCastRecv(std::string& networkId, uint8_t* data, uint32_t dataLen)
{
return;
}
int32_t SoftbusAdapter::RegisterSoftbusEventListener(std::shared_ptr<SoftbusAdapterListener>& listener)
{
return 0;
}
int32_t SoftbusAdapter::UnregisterSoftbusEventListener(std::shared_ptr<SoftbusAdapterListener>& listener)
{
return 0;
}
} // namespace DistributedSchedule
} // namespace OHOS

View File

@ -116,6 +116,12 @@ dtbschedmgr_sources = [
"unittest/distributed_sched_util.cpp",
]
if (!distributed_sched_softbus_adapter_common) {
dtbschedmgr_sources += [ "${distributed_service}/dtbschedmgr/src/softbus_adapter/softbus_adapter.cpp" ]
} else {
dtbschedmgr_sources += [ "${distributed_service}/dtbschedmgr/src/softbus_adapter/softbus_adapter_common.cpp" ]
}
if (dmsfwk_mission_manager) {
dtbschedmgr_sources += [
"${distributed_service}/dtbschedmgr/src/mission/distributed_data_change_listener.cpp",
@ -340,6 +346,28 @@ ohos_unittest("bundlemanagerinternaltest") {
subsystem_name = "ability"
}
ohos_unittest("softbusadaptertest") {
module_out_path = module_output_path
sources = [ "unittest/softbus_adapter/softbus_adapter_test.cpp" ]
sources += dtbschedmgr_sources
configs = [
":test_config",
"${dms_path}/services/dtbschedmgr/test/resource:coverage_flags",
]
configs += dsched_configs
deps = [
"${distributed_service}/dtbschedmgr:distributedschedsvr",
"${dms_path}/test/sceneProject/unittest/bundle_dependencies/bmsThirdBundle:bmsThirdBundle",
"${dms_path}/test/sceneProject/unittest/ohos_test:copy_ohos_test",
]
if (is_standard_system) {
external_deps = dsched_external_deps
public_deps = dsched_public_deps
}
part_name = "dmsfwk"
subsystem_name = "ability"
}
ohos_unittest("hisyseventreporttest") {
module_out_path = module_output_path
sources = [ "unittest/dms_hisysevent_report_test.cpp" ]

View File

@ -0,0 +1,103 @@
/*
* Copyright (c) 2021-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 "softbus_adapter_test.h"
#include "softbus_error_code.h"
#include "test_log.h"
using namespace testing;
using namespace testing::ext;
namespace OHOS {
namespace DistributedSchedule {
void SoftbusAdapterTest::SetUpTestCase()
{
DTEST_LOG << "SoftbusAdapterTest::SetUpTestCase" << std::endl;
}
void SoftbusAdapterTest::TearDownTestCase()
{
DTEST_LOG << "SoftbusAdapterTest::TearDownTestCase" << std::endl;
}
void SoftbusAdapterTest::TearDown()
{
DTEST_LOG << "SoftbusAdapterTest::TearDown" << std::endl;
}
void SoftbusAdapterTest::SetUp()
{
DTEST_LOG << "SoftbusAdapterTest::SetUp" << std::endl;
}
/**
* @tc.name: SendSoftbusEvent_001
* @tc.desc: call SendSoftbusEvent from distributedsched
* @tc.type: FUNC
*/
HWTEST_F(SoftbusAdapterTest, SendSoftbusEvent_001, TestSize.Level3)
{
DTEST_LOG << "SoftbusAdapterTest SendSoftbusEvent_001 begin" << std::endl;
uint8_t* sendData = nullptr;
uint32_t sendDataLen = 1;
uint32_t result = SoftbusAdapter::GetInstance().SendSoftbusEvent(sendData, sendDataLen);
EXPECT_NE(result, SOFTBUS_OK);
DTEST_LOG << "SoftbusAdapterTest SendSoftbusEvent_001 end" << std::endl;
}
/**
* @tc.name: StopSoftbusEvent_001
* @tc.desc: call StopSoftbusEvent from distributedsched
* @tc.type: FUNC
*/
HWTEST_F(SoftbusAdapterTest, StopSoftbusEvent_001, TestSize.Level3)
{
DTEST_LOG << "SoftbusAdapterTest StopSoftbusEvent_001 begin" << std::endl;
uint32_t result = SoftbusAdapter::GetInstance().StopSoftbusEvent();
EXPECT_NE(result, SOFTBUS_OK);
DTEST_LOG << "SoftbusAdapterTest StopSoftbusEvent_001 end" << std::endl;
}
/**
* @tc.name: RegisterSoftbusEventListener_001
* @tc.desc: call RegisterSoftbusEventListener from distributedsched
* @tc.type: FUNC
*/
HWTEST_F(SoftbusAdapterTest, RegisterSoftbusEventListener_001, TestSize.Level3)
{
DTEST_LOG << "SoftbusAdapterTest RegisterSoftbusEventListener_001 begin" << std::endl;
//SoftbusAdapterListener *listener = nullptr;
std::shared_ptr<SubSoftbusAdapterListener> listener = std::make_shared<SubSoftbusAdapterListener>();
uint32_t result = SoftbusAdapter::GetInstance().RegisterSoftbusEventListener(listener);
EXPECT_NE(result, SOFTBUS_OK);
DTEST_LOG << "SoftbusAdapterTest RegisterSoftbusEventListener_001 end" << std::endl;
}
/**
* @tc.name: UnregisterSoftbusEventListener_001
* @tc.desc: call UnregisterSoftbusEventListener from distributedsched
* @tc.type: FUNC
*/
HWTEST_F(SoftbusAdapterTest, UnregisterSoftbusEventListener_001, TestSize.Level3)
{
DTEST_LOG << "SoftbusAdapterTest UnregisterSoftbusEventListener_001 begin" << std::endl;
//SoftbusAdapterListener *listener = nullptr;
std::shared_ptr<SubSoftbusAdapterListener> listener = std::make_shared<SubSoftbusAdapterListener>();
uint32_t result = SoftbusAdapter::GetInstance().UnregisterSoftbusEventListener(listener);
EXPECT_NE(result, SOFTBUS_OK);
DTEST_LOG << "SoftbusAdapterTest UnregisterSoftbusEventListener_001 end" << std::endl;
}
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2021-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 SOFTBUS_ADAPTER_H
#define SOFTBUS_ADAPTER_H
#include "gtest/gtest.h"
#define private public
#include "softbus_adapter/softbus_adapter.h"
#undef private
namespace OHOS {
namespace DistributedSchedule {
class SoftbusAdapterTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
void SetUp();
void TearDown();
};
class SubSoftbusAdapterListener : public SoftbusAdapterListener {
public:
void OnDataRecv(std::string& senderNetworkId, uint8_t* payload, uint32_t dataLen) {};
};
} // namespace DistributedSchedule
} // namespace OHOS
#endif // SOFTBUS_ADAPTER_H