hunting/test/utils/McuProtocol/tool/src/ProtocolMonitor.cpp
2024-06-17 23:43:39 +08:00

81 lines
3.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 "ProtocolMonitor.h"
#include "GtestUsing.h"
#include "ILog.h"
#include <cstddef>
#include <cstdio>
#include <gmock/gmock-spec-builders.h>
#include <memory>
static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log)
{
printf("%s { 0x%02X", log, *(unsigned char *)buf);
for (size_t i = 1; i < bufLength; i++) {
printf(", 0x%02X", *((unsigned char *)buf + i));
}
printf(" }\n");
}
std::shared_ptr<ProtocolMonitor> &ProtocolMonitor::GetInstance(std::shared_ptr<ProtocolMonitor> *impl)
{
static auto instance = std::make_shared<ProtocolMonitor>();
if (impl) {
if (instance.use_count() == 1) {
LogInfo("Instance changed succeed.\n");
instance = *impl;
}
else {
LogError("Can't changing the instance becase of using by some one.\n");
}
}
return instance;
}
void ProtocolMonitor::MonitorWriteProtocolData(const short &head, const unsigned int &serialNumber,
const short &command, const void *data, const short &packetLength)
{
}
void ProtocolMonitorTest::Init(std::shared_ptr<ProtocolMonitorTest> &test)
{
auto printfParam = [=](const short &head,
const unsigned int &serialNumber,
const short &command,
const void *data,
const short &packetLength) {
LogInfo("MonitorWriteProtocolData called.\n");
PrintHexadecimalData(&head, sizeof(head), "MonitorWriteProtocolData(head):");
PrintHexadecimalData(&serialNumber, sizeof(serialNumber), "MonitorWriteProtocolData(serialNumber):");
PrintHexadecimalData(&command, sizeof(command), "MonitorWriteProtocolData(command):");
PrintHexadecimalData(&packetLength, sizeof(packetLength), "MonitorWriteProtocolData(packetLength):");
};
EXPECT_CALL(*test.get(), MonitorWriteProtocolData(_, _, _, _, _))
.WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(printfParam)), Return()));
}
void ProtocolMonitorTest::WriteDataOnce(std::shared_ptr<ProtocolMonitorTest> &test, const short &head,
const unsigned int &serialNumber, const short &command, const void *data,
const short &packetLength)
{
auto printfParam = [=](const short &head,
const unsigned int &serialNumber,
const short &command,
const void *data,
const short &packetLength) {
};
PrintHexadecimalData(&head, sizeof(head), "WriteDataOnce(head):");
PrintHexadecimalData(&serialNumber, sizeof(serialNumber), "WriteDataOnce(serialNumber):");
PrintHexadecimalData(&command, sizeof(command), "WriteDataOnce(command):");
PrintHexadecimalData(&packetLength, sizeof(packetLength), "WriteDataOnce(packetLength):");
EXPECT_CALL(*test.get(), MonitorWriteProtocolData(head, serialNumber, command, _, packetLength))
.Times(1)
.WillOnce(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(printfParam)), Return()));
}