2323#include "filecmd.h"
2424#include "stats.h"
2525#include "test.h"
26+ #include "cmd.h"
2627#include "log.h"
2728#include "utils.h"
2829
3839
3940void parse_commands (char * data , int length )
4041{
41- switch (data [0 ])
42+ // Use command module to parse the network command
43+ net_command_t cmd = cmd_parse_network (data , length );
44+
45+ // Handle the parsed command based on its type
46+ switch (cmd .type )
4247 {
43- case 'p' : // pid heartbeat : p<pid> ? p1234
48+ case NET_CMD_HEARTBEAT :
4449 {
45- int n = parse_number (data , length , NULL );
46- LOGD ("Heartbeat command received from pid %d : %s" , n , data );
50+ int i = find_pid (cmd .pid );
4751
48- if (0 < n && INT32_MAX > n )
52+ if (i >= 0 )
4953 {
50- int i = find_pid ( n );
54+ time_t t = get_heartbeat_time ( i );
5155
52- if (i >= 0 )
56+ if (get_first_heartbeat ( i ) )
5357 {
54- time_t t = get_heartbeat_time (i );
55-
56- if (get_first_heartbeat (i ))
57- {
58- if (t >= 0 )
59- {
60- LOGD ("%s heartbeat after %d seconds" , get_app_name (i ), t );
61- stats_update_heartbeat_time (i , t );
62- }
63- }
64- else
58+ if (t >= 0 )
6559 {
66- LOGD ("%s first heartbeat after %d seconds" , get_app_name (i ), t );
67- stats_update_first_heartbeat_time (i , t );
68- set_first_heartbeat (i );
60+ LOGD ("%s heartbeat after %d seconds" , get_app_name (i ), t );
61+ stats_update_heartbeat_time (i , t );
6962 }
70-
71- update_heartbeat_time (i );
7263 }
73- }
74- else
75- {
76- LOGE ("Invalid pid received, pid %d : %s" , n , data );
64+ else
65+ {
66+ LOGD ("%s first heartbeat after %d seconds" , get_app_name (i ), t );
67+ stats_update_first_heartbeat_time (i , t );
68+ set_first_heartbeat (i );
69+ }
70+
71+ update_heartbeat_time (i );
7772 }
7873 }
7974 break ;
80- #if 0 // feature disabled
8175
82- case 'a' : // stArt : a<name> ? aBot
76+ #if 0 // feature disabled
77+ case NET_CMD_START :
8378 {
84- LOGD ("Start command received: %s" , data );
85- char name [MAX_APP_NAME_LENGTH ];
86- strncpy (name , & data [1 ], MAX_APP_NAME_LENGTH - 1 );
87-
8879 for (int i = 0 ; i < get_app_count (); i ++ )
8980 {
90- if (0 == strncmp (get_app_name (i ), name , MAX_APP_NAME_LENGTH - 1 ))
81+ if (0 == strncmp (get_app_name (i ), cmd . app_name , MAX_APP_NAME_LENGTH - 1 ))
9182 {
9283 if (!is_application_started (i ))
9384 {
@@ -99,15 +90,11 @@ void parse_commands(char *data, int length)
9990 }
10091 break ;
10192
102- case 'o' : // stOp : o<name> ? oBot
93+ case NET_CMD_STOP :
10394 {
104- LOGD ("Stop command received: %s" , data );
105- char name [MAX_APP_NAME_LENGTH ];
106- strncpy (name , & data [1 ], MAX_APP_NAME_LENGTH - 1 );
107-
10895 for (int i = 0 ; i < get_app_count (); i ++ )
10996 {
110- if (0 == strncmp (get_app_name (i ), name , MAX_APP_NAME_LENGTH - 1 ))
97+ if (0 == strncmp (get_app_name (i ), cmd . app_name , MAX_APP_NAME_LENGTH - 1 ))
11198 {
11299 if (is_application_running (i ))
113100 {
@@ -119,15 +106,11 @@ void parse_commands(char *data, int length)
119106 }
120107 break ;
121108
122- case 'r' : // Restart : r<name> ? rBot
109+ case NET_CMD_RESTART :
123110 {
124- LOGD ("Restart command received: %s" , data );
125- char name [MAX_APP_NAME_LENGTH ];
126- strncpy (name , & data [1 ], MAX_APP_NAME_LENGTH - 1 );
127-
128111 for (int i = 0 ; i < get_app_count (); i ++ )
129112 {
130- if (0 == strncmp (get_app_name (i ), name , MAX_APP_NAME_LENGTH - 1 ))
113+ if (0 == strncmp (get_app_name (i ), cmd . app_name , MAX_APP_NAME_LENGTH - 1 ))
131114 {
132115 restart_application (i );
133116 filecmd_remove_restart (i );
@@ -137,30 +120,10 @@ void parse_commands(char *data, int length)
137120 break ;
138121#endif
139122
123+ case NET_CMD_UNKNOWN :
140124 default :
141- {
142- const int max_bytes = MAX_APP_NAME_LENGTH ;
143- const int max_length = max_bytes * 3 ;
144- char hexStr [max_length ];
145- memset (hexStr , 0 , sizeof (hexStr ));
146-
147- if (length > max_bytes ) {
148- length = max_bytes ;
149- }
150-
151- for (int i = 0 ; i < length ; i ++ ) {
152- snprintf (& hexStr [i * 3 ], sizeof (hexStr ) - (i * 3 ), "%02X " , data [i ]);
153- }
154-
155- char printableStr [max_bytes + 1 ];
156- memset (printableStr , 0 , sizeof (printableStr ));
157- for (int i = 0 ; i < length ; i ++ ) {
158- printableStr [i ] = (data [i ] >= 32 && data [i ] < 127 ) ? data [i ] : '.' ;
159- }
160-
161- LOGE ("Unknown command received : %s | %s" , printableStr , hexStr );
162- }
163- break ;
125+ // Error logging is already handled in cmd_parse_network
126+ break ;
164127 }
165128}
166129
0 commit comments