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