From 91ac0330e7491d31b80e3bb32a62ce028ffd8174 Mon Sep 17 00:00:00 2001 From: zhaohe Date: Sat, 15 Jun 2024 19:01:41 +0800 Subject: [PATCH] update --- .gitignore | 3 +- .vscode/c_cpp_properties.json | 21 -- .vscode/settings.json | 89 -------- bak/xsync_udp_factory_impl.cpp | 220 ------------------- bak/xsync_udp_factory_impl.hpp | 29 --- udp_packet_monitor/build.bat | 1 + udp_packet_monitor/main.cpp | 237 +++++++++++++++++++++ udp_receiver/main.cpp | 230 -------------------- xsync_timecode_recorder/build.bat | 2 + xsync_timecode_recorder/build2.bat | 1 + xsync_timecode_recorder/build_xsync.bat | 2 + .../xsync_timecode_recorder.cpp | 220 +++++++++++++++++++ 12 files changed, 465 insertions(+), 590 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json delete mode 100644 .vscode/settings.json delete mode 100644 bak/xsync_udp_factory_impl.cpp delete mode 100644 bak/xsync_udp_factory_impl.hpp create mode 100644 udp_packet_monitor/build.bat create mode 100644 udp_packet_monitor/main.cpp delete mode 100644 udp_receiver/main.cpp create mode 100644 xsync_timecode_recorder/build.bat create mode 100644 xsync_timecode_recorder/build2.bat create mode 100644 xsync_timecode_recorder/build_xsync.bat create mode 100644 xsync_timecode_recorder/xsync_timecode_recorder.cpp diff --git a/.gitignore b/.gitignore index 8b88018..bdbd42b 100644 --- a/.gitignore +++ b/.gitignore @@ -71,4 +71,5 @@ Thumbs.db *.dll *.exe -build \ No newline at end of file +build +*.log \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 913ddf2..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "configurations": [ - { - "name": "Win32", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [ - "_DEBUG", - "UNICODE", - "_UNICODE" - ], - "compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe", - "cStandard": "c17", - "cppStandard": "gnu++17", - "intelliSenseMode": "windows-gcc-x64", - "compileCommands": "${workspaceFolder}/build/compile_commands.json" - } - ], - "version": 4 -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 3b20dbb..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,89 +0,0 @@ -{ - "files.associations": { - "*.hpp": "cpp", - "qapplication": "cpp", - "qdebug": "cpp", - "qdatetime": "cpp", - "vector": "cpp", - "typeindex": "cpp", - "typeinfo": "cpp", - "string": "cpp", - "memory": "cpp", - "any": "cpp", - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "bitset": "cpp", - "cctype": "cpp", - "charconv": "cpp", - "chrono": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "codecvt": "cpp", - "compare": "cpp", - "complex": "cpp", - "concepts": "cpp", - "condition_variable": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "list": "cpp", - "map": "cpp", - "set": "cpp", - "unordered_map": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "ratio": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "fstream": "cpp", - "initializer_list": "cpp", - "iomanip": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "mutex": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "semaphore": "cpp", - "span": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "stop_token": "cpp", - "streambuf": "cpp", - "thread": "cpp", - "cinttypes": "cpp", - "variant": "cpp", - "qfile": "cpp", - "qmainwindow": "cpp", - "qvalueaxis": "cpp", - "qlabel": "cpp", - "future": "cpp", - "qcoreapplication": "cpp", - "qcategoryaxis": "cpp", - "qaction": "cpp", - "qthread": "cpp", - "qlineseries": "cpp", - "qsplineseries": "cpp" - }, - "files.autoGuessEncoding": true, -} \ No newline at end of file diff --git a/bak/xsync_udp_factory_impl.cpp b/bak/xsync_udp_factory_impl.cpp deleted file mode 100644 index 2fb937c..0000000 --- a/bak/xsync_udp_factory_impl.cpp +++ /dev/null @@ -1,220 +0,0 @@ -#include "xsync_udp_factory_impl.hpp" - -#include "zqthread.hpp" -// -#include -// -#include - -#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 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 XSyncUdpFactoryImpl::createXSUDP() { return make_shared(); } \ No newline at end of file diff --git a/bak/xsync_udp_factory_impl.hpp b/bak/xsync_udp_factory_impl.hpp deleted file mode 100644 index 42409f1..0000000 --- a/bak/xsync_udp_factory_impl.hpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#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 createXSUDP() override; -}; - -} // namespace xsync \ No newline at end of file diff --git a/udp_packet_monitor/build.bat b/udp_packet_monitor/build.bat new file mode 100644 index 0000000..2c3fda6 --- /dev/null +++ b/udp_packet_monitor/build.bat @@ -0,0 +1 @@ +g++ -static -static-libgcc -static-libstdc++ -I ../libxsync/include main.cpp -L "./" -lwsock32 -lstdc++ -o udp_packet_monitor.exe \ No newline at end of file diff --git a/udp_packet_monitor/main.cpp b/udp_packet_monitor/main.cpp new file mode 100644 index 0000000..31debb6 --- /dev/null +++ b/udp_packet_monitor/main.cpp @@ -0,0 +1,237 @@ +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// +#include + +#pragma comment(lib, "ws2_32.lib") +#define PORT 19903 +using namespace std; + +typedef struct { + uint32_t eventid; + int32_t data[4]; +} iflytop_xsync_event_report_packet_t; + +static std::ofstream* logfile; + +/** + * @brief 获取 年月日时分秒:毫秒 + * + * @return string + */ +static const char* gettime_str() { + time_t t = time(0); + static char tmp[64]; + strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&t)); + struct tm* tt = localtime(&t); + // sprintf(tmp, "%s:%03d", tmp, GetTickCount() % 1000); + return tmp; +} + +static string getlogfilename() { + time_t t = time(0); + char tmp[64]; + strftime(tmp, sizeof(tmp), "%Y%m%d_%H%M%S.log", localtime(&t)); + return tmp; +} + +/** + * @brief 写日志到文件中 + */ +bool log_file_init() { + // 创建日志文件 + logfile = new ofstream(getlogfilename(), ios::app); + if (logfile == NULL) { + printf("创建日志文件失败\n"); + return false; + } + return true; +} + +void log(const char* fmt, ...) { + char buf[1024] = {0}; + va_list args; + va_start(args, fmt); + vsprintf(buf, fmt, args); + va_end(args); + cout << buf; +} + +void logd(const char* fmt, ...) { + char buf[1024] = {0}; + va_list args; + va_start(args, fmt); + vsprintf(buf, fmt, args); + va_end(args); + if (logfile) { + *logfile << buf; + logfile->flush(); + } + cout << buf; +} + +#define LOGD(fmt, ...) logd("[%s] INFO : " fmt "\n", gettime_str(), ##__VA_ARGS__); + +int64_t gettime() { + auto currentTime = std::chrono::system_clock::now(); + return currentTime.time_since_epoch().count() / 1000000; +} +typedef struct { + uint32_t tc0; + uint32_t tc1; + uint32_t subframe; +} Timecode64_t; + +typedef struct { + uint8_t hour; + uint8_t minute; + uint8_t second; + uint8_t frame; + uint8_t subframe; +} XsyncTimecode_t; + +XsyncTimecode_t timecode64ToXsyncTimeCode(Timecode64_t tc64) { + uint8_t frameuints = tc64.tc0 & 0x0f; + uint8_t frame10s = (tc64.tc0 >> 8) & 0x3; + uint8_t seconduints = (tc64.tc0 >> 16) & 0x0f; + uint8_t second10s = (tc64.tc0 >> 24) & 0x07; + + uint8_t minuteuints = tc64.tc1 & 0x0f; + uint8_t minute10s = (tc64.tc1 >> 8) & 0x07; + uint8_t houruints = (tc64.tc1 >> 16) & 0x0f; + uint8_t hour10s = (tc64.tc1 >> 24) & 0x03; + + XsyncTimecode_t timecode; + timecode.hour = hour10s * 10 + houruints; + timecode.minute = minute10s * 10 + minuteuints; + timecode.second = second10s * 10 + seconduints; + timecode.frame = frame10s * 10 + frameuints; + timecode.subframe = tc64.subframe; + return timecode; +} +int main(int argc, char* argv[]) { + // 初始化网络环境 + log_file_init(); + WSADATA wsa; + if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { + LOGD("WSAStartup failed"); + return -1; + } + + // 建立一个UDP的socket + SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (sock == SOCKET_ERROR) { + LOGD("create socket failed"); + return -1; + } + + // 绑定地址信息 + sockaddr_in serverAddr; + serverAddr.sin_family = AF_INET; + serverAddr.sin_port = htons(PORT); + serverAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); + + bool bOptval = true; + int32_t iret = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&bOptval, sizeof(bOptval)); + if (iret != 0) { + LOGD("setsockopt fail: %s", WSAGetLastError()); + return -1; + } + + int ret = ::bind(sock, (sockaddr*)&serverAddr, sizeof(sockaddr)); + if (ret != 0) { + LOGD("bind failed"); + return -1; + } + + char buf[512]; + + while (TRUE) { + memset(buf, 0, 512); + sockaddr_in clientAddr; + memset(&clientAddr, 0, sizeof(sockaddr_in)); + int clientAddrLen = sizeof(sockaddr); + // 接收客户端发来的数据 + int ret = recvfrom(sock, buf, 512, 0, (sockaddr*)&clientAddr, &clientAddrLen); + + iflytop_xsync_event_report_packet_t* packet = (iflytop_xsync_event_report_packet_t*)buf; + + uint32_t stm32_ticket = packet->data[3]; + uint32_t stm32_send_delay = packet->data[4]; + + Timecode64_t tc64; + tc64.tc0 = packet->data[0]; + tc64.tc1 = packet->data[1]; + tc64.subframe = packet->data[2]; + XsyncTimecode_t timecode = timecode64ToXsyncTimeCode(tc64); + + static int64_t lastrxticket; + static uint32_t laststm32ticket = 0; + static int32_t et1 = 0; + static int32_t et2 = 0; + static int32_t et3 = 0; + static int32_t et4 = 0; + static Timecode64_t lasttimecode64; + static int32_t lastdifftime = 0; + static int32_t lastdiffstm32; + int64_t nowticket = gettime(); + int64_t difftime = nowticket - lastrxticket; + int64_t diffstm32 = stm32_ticket - laststm32ticket; + static int32_t totalPacketNum = 0; + totalPacketNum++; + + { + if (abs(difftime - lastdifftime) > 4) { + et1++; + LOGD("exception_1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + } + + if (abs(diffstm32 - lastdiffstm32) > 2) { + et2++; + LOGD("exception_2 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + } + + if (stm32_send_delay > 1) { + et3++; + LOGD("exception_3 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + } + + if (memcmp(&lasttimecode64, &tc64, sizeof(Timecode64_t)) == 0) { + et4++; + LOGD("exception_4 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + } + + LOGD("pcticket:%ld(%10d) tc:%2d:%2d:%2d:%2d:%2d stm32ticket:%10d(%10d) stm32_send_delay:%d et:(%d %d %d %d)/%d", nowticket, difftime, // + timecode.hour, timecode.minute, timecode.second, timecode.frame, timecode.subframe, // + stm32_ticket, diffstm32, stm32_send_delay, // + et1 / 2, et2, et3, et4, totalPacketNum); + } + + lasttimecode64 = tc64; + + // + lastrxticket = nowticket; + laststm32ticket = stm32_ticket; + lastdifftime = difftime; + lastdiffstm32 = diffstm32; + + // 发一个数据包返回给客户 + } + return 0; +} \ No newline at end of file diff --git a/udp_receiver/main.cpp b/udp_receiver/main.cpp deleted file mode 100644 index 330f1fe..0000000 --- a/udp_receiver/main.cpp +++ /dev/null @@ -1,230 +0,0 @@ -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -// -#include - -#pragma comment(lib, "ws2_32.lib") -#define PORT 19903 -using namespace std; - -typedef struct { - uint32_t eventid; - int32_t data[4]; -} iflytop_xsync_event_report_packet_t; - -static std::ofstream* logfile; - -/** - * @brief 获取 年月日时分秒:毫秒 - * - * @return string - */ -static const char* gettime_str() { - time_t t = time(0); - static char tmp[64]; - strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&t)); - struct tm* tt = localtime(&t); - // sprintf(tmp, "%s:%03d", tmp, GetTickCount() % 1000); - return tmp; -} - -static string getlogfilename() { - time_t t = time(0); - char tmp[64]; - strftime(tmp, sizeof(tmp), "%Y%m%d_%H%M%S.txt", localtime(&t)); - return tmp; -} - -/** - * @brief 写日志到文件中 - */ -bool log_file_init() { - // 创建日志文件 - logfile = new ofstream(getlogfilename(), ios::app); - if (logfile == NULL) { - printf("创建日志文件失败\n"); - return false; - } - return true; -} - -void log(const char* fmt, ...) { - char buf[1024] = {0}; - va_list args; - va_start(args, fmt); - vsprintf(buf, fmt, args); - va_end(args); - cout << buf; -} - -void logd(const char* fmt, ...) { - char buf[1024] = {0}; - va_list args; - va_start(args, fmt); - vsprintf(buf, fmt, args); - va_end(args); - if (logfile) { - *logfile << buf; - logfile->flush(); - } - cout << buf; -} - -#define LOGD(fmt, ...) logd("[%s] INFO : " fmt "\n", gettime_str(), ##__VA_ARGS__); - -int64_t gettime() { - auto currentTime = std::chrono::system_clock::now(); - return currentTime.time_since_epoch().count() / 1000000; -} -typedef struct { - uint32_t tc0; - uint32_t tc1; - uint32_t subframe; -} Timecode64_t; - -typedef struct { - uint8_t hour; - uint8_t minute; - uint8_t second; - uint8_t frame; - uint8_t subframe; -} XsyncTimecode_t; - -XsyncTimecode_t timecode64ToXsyncTimeCode(Timecode64_t tc64) { - uint8_t frameuints = tc64.tc0 & 0x0f; - uint8_t frame10s = (tc64.tc0 >> 8) & 0x3; - uint8_t seconduints = (tc64.tc0 >> 16) & 0x0f; - uint8_t second10s = (tc64.tc0 >> 24) & 0x07; - - uint8_t minuteuints = tc64.tc1 & 0x0f; - uint8_t minute10s = (tc64.tc1 >> 8) & 0x07; - uint8_t houruints = (tc64.tc1 >> 16) & 0x0f; - uint8_t hour10s = (tc64.tc1 >> 24) & 0x03; - - XsyncTimecode_t timecode; - timecode.hour = hour10s * 10 + houruints; - timecode.minute = minute10s * 10 + minuteuints; - timecode.second = second10s * 10 + seconduints; - timecode.frame = frame10s * 10 + frameuints; - timecode.subframe = tc64.subframe; - return timecode; -} -int main(int argc, char* argv[]) { - // 初始化网络环境 - log_file_init(); - WSADATA wsa; - if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { - LOGD("WSAStartup failed"); - return -1; - } - - // 建立一个UDP的socket - SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (sock == SOCKET_ERROR) { - LOGD("create socket failed"); - return -1; - } - - // 绑定地址信息 - sockaddr_in serverAddr; - serverAddr.sin_family = AF_INET; - serverAddr.sin_port = htons(PORT); - serverAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); - - int ret = ::bind(sock, (sockaddr*)&serverAddr, sizeof(sockaddr)); - if (ret != 0) { - LOGD("bind failed"); - return -1; - } - - char buf[512]; - - while (TRUE) { - memset(buf, 0, 512); - sockaddr_in clientAddr; - memset(&clientAddr, 0, sizeof(sockaddr_in)); - int clientAddrLen = sizeof(sockaddr); - // 接收客户端发来的数据 - int ret = recvfrom(sock, buf, 512, 0, (sockaddr*)&clientAddr, &clientAddrLen); - - iflytop_xsync_event_report_packet_t* packet = (iflytop_xsync_event_report_packet_t*)buf; - - uint32_t stm32_ticket = packet->data[3]; - uint32_t stm32_send_delay = packet->data[4]; - - Timecode64_t tc64; - tc64.tc0 = packet->data[0]; - tc64.tc1 = packet->data[1]; - tc64.subframe = packet->data[2]; - XsyncTimecode_t timecode = timecode64ToXsyncTimeCode(tc64); - - static int64_t lastrxticket; - static uint32_t laststm32ticket = 0; - static int32_t et1 = 0; - static int32_t et2 = 0; - static int32_t et3 = 0; - static int32_t et4 = 0; - static Timecode64_t lasttimecode64; - static int32_t lastdifftime = 0; - static int32_t lastdiffstm32; - int64_t nowticket = gettime(); - int64_t difftime = nowticket - lastrxticket; - int64_t diffstm32 = stm32_ticket - laststm32ticket; - static int32_t totalPacketNum = 0; - totalPacketNum++; - - { - if (abs(difftime - lastdifftime) > 4) { - et1++; - LOGD("exception_1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - } - - if (abs(diffstm32 - lastdiffstm32) > 2) { - et2++; - LOGD("exception_2 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - } - - if (stm32_send_delay > 1) { - et3++; - LOGD("exception_3 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - } - - if (memcmp(&lasttimecode64, &tc64, sizeof(Timecode64_t)) == 0) { - et4++; - LOGD("exception_4 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - } - - LOGD("pcticket:%ld(%10d) tc:%2d:%2d:%2d:%2d:%2d stm32ticket:%10d(%10d) stm32_send_delay:%d et:(%d %d %d %d)/%d", nowticket, difftime, // - timecode.hour, timecode.minute, timecode.second, timecode.frame, timecode.subframe, // - stm32_ticket, diffstm32, stm32_send_delay, // - et1 / 2, et2, et3, et4,totalPacketNum); - } - - lasttimecode64 = tc64; - - // - lastrxticket = nowticket; - laststm32ticket = stm32_ticket; - lastdifftime = difftime; - lastdiffstm32 = diffstm32; - - // 发一个数据包返回给客户 - } - return 0; -} \ No newline at end of file diff --git a/xsync_timecode_recorder/build.bat b/xsync_timecode_recorder/build.bat new file mode 100644 index 0000000..5a4a5d6 --- /dev/null +++ b/xsync_timecode_recorder/build.bat @@ -0,0 +1,2 @@ +@REM g++ -static -static-libgcc -static-libstdc++ -I ../libxsync/include xsync_timecode_recorder.cpp -L "./" -lwsock32 -lstdc++ -llibxsync.dll +g++ -static-libgcc -static-libstdc++ -I ../libxsync/include xsync_timecode_recorder.cpp -L "./" -o xsync_timecode_recorder.exe -lxsync -lwsock32 -fpic \ No newline at end of file diff --git a/xsync_timecode_recorder/build2.bat b/xsync_timecode_recorder/build2.bat new file mode 100644 index 0000000..3ebd833 --- /dev/null +++ b/xsync_timecode_recorder/build2.bat @@ -0,0 +1 @@ +g++ -static -static-libgcc -static-libstdc++ -I ../libxsync/include ../libxsync/src/xsync_udp_factory_impl.cpp ../libxsync/src/xsync_utils.cpp ../libxsync/src/xsync_v2_sig_type.cpp ../libxsync/src/xsync_v2.cpp xsync_timecode_recorder.cpp -L "./" -o xsync_timecode_recorder.exe -lwsock32 \ No newline at end of file diff --git a/xsync_timecode_recorder/build_xsync.bat b/xsync_timecode_recorder/build_xsync.bat new file mode 100644 index 0000000..70cac21 --- /dev/null +++ b/xsync_timecode_recorder/build_xsync.bat @@ -0,0 +1,2 @@ +g++ -I ../libxsync/include ../libxsync/src/xsync_udp_factory_impl.cpp ../libxsync/src/xsync_utils.cpp ../libxsync/src/xsync_v2_sig_type.cpp ../libxsync/src/xsync_v2.cpp -o xsync.dll -lwsock32 -fpic -shared + \ No newline at end of file diff --git a/xsync_timecode_recorder/xsync_timecode_recorder.cpp b/xsync_timecode_recorder/xsync_timecode_recorder.cpp new file mode 100644 index 0000000..dcee785 --- /dev/null +++ b/xsync_timecode_recorder/xsync_timecode_recorder.cpp @@ -0,0 +1,220 @@ +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +// +#include "xsync_packet.hpp" +#include "xsync_udp_factory_impl.hpp" +#include "xsync_v2.hpp" + +// +#include + +#pragma comment(lib, "ws2_32.lib") +using namespace std; +using namespace xsync; + +static std::ofstream* logfile; + +/** + * @brief 获取 年月日时分秒:毫秒 + * + * @return string + */ +static const char* gettime_str() { + time_t t = time(0); + static char tmp[64]; + strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&t)); + struct tm* tt = localtime(&t); + // sprintf(tmp, "%s:%03d", tmp, GetTickCount() % 1000); + return tmp; +} + +static string getlogfilename() { + time_t t = time(0); + char tmp[64]; + strftime(tmp, sizeof(tmp), "%Y%m%d_%H%M%S.log", localtime(&t)); + return tmp; +} + +/** + * @brief 写日志到文件中 + */ +bool log_file_init() { + // 创建日志文件 + logfile = new ofstream(getlogfilename(), ios::app); + if (logfile == NULL) { + printf("创建日志文件失败\n"); + return false; + } + return true; +} + +void log(const char* fmt, ...) { + char buf[1024] = {0}; + va_list args; + va_start(args, fmt); + vsprintf(buf, fmt, args); + va_end(args); + cout << buf; +} + +void logd(const char* fmt, ...) { + char buf[1024] = {0}; + va_list args; + va_start(args, fmt); + vsprintf(buf, fmt, args); + va_end(args); + if (logfile) { + *logfile << buf; + logfile->flush(); + } + cout << buf; +} + +#define LOGD(fmt, ...) logd("[%s] INFO : " fmt "\n", gettime_str(), ##__VA_ARGS__); + +int64_t gettime() { + auto currentTime = std::chrono::system_clock::now(); + return currentTime.time_since_epoch().count() / 1000000; +} +XsyncTimecode_t nowtc; + +int main(int argc, char* argv[]) { + // 初始化网络环境 + log_file_init(); + WSADATA wsa; + if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) { + LOGD("WSAStartup failed"); + return -1; + } + + XSyncUdpFactoryImpl::Ins()->initialize(); + + // m_xsync.reset(new Xsync()); + + XsyncInit(XSyncUdpFactoryImpl::Ins()); + xsync_config_t config = {0}; + config.listenCameraSyncPacket = true; + xs_error_code_t ecode = XsyncIns()->initialize(&config); + if (ecode != 0) { + LOGD("initialize failed"); + return -1; + } + + bool online = false; + XsyncIns()->registerOnConnectStateChangeCallback([&online](bool state, string targetIp) { + LOGD("targetIp:%s,state:%d", targetIp.c_str(), state); + online = state; + }); + + while (!online) { + Sleep(1); + } + version_t sdk_version; + version_t fpga_version; + version_t stm32_version; + XsyncIns()->readSDKVersion(sdk_version); + XsyncIns()->readARMSoftwareVersion(stm32_version); + XsyncIns()->readFPGASoftwareVersion(fpga_version); + + LOGD("sdk_version:%d.%d.%d", sdk_version.main, sdk_version.sub, sdk_version.fix); + LOGD("fpga_version:%d.%d.%d", fpga_version.main, fpga_version.sub, fpga_version.fix); + LOGD("stm32_version:%d.%d.%d", stm32_version.main, stm32_version.sub, stm32_version.fix); + + XsyncIns()->registerOnTimecodeMsgCallback([](XsyncTimecode_t* timecode_msg) { // + nowtc = *timecode_msg; + }); + + XsyncIns()->registerOnTimecodeRawMsgCallback([](XsyncNetAdd& from, uint8_t* rawpacket, size_t length) { + // + iflytop_xsync_event_report_packet_t* packet = (iflytop_xsync_event_report_packet_t*)rawpacket; + + uint32_t stm32_ticket = packet->data[3]; + uint32_t stm32_send_delay = packet->data[4]; + + Timecode64_t tc64; + tc64.tc0 = packet->data[0]; + tc64.tc1 = packet->data[1]; + tc64.subframe = packet->data[2]; + XsyncTimecode_t timecode = nowtc; + + static int64_t lastrxticket; + static uint32_t laststm32ticket = 0; + static int32_t et1 = 0; + static int32_t et2 = 0; + static int32_t et3 = 0; + static int32_t et4 = 0; + static Timecode64_t lasttimecode64; + static int32_t lastdifftime = 0; + static int32_t lastdiffstm32; + int64_t nowticket = gettime(); + int64_t difftime = nowticket - lastrxticket; + int64_t diffstm32 = stm32_ticket - laststm32ticket; + static int32_t totalPacketNum = 0; + totalPacketNum++; + + { + if (abs(difftime - lastdifftime) > 4) { + et1++; + LOGD("e1:abs(difftime - lastdifftime) > 4"); + } + + if (abs(diffstm32 - lastdiffstm32) > 2) { + et2++; + LOGD("e2:abs(diffstm32 - lastdiffstm32) > 2"); + } + + if (stm32_send_delay > 1) { + et3++; + LOGD("e3:stm32_send_delay>1"); + } + + if (memcmp(&lasttimecode64, &tc64, sizeof(Timecode64_t)) == 0) { + et4++; + LOGD("e4:memcmp(&lasttimecode64, &tc64, sizeof(Timecode64_t)) == 0"); + } + + LOGD("pcticket:%ld(%10d) tc:%2d:%2d:%2d:%2d:%2d stm32ticket:%10d(%10d) stm32_send_delay:%d et:(%d %d %d %d)/%d", nowticket, difftime, // + timecode.hour, timecode.minute, timecode.second, timecode.frame, timecode.subframe, // + stm32_ticket, diffstm32, stm32_send_delay, // + et1 / 2, et2, et3, et4, totalPacketNum); + } + + lasttimecode64 = tc64; + + // + lastrxticket = nowticket; + laststm32ticket = stm32_ticket; + lastdifftime = difftime; + lastdiffstm32 = diffstm32; + }); + + // XsyncIns()->registerOnCameraSyncMsgCallback([](xysnc_camera_sync_data_t* camera_sync_msg) { // + // xysnc_camera_sync_data_t camera_sync_data = *camera_sync_msg; + // LOGD("OnCameraSyncMsgCallback:%d", camera_sync_msg->frameIndex); + // }); + + // XsyncIns()->registerOnRecordSigChangeMsgCallback([](uint32_t recordSig, XsyncTimecode_t* timecode_msg) { // + // LOGD("OnRecordSigChangeMsgCallback :%d", recordSig); + // }); + + while (true) { + Sleep(1000); + } + return 0; +}