• 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_DEVICE_MANAGER_IPC_CMD_PARSER_H
17 #define OHOS_DEVICE_MANAGER_IPC_CMD_PARSER_H
18 
19 #include <cstdint>
20 #include <unordered_map>
21 #include <memory>
22 
23 #include "liteipc_adapter.h"
24 #include "single_instance.h"
25 
26 #include "ipc_req.h"
27 #include "ipc_rsp.h"
28 
29 namespace OHOS {
30 namespace DistributedHardware {
31 #define ON_IPC_SET_REQUEST(cmdCode, paraA, paraB, paraC, paraD) \
32     static int32_t IpcSetRequest##cmdCode(paraA, paraB, paraC, paraD); \
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, paraC, paraD) \
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) \
54     static void IpcCmdProcess##cmdCode(paraA); \
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 void IpcCmdProcess##cmdCode(paraA) \
63 
64 #define ON_IPC_SERVER_CMD(cmdCode, paraA, paraB) \
65     static void IpcServerCmdProcess##cmdCode(paraA, paraB); \
66     class IpcRegisterServerCmdProcessFunc##cmdCode { \
67     public: \
68         IpcRegisterServerCmdProcessFunc##cmdCode() \
69         { \
70             IpcCmdRegister::GetInstance().RegisterServerCmdProcessFunc(cmdCode, IpcServerCmdProcess##cmdCode); \
71         } \
72     }; \
73     IpcRegisterServerCmdProcessFunc##cmdCode g_IpcRegisterServerCmdProcessFunc##cmdCode; \
74     static void IpcServerCmdProcess##cmdCode(paraA, paraB)
75 
76 using SetIpcRequestFunc = int32_t (*)(std::shared_ptr<IpcReq> pBaseReq, IpcIo &request,
77     uint8_t *buffer, size_t bufferLen);
78 using ReadResponseFunc = int32_t (*)(IpcIo &reply, std::shared_ptr<IpcRsp> pBaseRsp);
79 using OnIpcCmdFunc = void (*)(IpcIo &reply);
80 using OnIpcServerCmdFunc = void (*)(IpcIo &req, IpcIo &reply);
81 
82 class IpcCmdRegister {
83 DECLARE_SINGLE_INSTANCE(IpcCmdRegister);
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,
102         uint8_t *buffer, 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 private:
107     std::unordered_map<int32_t, SetIpcRequestFunc> setIpcRequestFuncMap_;
108     std::unordered_map<int32_t, ReadResponseFunc> readResponseFuncMap_;
109     std::unordered_map<int32_t, OnIpcCmdFunc> onIpcCmdFuncMap_;
110     std::unordered_map<int32_t, OnIpcServerCmdFunc> onIpcServerCmdFuncMap_;
111 };
112 } // namespace DistributedHardware
113 } // namespace OHOS
114 #endif // OHOS_DEVICE_MANAGER_IPC_CMD_PARSER_H