添加一些基本类
This commit is contained in:
0
interface/codec.cpp
Normal file
0
interface/codec.cpp
Normal file
34
interface/codec.h
Normal file
34
interface/codec.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef CODEC_H
|
||||
#define CODEC_H
|
||||
|
||||
#include "QObject"
|
||||
#include "base/base.h"
|
||||
|
||||
class Codec : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
Codec()
|
||||
{
|
||||
fullFrame = false;
|
||||
}
|
||||
virtual ~Codec() {}
|
||||
virtual myarray encode(int srcAddr, int dstAddr, int cmd, myarray data) = 0;
|
||||
virtual myarray decode(int &srcAddr, int &dstAddr, int &cmd, myarray data) = 0;
|
||||
virtual myarray decode(int &srcAddr, int &dstAddr, int &cmd)
|
||||
{
|
||||
if (fullFrame == true)
|
||||
{
|
||||
fullFrame = false;
|
||||
return decode(srcAddr, dstAddr, cmd, data);
|
||||
}
|
||||
return myarray();
|
||||
}
|
||||
virtual bool recvByte(int byte) = 0;
|
||||
|
||||
protected:
|
||||
bool fullFrame;
|
||||
myarray data;
|
||||
};
|
||||
|
||||
#endif // CODEC_H
|
136
interface/codec_ym.cpp
Normal file
136
interface/codec_ym.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
|
||||
#include "codec.h"
|
||||
|
||||
class CodecYm : public Codec
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
CodecYm() : Codec()
|
||||
{
|
||||
is_big_data = false;
|
||||
num_to_read = 0;
|
||||
}
|
||||
~CodecYm() {}
|
||||
// 重载接收字节函数
|
||||
bool recvByte(int byte)
|
||||
{
|
||||
bool ack = false;
|
||||
if (fullFrame == true)
|
||||
{
|
||||
return fullFrame;
|
||||
}
|
||||
data.append(uint8_t(byte));
|
||||
switch (data.size())
|
||||
{
|
||||
case 2:
|
||||
if (uint8_t(data[0]) == 0x59u && uint8_t(data[1]) == 0x6du)
|
||||
{
|
||||
is_big_data = false;
|
||||
num_to_read = 4; // 帧头+2byte负载长度
|
||||
}
|
||||
else
|
||||
{
|
||||
data.remove(0, 1);
|
||||
num_to_read = 0;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
{
|
||||
int len = data[2] | (data[3] << 8);
|
||||
if (len < 65535)
|
||||
{
|
||||
is_big_data = false;
|
||||
num_to_read += len + 2; // 负载+2byte校验
|
||||
qDebug("normal data. len=%d", len);
|
||||
}
|
||||
else
|
||||
{
|
||||
is_big_data = true;
|
||||
num_to_read = 11; // 扩展数据长度
|
||||
qDebug("big data.");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 11:
|
||||
{
|
||||
if (is_big_data == true)
|
||||
{
|
||||
int len = data[7] | (data[8] << 8) | (data[9] << 16) | (data[10] << 24);
|
||||
num_to_read = 4 + len + 2; // 负载+2byte校验
|
||||
qDebug("big data. len=%d", len);
|
||||
}
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
// 此时一帧数据已完成
|
||||
if (num_to_read > 0 && num_to_read == data.size())
|
||||
{
|
||||
ack = true;
|
||||
num_to_read = 0;
|
||||
}
|
||||
|
||||
fullFrame = ack;
|
||||
return fullFrame;
|
||||
}
|
||||
// 重载解码函数
|
||||
QByteArray decode(int &srcAddr, int &dstAddr, int &cmd, QByteArray data)
|
||||
{
|
||||
Q_UNUSED(srcAddr);
|
||||
Q_UNUSED(dstAddr);
|
||||
QByteArray r;
|
||||
if (data.size() < 9)
|
||||
{
|
||||
// 主机一帧数据至少9字节
|
||||
qWarning("recv data len too less");
|
||||
return r;
|
||||
}
|
||||
|
||||
if ((uint8_t(data[0]) != 0x59u) || (uint8_t(data[1]) != 0x6du))
|
||||
{
|
||||
// 帧头不对
|
||||
qWarning("frame head not 0x59 0x6d");
|
||||
return r;
|
||||
}
|
||||
|
||||
int len = (data[2] | (data[3] << 8));
|
||||
if (len == 65535)
|
||||
{
|
||||
// 重新设置数据长度
|
||||
len = data[7] | (data[8] << 8) | (data[9] << 16) | (data[10] << 24);
|
||||
is_big_data = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
is_big_data = false;
|
||||
}
|
||||
if (len + 6 != data.size())
|
||||
{
|
||||
// 如果长度不相等则产生了数据丢失
|
||||
qWarning("recv data have lossed");
|
||||
return r;
|
||||
}
|
||||
uint8_t chk_a = 0, chk_b = 0;
|
||||
// crc::crc16((uint8_t *)data.data(),2,len+4,&chk_a,&chk_b);
|
||||
if (chk_a != uint8_t(data[data.size() - 2]) || chk_b != uint8_t(data[data.size() - 1]))
|
||||
{
|
||||
// crc校验不对
|
||||
qWarning("recv data check error:%02x,%02x %02x,%02x", chk_a, chk_b, int(data[data.size() - 2]),
|
||||
int(data[data.size() - 1]));
|
||||
}
|
||||
// 保存此流水号
|
||||
cmd_no = data[5] | (data[6] << 8);
|
||||
cmd = data[4];
|
||||
// 数据负载
|
||||
if (is_big_data == false)
|
||||
r = data.mid(7, len - 3);
|
||||
else
|
||||
r = data.mid(11, len - 7);
|
||||
return r;
|
||||
}
|
||||
|
||||
private:
|
||||
bool is_big_data;
|
||||
int num_to_read;
|
||||
int cmd_no;
|
||||
};
|
11
interface/if_tcp.cpp
Normal file
11
interface/if_tcp.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "if_tcp.h"
|
||||
|
||||
if_tcp::if_tcp()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
if_tcp::~if_tcp()
|
||||
{
|
||||
|
||||
}
|
17
interface/if_tcp.h
Normal file
17
interface/if_tcp.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef IF_TCP_H
|
||||
#define IF_TCP_H
|
||||
|
||||
#include <QObject>
|
||||
#include <interface/interface.h>
|
||||
|
||||
class if_tcp : public InterFace
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
if_tcp();
|
||||
virtual ~if_tcp();
|
||||
|
||||
protected:
|
||||
};
|
||||
|
||||
#endif // IF_TCP_H
|
32
interface/interface.cpp
Normal file
32
interface/interface.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
#include "interface.h"
|
||||
#include "string.h"
|
||||
#include "QDebug"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
InterFace *interface;
|
||||
const char *name;
|
||||
} self_table_def;
|
||||
|
||||
static self_table_def g_self_table[] =
|
||||
{
|
||||
{.interface = 0, .name = "default"},
|
||||
};
|
||||
|
||||
InterFace *interFaceFind(const char *name)
|
||||
{
|
||||
int num = sizeof(g_self_table) / sizeof(g_self_table[0]);
|
||||
self_table_def *item = 0;
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
item = &g_self_table[i];
|
||||
if (item != nullptr)
|
||||
{
|
||||
if (strcmp(name, item->name) == 0)
|
||||
return item->interface;
|
||||
}
|
||||
}
|
||||
qWarning("can not find interface named '%s'", name);
|
||||
return nullptr;
|
||||
}
|
25
interface/interface.h
Normal file
25
interface/interface.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef INTERFACE_H
|
||||
#define INTERFACE_H
|
||||
|
||||
#include <QObject>
|
||||
#include "base/base.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace std::placeholders;
|
||||
|
||||
typedef std::function<void(void)> irq_cb;
|
||||
|
||||
class InterFace : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
InterFace() {}
|
||||
virtual ~InterFace() {}
|
||||
virtual void init() = 0;
|
||||
virtual int write(myarray data) = 0;
|
||||
virtual void set_irq(irq_cb fun, myarray &data) = 0;
|
||||
};
|
||||
|
||||
InterFace *interFaceFind(const char *name);
|
||||
|
||||
#endif // INTERFACE_H
|
Reference in New Issue
Block a user