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