15490a39dSopenharmony_ci/*
25490a39dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
35490a39dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45490a39dSopenharmony_ci * you may not use this file except in compliance with the License.
55490a39dSopenharmony_ci * You may obtain a copy of the License at
65490a39dSopenharmony_ci *
75490a39dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85490a39dSopenharmony_ci *
95490a39dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105490a39dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115490a39dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125490a39dSopenharmony_ci * See the License for the specific language governing permissions and
135490a39dSopenharmony_ci * limitations under the License.
145490a39dSopenharmony_ci */
155490a39dSopenharmony_ci#include "msg_handle_thread.h"
165490a39dSopenharmony_ci#include <sys/types.h>
175490a39dSopenharmony_ci#include <unistd.h>
185490a39dSopenharmony_ci#include "intell_voice_log.h"
195490a39dSopenharmony_ci
205490a39dSopenharmony_ci#define LOG_TAG "MsgHandleThread"
215490a39dSopenharmony_ci
225490a39dSopenharmony_ciusing namespace std;
235490a39dSopenharmony_ci
245490a39dSopenharmony_cinamespace OHOS {
255490a39dSopenharmony_cinamespace IntellVoiceUtils {
265490a39dSopenharmony_cistatic const uint32_t MSQ_QUEUE_MAX_LEN = 100;
275490a39dSopenharmony_cistatic const int32_t MSG_MAX_SYNC_TIMEOUT = 5;
285490a39dSopenharmony_ci
295490a39dSopenharmony_ciMsgHandleThread::MsgHandleThread() : msgQue_(MSQ_QUEUE_MAX_LEN), callbackThread_(nullptr) {}
305490a39dSopenharmony_ci
315490a39dSopenharmony_ciMsgHandleThread::MsgHandleThread(std::shared_ptr<MessageQueue> callbackMsgQue)
325490a39dSopenharmony_ci    : callbackMsgQue_(callbackMsgQue), msgQue_(MSQ_QUEUE_MAX_LEN), callbackThread_(nullptr)
335490a39dSopenharmony_ci{}
345490a39dSopenharmony_ci
355490a39dSopenharmony_ciMsgHandleThread::MsgHandleThread(MsgHandleThread *callbackThread)
365490a39dSopenharmony_ci    : msgQue_(MSQ_QUEUE_MAX_LEN), callbackThread_(callbackThread)
375490a39dSopenharmony_ci{}
385490a39dSopenharmony_ci
395490a39dSopenharmony_ciMsgHandleThread::~MsgHandleThread() {}
405490a39dSopenharmony_ci
415490a39dSopenharmony_civoid MsgHandleThread::SetCallbackThread(MsgHandleThread *tmpCallbackThread)
425490a39dSopenharmony_ci{
435490a39dSopenharmony_ci    callbackThread_ = tmpCallbackThread;
445490a39dSopenharmony_ci}
455490a39dSopenharmony_ci
465490a39dSopenharmony_ci// the default realization is for debug, subclass should override this func
475490a39dSopenharmony_cibool MsgHandleThread::HandleMsg(Message &msg)
485490a39dSopenharmony_ci{
495490a39dSopenharmony_ci    INTELL_VOICE_LOG_INFO("run thread %{public}u process msg %{public}u", Gettid(), msg.what_);
505490a39dSopenharmony_ci
515490a39dSopenharmony_ci    SendbackMsg(msg);
525490a39dSopenharmony_ci
535490a39dSopenharmony_ci    return true;
545490a39dSopenharmony_ci}
555490a39dSopenharmony_ci
565490a39dSopenharmony_cibool MsgHandleThread::SendMsg(Message msg)
575490a39dSopenharmony_ci{
585490a39dSopenharmony_ci    try {
595490a39dSopenharmony_ci        msgQue_.SendMsg(std::make_shared<Message>(msg));
605490a39dSopenharmony_ci    } catch (const std::length_error& err) {
615490a39dSopenharmony_ci        INTELL_VOICE_LOG_ERROR("length error");
625490a39dSopenharmony_ci        return false;
635490a39dSopenharmony_ci    }
645490a39dSopenharmony_ci
655490a39dSopenharmony_ci    return true;
665490a39dSopenharmony_ci}
675490a39dSopenharmony_ci
685490a39dSopenharmony_cibool MsgHandleThread::SendMsg(std::shared_ptr<Message> msg)
695490a39dSopenharmony_ci{
705490a39dSopenharmony_ci    if (msg == nullptr) {
715490a39dSopenharmony_ci        return false;
725490a39dSopenharmony_ci    }
735490a39dSopenharmony_ci
745490a39dSopenharmony_ci    msgQue_.SendMsg(msg);
755490a39dSopenharmony_ci    return true;
765490a39dSopenharmony_ci}
775490a39dSopenharmony_ci
785490a39dSopenharmony_cibool MsgHandleThread::SendSynMsg(shared_ptr<Message> msg)
795490a39dSopenharmony_ci{
805490a39dSopenharmony_ci    if (msg == nullptr) {
815490a39dSopenharmony_ci        return false;
825490a39dSopenharmony_ci    }
835490a39dSopenharmony_ci
845490a39dSopenharmony_ci    msg->result_ = std::make_shared<SynInfo>();
855490a39dSopenharmony_ci    if (msg->result_ == nullptr) {
865490a39dSopenharmony_ci        INTELL_VOICE_LOG_ERROR("create sync info failed");
875490a39dSopenharmony_ci        return false;
885490a39dSopenharmony_ci    }
895490a39dSopenharmony_ci
905490a39dSopenharmony_ci    unique_lock<mutex> lock(msg->result_->mutex_);
915490a39dSopenharmony_ci    msgQue_.SendMsg(msg);
925490a39dSopenharmony_ci    if (msg->result_->cv_.wait_for(lock, chrono::seconds(MSG_MAX_SYNC_TIMEOUT)) == std::cv_status::no_timeout) {
935490a39dSopenharmony_ci        return true;
945490a39dSopenharmony_ci    } else {
955490a39dSopenharmony_ci        INTELL_VOICE_LOG_WARN("send syn msg timeout");
965490a39dSopenharmony_ci        return false;
975490a39dSopenharmony_ci    }
985490a39dSopenharmony_ci}
995490a39dSopenharmony_ci
1005490a39dSopenharmony_civoid MsgHandleThread::SendbackMsg(Message msg)
1015490a39dSopenharmony_ci{
1025490a39dSopenharmony_ci    if (callbackThread_ != nullptr) {
1035490a39dSopenharmony_ci        callbackThread_->SendMsg(msg);
1045490a39dSopenharmony_ci    }
1055490a39dSopenharmony_ci
1065490a39dSopenharmony_ci    if (callbackMsgQue_ != nullptr) {
1075490a39dSopenharmony_ci        callbackMsgQue_->SendMsg(make_shared<Message>(msg));
1085490a39dSopenharmony_ci    }
1095490a39dSopenharmony_ci}
1105490a39dSopenharmony_ci
1115490a39dSopenharmony_civoid MsgHandleThread::Run()
1125490a39dSopenharmony_ci{
1135490a39dSopenharmony_ci    bool isQuit = false;
1145490a39dSopenharmony_ci
1155490a39dSopenharmony_ci    while (!isQuit) {
1165490a39dSopenharmony_ci        shared_ptr<Message> msg = msgQue_.ReceiveMsg();
1175490a39dSopenharmony_ci
1185490a39dSopenharmony_ci        isQuit = HandleMsg(*msg);
1195490a39dSopenharmony_ci
1205490a39dSopenharmony_ci        if (msg->result_ != nullptr) {
1215490a39dSopenharmony_ci            unique_lock<mutex> lock(msg->result_->mutex_);
1225490a39dSopenharmony_ci            msg->result_->cv_.notify_all();
1235490a39dSopenharmony_ci        }
1245490a39dSopenharmony_ci    }
1255490a39dSopenharmony_ci}
1265490a39dSopenharmony_ci}
1275490a39dSopenharmony_ci}
128