• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "single_instance.h"
26 namespace OHOS { class MessageParcel; }
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 #define ON_IPC_SET_REQUEST(cmdCode, paraA, paraB)                                                  \
31     static int32_t IpcSetRequest##cmdCode(paraA, paraB);                                           \
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)
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, paraB)                                                          \
53     static int32_t IpcCmdProcess##cmdCode(paraA, paraB);                                           \
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 int32_t IpcCmdProcess##cmdCode(paraA, paraB)
62 
63 using SetIpcRequestFunc = int32_t (*)(std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data);
64 using ReadResponseFunc = int32_t (*)(MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp);
65 using OnIpcCmdFunc = int32_t (*)(MessageParcel &data, MessageParcel &reply);
66 
67 class IpcCmdRegister {
68     DECLARE_SINGLE_INSTANCE(IpcCmdRegister);
69 
70 public:
71     /**
72      * @tc.name: IpcCmdRegister::RegisterSetRequestFunc
73      * @tc.desc: RegisterSetRequestFunc of the Ipc Cmd Register
74      * @tc.type: FUNC
75      */
RegisterSetRequestFunc(int32_t cmdCode,SetIpcRequestFunc setIpcRequestFunc)76     void RegisterSetRequestFunc(int32_t cmdCode, SetIpcRequestFunc setIpcRequestFunc)
77     {
78         setIpcRequestFuncMap_.emplace(cmdCode, setIpcRequestFunc);
79     };
80 
81     /**
82      * @tc.name: IpcCmdRegister::RegisterReadResponseFunc
83      * @tc.desc: RegisterReadResponseFunc of the Ipc Cmd Register
84      * @tc.type: FUNC
85      */
RegisterReadResponseFunc(int32_t cmdCode,ReadResponseFunc readResponseFunc)86     void RegisterReadResponseFunc(int32_t cmdCode, ReadResponseFunc readResponseFunc)
87     {
88         readResponseFuncMap_.emplace(cmdCode, readResponseFunc);
89     };
90 
91     /**
92      * @tc.name: IpcCmdRegister::RegisterCmdProcessFunc
93      * @tc.desc: RegisterCmdProcessFunc of the Ipc Cmd Register
94      * @tc.type: FUNC
95      */
RegisterCmdProcessFunc(int32_t cmdCode,OnIpcCmdFunc onIpcCmdFunc)96     void RegisterCmdProcessFunc(int32_t cmdCode, OnIpcCmdFunc onIpcCmdFunc)
97     {
98         onIpcCmdFuncMap_.emplace(cmdCode, onIpcCmdFunc);
99     };
100 
101     /**
102      * @tc.name: IpcCmdRegister::RegisterSetRequestFunc
103      * @tc.desc: RegisterSetRequestFunc of the Ipc Cmd Register
104      * @tc.type: FUNC
105      */
106     int32_t SetRequest(int32_t cmdCode, std::shared_ptr<IpcReq> pBaseReq, MessageParcel &data);
107 
108     /**
109      * @tc.name: IpcCmdRegister::ReadResponse
110      * @tc.desc: ReadResponse of the Ipc Cmd Register
111      * @tc.type: FUNC
112      */
113     int32_t ReadResponse(int32_t cmdCode, MessageParcel &reply, std::shared_ptr<IpcRsp> pBaseRsp);
114 
115     /**
116      * @tc.name: IpcCmdRegister::OnIpcCmd
117      * @tc.desc: OnIpcCmd of the Ipc Cmd Register
118      * @tc.type: FUNC
119      */
120     int32_t OnIpcCmd(int32_t cmdCode, MessageParcel &data, MessageParcel &reply);
121 
122 private:
123     std::unordered_map<int32_t, SetIpcRequestFunc> setIpcRequestFuncMap_;
124     std::unordered_map<int32_t, ReadResponseFunc> readResponseFuncMap_;
125     std::unordered_map<int32_t, OnIpcCmdFunc> onIpcCmdFuncMap_;
126 };
127 } // namespace DistributedHardware
128 } // namespace OHOS
129 #endif // OHOS_DM_IPC_CMD_PARSER_H
130