Update tuner default AIDL HAL.

*) Use tuner AIDL HAL return values.
*) Replace mTunerService with mTuner to avoid confusing.
*) Merge other HIDL HAL changes.
   *) ag/15371813, ag/15371814 and ag/15371815.

Bug: 191825295
Test: make and run VtsHalTvTunerTargetTest
Change-Id: Ibbf94c4fc36c3bda94a7e0f3e0697c83ac414d0e
diff --git a/tv/tuner/aidl/default/Demux.cpp b/tv/tuner/aidl/default/Demux.cpp
index 15022ee..d5a653b 100644
--- a/tv/tuner/aidl/default/Demux.cpp
+++ b/tv/tuner/aidl/default/Demux.cpp
@@ -18,6 +18,7 @@
 #define LOG_TAG "android.hardware.tv.tuner-service.example-Demux"
 
 #include <aidl/android/hardware/tv/tuner/DemuxQueueNotifyBits.h>
+#include <aidl/android/hardware/tv/tuner/Result.h>
 
 #include <utils/Log.h>
 #include "Demux.h"
@@ -32,7 +33,7 @@
 
 Demux::Demux(int32_t demuxId, std::shared_ptr<Tuner> tuner) {
     mDemuxId = demuxId;
-    mTunerService = tuner;
+    mTuner = tuner;
 }
 
 Demux::~Demux() {
@@ -43,17 +44,18 @@
 ::ndk::ScopedAStatus Demux::setFrontendDataSource(int32_t in_frontendId) {
     ALOGV("%s", __FUNCTION__);
 
-    if (mTunerService == nullptr) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_NO_INIT);
+    if (mTuner == nullptr) {
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::NOT_INITIALIZED));
     }
 
-    mFrontend = mTunerService->getFrontendById(in_frontendId);
-
+    mFrontend = mTuner->getFrontendById(in_frontendId);
     if (mFrontend == nullptr) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_STATE));
     }
 
-    mTunerService->setFrontendAsDemuxSource(in_frontendId, mDemuxId);
+    mTuner->setFrontendAsDemuxSource(in_frontendId, mDemuxId);
 
     return ::ndk::ScopedAStatus::ok();
 }
@@ -69,14 +71,16 @@
     if (in_cb == nullptr) {
         ALOGW("[Demux] callback can't be null");
         *_aidl_return = nullptr;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     std::shared_ptr<Filter> filter =
             ndk::SharedRefBase::make<Filter>(in_type, filterId, in_bufferSize, in_cb, ref<Demux>());
     if (!filter->createFilterMQ()) {
         *_aidl_return = nullptr;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::UNKNOWN_ERROR));
     }
 
     mFilters[filterId] = filter;
@@ -95,7 +99,8 @@
 
     if (!result) {
         *_aidl_return = nullptr;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     *_aidl_return = filter;
@@ -122,13 +127,15 @@
     if (!status.isOk()) {
         ALOGE("[Demux] Can't get filter Id.");
         *_aidl_return = -1;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_STATE));
     }
 
     if (!mFilters[id]->isMediaFilter()) {
         ALOGE("[Demux] Given filter is not a media filter.");
         *_aidl_return = -1;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_STATE));
     }
 
     if (!mPcrFilterIds.empty()) {
@@ -139,7 +146,8 @@
 
     ALOGE("[Demux] No PCR filter opened.");
     *_aidl_return = -1;
-    return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+    return ::ndk::ScopedAStatus::fromServiceSpecificError(
+            static_cast<int32_t>(Result::INVALID_STATE));
 }
 
 ::ndk::ScopedAStatus Demux::getAvSyncTime(int32_t in_avSyncHwId, int64_t* _aidl_return) {
@@ -147,11 +155,13 @@
 
     if (mPcrFilterIds.empty()) {
         *_aidl_return = -1;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_STATE));
     }
     if (in_avSyncHwId != *mPcrFilterIds.begin()) {
         *_aidl_return = -1;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     *_aidl_return = -1;
@@ -169,7 +179,7 @@
     mRecordFilterIds.clear();
     mFilters.clear();
     mLastUsedFilterId = -1;
-    mTunerService->removeDemux(mDemuxId);
+    mTuner->removeDemux(mDemuxId);
     mFrontendInputThreadRunning = false;
     std::lock_guard<std::mutex> lock(mFrontendInputThreadLock);
 
@@ -184,7 +194,8 @@
     if (in_cb == nullptr) {
         ALOGW("[Demux] DVR callback can't be null");
         *_aidl_return = nullptr;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     set<int64_t>::iterator it;
@@ -195,7 +206,8 @@
             if (!mDvrPlayback->createDvrMQ()) {
                 mDvrPlayback = nullptr;
                 *_aidl_return = mDvrPlayback;
-                return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+                return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                        static_cast<int32_t>(Result::UNKNOWN_ERROR));
             }
 
             for (it = mPlaybackFilterIds.begin(); it != mPlaybackFilterIds.end(); it++) {
@@ -203,7 +215,8 @@
                     ALOGE("[Demux] Can't get filter info for DVR playback");
                     mDvrPlayback = nullptr;
                     *_aidl_return = mDvrPlayback;
-                    return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+                    return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                            static_cast<int32_t>(Result::UNKNOWN_ERROR));
                 }
             }
 
@@ -214,14 +227,16 @@
             if (!mDvrRecord->createDvrMQ()) {
                 mDvrRecord = nullptr;
                 *_aidl_return = mDvrRecord;
-                return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+                return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                        static_cast<int32_t>(Result::UNKNOWN_ERROR));
             }
 
             *_aidl_return = mDvrRecord;
             return ::ndk::ScopedAStatus::ok();
         default:
             *_aidl_return = nullptr;
-            return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+            return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                    static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 }
 
diff --git a/tv/tuner/aidl/default/Demux.h b/tv/tuner/aidl/default/Demux.h
index 9f96d0f..4d9b7fe 100644
--- a/tv/tuner/aidl/default/Demux.h
+++ b/tv/tuner/aidl/default/Demux.h
@@ -95,7 +95,7 @@
 
   private:
     // Tuner service
-    std::shared_ptr<Tuner> mTunerService;
+    std::shared_ptr<Tuner> mTuner;
 
     // Frontend source
     std::shared_ptr<Frontend> mFrontend;
diff --git a/tv/tuner/aidl/default/Descrambler.cpp b/tv/tuner/aidl/default/Descrambler.cpp
index 8af3a92..586db73 100644
--- a/tv/tuner/aidl/default/Descrambler.cpp
+++ b/tv/tuner/aidl/default/Descrambler.cpp
@@ -18,6 +18,7 @@
 #define LOG_TAG "android.hardware.tv.tuner-service.example-Descrambler"
 
 #include <aidl/android/hardware/tv/tuner/IFrontendCallback.h>
+#include <aidl/android/hardware/tv/tuner/Result.h>
 #include <utils/Log.h>
 
 #include "Descrambler.h"
@@ -37,7 +38,8 @@
     if (mDemuxSet) {
         ALOGW("[   WARN   ] Descrambler has already been set with a demux id %" PRIu32,
               mSourceDemuxId);
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_STATE));
     }
     mDemuxSet = true;
     mSourceDemuxId = in_demuxId;
diff --git a/tv/tuner/aidl/default/Dvr.cpp b/tv/tuner/aidl/default/Dvr.cpp
index a042dc3..51cb1e0 100644
--- a/tv/tuner/aidl/default/Dvr.cpp
+++ b/tv/tuner/aidl/default/Dvr.cpp
@@ -18,6 +18,7 @@
 #define LOG_TAG "android.hardware.tv.tuner-service.example-Dvr"
 
 #include <aidl/android/hardware/tv/tuner/DemuxQueueNotifyBits.h>
+#include <aidl/android/hardware/tv/tuner/Result.h>
 
 #include <utils/Log.h>
 #include "Dvr.h"
@@ -39,8 +40,8 @@
 }
 
 Dvr::~Dvr() {
-    mDvrThreadRunning = false;
-    lock_guard<mutex> lock(mDvrThreadLock);
+    // make sure thread has joined
+    close();
 }
 
 ::ndk::ScopedAStatus Dvr::getQueueDesc(MQDescriptor<int8_t, SynchronizedReadWrite>* out_queue) {
@@ -70,7 +71,8 @@
     }
 
     if (!mDemux->attachRecordFilter(filterId)) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     return ::ndk::ScopedAStatus::ok();
@@ -86,7 +88,8 @@
     }
 
     if (!mDemux->detachRecordFilter(filterId)) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     return ::ndk::ScopedAStatus::ok();
@@ -99,17 +102,18 @@
     }
 
     if (!mCallback) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_NO_INIT);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::NOT_INITIALIZED));
     }
 
     if (!mDvrConfigured) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_STATE));
     }
 
     if (mType == DvrType::PLAYBACK) {
         mDvrThreadRunning = true;
-        pthread_create(&mDvrThread, NULL, __threadLoopPlayback, this);
-        pthread_setname_np(mDvrThread, "playback_waiting_loop");
+        mDvrThread = std::thread(&Dvr::playbackThreadLoop, this);
     } else if (mType == DvrType::RECORD) {
         mRecordStatus = RecordStatus::DATA_READY;
         mDemux->setIsRecording(mType == DvrType::RECORD);
@@ -124,9 +128,11 @@
     ALOGV("%s", __FUNCTION__);
 
     mDvrThreadRunning = false;
-    lock_guard<mutex> lock(mDvrThreadLock);
-
-    mIsRecordStarted = false;
+    if (mDvrThread.joinable()) {
+        mDvrThread.join();
+    }
+    // thread should always be joinable if it is running,
+    // so it should be safe to assume recording stopped.
     mDemux->setIsRecording(false);
 
     return ::ndk::ScopedAStatus::ok();
@@ -143,8 +149,8 @@
 ::ndk::ScopedAStatus Dvr::close() {
     ALOGV("%s", __FUNCTION__);
 
-    mDvrThreadRunning = false;
-    lock_guard<mutex> lock(mDvrThreadLock);
+    stop();
+
     return ::ndk::ScopedAStatus::ok();
 }
 
@@ -171,15 +177,8 @@
     return mDvrEventFlag;
 }
 
-void* Dvr::__threadLoopPlayback(void* user) {
-    Dvr* const self = static_cast<Dvr*>(user);
-    self->playbackThreadLoop();
-    return 0;
-}
-
 void Dvr::playbackThreadLoop() {
     ALOGD("[Dvr] playback threadLoop start.");
-    lock_guard<mutex> lock(mDvrThreadLock);
 
     while (mDvrThreadRunning) {
         uint32_t efState = 0;
diff --git a/tv/tuner/aidl/default/Dvr.h b/tv/tuner/aidl/default/Dvr.h
index fb22a2e..68933ae 100644
--- a/tv/tuner/aidl/default/Dvr.h
+++ b/tv/tuner/aidl/default/Dvr.h
@@ -21,7 +21,9 @@
 
 #include <fmq/AidlMessageQueue.h>
 #include <math.h>
+#include <atomic>
 #include <set>
+#include <thread>
 #include "Demux.h"
 #include "Frontend.h"
 #include "Tuner.h"
@@ -107,10 +109,7 @@
      * Each filter handler handles the data filtering/output writing/filterEvent updating.
      */
     void startTpidFilter(vector<int8_t> data);
-    static void* __threadLoopPlayback(void* user);
-    static void* __threadLoopRecord(void* user);
     void playbackThreadLoop();
-    void recordThreadLoop();
 
     unique_ptr<DvrMQ> mDvrMQ;
     EventFlag* mDvrEventFlag;
@@ -121,7 +120,7 @@
     DvrSettings mDvrSettings;
 
     // Thread handlers
-    pthread_t mDvrThread;
+    std::thread mDvrThread;
 
     // FMQ status local records
     PlaybackStatus mPlaybackStatus;
@@ -129,7 +128,7 @@
     /**
      * If a specific filter's writing loop is still running
      */
-    bool mDvrThreadRunning;
+    std::atomic<bool> mDvrThreadRunning;
     bool mKeepFetchingDataFromFrontend;
     /**
      * Lock to protect writes to the FMQs
@@ -140,13 +139,8 @@
      */
     std::mutex mPlaybackStatusLock;
     std::mutex mRecordStatusLock;
-    std::mutex mDvrThreadLock;
 
     const bool DEBUG_DVR = false;
-
-    // Booleans to check if recording is running.
-    // Recording is ready when both of the following are set to true.
-    bool mIsRecordStarted = false;
 };
 
 }  // namespace tuner
diff --git a/tv/tuner/aidl/default/Filter.cpp b/tv/tuner/aidl/default/Filter.cpp
index 18c1f00..dd9bee3 100644
--- a/tv/tuner/aidl/default/Filter.cpp
+++ b/tv/tuner/aidl/default/Filter.cpp
@@ -20,6 +20,7 @@
 #include <BufferAllocator/BufferAllocator.h>
 #include <aidl/android/hardware/tv/tuner/DemuxFilterMonitorEventType.h>
 #include <aidl/android/hardware/tv/tuner/DemuxQueueNotifyBits.h>
+#include <aidl/android/hardware/tv/tuner/Result.h>
 #include <aidlcommonsupport/NativeHandle.h>
 #include <utils/Log.h>
 
@@ -214,7 +215,8 @@
     }
 
     if (mDataId2Avfd.find(in_avDataId) == mDataId2Avfd.end()) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     ::close(mDataId2Avfd[in_avDataId]);
@@ -233,7 +235,8 @@
     ALOGV("%s", __FUNCTION__);
 
     if (mType.mainType != DemuxFilterMainType::IP) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_STATE));
     }
 
     mCid = in_ipCid;
@@ -244,7 +247,8 @@
     ALOGV("%s", __FUNCTION__);
 
     if (!mIsMediaFilter) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_STATE));
     }
 
     if (mSharedAvMemHandle != nullptr) {
@@ -256,14 +260,16 @@
 
     int av_fd = createAvIonFd(BUFFER_SIZE_16M);
     if (av_fd < 0) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_NO_MEMORY);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::OUT_OF_MEMORY));
     }
 
     mSharedAvMemHandle = createNativeHandle(av_fd);
     if (mSharedAvMemHandle == nullptr) {
         ::close(av_fd);
         *_aidl_return = 0;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::UNKNOWN_ERROR));
     }
     ::close(av_fd);
     mUsingSharedAvMem = true;
@@ -277,7 +283,8 @@
     ALOGV("%s", __FUNCTION__);
 
     if (!mIsMediaFilter) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::UNAVAILABLE));
     }
 
     switch (in_avStreamType.getTag()) {
@@ -319,7 +326,8 @@
                 events[0].set<DemuxFilterEvent::monitorEvent>(monitorEvent);
                 mCallback->onFilterEvent(events);
             } else {
-                return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+                return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                        static_cast<int32_t>(Result::INVALID_STATE));
             }
         }
     }
@@ -337,7 +345,8 @@
                 events[0].set<DemuxFilterEvent::monitorEvent>(monitorEvent);
                 mCallback->onFilterEvent(events);
             } else {
-                return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+                return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                        static_cast<int32_t>(Result::INVALID_STATE));
             }
         }
     }
@@ -579,7 +588,8 @@
     }
     if (!writeSectionsAndCreateEvent(mFilterOutput)) {
         ALOGD("[Filter] filter %" PRIu64 " fails to write into FMQ. Ending thread", mFilterId);
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::UNKNOWN_ERROR));
     }
 
     mFilterOutput.clear();
@@ -629,7 +639,8 @@
         if (!writeDataToFilterMQ(mPesOutput)) {
             ALOGD("[Filter] pes data write failed");
             mFilterOutput.clear();
-            return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+            return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                    static_cast<int32_t>(Result::INVALID_ARGUMENT));
         }
         maySendFilterStatusCallback();
         DemuxFilterPesEvent pesEvent;
@@ -720,7 +731,8 @@
 ::ndk::ScopedAStatus Filter::createMediaFilterEventWithIon(vector<int8_t>& output) {
     if (mUsingSharedAvMem) {
         if (mSharedAvMemHandle == nullptr) {
-            return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+            return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                    static_cast<int32_t>(Result::UNKNOWN_ERROR));
         }
         return createShareMemMediaEvents(output);
     }
@@ -736,7 +748,8 @@
 
     if (mDvr == nullptr || !mDvr->writeRecordFMQ(mRecordFilterOutput)) {
         ALOGD("[Filter] dvr fails to write into record FMQ.");
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::UNKNOWN_ERROR));
     }
 
     DemuxFilterTsRecordEvent recordEvent;
@@ -849,18 +862,21 @@
 ::ndk::ScopedAStatus Filter::createIndependentMediaEvents(vector<int8_t>& output) {
     int av_fd = createAvIonFd(output.size());
     if (av_fd == -1) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::UNKNOWN_ERROR));
     }
     // copy the filtered data to the buffer
     uint8_t* avBuffer = getIonBuffer(av_fd, output.size());
     if (avBuffer == NULL) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::UNKNOWN_ERROR));
     }
     memcpy(avBuffer, output.data(), output.size() * sizeof(uint8_t));
 
     native_handle_t* nativeHandle = createNativeHandle(av_fd);
     if (nativeHandle == NULL) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::UNKNOWN_ERROR));
     }
 
     // Create a dataId and add a <dataId, av_fd> pair into the dataId2Avfd map
@@ -898,14 +914,16 @@
     uint8_t* sharedAvBuffer =
             getIonBuffer(mSharedAvMemHandle->data[0], output.size() + mSharedAvMemOffset);
     if (sharedAvBuffer == NULL) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::UNKNOWN_ERROR));
     }
     memcpy(sharedAvBuffer + mSharedAvMemOffset, output.data(), output.size() * sizeof(uint8_t));
 
     // Create a memory handle with numFds == 0
     native_handle_t* nativeHandle = createNativeHandle(-1);
     if (nativeHandle == NULL) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_UNKNOWN_ERROR);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::UNKNOWN_ERROR));
     }
 
     // Create mediaEvent and send callback
diff --git a/tv/tuner/aidl/default/Frontend.cpp b/tv/tuner/aidl/default/Frontend.cpp
index 2be13d3..7f7b989 100644
--- a/tv/tuner/aidl/default/Frontend.cpp
+++ b/tv/tuner/aidl/default/Frontend.cpp
@@ -17,9 +17,11 @@
 //#define LOG_NDEBUG 0
 #define LOG_TAG "android.hardware.tv.tuner-service.example-Frontend"
 
-#include "Frontend.h"
+#include <aidl/android/hardware/tv/tuner/Result.h>
 #include <utils/Log.h>
 
+#include "Frontend.h"
+
 namespace aidl {
 namespace android {
 namespace hardware {
@@ -29,7 +31,7 @@
 Frontend::Frontend(FrontendType type, int32_t id, std::shared_ptr<Tuner> tuner) {
     mType = type;
     mId = id;
-    mTunerService = tuner;
+    mTuner = tuner;
     // Init callback to nullptr
     mCallback = nullptr;
 }
@@ -41,7 +43,7 @@
     // Reset callback
     mCallback = nullptr;
     mIsLocked = false;
-    mTunerService->removeFrontend(mId);
+    mTuner->removeFrontend(mId);
 
     return ::ndk::ScopedAStatus::ok();
 }
@@ -50,7 +52,8 @@
     ALOGV("%s", __FUNCTION__);
     if (in_callback == nullptr) {
         ALOGW("[   WARN   ] Set Frontend callback with nullptr");
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     mCallback = in_callback;
@@ -61,10 +64,11 @@
     ALOGV("%s", __FUNCTION__);
     if (mCallback == nullptr) {
         ALOGW("[   WARN   ] Frontend callback is not set when tune");
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_STATE));
     }
 
-    mTunerService->frontendStartTune(mId);
+    mTuner->frontendStartTune(mId);
     mCallback->onEvent(FrontendEventType::LOCKED);
     mIsLocked = true;
 
@@ -74,7 +78,7 @@
 ::ndk::ScopedAStatus Frontend::stopTune() {
     ALOGV("%s", __FUNCTION__);
 
-    mTunerService->frontendStopTune(mId);
+    mTuner->frontendStopTune(mId);
     mIsLocked = false;
 
     return ::ndk::ScopedAStatus::ok();
@@ -82,9 +86,9 @@
 
 ::ndk::ScopedAStatus Frontend::scan(const FrontendSettings& in_settings, FrontendScanType in_type) {
     ALOGV("%s", __FUNCTION__);
-    FrontendScanMessage msg;
 
     if (mIsLocked) {
+        FrontendScanMessage msg;
         msg.set<FrontendScanMessage::Tag::isEnd>(true);
         mCallback->onScanMessage(FrontendScanMessageType::END, msg);
         return ::ndk::ScopedAStatus::ok();
@@ -675,7 +679,8 @@
 ::ndk::ScopedAStatus Frontend::setLnb(int32_t /* in_lnbId */) {
     ALOGV("%s", __FUNCTION__);
     if (!supportsSatellite()) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_STATE));
     }
     return ::ndk::ScopedAStatus::ok();
 }
diff --git a/tv/tuner/aidl/default/Frontend.h b/tv/tuner/aidl/default/Frontend.h
index f89e74d..3c602cf 100644
--- a/tv/tuner/aidl/default/Frontend.h
+++ b/tv/tuner/aidl/default/Frontend.h
@@ -59,7 +59,7 @@
     virtual ~Frontend();
     bool supportsSatellite();
     std::shared_ptr<IFrontendCallback> mCallback;
-    std::shared_ptr<Tuner> mTunerService;
+    std::shared_ptr<Tuner> mTuner;
     FrontendType mType = FrontendType::UNDEFINED;
     int32_t mId = 0;
     bool mIsLocked = false;
diff --git a/tv/tuner/aidl/default/TimeFilter.cpp b/tv/tuner/aidl/default/TimeFilter.cpp
index 4fd8d21..dde7be3 100644
--- a/tv/tuner/aidl/default/TimeFilter.cpp
+++ b/tv/tuner/aidl/default/TimeFilter.cpp
@@ -17,9 +17,11 @@
 //#define LOG_NDEBUG 0
 #define LOG_TAG "android.hardware.tv.tuner-service.example-TimeFilter"
 
-#include "TimeFilter.h"
+#include <aidl/android/hardware/tv/tuner/Result.h>
 #include <utils/Log.h>
 
+#include "TimeFilter.h"
+
 namespace aidl {
 namespace android {
 namespace hardware {
@@ -37,7 +39,8 @@
 ::ndk::ScopedAStatus TimeFilter::setTimeStamp(int64_t in_timeStamp) {
     ALOGV("%s", __FUNCTION__);
     if (in_timeStamp == INVALID_TIME_STAMP) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
     mTimeStamp = in_timeStamp;
     mBeginTime = time(NULL);
@@ -56,7 +59,8 @@
     ALOGV("%s", __FUNCTION__);
     if (mTimeStamp == INVALID_TIME_STAMP) {
         *_aidl_return = mTimeStamp;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     uint64_t currentTimeStamp = mTimeStamp + difftime(time(NULL), mBeginTime) * 900000;
diff --git a/tv/tuner/aidl/default/Tuner.cpp b/tv/tuner/aidl/default/Tuner.cpp
index dc0bd5e..8335ffa 100644
--- a/tv/tuner/aidl/default/Tuner.cpp
+++ b/tv/tuner/aidl/default/Tuner.cpp
@@ -17,12 +17,14 @@
 //#define LOG_NDEBUG 0
 #define LOG_TAG "android.hardware.tv.tuner-service.example-Tuner"
 
-#include "Tuner.h"
+#include <aidl/android/hardware/tv/tuner/Result.h>
 #include <utils/Log.h>
+
 #include "Demux.h"
 #include "Descrambler.h"
 #include "Frontend.h"
 #include "Lnb.h"
+#include "Tuner.h"
 
 namespace aidl {
 namespace android {
@@ -198,7 +200,8 @@
     if (in_frontendId >= mFrontendSize || in_frontendId < 0) {
         ALOGW("[   WARN   ] Frontend with id %d isn't available", in_frontendId);
         *_aidl_return = nullptr;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     *_aidl_return = mFrontends[in_frontendId];
@@ -241,7 +244,8 @@
     ALOGV("%s", __FUNCTION__);
 
     if (in_frontendId >= mFrontendSize) {
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     // assign randomly selected values for testing.
@@ -276,7 +280,8 @@
 
     if (in_lnbId >= mLnbs.size()) {
         *_aidl_return = nullptr;
-        return ::ndk::ScopedAStatus::fromExceptionCode(STATUS_INVALID_OPERATION);
+        return ::ndk::ScopedAStatus::fromServiceSpecificError(
+                static_cast<int32_t>(Result::INVALID_ARGUMENT));
     }
 
     *_aidl_return = mLnbs[in_lnbId];