1 /*
2 * Copyright (c) 2021 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 "dm_constants.h"
19 #include "dm_log.h"
20
21 namespace OHOS {
22 namespace DistributedHardware {
23 IMPLEMENT_SINGLE_INSTANCE(IpcCmdRegister);
24
SetRequest(int32_t cmdCode,std::shared_ptr<IpcReq> pBaseReq,MessageParcel & data)25 int32_t IpcCmdRegister::SetRequest(int32_t cmdCode, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data)
26 {
27 if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT || pBaseReq == nullptr) {
28 LOGE("IpcCmdRegister::SetRequest cmdCode param invalid!");
29 return ERR_DM_INPUT_PARA_INVALID;
30 }
31 auto setRequestMapIter = setIpcRequestFuncMap_.find(cmdCode);
32 if (setRequestMapIter == setIpcRequestFuncMap_.end()) {
33 LOGE("cmdCode:%d not register SetRequestFunc", cmdCode);
34 return DM_IPC_NOT_REGISTER_FUNC;
35 }
36 return (setRequestMapIter->second)(pBaseReq, data);
37 }
38
ReadResponse(int32_t cmdCode,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)39 int32_t IpcCmdRegister::ReadResponse(int32_t cmdCode, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
40 {
41 if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
42 LOGE("IpcCmdRegister::ReadResponse cmdCode param invalid!");
43 return ERR_DM_INPUT_PARA_INVALID;
44 }
45 auto readResponseMapIter = readResponseFuncMap_.find(cmdCode);
46 if (readResponseMapIter == readResponseFuncMap_.end()) {
47 LOGE("cmdCode:%d not register ReadResponseFunc", cmdCode);
48 return DM_IPC_NOT_REGISTER_FUNC;
49 }
50 return (readResponseMapIter->second)(reply, pBaseRsp);
51 }
52
OnIpcCmd(int32_t cmdCode,MessageParcel & data,MessageParcel & reply)53 int32_t IpcCmdRegister::OnIpcCmd(int32_t cmdCode, MessageParcel &data, MessageParcel &reply)
54 {
55 if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
56 LOGE("IpcCmdRegister::OnIpcCmd cmdCode param invalid!");
57 return ERR_DM_INPUT_PARA_INVALID;
58 }
59 auto onIpcCmdMapIter = onIpcCmdFuncMap_.find(cmdCode);
60 if (onIpcCmdMapIter == onIpcCmdFuncMap_.end()) {
61 LOGE("cmdCode:%d not register OnIpcCmdFunc", cmdCode);
62 return DM_IPC_NOT_REGISTER_FUNC;
63 }
64 return (onIpcCmdMapIter->second)(data, reply);
65 }
66 } // namespace DistributedHardware
67 } // namespace OHOS
68