hunting/utils/McuProtocol/src/ProtocolHandle.cpp
2024-02-06 18:51:26 -08:00

186 lines
6.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 "ProtocolHandle.h"
#include "ILog.h"
#include "McuProtocol.h"
#include "McuProtocolMakePtr.h"
#include "ModBusCRC16.h"
#include <netinet/in.h>
#include <string.h>
unsigned int ProtocolHandle::mSerialNumber = 0;
constexpr unsigned short PROTOCOL_HEAD = 0xFAC1;
constexpr size_t KEY_HEAD_LENGTH = sizeof(short) + sizeof(unsigned int) + sizeof(short) + sizeof(short);
ProtocolHandle::ProtocolHandle(const std::shared_ptr<VProtocolParam> &param) : mParam(param)
{
mProtocolData = nullptr;
mProtocolDataLength = 0;
mSerialNumber = 0;
mMakePacketFunc[ASK_IPC_MISSION] = std::bind(&ProtocolHandle::MakeAskIpcMissionPacket, this, std::placeholders::_1);
}
ProtocolHandle::ProtocolHandle(const void *data, const size_t &length)
{
mProtocolData = (unsigned char *)malloc(length);
if (nullptr != mProtocolData) {
memcpy(mProtocolData, data, length);
}
mProtocolDataLength = length;
mAnalyzePacketFunc[REPLY_IPC_MISSION] =
std::bind(&ProtocolHandle::AnalyzeReplyIpcMissionPacket, this, std::placeholders::_1);
}
ProtocolHandle::~ProtocolHandle()
{
if (nullptr != mProtocolData) {
free(mProtocolData);
mProtocolData = nullptr;
}
}
void ProtocolHandle::MakeProtocolPacket(const std::shared_ptr<VProtocolParam> &param)
{
std::map<PROTOCOL_COMMAND, MakePacketFunc>::iterator iter;
iter = mMakePacketFunc.find(param->mCommand);
if (iter != mMakePacketFunc.end()) {
mMakePacketFunc[param->mCommand](param);
}
else {
LogError("Unknown command.\n");
}
}
void ProtocolHandle::MakeAskIpcMissionPacket(const std::shared_ptr<VProtocolParam> &param)
{
if (mProtocolData != nullptr) {
LogError("Something wrong happened, make packet failed.\n");
return;
}
size_t dataLength = KEY_HEAD_LENGTH + sizeof(short);
mProtocolData = (unsigned char *)malloc(dataLength);
if (nullptr == mProtocolData) {
LogError("malloc failed, MakeAskIpcMissionPacket return.\n");
return;
}
ProtocolPacket packet;
packet.mHead = PROTOCOL_HEAD;
packet.mCommand = param->mCommand;
packet.mLength = dataLength;
packet.mCheckCode = Calculate_Check_Sum(mProtocolData, dataLength - sizeof(short));
packet.mSerialNumber = mSerialNumber;
mSerialNumber++;
BigEndianConversion(packet);
memcpy(mProtocolData, &packet, KEY_HEAD_LENGTH);
memcpy(mProtocolData + KEY_HEAD_LENGTH, &packet.mCheckCode, sizeof(short));
mProtocolDataLength = dataLength;
}
void ProtocolHandle::AnalyzeProtocolPacket(void)
{
ProtocolPacket packet = {0};
memcpy(&packet, mProtocolData, KEY_HEAD_LENGTH);
memcpy(&packet.mCheckCode,
(unsigned char *)mProtocolData + mProtocolDataLength - sizeof(unsigned short),
sizeof(unsigned short));
HostByteOrderConversion(packet);
LogInfo("AnalyzeProtocolPacket, command = 0x%04X\n", packet.mCommand);
PROTOCOL_COMMAND command = static_cast<PROTOCOL_COMMAND>(packet.mCommand);
std::map<PROTOCOL_COMMAND, AnalyzePacketFunc>::iterator iter;
iter = mAnalyzePacketFunc.find(command);
if (iter != mAnalyzePacketFunc.end()) {
mAnalyzePacketFunc[command](packet);
}
else {
LogError("Unknown command.\n");
}
}
void ProtocolHandle::AnalyzeReplyIpcMissionPacket(const ProtocolPacket &packet)
{
LogInfo("AnalyzeReplyIpcMissionPacket\n");
constexpr unsigned char UNKNOWN_MISSION = 0xFF;
constexpr unsigned int PROTOCOL_DATA_START_ADDRESS = KEY_HEAD_LENGTH;
unsigned char ipcMission = UNKNOWN_MISSION;
ipcMission = mProtocolData[PROTOCOL_DATA_START_ADDRESS];
LogInfo("ipc mission = 0x%02X\n", ipcMission);
VProtocolRecv::GetInstance()->GetIpcMissionReply(packet.mSerialNumber, ipcMission);
}
std::shared_ptr<ProtocolHandle> ProtocolHandle::CreateProtocolData(const std::shared_ptr<VProtocolParam> &param)
{
std::shared_ptr<ProtocolHandle> handle;
McuProtocolMakePtr::GetInstance()->CreateProtocolHandle(handle, param);
if (handle) {
handle->MakeProtocolPacket(param);
}
return handle;
}
void ProtocolHandle::ProtocolAnalysis(const void *data, const size_t &length)
{
LogInfo("ProtocolAnalysis\n");
std::shared_ptr<ProtocolHandle> handle;
McuProtocolMakePtr::GetInstance()->CreateProtocolHandle(handle, data, length);
if (handle) {
handle->AnalyzeProtocolPacket();
}
}
size_t ProtocolHandle::GetKeyHeadLength(void) { return KEY_HEAD_LENGTH; }
StatusCode ProtocolHandle::GetDataLength(const void *keyHead, const size_t &headLength, size_t &dataLength)
{
if (KEY_HEAD_LENGTH != headLength) {
LogError("key head buf error.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
ProtocolHandle::PrintHexadecimalData(keyHead, headLength);
unsigned short headNum = PROTOCOL_HEAD;
char byteOrder = ProtocolHandle::GetByteOrder();
if (ORDER_LITTLE_ENDIAN == byteOrder) {
headNum = htons(PROTOCOL_HEAD);
}
unsigned char head[3] = {0};
memcpy(head, &headNum, sizeof(unsigned short));
if (strstr((const char *)keyHead, (const char *)head) == nullptr) {
LogError("Protocol head unmatch.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
ProtocolPacket packet = {0};
memcpy(&packet, keyHead, headLength);
if (ORDER_LITTLE_ENDIAN == byteOrder) {
packet.mLength = htons(packet.mLength);
}
dataLength = packet.mLength;
return CreateStatusCode(STATUS_CODE_OK);
}
char ProtocolHandle::GetByteOrder(void)
{
static char byteOrder = 0x00;
if (0x00 != byteOrder) {
return byteOrder;
}
unsigned int value = 0x12345678;
unsigned int networkOrder = htonl(value);
if (value == networkOrder) {
LogInfo("Big Endian.\n");
byteOrder = ORDER_BIG_ENDIAN;
}
else {
LogInfo("Little Endian.\n");
byteOrder = ORDER_LITTLE_ENDIAN;
}
return byteOrder;
}
void ProtocolHandle::PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log)
{
if (log) {
printf("%s", log);
}
printf(" {0x%02X", *(unsigned char *)buf);
for (size_t i = 1; i < bufLength; i++) {
printf(", 0x%02X", *((unsigned char *)buf + i));
}
printf("}\n");
}