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_hotspot_callback_stub.h"
17 #include "define.h"
18 #include "wifi_manager_service_ipc_interface_code.h"
19 #include "wifi_hisysevent.h"
20 #include "wifi_logger.h"
21 #include "wifi_msg.h"
22 #include "wifi_errcode.h"
23
24 DEFINE_WIFILOG_HOTSPOT_LABEL("WifiHotspotCallbackStub");
25 namespace OHOS {
26 namespace Wifi {
WifiHotspotCallbackStub()27 WifiHotspotCallbackStub::WifiHotspotCallbackStub() : userCallback_(nullptr), mRemoteDied(false)
28 {}
29
~WifiHotspotCallbackStub()30 WifiHotspotCallbackStub::~WifiHotspotCallbackStub()
31 {}
32
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int WifiHotspotCallbackStub::OnRemoteRequest(
34 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35 {
36 WIFI_LOGD("WifiHotspotCallbackStub::OnRemoteRequest!");
37 if (data.ReadInterfaceToken() != GetDescriptor()) {
38 WIFI_LOGE("Hotspot callback stub token verification error: %{public}d", code);
39 return WIFI_OPT_FAILED;
40 }
41
42 int exception = data.ReadInt32();
43 if (exception) {
44 return -1;
45 }
46 int ret = -1;
47 switch (code) {
48 case static_cast<uint32_t>(HotspotInterfaceCode::WIFI_CBK_CMD_HOTSPOT_STATE_CHANGE): {
49 ret = RemoteOnHotspotStateChanged(code, data, reply);
50 break;
51 }
52 case static_cast<uint32_t>(HotspotInterfaceCode::WIFI_CBK_CMD_HOTSPOT_STATE_JOIN): {
53 ret = RemoteOnHotspotStaJoin(code, data, reply);
54 break;
55 }
56 case static_cast<uint32_t>(HotspotInterfaceCode::WIFI_CBK_CMD_HOTSPOT_STATE_LEAVE): {
57 ret = RemoteOnHotspotStaLeave(code, data, reply);
58 break;
59 }
60 default: {
61 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62 break;
63 }
64 }
65 return ret;
66 }
67
RemoteOnHotspotStateChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)68 int WifiHotspotCallbackStub::RemoteOnHotspotStateChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
69 {
70 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
71 int state = data.ReadInt32();
72 OnHotspotStateChanged(state);
73 return 0;
74 }
75
RemoteOnHotspotStaJoin(uint32_t code,MessageParcel & data,MessageParcel & reply)76 int WifiHotspotCallbackStub::RemoteOnHotspotStaJoin(uint32_t code, MessageParcel &data, MessageParcel &reply)
77 {
78 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
79 const char *readStr = nullptr;
80 StationInfo info;
81 readStr = data.ReadCString();
82 info.deviceName = (readStr != nullptr) ? readStr : "";
83 readStr = data.ReadCString();
84 info.bssid = (readStr != nullptr) ? readStr : "";
85 info.bssidType = data.ReadInt32();
86 readStr = data.ReadCString();
87 info.ipAddr = (readStr != nullptr) ? readStr : "";
88 OnHotspotStaJoin(info);
89 return 0;
90 }
91
RemoteOnHotspotStaLeave(uint32_t code,MessageParcel & data,MessageParcel & reply)92 int WifiHotspotCallbackStub::RemoteOnHotspotStaLeave(uint32_t code, MessageParcel &data, MessageParcel &reply)
93 {
94 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
95 const char *readStr = nullptr;
96 StationInfo info;
97 readStr = data.ReadCString();
98 info.deviceName = (readStr != nullptr) ? readStr : "";
99 readStr = data.ReadCString();
100 info.bssid = (readStr != nullptr) ? readStr : "";
101 info.bssidType = data.ReadInt32();
102 readStr = data.ReadCString();
103 info.ipAddr = (readStr != nullptr) ? readStr : "";
104 OnHotspotStaLeave(info);
105 return 0;
106 }
107
RegisterCallBack(const sptr<IWifiHotspotCallback> & callBack)108 void WifiHotspotCallbackStub::RegisterCallBack(const sptr<IWifiHotspotCallback> &callBack)
109 {
110 if (callBack == nullptr) {
111 WIFI_LOGD("RegisterCallBack:callBack is nullptr!");
112 return;
113 }
114 userCallback_ = callBack;
115 }
116
OnHotspotStateChanged(int state)117 void WifiHotspotCallbackStub::OnHotspotStateChanged(int state)
118 {
119 WIFI_LOGI("WifiHotspotCallbackStub::OnHotspotStateChanged, state:%{public}d.", state);
120 if (userCallback_) {
121 userCallback_->OnHotspotStateChanged(state);
122 }
123 WriteWifiEventReceivedHiSysEvent(HISYS_HOTSPOT_STATE_CHANGE, state);
124 }
125
OnHotspotStaJoin(const StationInfo & info)126 void WifiHotspotCallbackStub::OnHotspotStaJoin(const StationInfo &info)
127 {
128 WIFI_LOGI("WifiHotspotCallbackStub::OnHotspotStaJoin");
129 if (userCallback_) {
130 userCallback_->OnHotspotStaJoin(info);
131 }
132 WriteWifiEventReceivedHiSysEvent(HISYS_HOTSPOT_STA_JOIN, HISYS_EVENT_DEFAULT_VALUE);
133 }
134
OnHotspotStaLeave(const StationInfo & info)135 void WifiHotspotCallbackStub::OnHotspotStaLeave(const StationInfo &info)
136 {
137 WIFI_LOGI("WifiHotspotCallbackStub::OnHotspotStaLeave");
138 if (userCallback_) {
139 userCallback_->OnHotspotStaLeave(info);
140 }
141 WriteWifiEventReceivedHiSysEvent(HISYS_HOTSPOT_STA_LEAVE, HISYS_EVENT_DEFAULT_VALUE);
142 }
143
IsRemoteDied() const144 bool WifiHotspotCallbackStub::IsRemoteDied() const
145 {
146 return mRemoteDied;
147 }
148
SetRemoteDied(bool val)149 void WifiHotspotCallbackStub::SetRemoteDied(bool val)
150 {
151 mRemoteDied = val;
152 }
153 } // namespace Wifi
154 } // namespace OHOS