99#include " font_awesome_symbols.h"
1010#include " iot/thing_manager.h"
1111#include " assets/lang_config.h"
12+ #include " mcp_server.h"
1213
1314#if CONFIG_USE_AUDIO_PROCESSOR
1415#include " afe_audio_processor.h"
@@ -41,7 +42,7 @@ static const char* const STATE_STRINGS[] = {
4142
4243Application::Application () {
4344 event_group_ = xEventGroupCreate ();
44- background_task_ = new BackgroundTask (4096 * 8 );
45+ background_task_ = new BackgroundTask (4096 * 7 );
4546
4647#if CONFIG_USE_AUDIO_PROCESSOR
4748 audio_processor_ = std::make_unique<AfeAudioProcessor>();
@@ -425,12 +426,15 @@ void Application::Start() {
425426 protocol_->server_sample_rate (), codec->output_sample_rate ());
426427 }
427428 SetDecodeSampleRate (protocol_->server_sample_rate (), protocol_->server_frame_duration ());
429+
430+ #if CONFIG_IOT_PROTOCOL_XIAOZHI
428431 auto & thing_manager = iot::ThingManager::GetInstance ();
429432 protocol_->SendIotDescriptors (thing_manager.GetDescriptorsJson ());
430433 std::string states;
431434 if (thing_manager.GetStatesJson (states, false )) {
432435 protocol_->SendIotStates (states);
433436 }
437+ #endif
434438 });
435439 protocol_->OnAudioChannelClosed ([this , &board]() {
436440 board.SetPowerSaveMode (true );
@@ -465,7 +469,7 @@ void Application::Start() {
465469 });
466470 } else if (strcmp (state->valuestring , " sentence_start" ) == 0 ) {
467471 auto text = cJSON_GetObjectItem (root, " text" );
468- if (text != NULL ) {
472+ if (cJSON_IsString ( text) ) {
469473 ESP_LOGI (TAG, " << %s" , text->valuestring );
470474 Schedule ([this , display, message = std::string (text->valuestring )]() {
471475 display->SetChatMessage (" assistant" , message.c_str ());
@@ -474,31 +478,40 @@ void Application::Start() {
474478 }
475479 } else if (strcmp (type->valuestring , " stt" ) == 0 ) {
476480 auto text = cJSON_GetObjectItem (root, " text" );
477- if (text != NULL ) {
481+ if (cJSON_IsString ( text) ) {
478482 ESP_LOGI (TAG, " >> %s" , text->valuestring );
479483 Schedule ([this , display, message = std::string (text->valuestring )]() {
480484 display->SetChatMessage (" user" , message.c_str ());
481485 });
482486 }
483487 } else if (strcmp (type->valuestring , " llm" ) == 0 ) {
484488 auto emotion = cJSON_GetObjectItem (root, " emotion" );
485- if (emotion != NULL ) {
489+ if (cJSON_IsString ( emotion) ) {
486490 Schedule ([this , display, emotion_str = std::string (emotion->valuestring )]() {
487491 display->SetEmotion (emotion_str.c_str ());
488492 });
489493 }
494+ #if CONFIG_IOT_PROTOCOL_MCP
495+ } else if (strcmp (type->valuestring , " mcp" ) == 0 ) {
496+ auto payload = cJSON_GetObjectItem (root, " payload" );
497+ if (cJSON_IsObject (payload)) {
498+ McpServer::GetInstance ().ParseMessage (payload);
499+ }
500+ #endif
501+ #if CONFIG_IOT_PROTOCOL_XIAOZHI
490502 } else if (strcmp (type->valuestring , " iot" ) == 0 ) {
491503 auto commands = cJSON_GetObjectItem (root, " commands" );
492- if (commands != NULL ) {
504+ if (cJSON_IsArray ( commands) ) {
493505 auto & thing_manager = iot::ThingManager::GetInstance ();
494506 for (int i = 0 ; i < cJSON_GetArraySize (commands); ++i) {
495507 auto command = cJSON_GetArrayItem (commands, i);
496508 thing_manager.Invoke (command);
497509 }
498510 }
511+ #endif
499512 } else if (strcmp (type->valuestring , " system" ) == 0 ) {
500513 auto command = cJSON_GetObjectItem (root, " command" );
501- if (command != NULL ) {
514+ if (cJSON_IsString ( command) ) {
502515 ESP_LOGI (TAG, " System command: %s" , command->valuestring );
503516 if (strcmp (command->valuestring , " reboot" ) == 0 ) {
504517 // Do a reboot if user requests a OTA update
@@ -513,7 +526,7 @@ void Application::Start() {
513526 auto status = cJSON_GetObjectItem (root, " status" );
514527 auto message = cJSON_GetObjectItem (root, " message" );
515528 auto emotion = cJSON_GetObjectItem (root, " emotion" );
516- if (status != NULL && message != NULL && emotion != NULL ) {
529+ if (cJSON_IsString ( status) && cJSON_IsString ( message) && cJSON_IsString ( emotion) ) {
517530 Alert (status->valuestring , message->valuestring , emotion->valuestring , Lang::Sounds::P3_VIBRATION);
518531 } else {
519532 ESP_LOGW (TAG, " Alert command requires status, message and emotion" );
@@ -620,7 +633,10 @@ void Application::OnClockTimer() {
620633 clock_ticks_++;
621634
622635 // Print the debug info every 10 seconds
623- if (clock_ticks_ % 10 == 0 ) {
636+ if (clock_ticks_ % 3 == 0 ) {
637+ // char buffer[500];
638+ // vTaskList(buffer);
639+ // ESP_LOGI(TAG, "Task list: \n%s", buffer);
624640 // SystemInfo::PrintRealTimeStats(pdMS_TO_TICKS(1000));
625641
626642 int free_sram = heap_caps_get_free_size (MALLOC_CAP_INTERNAL);
@@ -850,7 +866,9 @@ void Application::SetDeviceState(DeviceState state) {
850866 display->SetStatus (Lang::Strings::LISTENING);
851867 display->SetEmotion (" neutral" );
852868 // Update the IoT states before sending the start listening command
869+ #if CONFIG_IOT_PROTOCOL_XIAOZHI
853870 UpdateIotStates ();
871+ #endif
854872
855873 // Make sure the audio processor is running
856874 if (!audio_processor_->IsRunning ()) {
@@ -910,11 +928,13 @@ void Application::SetDecodeSampleRate(int sample_rate, int frame_duration) {
910928}
911929
912930void Application::UpdateIotStates () {
931+ #if CONFIG_IOT_PROTOCOL_XIAOZHI
913932 auto & thing_manager = iot::ThingManager::GetInstance ();
914933 std::string states;
915934 if (thing_manager.GetStatesJson (states, true )) {
916935 protocol_->SendIotStates (states);
917936 }
937+ #endif
918938}
919939
920940void Application::Reboot () {
@@ -955,3 +975,11 @@ bool Application::CanEnterSleepMode() {
955975 // Now it is safe to enter sleep mode
956976 return true ;
957977}
978+
979+ void Application::SendMcpMessage (const std::string& payload) {
980+ Schedule ([this , payload]() {
981+ if (protocol_) {
982+ protocol_->SendMcpMessage (payload);
983+ }
984+ });
985+ }
0 commit comments