Files

55 lines
1.9 KiB
C++

//=======================================================================
// udp_feeder.cpp: 集成测试用 — 向 pPowerManger 发送 CCU 状态反馈 UDP 消息。
// 直接使用真实 C++ 结构体构造消息(字节级与协议一致,无 Python 打包漂移)。
// 用法: udpFeeder [host] [port] [count]
//=======================================================================
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <thread>
#include <chrono>
#include "udpcomm/udpComm.h"
#include "udpcomm/ccuUdpMsg.h"
int main(int argc, char* argv[])
{
const char* host = (argc > 1) ? argv[1] : "127.0.0.1";
int port = (argc > 2) ? atoi(argv[2]) : 5001;
int count = (argc > 3) ? atoi(argv[3]) : 6;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) { perror("socket"); return 1; }
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(static_cast<unsigned short>(port));
if (inet_pton(AF_INET, host, &addr.sin_addr) != 1) {
perror("inet_pton");
close(fd);
return 1;
}
udpComm u;
for (int i = 0; i < count; i++) {
msg_CcuStateFbMsg m;
memset(&m, 0, sizeof(m));
m.header.start1 = CCU_UDPMSG_START1;
m.header.start2 = CCU_UDPMSG_START2;
m.header.id = CCU_STATE_FB_ID;
m.data.ins_soc = 42;
m.data.dyn_soc = 66;
m.data.fc_status = 0x04;
m.data.heartbeat = i + 1;
m.checkCode = u.calculateChecksum(reinterpret_cast<unsigned char*>(&m), sizeof(m));
ssize_t n = sendto(fd, &m, sizeof(m), 0, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
if (n < 0) { perror("sendto"); close(fd); return 1; }
std::this_thread::sleep_for(std::chrono::milliseconds(300));
}
close(fd);
printf("sent %d CCU state msgs to %s:%d\n", count, host, port);
return 0;
}