1+ /**
2+ * @file cli_cmd.c
3+ * @brief Command Line Interface (CLI) commands for Tuya IoT applications.
4+ *
5+ * This file implements a set of CLI commands for controlling and managing Tuya
6+ * IoT devices. It includes commands for switching device states, executing
7+ * system commands, managing key-value pairs, resetting and starting/stopping
8+ * the IoT process, and retrieving memory usage information. These commands
9+ * facilitate debugging, testing, and managing Tuya IoT applications directly
10+ * from a command line interface.
11+ *
12+ * Key functionalities provided in this file:
13+ * - Switching device states (on/off).
14+ * - Executing arbitrary system commands.
15+ * - Key-value pair management for device configuration.
16+ * - Resetting, starting, and stopping the IoT process.
17+ * - Retrieving current free heap memory size.
18+ *
19+ * This implementation leverages Tuya's Application Layer (TAL) APIs and IoT SDK
20+ * to provide a rich set of commands for device management and debugging. It is
21+ * designed to enhance the development and testing process of Tuya IoT
22+ * applications.
23+ *
24+ * @copyright Copyright (c) 2021-2024 Tuya Inc. All Rights Reserved.
25+ *
26+ */
27+
28+ #include "tal_api.h"
29+ #include "tuya_iot.h"
30+ #include <stdlib.h>
31+
32+ extern void tal_kv_cmd (int argc , char * argv []);
33+ extern void netmgr_cmd (int argc , char * argv []);
34+ extern void weather_get (int argc , char * argv []);
35+
36+ /**
37+ * @brief excute system cmd
38+ *
39+ * @param argc
40+ * @param argv
41+ * @return void
42+ */
43+ static void system_cmd (int argc , char * argv [])
44+ {
45+ char cmd [256 ];
46+
47+ if (argc < 2 ) {
48+ PR_INFO ("usge: sys <cmd>" );
49+ return ;
50+ }
51+
52+ size_t offset = 0 ;
53+
54+ for (int i = 1 ; i < argc ; i ++ ) {
55+ offset += sprintf (cmd + offset , "%s " , argv [i ]);
56+ }
57+
58+ PR_DEBUG ("system %s" , cmd );
59+ system (cmd );
60+ }
61+
62+ /**
63+ * @brief get free heap size cmd
64+ *
65+ * @param argc
66+ * @param argv
67+ */
68+ static void mem (int argc , char * argv [])
69+ {
70+ int free_heap = 0 ;
71+ free_heap = tal_system_get_free_heap_size ();
72+ PR_NOTICE ("cur free heap: %d" , free_heap );
73+ }
74+
75+ /**
76+ * @brief reset iot to unactive/unregister
77+ *
78+ * @param argc
79+ * @param argv
80+ */
81+ static void reset (int argc , char * argv [])
82+ {
83+ tuya_iot_reset (tuya_iot_client_get ());
84+ }
85+
86+ /**
87+ * @brief reset iot to unactive/unregister
88+ *
89+ * @param argc
90+ * @param argv
91+ */
92+ static void start (int argc , char * argv [])
93+ {
94+ tuya_iot_start (tuya_iot_client_get ());
95+ }
96+
97+ /**
98+ * @brief stop iot
99+ *
100+ * @param argc
101+ * @param argv
102+ */
103+ static void stop (int argc , char * argv [])
104+ {
105+ tuya_iot_stop (tuya_iot_client_get ());
106+ }
107+
108+ /**
109+ * @brief cli cmd list
110+ *
111+ */
112+ static cli_cmd_t s_cli_cmd [] = {
113+ {.name = "weather" , .func = weather_get , .help = "weather get" },
114+ {.name = "kv" , .func = tal_kv_cmd , .help = "kv test" },
115+ {.name = "sys" , .func = system_cmd , .help = "system cmd" },
116+ {.name = "reset" , .func = reset , .help = "reset iot" },
117+ {.name = "stop" , .func = stop , .help = "stop iot" },
118+ {.name = "start" , .func = start , .help = "start iot" },
119+ {.name = "mem" , .func = mem , .help = "mem size" },
120+ {.name = "netmgr" , .func = netmgr_cmd , .help = "netmgr cmd" },
121+ };
122+
123+ /**
124+ * @brief
125+ *
126+ */
127+ void tuya_app_cli_init (void )
128+ {
129+ tal_cli_cmd_register (s_cli_cmd , sizeof (s_cli_cmd ) / sizeof (s_cli_cmd [0 ]));
130+ }
0 commit comments