1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include "cli_uagent.h"
6 #include "aos/cli.h"
7 #include "cli_api.h"
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11 
12 static uint8_t g_cloud_cmd_enable = 0;
13 unsigned char g_cmd_from_cloud = 0;
14 
15 int32_t cli_handle_input(char *inbuf);
16 
on_cli_handler(void * p,const unsigned short len,void * cmd)17 static int on_cli_handler(void *p, const unsigned short len, void *cmd)
18 {
19     int rc = -1;
20     if (cmd == NULL) {
21         return rc;
22     }
23 
24     g_cmd_from_cloud = 1;
25     if (NULL != strstr(cmd, "cli cloud ")) {
26         g_cmd_from_cloud = 0;
27         if (0 == strncmp("cli cloud start", cmd, len)) {
28             aos_cli_printf("ready for your command!\n");
29             aos_cli_printf("CLI: switch on cloud control\n");
30             g_cloud_cmd_enable = 1;
31             rc = 0;
32         } else if (0 == strncmp("cli cloud stop", cmd, len)) {
33             aos_cli_printf("byebye!\n");
34             aos_cli_printf("CLI: switch off cloud control\n");
35              g_cloud_cmd_enable = 0;
36             rc = 0;
37         } else if (NULL != strstr(cmd, "cli cloud timeout=")) {
38             if (0 != uagent_request_service(
39                          UAGENT_MOD_CLI, UAGENT_MOD_UAGENT,
40                          UAGENT_FUNC_UA_DUMP_DELAY_SEND_PARAM, len, cmd)) {
41                 aos_cli_printf("CLI Setting Delay Parameter %s Fail\n",
42                                (char *)cmd);
43             } else {
44                 aos_cli_printf("CLI Setting Delay Parameter %s OK\n",
45                                (char *)cmd);
46                 rc = 0;
47             }
48         }
49         return rc;
50     }
51 
52     if (1 == g_cloud_cmd_enable) {
53         aos_cli_printf("\n# %s", (char *)cmd);
54         rc = cli_handle_input(cmd);
55         if (rc == CLI_ERR_BADCMD) {
56             if (cmd != NULL) {
57                 aos_cli_printf("command '%s' not found\r\n", cmd);
58             }
59         } else if (rc == CLI_ERR_SYNTAX) {
60             aos_cli_printf("syntax error\r\n");
61         }
62     }
63     g_cmd_from_cloud = 0;
64     return rc;
65 }
66 
67 static uagent_func_node_t cli_uagent_funclist[] = {
68     {CLI_RESPONE, "cli result", NULL, NULL, &cli_uagent_funclist[1]},
69     {CLI_CMD_INPUT, "cli input", on_cli_handler, NULL, NULL},
70 };
71 
72 static mod_func_t cli_uagent_func = {{UAGENT_MOD_CLI, 2, "CLI", MOD_VER},
73                                      cli_uagent_funclist};
74 
cli_uagent_init(void)75 void cli_uagent_init(void)
76 {
77     const char cmd[] = "cli cloud timeout=20";
78     uagent_func_node_t *p = cli_uagent_func.header;
79     while (NULL != p) {
80         if (0 != uagent_register(UAGENT_MOD_CLI, "CLI", MOD_VER, p->func,
81                                  p->func_name, p->service, p->argu)) {
82             printf("register into uagent fail");
83         }
84         p = p->next;
85     }
86     uagent_request_service(UAGENT_MOD_CLI, UAGENT_MOD_UAGENT,
87                            UAGENT_FUNC_UA_DUMP_DELAY_SEND_PARAM, strlen(cmd),
88                            cmd);
89 }
90