2424#include " esp3d.h"
2525#include " esp3d_settings.h"
2626
27+ #if defined(ESP_LOG_FEATURE)
28+ const char * esp3dclientstr[]={
29+ " no_client" ,
30+ " serial" ,
31+ " usb_serial" ,
32+ " stream" ,
33+ " telnet" ,
34+ " http" ,
35+ " webui_websocket" ,
36+ " websocket" ,
37+ " rendering" ,
38+ " bluetooth" ,
39+ " socket_serial" ,
40+ " echo_serial" ,
41+ " serial_bridge" ,
42+ " remote_screen" ,
43+ " mks_serial" ,
44+ " command" ,
45+ " system" ,
46+ " all_clients"
47+ };
48+ #define GETCLIENTSTR (id ) static_cast <uint8_t >(id)>=0 && static_cast <uint8_t >(id)<=static_cast <uint8_t >(ESP3DClientType::all_clients)?esp3dclientstr[static_cast <uint8_t >(id)] :" Out of index"
49+
50+ const char * esp3dmsgstr[] = {
51+ " head" , " core" , " tail" , " unique"
52+ };
53+ #define GETMSGTYPESTR (id ) static_cast <uint8_t >(id)>=0 && static_cast <uint8_t >(id)<=static_cast <uint8_t >(ESP3DMessageType::unique)?esp3dmsgstr[static_cast <uint8_t >(id)] :" Out of index"
54+
55+ #endif // defined(ESP_LOG_FEATURE)
56+
57+
2758#if COMMUNICATION_PROTOCOL == MKS_SERIAL
2859#include " ../modules/mks/mks_service.h"
2960#endif // COMMUNICATION_PROTOCOL == MKS_SERIAL
@@ -1180,16 +1211,19 @@ bool ESP3DCommands::dispatch(const char *sbuf, ESP3DClientType target,
11801211ESP3DClientType ESP3DCommands::getOutputClient (bool fromSettings) {
11811212 // TODO: add setting for it when necessary
11821213 (void )fromSettings;
1214+ esp3d_log (" OutputClient: %d %s" , static_cast <uint8_t >(_output_client), GETCLIENTSTR (_output_client));
11831215 return _output_client;
11841216}
11851217
11861218bool ESP3DCommands::dispatch (ESP3DMessage *msg) {
11871219 bool sendOk = true ;
11881220 String tmp;
1189- esp3d_log (" Dispatch message origin %d to client %d , size: %d, type: %d" ,
1221+ esp3d_log (" Dispatch message origin %d(%s) to client %d(%s) , size: %d, type: %d(%s) " ,
11901222 static_cast <uint8_t >(msg->origin ),
1191- static_cast <uint8_t >(msg->target ), msg->size ,
1192- static_cast <uint8_t >(msg->type ));
1223+ esp3dclientstr[static_cast <uint8_t >(msg->origin )],
1224+ static_cast <uint8_t >(msg->target ), GETCLIENTSTR (msg->target ),
1225+ msg->size , static_cast <uint8_t >(msg->type ),GETMSGTYPESTR (msg->type ));
1226+ esp3d_log (" Dispatch message data: %s" , (const char *)msg->data );
11931227 if (!msg) {
11941228 esp3d_log_e (" no msg" );
11951229 return false ;
@@ -1199,6 +1233,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
11991233 switch (msg->target ) {
12001234#if COMMUNICATION_PROTOCOL == RAW_SERIAL
12011235 case ESP3DClientType::serial:
1236+ esp3d_log (" Serial message" );
12021237 if (!esp3d_serial_service.dispatch (msg)) {
12031238 sendOk = false ;
12041239 esp3d_log_e (" Serial dispatch failed" );
@@ -1208,6 +1243,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
12081243
12091244#if COMMUNICATION_PROTOCOL == SOCKET_SERIAL
12101245 case ESP3DClientType::echo_serial:
1246+ esp3d_log (" Echo serial message" );
12111247 MYSERIAL1.write (msg->data , msg->size );
12121248 if (msg->type == ESP3DMessageType::unique || msg->type == ESP3DMessageType::tail) {
12131249 if (msg->data [msg->size -1 ]!=' \n ' ){
@@ -1216,10 +1252,19 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
12161252 }
12171253 ESP3DMessageManager::deleteMsg (msg);
12181254 break ;
1255+
1256+ case ESP3DClientType::socket_serial:
1257+ esp3d_log (" Socket serial message" );
1258+ if (!Serial2Socket.dispatch (msg)) {
1259+ sendOk = false ;
1260+ esp3d_log_e (" Socket dispatch failed" );
1261+ }
1262+ break ;
12191263#endif // COMMUNICATION_PROTOCOL == SOCKET_SERIAL
12201264
12211265#if defined(ESP_SERIAL_BRIDGE_OUTPUT)
12221266 case ESP3DClientType::serial_bridge:
1267+ esp3d_log (" Serial bridge message" );
12231268 if (!serial_bridge_service.dispatch (msg)) {
12241269 sendOk = false ;
12251270 esp3d_log_e (" Serial bridge dispatch failed" );
@@ -1229,6 +1274,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
12291274
12301275#ifdef WS_DATA_FEATURE
12311276 case ESP3DClientType::websocket:
1277+ esp3d_log (" Websocket message" );
12321278 if (!websocket_data_server.dispatch (msg)) {
12331279 sendOk = false ;
12341280 esp3d_log_e (" Telnet dispatch failed" );
@@ -1238,6 +1284,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
12381284
12391285#ifdef TELNET_FEATURE
12401286 case ESP3DClientType::telnet:
1287+ esp3d_log (" Telnet message" );
12411288 if (!telnet_server.dispatch (msg)) {
12421289 sendOk = false ;
12431290 esp3d_log_e (" Telnet dispatch failed" );
@@ -1247,30 +1294,24 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
12471294
12481295#ifdef BLUETOOTH_FEATURE
12491296 case ESP3DClientType::bluetooth:
1297+ esp3d_log (" Bluetooth message" );
12501298 if (!bt_service.dispatch (msg)) {
12511299 sendOk = false ;
12521300 esp3d_log_e (" Bluetooth dispatch failed" );
12531301 }
12541302 break ;
12551303#endif // BLUETOOTH_FEATURE
12561304
1257- #if defined(ESP3DLIB_ENV) && COMMUNICATION_PROTOCOL == SOCKET_SERIAL
1258- case ESP3DClientType::socket_serial:
1259- if (!Serial2Socket.dispatch (msg)) {
1260- sendOk = false ;
1261- esp3d_log_e (" Socket dispatch failed" );
1262- }
1263- break ;
1264- #endif // defined(ESP3DLIB_ENV) && COMMUNICATION_PROTOCOL == SOCKET_SERIAL
1265-
12661305#ifdef HTTP_FEATURE
12671306 case ESP3DClientType::webui_websocket:
1307+ esp3d_log (" Webui websocket message" );
12681308 if (!websocket_terminal_server.dispatch (msg)) {
12691309 sendOk = false ;
12701310 esp3d_log_e (" Webui websocket dispatch failed" );
12711311 }
12721312 break ;
12731313 case ESP3DClientType::http:
1314+ esp3d_log (" Http message" );
12741315 if (!HTTP_Server::dispatch (msg)) {
12751316 sendOk = false ;
12761317 esp3d_log_e (" Webui websocket dispatch failed" );
@@ -1279,6 +1320,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
12791320#endif // HTTP_FEATURE
12801321#if defined(DISPLAY_DEVICE)
12811322 case ESP3DClientType::rendering:
1323+ esp3d_log (" Rendering message" );
12821324 if (!esp3d_display.dispatch (msg)) {
12831325 sendOk = false ;
12841326 esp3d_log_e (" Display dispatch failed" );
@@ -1288,6 +1330,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
12881330
12891331#if COMMUNICATION_PROTOCOL == MKS_SERIAL
12901332 case ESP3DClientType::mks_serial:
1333+ esp3d_log (" MKS Serial message" );
12911334 if (!MKSService::dispatch (msg)) {
12921335 sendOk = false ;
12931336 esp3d_log_e (" MKS Serial dispatch failed" );
@@ -1297,6 +1340,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
12971340
12981341#ifdef PRINTER_HAS_DISPLAY
12991342 case ESP3DClientType::remote_screen:
1343+ esp3d_log (" Remote screen message" );
13001344 // change target to output client
13011345 msg->target = getOutputClient ();
13021346 // change text to GCODE M117
@@ -1315,6 +1359,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
13151359 break ;
13161360#endif // PRINTER_HAS_DISPLAY
13171361 case ESP3DClientType::all_clients:
1362+ esp3d_log (" All clients message" );
13181363 // Add each client one by one
13191364#ifdef PRINTER_HAS_DISPLAY
13201365 if (msg->origin != ESP3DClientType::remote_screen &&
@@ -1410,6 +1455,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
14101455#endif // ESP_SERIAL_BRIDGE_OUTPUT
14111456
14121457#ifdef BLUETOOTH_FEATURE
1458+ // FIXME: add test if connected to avoid unnecessary copy
14131459 if (msg->origin != ESP3DClientType::bluetooth) {
14141460 String msgstr = (const char *)msg->data ;
14151461 msgstr.trim ();
@@ -1432,7 +1478,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
14321478#endif // BLUETOOTH_FEATURE
14331479
14341480#ifdef TELNET_FEATURE
1435- if (msg->origin != ESP3DClientType::telnet) {
1481+ if (msg->origin != ESP3DClientType::telnet && telnet_server. isConnected () ) {
14361482 String msgstr = (const char *)msg->data ;
14371483 msgstr.trim ();
14381484 if (msgstr.length () > 0 ) {
@@ -1455,6 +1501,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
14551501
14561502#ifdef HTTP_FEATURE // http cannot be in all client because it depend of any
14571503 // connection of the server
1504+ // FIXME: add test if connected to avoid unnecessary copy
14581505 if (msg->origin != ESP3DClientType::webui_websocket) {
14591506 String msgstr = (const char *)msg->data ;
14601507 msgstr.trim ();
@@ -1477,6 +1524,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
14771524#endif // HTTP_FEATURE
14781525
14791526#ifdef WS_DATA_FEATURE
1527+ // FIXME: add test if connected to avoid unnecessary copy
14801528 if (msg->origin != ESP3DClientType::websocket) {
14811529 String msgstr = (const char *)msg->data ;
14821530 msgstr.trim ();
@@ -1514,7 +1562,7 @@ bool ESP3DCommands::dispatch(ESP3DMessage *msg) {
15141562 }
15151563 // clear message
15161564 if (!sendOk) {
1517- esp3d_log (" Send msg failed" );
1565+ esp3d_log_e (" Send msg failed" );
15181566 ESP3DMessageManager::deleteMsg (msg);
15191567 }
15201568 return sendOk;
0 commit comments