2023-11-21 20:28:23 +08:00
|
|
|
#ifndef CODEC_H
|
|
|
|
#define CODEC_H
|
|
|
|
|
|
|
|
#include "QObject"
|
|
|
|
#include "base/base.h"
|
|
|
|
|
|
|
|
class Codec : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
Codec()
|
|
|
|
{
|
|
|
|
fullFrame = false;
|
2023-12-26 18:05:20 +08:00
|
|
|
failed = false;
|
2023-11-21 20:28:23 +08:00
|
|
|
}
|
|
|
|
virtual ~Codec() {}
|
2023-11-26 23:05:35 +08:00
|
|
|
// 返回true,则是一个完整的帧
|
2023-11-27 14:31:00 +08:00
|
|
|
// virtual bool packCheck(myarray data){return false;}
|
|
|
|
// virtual bool recvByte(int byte){return false;}
|
|
|
|
// virtual myarray encode(int srcAddr, int dstAddr, int cmd, myarray data){return myarray();}
|
|
|
|
// virtual myarray decode(int &srcAddr, int &dstAddr, int &cmd, myarray data){return myarray();}
|
2023-11-29 15:36:45 +08:00
|
|
|
virtual int packCheck(myarray data) = 0;
|
2023-11-27 14:31:00 +08:00
|
|
|
virtual bool recvByte(int byte) = 0;
|
|
|
|
virtual myarray encode(int srcAddr, int dstAddr, int cmd, myarray data) = 0;
|
|
|
|
virtual myarray decode(int &srcAddr, int &dstAddr, int &cmd, myarray data) = 0;
|
2023-11-26 23:05:35 +08:00
|
|
|
myarray decode(int &srcAddr, int &dstAddr, int &cmd)
|
2023-11-21 20:28:23 +08:00
|
|
|
{
|
|
|
|
if (fullFrame == true)
|
|
|
|
{
|
|
|
|
fullFrame = false;
|
|
|
|
return decode(srcAddr, dstAddr, cmd, data);
|
|
|
|
}
|
|
|
|
return myarray();
|
|
|
|
}
|
2023-12-26 18:05:20 +08:00
|
|
|
public:
|
|
|
|
bool failed;// true时解析失败
|
2023-11-21 20:28:23 +08:00
|
|
|
protected:
|
|
|
|
bool fullFrame;
|
|
|
|
myarray data;
|
|
|
|
};
|
|
|
|
|
2023-11-26 23:05:35 +08:00
|
|
|
Codec *codecFind(const char *name);
|
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
Codec *(*codec_get_fun)();
|
|
|
|
} codec_def;
|
2023-11-26 23:05:35 +08:00
|
|
|
|
2023-11-27 14:31:00 +08:00
|
|
|
#define codec_export(name_, fun_) \
|
2023-11-26 23:05:35 +08:00
|
|
|
const static char __codec_##name_##_name[] __attribute__((section(".rodata.codecstr"))) = #name_; \
|
2023-11-27 14:31:00 +08:00
|
|
|
__attribute__((used)) static codec_def _codec_##name_ __attribute__((section("codecdef"))) = { \
|
|
|
|
.name = __codec_##name_##_name, \
|
|
|
|
.codec_get_fun = fun_, \
|
|
|
|
}
|
2023-11-26 23:05:35 +08:00
|
|
|
|
2023-11-21 20:28:23 +08:00
|
|
|
#endif // CODEC_H
|