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