1 /*
2  * Copyright (c) 2022-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 
16 #include "ipc_client_stub.h"
17 
18 #include "device_manager_ipc_interface_code.h"
19 #include "dm_constants.h"
20 #include "dm_log.h"
21 #include "ipc_cmd_register.h"
22 #include "ipc_object_stub.h"   // for IPCObjectStub
23 #include "message_option.h"    // for MessageOption
24 #include "message_parcel.h"    // for MessageParcel
25 namespace OHOS::DistributedHardware { class IpcReq; }
26 namespace OHOS::DistributedHardware { class IpcRsp; }
27 
28 namespace OHOS {
29 namespace DistributedHardware {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int32_t IpcClientStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
31 {
32     auto remoteDescriptor = data.ReadInterfaceToken();
33     if (GetDescriptor() != remoteDescriptor) {
34         LOGI("ReadInterfaceToken fail!");
35         return ERR_DM_IPC_READ_FAILED;
36     }
37     if (IpcCmdRegister::GetInstance().OnIpcCmd(static_cast<int32_t>(code), data, reply) == DM_OK) {
38         return DM_OK;
39     }
40     LOGW("unsupported code: %{public}u", code);
41     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
42 }
43 
SendCmd(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)44 int32_t IpcClientStub::SendCmd(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
45 {
46     MessageParcel data;
47     MessageParcel reply;
48     MessageOption option;
49     if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
50         LOGE("IpcClientStub::SetRequest cmdCode param invalid!");
51         return IPCObjectStub::OnRemoteRequest(cmdCode, data, reply, option);
52     }
53     LOGI("SendCmd cmdCode: %{public}d", cmdCode);
54 
55     if (IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, data) != DM_OK) {
56         LOGE("set request cmd failed");
57         return ERR_DM_IPC_SEND_REQUEST_FAILED;
58     }
59 
60     LOGI("cmdCode = %{public}d, flags = %{public}d.", cmdCode, option.GetFlags());
61     if (IpcCmdRegister::GetInstance().OnIpcCmd(cmdCode, data, reply) == DM_OK) {
62         LOGE("on ipc cmd success");
63         return DM_OK;
64     }
65     return IpcCmdRegister::GetInstance().ReadResponse(cmdCode, reply, rsp);
66 }
67 } // namespace DistributedHardware
68 } // namespace OHOS
69