28 changed files with 1157 additions and 3052 deletions
-
7.vscode/settings.json
-
14CMakeLists.txt
-
17libzaf/include/zaf.hpp
-
0libzaf/src/zaf.cpp
-
0libzqt/QFunction.cpp
-
0libzqt/QFunction.hpp
-
0libzqt/logger.cpp
-
0libzqt/logger.hpp
-
0libzqt/zqthread.cpp
-
0libzqt/zqthread.hpp
-
100mainwindow.cpp
-
8mainwindow.h
-
2765mainwindow.ui
-
0src/camera_light_src_timing_controller/clst.cpp
-
43src/camera_light_src_timing_controller/clst.hpp
-
313src/camera_light_src_timing_controller/clst_controler.cpp
-
93src/camera_light_src_timing_controller/clst_controler.hpp
-
37src/camera_light_src_timing_controller/qt_serial_datachannel.cpp
-
49src/camera_light_src_timing_controller/qt_serial_datachannel.hpp
-
220src/xsync_udp_factory_impl.cpp
-
29src/xsync_udp_factory_impl.hpp
-
2zaf_protocol
@ -1,17 +0,0 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
|
|||
|
|||
|
|||
} // namespace iflytop
|
2765
mainwindow.ui
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -1,43 +0,0 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <mutex>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
namespace iflytop { |
|||
using namespace std; |
|||
|
|||
class IXsync { |
|||
public: |
|||
virtual ~IXsync() {} |
|||
|
|||
public: |
|||
// /***********************************************************************************************
|
|||
// * 设备基本操作 *
|
|||
// ***********************************************************************************************/
|
|||
|
|||
// virtual xs_error_code_t changeXsyncIp(string xsync_ip) = 0;
|
|||
// virtual bool ping() = 0;
|
|||
|
|||
// virtual xs_error_code_t generatorNewMac() = 0;
|
|||
// virtual xs_error_code_t factoryReset() = 0;
|
|||
// virtual xs_error_code_t reboot() = 0;
|
|||
// virtual xs_error_code_t changeNetworkConfig(string ip, string mask, string gateway) = 0;
|
|||
// virtual xs_error_code_t readSn(string &sn) = 0;
|
|||
// virtual xs_error_code_t readMac(string &mac) = 0;
|
|||
|
|||
// public:
|
|||
// virtual xs_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, uint32_t ®backvalue, int32_t overtime_ms = 100) = 0;
|
|||
// virtual xs_error_code_t reg_read(uint32_t regadd, uint32_t ®value, int32_t overtime_ms = 100) = 0;
|
|||
|
|||
public: |
|||
}; |
|||
|
|||
} // namespace xsync
|
@ -0,0 +1,313 @@ |
|||
#include "clst_controler.hpp"
|
|||
|
|||
#include <stdlib.h>
|
|||
#include <string.h>
|
|||
|
|||
#include "logger.hpp"
|
|||
using namespace iflytop; |
|||
using namespace clst; |
|||
|
|||
#define TAG "CLSTControler"
|
|||
|
|||
CLSTControler *CLSTControler::ins() { |
|||
static CLSTControler *ins = nullptr; |
|||
if (ins == nullptr) { |
|||
ins = new CLSTControler(); |
|||
} |
|||
return ins; |
|||
} |
|||
|
|||
void CLSTControler::initialize(IDataChannel *channel) { //
|
|||
m_channel = channel; |
|||
m_channel->regRxListener([this](uint8_t *data, size_t len) { |
|||
{ |
|||
lock_guard<mutex> lock(lock_); |
|||
if (len + m_rxlen > sizeof(m_rxcache)) { |
|||
m_rxlen = 0; |
|||
} |
|||
memcpy(m_rxcache + m_rxlen, data, len); |
|||
m_rxlen += len; |
|||
} |
|||
}); |
|||
|
|||
m_thread.reset(new thread([this]() { |
|||
uint32_t last_rx_cnt = 0; |
|||
uint8_t rx_process_cache[1024]; |
|||
uint32_t rx_process_cache_len; |
|||
while (true) { |
|||
this_thread::sleep_for(chrono::milliseconds(5)); |
|||
|
|||
{ |
|||
lock_guard<mutex> lock(lock_); |
|||
if (last_rx_cnt == m_rxlen && m_rxlen != 0) { |
|||
memcpy(rx_process_cache, m_rxcache, m_rxlen); |
|||
rx_process_cache_len = m_rxlen; |
|||
|
|||
m_rxlen = 0; |
|||
last_rx_cnt = 0; |
|||
} |
|||
} |
|||
|
|||
if (rx_process_cache_len != 0) { |
|||
processRxData(rx_process_cache, rx_process_cache_len); |
|||
memset(rx_process_cache, 0, sizeof(rx_process_cache)); |
|||
rx_process_cache_len = 0; |
|||
} |
|||
|
|||
last_rx_cnt = m_rxlen; |
|||
} |
|||
})); |
|||
} |
|||
|
|||
void CLSTControler::regRawDataListener(raw_data_cb_t cb) { m_raw_data_cb = cb; } |
|||
|
|||
void CLSTControler::processRxData(uint8_t *rx, uint32_t rxlen) { |
|||
/**
|
|||
* @brief |
|||
* 1. findHeader |
|||
* 2. processRx |
|||
* |
|||
* ²âÊÔÖ¸Áî: |
|||
* 5A 5A 02 00 01 00 01 00 01 00 01 00 00 00 06 A5 A5 |
|||
* |
|||
*/ |
|||
// ZLOGI(TAG, "processRxData %d", rxlen);
|
|||
|
|||
for (uint32_t i = 0; i < rxlen; i++) { |
|||
zaf_packet_header_t *header = (zaf_packet_header_t *)(&rx[i]); |
|||
uint8_t *packetu8 = &rx[i]; |
|||
|
|||
if (header->packet_header == PACKET_HEADER) { |
|||
uint8_t check = packetu8[header->ndata * 4 + sizeof(zaf_packet_header_t) + 0]; |
|||
uint8_t tail0 = packetu8[header->ndata * 4 + sizeof(zaf_packet_header_t) + 1]; |
|||
uint8_t tail1 = packetu8[header->ndata * 4 + sizeof(zaf_packet_header_t) + 2]; |
|||
|
|||
uint16_t tail = (tail1 << 8) | tail0; |
|||
|
|||
uint8_t expectcheck = 0; |
|||
for (uint32_t j = 2; j < header->ndata * 4 + sizeof(zaf_packet_header_t); j++) { |
|||
expectcheck += packetu8[j]; |
|||
} |
|||
|
|||
if (tail == PACKET_TAIL) { |
|||
if (expectcheck == check) { |
|||
processRxPacket(header); |
|||
} else { |
|||
ZLOGE(TAG, "Rx packet check error %d != %d", expectcheck, check); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
void CLSTControler::processRxPacket(zaf_packet_header_t *packet) { //
|
|||
// ZLOGI(TAG, "RX packet");
|
|||
// ZLOGI(TAG, " type :%d", packet->packet_type);
|
|||
// ZLOGI(TAG, " index :%d", packet->index);
|
|||
// ZLOGI(TAG, " cmd :%d", packet->cmd);
|
|||
// ZLOGI(TAG, " ndata :%d", packet->ndata);
|
|||
// for (uint32_t i = 0; i < packet->ndata; i++) {
|
|||
// ZLOGI(TAG, " data[%d]:%d", i, packet->data[i]);
|
|||
// }
|
|||
if (packet->packet_type == kzaf_packet_type_receipt) { |
|||
lock_guard<mutex> lock(m_rxReceiptContext_lock); |
|||
if (m_rxReceiptContext.waittingForReceipt) { |
|||
if (m_rxReceiptContext.waittingIndex == packet->index) { |
|||
m_rxReceiptContext.receiptIsReady = true; |
|||
m_rxReceiptContext.receiptLen = packet->ndata; |
|||
memcpy(m_rxReceiptContext.receipt, packet->data, packet->ndata); |
|||
} |
|||
m_rxReceiptContext.waittingForReceipt = false; |
|||
} |
|||
} |
|||
|
|||
if (m_raw_data_cb) { |
|||
m_raw_data_cb(kuart_raw_rx, (uint8_t *)packet, sizeof(zaf_packet_header_t) + packet->ndata * 4 + 3); |
|||
} |
|||
} |
|||
|
|||
bool CLSTControler::ping() {} |
|||
|
|||
zaf_error_code_t CLSTControler::sendPacket(zaf_packet_header_t *packet, uint32_t len, uint32_t overtime) { |
|||
zaf_packet_header_t *rxpacket = (zaf_packet_header_t *)&m_rxReceiptContext.receipt[0]; |
|||
|
|||
{ |
|||
lock_guard<mutex> lock(m_rxReceiptContext_lock); |
|||
m_rxReceiptContext.waittingIndex = packet->index; |
|||
m_rxReceiptContext.waittingForReceipt = true; |
|||
m_rxReceiptContext.receiptIsReady = false; |
|||
m_rxReceiptContext.receiptLen = 0; |
|||
} |
|||
|
|||
if (!m_channel) { |
|||
return kaf_ec_overtime; |
|||
} |
|||
|
|||
if (m_raw_data_cb) { |
|||
m_raw_data_cb(kuart_raw_tx, (uint8_t *)packet, sizeof(zaf_packet_header_t) + packet->ndata * 4 + 3); |
|||
} |
|||
m_channel->send((uint8_t *)packet, len); |
|||
|
|||
for (size_t i = 0; i < overtime; i++) { |
|||
{ |
|||
lock_guard<mutex> lock(m_rxReceiptContext_lock); |
|||
if (m_rxReceiptContext.receiptIsReady) { |
|||
if (rxpacket->data[0] != 0) { |
|||
return (zaf_error_code_t)rxpacket->data[0]; |
|||
} else { |
|||
return kaf_ec_success; |
|||
} |
|||
} |
|||
} |
|||
|
|||
this_thread::sleep_for(chrono::milliseconds(1)); |
|||
} |
|||
return kaf_ec_overtime; |
|||
} |
|||
|
|||
#define PACKET_LEN(__packet) (sizeof(zaf_packet_header_t) + (__packet->ndata) * sizeof(uint32_t) + 1 /*checksum*/ + 2 /*tail*/)
|
|||
|
|||
zaf_error_code_t CLSTControler::reg_write(uint32_t regadd, uint32_t regvalue, uint32_t ®backvalue, int32_t overtime_ms) { //
|
|||
lock_guard<mutex> lock(m_tx_lock); |
|||
|
|||
uint8_t txdata[128] = {0}; |
|||
|
|||
zaf_packet_header_t *txpacket = (zaf_packet_header_t *)txdata; |
|||
zaf_packet_header_t *rxpacket = (zaf_packet_header_t *)&m_rxReceiptContext.receipt[0]; |
|||
|
|||
txpacket->packet_header = PACKET_HEADER; |
|||
txpacket->packet_type = kzaf_packet_type_cmd; |
|||
txpacket->index = ++txindex; |
|||
txpacket->cmd = kzaf_cmd_reg_write; |
|||
txpacket->ndata = 2; |
|||
txpacket->data[0] = regadd; |
|||
txpacket->data[1] = regvalue; |
|||
|
|||
uint32_t txpacklen = PACKET_LEN(txpacket); |
|||
uint8_t checksum = 0; |
|||
for (int i = 2; i < txpacklen - 3; i++) { |
|||
checksum += txdata[i]; |
|||
} |
|||
txdata[txpacklen - 3] = checksum; |
|||
txdata[txpacklen - 2] = PACKET_TAIL & 0xFF; |
|||
txdata[txpacklen - 1] = (PACKET_TAIL >> 8) & 0xFF; |
|||
|
|||
zaf_error_code_t ecode = sendPacket(txpacket, txpacklen, overtime_ms); |
|||
if (ecode != kaf_ec_success) { |
|||
return ecode; |
|||
} |
|||
|
|||
regbackvalue = rxpacket->data[1]; |
|||
return kaf_ec_success; |
|||
} |
|||
zaf_error_code_t CLSTControler::reg_read(uint32_t regadd, uint32_t ®value, int32_t overtime_ms) { |
|||
lock_guard<mutex> lock(m_tx_lock); |
|||
|
|||
uint8_t txdata[128] = {0}; |
|||
|
|||
zaf_packet_header_t *txpacket = (zaf_packet_header_t *)txdata; |
|||
zaf_packet_header_t *rxpacket = (zaf_packet_header_t *)&m_rxReceiptContext.receipt[0]; |
|||
|
|||
txpacket->packet_header = PACKET_HEADER; |
|||
txpacket->packet_type = kzaf_packet_type_cmd; |
|||
txpacket->index = ++txindex; |
|||
txpacket->cmd = kzaf_cmd_reg_read; |
|||
txpacket->ndata = 1; |
|||
txpacket->data[0] = regadd; |
|||
|
|||
uint32_t txpacklen = PACKET_LEN(txpacket); |
|||
uint8_t checksum = 0; |
|||
for (int i = 2; i < txpacklen - 3; i++) { |
|||
checksum += txdata[i]; |
|||
} |
|||
txdata[txpacklen - 3] = checksum; |
|||
txdata[txpacklen - 2] = PACKET_TAIL & 0xFF; |
|||
txdata[txpacklen - 1] = (PACKET_TAIL >> 8) & 0xFF; |
|||
|
|||
zaf_error_code_t ecode = sendPacket(txpacket, txpacklen, overtime_ms); |
|||
if (ecode != kaf_ec_success) { |
|||
return ecode; |
|||
} |
|||
|
|||
regvalue = rxpacket->data[1]; |
|||
return kaf_ec_success; |
|||
} |
|||
|
|||
zaf_error_code_t CLSTControler::factoryReset() { |
|||
uint8_t txdata[128] = {0}; |
|||
|
|||
zaf_packet_header_t *txpacket = (zaf_packet_header_t *)txdata; |
|||
zaf_packet_header_t *rxpacket = (zaf_packet_header_t *)&m_rxReceiptContext.receipt[0]; |
|||
|
|||
txpacket->packet_header = PACKET_HEADER; |
|||
txpacket->packet_type = kzaf_packet_type_cmd; |
|||
txpacket->index = ++txindex; |
|||
txpacket->cmd = kzaf_cmd_factory_reset; |
|||
txpacket->ndata = 0; |
|||
|
|||
uint32_t txpacklen = PACKET_LEN(txpacket); |
|||
uint8_t checksum = 0; |
|||
for (int i = 2; i < txpacklen - 3; i++) { |
|||
checksum += txdata[i]; |
|||
} |
|||
txdata[txpacklen - 3] = checksum; |
|||
txdata[txpacklen - 2] = PACKET_TAIL & 0xFF; |
|||
txdata[txpacklen - 1] = (PACKET_TAIL >> 8) & 0xFF; |
|||
|
|||
zaf_error_code_t ecode = sendPacket(txpacket, txpacklen, 1000); |
|||
if (ecode != kaf_ec_success) return ecode; |
|||
|
|||
return kaf_ec_success; |
|||
} |
|||
zaf_error_code_t CLSTControler::reboot() { |
|||
uint8_t txdata[128] = {0}; |
|||
|
|||
zaf_packet_header_t *txpacket = (zaf_packet_header_t *)txdata; |
|||
zaf_packet_header_t *rxpacket = (zaf_packet_header_t *)&m_rxReceiptContext.receipt[0]; |
|||
|
|||
txpacket->packet_header = PACKET_HEADER; |
|||
txpacket->packet_type = kzaf_packet_type_cmd; |
|||
txpacket->index = ++txindex; |
|||
txpacket->cmd = kzaf_cmd_reboot; |
|||
txpacket->ndata = 0; |
|||
|
|||
uint32_t txpacklen = PACKET_LEN(txpacket); |
|||
uint8_t checksum = 0; |
|||
for (int i = 2; i < txpacklen - 3; i++) { |
|||
checksum += txdata[i]; |
|||
} |
|||
txdata[txpacklen - 3] = checksum; |
|||
txdata[txpacklen - 2] = PACKET_TAIL & 0xFF; |
|||
txdata[txpacklen - 1] = (PACKET_TAIL >> 8) & 0xFF; |
|||
|
|||
zaf_error_code_t ecode = sendPacket(txpacket, txpacklen, 100); |
|||
if (ecode != kaf_ec_success) return ecode; |
|||
|
|||
return kaf_ec_success; |
|||
} |
|||
zaf_error_code_t CLSTControler::storageConfigs() { |
|||
uint8_t txdata[128] = {0}; |
|||
|
|||
zaf_packet_header_t *txpacket = (zaf_packet_header_t *)txdata; |
|||
zaf_packet_header_t *rxpacket = (zaf_packet_header_t *)&m_rxReceiptContext.receipt[0]; |
|||
|
|||
txpacket->packet_header = PACKET_HEADER; |
|||
txpacket->packet_type = kzaf_packet_type_cmd; |
|||
txpacket->index = ++txindex; |
|||
txpacket->cmd = kzaf_cmd_storage_cfg; |
|||
txpacket->ndata = 0; |
|||
|
|||
uint32_t txpacklen = PACKET_LEN(txpacket); |
|||
uint8_t checksum = 0; |
|||
for (int i = 2; i < txpacklen - 3; i++) { |
|||
checksum += txdata[i]; |
|||
} |
|||
txdata[txpacklen - 3] = checksum; |
|||
txdata[txpacklen - 2] = PACKET_TAIL & 0xFF; |
|||
txdata[txpacklen - 1] = (PACKET_TAIL >> 8) & 0xFF; |
|||
|
|||
zaf_error_code_t ecode = sendPacket(txpacket, txpacklen, 100); |
|||
if (ecode != kaf_ec_success) return ecode; |
|||
|
|||
return kaf_ec_success; |
|||
} |
@ -0,0 +1,93 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <mutex>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <thread>
|
|||
#include <vector>
|
|||
|
|||
#include "zaf_protocol/zaf_protocol.h"
|
|||
|
|||
#define SDK_VERSION 1
|
|||
|
|||
namespace iflytop { |
|||
namespace clst { |
|||
using namespace std; |
|||
|
|||
typedef enum { |
|||
kuart_raw_tx, |
|||
kuart_raw_rx, |
|||
} uart_message_type_t; |
|||
|
|||
typedef function<void(bool connect)> device_state_cb_t; |
|||
typedef function<void(uart_message_type_t type, uint8_t *data, size_t len)> raw_data_cb_t; |
|||
|
|||
class IDataChannel { |
|||
public: |
|||
virtual ~IDataChannel(){}; |
|||
virtual bool isOpen() = 0; |
|||
virtual bool send(const uint8_t *data, size_t len) = 0; |
|||
virtual void regRxListener(function<void(uint8_t *data, size_t len)> cb) = 0; |
|||
}; |
|||
|
|||
class RxReceiptContext { |
|||
public: |
|||
bool waittingForReceipt; |
|||
bool receiptIsReady; |
|||
uint16_t waittingIndex; |
|||
uint8_t receipt[1024]; |
|||
size_t receiptLen; |
|||
}; |
|||
|
|||
class CLSTControler { |
|||
CLSTControler() {} |
|||
|
|||
IDataChannel *m_channel = nullptr; |
|||
|
|||
uint8_t m_rxcache[1024]; |
|||
size_t m_rxlen = 0; |
|||
|
|||
mutex lock_; |
|||
|
|||
unique_ptr<thread> m_thread; |
|||
|
|||
RxReceiptContext m_rxReceiptContext; |
|||
mutex m_rxReceiptContext_lock; |
|||
|
|||
mutex m_tx_lock; |
|||
|
|||
|
|||
raw_data_cb_t m_raw_data_cb; |
|||
uint16_t txindex = 0; |
|||
|
|||
public: |
|||
static CLSTControler *ins(); |
|||
|
|||
void initialize(IDataChannel *channel); |
|||
void regRawDataListener(raw_data_cb_t cb); |
|||
|
|||
public: |
|||
bool ping(); |
|||
zaf_error_code_t factoryReset(); |
|||
zaf_error_code_t reboot(); |
|||
zaf_error_code_t storageConfigs(); |
|||
|
|||
public: |
|||
zaf_error_code_t reg_write(uint32_t regadd, uint32_t regvalue, uint32_t ®backvalue, int32_t overtime_ms); |
|||
zaf_error_code_t reg_read(uint32_t regadd, uint32_t ®value, int32_t overtime_ms); |
|||
|
|||
public: |
|||
void processRxData(uint8_t *rx, uint32_t rxlen); |
|||
void processRxPacket(zaf_packet_header_t *packet); |
|||
|
|||
zaf_error_code_t sendPacket(zaf_packet_header_t *packet, uint32_t len, uint32_t overtime); |
|||
}; |
|||
|
|||
} // namespace clst
|
|||
} // namespace iflytop
|
@ -0,0 +1,37 @@ |
|||
#include "qt_serial_datachannel.hpp"
|
|||
|
|||
#include <QtSerialPort/QSerialPort>
|
|||
#include <QtSerialPort/QSerialPortInfo>
|
|||
|
|||
#include "logger.hpp"
|
|||
|
|||
using namespace iflytop; |
|||
using namespace std; |
|||
using namespace clst; |
|||
|
|||
void QTDataChannel::bind(QSerialPort *serialPort) { //
|
|||
m_serialPort = serialPort; |
|||
connect(m_serialPort, SIGNAL(readyRead()), this, SLOT(readyReadSlot())); |
|||
} |
|||
|
|||
void QTDataChannel::readyReadSlot() { |
|||
if (!m_serialPort) return; |
|||
|
|||
QByteArray data = m_serialPort->readAll(); |
|||
// ZLOGI("QTDataChannel", "RX %d", data.size());
|
|||
if (m_rxcb) m_rxcb((uint8_t *)data.data(), data.size()); |
|||
} |
|||
|
|||
bool QTDataChannel::isOpen() { |
|||
if (!m_serialPort) return false; |
|||
return m_serialPort->isOpen(); |
|||
} |
|||
bool QTDataChannel::send(const uint8_t *data, size_t len) { |
|||
if (!m_serialPort) return false; |
|||
|
|||
QByteArray qdata; |
|||
qdata.append((char *)data, len); |
|||
m_serialPort->write(qdata); |
|||
return true; |
|||
} |
|||
void QTDataChannel::regRxListener(function<void(uint8_t *data, size_t len)> cb) { m_rxcb = cb; } |
@ -0,0 +1,49 @@ |
|||
#pragma once
|
|||
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <mutex>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
//
|
|||
#include <QtSerialPort/QSerialPort>
|
|||
#include <QtSerialPort/QSerialPortInfo>
|
|||
//
|
|||
#include "clst_controler.hpp"
|
|||
#include "zqthread.hpp"
|
|||
|
|||
#define SDK_VERSION 1
|
|||
|
|||
namespace iflytop { |
|||
namespace clst { |
|||
using namespace std; |
|||
// QT_CHARTS_USE_NAMESPACE
|
|||
|
|||
typedef function<void(bool connect)> device_state_cb_t; |
|||
|
|||
class QTDataChannel : public QObject, public IDataChannel { |
|||
Q_OBJECT |
|||
|
|||
function<void(uint8_t *data, size_t len)> m_rxcb; |
|||
|
|||
QSerialPort *m_serialPort = nullptr; |
|||
unique_ptr<ZQThread> m_thread; |
|||
|
|||
public: |
|||
void bind(QSerialPort *serialPort); |
|||
|
|||
virtual bool isOpen() override; |
|||
virtual bool send(const uint8_t *data, size_t len) override; |
|||
virtual void regRxListener(function<void(uint8_t *data, size_t len)> cb) override; |
|||
|
|||
public slots: |
|||
void readyReadSlot(); |
|||
}; |
|||
} // namespace clst
|
|||
} // namespace iflytop
|
@ -1,220 +0,0 @@ |
|||
#include "xsync_udp_factory_impl.hpp"
|
|||
|
|||
#include "zqthread.hpp"
|
|||
//
|
|||
#include <winsock2.h>
|
|||
//
|
|||
#include <Windows.h>
|
|||
|
|||
#define ENABLE_LOG
|
|||
#ifdef ENABLE_LOG
|
|||
#include "../src/logger.hpp"
|
|||
#endif
|
|||
#define TAG "XSYNC_UDP"
|
|||
#pragma comment(lib, "ws2_32")
|
|||
using namespace xsync; |
|||
using namespace iflytop; |
|||
|
|||
/*******************************************************************************
|
|||
* XSUDP * |
|||
*******************************************************************************/ |
|||
|
|||
#define USB_NO_BLOCK_UDP 0
|
|||
|
|||
class XSUDP : public I_XSUDP { |
|||
uint32_t m_ip; |
|||
int m_localport; |
|||
|
|||
struct sockaddr_in localadd = {}; |
|||
int m_sock_fd = 0; |
|||
unique_ptr<ZQThread> m_zq_thread; |
|||
onMessage_t m_onMessage; |
|||
|
|||
char* m_rxbuf = nullptr; |
|||
size_t m_rxbufsize = 0; |
|||
|
|||
public: |
|||
virtual xs_error_code_t initialize(string ip, int localport) override; |
|||
virtual xs_error_code_t sendto(const XsyncNetAdd& to, const char* data, int32_t length, int32_t* sendlength) override; |
|||
virtual xs_error_code_t receive(char* data, int32_t& length, XsyncNetAdd& from, int overtimems) override; |
|||
virtual xs_error_code_t startReceive(onMessage_t onMessage) override; |
|||
virtual xs_error_code_t stopReceive() override; |
|||
virtual xs_error_code_t clearRxBuffer() override; |
|||
virtual ~XSUDP(); |
|||
}; |
|||
|
|||
const char* fmtip(uint32_t ip) { |
|||
static char ipstr[16]; |
|||
sprintf(ipstr, "%d.%d.%d.%d", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); |
|||
return ipstr; |
|||
} |
|||
|
|||
xs_error_code_t XSUDP::initialize(string ip, int localport) { |
|||
localadd.sin_family = AF_INET; |
|||
localadd.sin_addr.s_addr = inet_addr(ip.c_str()); |
|||
localadd.sin_port = htons(localport); |
|||
|
|||
// 创建客户端用于通信的Socket
|
|||
m_sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
|||
if (m_sock_fd < 0) return kxs_ec_socket_fail; |
|||
|
|||
int ret = bind(m_sock_fd, (struct sockaddr*)&localadd, sizeof(localadd)); |
|||
if (ret < 0) return kxs_ec_bind_fail; |
|||
|
|||
#if USB_NO_BLOCK_UDP
|
|||
u_long mode = 1; |
|||
if (ioctlsocket(m_sock_fd, FIONBIO, &mode) != NO_ERROR) { |
|||
return kxs_ec_setsockopt_rx_timeout_fail; |
|||
} |
|||
#endif
|
|||
return kxs_ec_success; |
|||
} |
|||
|
|||
xs_error_code_t XSUDP::sendto(const XsyncNetAdd& to, const char* data, int32_t length, int32_t* sendlength) { |
|||
struct sockaddr_in sockaddr; |
|||
sockaddr.sin_family = AF_INET; |
|||
sockaddr.sin_addr.s_addr = inet_addr(to.ip.c_str()); |
|||
sockaddr.sin_port = htons(to.port); |
|||
int ret = ::sendto(m_sock_fd, data, length, 0, (struct sockaddr*)&sockaddr, sizeof(sockaddr)); |
|||
if (sendlength) *sendlength = ret; |
|||
if (ret >= 0) { |
|||
return kxs_ec_success; |
|||
} |
|||
return kxs_ec_send_fail; |
|||
} |
|||
xs_error_code_t XSUDP::receive(char* data, int32_t& length, XsyncNetAdd& from, int overtimems) { |
|||
#if USB_NO_BLOCK_UDP
|
|||
struct sockaddr_in sockaddr = {0}; |
|||
int32_t overtime_10ms = overtimems / 10; |
|||
int ret = 0; |
|||
overtime_10ms += 1; |
|||
for (size_t i = 0;; i++) { |
|||
int senderAddressLen = sizeof(sockaddr); |
|||
ret = ::recvfrom(m_sock_fd, (char*)data, length, 0, (struct sockaddr*)&sockaddr, &senderAddressLen); |
|||
length = ret; |
|||
|
|||
if (ret > 0) break; |
|||
if (i >= overtime_10ms) break; |
|||
Sleep(10); |
|||
} |
|||
|
|||
if (ret <= 0) { |
|||
return kxs_ec_overtime; |
|||
} |
|||
|
|||
uint32_t ip = ntohl(sockaddr.sin_addr.s_addr); |
|||
from.ip = string(fmtip(ip)); |
|||
from.port = ntohs(sockaddr.sin_port); |
|||
|
|||
return kxs_ec_success; |
|||
#else
|
|||
struct sockaddr_in sockaddr = {0}; |
|||
|
|||
timeval timeout; |
|||
timeout.tv_sec = overtimems; // 这个结构体的单位是ms,不是秒
|
|||
timeout.tv_usec = 0; |
|||
|
|||
if (timeout.tv_sec == 0) { |
|||
timeout.tv_sec = 1; |
|||
} |
|||
|
|||
if (setsockopt(m_sock_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout, sizeof(timeout)) == -1) { |
|||
return kxs_ec_setsockopt_rx_timeout_fail; |
|||
} |
|||
int senderAddressLen = sizeof(sockaddr); |
|||
int ret = ::recvfrom(m_sock_fd, (char*)data, length, 0, (struct sockaddr*)&sockaddr, &senderAddressLen); |
|||
length = ret; |
|||
data[length] = 0; |
|||
|
|||
if (ret < 0) { |
|||
// ZLOGI(TAG, "recvfrom error %d %s", ret, strerror(errno));
|
|||
// if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
|||
return kxs_ec_overtime; |
|||
// } else {
|
|||
// return kxs_ec_receive_fail;
|
|||
// }
|
|||
} |
|||
|
|||
// inet_ntop(AF_INET, &(sockaddr.sin_addr), ip, INET_ADDRSTRLEN);
|
|||
uint32_t ip = ntohl(sockaddr.sin_addr.s_addr); |
|||
from.ip = string(fmtip(ip)); |
|||
from.port = ntohs(sockaddr.sin_port); |
|||
|
|||
return kxs_ec_success; |
|||
#endif
|
|||
} |
|||
|
|||
xs_error_code_t XSUDP::startReceive(onMessage_t onMessage) { |
|||
m_onMessage = onMessage; |
|||
|
|||
if (m_zq_thread) { |
|||
return kxs_ec_success; |
|||
} |
|||
m_rxbuf = (char*)malloc(10240); |
|||
m_rxbufsize = 10240; |
|||
|
|||
m_zq_thread.reset(new ZQThread("udplistener_thread", [this]() { |
|||
while (!m_zq_thread->isTryExit()) { |
|||
memset(m_rxbuf, 0, m_rxbufsize); |
|||
int32_t length = m_rxbufsize; |
|||
XsyncNetAdd from; |
|||
xs_error_code_t ret = receive(m_rxbuf, length, from, 1000); |
|||
if (ret == kxs_ec_success) { |
|||
if (m_onMessage) m_onMessage(from, (uint8_t*)m_rxbuf, length); |
|||
} |
|||
} |
|||
})); |
|||
m_zq_thread->start(); |
|||
return kxs_ec_success; |
|||
} |
|||
|
|||
xs_error_code_t XSUDP::stopReceive() { |
|||
if (m_zq_thread) { |
|||
m_zq_thread->quit(); |
|||
m_zq_thread->wait(); |
|||
m_zq_thread.reset(nullptr); |
|||
} |
|||
|
|||
if (!m_rxbuf) { |
|||
free(m_rxbuf); |
|||
m_rxbuf = nullptr; |
|||
m_rxbufsize = 0; |
|||
} |
|||
} |
|||
|
|||
xs_error_code_t XSUDP::clearRxBuffer() { |
|||
#if USB_NO_BLOCK_UDP
|
|||
char buf[1024]; |
|||
int32_t length = 1024; |
|||
XsyncNetAdd from; |
|||
xs_error_code_t ret = kxs_ec_success; |
|||
|
|||
while (ret == kxs_ec_success) { |
|||
ret = receive(buf, length, from, 1); |
|||
} |
|||
#endif
|
|||
|
|||
return kxs_ec_success; |
|||
} |
|||
|
|||
XSUDP::~XSUDP() { |
|||
stopReceive(); |
|||
|
|||
if (m_sock_fd > 0) { |
|||
closesocket(m_sock_fd); |
|||
m_sock_fd = -1; |
|||
} |
|||
} |
|||
|
|||
/*******************************************************************************
|
|||
* xSyncUdpFactoryImpl * |
|||
*******************************************************************************/ |
|||
|
|||
void XSyncUdpFactoryImpl::initialize() {} |
|||
|
|||
XSyncUdpFactoryImpl* XSyncUdpFactoryImpl::Ins() { |
|||
static XSyncUdpFactoryImpl instance; |
|||
return &instance; |
|||
} |
|||
|
|||
shared_ptr<I_XSUDP> XSyncUdpFactoryImpl::createXSUDP() { return make_shared<XSUDP>(); } |
@ -1,29 +0,0 @@ |
|||
#pragma once
|
|||
#include <fstream>
|
|||
#include <functional>
|
|||
#include <iostream>
|
|||
#include <list>
|
|||
#include <map>
|
|||
#include <memory>
|
|||
#include <set>
|
|||
#include <sstream>
|
|||
#include <string>
|
|||
#include <vector>
|
|||
|
|||
#include "xsync_v2.hpp"
|
|||
namespace xsync { |
|||
using namespace std; |
|||
|
|||
class XSyncUdpFactoryImpl : public I_XSUDPFactory { |
|||
private: |
|||
/* data */ |
|||
XSyncUdpFactoryImpl(){}; |
|||
|
|||
public: |
|||
static XSyncUdpFactoryImpl* Ins(); |
|||
|
|||
void initialize(); |
|||
virtual shared_ptr<I_XSUDP> createXSUDP() override; |
|||
}; |
|||
|
|||
} // namespace xsync
|
@ -1 +1 @@ |
|||
Subproject commit 1f37a710c5a4cb8fee30ced8e246488310f9017a |
|||
Subproject commit 7d27a34ee665cc3cba2d12ff357e774e34871b38 |
Write
Preview
Loading…
Cancel
Save
Reference in new issue