1 /*
2 * Copyright (c) 2021 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 "lnn_event_monitor_impl.h"
17
18 #include <securec.h>
19
20 #include "bus_center_event.h"
21 #include "common_event_data.h"
22 #include "common_event_manager.h"
23 #include "common_event_subscriber.h"
24 #include "common_event_support.h"
25 #include "lnn_async_callback_utils.h"
26 #include "lnn_log.h"
27 #include "want.h"
28 #include "wifi_msg.h"
29 #include "softbus_adapter_mem.h"
30 #include "softbus_errcode.h"
31 #include "wifi_event.h"
32 #include "wifi_ap_msg.h"
33
34 static const int32_t DELAY_LEN = 1000;
35 static const int32_t RETRY_MAX = 20;
36
37 namespace OHOS {
38 namespace EventFwk {
39 class WifiServiceMonitor : public CommonEventSubscriber {
40 public:
41 explicit WifiServiceMonitor(const CommonEventSubscribeInfo &subscriberInfo);
~WifiServiceMonitor()42 virtual ~WifiServiceMonitor() {}
43 virtual void OnReceiveEvent(const CommonEventData &data);
44 };
45
WifiServiceMonitor(const CommonEventSubscribeInfo & subscriberInfo)46 WifiServiceMonitor::WifiServiceMonitor(const CommonEventSubscribeInfo &subscriberInfo)
47 :CommonEventSubscriber(subscriberInfo)
48 {
49 }
50
SetSoftBusWifiConnState(const int code,SoftBusWifiState * state)51 static void SetSoftBusWifiConnState(const int code, SoftBusWifiState *state)
52 {
53 switch (code) {
54 case int(OHOS::Wifi::ConnState::OBTAINING_IPADDR):
55 *state = SOFTBUS_WIFI_OBTAINING_IPADDR;
56 break;
57 case int(OHOS::Wifi::ConnState::CONNECTED):
58 *state = SOFTBUS_WIFI_CONNECTED;
59 break;
60 case int(OHOS::Wifi::ConnState::DISCONNECTED):
61 *state = SOFTBUS_WIFI_DISCONNECTED;
62 break;
63 default: {
64 break;
65 }
66 }
67 }
68
SetSoftBusWifiUseState(const int code,SoftBusWifiState * state)69 static void SetSoftBusWifiUseState(const int code, SoftBusWifiState *state)
70 {
71 switch (code) {
72 case int(OHOS::Wifi::WifiState::DISABLED):
73 *state = SOFTBUS_WIFI_DISABLED;
74 break;
75 case int(OHOS::Wifi::WifiState::ENABLED):
76 *state = SOFTBUS_WIFI_ENABLED;
77 break;
78 default: {
79 break;
80 }
81 }
82 }
83
SetSoftBusWifiHotSpotState(const int code,SoftBusWifiState * state)84 static void SetSoftBusWifiHotSpotState(const int code, SoftBusWifiState *state)
85 {
86 switch (code) {
87 case int(OHOS::Wifi::ApState::AP_STATE_STARTED):
88 *state = SOFTBUS_AP_ENABLED;
89 break;
90 case int(OHOS::Wifi::ApState::AP_STATE_CLOSED):
91 *state = SOFTBUS_AP_DISABLED;
92 break;
93 default: {
94 break;
95 }
96 }
97 }
OnReceiveEvent(const CommonEventData & data)98 void WifiServiceMonitor::OnReceiveEvent(const CommonEventData &data)
99 {
100 int code = data.GetCode();
101 std::string action = data.GetWant().GetAction();
102 SoftBusWifiState state = SOFTBUS_WIFI_UNKNOWN;
103 LNN_LOGI(LNN_BUILDER, "notify wifiservice event=%{public}s, code=%{public}d", action.c_str(), code);
104 if (action == CommonEventSupport::COMMON_EVENT_WIFI_CONN_STATE) {
105 SetSoftBusWifiConnState(code, &state);
106 }
107 if (action == CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE) {
108 SetSoftBusWifiUseState(code, &state);
109 }
110 if (action == CommonEventSupport::COMMON_EVENT_WIFI_HOTSPOT_STATE) {
111 SetSoftBusWifiHotSpotState(code, &state);
112 }
113 if (state != SOFTBUS_WIFI_UNKNOWN) {
114 LnnNotifyWlanStateChangeEvent(state);
115 }
116 }
117
118 class SubscribeEvent {
119 public:
120 int32_t SubscribeWifiConnStateEvent();
121 int32_t SubscribeWifiPowerStateEvent();
122 int32_t SubscribeAPConnStateEvent();
123 };
124
SubscribeAPConnStateEvent()125 int32_t SubscribeEvent::SubscribeAPConnStateEvent()
126 {
127 MatchingSkills matchingSkills;
128 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_WIFI_HOTSPOT_STATE);
129 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
130 std::shared_ptr<WifiServiceMonitor> subscriberPtr = std::make_shared<WifiServiceMonitor>(subscriberInfo);
131 if (!CommonEventManager::SubscribeCommonEvent(subscriberPtr)) {
132 return SOFTBUS_ERR;
133 }
134 return SOFTBUS_OK;
135 }
136
SubscribeWifiConnStateEvent()137 int32_t SubscribeEvent::SubscribeWifiConnStateEvent()
138 {
139 MatchingSkills matchingSkills;
140 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_WIFI_CONN_STATE);
141 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
142 std::shared_ptr<WifiServiceMonitor> subscriberPtr = std::make_shared<WifiServiceMonitor>(subscriberInfo);
143 if (!CommonEventManager::SubscribeCommonEvent(subscriberPtr)) {
144 return SOFTBUS_ERR;
145 }
146 return SOFTBUS_OK;
147 }
148
SubscribeWifiPowerStateEvent()149 int32_t SubscribeEvent::SubscribeWifiPowerStateEvent()
150 {
151 MatchingSkills matchingSkills;
152 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE);
153 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
154 std::shared_ptr<WifiServiceMonitor> subscriberPtr = std::make_shared<WifiServiceMonitor>(subscriberInfo);
155 if (!CommonEventManager::SubscribeCommonEvent(subscriberPtr)) {
156 return SOFTBUS_ERR;
157 }
158 return SOFTBUS_OK;
159 }
160 } // namespace EventFwk
161 } // namespace OHOS
162
LnnSubscribeWifiService(void * para)163 static void LnnSubscribeWifiService(void *para)
164 {
165 (void)para;
166 static int32_t retry = 0;
167 if (retry > RETRY_MAX) {
168 LNN_LOGE(LNN_BUILDER, "try subscribe wifiservice event max times");
169 return;
170 }
171 OHOS::EventFwk::SubscribeEvent *subscriberPtr = new OHOS::EventFwk::SubscribeEvent();
172 if (subscriberPtr == nullptr) {
173 LNN_LOGE(LNN_BUILDER, "SubscribeEvent init fail");
174 return;
175 }
176 if (subscriberPtr->SubscribeWifiConnStateEvent() == SOFTBUS_OK &&
177 subscriberPtr->SubscribeWifiPowerStateEvent() == SOFTBUS_OK &&
178 subscriberPtr->SubscribeAPConnStateEvent() == SOFTBUS_OK) {
179 LNN_LOGI(LNN_BUILDER, "subscribe wifiservice conn and power state success");
180 } else {
181 LNN_LOGE(LNN_BUILDER, "subscribe wifiservice event fail");
182 retry++;
183 SoftBusLooper *looper = GetLooper(LOOP_TYPE_DEFAULT);
184 if (LnnAsyncCallbackDelayHelper(looper, LnnSubscribeWifiService, NULL, DELAY_LEN) != SOFTBUS_OK) {
185 LNN_LOGE(LNN_BUILDER, "LnnAsyncCallbackDelayHelper fail");
186 }
187 }
188 delete subscriberPtr;
189 }
190
LnnInitWifiServiceMonitorImpl(void)191 int32_t LnnInitWifiServiceMonitorImpl(void)
192 {
193 SoftBusLooper *looper = GetLooper(LOOP_TYPE_DEFAULT);
194 int32_t ret = LnnAsyncCallbackDelayHelper(looper, LnnSubscribeWifiService, NULL, DELAY_LEN);
195 if (ret != SOFTBUS_OK) {
196 LNN_LOGE(LNN_INIT, "LnnAsyncCallbackDelayHelper fail");
197 }
198 return ret;
199 }
200