RDKTV-28536: Use JSOn Direct calls instead of Worker threads

Reason for change: Use as a temporary fix to address Thunder hang
Test Procedure: Monitor for Thunder hangs.
Risks: Low
Priority: P0
Change-Id: I3e9ff94e70fdfbbfdee37a982fc8616d3cb1661a
Signed-off-by: binuinbaraj <Binu_Inbaraj@cable.comcast.com>
(cherry picked from commit ca76c4580aa7f414c1ab29154c0d7e1844a47ae7)
diff --git a/recipes-extended/rdkservices/files/0003-UseJsonDirect.patch b/recipes-extended/rdkservices/files/0003-UseJsonDirect.patch
new file mode 100644
index 0000000..d067038
--- /dev/null
+++ b/recipes-extended/rdkservices/files/0003-UseJsonDirect.patch
@@ -0,0 +1,272 @@
+From 663fdb41cb3c44053fa71dfeeb960d93255c0947 Mon Sep 17 00:00:00 2001
+From: binbar366 <Binu_Inbaraj@comcast.com>
+Date: Thu, 14 Mar 2024 19:58:37 +0000
+Subject: [PATCH] Performance Metrics Compilation fix
+
+Signed-off-: Binu Inbaraj Binu_Inbaraj@comcast.com>
+---
+
+diff --git a/RDKShell/RDKShell.cpp b/RDKShell/RDKShell.cpp
+index 81f90535..d5fbf61d 100755
+--- a/RDKShell/RDKShell.cpp
++++ b/RDKShell/RDKShell.cpp
+@@ -417,29 +417,6 @@ namespace WPEFramework {
+             private:
+                 std::function<void()> _work;
+             };
+-            uint32_t cloneService(PluginHost::IShell* shell, const string& basecallsign, const string& newcallsign)
+-            {
+-                uint32_t result = Core::ERROR_ASYNC_FAILED;
+-                Core::Event event(false, true);
+-#ifndef USE_THUNDER_R4
+-                Core::IWorkerPool::Instance().Submit(Core::ProxyType<Core::IDispatchType<void>>(Core::ProxyType<Job>::Create([&]() {
+-#else
+-                Core::IWorkerPool::Instance().Submit(Core::ProxyType<Core::IDispatch>(Core::ProxyType<Job>::Create([&]() {
+-#endif /* USE_THUNDER_R4 */
+-                    auto interface = shell->QueryInterfaceByCallsign<PluginHost::IController>("");
+-                    if (interface == nullptr) {
+-                        result = Core::ERROR_UNAVAILABLE;
+-                        std::cout << "no IController" << std::endl;
+-                    } else {
+-                        result = interface->Clone(basecallsign, newcallsign);
+-                        std::cout << "IController clone status " << result << std::endl;
+-                        interface->Release();
+-                    }
+-                    event.SetEvent();
+-                })));
+-                event.Lock();
+-                return result;
+-            }
+             uint32_t getValue(PluginHost::IShell* shell, const string& ns, const string& key, string& value)
+             {
+                 uint32_t result;
+@@ -516,52 +493,6 @@ namespace WPEFramework {
+                 }
+                 return result;
+             }
+-            uint32_t activate(PluginHost::IShell* shell, const string& callsign)
+-            {
+-                uint32_t result = Core::ERROR_ASYNC_FAILED;
+-                Core::Event event(false, true);
+-#ifndef USE_THUNDER_R4
+-                Core::IWorkerPool::Instance().Submit(Core::ProxyType<Core::IDispatchType<void>>(Core::ProxyType<Job>::Create([&]() {
+-#else
+-                Core::IWorkerPool::Instance().Submit(Core::ProxyType<Core::IDispatch>(Core::ProxyType<Job>::Create([&]() {
+-#endif /* USE_THUNDER_R4 */
+-                    auto interface = shell->QueryInterfaceByCallsign<PluginHost::IShell>(callsign);
+-                    if (interface == nullptr) {
+-                        result = Core::ERROR_UNAVAILABLE;
+-                        std::cout << "no IShell for " << callsign << std::endl;
+-                    } else {
+-                        result = interface->Activate(PluginHost::IShell::reason::REQUESTED);
+-                        std::cout << "IShell activate status " << result << " for " << callsign << std::endl;
+-                        interface->Release();
+-                    }
+-                    event.SetEvent();
+-                })));
+-                event.Lock();
+-                return result;
+-            }
+-            uint32_t deactivate(PluginHost::IShell* shell, const string& callsign)
+-            {
+-                uint32_t result = Core::ERROR_ASYNC_FAILED;
+-                Core::Event event(false, true);
+-#ifndef USE_THUNDER_R4
+-                Core::IWorkerPool::Instance().Submit(Core::ProxyType<Core::IDispatchType<void>>(Core::ProxyType<Job>::Create([&]() {
+-#else
+-                Core::IWorkerPool::Instance().Submit(Core::ProxyType<Core::IDispatch>(Core::ProxyType<Job>::Create([&]() {
+-#endif /* USE_THUNDER_R4 */
+-                    auto interface = shell->QueryInterfaceByCallsign<PluginHost::IShell>(callsign);
+-                    if (interface == nullptr) {
+-                        result = Core::ERROR_UNAVAILABLE;
+-                        std::cout << "no IShell for " << callsign << std::endl;
+-                    } else {
+-                        result = interface->Deactivate(PluginHost::IShell::reason::REQUESTED);
+-                        std::cout << "IShell deactivate status " << result << " for " << callsign << std::endl;
+-                        interface->Release();
+-                    }
+-                    event.SetEvent();
+-                })));
+-                event.Lock();
+-                return result;
+-            }
+         }
+ 
+         struct JSONRPCDirectLink
+@@ -873,7 +804,12 @@ namespace WPEFramework {
+                 }
+ 		else if (requestName.compare("deactivateresidentapp") == 0)
+                 {
+-                    auto deactivateStatus = deactivate(mCurrentService, "ResidentApp");
++                    auto thunderController = std::unique_ptr<JSONRPCDirectLink>(new JSONRPCDirectLink(mCurrentService));
++                    JsonObject deactivateParams;
++                    deactivateParams.Set("callsign", "ResidentApp");
++                    JsonObject deactivateResult;
++                    int32_t deactivateStatus = thunderController->Invoke(0, "deactivate", deactivateParams, deactivateResult);
++                    // auto deactivateStatus = deactivate(mCurrentService, "ResidentApp");
+                     std::cout << "deactivating resident app status " << deactivateStatus << std::endl;
+                 }
+ 		else if (requestName.compare("susbscribeSystemEvent") == 0)
+@@ -2286,7 +2222,12 @@ namespace WPEFramework {
+                     std::cout << "callsign will be " << callsign << std::endl;
+                     if (callsign.compare("org.rdk.RDKShell.1") != 0)
+                     {
+-                        activate(mShell.mCurrentService, callsign);
++                        //get callsign
++                        JsonObject activateParams;
++                        activateParams.Set("callsign",callsign.c_str());
++                        JsonObject activateResult;
++                        thunderController->Invoke<JsonObject, JsonObject>(3500, "activate", activateParams, activateResult);
++                        //activate(mShell.mCurrentService, callsign);
+                     }
+                     else
+                     {
+@@ -3990,6 +3931,7 @@ namespace WPEFramework {
+                       originalPluginFound = true;
+                     }
+                 }
++                auto thunderController = std::unique_ptr<JSONRPCDirectLink>(new JSONRPCDirectLink(mCurrentService));
+                 if ((false == newPluginFound) && (false == originalPluginFound)) {
+                     PluginHost::IShell::state state;
+                     if (getServiceState(mCurrentService, callsign, state) == Core::ERROR_NONE) {
+@@ -4014,13 +3956,23 @@ namespace WPEFramework {
+                 else if (!newPluginFound)
+                 {
+                     std::cout << "attempting to clone type: " << type << " into " << callsign << std::endl;
+-                    uint32_t status = cloneService(mCurrentService, type, callsign);
++                    JsonObject joParams;
++                    joParams.Set("callsign", type);
++                    joParams.Set("newcallsign",callsign.c_str());
++                    JsonObject joResult;
++                    // setting wait Time to 2 seconds
++                    uint32_t status = thunderController->Invoke(RDKSHELL_THUNDER_TIMEOUT, "clone", joParams, joResult, true);
++                    //uint32_t status = cloneService(mCurrentService, type, callsign);
+ 
+                     std::cout << "clone status: " << status << std::endl;
+                     if (status > 0)
+                     {
+                         std::cout << "trying status one more time...\n";
+-                        status = cloneService(mCurrentService, type, callsign);
++                        JsonObject joParams2;
++                        joParams2.Set("callsign", type);
++                        joParams2.Set("newcallsign",callsign.c_str());
++                        status = thunderController->Invoke(RDKSHELL_THUNDER_TIMEOUT, "clone", joParams2, joResult, true);
++                        //status = cloneService(mCurrentService, type, callsign);
+                         std::cout << "clone status: " << status << std::endl;
+                     }
+ 
+@@ -4059,6 +4011,7 @@ namespace WPEFramework {
+                 string configString;
+ 
+                 uint32_t status = 0;
++                Core::JSON::ArrayType<PluginHost::MetaData::Service> joResult;
+                 status = getConfig(mCurrentService, callsign, configString);
+ 
+                 std::cout << "config status: " << status << std::endl;
+@@ -4254,13 +4207,19 @@ namespace WPEFramework {
+                             state == PluginHost::IShell::state::PRECONDITION)
+                         {
+                             launchType = RDKShellLaunchType::ACTIVATE;
+-                            status = activate(mCurrentService, callsign);
++                            JsonObject activateParams;
++                            activateParams.Set("callsign",callsign.c_str());
++                            status = thunderController->Invoke(RDKSHELL_THUNDER_TIMEOUT, "activate", activateParams, joResult);
++                            //status = activate(mCurrentService, callsign);
+ 
+                             std::cout << "activate 1 status: " << status << std::endl;
+                             if (status > 0)
+                             {
+                                 std::cout << "trying status one more time...\n";
+-                                status = activate(mCurrentService, callsign);
++                                JsonObject activateParams2;
++                                activateParams2.Set("callsign",callsign.c_str());
++                                status = thunderController->Invoke(RDKSHELL_THUNDER_TIMEOUT, "activate", activateParams2, joResult);
++                                //status = activate(mCurrentService, callsign);
+                                 std::cout << "activate 1 status: " << status << std::endl;
+                             }
+                         }
+@@ -4268,25 +4227,37 @@ namespace WPEFramework {
+                     else
+                     {
+                         launchType = RDKShellLaunchType::ACTIVATE;
+-                        status = activate(mCurrentService, callsign);
++                        JsonObject activateParams;
++                        activateParams.Set("callsign",callsign.c_str());
++                        status = thunderController->Invoke(RDKSHELL_THUNDER_TIMEOUT, "activate", activateParams, joResult);
++                        //status = activate(mCurrentService, callsign);
+                         std::cout << "activate 2 status: " << status << std::endl;
+                         if (status > 0)
+                         {
+                             std::cout << "trying status one more time...\n";
+-                            status = activate(mCurrentService, callsign);
++                            JsonObject activateParams2;
++                            activateParams2.Set("callsign",callsign.c_str());
++                            status = thunderController->Invoke(RDKSHELL_THUNDER_TIMEOUT, "activate", activateParams2, joResult);
++                            //status = activate(mCurrentService, callsign);
+                             std::cout << "activate 2 status: " << status << std::endl;
+                         }
+                     }
+                 }
+                 else
+                 {
+-                    status = activate(mCurrentService, callsign);
++                    JsonObject activateParams;
++                    activateParams.Set("callsign",callsign.c_str());
++                    status = thunderController->Invoke(RDKSHELL_THUNDER_TIMEOUT, "activate", activateParams, joResult);
++                    //status = activate(mCurrentService, callsign);
+ 
+                     std::cout << "activate 3 status: " << status << std::endl;
+                     if (status > 0)
+                     {
+                         std::cout << "trying status one more time...\n";
+-                        status = activate(mCurrentService, callsign);
++                        JsonObject activateParams2;
++                        activateParams2.Set("callsign",callsign.c_str());
++                        status = thunderController->Invoke(RDKSHELL_THUNDER_TIMEOUT, "activate", activateParams2, joResult);
++                        //status = activate(mCurrentService, callsign);
+                         std::cout << "activate 3 status: " << status << std::endl;
+                     }
+                 }
+@@ -4645,8 +4616,13 @@ namespace WPEFramework {
+                     returnResponse(false);
+                 }
+                 std::cout << "destroying " << callsign << std::endl;
++                JsonObject joParams;
++                joParams.Set("callsign",callsign.c_str());
++                JsonObject joResult;
++                auto thunderController = getThunderControllerClient();
+                 gDestroyMutex.lock();
+-                uint32_t status = deactivate(mCurrentService, callsign);
++                 uint32_t status = thunderController->Invoke<JsonObject, JsonObject>(RDKSHELL_THUNDER_TIMEOUT, "deactivate", joParams, joResult);
++                //uint32_t status = deactivate(mCurrentService, callsign);
+                 gDestroyMutex.unlock();
+                 if (status > 0)
+                 {
+@@ -5622,6 +5598,7 @@ namespace WPEFramework {
+             std::cout << "stopHdmiStatus status: " << stopHdmiStatus << std::endl;
+ 
+             sForceResidentAppLaunch = true;
++            auto thunderController = getThunderControllerClient();
+             string configString;
+ 
+             int32_t status = 0;
+@@ -5652,12 +5629,17 @@ namespace WPEFramework {
+ 
+             bool ret = true;
+             std::string callsign("ResidentApp");
+-            status = activate(mCurrentService, callsign);
++            JsonObject activateParams;
++            activateParams.Set("callsign",callsign.c_str());
++            JsonObject activateResult;
++            status = thunderController->Invoke<JsonObject, JsonObject>(3500, "activate", activateParams, activateResult);
++            //status = activate(mCurrentService, callsign);
+             std::cout << "activate resident app status: " << status << std::endl;
+             if (status > 0)
+             {
+                 std::cout << "trying status one more time...\n";
+-                status = activate(mCurrentService, callsign);
++                status = thunderController->Invoke<JsonObject, JsonObject>(3500, "activate", activateParams, activateResult);
++                //status = activate(mCurrentService, callsign);
+                 std::cout << "activate resident app status: " << status << std::endl;
+                 if (status > 0)
+                 {
diff --git a/recipes-extended/rdkservices/rdkservices_git.bb b/recipes-extended/rdkservices/rdkservices_git.bb
index 6896a32..f8a6537 100644
--- a/recipes-extended/rdkservices/rdkservices_git.bb
+++ b/recipes-extended/rdkservices/rdkservices_git.bb
@@ -27,6 +27,7 @@
            file://0001-RDK-41681-PR4013.patch \
            file://0002-Adding-Support-For-R4.patch \
            file://0001-Add-a-new-metrics-punch-through-on-the-OCDM-framework-rdkservice.patch \
+           file://0003-UseJsonDirect.patch \
           "
 
 # Nov 28 2023