• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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_constants.h"
22 #include "dm_log.h"
23 #include "ipc_def.h"
24 namespace OHOS { class MessageParcel; }
25 
26 namespace OHOS {
27 namespace DistributedHardware {
28 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     int32_t ret = DM_OK;
33     if (pBaseReq == nullptr) {
34         return ERR_DM_INPUT_PARA_INVALID;
35     }
36 
37     if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
38         LOGE("IpcCmdRegister::SetRequest cmdCode param invalid!");
39         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
40     }
41 
42     if (setIpcRequestFuncMap_.count(cmdCode) == 0) {
43         LOGE("cmdCode:%d not register SetRequestFunc", cmdCode);
44         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
45     }
46 
47     auto setRequestMapIter = setIpcRequestFuncMap_.find(cmdCode);
48     if (setRequestMapIter != setIpcRequestFuncMap_.end()) {
49         SetIpcRequestFunc ptr = setRequestMapIter->second;
50         if (ptr == nullptr) {
51             LOGE("IpcCmdRegister::SetRequest setRequestMapIter->second is null");
52             return ERR_DM_POINT_NULL;
53         }
54         ret = (setRequestMapIter->second)(pBaseReq, data);
55     }
56     return ret;
57 }
58 
ReadResponse(int32_t cmdCode,MessageParcel & reply,std::shared_ptr<IpcRsp> pBaseRsp)59 int32_t IpcCmdRegister::ReadResponse(int32_t cmdCode, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp)
60 {
61     if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
62         LOGE("IpcCmdRegister::ReadResponse cmdCode param invalid!");
63         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
64     }
65     auto readResponseMapIter = readResponseFuncMap_.find(cmdCode);
66     if (readResponseMapIter == readResponseFuncMap_.end()) {
67         LOGE("cmdCode:%d not register ReadResponseFunc", cmdCode);
68         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
69     }
70     if (readResponseMapIter->second == nullptr) {
71         return ERR_DM_POINT_NULL;
72     }
73     return (readResponseMapIter->second)(reply, pBaseRsp);
74 }
75 
OnIpcCmd(int32_t cmdCode,MessageParcel & data,MessageParcel & reply)76 int32_t IpcCmdRegister::OnIpcCmd(int32_t cmdCode, MessageParcel &data, MessageParcel &reply)
77 {
78     if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
79         LOGE("IpcCmdRegister::OnIpcCmd cmdCode param invalid!");
80         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
81     }
82     auto onIpcCmdMapIter = onIpcCmdFuncMap_.find(cmdCode);
83     if (onIpcCmdMapIter == onIpcCmdFuncMap_.end()) {
84         LOGE("cmdCode:%d not register OnIpcCmdFunc", cmdCode);
85         return ERR_DM_UNSUPPORTED_IPC_COMMAND;
86     }
87     if (onIpcCmdMapIter->second ==  nullptr) {
88         return ERR_DM_POINT_NULL;
89     }
90     return (onIpcCmdMapIter->second)(data, reply);
91 }
92 } // namespace DistributedHardware
93 } // namespace OHOS
94