Backup:mcu protocol.
This commit is contained in:
parent
15303359d6
commit
5057623540
|
@ -101,9 +101,8 @@ class VMcuMonitor
|
|||
public:
|
||||
VMcuMonitor() = default;
|
||||
virtual ~VMcuMonitor() = default;
|
||||
virtual void RecvIpcMissionEvent(std::shared_ptr<VMcuAsk> &recv, const IpcMission &mission)
|
||||
{
|
||||
}
|
||||
virtual void RecvIpcMissionEvent(std::shared_ptr<VMcuAsk> &recv, const IpcMission &mission);
|
||||
virtual void RecvMcuHeartBeat(std::shared_ptr<VMcuAsk> &recv);
|
||||
};
|
||||
class IMcuManager
|
||||
{
|
||||
|
|
|
@ -14,6 +14,12 @@
|
|||
*/
|
||||
#include "IMcuManager.h"
|
||||
#include "ILog.h"
|
||||
void VMcuMonitor::RecvIpcMissionEvent(std::shared_ptr<VMcuAsk> &recv, const IpcMission &mission)
|
||||
{
|
||||
}
|
||||
void VMcuMonitor::RecvMcuHeartBeat(std::shared_ptr<VMcuAsk> &recv)
|
||||
{
|
||||
}
|
||||
std::shared_ptr<IMcuManager> &IMcuManager::GetInstance(std::shared_ptr<IMcuManager> *impl)
|
||||
{
|
||||
static auto instance = std::make_shared<IMcuManager>();
|
||||
|
|
|
@ -15,6 +15,43 @@
|
|||
#include "McuManagerImpl.h"
|
||||
#include "ILog.h"
|
||||
#include "UartRecvAsk.h"
|
||||
class OtherSideSend : public UartRecvAsk, public McuAsk<ASK_RESULT>
|
||||
{
|
||||
public:
|
||||
OtherSideSend(std::shared_ptr<McuManagerImpl> &mcuManager, const unsigned int &serialNumber,
|
||||
const OtherSideSendType &sendType)
|
||||
: mMcuManager(mcuManager), mSendType(sendType)
|
||||
{
|
||||
McuAsk::mSerialNumber = serialNumber;
|
||||
}
|
||||
virtual ~OtherSideSend() = default;
|
||||
|
||||
protected:
|
||||
std::shared_ptr<McuManagerImpl> mMcuManager;
|
||||
|
||||
public:
|
||||
const OtherSideSendType mSendType;
|
||||
};
|
||||
template <typename T>
|
||||
class OtherSideSendWithData : public OtherSideSend
|
||||
{
|
||||
public:
|
||||
OtherSideSendWithData(std::shared_ptr<McuManagerImpl> &mcuManager, const unsigned int &serialNumber,
|
||||
const OtherSideSendType &sendType, const T &otherSideData)
|
||||
: OtherSideSend(mcuManager, serialNumber, sendType), mOtherSideData(otherSideData)
|
||||
{
|
||||
}
|
||||
virtual ~OtherSideSendWithData() = default;
|
||||
|
||||
public:
|
||||
const T mOtherSideData;
|
||||
};
|
||||
McuManagerImpl::McuManagerImpl()
|
||||
{
|
||||
mMcuAskHandle[OtherSideSendType::SEND_IPC_MISSION] =
|
||||
std::bind(&McuManagerImpl::McuAskSendIpcMissionHandle, this, _1);
|
||||
mMcuAskHandle[OtherSideSendType::SEND_HEART_BEAT] = std::bind(&McuManagerImpl::McuAskSendHeartBeatHandle, this, _1);
|
||||
}
|
||||
std::shared_ptr<VProtocolBase> McuManagerImpl::SharedFromThis(void)
|
||||
{
|
||||
return shared_from_this();
|
||||
|
@ -29,11 +66,21 @@ const StatusCode McuManagerImpl::UnInit(void)
|
|||
{
|
||||
McuDevice::UnInit();
|
||||
McuProtocol::UnInit();
|
||||
mMcuAskList.clear();
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
const StatusCode McuManagerImpl::SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
mMonitor = monitor;
|
||||
for (auto ask : mMcuAskList) {
|
||||
std::shared_ptr<OtherSideSend> data = std::dynamic_pointer_cast<OtherSideSend>(ask);
|
||||
auto handle = mMcuAskHandle.find(data->mSendType);
|
||||
if (handle != mMcuAskHandle.end()) {
|
||||
handle->second(ask);
|
||||
}
|
||||
}
|
||||
mMcuAskList.clear();
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
const StatusCode McuManagerImpl::GetIpcMission(std::shared_ptr<VMcuAsk> &ask)
|
||||
|
@ -93,32 +140,88 @@ std::shared_ptr<VMcuMonitor> McuManagerImpl::GetMcuMonitor(void)
|
|||
}
|
||||
void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission)
|
||||
{
|
||||
class OtherSideSend : public UartRecvAsk, public McuAsk<ASK_RESULT>
|
||||
class OtherSideSendV2 : public OtherSideSendWithData<unsigned char>
|
||||
{
|
||||
public:
|
||||
OtherSideSend(std::shared_ptr<McuManagerImpl> &mcuManager, const unsigned int &serialNumber)
|
||||
: mMcuManager(mcuManager)
|
||||
OtherSideSendV2(std::shared_ptr<McuManagerImpl> &mcuManager, const unsigned int &serialNumber,
|
||||
const OtherSideSendType &sendType, const unsigned char &mission)
|
||||
: OtherSideSendWithData(mcuManager, serialNumber, sendType, mission)
|
||||
{
|
||||
mSerialNumber = serialNumber;
|
||||
}
|
||||
~OtherSideSend() = default;
|
||||
~OtherSideSendV2() = default;
|
||||
void ReplyFinished(const bool result) override
|
||||
{
|
||||
if (result) {
|
||||
mMcuManager->ReplyOtherSideSendIpcMission(mDataReply, mSerialNumber);
|
||||
}
|
||||
mMcuManager->ReplyOtherSideSendIpcMission(McuAsk::mDataReply, VMcuAsk::mSerialNumber);
|
||||
}
|
||||
std::shared_ptr<McuManagerImpl> mMcuManager;
|
||||
};
|
||||
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
|
||||
std::shared_ptr<McuManagerImpl> manager = std::dynamic_pointer_cast<McuManagerImpl>(SharedFromThis());
|
||||
std::shared_ptr<VMcuAsk> ask =
|
||||
std::make_shared<OtherSideSendV2>(manager, serialNumber, OtherSideSendType::SEND_IPC_MISSION, mission);
|
||||
if (monitor) {
|
||||
std::shared_ptr<McuManagerImpl> manager = std::dynamic_pointer_cast<McuManagerImpl>(SharedFromThis());
|
||||
std::shared_ptr<VMcuAsk> ask = std::make_shared<OtherSideSend>(manager, serialNumber);
|
||||
monitor->RecvIpcMissionEvent(ask, static_cast<IpcMission>(mission));
|
||||
}
|
||||
else {
|
||||
LogWarning("mMonitor is nullptr, AddMcuAsk.\n");
|
||||
AddMcuAsk(ask);
|
||||
}
|
||||
}
|
||||
void McuManagerImpl::OtherSideSendHearBeat(const unsigned int &serialNumber)
|
||||
{
|
||||
class OtherSideSendV2 : public OtherSideSend
|
||||
{
|
||||
public:
|
||||
OtherSideSendV2(std::shared_ptr<McuManagerImpl> &mcuManager, const unsigned int &serialNumber,
|
||||
const OtherSideSendType &sendType)
|
||||
: OtherSideSend(mcuManager, serialNumber, sendType)
|
||||
{
|
||||
}
|
||||
~OtherSideSendV2() = default;
|
||||
void ReplyFinished(const bool result) override
|
||||
{
|
||||
mMcuManager->ReplyOtherSideSendHeartBeat(mSerialNumber);
|
||||
}
|
||||
};
|
||||
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
|
||||
std::shared_ptr<McuManagerImpl> manager = std::dynamic_pointer_cast<McuManagerImpl>(SharedFromThis());
|
||||
std::shared_ptr<VMcuAsk> ask =
|
||||
std::make_shared<OtherSideSendV2>(manager, serialNumber, OtherSideSendType::SEND_HEART_BEAT);
|
||||
if (monitor) {
|
||||
monitor->RecvMcuHeartBeat(ask);
|
||||
}
|
||||
else {
|
||||
LogWarning("mMonitor is nullptr, AddMcuAsk.\n");
|
||||
AddMcuAsk(ask);
|
||||
}
|
||||
}
|
||||
void McuManagerImpl::ReplyOtherSideSendIpcMission(const ASK_RESULT &result, const unsigned int &serialNumber)
|
||||
{
|
||||
LogInfo("ReplyOtherSideSendIpcMission\n");
|
||||
return McuProtocol::ReplyOtherSideSendIpcMission(static_cast<ReplyResult>(result), serialNumber);
|
||||
McuProtocol::ReplyOtherSideSendIpcMission(static_cast<ReplyResult>(result), serialNumber);
|
||||
}
|
||||
void McuManagerImpl::ReplyOtherSideSendHeartBeat(const unsigned int &serialNumber)
|
||||
{
|
||||
LogInfo("ReplyOtherSideSendHeartBeat\n");
|
||||
McuProtocol::ReplyOtherSideSendHearBeat(static_cast<ReplyResult>(ASK_RESULT::SUCCEED), serialNumber);
|
||||
}
|
||||
void McuManagerImpl::AddMcuAsk(std::shared_ptr<VMcuAsk> &ask)
|
||||
{
|
||||
std::lock_guard<std::mutex> locker(mMutex);
|
||||
mMcuAskList.push_back(ask);
|
||||
}
|
||||
void McuManagerImpl::McuAskSendIpcMissionHandle(std::shared_ptr<VMcuAsk> &ask)
|
||||
{
|
||||
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
|
||||
std::shared_ptr<OtherSideSendWithData<unsigned char>> data =
|
||||
std::dynamic_pointer_cast<OtherSideSendWithData<unsigned char>>(ask);
|
||||
if (monitor) {
|
||||
monitor->RecvIpcMissionEvent(ask, static_cast<IpcMission>(data->mOtherSideData));
|
||||
}
|
||||
}
|
||||
void McuManagerImpl::McuAskSendHeartBeatHandle(std::shared_ptr<VMcuAsk> &ask)
|
||||
{
|
||||
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
|
||||
if (monitor) {
|
||||
monitor->RecvMcuHeartBeat(ask);
|
||||
}
|
||||
}
|
|
@ -17,10 +17,22 @@
|
|||
#include "IMcuManager.h"
|
||||
#include "McuDevice.h"
|
||||
#include "McuProtocol.h"
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
using std::placeholders::_1;
|
||||
using McuAskHandleFunc = std::function<void(std::shared_ptr<VMcuAsk> &)>;
|
||||
enum class OtherSideSendType
|
||||
{
|
||||
SEND_IPC_MISSION,
|
||||
SEND_HEART_BEAT,
|
||||
END
|
||||
};
|
||||
class McuManagerImpl : public McuDevice, public McuProtocol, public std::enable_shared_from_this<McuManagerImpl>
|
||||
{
|
||||
public:
|
||||
McuManagerImpl() = default;
|
||||
McuManagerImpl();
|
||||
virtual ~McuManagerImpl() = default;
|
||||
std::shared_ptr<VProtocolBase> SharedFromThis(void) override;
|
||||
const StatusCode Init(void) override;
|
||||
|
@ -41,9 +53,28 @@ private:
|
|||
|
||||
private:
|
||||
void OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission) override;
|
||||
/**
|
||||
* @brief The heartbeat packet must be processed by the state machine in the main thread. When the state machine
|
||||
* blocks/freezes, it can be restored by the external microcontroller after powering off and restarting.
|
||||
* @param serialNumber
|
||||
*/
|
||||
void OtherSideSendHearBeat(const unsigned int &serialNumber) override;
|
||||
void ReplyOtherSideSendIpcMission(const ASK_RESULT &result, const unsigned int &serialNumber);
|
||||
void ReplyOtherSideSendHeartBeat(const unsigned int &serialNumber);
|
||||
|
||||
private: // About mMcuAskList
|
||||
void AddMcuAsk(std::shared_ptr<VMcuAsk> &ask);
|
||||
void McuAskSendIpcMissionHandle(std::shared_ptr<VMcuAsk> &ask);
|
||||
void McuAskSendHeartBeatHandle(std::shared_ptr<VMcuAsk> &ask);
|
||||
|
||||
private:
|
||||
std::mutex mMutex;
|
||||
std::weak_ptr<VMcuMonitor> mMonitor;
|
||||
/**
|
||||
* @brief If the monitor has not been registered yet, it is necessary to cache the reported messages and report them
|
||||
* again when the monitor is registered.
|
||||
*/
|
||||
std::list<std::shared_ptr<VMcuAsk>> mMcuAskList;
|
||||
std::map<OtherSideSendType, McuAskHandleFunc> mMcuAskHandle;
|
||||
};
|
||||
#endif
|
|
@ -12,8 +12,8 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#ifndef OTHER_SIDE_ASK_H
|
||||
#define OTHER_SIDE_ASK_H
|
||||
#ifndef UART_RECV_ASK_H
|
||||
#define UART_RECV_ASK_H
|
||||
#include "McuAskBase.h"
|
||||
class UartRecvAsk : public McuAskBase
|
||||
{
|
||||
|
|
|
@ -20,7 +20,7 @@ include_directories(
|
|||
aux_source_directory(./src TEST_TOOL_SRC_FILES)
|
||||
set(TEST_TOOL_TARGET McuManagerTestTool)
|
||||
add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES})
|
||||
target_link_libraries(${TEST_TOOL_TARGET} McuProtocolTestTool UartDeviceTestTool LinuxApiMock Log)
|
||||
target_link_libraries(${TEST_TOOL_TARGET} McuManager McuProtocolTestTool UartDeviceTestTool LinuxApiMock Log)
|
||||
|
||||
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
|
||||
add_custom_target(
|
||||
|
|
|
@ -112,9 +112,8 @@ class OtherSideAsk
|
|||
public:
|
||||
OtherSideAsk() = default;
|
||||
virtual ~OtherSideAsk() = default;
|
||||
virtual void OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission)
|
||||
{
|
||||
}
|
||||
virtual void OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission);
|
||||
virtual void OtherSideSendHearBeat(const unsigned int &serialNumber);
|
||||
};
|
||||
class VProtocolRecv : public OtherSideReply, public OtherSideAsk
|
||||
{
|
||||
|
@ -148,6 +147,7 @@ protected:
|
|||
|
||||
protected:
|
||||
void ReplyOtherSideSendIpcMission(const ReplyResult &result, const unsigned int &serialNumber);
|
||||
void ReplyOtherSideSendHearBeat(const ReplyResult &result, const unsigned int &serialNumber);
|
||||
|
||||
protected:
|
||||
size_t GetKeyHeadLength(void) override;
|
||||
|
|
|
@ -16,6 +16,12 @@
|
|||
#include "ILog.h"
|
||||
#include "ProtocolHandle.h"
|
||||
#include <string.h>
|
||||
void OtherSideAsk::OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission)
|
||||
{
|
||||
}
|
||||
void OtherSideAsk::OtherSideSendHearBeat(const unsigned int &serialNumber)
|
||||
{
|
||||
}
|
||||
std::shared_ptr<VProtocolRecv> &VProtocolRecv::GetInstance(std::shared_ptr<VProtocolRecv> *impl)
|
||||
{
|
||||
static auto instance = std::make_shared<VProtocolRecv>();
|
||||
|
@ -155,6 +161,16 @@ void McuProtocol::ReplyOtherSideSendIpcMission(const ReplyResult &result, const
|
|||
WriteProtocolData(
|
||||
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), NULL_CONTEXT, handle->GetSerialNumber());
|
||||
}
|
||||
void McuProtocol::ReplyOtherSideSendHearBeat(const ReplyResult &result, const unsigned int &serialNumber)
|
||||
{
|
||||
std::shared_ptr<VProtocolContext> NULL_CONTEXT;
|
||||
std::shared_ptr<VProtocolParam> param = std::make_shared<ProtocolParam<const unsigned char>>(
|
||||
PROTOCOL_COMMAND::REPLY_OTHER_SIDE_ASK_SEND_HEART_BEAT, static_cast<const unsigned char>(result));
|
||||
param->mSerialNumber = serialNumber;
|
||||
std::shared_ptr<ProtocolHandle> handle = ProtocolHandle::CreateProtocolData(param);
|
||||
WriteProtocolData(
|
||||
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), NULL_CONTEXT, handle->GetSerialNumber());
|
||||
}
|
||||
size_t McuProtocol::GetKeyHeadLength(void)
|
||||
{
|
||||
return ProtocolHandle::GetKeyHeadLength();
|
||||
|
|
Loading…
Reference in New Issue
Block a user