1 /*
2  * Copyright (c) 2021-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 INJECTION_EVENT_DISPATCH_H
17 #define INJECTION_EVENT_DISPATCH_H
18 
19 #include "manage_inject_device.h"
20 #include "injection_tools_help_func.h"
21 
22 namespace OHOS {
23 namespace MMI {
24 using InjectFunction = std::function<int32_t()>;
25 
26 struct InjectFunctionMap {
27     std::string id;
28     InjectFunction fun;
29 };
30 
31 class InjectionEventDispatch {
32 public:
33     InjectionEventDispatch() = default;
34     ~InjectionEventDispatch() = default;
35     DISALLOW_COPY_AND_MOVE(InjectionEventDispatch);
36 
37     void Init();
38     void InitManageFunction();
39     void Run();
40     int32_t OnSendEvent();
41     int32_t OnJson();
42     int32_t OnHelp();
43     int32_t ExecuteFunction(std::string funId);
44     int32_t GetDevTypeIndex(int32_t devIndex) const;
45     int32_t GetDevIndexType(int32_t devType) const;
46     int32_t GetDeviceIndex(const std::string &deviceNameText) const;
47     void SetArgvs(const std::vector<std::string> &injectArgvs);
48     std::string GetFunId() const;
49     bool VerifyArgvs();
RegisterInjectEvent(InjectFunctionMap & msg)50     bool RegisterInjectEvent(InjectFunctionMap &msg)
51     {
52         auto it = injectFuns_.find(msg.id);
53         if (it != injectFuns_.end()) {
54             return false;
55         }
56         injectFuns_[msg.id] = msg.fun;
57         return true;
58     }
59 
GetFun(std::string id)60     InjectFunction* GetFun(std::string id)
61     {
62         auto it = injectFuns_.find(id);
63         if (it == injectFuns_.end()) {
64             return nullptr;
65         }
66         return &it->second;
67     }
68 private:
69     std::string funId_ { "" };
70     ManageInjectDevice manageInjectDevice_;
71     std::vector<std::string> injectArgvs_;
72     std::map<std::string, InjectFunction> injectFuns_;
73     std::map<std::string, int32_t> sendEventType_;
74     std::vector<DeviceInformation> allDevices_ = {};
75     bool CheckType(const std::string &inputType);
76     bool CheckCode(const std::string &inputCode);
77     bool CheckValue(const std::string &inputValue);
78     bool CheckEventValue(const std::string &inputType, const std::string &inputCode,
79     const std::string &inputValue);
80 };
81 } // namespace MMI
82 } // namespace OHOS
83 #endif // INJECTION_EVENT_DISPATCH_H