1 /*
2  * Copyright (C) 2021-2022 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 "wifi_common_event_helper.h"
17 #ifndef OHOS_ARCH_LITE
18 #include "common_event_manager.h"
19 #include "common_event.h"
20 #include "common_event_data.h"
21 #include "common_event_subscriber.h"
22 #endif
23 #include "wifi_logger.h"
24 
25 #ifndef OHOS_ARCH_LITE
26 using namespace OHOS::EventFwk;
27 #endif
28 DEFINE_WIFILOG_LABEL("WifiCommonEventHelper");
29 namespace OHOS {
30 namespace Wifi {
PublishEvent(const std::string & eventAction,const int & code,const std::string & data,const std::vector<std::string> & permissions)31 bool WifiCommonEventHelper::PublishEvent(const std::string &eventAction, const int &code, const std::string &data,
32     const std::vector<std::string> &permissions)
33 {
34     WIFI_LOGD("publish event[%{public}s], code:%{public}d", eventAction.c_str(), code);
35 #ifndef OHOS_ARCH_LITE
36     Want want;
37     want.SetAction(eventAction);
38     CommonEventData commonData;
39     commonData.SetWant(want);
40     commonData.SetCode(code);
41     commonData.SetData(data);
42     if (permissions.size() > 0) {
43         CommonEventPublishInfo publishInfo;
44         publishInfo.SetSubscriberPermissions(permissions);
45         if (!CommonEventManager::PublishCommonEvent(commonData, publishInfo)) {
46             WIFI_LOGE("failed to publish event[%{public}s], code:%{public}d", eventAction.c_str(), code);
47             return false;
48         }
49         return true;
50     }
51     if (!CommonEventManager::PublishCommonEvent(commonData)) {
52         WIFI_LOGE("failed to publish event[%{public}s], code:%{public}d", eventAction.c_str(), code);
53         return false;
54     }
55 #endif
56     return true;
57 }
58 
PublishEvent(const std::string & eventAction,const int & code,const std::string & data)59 bool WifiCommonEventHelper::PublishEvent(const std::string &eventAction, const int &code, const std::string &data)
60 {
61     WIFI_LOGD("publish event[%{public}s], code:%{public}d", eventAction.c_str(), code);
62 #ifndef OHOS_ARCH_LITE
63     Want want;
64     want.SetAction(eventAction);
65     CommonEventData commonData;
66     commonData.SetWant(want);
67     commonData.SetCode(code);
68     commonData.SetData(data);
69     if (!CommonEventManager::PublishCommonEvent(commonData)) {
70         WIFI_LOGE("failed to publish event[%{public}s], code:%{public}d", eventAction.c_str(), code);
71         return false;
72     }
73 #endif
74     return true;
75 }
76 
77 template <typename T>
PublishEvent(const std::string & eventAction,const std::string & paramKey,T paramValue,const int & code,const std::string & data)78 bool WifiCommonEventHelper::PublishEvent(const std::string &eventAction, const std::string &paramKey, T paramValue,
79     const int &code, const std::string &data)
80 {
81     WIFI_LOGD("publish event[%{public}s], paramKey:%{public}s,code:%{public}d",
82         eventAction.c_str(), paramKey.c_str(), code);
83 #ifndef OHOS_ARCH_LITE
84     Want want;
85     want.SetAction(eventAction);
86     want.SetParam(paramKey, paramValue);
87     CommonEventData commonData;
88     commonData.SetWant(want);
89     commonData.SetCode(code);
90     commonData.SetData(data);
91     if (!CommonEventManager::PublishCommonEvent(commonData)) {
92         WIFI_LOGE("failed to publish event:%{public}s, code:%{public}d", eventAction.c_str(), code);
93         return false;
94     }
95 #endif
96     return true;
97 }
98 
PublishWifi2PowerStateChangeEvent(const int & code,const std::string & data)99 bool WifiCommonEventHelper::PublishWifi2PowerStateChangeEvent(const int &code, const std::string &data)
100 {
101     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI2_POWER_STATE, code, data);
102 }
103 
PublishPowerStateChangeEvent(const int & code,const std::string & data)104 bool WifiCommonEventHelper::PublishPowerStateChangeEvent(const int &code, const std::string &data)
105 {
106     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_POWER_STATE, code, data);
107 }
108 
PublishScanFinishedEvent(const int & code,const std::string & data)109 bool WifiCommonEventHelper::PublishScanFinishedEvent(const int &code, const std::string &data)
110 {
111     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_SCAN_FINISHED, code, data);
112 }
113 
PublishScanStateChangedEvent(const int & code,const std::string & data)114 bool WifiCommonEventHelper::PublishScanStateChangedEvent(const int &code, const std::string &data)
115 {
116     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_SCAN_STATE, code, data);
117 }
118 
PublishWifiSemiStateChangedEvent(const int & code,const std::string & data)119 bool WifiCommonEventHelper::PublishWifiSemiStateChangedEvent(const int &code, const std::string &data)
120 {
121     WIFI_LOGI("PublishWifiSemiStateChangedEvent code:%{public}d", code);
122     std::vector<std::string> permissions;
123     permissions.push_back(COMMON_EVENT_GET_WIFI_INFO_PERMISSION);
124     permissions.push_back(COMMON_EVENT_MANAGE_WIFI_CONNECTION_PERMISSION);
125     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_SEMI_STATE, code, data, permissions);
126 }
127 
PublishRssiValueChangedEvent(const std::string & pramKey,int paramValue,const int & code,const std::string & data)128 bool WifiCommonEventHelper::PublishRssiValueChangedEvent(const std::string &pramKey, int paramValue,
129     const int &code, const std::string &data)
130 {
131     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_RSSI_VALUE, pramKey, paramValue, code, data);
132 }
133 
PublishWiTasRssiValueChangedEvent(const int & code,const std::string & data)134 bool WifiCommonEventHelper::PublishWiTasRssiValueChangedEvent(const int &code, const std::string &data)
135 {
136     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WITAS_RSSI_VALUE, code, data);
137 }
138 
PublishWifi2ConnStateChangedEvent(const int & code,const std::string & data)139 bool WifiCommonEventHelper::PublishWifi2ConnStateChangedEvent(const int &code, const std::string &data)
140 {
141     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI2_CONN_STATE, code, data);
142 }
143 
PublishConnStateChangedEvent(const int & code,const std::string & data)144 bool WifiCommonEventHelper::PublishConnStateChangedEvent(const int &code, const std::string &data)
145 {
146     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_CONN_STATE, code, data);
147 }
148 
PublishHotspotStateChangedEvent(const int & code,const std::string & data)149 bool WifiCommonEventHelper::PublishHotspotStateChangedEvent(const int &code, const std::string &data)
150 {
151     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_HOTSPOT_STATE, code, data);
152 }
153 
PublishApStaJoinEvent(const int & code,const std::string & data)154 bool WifiCommonEventHelper::PublishApStaJoinEvent(const int &code, const std::string &data)
155 {
156     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_AP_STA_JOIN, code, data);
157 }
158 
PublishApStaLeaveEvent(const int & code,const std::string & data)159 bool WifiCommonEventHelper::PublishApStaLeaveEvent(const int &code, const std::string &data)
160 {
161     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_AP_STA_LEAVE, code, data);
162 }
163 
PublishMPlinkEvent(const int & code,const std::string & data)164 bool WifiCommonEventHelper::PublishMPlinkEvent(const int &code, const std::string &data)
165 {
166     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_MPLINK_STATE, code, data);
167 }
168 
PublishP2pStateChangedEvent(const int & code,const std::string & data)169 bool WifiCommonEventHelper::PublishP2pStateChangedEvent(const int &code, const std::string &data)
170 {
171     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_STATE_CHANGED, code, data);
172 }
173 
PublishP2pConnStateEvent(const int & code,const std::string & data)174 bool WifiCommonEventHelper::PublishP2pConnStateEvent(const int &code, const std::string &data)
175 {
176     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_CONN_STATE, code, data);
177 }
178 
PublishP2pPeersStateChangedEvent(const int & code,const std::string & data)179 bool WifiCommonEventHelper::PublishP2pPeersStateChangedEvent(const int &code, const std::string &data)
180 {
181     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_PEERS_STATE_CHANGED, code, data);
182 }
183 
PublishP2pDicoveryStateChangedEvent(const int & code,const std::string & data)184 bool WifiCommonEventHelper::PublishP2pDicoveryStateChangedEvent(const int &code, const std::string &data)
185 {
186     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_PEERS_DISCOVERY_STATE_CHANGED, code, data);
187 }
188 
PublishP2pCurrentDeviceStateChangedEvent(const int & code,const std::string & data)189 bool WifiCommonEventHelper::PublishP2pCurrentDeviceStateChangedEvent(const int &code, const std::string &data)
190 {
191     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_CURRENT_DEVICE_STATE_CHANGED, code, data);
192 }
193 
PublishP2pGroupStateChangedEvent(const int & code,const std::string & data)194 bool WifiCommonEventHelper::PublishP2pGroupStateChangedEvent(const int &code, const std::string &data)
195 {
196     return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_GROUP_STATE_CHANGED, code, data);
197 }
198 
PublishSelfcureStateChangedEvent(const int & pid,const int & code,bool isSelfCureOnGoing)199 bool WifiCommonEventHelper::PublishSelfcureStateChangedEvent(const int &pid, const int &code, bool isSelfCureOnGoing)
200 {
201 #ifndef OHOS_ARCH_LITE
202     Want want;
203     want.SetAction(COMMON_EVENT_WIFI_SELF_CURE_STATE_CHANGED);
204     want.SetParam("pid", pid);
205     want.SetParam("selfcureType", code);
206     want.SetParam("isSelfCureOnGoing", isSelfCureOnGoing);
207     CommonEventData commonData;
208     commonData.SetWant(want);
209     std::vector<std::string> permissions;
210     permissions.push_back(COMMON_EVENT_GET_WIFI_INFO_PERMISSION);
211     CommonEventPublishInfo publishInfo;
212     publishInfo.SetSubscriberPermissions(permissions);
213     if (!CommonEventManager::PublishCommonEvent(commonData, publishInfo)) {
214         WIFI_LOGE("failed to publish SelfcureStateChanged Event");
215         return false;
216     }
217 #endif
218     return true;
219 }
220 }  // namespace Wifi
221 }  // namespace OHOS