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 "device_manager_ipc_interface_code.h"
19 #include "dm_constants.h"
20 #include "dm_log.h"
21 
22 namespace OHOS {
23 namespace DistributedHardware {
24 DM_IMPLEMENT_SINGLE_INSTANCE(IpcCmdRegister);
25 
SetRequest(int32_t cmdCode,std::shared_ptr<IpcReq> pBaseReq,IpcIo & request,uint8_t * buffer,size_t buffLen)26 int32_t IpcCmdRegister::SetRequest(int32_t cmdCode, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer,
27                                    size_t buffLen)
28 {
29     if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
30         LOGE("IpcCmdRegister::SetRequest cmdCode param invalid!");
31         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
32     }
33     auto setRequestMapIter = setIpcRequestFuncMap_.find(cmdCode);
34     if (setRequestMapIter == setIpcRequestFuncMap_.end()) {
35         LOGE("cmdCode:%{public}d not register SetRequestFunc", cmdCode);
36         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
37     }
38     return (setRequestMapIter->second)(pBaseReq, request, buffer, buffLen);
39 }
40 
ReadResponse(int32_t cmdCode,IpcIo & reply,std::shared_ptr<IpcRsp> pBaseRsp)41 int32_t IpcCmdRegister::ReadResponse(int32_t cmdCode, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp)
42 {
43     auto readResponseMapIter = readResponseFuncMap_.find(cmdCode);
44     if (readResponseMapIter == readResponseFuncMap_.end()) {
45         LOGE("cmdCode:%{public}d not register ReadResponseFunc", cmdCode);
46         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
47     }
48     return (readResponseMapIter->second)(reply, pBaseRsp);
49 }
50 
OnIpcCmd(int32_t cmdCode,IpcIo & reply)51 int32_t IpcCmdRegister::OnIpcCmd(int32_t cmdCode, IpcIo &reply)
52 {
53     if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
54         LOGE("IpcCmdRegister::OnIpcCmd cmdCode param invalid!");
55         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
56     }
57     auto onIpcCmdMapIter = onIpcCmdFuncMap_.find(cmdCode);
58     if (onIpcCmdMapIter == onIpcCmdFuncMap_.end()) {
59         LOGE("cmdCode:%{public}d not register OnIpcCmdFunc", cmdCode);
60         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
61     }
62     (onIpcCmdMapIter->second)(reply);
63     return DM_OK;
64 }
65 
OnIpcServerCmd(int32_t cmdCode,IpcIo & req,IpcIo & reply)66 int32_t IpcCmdRegister::OnIpcServerCmd(int32_t cmdCode, IpcIo &req, IpcIo &reply)
67 {
68     auto onIpcServerCmdMapIter = onIpcServerCmdFuncMap_.find(cmdCode);
69     if (onIpcServerCmdMapIter == onIpcServerCmdFuncMap_.end()) {
70         LOGE("cmdCode:%{public}d not register OnIpcCmdFunc", cmdCode);
71         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
72     }
73     (onIpcServerCmdMapIter->second)(req, reply);
74     return DM_OK;
75 }
76 } // namespace DistributedHardware
77 } // namespace OHOS
78