• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 constexpr int32_t ARGV_VALID = 2;
32 
33 class InjectionEventDispatch {
34 public:
35     InjectionEventDispatch() = default;
36     ~InjectionEventDispatch() = default;
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);
45     int32_t GetDevIndexType(int32_t devType);
46     int32_t GetDeviceIndex(const std::string& deviceNameText);
47     std::string GetFunId();
48     bool VirifyArgvs(const int32_t& argc, const std::vector<std::string>& argv);
RegistInjectEvent(InjectFunctionMap & msg)49     bool RegistInjectEvent(InjectFunctionMap& msg)
50     {
51         auto it = injectFuns_.find(msg.id);
52         if (it != injectFuns_.end()) {
53             return false;
54         }
55         injectFuns_[msg.id] = msg.fun;
56         return true;
57     }
58 
GetFun(std::string id)59     InjectFunction* GetFun(std::string id)
60     {
61         auto it = injectFuns_.find(id);
62         if (it == injectFuns_.end()) {
63             return nullptr;
64         }
65         return &it->second;
66     }
67 private:
68     std::string funId_ = "";
69     int32_t argvNum_ = 0;
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 private:
76     static constexpr uint32_t SEND_EVENT_ARGV_COUNTS = 5;
77     static constexpr uint32_t SEND_EVENT_DEV_NODE_INDEX = 1;
78     static constexpr uint32_t SEND_EVENT_TYPE_INDEX = 2;
79     static constexpr uint32_t SEND_EVENT_CODE_INDEX = 3;
80     static constexpr uint32_t SEND_EVENT_VALUE_INDEX = 4;
81     static constexpr int32_t ARGVS_TARGET_INDEX = 1;
82     static constexpr int32_t ARGVS_CODE_INDEX = 2;
83     static constexpr int32_t SEND_EVENT_TO_DEVICE = 0;
84     static constexpr int32_t JSON_FILE_PATH_INDEX = 1;
85 };
86 } // namespace MMI
87 } // namespace OHOS
88 #endif // INJECTION_EVENT_DISPATCH_H