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 <unordered_map>
22 
23 #include "ipc_req.h"
24 #include "ipc_rsp.h"
25 #include "ipc_skeleton.h"
26 #include "dm_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     DM_DECLARE_SINGLE_INSTANCE(IpcCmdRegister);
83 
84 public:
85     /**
86      * @tc.name: IpcCmdRegister::RegisterSetRequestFunc
87      * @tc.desc: Register Set Request Func of the Ipc Cmd Register
88      * @tc.type: FUNC
89      */
RegisterSetRequestFunc(int32_t cmdCode,SetIpcRequestFunc setIpcRequestFunc)90     void RegisterSetRequestFunc(int32_t cmdCode, SetIpcRequestFunc setIpcRequestFunc)
91     {
92         setIpcRequestFuncMap_.emplace(cmdCode, setIpcRequestFunc);
93     };
94 
95     /**
96      * @tc.name: IpcCmdRegister::RegisterReadResponseFunc
97      * @tc.desc: Register Read Response Func of the Ipc Cmd Register
98      * @tc.type: FUNC
99      */
RegisterReadResponseFunc(int32_t cmdCode,ReadResponseFunc readResponseFunc)100     void RegisterReadResponseFunc(int32_t cmdCode, ReadResponseFunc readResponseFunc)
101     {
102         readResponseFuncMap_.emplace(cmdCode, readResponseFunc);
103     };
104 
105     /**
106      * @tc.name: IpcCmdRegister::RegisterCmdProcessFunc
107      * @tc.desc: Register Cmd Process Func of the Ipc Cmd Register
108      * @tc.type: FUNC
109      */
RegisterCmdProcessFunc(int32_t cmdCode,OnIpcCmdFunc onIpcCmdFunc)110     void RegisterCmdProcessFunc(int32_t cmdCode, OnIpcCmdFunc onIpcCmdFunc)
111     {
112         onIpcCmdFuncMap_.emplace(cmdCode, onIpcCmdFunc);
113     };
114 
115     /**
116      * @tc.name: IpcCmdRegister::RegisterServerCmdProcessFunc
117      * @tc.desc: Register Server Cmd Process Func of the Ipc Cmd Register
118      * @tc.type: FUNC
119      */
RegisterServerCmdProcessFunc(int32_t cmdCode,OnIpcServerCmdFunc onIpcServerCmdFunc)120     void RegisterServerCmdProcessFunc(int32_t cmdCode, OnIpcServerCmdFunc onIpcServerCmdFunc)
121     {
122         onIpcServerCmdFuncMap_.emplace(cmdCode, onIpcServerCmdFunc);
123     };
124 
125     /**
126      * @tc.name: IpcCmdRegister::SetRequest
127      * @tc.desc: Set Request of the Ipc Cmd Register
128      * @tc.type: FUNC
129      */
130     int32_t SetRequest(int32_t cmdCode, std::shared_ptr<IpcReq> pBaseReq, IpcIo &request, uint8_t *buffer,
131                        size_t buffLen);
132 
133     /**
134      * @tc.name: IpcCmdRegister::ReadResponse
135      * @tc.desc: Read Response of the Ipc Cmd Register
136      * @tc.type: FUNC
137      */
138     int32_t ReadResponse(int32_t cmdCode, IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp);
139 
140     /**
141      * @tc.name: IpcCmdRegister::OnIpcCmd
142      * @tc.desc: On Ipc Cmd of the Ipc Cmd Register
143      * @tc.type: FUNC
144      */
145     int32_t OnIpcCmd(int32_t cmdCode, IpcIo &reply);
146 
147     /**
148      * @tc.name: IpcCmdRegister::OnIpcServerCmd
149      * @tc.desc: On Ipc Server Cmd of the Ipc Cmd Register
150      * @tc.type: FUNC
151      */
152     int32_t OnIpcServerCmd(int32_t cmdCode, IpcIo &req, IpcIo &reply);
153 
154 private:
155     std::unordered_map<int32_t, SetIpcRequestFunc> setIpcRequestFuncMap_;
156     std::unordered_map<int32_t, ReadResponseFunc> readResponseFuncMap_;
157     std::unordered_map<int32_t, OnIpcCmdFunc> onIpcCmdFuncMap_;
158     std::unordered_map<int32_t, OnIpcServerCmdFunc> onIpcServerCmdFuncMap_;
159 };
160 } // namespace DistributedHardware
161 } // namespace OHOS
162 #endif // OHOS_DM_IPC_CMD_PARSER_H
163