• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 COMMANDLINE_H
17 #define COMMANDLINE_H
18 
19 #include <set>
20 #include <vector>
21 #include "JsonReader.h"
22 #include "LocalSocket.h"
23 
24 class CommandLine {
25 public:
26     enum class CommandType { SET = 0, GET, ACTION, INVALID };
27 
28     CommandLine(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
29     virtual ~CommandLine();
30     void CheckAndRun();
31     void SetCommandResult(const std::string& type, const Json2::Value& resultContent);
32     void SetResultToManager(const std::string& type, const Json2::Value& resultContent, const std::string& messageType);
33     void RunAndSendResultToManager();
34     void SendResultToManager();
35     void SendResult();
RunSet()36     virtual void RunSet() {}
37     bool IsArgValid() const;
38     uint8_t ToUint8(std::string str) const;
39     void SetCommandName(std::string command);
40 
41 protected:
42     const Json2::Value& args;
43     const LocalSocket& cliSocket;
44     Json2::Value commandResult = JsonReader::CreateObject();
45     Json2::Value commandResultToManager = JsonReader::CreateObject();
46     CommandType type;
47     std::string commandName;
48     const std::vector<std::string> liteSupportedLanguages = {"zh-CN", "en-US"};
49     const std::vector<std::string> richSupportedLanguages = {
50         "zh_CN", "zh_HK", "zh_TW", "en_US", "en_GB", "ar_AE", "bg_BG", "bo_CN", "cs_CZ", "da_DK",
51         "de_DE", "el_GR", "en_PH", "es_ES", "es_LA", "fi_FI", "fr_FR", "he_IL", "hi_IN", "hu_HU",
52         "id_ID", "it_IT", "ja_JP", "kk_KZ", "ms_MY", "nl_NL", "no_NO", "pl_PL", "pt_BR", "pt_PT",
53         "ro_RO", "ru_RU", "sr_RS", "sv_SE", "th_TH", "tr_TR", "ug_CN", "uk_UA", "vi_VN"
54     };
55     const std::vector<std::string> LoadDocDevs = {"phone", "tablet", "wearable", "car", "tv", "2in1", "default"};
56     const int maxWidth = 3000;
57     const int minWidth = 50;
58     const int maxDpi = 640;
59     const int minDpi = 120;
60     const int maxKeyVal = 2119;
61     const int minKeyVal = 2000;
62     const int maxActionVal = 2;
63     const int minActionVal = 0;
64     const int maxLoadDocWidth = 3000;
65     const int minLoadDocWidth = 20;
66 
IsSetArgValid()67     virtual bool IsSetArgValid() const
68     {
69         return true;
70     }
IsGetArgValid()71     virtual bool IsGetArgValid() const
72     {
73         return true;
74     }
IsActionArgValid()75     virtual bool IsActionArgValid() const
76     {
77         return true;
78     }
RunGet()79     virtual void RunGet() {}
RunAction()80     virtual void RunAction() {}
81 
82     bool IsBoolType(std::string arg) const;
83     bool IsIntType(std::string arg) const;
84     bool IsOneDigitFloatType(std::string arg, bool allowNegativeNumber) const;
85 
86 private:
87     void Run();
88 };
89 
90 class TouchAndMouseCommand {
91 protected:
92     struct EventParams {
93         double x;
94         double y;
95         int type;
96         int button;
97         int action;
98         int sourceType;
99         int sourceTool;
100         std::set<int> pressedBtnsVec;
101         std::vector<double> axisVec; // 13 is array size
102         std::string name;
103     };
104     void SetEventParams(EventParams& params);
105 };
106 
107 class TouchPressCommand : public CommandLine, public TouchAndMouseCommand {
108 public:
109     TouchPressCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~TouchPressCommand()110     ~TouchPressCommand() override {}
111 
112 protected:
113     void RunAction() override;
114     bool IsActionArgValid() const override;
115 };
116 
117 class TouchMoveCommand : public CommandLine, public TouchAndMouseCommand {
118 public:
119     TouchMoveCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~TouchMoveCommand()120     ~TouchMoveCommand() override {}
121 
122 protected:
123     void RunAction() override;
124     bool IsActionArgValid() const override;
125 };
126 
127 class TouchReleaseCommand : public CommandLine, public TouchAndMouseCommand {
128 public:
129     TouchReleaseCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~TouchReleaseCommand()130     ~TouchReleaseCommand() override {}
131 
132 protected:
133     void RunAction() override;
134     bool IsActionArgValid() const override;
135 };
136 
137 class MouseWheelCommand : public CommandLine {
138 public:
139     MouseWheelCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~MouseWheelCommand()140     ~MouseWheelCommand() override {}
141 
142 protected:
143     void RunAction() override;
144     bool IsActionArgValid() const override;
145 };
146 
147 class BackClickedCommand : public CommandLine {
148 public:
149     BackClickedCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~BackClickedCommand()150     ~BackClickedCommand() override {}
151 
152 protected:
153     void RunAction() override;
154 };
155 
156 class RestartCommand : public CommandLine {
157 public:
158     RestartCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~RestartCommand()159     ~RestartCommand() override {}
160 
161 protected:
162     void RunAction() override;
163 };
164 
165 class PowerCommand : public CommandLine {
166 public:
167     PowerCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~PowerCommand()168     ~PowerCommand() override {}
169     void RunSet() override;
170 
171 protected:
172     void RunGet() override;
173     bool IsSetArgValid() const override;
174 };
175 
176 class VolumeCommand : public CommandLine {
177 public:
178     VolumeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~VolumeCommand()179     ~VolumeCommand() override {}
180     void RunSet() override;
181 
182 protected:
183     void RunGet() override;
184     bool IsSetArgValid() const override;
185 };
186 
187 class BarometerCommand : public CommandLine {
188 public:
189     BarometerCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~BarometerCommand()190     ~BarometerCommand() override {}
191     void RunSet() override;
192 
193 protected:
194     void RunGet() override;
195     bool IsSetArgValid() const override;
196 };
197 
198 class ResolutionSwitchCommand : public CommandLine {
199 public:
200     ResolutionSwitchCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~ResolutionSwitchCommand()201     ~ResolutionSwitchCommand() override {}
202     void RunSet() override;
203 
204 protected:
205     bool IsSetArgValid() const override;
206     bool IsIntValValid() const;
207 };
208 
209 class OrientationCommand : public CommandLine {
210 public:
211     OrientationCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~OrientationCommand()212     ~OrientationCommand() override {}
213     void RunSet() override;
214 
215 protected:
216     bool IsSetArgValid() const override;
217 };
218 
219 class ColorModeCommand : public CommandLine {
220 public:
221     ColorModeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~ColorModeCommand()222     ~ColorModeCommand() override {}
223     void RunSet() override;
224 
225 protected:
226     bool IsSetArgValid() const override;
227 };
228 
229 class LanguageCommand : public CommandLine {
230 public:
231     LanguageCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~LanguageCommand()232     ~LanguageCommand() override {}
233     void RunSet() override;
234 
235 protected:
236     void RunGet() override;
237     bool IsSetArgValid() const override;
238 };
239 
240 class FontSelectCommand : public CommandLine {
241 public:
242     FontSelectCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~FontSelectCommand()243     ~FontSelectCommand() override {}
244     void RunSet() override;
245 
246 protected:
247     bool IsSetArgValid() const override;
248 };
249 
250 class MemoryRefreshCommand : public CommandLine {
251 public:
252     MemoryRefreshCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~MemoryRefreshCommand()253     ~MemoryRefreshCommand() override {}
254     void RunSet() override;
255 
256 protected:
257     bool IsSetArgValid() const override;
258 };
259 
260 class LoadDocumentCommand : public CommandLine {
261 public:
262     LoadDocumentCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~LoadDocumentCommand()263     ~LoadDocumentCommand() override {}
264     void RunSet() override;
265 
266 protected:
267     bool IsSetArgValid() const override;
268     bool IsIntValValid(const Json2::Value& previewParam) const;
269     bool IsStrValVailid(const Json2::Value& previewParam) const;
270 };
271 
272 class ReloadRuntimePageCommand : public CommandLine {
273 public:
274     ReloadRuntimePageCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~ReloadRuntimePageCommand()275     ~ReloadRuntimePageCommand() override {}
276     void RunSet() override;
277 
278 protected:
279     bool IsSetArgValid() const override;
280 };
281 
282 class CurrentRouterCommand : public CommandLine {
283 public:
284     CurrentRouterCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~CurrentRouterCommand()285     ~CurrentRouterCommand() override {}
286 
287 protected:
288     void RunGet() override;
289 };
290 
291 class LoadContentCommand : public CommandLine {
292 public:
293     LoadContentCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~LoadContentCommand()294     ~LoadContentCommand() override {}
295 
296 protected:
297     void RunGet() override;
298 };
299 
300 class SupportedLanguagesCommand : public CommandLine {
301 public:
302     SupportedLanguagesCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~SupportedLanguagesCommand()303     ~SupportedLanguagesCommand() override {}
304 
305 protected:
306     void RunGet() override;
307 };
308 
309 class LocationCommand : public CommandLine {
310 public:
311     LocationCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~LocationCommand()312     ~LocationCommand() override {}
313     void RunSet() override;
314 
315 protected:
316     void RunGet() override;
317     bool IsSetArgValid() const override;
318 };
319 
320 class DistributedCommunicationsCommand : public CommandLine {
321 public:
322     DistributedCommunicationsCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~DistributedCommunicationsCommand()323     ~DistributedCommunicationsCommand() override {}
324 
325 protected:
326     void RunAction() override;
327     bool IsActionArgValid() const override;
328     std::vector<char> StringToCharVector(std::string str) const;
329 };
330 
331 class KeepScreenOnStateCommand : public CommandLine {
332 public:
333     KeepScreenOnStateCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~KeepScreenOnStateCommand()334     ~KeepScreenOnStateCommand() override {}
335     void RunSet() override;
336 
337 protected:
338     void RunGet() override;
339     bool IsSetArgValid() const override;
340 };
341 
342 class WearingStateCommand : public CommandLine {
343 public:
344     WearingStateCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~WearingStateCommand()345     ~WearingStateCommand() override {}
346     void RunSet() override;
347 
348 protected:
349     void RunGet() override;
350     bool IsSetArgValid() const override;
351 };
352 
353 class BrightnessModeCommand : public CommandLine {
354 public:
355     BrightnessModeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~BrightnessModeCommand()356     ~BrightnessModeCommand() override {}
357     void RunSet() override;
358 
359 protected:
360     void RunGet() override;
361     bool IsSetArgValid() const override;
362 };
363 
364 class ChargeModeCommand : public CommandLine {
365 public:
366     ChargeModeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~ChargeModeCommand()367     ~ChargeModeCommand() override {}
368     void RunSet() override;
369 
370 protected:
371     void RunGet() override;
372     bool IsSetArgValid() const override;
373 };
374 
375 class BrightnessCommand : public CommandLine {
376 public:
377     BrightnessCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~BrightnessCommand()378     ~BrightnessCommand() override {}
379     void RunSet() override;
380 
381 protected:
382     void RunGet() override;
383     bool IsSetArgValid() const override;
384 };
385 
386 class HeartRateCommand : public CommandLine {
387 public:
388     HeartRateCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~HeartRateCommand()389     ~HeartRateCommand() override {}
390     void RunSet() override;
391 
392 protected:
393     void RunGet() override;
394     bool IsSetArgValid() const override;
395 };
396 
397 class StepCountCommand : public CommandLine {
398 public:
399     StepCountCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~StepCountCommand()400     ~StepCountCommand() override {}
401     void RunSet() override;
402 
403 protected:
404     void RunGet() override;
405     bool IsSetArgValid() const override;
406 };
407 
408 class ExitCommand : public CommandLine {
409 public:
410     ExitCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~ExitCommand()411     ~ExitCommand() override {}
412 
413 protected:
414     void RunAction() override;
415 };
416 
417 class InspectorJSONTree : public CommandLine {
418 public:
419     InspectorJSONTree(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~InspectorJSONTree()420     ~InspectorJSONTree() override {}
421 
422 protected:
423     void RunAction() override;
424 };
425 
426 class InspectorDefault : public CommandLine {
427 public:
428     InspectorDefault(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~InspectorDefault()429     ~InspectorDefault() override {}
430 
431 protected:
432     void RunAction() override;
433 };
434 
435 class DeviceTypeCommand : public CommandLine {
436 public:
437     DeviceTypeCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~DeviceTypeCommand()438     ~DeviceTypeCommand() override {}
439 
440 protected:
441     void RunSet() override;
442 };
443 
444 class ResolutionCommand : public CommandLine {
445 public:
446     ResolutionCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~ResolutionCommand()447     ~ResolutionCommand() override {}
448 
449 protected:
450     void RunSet() override;
451 };
452 
453 class FastPreviewMsgCommand : public CommandLine {
454 public:
455     FastPreviewMsgCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~FastPreviewMsgCommand()456     ~FastPreviewMsgCommand() override {}
457 
458 protected:
459     void RunGet() override;
460 };
461 
462 class DropFrameCommand : public CommandLine {
463 public:
464     DropFrameCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~DropFrameCommand()465     ~DropFrameCommand() override {}
466     void RunSet() override;
467 
468 protected:
469     bool IsSetArgValid() const override;
470 };
471 
472 class KeyPressCommand : public CommandLine {
473 public:
474     KeyPressCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~KeyPressCommand()475     ~KeyPressCommand() override {}
476 
477 protected:
478     void RunAction() override;
479     bool IsActionArgValid() const override;
480     bool IsImeArgsValid() const;
481     bool IsKeyArgsValid() const;
482 };
483 
484 class PointEventCommand : public CommandLine, public TouchAndMouseCommand {
485 public:
486     PointEventCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~PointEventCommand()487     ~PointEventCommand() override {}
488 
489 protected:
490     void RunAction() override;
491     bool IsActionArgValid() const override;
492     bool IsArgsExist() const;
493     bool IsArgsValid() const;
494 };
495 
496 class FoldStatusCommand : public CommandLine {
497 public:
498     FoldStatusCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~FoldStatusCommand()499     ~FoldStatusCommand() override {}
500 
501 protected:
502     void RunSet() override;
503     bool IsSetArgValid() const override;
504 };
505 
506 class AvoidAreaCommand : public CommandLine {
507 public:
508     AvoidAreaCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~AvoidAreaCommand()509     ~AvoidAreaCommand() override {}
510 
511 protected:
512     void RunSet() override;
513     bool IsSetArgValid() const override;
514     bool IsObjectValid(const Json2::Value& val) const;
515 };
516 
517 class AvoidAreaChangedCommand : public CommandLine {
518 public:
519     AvoidAreaChangedCommand(CommandType commandType, const Json2::Value& arg, const LocalSocket& socket);
~AvoidAreaChangedCommand()520     ~AvoidAreaChangedCommand() override {}
521 
522 protected:
523     void RunGet() override;
524 };
525 #endif // COMMANDLINE_H
526