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