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 #include "dhcp_sdk_define.h"
19 
20 DEFINE_DHCPLOG_DHCP_LABEL("DhcpServreCallBackStubLite");
21 
22 namespace OHOS {
23 namespace DHCP {
24 
DhcpServreCallBackStub()25 DhcpServreCallBackStub::DhcpServreCallBackStub() : userCallback_(nullptr), mRemoteDied(false)
26 {
27     DHCP_LOGI("DhcpServreCallBackStub Enter DhcpServreCallBackStub");
28 }
29 
~DhcpServreCallBackStub()30 DhcpServreCallBackStub::~DhcpServreCallBackStub()
31 {
32     DHCP_LOGI("DhcpServreCallBackStub Enter ~DhcpServreCallBackStub");
33 }
34 
OnRemoteRequest(uint32_t code,IpcIo * data)35 int DhcpServreCallBackStub::OnRemoteRequest(uint32_t code, IpcIo *data)
36 {
37     int ret = -1;
38     DHCP_LOGD("OnRemoteRequest code:%{public}u!", code);
39     if (mRemoteDied || data == nullptr) {
40         DHCP_LOGD("Failed to %{public}s,mRemoteDied:%{public}d data:%{public}d!",
41             __func__, mRemoteDied, data == nullptr);
42         return ret;
43     }
44 
45     size_t length;
46     uint16_t* interfaceRead = nullptr;
47     interfaceRead = ReadInterfaceToken(data, &length);
48     for (size_t i = 0; i < length; i++) {
49         if (i >= DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH || interfaceRead[i] != DECLARE_INTERFACE_DESCRIPTOR_L1[i]) {
50             DHCP_LOGE("Scan stub token verification error: %{public}d", code);
51             return -1;
52         }
53     }
54 
55     int exception = -1;
56     (void)ReadInt32(data, &exception);
57     if (exception) {
58         DHCP_LOGD("OnRemoteRequest exception! %{public}d!", exception);
59         return ret;
60     }
61     switch (code) {
62         case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_CBK_SERVER_STATUS_CHANGE): {
63             ret = RemoteOnServerStatusChanged(code, data);
64             break;
65         }
66         case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_CBK_SERVER_LEASES_CHANGE): {
67             ret = RemoteOnServerLeasesChanged(code, data);
68             break;
69         }
70         case static_cast<uint32_t>(DhcpServerInterfaceCode::DHCP_SERVER_CBK_SER_EXIT_CHANGE): {
71             ret = RemoteOnServerSerExitChanged(code, data);
72             break;
73         }
74         default: {
75             ret = -1;
76             break;
77         }
78     }
79     return ret;
80 }
81 
RegisterCallBack(const std::shared_ptr<IDhcpServerCallBack> & userCallback)82 void DhcpServreCallBackStub::RegisterCallBack(const std::shared_ptr<IDhcpServerCallBack> &userCallback)
83 {
84     if (userCallback_ != nullptr) {
85         DHCP_LOGD("Callback has registered!");
86         return;
87     }
88     userCallback_ = userCallback;
89 }
90 
IsRemoteDied() const91 bool DhcpServreCallBackStub::IsRemoteDied() const
92 {
93     return mRemoteDied;
94 }
95 
SetRemoteDied(bool val)96 void DhcpServreCallBackStub::SetRemoteDied(bool val)
97 {
98     mRemoteDied = val;
99 }
100 
OnServerStatusChanged(int status)101 void DhcpServreCallBackStub::OnServerStatusChanged(int status)
102 {
103     DHCP_LOGI("DhcpServreCallBackStub::OnServerStatusChanged, status:%{public}d", status);
104     if (userCallback_) {
105         userCallback_->OnServerStatusChanged(status);
106     }
107 }
108 
OnServerLeasesChanged(const std::string & ifname,std::vector<std::string> & leases)109 void DhcpServreCallBackStub::OnServerLeasesChanged(const std::string& ifname, std::vector<std::string>& leases)
110 {
111     DHCP_LOGI("DhcpServreCallBackStub::OnServerLeasesChanged, ifname:%{public}s", ifname.c_str());
112     if (userCallback_) {
113         userCallback_->OnServerLeasesChanged(ifname, leases);
114     }
115 }
116 
OnServerSuccess(const std::string & ifname,std::vector<DhcpStationInfo> & stationInfos)117 void DhcpServreCallBackStub::OnServerSuccess(const std::string& ifname, std::vector<DhcpStationInfo>& stationInfos)
118 {
119     DHCP_LOGI("DhcpServreCallBackStub::OnServerSuccess, ifname:%{public}s", ifname.c_str());
120     if (userCallback_) {
121         userCallback_->OnServerSuccess(ifname.c_str(), stationInfos);
122     }
123 }
124 
OnServerSerExitChanged(const std::string & ifname)125 void DhcpServreCallBackStub::OnServerSerExitChanged(const std::string& ifname)
126 {
127     DHCP_LOGI("DhcpServreCallBackStub::OnWifiWpsStateChanged, ifname:%{public}s", ifname.c_str());
128     if (userCallback_) {
129         userCallback_->OnServerSerExitChanged(ifname);
130     }
131 }
132 
RemoteOnServerStatusChanged(uint32_t code,IpcIo * data)133 int DhcpServreCallBackStub::RemoteOnServerStatusChanged(uint32_t code, IpcIo *data)
134 {
135     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
136     int stateCode = 0;
137     (void)ReadInt32(data, &stateCode);
138     OnServerStatusChanged(state);
139     return 0;
140 }
141 
RemoteOnServerLeasesChanged(uint32_t code,IpcIo * data)142 int DhcpServreCallBackStub::RemoteOnServerLeasesChanged(uint32_t code, IpcIo *data)
143 {
144     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
145     return 0;
146 }
147 
RemoteOnServerSuccess(uint32_t code,IpcIo * data)148 int DhcpServreCallBackStub::RemoteOnServerSuccess(uint32_t code, IpcIo *data)
149 {
150     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
151     return 0;
152 }
153 
RemoteOnServerSerExitChanged(uint32_t code,IpcIo * data)154 int DhcpServreCallBackStub::RemoteOnServerSerExitChanged(uint32_t code, IpcIo *data)
155 {
156     DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
157     return 0;
158 }
159 }  // namespace DHCP
160 }  // namespace OHOS