embedded-framework/middleware/McuAskBase/src/McuAskBase.cpp
2024-05-10 19:19:40 +08:00

57 lines
1.7 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "McuAskBase.h"
#include "ILog.h"
McuAskBase::McuAskBase(const McuAskBlock isBlock, const McuAskReply needReply, const unsigned int timeoutMs)
: mIsBlock(isBlock), mNeedReply(needReply), mTimeout(timeoutMs)
{
constexpr int THREAD_SHARING = 0;
constexpr int INITIAL_VALUE_OF_SEMAPHORE = 0;
sem_init(&mSem, THREAD_SHARING, INITIAL_VALUE_OF_SEMAPHORE);
mReplyOK = false;
mWaitingTimeMs = 0;
}
ASK_RESULT McuAskBase::Blocking(void)
{
if (McuAskBlock::BLOCK == mIsBlock) {
sem_wait(&mSem);
}
if (true == mReplyOK) {
return ASK_RESULT::SUCCEED;
}
return ASK_RESULT::FAILED;
}
// void McuAskBase::StopBlocking(void)
// {
// //
// sem_post(&mSem);
// }
bool McuAskBase::NeedReply(void)
{
return mNeedReply == McuAskReply::NEED_REPLY ? true : false;
}
void McuAskBase::ReplyFinished(const bool result)
{
mReplyOK = result;
sem_post(&mSem);
}
bool McuAskBase::IfTimeout(const unsigned int &integrationTimeMs)
{
mWaitingTimeMs += integrationTimeMs;
if (mWaitingTimeMs >= mTimeout) {
return true;
}
return false;
}