1/*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include "broadcast_manager.h"
16
17#include "common_event_manager.h"
18#include "common_event_support.h"
19#include "want.h"
20
21#include "net_mgr_log_wrapper.h"
22
23namespace OHOS {
24namespace NetManagerStandard {
25BroadcastManager::BroadcastManager() {}
26
27BroadcastManager::~BroadcastManager() {}
28
29bool BroadcastManager::SendBroadcast(const BroadcastInfo &info, const std::map<std::string, bool> &param)
30{
31    return SendBroadcastEx(info, param);
32}
33
34bool BroadcastManager::SendBroadcast(const BroadcastInfo &info, const std::map<std::string, int32_t> &param)
35{
36    return SendBroadcastEx(info, param);
37}
38
39bool BroadcastManager::SendBroadcast(const BroadcastInfo &info, const std::map<std::string, int64_t> &param)
40{
41    return SendBroadcastEx(info, param);
42}
43
44bool BroadcastManager::SendBroadcast(const BroadcastInfo &info, const std::map<std::string, std::string> &param)
45{
46    return SendBroadcastEx(info, param);
47}
48
49template <typename T>
50bool BroadcastManager::SendBroadcastEx(const BroadcastInfo &info, const std::map<std::string, T> &param)
51{
52    if (info.action.empty()) {
53        NETMGR_LOG_E("The parameter of action is empty");
54        return false;
55    }
56    NETMGR_LOG_I("Net Send broadcast: %{public}s", info.action.c_str());
57    AAFwk::Want want;
58    want.SetAction(info.action);
59    for (const auto &x : param) {
60        want.SetParam(x.first, x.second);
61    }
62
63    EventFwk::CommonEventData eventData;
64    eventData.SetWant(want);
65    eventData.SetCode(info.code);
66    eventData.SetData(info.data);
67    EventFwk::CommonEventPublishInfo publishInfo;
68    publishInfo.SetOrdered(info.ordered);
69    if (!info.permission.empty()) {
70        publishInfo.SetSubscriberPermissions({info.permission});
71    }
72
73    bool publishResult = EventFwk::CommonEventManager::PublishCommonEvent(eventData, publishInfo, nullptr);
74    if (publishResult) {
75        NETMGR_LOG_D("Send broadcast is successfull");
76    } else {
77        NETMGR_LOG_D("Send broadcast is failed");
78    }
79
80    return publishResult;
81}
82} // namespace NetManagerStandard
83} // namespace OHOS
84