• 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 #include "translate.h"
16 #include "host_updater.h"
17 
18 namespace Hdc {
19 namespace TranslateCommand {
Usage()20     string Usage()
21     {
22         string ret = "";
23         ret = "\n                         OpenHarmony device connector(HDC) ...\n\n"
24               "---------------------------------global commands:----------------------------------\n"
25               " -h/help [verbose]                     - Print hdc help, 'verbose' for more other cmds\n"
26               " -v/version                            - Print hdc version\n"
27               " -t connectkey                         - Use device with given connect key\n"
28               "\n"
29               "---------------------------------component commands:-------------------------------\n"
30               "session commands(on server):\n"
31               " list targets [-v]                     - List all devices status, -v for detail\n"
32               " start [-r]                            - Start server. If with '-r', will be restart server\n"
33               " kill [-r]                             - Kill server. If with '-r', will be restart server\n"
34               "\n"
35               "service commands(on daemon):\n"
36               " target mount                          - Set /system /vendor partition read-write\n"
37               " wait                                  - Wait for the device to become available\n"
38               " target boot [-bootloader|-recovery]   - Reboot the device or boot into bootloader\\recovery.\n"
39               " target boot [MODE]                    - Reboot the into MODE.\n"
40               " smode [-r]                            - Restart daemon with root permissions, '-r' to cancel root\n"
41               "                                         permissions\n"
42               " tmode usb                             - Reboot the device, listening on USB\n"
43               " tmode port [port]                     - Reboot the device, listening on TCP port\n"
44               "\n"
45               "---------------------------------task commands:-------------------------------------\n"
46               "file commands:\n"
47               " file send [option] local remote       - Send file to device\n"
48               " file recv [option] remote local       - Recv file from device\n"
49               "                                         option is -a|-s|-z\n"
50               "                                         -a: hold target file timestamp\n"
51               "                                         -sync: just update newer file\n"
52               "                                         -z: compress transfer\n"
53               "                                         -m: mode sync\n"
54               "\n"
55               "forward commands:\n"
56               " fport localnode remotenode            - Forward local traffic to remote device\n"
57               " rport remotenode localnode            - Reserve remote traffic to local host\n"
58               "                                         node config name format 'schema:content'\n"
59               "                                         examples are below:\n"
60               "                                         tcp:<port>\n"
61               "                                         localfilesystem:<unix domain socket name>\n"
62               "                                         localreserved:<unix domain socket name>\n"
63               "                                         localabstract:<unix domain socket name>\n"
64               "                                         dev:<device name>\n"
65               "                                         jdwp:<pid> (remote only)\n"
66               " fport ls                              - Display forward/reverse tasks\n"
67               " fport rm taskstr                      - Remove forward/reverse task by taskstring\n"
68               "\n"
69               "app commands:\n"
70               " install [-r|-s] src                   - Send package(s) to device and install them\n"
71               "                                         src examples: single or multiple packages and directories\n"
72               "                                         (.hap .hsp)\n"
73               "                                         -r: replace existing application\n"
74               "                                         -s: install shared bundle for multi-apps\n"
75               " uninstall [-k] [-s] package           - Remove application package from device\n"
76               "                                         -k: keep the data and cache directories\n"
77               "                                         -s: remove shared bundle\n"
78               "\n"
79               "debug commands:\n"
80               " hilog [-h]                            - Show device log, -h for detail\n"
81               " shell [COMMAND...]                    - Run shell command (interactive shell if no command given)\n"
82               " bugreport [FILE]                      - Return all information from the device, stored in file if "
83               "FILE is specified\n"
84               " jpid                                  - List PIDs of processes hosting a JDWP transport\n"
85               " track-jpid [-a|-p]                    - Track PIDs of debug processes hosting a JDWP transport\n"
86               "                                         -a: include debug and release processes\n"
87               "                                         -p: don't display debug and release tags\n"
88               "\n"
89               "security commands:\n"
90               " keygen FILE                           - Generate public/private key; key stored in FILE and FILE.pub\n"
91               "\n";
92         return ret;
93     }
94 
Verbose()95     string Verbose()
96     {
97         string ret = "\n                         OpenHarmony device connector(HDC) ...\n\n"
98             "---------------------------------global commands:----------------------------------\n"
99             " -h/help [verbose]                     - Print hdc help, 'verbose' for more other cmds\n"
100             " -v/version                            - Print hdc version\n"
101             " -l[0-5]                               - Set runtime loglevel\n"
102             " -t connectkey                         - Use device with given connect key\n"
103             " checkserver                           - check client-server version\n"
104             " checkdevice                           - check server-daemon version(only uart)\n"
105             "\n"
106             "---------------------------------component commands:-------------------------------\n"
107             "session commands(on server):\n"
108             " discover                              - Discover devices listening on TCP via LAN broadcast\n"
109             " list targets [-v]                     - List all devices status, -v for detail\n"
110             " tconn key                             - Connect device via key, TCP use ip:port\n"
111             "                                         example:192.168.0.100:10178/192.168.0.100\n"
112             "                                         USB connect automatic, TCP need to connect manually\n"
113 #ifdef HDC_SUPPORT_UART
114             "\n"
115             "                                         UART connect need connect manually.\n"
116             "                                         Baud Rate can be specified with commas.\n"
117             "                                         key format: <Port Name>[,Baud Rate]\n"
118             "                                         example: tconn COM5,921600\n"
119             "                                         Default Baud Rate is 921600.\n"
120             "\n"
121 #endif
122             " start [-r]                            - Start server. If with '-r', will be restart server\n"
123             " kill [-r]                             - Kill server. If with '-r', will be restart server\n"
124             " -s [ip:]port                          - Set hdc server listen config\n"
125             "\n"
126             "service commands(on daemon):\n"
127             " target mount                          - Set /system /vendor partition read-write\n"
128             " target boot [-bootloader|-recovery]   - Reboot the device or boot into bootloader\\recovery.\n"
129             " target boot [MODE]                    - Reboot the into MODE.\n"
130             " smode [-r]                            - Restart daemon with root permissions, '-r' to cancel root\n"
131             "                                         permissions\n"
132             " tmode usb                             - Reboot the device, listening on USB\n"
133             " tmode port [port]                     - Reboot the device, listening on TCP port\n"
134             "\n"
135             "---------------------------------task commands:-------------------------------------\n"
136             "file commands:\n"
137             " file send [option] local remote       - Send file to device\n"
138             " file recv [option] remote local       - Recv file from device\n"
139             "                                         option is -a|-s|-z\n"
140             "                                         -a: hold target file timestamp\n"
141             "                                         -sync: just update newer file\n"
142             "                                         -z: compress transfer\n"
143             "                                         -m: mode sync\n"
144             "\n"
145             "forward commands:\n"
146             " fport localnode remotenode            - Forward local traffic to remote device\n"
147             " rport remotenode localnode            - Reserve remote traffic to local host\n"
148             "                                         node config name format 'schema:content'\n"
149             "                                         examples are below:\n"
150             "                                         tcp:<port>\n"
151             "                                         localfilesystem:<unix domain socket name>\n"
152             "                                         localreserved:<unix domain socket name>\n"
153             "                                         localabstract:<unix domain socket name>\n"
154             "                                         dev:<device name>\n"
155             "                                         jdwp:<pid> (remote only)\n"
156             " fport ls                              - Display forward/reverse tasks\n"
157             " fport rm taskstr                      - Remove forward/reverse task by taskstring\n"
158             "\n"
159             "app commands:\n"
160             " install [-r|-s] src                   - Send package(s) to device and install them\n"
161             "                                         src examples: single or multiple packages and directories\n"
162             "                                         (.hap .hsp)\n"
163             "                                         -r: replace existing application\n"
164             "                                         -s: install shared bundle for multi-apps\n"
165             " uninstall [-k] [-s] package           - Remove application package from device\n"
166             "                                         -k: keep the data and cache directories\n"
167             "                                         -s: remove shared bundle\n"
168             "\n"
169             "debug commands:\n"
170             " hilog [-h]                            - Show device log, -h for detail\n"
171             " shell [COMMAND...]                    - Run shell command (interactive shell if no command given)\n"
172             " bugreport [FILE]                      - Return all information from the device, stored in file if FILE "
173             "is specified\n"
174             " jpid                                  - List PIDs of processes hosting a JDWP transport\n"
175             " sideload [PATH]                       - Sideload the given full OTA package\n"
176             "\n"
177             "security commands:\n"
178             " keygen FILE                           - Generate public/private key; key stored in FILE and FILE.pub\n"
179             "\n"
180             "---------------------------------flash commands:------------------------------------\n"
181             "flash commands:\n"
182             " update packagename                    - Update system by package\n"
183             " flash [-f] partition imagename        - Flash partition by image\n"
184             " erase [-f] partition                  - Erase partition\n"
185             " format [-f] partition                 - Format partition\n"
186             "---------------------------------external commands:------------------------------------\n"
187             "extconn key                             - Connect external device via key, TCP use ip:port(remian)\n"
188             "-S [ip:]port                            - Set hdc external server listen config\n"
189             "\n";
190         return ret;
191     }
192 
TargetConnect(FormatCommand * outCmd)193     string TargetConnect(FormatCommand *outCmd)
194     {
195         string stringError;
196         if (Base::StringEndsWith(outCmd->parameters, " -remove")) {
197             outCmd->parameters = outCmd->parameters.substr(0, outCmd->parameters.size() - CMD_REMOTE_SIZE);
198             outCmd->cmdFlag = CMD_KERNEL_TARGET_DISCONNECT;
199         } else {
200             outCmd->cmdFlag = CMD_KERNEL_TARGET_CONNECT;
201             constexpr int maxKeyLength = 50; // 50: tcp max=21,USB max=8bytes, serial device name maybe long
202             if (outCmd->parameters.size() > maxKeyLength) {
203                 stringError = "Error connect key's size";
204                 outCmd->bJumpDo = true;
205             }
206         }
207         size_t pos = outCmd->parameters.find(":");
208         if (pos != std::string::npos) {
209             // tcp mode
210             string ip = outCmd->parameters.substr(0, pos);
211             string sport = outCmd->parameters.substr(pos + 1);
212             if (sport.empty()) {
213                 stringError = "Port incorrect";
214                 outCmd->bJumpDo = true;
215                 return stringError;
216             }
217             if (ip == "localhost") {
218                 ip = "127.0.0.1";
219                 outCmd->parameters.replace(0, pos, ip);
220             }
221             int port = std::stoi(sport);
222             sockaddr_in addr;
223             if ((port <= 0 || port > MAX_IP_PORT) || uv_ip4_addr(ip.c_str(), port, &addr) < 0) {
224                 stringError = "IP:Port incorrect";
225                 outCmd->bJumpDo = true;
226             }
227         }
228         return stringError;
229     }
230 
ForwardPort(const char * input,FormatCommand * outCmd)231     string ForwardPort(const char *input, FormatCommand *outCmd)
232     {
233         string stringError;
234         const char *pExtra = input + 6;  // CMDSTR_FORWARD_FPORT CMDSTR_FORWARD_RPORT + " " size
235         if (!strncmp(input, CMDSTR_FORWARD_FPORT.c_str(), CMDSTR_FORWARD_FPORT.size()) && !strcmp(pExtra, "ls")) {
236             outCmd->cmdFlag = CMD_FORWARD_LIST;
237         } else if (!strncmp(input, CMDSTR_FORWARD_FPORT.c_str(), CMDSTR_FORWARD_FPORT.size()) &&
238             !strncmp(pExtra, "rm", 2)) { // 2: "rm" size
239             outCmd->cmdFlag = CMD_FORWARD_REMOVE;
240             if (strcmp(pExtra, "rm")) {
241                 outCmd->parameters = input + FORWORD_PORT_RM_BUF_SIZE;
242             }
243         } else {
244             const char *p = input + FORWORD_PORT_OTHER_BUF_SIZE;
245             // clang-format off
246             if (strncmp(p, "tcp:", 4) && // 4: "tcp:" size
247                 strncmp(p, "localabstract:", 14) && // 14: "localabstract:" size
248                 strncmp(p, "localreserved:", 14) && // 14: "localreserved:" size
249                 strncmp(p, "localfilesystem:", 16) && // 16: "localfilesystem:" size
250                 strncmp(p, "dev:", 4) && // 4: "dev:" size
251                 strncmp(p, "jdwp:", 5) && // 5: "jdwp:" size
252                 strncmp(p, "ark:", 4)) { // 4: "ark:" size
253                 stringError = "Incorrect forward command";
254                 outCmd->bJumpDo = true;
255             }
256             // clang-format on
257             outCmd->cmdFlag = CMD_FORWARD_INIT;
258             outCmd->parameters = input;
259         }
260         return stringError;
261     }
262 
RunMode(const char * input,FormatCommand * outCmd)263     string RunMode(const char *input, FormatCommand *outCmd)
264     {
265         string stringError;
266         outCmd->cmdFlag = CMD_UNITY_RUNMODE;
267         outCmd->parameters = input + CMDSTR_TARGET_MODE.size() + 1;  // with  ' '
268         int portLength = 4;
269         int portSpaceLength = 5;
270         if (!strncmp(outCmd->parameters.c_str(), "port", portLength) &&
271             !strcmp(outCmd->parameters.c_str(), CMDSTR_TMODE_USB.c_str())) {
272             stringError = "Error tmode command";
273             outCmd->bJumpDo = true;
274         } else if (!strncmp(outCmd->parameters.c_str(), "port ", portSpaceLength)) {
275             const char *tmp = input + strlen("tmode port ");
276             // command is tmode port close
277             if (strcmp(tmp, "close") == 0) {
278                 return stringError;
279             }
280             int port = atoi(tmp);
281             if (port > MAX_IP_PORT || port <= 0) {
282                 stringError = "Incorrect port range";
283                 outCmd->bJumpDo = true;
284             }
285         }
286         return stringError;
287     }
288 
TargetReboot(const char * input,FormatCommand * outCmd)289     void TargetReboot(const char *input, FormatCommand *outCmd)
290     {
291         outCmd->cmdFlag = CMD_UNITY_REBOOT;
292         if (strcmp(input, CMDSTR_TARGET_REBOOT.c_str())) {
293             outCmd->parameters = input + CMDSTR_TARGET_REBOOT.size() + 1;  // with  ' '
294             if (outCmd->parameters == "-bootloader" || outCmd->parameters == "-recovery" ||
295                 outCmd->parameters == "-flashd") {
296                 outCmd->parameters.erase(outCmd->parameters.begin());
297             }
298         }
299     }
300 
301     // command input
302     // client side:Enter string data formatting conversion to module see internal processing command
String2FormatCommand(const char * inputRaw,int sizeInputRaw,FormatCommand * outCmd)303     string String2FormatCommand(const char *inputRaw, int sizeInputRaw, FormatCommand *outCmd)
304     {
305         string stringError;
306         string input = string(inputRaw, sizeInputRaw);
307         if (!strncmp(input.c_str(), CMDSTR_SOFTWARE_HELP.c_str(), CMDSTR_SOFTWARE_HELP.size())) {
308             outCmd->cmdFlag = CMD_KERNEL_HELP;
309             outCmd->bJumpDo = true;
310             if (strstr(input.c_str(), " verbose")) {
311                 stringError = Verbose();
312             } else {
313                 stringError = Usage();
314             }
315         } else if (!strcmp(input.c_str(), CMDSTR_SOFTWARE_VERSION.c_str())) {
316             outCmd->cmdFlag = CMD_KERNEL_HELP;
317             stringError = Base::GetVersion();
318             outCmd->bJumpDo = true;
319         } else if (!strcmp(input.c_str(), CMDSTR_TARGET_DISCOVER.c_str())) {
320             outCmd->cmdFlag = CMD_KERNEL_TARGET_DISCOVER;
321         } else if (!strncmp(input.c_str(), CMDSTR_LIST_TARGETS.c_str(), CMDSTR_LIST_TARGETS.size())) {
322             outCmd->cmdFlag = CMD_KERNEL_TARGET_LIST;
323             if (strstr(input.c_str(), " -v")) {
324                 outCmd->parameters = "v";
325             }
326         } else if (!strncmp(input.c_str(), CMDSTR_CHECK_SERVER.c_str(), CMDSTR_CHECK_SERVER.size())) {
327             outCmd->cmdFlag = CMD_CHECK_SERVER;
328         } else if (!strncmp(input.c_str(), CMDSTR_CHECK_DEVICE.c_str(), CMDSTR_CHECK_DEVICE.size())) {
329             outCmd->parameters = input.c_str() + CMDSTR_CHECK_DEVICE.size() + 1;  // with ' '
330             outCmd->cmdFlag = CMD_CHECK_DEVICE;
331         } else if (!strncmp(input.c_str(), CMDSTR_WAIT_FOR.c_str(), CMDSTR_WAIT_FOR.size())) {
332             outCmd->cmdFlag = CMD_WAIT_FOR;
333         } else if (!strcmp(input.c_str(), CMDSTR_CONNECT_ANY.c_str())) {
334             outCmd->cmdFlag = CMD_KERNEL_TARGET_ANY;
335         } else if (!strncmp(input.c_str(), CMDSTR_CONNECT_TARGET.c_str(), CMDSTR_CONNECT_TARGET.size())) {
336             outCmd->parameters = input.c_str() + CMDSTR_CONNECT_TARGET.size() + 1;  // with ' '
337             stringError = TargetConnect(outCmd);
338         } else if (!strncmp(input.c_str(), (CMDSTR_SHELL + " ").c_str(), CMDSTR_SHELL.size() + 1)) {
339             outCmd->cmdFlag = CMD_UNITY_EXECUTE;
340             outCmd->parameters = input.c_str() + CMDSTR_SHELL.size() + 1;
341         } else if (!strcmp(input.c_str(), CMDSTR_SHELL.c_str())) {
342             outCmd->cmdFlag = CMD_SHELL_INIT;
343         } else if (!strncmp(input.c_str(), CMDSTR_FILE_SEND.c_str(), CMDSTR_FILE_SEND.size()) ||
344                    !strncmp(input.c_str(), CMDSTR_FILE_RECV.c_str(), CMDSTR_FILE_RECV.size())) {
345             outCmd->cmdFlag = CMD_FILE_INIT;
346             outCmd->parameters = input.c_str() + strlen("file ");
347         } else if (!strncmp(input.c_str(), string(CMDSTR_FORWARD_FPORT + " ").c_str(), CMDSTR_FORWARD_FPORT.size() + 1)
348                    || !strncmp(input.c_str(), string(CMDSTR_FORWARD_RPORT + " ").c_str(),
349                                CMDSTR_FORWARD_RPORT.size() + 1)) {
350             stringError = ForwardPort(input.c_str(), outCmd);
351         } else if (!strncmp(input.c_str(), CMDSTR_APP_INSTALL.c_str(), CMDSTR_APP_INSTALL.size())) {
352             outCmd->cmdFlag = CMD_APP_INIT;
353             outCmd->parameters = input;
354         } else if (!strncmp(input.c_str(), CMDSTR_APP_UNINSTALL.c_str(), CMDSTR_APP_UNINSTALL.size())) {
355             outCmd->cmdFlag = CMD_APP_UNINSTALL;
356             outCmd->parameters = input;
357             if (outCmd->parameters.size() > MAX_APP_PARAM_SIZE || outCmd->parameters.size() < MIN_APP_PARAM_SIZE) {
358                 stringError = "Package's path incorrect";
359                 outCmd->bJumpDo = true;
360             }
361         } else if (!strcmp(input.c_str(), CMDSTR_TARGET_MOUNT.c_str())) {
362             outCmd->cmdFlag = CMD_UNITY_REMOUNT;
363         } else if (!strcmp(input.c_str(), CMDSTR_LIST_JDWP.c_str())) {
364             outCmd->cmdFlag = CMD_JDWP_LIST;
365         } else if (!strncmp(input.c_str(), CMDSTR_TRACK_JDWP.c_str(), CMDSTR_TRACK_JDWP.size())) {
366             outCmd->cmdFlag = CMD_JDWP_TRACK;
367             if (strstr(input.c_str(), " -p")) {
368                 outCmd->parameters = "p";
369             } else if (strstr(input.c_str(), " -a")) {
370                 outCmd->parameters = "a";
371             }
372         } else if (!strncmp(input.c_str(), CMDSTR_TARGET_REBOOT.c_str(), CMDSTR_TARGET_REBOOT.size())) {
373             TargetReboot(input.c_str(), outCmd);
374         } else if (!strncmp(input.c_str(), CMDSTR_TARGET_MODE.c_str(), CMDSTR_TARGET_MODE.size())) {
375             stringError = RunMode(input.c_str(), outCmd);
376         } else if (!strncmp(input.c_str(), CMDSTR_HILOG.c_str(), CMDSTR_HILOG.size())) {
377             outCmd->cmdFlag = CMD_UNITY_HILOG;
378             if (strstr(input.c_str(), " -h")) {
379                 outCmd->parameters = "h";
380             }
381         } else if (!strncmp(input.c_str(), CMDSTR_STARTUP_MODE.c_str(), CMDSTR_STARTUP_MODE.size())) {
382             outCmd->cmdFlag = CMD_UNITY_ROOTRUN;
383             if (strstr(input.c_str(), " -r")) {
384                 outCmd->parameters = "r";
385             }
386         } else if (!strncmp(input.c_str(), CMDSTR_APP_SIDELOAD.c_str(), CMDSTR_APP_SIDELOAD.size())) {
387             if (strlen(input.c_str()) == CMDSTR_APP_SIDELOAD.size()) {
388                 stringError = "Incorrect command, please with local path";
389                 outCmd->bJumpDo = true;
390             }
391             outCmd->cmdFlag = CMD_APP_SIDELOAD;
392             outCmd->parameters = input;
393         } else if (!strncmp(input.c_str(), CMDSTR_BUGREPORT.c_str(), CMDSTR_BUGREPORT.size())) {
394             outCmd->cmdFlag = CMD_UNITY_BUGREPORT_INIT;
395             outCmd->parameters = input;
396             if (outCmd->parameters.size() == CMDSTR_BUGREPORT.size() + 1) { // 1 is sizeInputRaw = input.size() + 1
397                 outCmd->parameters = CMDSTR_BUGREPORT + " ";
398             }
399         }
400         // Inner command, protocol uses only
401         else if (!strncmp(input.c_str(), CMDSTR_INNER_ENABLE_KEEPALIVE.c_str(), CMDSTR_INNER_ENABLE_KEEPALIVE.size())) {
402             outCmd->cmdFlag = CMD_KERNEL_ENABLE_KEEPALIVE;
403         } else if (HostUpdater::CheckMatchUpdate(input, *outCmd)) {
404             outCmd->parameters = input;
405         } else {
406             stringError = "Unknown command...";
407             outCmd->bJumpDo = true;
408         }
409 #ifdef HDC_DEBUG
410         WRITE_LOG(LOG_DEBUG, "String2FormatCommand cmdFlag:%d", outCmd->cmdFlag);
411 #endif
412         // nl
413         if (stringError.size()) {
414             stringError += "\n";
415         }
416         return stringError;
417     };
418 }
419 }  // namespace Hdc
420