You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

78 lines
2.7 KiB

#include "etcp_debug.h"
#include "etcp.h"
#include "../lib/debug_config.h"
#include <stdio.h>
#include <string.h>
#include <stdint.h>
IP_STR ip_to_string(uint32_t ip) {
uint8_t* b = (uint8_t*)&ip;
IP_STR ip_str;
snprintf(ip_str.a, sizeof(ip_str.a), "%u.%u.%u.%u", b[3], b[2], b[1], b[0]);
return ip_str;
}
void etcp_dump_pkt_sections(struct ETCP_DGRAM* pkt, struct ETCP_LINK* link, int is_send) {
if (!pkt) return;
char buf[256];
int pos = 0;
const char* dir = is_send ? "SEND" : "RECV";
pos += snprintf(buf + pos, sizeof(buf) - pos, "ETCP PKT: %s ts=%u", dir, pkt->timestamp);
uint8_t* data = pkt->data;
uint16_t len = pkt->data_len;
uint32_t ack_last_delivered = 0;
int ack_count = 0;
uint16_t ch_ret_ts = 0;
uint16_t ch_recv_ts = 0;
uint32_t payload_seq = 0;
uint16_t payload_size = 0;
int has_channel_ts = 0;
int has_payload = 0;
uint16_t offset = 0;
while (offset < len) {
if (offset >= len) break;
uint8_t type = data[offset];
switch (type) {
case ETCP_SECTION_ACK: {
if (offset + 2 > len) { offset = len; break; }
ack_count = data[offset + 1];
if (offset + 6 > len) { offset = len; break; }
ack_last_delivered = data[offset + 2] | (data[offset + 3] << 8) |
(data[offset + 4] << 16) | (data[offset + 5] << 24);
pos += snprintf(buf + pos, sizeof(buf) - pos, " ack=%u/%d", ack_last_delivered, ack_count);
offset += 6 + ack_count * 8;
break;
}
case ETCP_SECTION_TIMESTAMP: {
if (offset + 5 > len) { offset = len; break; }
ch_ret_ts = data[offset + 1] | (data[offset + 2] << 8);
ch_recv_ts = data[offset + 3] | (data[offset + 4] << 8);
has_channel_ts = 1;
pos += snprintf(buf + pos, sizeof(buf) - pos, " ch_ts=%u/%u", ch_ret_ts, ch_recv_ts);
offset += 5;
break;
}
case ETCP_SECTION_PAYLOAD: {
if (offset + 5 > len) { offset = len; break; }
payload_seq = data[offset + 1] | (data[offset + 2] << 8) |
(data[offset + 3] << 16) | (data[offset + 4] << 24);
payload_size = len - offset - 5;
has_payload = 1;
pos += snprintf(buf + pos, sizeof(buf) - pos, " payload=%u(seq=%u)", payload_size, payload_seq);
offset = len;
break;
}
default:
offset = len;
break;
}
}
DEBUG_INFO(DEBUG_CATEGORY_DUMP, "%s", buf);
}