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 ¶mKey, 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
PublishScanStartEvent(const int & code,const std::string & data)119 bool WifiCommonEventHelper::PublishScanStartEvent(const int &code, const std::string &data)
120 {
121 std::vector<std::string> permissions;
122 permissions.push_back(COMMON_EVENT_GET_WIFI_INFO_PERMISSION);
123 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_SCAN_START, code, data, permissions);
124 }
125
PublishWifiSemiStateChangedEvent(const int & code,const std::string & data)126 bool WifiCommonEventHelper::PublishWifiSemiStateChangedEvent(const int &code, const std::string &data)
127 {
128 WIFI_LOGI("PublishWifiSemiStateChangedEvent code:%{public}d", code);
129 std::vector<std::string> permissions;
130 permissions.push_back(COMMON_EVENT_GET_WIFI_INFO_PERMISSION);
131 permissions.push_back(COMMON_EVENT_MANAGE_WIFI_CONNECTION_PERMISSION);
132 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_SEMI_STATE, code, data, permissions);
133 }
134
PublishRssiValueChangedEvent(const std::string & pramKey,int paramValue,const int & code,const std::string & data)135 bool WifiCommonEventHelper::PublishRssiValueChangedEvent(const std::string &pramKey, int paramValue,
136 const int &code, const std::string &data)
137 {
138 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_RSSI_VALUE, pramKey, paramValue, code, data);
139 }
140
PublishWiTasRssiValueChangedEvent(const int & code,const std::string & data)141 bool WifiCommonEventHelper::PublishWiTasRssiValueChangedEvent(const int &code, const std::string &data)
142 {
143 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WITAS_RSSI_VALUE, code, data);
144 }
145
PublishWifi2ConnStateChangedEvent(const int & code,const std::string & data)146 bool WifiCommonEventHelper::PublishWifi2ConnStateChangedEvent(const int &code, const std::string &data)
147 {
148 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI2_CONN_STATE, code, data);
149 }
150
PublishConnStateChangedEvent(const int & code,const std::string & data)151 bool WifiCommonEventHelper::PublishConnStateChangedEvent(const int &code, const std::string &data)
152 {
153 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_CONN_STATE, code, data);
154 }
155
PublishHotspotStateChangedEvent(const int & code,const std::string & data)156 bool WifiCommonEventHelper::PublishHotspotStateChangedEvent(const int &code, const std::string &data)
157 {
158 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_HOTSPOT_STATE, code, data);
159 }
160
PublishHotspotStateChangedEvent(const std::string & apModeKey,int apModeValue,const int & code,const std::string & data)161 bool WifiCommonEventHelper::PublishHotspotStateChangedEvent(const std::string &apModeKey, int apModeValue,
162 const int &code, const std::string &data)
163 {
164 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_HOTSPOT_STATE, apModeKey, apModeValue, code, data);
165 }
166
PublishApStaJoinEvent(const int & code,const std::string & data)167 bool WifiCommonEventHelper::PublishApStaJoinEvent(const int &code, const std::string &data)
168 {
169 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_AP_STA_JOIN, code, data);
170 }
171
PublishApStaLeaveEvent(const int & code,const std::string & data)172 bool WifiCommonEventHelper::PublishApStaLeaveEvent(const int &code, const std::string &data)
173 {
174 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_AP_STA_LEAVE, code, data);
175 }
176
PublishMPlinkEvent(const int & code,const std::string & data)177 bool WifiCommonEventHelper::PublishMPlinkEvent(const int &code, const std::string &data)
178 {
179 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_MPLINK_STATE, code, data);
180 }
181
PublishP2pStateChangedEvent(const int & code,const std::string & data)182 bool WifiCommonEventHelper::PublishP2pStateChangedEvent(const int &code, const std::string &data)
183 {
184 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_STATE_CHANGED, code, data);
185 }
186
PublishP2pConnStateEvent(const int & code,const std::string & data)187 bool WifiCommonEventHelper::PublishP2pConnStateEvent(const int &code, const std::string &data)
188 {
189 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_CONN_STATE, code, data);
190 }
191
PublishP2pPeersStateChangedEvent(const int & code,const std::string & data)192 bool WifiCommonEventHelper::PublishP2pPeersStateChangedEvent(const int &code, const std::string &data)
193 {
194 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_PEERS_STATE_CHANGED, code, data);
195 }
196
PublishP2pDicoveryStateChangedEvent(const int & code,const std::string & data)197 bool WifiCommonEventHelper::PublishP2pDicoveryStateChangedEvent(const int &code, const std::string &data)
198 {
199 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_PEERS_DISCOVERY_STATE_CHANGED, code, data);
200 }
201
PublishP2pCurrentDeviceStateChangedEvent(const int & code,const std::string & data)202 bool WifiCommonEventHelper::PublishP2pCurrentDeviceStateChangedEvent(const int &code, const std::string &data)
203 {
204 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_CURRENT_DEVICE_STATE_CHANGED, code, data);
205 }
206
PublishP2pGroupStateChangedEvent(const int & code,const std::string & data)207 bool WifiCommonEventHelper::PublishP2pGroupStateChangedEvent(const int &code, const std::string &data)
208 {
209 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_P2P_GROUP_STATE_CHANGED, code, data);
210 }
211
PublishNotAvailableDialog()212 bool WifiCommonEventHelper::PublishNotAvailableDialog()
213 {
214 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_NOT_AVAILABLE_DIALOG, CANCEL_DIAG, "NotAvailableDialog");
215 }
216
PublishSelfcureStateChangedEvent(const int & pid,const int & code,bool isSelfCureOnGoing)217 bool WifiCommonEventHelper::PublishSelfcureStateChangedEvent(const int &pid, const int &code, bool isSelfCureOnGoing)
218 {
219 #ifndef OHOS_ARCH_LITE
220 Want want;
221 want.SetAction(COMMON_EVENT_WIFI_SELF_CURE_STATE_CHANGED);
222 want.SetParam("pid", pid);
223 want.SetParam("selfcureType", code);
224 want.SetParam("isSelfCureOnGoing", isSelfCureOnGoing);
225 CommonEventData commonData;
226 commonData.SetWant(want);
227 std::vector<std::string> permissions;
228 permissions.push_back(COMMON_EVENT_GET_WIFI_INFO_PERMISSION);
229 CommonEventPublishInfo publishInfo;
230 publishInfo.SetSubscriberPermissions(permissions);
231 if (!CommonEventManager::PublishCommonEvent(commonData, publishInfo)) {
232 WIFI_LOGE("failed to publish SelfcureStateChanged Event");
233 return false;
234 }
235 #endif
236 return true;
237 }
238
PublishVoWifiSignalDetectInterruptEvent(const int & code,const std::string & data)239 bool WifiCommonEventHelper::PublishVoWifiSignalDetectInterruptEvent(const int &code, const std::string &data)
240 {
241 std::vector<std::string> permissions;
242 permissions.push_back(COMMON_EVENT_GET_WIFI_CONFIG_PERMISSION);
243 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_VOWIFI_SIGNAL_DETECT_INTERRUPT, code, data, permissions);
244 }
245
PublishInternetAccessChangedEvent(const int & code,const std::string & data)246 bool WifiCommonEventHelper::PublishInternetAccessChangedEvent(const int &code, const std::string &data)
247 {
248 WIFI_LOGI("PublishInternetAccessChangedEvent code:%{public}d", code);
249 std::vector<std::string> permissions;
250 permissions.push_back(COMMON_EVENT_MANAGE_WIFI_CONNECTION_PERMISSION);
251 return WifiCommonEventHelper::PublishEvent(COMMON_EVENT_WIFI_INTERNET_ACCESS_CHANGE, code, data, permissions);
252 }
253 } // namespace Wifi
254 } // namespace OHOS