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