• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ipc_types.h"
26 #include "iremote_broker.h"
27 #include "single_instance.h"
28 
29 namespace OHOS {
30 namespace DistributedHardware {
31 #define ON_IPC_SET_REQUEST(cmdCode, paraA, paraB)                                                  \
32     static int32_t IpcSetRequest##cmdCode(paraA, paraB);                                           \
33     struct IpcRegisterSetRequestFunc##cmdCode {                                                    \
34         IpcRegisterSetRequestFunc##cmdCode()                                                       \
35         {                                                                                          \
36             IpcCmdRegister::GetInstance().RegisterSetRequestFunc(cmdCode, IpcSetRequest##cmdCode); \
37         }                                                                                          \
38     };                                                                                             \
39     IpcRegisterSetRequestFunc##cmdCode g_IpcRegisterSetRequestFunc##cmdCode;                       \
40     static int32_t IpcSetRequest##cmdCode(paraA, paraB)
41 
42 #define ON_IPC_READ_RESPONSE(cmdCode, paraA, paraB)                                                    \
43     static int32_t IpcReadResponse##cmdCode(paraA, paraB);                                             \
44     struct IpcRegisterReadResponseFunc##cmdCode {                                                      \
45         IpcRegisterReadResponseFunc##cmdCode()                                                         \
46         {                                                                                              \
47             IpcCmdRegister::GetInstance().RegisterReadResponseFunc(cmdCode, IpcReadResponse##cmdCode); \
48         }                                                                                              \
49     };                                                                                                 \
50     IpcRegisterReadResponseFunc##cmdCode g_IpcRegisterReadResponseFunc##cmdCode;                       \
51     static int32_t IpcReadResponse##cmdCode(paraA, paraB)
52 
53 #define ON_IPC_CMD(cmdCode, paraA, paraB)                                                          \
54     static int32_t IpcCmdProcess##cmdCode(paraA, paraB);                                           \
55     struct IpcRegisterCmdProcessFunc##cmdCode {                                                    \
56         IpcRegisterCmdProcessFunc##cmdCode()                                                       \
57         {                                                                                          \
58             IpcCmdRegister::GetInstance().RegisterCmdProcessFunc(cmdCode, IpcCmdProcess##cmdCode); \
59         }                                                                                          \
60     };                                                                                             \
61     IpcRegisterCmdProcessFunc##cmdCode g_IpcRegisterCmdProcessFunc##cmdCode;                       \
62     static int32_t IpcCmdProcess##cmdCode(paraA, paraB)
63 
64 using SetIpcRequestFunc = int32_t (*)(std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data);
65 using ReadResponseFunc = int32_t (*)(MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp);
66 using OnIpcCmdFunc = int32_t (*)(MessageParcel &data, MessageParcel &reply);
67 
68 class IpcCmdRegister {
69     DECLARE_SINGLE_INSTANCE(IpcCmdRegister);
70 
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 
88 private:
89     std::unordered_map<int32_t, SetIpcRequestFunc> setIpcRequestFuncMap_;
90     std::unordered_map<int32_t, ReadResponseFunc> readResponseFuncMap_;
91     std::unordered_map<int32_t, OnIpcCmdFunc> onIpcCmdFuncMap_;
92 };
93 } // namespace DistributedHardware
94 } // namespace OHOS
95 #endif // OHOS_DM_IPC_CMD_PARSER_H