1518678f8Sopenharmony_ci/*
2518678f8Sopenharmony_ci * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3518678f8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4518678f8Sopenharmony_ci * you may not use this file except in compliance with the License.
5518678f8Sopenharmony_ci * You may obtain a copy of the License at
6518678f8Sopenharmony_ci *
7518678f8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8518678f8Sopenharmony_ci *
9518678f8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10518678f8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11518678f8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12518678f8Sopenharmony_ci * See the License for the specific language governing permissions and
13518678f8Sopenharmony_ci * limitations under the License.
14518678f8Sopenharmony_ci */
15518678f8Sopenharmony_ci#include "dhcp_client_callback_proxy.h"
16518678f8Sopenharmony_ci#include "dhcp_client_stub.h"
17518678f8Sopenharmony_ci#include "dhcp_logger.h"
18518678f8Sopenharmony_ci#include "dhcp_manager_service_ipc_interface_code.h"
19518678f8Sopenharmony_ci#include "dhcp_client_state_machine.h"
20518678f8Sopenharmony_ci
21518678f8Sopenharmony_ciDEFINE_DHCPLOG_DHCP_LABEL("DhcpClientStub");
22518678f8Sopenharmony_ci
23518678f8Sopenharmony_cinamespace OHOS {
24518678f8Sopenharmony_cinamespace DHCP {
25518678f8Sopenharmony_ciDhcpClientStub::DhcpClientStub()
26518678f8Sopenharmony_ci{
27518678f8Sopenharmony_ci    InitHandleMap();
28518678f8Sopenharmony_ci}
29518678f8Sopenharmony_ci
30518678f8Sopenharmony_ciDhcpClientStub::~DhcpClientStub()
31518678f8Sopenharmony_ci{
32518678f8Sopenharmony_ci    DHCP_LOGI("enter ~DhcpClientStub!");
33518678f8Sopenharmony_ci#ifndef OHOS_ARCH_LITE
34518678f8Sopenharmony_ci#ifndef DTFUZZ_TEST
35518678f8Sopenharmony_ci    RemoveDeviceCbDeathRecipient();
36518678f8Sopenharmony_ci#endif
37518678f8Sopenharmony_ci#endif
38518678f8Sopenharmony_ci}
39518678f8Sopenharmony_ci
40518678f8Sopenharmony_ci#ifndef OHOS_ARCH_LITE
41518678f8Sopenharmony_civoid DhcpClientStub::RemoveDeviceCbDeathRecipient()
42518678f8Sopenharmony_ci{
43518678f8Sopenharmony_ci    DHCP_LOGI("enter RemoveDeathRecipient!");
44518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
45518678f8Sopenharmony_ci    for (auto iter = remoteDeathMap.begin(); iter != remoteDeathMap.end(); ++iter) {
46518678f8Sopenharmony_ci        iter->first->RemoveDeathRecipient(iter->second);
47518678f8Sopenharmony_ci    }
48518678f8Sopenharmony_ci    remoteDeathMap.clear();
49518678f8Sopenharmony_ci}
50518678f8Sopenharmony_ci
51518678f8Sopenharmony_civoid DhcpClientStub::RemoveDeviceCbDeathRecipient(const wptr<IRemoteObject> &remoteObject)
52518678f8Sopenharmony_ci{
53518678f8Sopenharmony_ci    DHCP_LOGI("RemoveDeathRecipient, remoteObject: %{private}p!", &remoteObject);
54518678f8Sopenharmony_ci    std::lock_guard<std::mutex> lock(mutex_);
55518678f8Sopenharmony_ci    RemoteDeathMap::iterator iter = remoteDeathMap.find(remoteObject.promote());
56518678f8Sopenharmony_ci    if (iter == remoteDeathMap.end()) {
57518678f8Sopenharmony_ci        DHCP_LOGI("not find remoteObject to deal!");
58518678f8Sopenharmony_ci    } else {
59518678f8Sopenharmony_ci        remoteObject->RemoveDeathRecipient(iter->second);
60518678f8Sopenharmony_ci        remoteDeathMap.erase(iter);
61518678f8Sopenharmony_ci        DHCP_LOGI("remove death recipient success!");
62518678f8Sopenharmony_ci    }
63518678f8Sopenharmony_ci}
64518678f8Sopenharmony_ci
65518678f8Sopenharmony_civoid DhcpClientStub::OnRemoteDied(const wptr<IRemoteObject> &remoteObject)
66518678f8Sopenharmony_ci{
67518678f8Sopenharmony_ci    DHCP_LOGI("OnRemoteDied, Remote is died! remoteObject: %{private}p", &remoteObject);
68518678f8Sopenharmony_ci    RemoveDeviceCbDeathRecipient(remoteObject);
69518678f8Sopenharmony_ci}
70518678f8Sopenharmony_ci#endif
71518678f8Sopenharmony_ci
72518678f8Sopenharmony_civoid DhcpClientStub::InitHandleMap()
73518678f8Sopenharmony_ci{
74518678f8Sopenharmony_ci    handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_REG_CALL_BACK)] =
75518678f8Sopenharmony_ci        [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
76518678f8Sopenharmony_ci            return OnRegisterCallBack(code, data, reply, option);
77518678f8Sopenharmony_ci        };
78518678f8Sopenharmony_ci    handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_START_DHCP_CLIENT)] =
79518678f8Sopenharmony_ci        [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
80518678f8Sopenharmony_ci            return OnStartDhcpClient(code, data, reply, option);
81518678f8Sopenharmony_ci        };
82518678f8Sopenharmony_ci    handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_STOP_DHCP_CLIENT)] =
83518678f8Sopenharmony_ci        [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
84518678f8Sopenharmony_ci            return OnStopDhcpClient(code, data, reply, option);
85518678f8Sopenharmony_ci        };
86518678f8Sopenharmony_ci    handleFuncMap[static_cast<uint32_t>(DhcpClientInterfaceCode::DHCP_CLIENT_SVR_CMD_SET_CONFIG)] =
87518678f8Sopenharmony_ci        [this](uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) {
88518678f8Sopenharmony_ci            return OnSetConfiguration(code, data, reply, option);
89518678f8Sopenharmony_ci        };
90518678f8Sopenharmony_ci}
91518678f8Sopenharmony_ci
92518678f8Sopenharmony_ciint DhcpClientStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
93518678f8Sopenharmony_ci{
94518678f8Sopenharmony_ci    if (data.ReadInterfaceToken() != GetDescriptor()) {
95518678f8Sopenharmony_ci        DHCP_LOGE("dhcp client stub token verification error: %{public}d", code);
96518678f8Sopenharmony_ci        return DHCP_OPT_FAILED;
97518678f8Sopenharmony_ci    }
98518678f8Sopenharmony_ci    int exception = data.ReadInt32();
99518678f8Sopenharmony_ci    if (exception) {
100518678f8Sopenharmony_ci        DHCP_LOGI("exception is ture, return failed");
101518678f8Sopenharmony_ci        return DHCP_OPT_FAILED;
102518678f8Sopenharmony_ci    }
103518678f8Sopenharmony_ci    HandleFuncMap::iterator iter = handleFuncMap.find(code);
104518678f8Sopenharmony_ci    if (iter == handleFuncMap.end()) {
105518678f8Sopenharmony_ci        DHCP_LOGI("not find function to deal, code %{public}u", code);
106518678f8Sopenharmony_ci        return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
107518678f8Sopenharmony_ci    } else {
108518678f8Sopenharmony_ci        return (iter->second)(code, data, reply, option);
109518678f8Sopenharmony_ci    }
110518678f8Sopenharmony_ci}
111518678f8Sopenharmony_ci
112518678f8Sopenharmony_ciint DhcpClientStub::OnRegisterCallBack(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
113518678f8Sopenharmony_ci{
114518678f8Sopenharmony_ci    DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
115518678f8Sopenharmony_ci    sptr<IRemoteObject> remote = data.ReadRemoteObject();
116518678f8Sopenharmony_ci    if (remote == nullptr) {
117518678f8Sopenharmony_ci        DHCP_LOGE("Failed to ReadRemoteObject!");
118518678f8Sopenharmony_ci        return DHCP_E_INVALID_PARAM;
119518678f8Sopenharmony_ci    }
120518678f8Sopenharmony_ci    sptr<IDhcpClientCallBack> callback_ = iface_cast<IDhcpClientCallBack>(remote);
121518678f8Sopenharmony_ci    if (callback_ == nullptr) {
122518678f8Sopenharmony_ci        callback_ = new (std::nothrow) DhcpClientCallbackProxy(remote);
123518678f8Sopenharmony_ci        DHCP_LOGI("create new DhcpClientCallbackProxy!");
124518678f8Sopenharmony_ci    }
125518678f8Sopenharmony_ci    std::string ifName = data.ReadString();
126518678f8Sopenharmony_ci    if (deathRecipient_ == nullptr) {
127518678f8Sopenharmony_ci#ifdef OHOS_ARCH_LITE
128518678f8Sopenharmony_ci        deathRecipient_ = new (std::nothrow) DhcpClientDeathRecipient();
129518678f8Sopenharmony_ci#else
130518678f8Sopenharmony_ci        deathRecipient_ = new (std::nothrow) ClientDeathRecipient(*this);
131518678f8Sopenharmony_ci        remoteDeathMap.insert(std::make_pair(remote, deathRecipient_));
132518678f8Sopenharmony_ci#endif
133518678f8Sopenharmony_ci    }
134518678f8Sopenharmony_ci    ErrCode ret = RegisterDhcpClientCallBack(ifName, callback_);
135518678f8Sopenharmony_ci    reply.WriteInt32(0);
136518678f8Sopenharmony_ci    reply.WriteInt32(ret);
137518678f8Sopenharmony_ci    return 0;
138518678f8Sopenharmony_ci}
139518678f8Sopenharmony_ci
140518678f8Sopenharmony_ciint DhcpClientStub::OnStartDhcpClient(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
141518678f8Sopenharmony_ci{
142518678f8Sopenharmony_ci    DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
143518678f8Sopenharmony_ci    std::string ifname = data.ReadString();
144518678f8Sopenharmony_ci    bool bIpv6 = data.ReadBool();
145518678f8Sopenharmony_ci    ErrCode ret = StartDhcpClient(ifname, bIpv6);
146518678f8Sopenharmony_ci    reply.WriteInt32(0);
147518678f8Sopenharmony_ci    reply.WriteInt32(ret);
148518678f8Sopenharmony_ci    return 0;
149518678f8Sopenharmony_ci}
150518678f8Sopenharmony_ci
151518678f8Sopenharmony_ciint DhcpClientStub::OnSetConfiguration(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
152518678f8Sopenharmony_ci{
153518678f8Sopenharmony_ci    DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
154518678f8Sopenharmony_ci    std::string ifname = data.ReadString();
155518678f8Sopenharmony_ci    RouterConfig config;
156518678f8Sopenharmony_ci    config.bssid = data.ReadString();
157518678f8Sopenharmony_ci    ErrCode ret = SetConfiguration(ifname, config);
158518678f8Sopenharmony_ci    reply.WriteInt32(0);
159518678f8Sopenharmony_ci    reply.WriteInt32(ret);
160518678f8Sopenharmony_ci    return 0;
161518678f8Sopenharmony_ci}
162518678f8Sopenharmony_ci
163518678f8Sopenharmony_ciint DhcpClientStub::OnStopDhcpClient(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
164518678f8Sopenharmony_ci{
165518678f8Sopenharmony_ci    DHCP_LOGI("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
166518678f8Sopenharmony_ci    std::string ifname = data.ReadString();
167518678f8Sopenharmony_ci    bool bIpv6 = data.ReadBool();
168518678f8Sopenharmony_ci    ErrCode ret = StopDhcpClient(ifname, bIpv6);
169518678f8Sopenharmony_ci    reply.WriteInt32(0);
170518678f8Sopenharmony_ci    reply.WriteInt32(ret);
171518678f8Sopenharmony_ci    return 0;
172518678f8Sopenharmony_ci}
173518678f8Sopenharmony_ci}
174518678f8Sopenharmony_ci}
175