1 /* 2 * Copyright (c) 2022-2024 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 <mutex> 22 #include <unordered_map> 23 24 #include "device_manager_ipc_interface_code.h" 25 #include "ipc_req.h" 26 #include "ipc_rsp.h" 27 #include "dm_single_instance.h" 28 namespace OHOS { class MessageParcel; } 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 DM_DECLARE_SINGLE_INSTANCE(IpcCmdRegister); 71 72 public: 73 /** 74 * @tc.name: IpcCmdRegister::RegisterSetRequestFunc 75 * @tc.desc: RegisterSetRequestFunc of the Ipc Cmd Register 76 * @tc.type: FUNC 77 */ RegisterSetRequestFunc(int32_t cmdCode,SetIpcRequestFunc setIpcRequestFunc)78 void RegisterSetRequestFunc(int32_t cmdCode, SetIpcRequestFunc setIpcRequestFunc) 79 { 80 std::lock_guard<std::mutex> autoLock(setIpcRequestFuncMapLock_); 81 setIpcRequestFuncMap_.emplace(cmdCode, setIpcRequestFunc); 82 }; 83 84 /** 85 * @tc.name: IpcCmdRegister::RegisterReadResponseFunc 86 * @tc.desc: RegisterReadResponseFunc of the Ipc Cmd Register 87 * @tc.type: FUNC 88 */ RegisterReadResponseFunc(int32_t cmdCode,ReadResponseFunc readResponseFunc)89 void RegisterReadResponseFunc(int32_t cmdCode, ReadResponseFunc readResponseFunc) 90 { 91 std::lock_guard<std::mutex> autoLock(readResponseFuncMapLock_); 92 readResponseFuncMap_.emplace(cmdCode, readResponseFunc); 93 }; 94 95 /** 96 * @tc.name: IpcCmdRegister::RegisterCmdProcessFunc 97 * @tc.desc: RegisterCmdProcessFunc of the Ipc Cmd Register 98 * @tc.type: FUNC 99 */ RegisterCmdProcessFunc(int32_t cmdCode,OnIpcCmdFunc onIpcCmdFunc)100 void RegisterCmdProcessFunc(int32_t cmdCode, OnIpcCmdFunc onIpcCmdFunc) 101 { 102 std::lock_guard<std::mutex> autoLock(onIpcCmdFuncMapLock_); 103 onIpcCmdFuncMap_.emplace(cmdCode, onIpcCmdFunc); 104 }; 105 106 /** 107 * @tc.name: IpcCmdRegister::RegisterSetRequestFunc 108 * @tc.desc: RegisterSetRequestFunc of the Ipc Cmd Register 109 * @tc.type: FUNC 110 */ 111 int32_t SetRequest(int32_t cmdCode, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data); 112 113 /** 114 * @tc.name: IpcCmdRegister::ReadResponse 115 * @tc.desc: ReadResponse of the Ipc Cmd Register 116 * @tc.type: FUNC 117 */ 118 int32_t ReadResponse(int32_t cmdCode, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp); 119 120 /** 121 * @tc.name: IpcCmdRegister::OnIpcCmd 122 * @tc.desc: OnIpcCmd of the Ipc Cmd Register 123 * @tc.type: FUNC 124 */ 125 int32_t OnIpcCmd(int32_t cmdCode, MessageParcel &data, MessageParcel &reply); 126 127 private: 128 std::mutex setIpcRequestFuncMapLock_; 129 std::unordered_map<int32_t, SetIpcRequestFunc> setIpcRequestFuncMap_; 130 std::mutex readResponseFuncMapLock_; 131 std::unordered_map<int32_t, ReadResponseFunc> readResponseFuncMap_; 132 std::mutex onIpcCmdFuncMapLock_; 133 std::unordered_map<int32_t, OnIpcCmdFunc> onIpcCmdFuncMap_; 134 }; 135 } // namespace DistributedHardware 136 } // namespace OHOS 137 #endif // OHOS_DM_IPC_CMD_PARSER_H 138