/* * 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 #include using std::placeholders::_1; // using std::placeholders::_2; // using std::placeholders::_3; // using std::placeholders::_4; unsigned int ProtocolHandle::mSerialNumber = 1; std::mutex ProtocolHandle::mMutex; ProtocolHandle::ProtocolHandle(const std::shared_ptr ¶m) : mParam(param) { mProtocolData = nullptr; mProtocolDataLength = 0; mMakePacketFunc[ASK_IPC_MISSION] = std::bind(&ProtocolHandle::MakeAskIpcMissionPacket, this, _1); mMakePacketFunc[ASK_CUT_OFF_PWOER_SUPPLY] = std::bind(&ProtocolHandle::MakeAskCutOffPowerSupplyPacket, this, _1); mMakePacketFunc[ASK_FEED_WATCH_DOG] = std::bind(&ProtocolHandle::MakeAskFeedWatchDogPacket, this, _1); mMakePacketFunc[ASK_SET_FEEDING_CYCLE] = std::bind(&ProtocolHandle::MakeAskSetFeedingCyclePacket, this, _1); mMakePacketFunc[ASK_SET_DATE_TIME] = std::bind(&ProtocolHandle::MakeAskSetDateTimePacket, this, _1); mMakePacketFunc[ASK_SET_PIR_SENSITIVITY] = std::bind(&ProtocolHandle::MakeAskSetPirSensitivityPacket, this, _1); mMakePacketFunc[ASK_CONTORL_INFRARED_LIGHT] = std::bind(&ProtocolHandle::MakeAskControlInfraredLightPacket, this, _1); mMakePacketFunc[ASK_GET_PHOTOSENSITIVITY] = std::bind(&ProtocolHandle::MakeAskGetPhotosensitivityPacket, this, _1); /**************************************************************************************************************************/ mMakePacketFunc[REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION] = std::bind(&ProtocolHandle::MakeReplyOtherSideSendIpcMissionPacket, this, _1); mMakePacketFunc[REPLY_OTHER_SIDE_ASK_SEND_HEART_BEAT] = std::bind(&ProtocolHandle::MakeReplyOtherSideSendHeartBeatPacket, this, _1); mMakePacketFunc[REPLY_OTHER_SIDE_ASK_GET_INTERVAL_START] = std::bind(&ProtocolHandle::MakeReplyOtherSideGetIntervalStartPacket, this, _1); mMakePacketFunc[REPLY_OTHER_SIDE_ASK_GET_DATE_TIME] = std::bind(&ProtocolHandle::MakeReplyOtherSideGetDateTimePacket, this, _1); mMakePacketFunc[REPLY_OTHER_SIDE_ASK_GET_PIR_SENSITIVITY] = std::bind(&ProtocolHandle::MakeReplyOtherSideGetPirSensitivityPacket, this, _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, _1); mAnalyzePacketFunc[REPLY_SET_FEEDING_CYCLE] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1); mAnalyzePacketFunc[REPLY_SET_DATE_TIME] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1); mAnalyzePacketFunc[REPLY_SET_PIR_SENSITIVITY] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1); mAnalyzePacketFunc[REPLY_CONTORL_INFRARED_LIGHT] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1); mAnalyzePacketFunc[REPLY_GET_PHOTOSENSITIVITY] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1); /**************************************************************************************************************************/ mAnalyzePacketFunc[OTHER_SIDE_ASK_SEND_IPC_MISSION] = std::bind(&ProtocolHandle::AnalyzeOtherSideSendIpcMissionPacket, this, _1); mAnalyzePacketFunc[OTHER_SIDE_ASK_SEND_HEART_BEAT] = std::bind(&ProtocolHandle::AnalyzeOtherSideSendHeartBeatPacket, this, _1); mAnalyzePacketFunc[OTHER_SIDE_ASK_GET_INTERVAL_START] = std::bind(&ProtocolHandle::AnalyzeOtherSideSendGetIntervalStart, this, _1); mAnalyzePacketFunc[OTHER_SIDE_ASK_GET_DATE_TIME] = std::bind(&ProtocolHandle::AnalyzeOtherSideSendGetDataTime, this, _1); mAnalyzePacketFunc[OTHER_SIDE_ASK_GET_PIR_SENSITIVITY] = std::bind(&ProtocolHandle::AnalyzeOtherSideSendGetPirSensitivity, this, _1); } ProtocolHandle::~ProtocolHandle() { if (nullptr != mProtocolData) { free(mProtocolData); mProtocolData = nullptr; } } ProtocolPacket ProtocolHandle::CreatePocketWithSerialNumber(void) { std::lock_guard locker(mMutex); ProtocolPacket packet; memset(&packet, 0, sizeof(ProtocolPacket)); if (SERIAL_NUMBER_NEED_TO_MAKE_A_LOCAL_ONE == mProtocolSerialNumber) { packet.mSerialNumber = mSerialNumber; mProtocolSerialNumber = packet.mSerialNumber; mSerialNumber++; } else { packet.mSerialNumber = mProtocolSerialNumber; } return packet; } void ProtocolHandle::MallocPacketDataBuff(const void *data, const size_t dataLength, const short &command) { size_t packetLength = KEY_HEAD_LENGTH + dataLength + CHECK_CODE_LENGTH; mProtocolData = (unsigned char *)malloc(packetLength); if (nullptr == mProtocolData) { LogError("malloc failed, MakeAskIpcMissionPacket return.\n"); return; } ProtocolPacket packet = CreatePocketWithSerialNumber(); packet.mHead = PROTOCOL_HEAD; packet.mCommand = command; packet.mLength = packetLength; BigEndianConversion(packet); memcpy(mProtocolData, &packet, KEY_HEAD_LENGTH); memcpy(mProtocolData + KEY_HEAD_LENGTH, data, dataLength); packet.mCheckCode = calculate_check_sum(mProtocolData, packetLength - CHECK_CODE_LENGTH); packet.mCheckCode = BigEndianConversion(packet.mCheckCode); memcpy(mProtocolData + packetLength - CHECK_CODE_LENGTH, &packet.mCheckCode, CHECK_CODE_LENGTH); mProtocolDataLength = packetLength; ProtocolHandle::PrintHexadecimalData(mProtocolData, mProtocolDataLength, "Make protocol packet:"); } void ProtocolHandle::MakeProtocolPacket(const std::shared_ptr ¶m) { if (mProtocolData != nullptr) { LogError("Something wrong happened, make packet failed.\n"); return; } mProtocolSerialNumber = param->mSerialNumber; std::map::iterator iter; iter = mMakePacketFunc.find(param->mCommand); if (iter != mMakePacketFunc.end()) { mMakePacketFunc[param->mCommand](param); } else { LogError("Unknown command.\n"); } } void ProtocolHandle::MakeNoUserDataPacket(const std::shared_ptr ¶m) { size_t packetLength = KEY_HEAD_LENGTH + CHECK_CODE_LENGTH; mProtocolData = (unsigned char *)malloc(packetLength); if (nullptr == mProtocolData) { LogError("malloc failed, MakeAskIpcMissionPacket return.\n"); return; } ProtocolPacket packet = CreatePocketWithSerialNumber(); packet.mHead = PROTOCOL_HEAD; packet.mCommand = param->mCommand; packet.mLength = packetLength; BigEndianConversion(packet); memcpy(mProtocolData, &packet, KEY_HEAD_LENGTH); packet.mCheckCode = calculate_check_sum(mProtocolData, packetLength - CHECK_CODE_LENGTH); packet.mCheckCode = BigEndianConversion(packet.mCheckCode); memcpy(mProtocolData + KEY_HEAD_LENGTH, &packet.mCheckCode, CHECK_CODE_LENGTH); mProtocolDataLength = packetLength; if (param->mCommand != ASK_FEED_WATCH_DOG) { ProtocolHandle::PrintHexadecimalData( mProtocolData, mProtocolDataLength - CHECK_CODE_LENGTH, "MakeNoUserDataPacket:"); } } void ProtocolHandle::MakeAskIpcMissionPacket(const std::shared_ptr ¶m) { MakeNoUserDataPacket(param); } void ProtocolHandle::MakeAskCutOffPowerSupplyPacket(const std::shared_ptr ¶m) { MakeNoUserDataPacket(param); } void ProtocolHandle::MakeAskFeedWatchDogPacket(const std::shared_ptr ¶m) { MakeNoUserDataPacket(param); } void ProtocolHandle::MakeAskSetFeedingCyclePacket(const std::shared_ptr ¶m) { MakeProtocolData(param); } void ProtocolHandle::MakeAskSetDateTimePacket(const std::shared_ptr ¶m) { MakeProtocolData(param); } void ProtocolHandle::MakeAskSetPirSensitivityPacket(const std::shared_ptr ¶m) { MakeProtocolData(param); } void ProtocolHandle::MakeReplyOtherSideSendIpcMissionPacket(const std::shared_ptr ¶m) { MakeProtocolData(param); } void ProtocolHandle::MakeReplyOtherSideSendHeartBeatPacket(const std::shared_ptr ¶m) { MakeNoUserDataPacket(param); } void ProtocolHandle::MakeAskControlInfraredLightPacket(const std::shared_ptr ¶m) { MakeProtocolData(param); } void ProtocolHandle::MakeAskGetPhotosensitivityPacket(const std::shared_ptr ¶m) { MakeNoUserDataPacket(param); } void ProtocolHandle::MakeReplyOtherSideGetIntervalStartPacket(const std::shared_ptr ¶m) { MakeProtocolData(param); } void ProtocolHandle::MakeReplyOtherSideGetDateTimePacket(const std::shared_ptr ¶m) { MakeProtocolData(param); } void ProtocolHandle::MakeReplyOtherSideGetPirSensitivityPacket(const std::shared_ptr ¶m) { MakeProtocolData(param); } 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); if (CheckoutTheCheckCode(packet) == false) { LogError("CheckoutTheCheckCode failed.\n"); return; } /** * @brief unsigned int number = packet.mSerialNumber This line of code is for * avoiding errors: runtime error: reference binding to misaligned address xxxx */ mProtocolSerialNumber = packet.mSerialNumber; LogInfo("AnalyzeProtocolPacket, command = 0x%04X\n", packet.mCommand); PROTOCOL_COMMAND command = static_cast(packet.mCommand); std::map::iterator iter; iter = mAnalyzePacketFunc.find(command); if (iter != mAnalyzePacketFunc.end()) { mAnalyzePacketFunc[command](packet); } else { LogError("Unknown command.\n"); } } unsigned char ProtocolHandle::ReplyOneBytePacketResult(const ProtocolPacket &packet) { constexpr unsigned char UNKNOWN_RESULT = 0xFF; constexpr unsigned int PROTOCOL_DATA_START_ADDRESS = KEY_HEAD_LENGTH; unsigned char replyResult = UNKNOWN_RESULT; replyResult = mProtocolData[PROTOCOL_DATA_START_ADDRESS]; LogInfo("Other side send: result = 0x%02X\n", replyResult); return replyResult; } void ProtocolHandle::AnalyzeReplyResultPacket(const ProtocolPacket &packet) { LogInfo("AnalyzeReplyResultPacket\n"); unsigned char replyResult = ReplyOneBytePacketResult(packet); VProtocolRecv::GetInstance()->OnlyResultReply(mProtocolSerialNumber, static_cast(replyResult)); } void ProtocolHandle::AnalyzeReplyIpcMissionPacket(const ProtocolPacket &packet) { LogInfo("AnalyzeReplyIpcMissionPacket\n"); unsigned char ipcMission = ReplyOneBytePacketResult(packet); VProtocolRecv::GetInstance()->GetIpcMissionReply(mProtocolSerialNumber, ipcMission); } void ProtocolHandle::AnalyzeOtherSideSendIpcMissionPacket(const ProtocolPacket &packet) { LogInfo("AnalyzeOtherSideSendIpcMissionPacket\n"); unsigned char ipcMission = ReplyOneBytePacketResult(packet); VProtocolRecv::GetInstance()->OtherSideSendIpcMission(mProtocolSerialNumber, ipcMission); } void ProtocolHandle::AnalyzeOtherSideSendHeartBeatPacket(const ProtocolPacket &packet) { LogInfo("AnalyzeOtherSideSendHeartBeatPacket\n"); VProtocolRecv::GetInstance()->OtherSideSendHearBeat(mProtocolSerialNumber); } void ProtocolHandle::AnalyzeOtherSideSendGetIntervalStart(const ProtocolPacket &packet) { LogInfo("AnalyzeOtherSideSendGetIntervalStart\n"); VProtocolRecv::GetInstance()->OtherSideSendGetIntervalStart(mProtocolSerialNumber); } void ProtocolHandle::AnalyzeOtherSideSendGetDataTime(const ProtocolPacket &packet) { LogInfo("AnalyzeOtherSideSendGetDataTime\n"); VProtocolRecv::GetInstance()->OtherSideSendGetDateTime(mProtocolSerialNumber); } void ProtocolHandle::AnalyzeOtherSideSendGetPirSensitivity(const ProtocolPacket &packet) { LogInfo("AnalyzeOtherSideSendGetPirSensitivity\n"); VProtocolRecv::GetInstance()->OtherSideSendGetPirSensitivity(mProtocolSerialNumber); } bool ProtocolHandle::CheckoutTheCheckCode(const ProtocolPacket &packet) { ProtocolHandle::PrintHexadecimalData(&packet.mCheckCode, CHECK_CODE_LENGTH, "CheckoutTheCheckCode:"); short code = calculate_check_sum(mProtocolData, mProtocolDataLength - CHECK_CODE_LENGTH); if (code == packet.mCheckCode) { return true; } return false; } void ProtocolHandle::Init(void) { mSerialNumber = 1; } void ProtocolHandle::UnInit(void) { mSerialNumber = 1; } std::shared_ptr ProtocolHandle::CreateProtocolData(const std::shared_ptr ¶m) { std::shared_ptr 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 handle; McuProtocolMakePtr::GetInstance()->CreateProtocolHandle(handle, data, length); if (handle) { handle->AnalyzeProtocolPacket(); } } size_t ProtocolHandle::GetKeyHeadLength(void) { return KEY_HEAD_LENGTH; } StatusCode ProtocolHandle::CheckHeader(void *header, size_t &headLength) { unsigned char *data = (unsigned char *)header; unsigned short protocolHead = 0; protocolHead = PROTOCOL_HEAD; // LogInfo("head = 0x%02X, 0x%02X\n", protocolHead >> 8, protocolHead & 0x00FF); for (size_t i = 0; i < headLength; i++) { if ((protocolHead >> 8 == data[i]) && ((protocolHead & 0x00FF) == data[i + 1])) { headLength -= i; if (0 == i) { return CreateStatusCode(STATUS_CODE_OK); } unsigned char fixedHeader[KEY_HEAD_LENGTH] = {0}; memcpy(fixedHeader, data + i, headLength); memcpy(header, fixedHeader, headLength); LogWarning("Protocol head unmatch, unknow data before head. headLength =%d\n", headLength); ProtocolHandle::PrintHexadecimalData(header, headLength, "Fixed header:"); return CreateStatusCode(STATUS_CODE_NOT_OK); } } LogWarning("Protocol head unmatch, head not found.\n"); return CreateStatusCode(STATUS_CODE_NOT_OK); } 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); } 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"); }