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