• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "common_event_data.h"
21 #include "common_event_manager.h"
22 #include "common_event_subscriber.h"
23 #include "common_event_support.h"
24 #include "lnn_async_callback_utils.h"
25 #include "ohos/aafwk/content/want.h"
26 #include "wifi_msg.h"
27 
28 #include "softbus_adapter_mem.h"
29 #include "softbus_errcode.h"
30 #include "softbus_log.h"
31 
32 static const int32_t DELAY_LEN = 1000;
33 static const int32_t RETRY_MAX = 10;
34 static LnnMonitorEventHandler g_eventHandler;
35 
36 namespace OHOS {
37 namespace EventFwk {
38 class WifiServiceMonitor : public CommonEventSubscriber {
39 public:
40     explicit WifiServiceMonitor(const CommonEventSubscribeInfo &subscriberInfo);
~WifiServiceMonitor()41     virtual ~WifiServiceMonitor(){}
42     virtual void OnReceiveEvent(const CommonEventData &data);
43 };
44 
WifiServiceMonitor(const CommonEventSubscribeInfo & subscriberInfo)45 WifiServiceMonitor::WifiServiceMonitor(const CommonEventSubscribeInfo &subscriberInfo)
46     : CommonEventSubscriber(subscriberInfo)
47 {}
48 
OnReceiveEvent(const CommonEventData & data)49 void WifiServiceMonitor::OnReceiveEvent(const CommonEventData &data)
50 {
51     int code = data.GetCode();
52     std::string action = data.GetWant().GetAction();
53     SoftBusWifiState state = SOFTBUS_UNKNOWN;
54     LnnMoniterData *para = (LnnMoniterData *)SoftBusCalloc(sizeof(LnnMoniterData) + sizeof(int));
55     if (para == NULL) {
56         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "LnnMoniterData malloc failed");
57         return;
58     }
59     para->len = sizeof(int);
60     SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "notify wifiservice event %s, code(%d)", action.c_str(), code);
61 
62     if (action == CommonEventSupport::COMMON_EVENT_WIFI_CONN_STATE) {
63         switch (code) {
64             case int(OHOS::Wifi::ConnectionState::CONNECT_AP_CONNECTED):
65                 state = SOFTBUS_WIFI_CONNECTED;
66                 break;
67             case int(OHOS::Wifi::ConnectionState::DISCONNECT_DISCONNECTED):
68                 state = SOFTBUS_WIFI_DISCONNECTED;
69                 break;
70             default: {
71                 break;
72             }
73         }
74     }
75     if (action == CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE) {
76         switch (code) {
77             case int(OHOS::Wifi::WifiState::DISABLED):
78                 state = SOFTBUS_WIFI_DISABLED;
79                 break;
80             default: {
81                 break;
82             }
83         }
84     }
85     if (state != SOFTBUS_UNKNOWN) {
86         (void)memcpy_s(para->value, para->len, &state, sizeof(int));
87         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_INFO, "send wifi state change event to LNN");
88         g_eventHandler(LNN_MONITOR_EVENT_WIFI_STATE_CHANGED, para);
89     }
90     SoftBusFree(para);
91 }
92 
93 class SubscribeEvent {
94 public:
95     int32_t SubscribeWifiConnStateEvent();
96     int32_t SubscribeWifiPowerStateEvent();
97 };
98 
SubscribeWifiConnStateEvent()99 int32_t SubscribeEvent::SubscribeWifiConnStateEvent()
100 {
101     MatchingSkills matchingSkills;
102     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_WIFI_CONN_STATE);
103     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
104     std::shared_ptr<WifiServiceMonitor> subscriberPtr = std::make_shared<WifiServiceMonitor>(subscriberInfo);
105     if (!CommonEventManager::SubscribeCommonEvent(subscriberPtr)) {
106         return SOFTBUS_ERR;
107     }
108     return SOFTBUS_OK;
109 }
110 
SubscribeWifiPowerStateEvent()111 int32_t SubscribeEvent::SubscribeWifiPowerStateEvent()
112 {
113     MatchingSkills matchingSkills;
114     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE);
115     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
116     std::shared_ptr<WifiServiceMonitor> subscriberPtr = std::make_shared<WifiServiceMonitor>(subscriberInfo);
117     if (!CommonEventManager::SubscribeCommonEvent(subscriberPtr)) {
118         return SOFTBUS_ERR;
119     }
120     return SOFTBUS_OK;
121 }
122 }
123 }
124 
LnnSubscribeWifiService(void * para)125 static void LnnSubscribeWifiService(void *para)
126 {
127     (void)para;
128     static int32_t retry = 0;
129     if (retry > RETRY_MAX) {
130         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "try subscribe wifiservice event max times");
131         return;
132     }
133     OHOS::EventFwk::SubscribeEvent *subscriberPtr = new OHOS::EventFwk::SubscribeEvent();
134     if (subscriberPtr == nullptr) {
135         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "SubscribeEvent init fail");
136         return;
137     }
138     if (subscriberPtr->SubscribeWifiConnStateEvent() == SOFTBUS_OK &&
139         subscriberPtr->SubscribeWifiPowerStateEvent() == SOFTBUS_OK) {
140         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "subscribe wifiservice conn and power state success");
141     } else {
142         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "subscribe wifiservice event fail");
143         retry++;
144         SoftBusLooper *looper = GetLooper(LOOP_TYPE_DEFAULT);
145         if (LnnAsyncCallbackDelayHelper(looper, LnnSubscribeWifiService, NULL, DELAY_LEN) != SOFTBUS_OK) {
146             SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init wifiservice LnnAsyncCallbackDelayHelper fail");
147         }
148     }
149     delete subscriberPtr;
150 }
151 
LnnInitWifiServiceMonitorImpl(LnnMonitorEventHandler handler)152 int32_t LnnInitWifiServiceMonitorImpl(LnnMonitorEventHandler handler)
153 {
154     if (handler == NULL) {
155         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "handler is null");
156         return SOFTBUS_ERR;
157     }
158     g_eventHandler = handler;
159     SoftBusLooper *looper = GetLooper(LOOP_TYPE_DEFAULT);
160     int32_t ret = LnnAsyncCallbackDelayHelper(looper, LnnSubscribeWifiService, NULL, DELAY_LEN);
161     if (ret != SOFTBUS_OK) {
162         SoftBusLog(SOFTBUS_LOG_LNN, SOFTBUS_LOG_ERROR, "init wifiservice LnnAsyncCallbackDelayHelper fail");
163     }
164     return ret;
165 }
166