1 /* 2 * Copyright (c) 2021 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 #ifndef OHOS_DEVICE_MANAGER_IPC_CMD_PARSER_H 17 #define OHOS_DEVICE_MANAGER_IPC_CMD_PARSER_H 18 19 #include <cstdint> 20 #include <unordered_map> 21 #include <memory> 22 23 #include "iremote_broker.h" 24 #include "single_instance.h" 25 26 #include "ipc_types.h" 27 #include "ipc_req.h" 28 #include "ipc_rsp.h" 29 30 namespace OHOS { 31 namespace DistributedHardware { 32 #define ON_IPC_SET_REQUEST(cmdCode, paraA, paraB) \ 33 static int32_t IpcSetRequest##cmdCode(paraA, paraB); \ 34 struct IpcRegisterSetRequestFunc##cmdCode { \ 35 IpcRegisterSetRequestFunc##cmdCode() \ 36 { \ 37 IpcCmdRegister::GetInstance().RegisterSetRequestFunc(cmdCode, IpcSetRequest##cmdCode); \ 38 } \ 39 }; \ 40 IpcRegisterSetRequestFunc##cmdCode g_IpcRegisterSetRequestFunc##cmdCode; \ 41 static int32_t IpcSetRequest##cmdCode(paraA, paraB) \ 42 43 #define ON_IPC_READ_RESPONSE(cmdCode, paraA, paraB) \ 44 static int32_t IpcReadResponse##cmdCode(paraA, paraB); \ 45 struct IpcRegisterReadResponseFunc##cmdCode { \ 46 IpcRegisterReadResponseFunc##cmdCode() \ 47 { \ 48 IpcCmdRegister::GetInstance().RegisterReadResponseFunc(cmdCode, IpcReadResponse##cmdCode); \ 49 } \ 50 }; \ 51 IpcRegisterReadResponseFunc##cmdCode g_IpcRegisterReadResponseFunc##cmdCode; \ 52 static int32_t IpcReadResponse##cmdCode(paraA, paraB) \ 53 54 #define ON_IPC_CMD(cmdCode, paraA, paraB) \ 55 static int32_t IpcCmdProcess##cmdCode(paraA, paraB); \ 56 struct IpcRegisterCmdProcessFunc##cmdCode { \ 57 IpcRegisterCmdProcessFunc##cmdCode() \ 58 { \ 59 IpcCmdRegister::GetInstance().RegisterCmdProcessFunc(cmdCode, IpcCmdProcess##cmdCode); \ 60 } \ 61 }; \ 62 IpcRegisterCmdProcessFunc##cmdCode g_IpcRegisterCmdProcessFunc##cmdCode; \ 63 static int32_t IpcCmdProcess##cmdCode(paraA, paraB) 64 65 using SetIpcRequestFunc = int32_t (*)(std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data); 66 using ReadResponseFunc = int32_t (*)(MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp); 67 using OnIpcCmdFunc = int32_t (*)(MessageParcel &data, MessageParcel &reply); 68 69 class IpcCmdRegister { 70 DECLARE_SINGLE_INSTANCE(IpcCmdRegister); 71 public: RegisterSetRequestFunc(int32_t cmdCode,SetIpcRequestFunc setIpcRequestFunc)72 void RegisterSetRequestFunc(int32_t cmdCode, SetIpcRequestFunc setIpcRequestFunc) 73 { 74 setIpcRequestFuncMap_.emplace(cmdCode, setIpcRequestFunc); 75 }; RegisterReadResponseFunc(int32_t cmdCode,ReadResponseFunc readResponseFunc)76 void RegisterReadResponseFunc(int32_t cmdCode, ReadResponseFunc readResponseFunc) 77 { 78 readResponseFuncMap_.emplace(cmdCode, readResponseFunc); 79 }; RegisterCmdProcessFunc(int32_t cmdCode,OnIpcCmdFunc onIpcCmdFunc)80 void RegisterCmdProcessFunc(int32_t cmdCode, OnIpcCmdFunc onIpcCmdFunc) 81 { 82 onIpcCmdFuncMap_.emplace(cmdCode, onIpcCmdFunc); 83 }; 84 int32_t SetRequest(int32_t cmdCode, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data); 85 int32_t ReadResponse(int32_t cmdCode, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp); 86 int32_t OnIpcCmd(int32_t cmdCode, MessageParcel &data, MessageParcel &reply); 87 private: 88 std::unordered_map<int32_t, SetIpcRequestFunc> setIpcRequestFuncMap_; 89 std::unordered_map<int32_t, ReadResponseFunc> readResponseFuncMap_; 90 std::unordered_map<int32_t, OnIpcCmdFunc> onIpcCmdFuncMap_; 91 }; 92 } // namespace DistributedHardware 93 } // namespace OHOS 94 #endif // OHOS_DEVICE_MANAGER_IPC_CMD_PARSER_H