• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #include "softbus_adapter/softbus_adapter.h"
17 #include "broadcast.h"
18 #include "dtbschedmgr_log.h"
19 #include "softbus_error_code.h"
20 
21 namespace OHOS {
22 namespace DistributedSchedule {
23 namespace {
24 const std::string TAG = "SoftbusAdapter";
25 }
26 
27 IMPLEMENT_SINGLE_INSTANCE(SoftbusAdapter);
28 
SendSoftbusEvent(uint8_t * sendData,uint32_t sendDataLen)29 int32_t SoftbusAdapter::SendSoftbusEvent(uint8_t* sendData, uint32_t sendDataLen)
30 {
31     HILOGI("SendSoftbusEvent pkgName: %{public}s.", pkgName_.c_str());
32     EventData eventData;
33     eventData.event = FOREGROUND_APP;
34     eventData.freq = EVENT_HIGH_FREQ;
35     eventData.data = sendData;
36     eventData.dataLen = sendDataLen;
37     int32_t ret = SendEvent(pkgName_.c_str(), BROADCAST_TARGET_AREA, &eventData);
38     if (ret != SOFTBUS_OK) {
39         HILOGE("SendEvent failed, ret:%{public}d.", ret);
40         return ret;
41     }
42     return SOFTBUS_OK;
43 }
44 
StopSoftbusEvent()45 int32_t SoftbusAdapter::StopSoftbusEvent()
46 {
47     HILOGI("StopSoftbusEvent pkgName: %{public}s.", pkgName_.c_str());
48     int32_t ret = StopEvent(pkgName_.c_str(), BROADCAST_TARGET_AREA, FOREGROUND_APP);
49     if (ret != SOFTBUS_OK) {
50         HILOGE("StopEvent failed, ret:%{public}d.", ret);
51         return ret;
52     }
53     return SOFTBUS_OK;
54 }
55 
EventListenerReceived(const EventNotify * eventNotify)56 static void EventListenerReceived(const EventNotify *eventNotify)
57 {
58     HILOGI("%{public}s called.", __func__);
59     std::string networkId(eventNotify->senderNetworkId);
60     SoftbusAdapter::GetInstance().OnBroadCastRecv(networkId, eventNotify->data, eventNotify->dataLen);
61 }
62 
OnBroadCastRecv(std::string & networkId,uint8_t * data,uint32_t dataLen)63 void SoftbusAdapter::OnBroadCastRecv(std::string& networkId, uint8_t* data, uint32_t dataLen)
64 {
65     if (softbusAdapterListener_ != nullptr) {
66         softbusAdapterListener_->OnDataRecv(networkId, data, dataLen);
67     } else {
68         HILOGE("softbusAdapterListener_ is nullptr");
69     }
70 }
71 
RegisterSoftbusEventListener(const std::shared_ptr<SoftbusAdapterListener> & listener)72 int32_t SoftbusAdapter::RegisterSoftbusEventListener(const std::shared_ptr<SoftbusAdapterListener>& listener)
73 {
74     if (listener == nullptr) {
75         HILOGE("Registering listener failed");
76         return SOFTBUS_INVALID_PARAM;
77     }
78     softbusAdapterListener_ = listener;
79     EventListener eventListener;
80     eventListener.event = FOREGROUND_APP;
81     eventListener.freq = EVENT_MID_FREQ;
82     eventListener.OnEventReceived = EventListenerReceived;
83     HILOGI("RegisterSoftbusEventListener pkgName: %s.", pkgName_.c_str());
84     int32_t ret = RegisterEventListener(pkgName_.c_str(), &eventListener);
85     if (ret != SOFTBUS_OK) {
86         HILOGE("RegisterSoftbusEventListener failed, ret:%d.", ret);
87         return ret;
88     }
89     return SOFTBUS_OK;
90 }
91 
UnregisterSoftbusEventListener(const std::shared_ptr<SoftbusAdapterListener> & listener)92 int32_t SoftbusAdapter::UnregisterSoftbusEventListener(const std::shared_ptr<SoftbusAdapterListener>& listener)
93 {
94     if (listener == nullptr) {
95         HILOGE("Unregistering listener failed");
96         return SOFTBUS_INVALID_PARAM;
97     }
98     softbusAdapterListener_ = listener;
99     EventListener eventListener;
100     eventListener.event = FOREGROUND_APP;
101     eventListener.freq = EVENT_MID_FREQ;
102     eventListener.OnEventReceived = EventListenerReceived;
103     HILOGI("UnregisterSoftbusEventListener pkgName: %s.", pkgName_.c_str());
104     int32_t ret = UnregisterEventListener(pkgName_.c_str(), &eventListener);
105     if (ret != SOFTBUS_OK) {
106         HILOGE("UnregisterSoftbusEventListener failed, ret:%d.", ret);
107         return ret;
108     }
109     return SOFTBUS_OK;
110 }
111 } // namespace DistributedSchedule
112 } // namespace OHOS
113