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