1 /*
2 * Copyright (c) 2022-2024 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_cmd_register.h"
17
18 #include <utility> // for pair
19
20 #include "device_manager_ipc_interface_code.h"
21 #include "dm_log.h"
22 #include "ipc_def.h"
23 namespace OHOS { class MessageParcel; }
24
25 namespace OHOS {
26 namespace DistributedHardware {
27 DM_IMPLEMENT_SINGLE_INSTANCE(IpcCmdRegister);
28 constexpr int32_t DM_OK = 0;
29 constexpr int32_t ERR_DM_INPUT_PARA_INVALID = 96929749;
30 constexpr int32_t ERR_DM_UNSUPPORTED_IPC_COMMAND = 96929757;
31 constexpr int32_t ERR_DM_POINT_NULL = 96929748;
32
SetRequest(int32_t cmdCode,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)33 int32_t IpcCmdRegister::SetRequest(int32_t cmdCode, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
34 {
35 int32_t ret = DM_OK;
36 if (pBaseReq == nullptr) {
37 return ERR_DM_INPUT_PARA_INVALID;
38 }
39
40 if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
41 LOGE("IpcCmdRegister::SetRequest cmdCode param invalid!");
42 return ERR_DM_UNSUPPORTED_IPC_COMMAND;
43 }
44
45 if (setIpcRequestFuncMap_.count(cmdCode) == 0) {
46 LOGE("cmdCode:%{public}d not register SetRequestFunc", cmdCode);
47 return ERR_DM_UNSUPPORTED_IPC_COMMAND;
48 }
49
50 auto setRequestMapIter = setIpcRequestFuncMap_.find(cmdCode);
51 if (setRequestMapIter != setIpcRequestFuncMap_.end()) {
52 SetIpcRequestFunc ptr = setRequestMapIter->second;
53 if (ptr == nullptr) {
54 LOGE("IpcCmdRegister::SetRequest setRequestMapIter->second is null");
55 return ERR_DM_POINT_NULL;
56 }
57 ret = (setRequestMapIter->second)(pBaseReq, data);
58 }
59 return ret;
60 }
61
ReadResponse(int32_t cmdCode,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)62 int32_t IpcCmdRegister::ReadResponse(int32_t cmdCode, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
63 {
64 if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
65 LOGE("IpcCmdRegister::ReadResponse cmdCode param invalid!");
66 return ERR_DM_UNSUPPORTED_IPC_COMMAND;
67 }
68 auto readResponseMapIter = readResponseFuncMap_.find(cmdCode);
69 if (readResponseMapIter == readResponseFuncMap_.end()) {
70 LOGE("cmdCode:%{public}d not register ReadResponseFunc", cmdCode);
71 return ERR_DM_UNSUPPORTED_IPC_COMMAND;
72 }
73 if (readResponseMapIter->second == nullptr) {
74 return ERR_DM_POINT_NULL;
75 }
76 return (readResponseMapIter->second)(reply, pBaseRsp);
77 }
78
OnIpcCmd(int32_t cmdCode,MessageParcel & data,MessageParcel & reply)79 int32_t IpcCmdRegister::OnIpcCmd(int32_t cmdCode, MessageParcel &data, MessageParcel &reply)
80 {
81 if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
82 LOGE("IpcCmdRegister::OnIpcCmd cmdCode param invalid!");
83 return ERR_DM_UNSUPPORTED_IPC_COMMAND;
84 }
85 auto onIpcCmdMapIter = onIpcCmdFuncMap_.find(cmdCode);
86 if (onIpcCmdMapIter == onIpcCmdFuncMap_.end()) {
87 LOGE("cmdCode:%{public}d not register OnIpcCmdFunc", cmdCode);
88 return ERR_DM_UNSUPPORTED_IPC_COMMAND;
89 }
90 if (onIpcCmdMapIter->second == nullptr) {
91 return ERR_DM_POINT_NULL;
92 }
93 return (onIpcCmdMapIter->second)(data, reply);
94 }
95 } // namespace DistributedHardware
96 } // namespace OHOS
97