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