Remove synchronous calls that can block from TV code (#32010)

* Remove synchronous calls

* move blocking calls to use a (future) separate thread pool

* cleanup and fix ci

* Restyled by clang-format (#32011)

Co-authored-by: Restyled.io <commits@restyled.io>

* address comments

* address comments

* Address comments

* Fix build

* Address comments

* cleanup

* cleanup

* Fix rotating id

* address comments

* fix CI

* Address comments

---------

Co-authored-by: restyled-io[bot] <32688539+restyled-io[bot]@users.noreply.github.com>
Co-authored-by: Restyled.io <commits@restyled.io>
diff --git a/src/controller/CommissionerDiscoveryController.h b/src/controller/CommissionerDiscoveryController.h
index f0dcf6d..c4c90c3 100644
--- a/src/controller/CommissionerDiscoveryController.h
+++ b/src/controller/CommissionerDiscoveryController.h
@@ -153,7 +153,6 @@
     virtual ~UserPrompter() = default;
 };
 
-// TODO: rename this to Passcode?
 class DLL_EXPORT PasscodeService
 {
 public:
@@ -162,15 +161,18 @@
      *   Called to determine if the given target app is available to the commissionee with the given given
      * vendorId/productId, and if so, return the passcode.
      *
+     * This will be called by the main chip thread so any blocking work should be moved to a separate thread.
+     *
+     * After lookup and attempting to obtain the passcode, implementor should call HandleContentAppCheck();
+     *
      *  @param[in]    vendorId           The vendorId in the DNS-SD advertisement of the requesting commissionee.
      *  @param[in]    productId          The productId in the DNS-SD advertisement of the requesting commissionee.
      *  @param[in]    rotatingId         The rotatingId in the DNS-SD advertisement of the requesting commissionee.
      *  @param[in]    info               App info to look for.
-     *  @param[in]    passcode           Passcode for the given commissionee, or 0 if passcode cannot be obtained.
      *
      */
-    virtual bool HasTargetContentApp(uint16_t vendorId, uint16_t productId, chip::CharSpan rotatingId,
-                                     chip::Protocols::UserDirectedCommissioning::TargetAppInfo & info, uint32_t & passcode) = 0;
+    virtual void LookupTargetContentApp(uint16_t vendorId, uint16_t productId, chip::CharSpan rotatingId,
+                                        chip::Protocols::UserDirectedCommissioning::TargetAppInfo & info) = 0;
 
     /**
      * @brief
@@ -186,18 +188,18 @@
 
     /**
      * @brief
-     *   Called to get the setup passcode from the content app corresponding to the given vendorId/productId
-     * Returns 0 if passcode cannot be obtained
+     *   Called to get the setup passcode from the content app corresponding to the given vendorId/productId.
      *
-     * If user responds with OK then implementor should call CommissionerRespondOk();
-     * If user responds with Cancel then implementor should call CommissionerRespondCancel();
+     * This will be called by the main chip thread so any blocking work should be moved to a separate thread.
+     *
+     * After attempting to obtain the passcode, implementor should call HandleContentAppPasscodeResponse();
      *
      *  @param[in]    vendorId           The vendorId in the DNS-SD advertisement of the requesting commissionee.
      *  @param[in]    productId          The productId in the DNS-SD advertisement of the requesting commissionee.
      *  @param[in]    rotatingId         The rotatingId in the DNS-SD advertisement of the requesting commissionee.
      *
      */
-    virtual uint32_t FetchCommissionPasscodeFromContentApp(uint16_t vendorId, uint16_t productId, chip::CharSpan rotatingId) = 0;
+    virtual void FetchCommissionPasscodeFromContentApp(uint16_t vendorId, uint16_t productId, chip::CharSpan rotatingId) = 0;
 
     virtual ~PasscodeService() = default;
 };
@@ -260,6 +262,11 @@
     void ResetState();
 
     /**
+     * Check whether we have a valid session (and reset state if not).
+     */
+    void ValidateSession();
+
+    /**
      * UserConfirmationProvider callback.
      *
      * Notification that a UDC protocol message was received.
@@ -282,10 +289,38 @@
     void Cancel();
 
     /**
+     * @brief
+     *   Called with the result of attempting to obtain the passcode from the content app corresponding to the given
+     * vendorId/productId.
+     *
+     *  @param[in]    passcode           Passcode for the given commissionee, or 0 if passcode cannot be obtained.
+     *
+     */
+    void HandleContentAppPasscodeResponse(uint32_t passcode);
+    void InternalHandleContentAppPasscodeResponse();
+
+    /**
+     * Cache the passcode to use for commissioning
+     */
+    inline void SetPasscode(uint32_t passcode) { mPasscode = passcode; }
+
+    /**
+     * @brief
+     *   Called with the result of attempting to lookup and obtain the passcode from the content app corresponding to the given
+     * target.
+     *
+     *  @param[in]    target             Target app info for app check.
+     *  @param[in]    passcode           Passcode for the given commissionee, or 0 if passcode cannot be obtained.
+     *
+     */
+    void HandleTargetContentAppCheck(chip::Protocols::UserDirectedCommissioning::TargetAppInfo target, uint32_t passcode);
+
+    /**
      * This method should be called with the passcode for the client
      * indicated in the UserPrompter's PromptForCommissionPasscode callback
      */
     void CommissionWithPasscode(uint32_t passcode);
+    void InternalCommissionWithPasscode();
 
     /**
      * This method should be called by the commissioner to indicate that commissioning succeeded.
@@ -337,6 +372,7 @@
      * Assign a PasscodeService
      */
     inline void SetPasscodeService(PasscodeService * passcodeService) { mPasscodeService = passcodeService; }
+    inline PasscodeService * GetPasscodeService() { return mPasscodeService; }
 
     /**
      * Assign a Commissioner Callback to perform commissioning once user consent has been given
@@ -371,6 +407,7 @@
     uint16_t mVendorId  = 0;
     uint16_t mProductId = 0;
     NodeId mNodeId      = 0;
+    uint32_t mPasscode  = 0;
 
     UserDirectedCommissioningServer * mUdcServer           = nullptr;
     UserPrompter * mUserPrompter                           = nullptr;