1 /*
2  * Copyright (C) 2021-2023 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 #include "dhcp_server_callback_stub.h"
16 #include "dhcp_manager_service_ipc_interface_code.h"
17 #include "dhcp_logger.h"
18 
19 DEFINE_DHCPLOG_DHCP_LABEL("DhcpServreCallBackStub");
20 
21 namespace OHOS {
22 namespace DHCP {
DhcpServreCallBackStub()23 DhcpServreCallBackStub::DhcpServreCallBackStub() : callback_(nullptr), mRemoteDied(false)
24 {
25     DHCP_LOGI("DhcpServreCallBackStub Enter DhcpServreCallBackStub");
26 }
27 
~DhcpServreCallBackStub()28 DhcpServreCallBackStub::~DhcpServreCallBackStub()
29 {
30     DHCP_LOGI("DhcpServreCallBackStub Enter ~DhcpServreCallBackStub");
31 }
32 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int DhcpServreCallBackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
34     MessageOption &option)
35 {
36     DHCP_LOGI("DhcpServreCallBackStub::OnRemoteRequest, code:%{public}u", code);
37     if (data.ReadInterfaceToken() != GetDescriptor()) {
38         DHCP_LOGE("Sta callback stub token verification error: %{public}d", code);
39         return DHCP_E_FAILED;
40     }
41 
42     int exception = data.ReadInt32();
43     if (exception) {
44         DHCP_LOGE("DhcpServreCallBackStub::OnRemoteRequest, got exception: %{public}d!", exception);
45         return DHCP_E_FAILED;
46     }
47     int ret = -1;
48     switch (code) {
49         case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_CBK_SERVER_STATUS_CHANGE): {
50             ret = RemoteOnServerStatusChanged(code, data, reply);
51             break;
52         }
53         case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_CBK_SERVER_LEASES_CHANGE): {
54             ret = RemoteOnServerLeasesChanged(code, data, reply);
55             break;
56         }
57         case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_CBK_SER_EXIT_CHANGE): {
58             ret = RemoteOnServerSerExitChanged(code, data, reply);
59             break;
60         }
61         default: {
62             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
63             break;
64         }
65     }
66     DHCP_LOGI("DhcpClientCallBackStub OnRemoteRequest, ret:%{public}d", ret);
67     return ret;
68 }
69 
RegisterCallBack(const sptr<IDhcpServerCallBack> & callBack)70 void DhcpServreCallBackStub::RegisterCallBack(const sptr<IDhcpServerCallBack> &callBack)
71 {
72     if (callBack == nullptr) {
73         DHCP_LOGE("DhcpServreCallBackStub:callBack is nullptr!");
74         return;
75     }
76     callback_ = callBack;
77 }
78 
IsRemoteDied() const79 bool DhcpServreCallBackStub::IsRemoteDied() const
80 {
81     return mRemoteDied;
82 }
83 
SetRemoteDied(bool val)84 void DhcpServreCallBackStub::SetRemoteDied(bool val)
85 {
86     DHCP_LOGI("DhcpServreCallBackStub::SetRemoteDied, state:%{public}d!", val);
87     mRemoteDied = val;
88 }
89 
OnServerStatusChanged(int status)90 void DhcpServreCallBackStub::OnServerStatusChanged(int status)
91 {
92     DHCP_LOGI("DhcpServreCallBackStub::OnServerStatusChanged, status:%{public}d", status);
93     if (callback_) {
94         callback_->OnServerStatusChanged(status);
95     }
96 }
97 
OnServerLeasesChanged(const std::string & ifname,std::vector<std::string> & leases)98 void DhcpServreCallBackStub::OnServerLeasesChanged(const std::string& ifname, std::vector<std::string>& leases)
99 {
100     DHCP_LOGI("DhcpServreCallBackStub::OnServerLeasesChanged, ifname:%{public}s", ifname.c_str());
101     if (callback_) {
102         callback_->OnServerLeasesChanged(ifname, leases);
103     }
104 }
105 
OnServerSuccess(const std::string & ifname,std::vector<DhcpStationInfo> & stationInfos)106 void DhcpServreCallBackStub::OnServerSuccess(const std::string &ifname, std::vector<DhcpStationInfo> &stationInfos)
107 {
108     DHCP_LOGI("DhcpServreCallBackStub::OnServerSuccess, ifname:%{public}s", ifname.c_str());
109     if (callback_) {
110         callback_->OnServerSuccess(ifname.c_str(), stationInfos);
111     }
112 }
113 
OnServerSerExitChanged(const std::string & ifname)114 void DhcpServreCallBackStub::OnServerSerExitChanged(const std::string& ifname)
115 {
116     DHCP_LOGI("DhcpServreCallBackStub::OnWifiWpsStateChanged, ifname:%{public}s", ifname.c_str());
117     if (callback_) {
118         callback_->OnServerSerExitChanged(ifname);
119     }
120 }
121 
RemoteOnServerStatusChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)122 int DhcpServreCallBackStub::RemoteOnServerStatusChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
123 {
124     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
125     // callback OnServerStatusChanged
126     int state = data.ReadInt32();
127     OnServerStatusChanged(state);
128     reply.WriteInt32(0);
129     return 0;
130 }
131 
RemoteOnServerSuccess(uint32_t code,MessageParcel & data,MessageParcel & reply)132 int DhcpServreCallBackStub::RemoteOnServerSuccess(uint32_t code, MessageParcel &data, MessageParcel &reply)
133 {
134     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
135     reply.WriteInt32(0);
136     return 0;
137 }
138 
RemoteOnServerLeasesChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)139 int DhcpServreCallBackStub::RemoteOnServerLeasesChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
140 {
141     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
142     reply.WriteInt32(0);
143     return 0;
144 }
145 
RemoteOnServerSerExitChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)146 int DhcpServreCallBackStub::RemoteOnServerSerExitChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
147 {
148     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
149     reply.WriteInt32(0);
150     return 0;
151 }
152 
153 }  // namespace DHCP
154 }  // namespace OHOS