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_DM_IPC_CMD_PARSER_H 17 #define OHOS_DM_IPC_CMD_PARSER_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <unordered_map> 22 23 #include "ipc_req.h" 24 #include "ipc_rsp.h" 25 #include "liteipc_adapter.h" 26 #include "single_instance.h" 27 28 namespace OHOS { 29 namespace DistributedHardware { 30 #define ON_IPC_SET_REQUEST(cmdCode, paraA, paraB, paraC, paraD) \ 31 static int32_t IpcSetRequest##cmdCode(paraA, paraB, paraC, paraD); \ 32 struct IpcRegisterSetRequestFunc##cmdCode { \ 33 IpcRegisterSetRequestFunc##cmdCode() \ 34 { \ 35 IpcCmdRegister::GetInstance().RegisterSetRequestFunc(cmdCode, IpcSetRequest##cmdCode); \ 36 } \ 37 }; \ 38 IpcRegisterSetRequestFunc##cmdCode g_IpcRegisterSetRequestFunc##cmdCode; \ 39 static int32_t IpcSetRequest##cmdCode(paraA, paraB, paraC, paraD) 40 41 #define ON_IPC_READ_RESPONSE(cmdCode, paraA, paraB) \ 42 static int32_t IpcReadResponse##cmdCode(paraA, paraB); \ 43 struct IpcRegisterReadResponseFunc##cmdCode { \ 44 IpcRegisterReadResponseFunc##cmdCode() \ 45 { \ 46 IpcCmdRegister::GetInstance().RegisterReadResponseFunc(cmdCode, IpcReadResponse##cmdCode); \ 47 } \ 48 }; \ 49 IpcRegisterReadResponseFunc##cmdCode g_IpcRegisterReadResponseFunc##cmdCode; \ 50 static int32_t IpcReadResponse##cmdCode(paraA, paraB) 51 52 #define ON_IPC_CMD(cmdCode, paraA) \ 53 static void IpcCmdProcess##cmdCode(paraA); \ 54 struct IpcRegisterCmdProcessFunc##cmdCode { \ 55 IpcRegisterCmdProcessFunc##cmdCode() \ 56 { \ 57 IpcCmdRegister::GetInstance().RegisterCmdProcessFunc(cmdCode, IpcCmdProcess##cmdCode); \ 58 } \ 59 }; \ 60 IpcRegisterCmdProcessFunc##cmdCode g_IpcRegisterCmdProcessFunc##cmdCode; \ 61 static void IpcCmdProcess##cmdCode(paraA) 62 63 #define ON_IPC_SERVER_CMD(cmdCode, paraA, paraB) \ 64 static void IpcServerCmdProcess##cmdCode(paraA, paraB); \ 65 class IpcRegisterServerCmdProcessFunc##cmdCode { \ 66 public: \ 67 IpcRegisterServerCmdProcessFunc##cmdCode() \ 68 { \ 69 IpcCmdRegister::GetInstance().RegisterServerCmdProcessFunc(cmdCode, IpcServerCmdProcess##cmdCode); \ 70 } \ 71 }; \ 72 IpcRegisterServerCmdProcessFunc##cmdCode g_IpcRegisterServerCmdProcessFunc##cmdCode; \ 73 static void IpcServerCmdProcess##cmdCode(paraA, paraB) 74 75 using SetIpcRequestFunc = int32_t (*)(std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer, 76 size_t bufferLen); 77 using ReadResponseFunc = int32_t (*)(IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp); 78 using OnIpcCmdFunc = void (*)(IpcIo &reply); 79 using OnIpcServerCmdFunc = void (*)(IpcIo &req, IpcIo &reply); 80 81 class IpcCmdRegister { 82 DECLARE_SINGLE_INSTANCE(IpcCmdRegister); 83 84 public: RegisterSetRequestFunc(int32_t cmdCode,SetIpcRequestFunc setIpcRequestFunc)85 void RegisterSetRequestFunc(int32_t cmdCode, SetIpcRequestFunc setIpcRequestFunc) 86 { 87 setIpcRequestFuncMap_.emplace(cmdCode, setIpcRequestFunc); 88 }; RegisterReadResponseFunc(int32_t cmdCode,ReadResponseFunc readResponseFunc)89 void RegisterReadResponseFunc(int32_t cmdCode, ReadResponseFunc readResponseFunc) 90 { 91 readResponseFuncMap_.emplace(cmdCode, readResponseFunc); 92 }; RegisterCmdProcessFunc(int32_t cmdCode,OnIpcCmdFunc onIpcCmdFunc)93 void RegisterCmdProcessFunc(int32_t cmdCode, OnIpcCmdFunc onIpcCmdFunc) 94 { 95 onIpcCmdFuncMap_.emplace(cmdCode, onIpcCmdFunc); 96 }; RegisterServerCmdProcessFunc(int32_t cmdCode,OnIpcServerCmdFunc onIpcServerCmdFunc)97 void RegisterServerCmdProcessFunc(int32_t cmdCode, OnIpcServerCmdFunc onIpcServerCmdFunc) 98 { 99 onIpcServerCmdFuncMap_.emplace(cmdCode, onIpcServerCmdFunc); 100 }; 101 int32_t SetRequest(int32_t cmdCode, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer, 102 size_t buffLen); 103 int32_t ReadResponse(int32_t cmdCode, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp); 104 int32_t OnIpcCmd(int32_t cmdCode, IpcIo &reply); 105 int32_t OnIpcServerCmd(int32_t cmdCode, IpcIo &req, IpcIo &reply); 106 107 private: 108 std::unordered_map<int32_t, SetIpcRequestFunc> setIpcRequestFuncMap_; 109 std::unordered_map<int32_t, ReadResponseFunc> readResponseFuncMap_; 110 std::unordered_map<int32_t, OnIpcCmdFunc> onIpcCmdFuncMap_; 111 std::unordered_map<int32_t, OnIpcServerCmdFunc> onIpcServerCmdFuncMap_; 112 }; 113 } // namespace DistributedHardware 114 } // namespace OHOS 115 #endif // OHOS_DM_IPC_CMD_PARSER_H