avs-device-sdk/docs/AVS SDK Porting Document(1....

20 KiB
Raw Blame History

1. 修改根目录编译配置文件 /CMakeLists.txt 增加以下内容

if (COMPANION_APP_AUTH)
	add_definitions("-DCOMPANION_APP_AUTH")
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")

set(AUTH_APP_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/extension/avs-app-auth/include" CACHE INTERNAL "")

if (ASPMIC)
	add_subdirectory("extension/mic-asp-sdk")
endif()

add_subdirectory("extension/avs-app-auth")
add_subdirectory("extension/avs-msg-sdk")

2. 增加CA认证功能

2.1. 修改编译配置文件SampleApp/src/CMakeLists.txt增加CA认证编译配置

 # 修改对应代码增加CA认证库
if (COMPANION_APP_AUTH)
    target_link_libraries(LibSampleApp 
    CAAuthDelegate
    app_auth
)
else ()
    target_link_libraries(LibSampleApp CBLAuthDelegate)
endif ()

2.2. 增加以下目录以及目录下的所有源码文件

  • SampleApp/Authorization/CAAuthDelegate/
  • applications/acsdkCAAuthorizationDelegate/
  • applications\acsdkSampleApplicationCAAuthRequester/

2.3. 修改编译配置文件SampleApp/Authorization/CMakeLists.txt 增加CA认证源码支持

if (COMPANION_APP_AUTH)
    add_subdirectory("CAAuthDelegate")
else ()
    add_subdirectory("CBLAuthDelegate")
endif ()

2.4. 修改编译配置文件applications/CMakeLists.txt 增加CA认证源码支持

2.4.1. 删除原配置

~~add_subdirectory("acsdkCBLAuthorizationDelegate")~~  

2.4.2. 增加以下代码

if (COMPANION_APP_AUTH)
    add_subdirectory("acsdkSampleApplicationCAAuthRequester")
    add_subdirectory("acsdkCAAuthorizationDelegate")
else ()
    add_subdirectory("acsdkSampleApplicationCBLAuthRequester")
    add_subdirectory("acsdkCBLAuthorizationDelegate")
endif ()    

2.5. 修改编译配置文件applications/acsdkDefaultSampleApplicationOptions/src/CMakeLists.txt 增加CA认证源码支持

target_link_libraries(acsdkDefaultSampleApplicationOptions
    acsdkCore
    acsdkManufactory
    acsdkNullMetricRecorder
    acsdkSampleApplicationInterfaces
    acsdkShared
    )
if (COMPANION_APP_AUTH)
    target_link_libraries(acsdkDefaultSampleApplicationOptions
            acsdkSampleApplicationCAAuthRequester
            CAAuthDelegate
            )
else ()
    target_link_libraries(acsdkDefaultSampleApplicationOptions
            acsdkSampleApplicationCBLAuthRequester
            CBLAuthDelegate
            )
endif ()

2.6. 修改编译配置文件applications/acsdkPreviewAlexaClient/src/CMakeLists.txt 增加CA认证源码支持

if (COMPANION_APP_AUTH)
    SET(ACSDKAUTHORIZATIONDELEGATE_LIB acsdkCAAuthorizationDelegate)
else ()
    SET(ACSDKAUTHORIZATIONDELEGATE_LIB acsdkCBLAuthorizationDelegate)
endif ()  

2.7. 修改编译配置文件cmakeBuild/cmake/DefaultLibNames.cmake 增加CA认证源码支持

if (COMPANION_APP_AUTH)
    UseDefaultIfNotSet(ACSDKAUTHORIZATIONDELEGATE_LIB acsdkCAAuthorizationDelegate)
elseif()
    UseDefaultIfNotSet(ACSDKAUTHORIZATIONDELEGATE_LIB acsdkCBLAuthorizationDelegate)
endif()

2.8. 修改源码

2.8.1. applications/acsdkDefaultSampleApplicationOptions/include/acsdkDefaultSampleApplicationOptions/DefaultSampleApplicationOptionsComponent.h

 #ifdef COMPANION_APP_AUTH
    #include <CAAuthDelegate/CAAuthRequesterInterface.h>
 #else
    #include <CBLAuthDelegate/CBLAuthRequesterInterface.h>
 #endif
 
  ......

 using SampleApplicationOptionsComponent = acsdkManufactory::Component<
 std::shared_ptr<avsCommon::sdkInterfaces::AuthDelegateInterface>,
 std::shared_ptr<avsCommon::utils::logger::Logger>,
 std::shared_ptr<avsCommon::utils::metrics::MetricRecorderInterface>,
  #ifdef COMPANION_APP_AUTH
     acsdkManufactory::Import<std::shared_ptr<authorization::caAuthDelegate::CAAuthRequesterInterface>>,
  #else
     acsdkManufactory::Import<std::shared_ptr<authorization::cblAuthDelegate::CBLAuthRequesterInterface>>,
  #endif
 acsdkManufactory::Import<std::unique_ptr<avsCommon::utils::libcurlUtils::HttpPostInterface>>,
 acsdkManufactory::Import<std::shared_ptr<avsCommon::utils::DeviceInfo>>,
 acsdkManufactory::Import<std::shared_ptr<registrationManager::CustomerDataManagerInterface>>,
 acsdkManufactory::Import<std::shared_ptr<acsdkCryptoInterfaces::CryptoFactoryInterface>>,
 acsdkManufactory::Import<std::shared_ptr<acsdkCryptoInterfaces::KeyStoreInterface>>>;

2.8.2. applications/acsdkDefaultSampleApplicationOptions/src/DefaultSampleApplicationOptionsComponent.cpp

 #ifdef COMPANION_APP_AUTH
    #include <CAAuthDelegate/CAAuthDelegate.h>
    #include <CAAuthDelegate/SQLiteCAAuthDelegateStorage.h>
 #else
    #include <CBLAuthDelegate/CBLAuthDelegate.h>
    #include <CBLAuthDelegate/SQLiteCBLAuthDelegateStorage.h>
 #endif
 
 ......
 
 #ifdef COMPANION_APP_AUTH
    using namespace authorization::caAuthDelegate;
 #else
    using namespace authorization::cblAuthDelegate;
 #endif

 ......

 static acsdkManufactory::Component<
     std::shared_ptr<avsCommon::sdkInterfaces::AuthDelegateInterface>,
 #ifdef COMPANION_APP_AUTH
     acsdkManufactory::Import<std::shared_ptr<authorization::caAuthDelegate::CAAuthRequesterInterface>>,
 #else
     acsdkManufactory::Import<std::shared_ptr<authorization::cblAuthDelegate::CBLAuthRequesterInterface>>,
 #endif
     acsdkManufactory::Import<std::shared_ptr<avsCommon::utils::DeviceInfo>>,
     acsdkManufactory::Import<std::shared_ptr<avsCommon::utils::configuration::ConfigurationNode>>,
     acsdkManufactory::Import<std::unique_ptr<avsCommon::utils::libcurlUtils::HttpPostInterface>>,
     acsdkManufactory::Import<std::shared_ptr<registrationManager::CustomerDataManagerInterface>>,
     acsdkManufactory::Import<std::shared_ptr<acsdkCryptoInterfaces::CryptoFactoryInterface>>,
     acsdkManufactory::Import<std::shared_ptr<acsdkCryptoInterfaces::KeyStoreInterface>>>

 ......

    return ComponentAccumulator<>()
 #ifdef COMPANION_APP_AUTH
         .addRetainedFactory(CAAuthDelegate::createAuthDelegateInterface)
         .addRetainedFactory(SQLiteCAAuthDelegateStorage::createCAAuthDelegateStorageInterface);
 #else
         .addRetainedFactory(CBLAuthDelegate::createAuthDelegateInterface)
         .addRetainedFactory(SQLiteCBLAuthDelegateStorage::createCBLAuthDelegateStorageInterface);
 #endif
}

2.8.3. applications/acsdkPreviewAlexaClient/include/acsdkPreviewAlexaClient/PreviewAlexaClientComponent.h

 #ifdef COMPANION_APP_AUTH
    #include <CAAuthDelegate/CAAuthDelegateStorageInterface.h>
    #include <CAAuthDelegate/CAAuthRequesterInterface.h>
 #else
    #include <CBLAuthDelegate/CBLAuthDelegateStorageInterface.h>
    #include <CBLAuthDelegate/CBLAuthRequesterInterface.h>
 #endif

2.8.4. applications/acsdkPreviewAlexaClient/src/PreviewAlexaClientComponent.cpp

 #ifdef COMPANION_APP_AUTH
    #include <acsdkSampleApplicationCAAuthRequester/SampleApplicationCAAuthRequester.h>
 #else
    #include <acsdkSampleApplicationCBLAuthRequester/SampleApplicationCBLAuthRequester.h>
 #endif
 
 ......
 
 #ifdef COMPANION_APP_AUTH
    using namespace acsdkSampleApplicationCAAuthRequester;
 #else
    using namespace acsdkSampleApplicationCBLAuthRequester;
 #endif
 
 ......
 
         .addRetainedFactory(createUIManagerInterface)
         .addRetainedFactory(defaultClient::EqualizerRuntimeSetup::createEqualizerRuntimeSetupInterface)
         .addRequiredFactory(sampleApp::CaptionPresenter::createCaptionPresenterInterface)
         .addRetainedFactory(sampleApp::LocaleAssetsManager::createLocaleAssetsManagerInterface)
         .addRetainedFactory(sampleApp::SampleEqualizerModeController::createEqualizerModeControllerInterface)
         .addRetainedFactory(sampleApp::UIManager::create)
 #ifdef COMPANION_APP_AUTH
         .addUnloadableFactory(SampleApplicationCAAuthRequester::createCAAuthRequesterInterface)
 #else
         .addUnloadableFactory(SampleApplicationCBLAuthRequester::createCBLAuthRequesterInterface)
 #endif
         /// SQLite implementations of databases used by Capability Agents and other components.
         /// Applications may choose to replace these with their own database implementations.
         .addRetainedFactory(acsdkAlerts::storage::SQLiteAlertStorage::createAlertStorageInterface)
         .addRetainedFactory(acsdkBluetooth::SQLiteBluetoothStorage::createBluetoothStorageInterface)
         .addRetainedFactory(acsdkNotifications::SQLiteNotificationsStorage::createNotificationsStorageInterface)
         .addRetainedFactory(certifiedSender::SQLiteMessageStorage::createMessageStorageInterface)

2.8.5. AVSCommon/SDKInterfaces/include/AVSCommon/SDKInterfaces/AuthObserverInterface.h

        AUTHORIZATION_PENDING,
        /// Client should slow down in the rate of requests polling for an access token.
        SLOW_DOWN,
        /// Internal error in client code.
        INTERNAL_ERROR,
        /// Client ID not valid for use with code based linking.
        INVALID_CLIENT_ID
    };

    ......

      case AuthObserverInterface::Error::AUTHORIZATION_PENDING:
            return stream << "AUTHORIZATION_PENDING";
        case AuthObserverInterface::Error::SLOW_DOWN:
            return stream << "SLOW_DOWN";
        case AuthObserverInterface::Error::INTERNAL_ERROR:
            return stream << "INTERNAL_ERROR";
        case AuthObserverInterface::Error::INVALID_CLIENT_ID:
            return stream << "INVALID_CLIENT_ID";
    }

2.8.6. core/Authorization/acsdkAuthorization/include/acsdkAuthorization/LWA/LWAAuthorizationAdapter.h

 #ifdef COMPANION_APP_AUTH
     bool authorizeUsingCA(
         const std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CAAuthorizationObserverInterface>& observer) override;
     bool authorizeUsingCAWithCustomerProfile(
         const std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CAAuthorizationObserverInterface>& observer) override;
 #else
     bool authorizeUsingCBL(
         const std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CBLAuthorizationObserverInterface>& observer) override;
     bool authorizeUsingCBLWithCustomerProfile(
         const std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CBLAuthorizationObserverInterface>& observer) override;
 #endif
 
 ......
 
 #ifdef COMPANION_APP_AUTH
     bool authorizeUsingCAHelper(
         const std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CAAuthorizationObserverInterface>& observer,
         bool requestCustomerProfile);
 #else
     bool authorizeUsingCBLHelper(
         const std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CBLAuthorizationObserverInterface>& observer,
         bool requestCustomerProfile);
 #endif
 
 ......
 
     /// The interval to make token requests to LWA.
     std::chrono::seconds m_tokenRequestInterval;
 
     /// The observer that will respond to CBL related callbacks.
 #ifdef COMPANION_APP_AUTH
     std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CAAuthorizationObserverInterface> m_cblRequester;
 #else
     std::shared_ptr<acsdkAuthorizationInterfaces::lwa::CBLAuthorizationObserverInterface> m_cblRequester;
 #endif
     /// Whether to request extended customer profile information such as name and email.
     bool m_requestCustomerProfile;
     /// @}
 
     /// True if an authorization failure was reported for the current value of m_accessToken.
     bool m_authFailureReported;

2.8.7. core/Authorization/acsdkAuthorization/include/acsdkAuthorization/LWA/LWAAuthorizationConfiguration.h

     * @return The URL to use when getting the user profile.
     */
    std::string getCustomerProfileUrl() const;

    std::string getManufacturerName() const;

    std::string getDescription() const;

 private:
    /**
     * Initialize the new instance.
     *
     * @param root The root level node of the raw configuration from which to populate this instance.
     * @param deviceInfo The deviceInfo instance.

2.8.8. core/Authorization/acsdkAuthorization/src/LWA/LWAAuthorizationAdapter.cpp

     case AuthObserverInterface::Error::INTERNAL_ERROR:
     case AuthObserverInterface::Error::INVALID_CLIENT_ID:
         return result;
     // Retry Errors
     case AuthObserverInterface::Error::UNKNOWN_ERROR:
 
 ......
 
     case AuthObserverInterface::Error::INTERNAL_ERROR:
     case AuthObserverInterface::Error::INVALID_CLIENT_ID:
         return result;
     /// Retriable errors.
     case AuthObserverInterface::Error::SLOW_DOWN:
 
 ......
 
     case AuthObserverInterface::Error::INVALID_REQUEST:
     if (!m_refreshTokenResponse.isRefreshTokenVerified) {
         error = AuthObserverInterface::Error::INVALID_CLIENT_ID;
     }
     // Falls through
     case AuthObserverInterface::Error::AUTHORIZATION_FAILED:
     
 ......
     case AuthObserverInterface::Error::INTERNAL_ERROR:
     case AuthObserverInterface::Error::INVALID_CLIENT_ID: {
         updateStateAndNotifyManager(AuthObserverInterface::State::UNRECOVERABLE_ERROR, error);
         return FlowState::IDLE;
     }
 
 ......
 
 #ifdef COMPANION_APP_AUTH
 bool LWAAuthorizationAdapter::authorizeUsingCAHelper(
     const std::shared_ptr<CAAuthorizationObserverInterface>& observer,
 #else
 bool LWAAuthorizationAdapter::authorizeUsingCBLHelper(
     const std::shared_ptr<CBLAuthorizationObserverInterface>& observer,
 #endif
     bool requestCustomerProfile) {
     if (!observer) {
         ACSDK_ERROR(LX("authorizeUsingCBLHelperFailed").d("reason", "nullObserver"));
         return false;
     }
 
 ......
 
 #ifdef COMPANION_APP_AUTH
 bool LWAAuthorizationAdapter::authorizeUsingCA(const std::shared_ptr<CAAuthorizationObserverInterface>& observer) {
     return authorizeUsingCAHelper(observer, false);
 }
 
 bool LWAAuthorizationAdapter::authorizeUsingCAWithCustomerProfile(
     const std::shared_ptr<CAAuthorizationObserverInterface>& observer) {
     return authorizeUsingCAHelper(observer, true);
 }
 #else
 bool LWAAuthorizationAdapter::authorizeUsingCBL(const std::shared_ptr<CBLAuthorizationObserverInterface>& observer) {
     return authorizeUsingCBLHelper(observer, false);
 }
 
 bool LWAAuthorizationAdapter::authorizeUsingCBLWithCustomerProfile(
     const std::shared_ptr<CBLAuthorizationObserverInterface>& observer) {
     return authorizeUsingCBLHelper(observer, true);
 }
 #endif
 std::string LWAAuthorizationAdapter::getId() {
     // m_adapterId is const, no need to lock.
     ACSDK_DEBUG5(LX("getId").d("id", m_adapterId));
     return m_adapterId;
 }

2.8.9. core/Authorization/acsdkAuthorization/src/LWA/LWAAuthorizationConfiguration.cpp

std::string LWAAuthorizationConfiguration::getCustomerProfileUrl() const {
    return CUSTOMER_PROFILE_URL;
}

std::string LWAAuthorizationConfiguration::getManufacturerName() const {
    return m_deviceInfo->getManufacturerName();
}

std::string LWAAuthorizationConfiguration::getDescription() const {
    return m_deviceInfo->getDeviceDescription();
}

bool LWAAuthorizationConfiguration::initScopeData() {
    ACSDK_DEBUG5(LX("initScopeData"));

2.8.10. 增加文件 core/Authorization/acsdkAuthorizationInterfaces/include/acsdkAuthorizationInterfaces/LWA/CAAuthorizationObserverInterface.h

2.8.11. core/Authorization/acsdkAuthorizationInterfaces/include/acsdkAuthorizationInterfaces/LWA/LWAAuthorizationInterface.h

 #include <memory>
 
 #include <acsdkAuthorizationInterfaces/AuthorizationInterface.h>
 
 #ifdef COMPANION_APP_AUTH
 #include "CAAuthorizationObserverInterface.h"
 #else
 #include "CBLAuthorizationObserverInterface.h"
 #endif
 
 ......
 
     /**
      * Authorize using LWA and the Code Based Linking method.
      *
      * @param observer The CBL observer which will receive a callback to display information back to the user.
      * This will be be sent in an @c CBLAuthorizationObserverInterface callback.
      */
 #ifdef COMPANION_APP_AUTH
     virtual bool authorizeUsingCA(const std::shared_ptr<CAAuthorizationObserverInterface>& observer) = 0;
 #else
     virtual bool authorizeUsingCBL(const std::shared_ptr<CBLAuthorizationObserverInterface>& observer) = 0;
 #endif
     /**
      * Authorize using LWA and the Code Based Linking method. This API additionally requets customer profile information
      * (name, email).
      *
      * @attention THE USER MUST CONSENT (EX. UI DIALOG) BEFORE APPLICATION CAN REQUEST CUSTOMER PROFILE INFORMATION.
      *
      * @param observer The CBL observer which will receive a callback to display information back to the user.
      * This will be sent in an @c CBLAuthorizationObserverInterface callback.
      */
 #ifdef COMPANION_APP_AUTH
     virtual bool authorizeUsingCAWithCustomerProfile(
         const std::shared_ptr<CAAuthorizationObserverInterface>& observer) = 0;
 #else
     virtual bool authorizeUsingCBLWithCustomerProfile(
         const std::shared_ptr<CBLAuthorizationObserverInterface>& observer) = 0;
 #endif
 };

2.8.12. SampleApp/Authorization/CBLAuthDelegate/src/CBLAuthDelegate.cpp

    case AuthObserverInterface::Error::UNSUPPORTED_GRANT_TYPE:
    case AuthObserverInterface::Error::INTERNAL_ERROR:
    case AuthObserverInterface::Error::INVALID_CBL_CLIENT_ID: {
        setAuthState(AuthObserverInterface::State::UNRECOVERABLE_ERROR);
        return FlowState::STOPPING;
    }

......

    case AuthObserverInterface::Error::INVALID_CODE_PAIR:
    case AuthObserverInterface::Error::INTERNAL_ERROR:
    case AuthObserverInterface::Error::INVALID_CBL_CLIENT_ID: {
        setAuthState(AuthObserverInterface::State::UNRECOVERABLE_ERROR);
        return FlowState::STOPPING;
    }

......

    case AuthObserverInterface::Error::AUTHORIZATION_PENDING:
    case AuthObserverInterface::Error::SLOW_DOWN:
        m_timeToRefresh = calculateTimeToRetry(m_retryCount++);
        break;
    case AuthObserverInterface::Error::INVALID_REQUEST:
        if (newRefreshToken) {
            setAuthError(AuthObserverInterface::Error::INVALID_CBL_CLIENT_ID);
        }
    // Falls through
    case AuthObserverInterface::Error::AUTHORIZATION_FAILED:
    case AuthObserverInterface::Error::UNAUTHORIZED_CLIENT:
    case AuthObserverInterface::Error::INVALID_VALUE:
    case AuthObserverInterface::Error::AUTHORIZATION_EXPIRED:
    case AuthObserverInterface::Error::UNSUPPORTED_GRANT_TYPE:
    case AuthObserverInterface::Error::INVALID_CODE_PAIR:
    case AuthObserverInterface::Error::INTERNAL_ERROR:
    case AuthObserverInterface::Error::INVALID_CBL_CLIENT_ID: {
        setAuthState(AuthObserverInterface::State::UNRECOVERABLE_ERROR);
        return FlowState::STOPPING;

2.9. 增加以下目录以及子目录下所有新文件

  • extension/avs-app-auth/
  • extension/avs-msg-sdk/

3. 集成MIC语言采样SDK

3.1. 增加以下新文件

  • cmakeBuild/cmake/AspMic.cmake
  • SampleApp/include/SampleApp/AspMicrophoneWrapper.h
  • SampleApp/src/AspMicrophoneWrapper.cpp

3.2. 修改编译配置文件 cmakeBuild/BuildDefaults.cmake增加ASP MIC语音处理编译配置

 # Setup AspMic variables.
include_once(AspMic)

3.3. 修改编译配置文件 SampleApp/CMakeLists.txt增加ASP MIC语音处理编译配置

if (PORTAUDIO AND (GSTREAMER_MEDIA_PLAYER OR CUSTOM_MEDIA_PLAYER))
    set(VALID TRUE)
elseif (ASPMIC AND (GSTREAMER_MEDIA_PLAYER OR CUSTOM_MEDIA_PLAYER))
    set(VALID TRUE)
elseif (ANDROID_MEDIA_PLAYER AND ANDROID_MICROPHONE)
    set(VALID TRUE)
elseif (AUDIO_INJECTION AND (GSTREAMER_MEDIA_PLAYER OR ANDROID_MEDIA_PLAYER OR CUSTOM_MEDIA_PLAYER))
    set(VALID TRUE)
endif()

3.4. 增加以下目录以及子目录下所有新文件

  • extension/mic-asp-sdk/

4. 集成KWD唤醒

4.1 参考KWD集成文档