Unify communication traces using trace_buffer() or trace2_buffer()

This commit is contained in:
Stefan Rueger
2023-11-02 16:58:24 +00:00
parent 0fc5749c24
commit 124fcc8110
7 changed files with 81 additions and 281 deletions

View File

@@ -25,6 +25,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/time.h>
#include <time.h>
@@ -1617,3 +1618,23 @@ void report_progress(int completed, int total, const char *hdr) {
update_progress(percent, t - start_time, hdr, total < 0? -1: !!total);
}
}
// Output comms buffer
void trace_buffer(char *what, const unsigned char *buf, size_t buflen) {
pmsg_trace("%s", what);
while(buflen--) {
unsigned char c = *buf++;
msg_trace("%c [%02x]%s", isascii(c) && isprint(c)? c: '.', c, buflen? " ": "");
}
msg_trace("\n");
}
void trace2_buffer(char *what, const unsigned char *buf, size_t buflen) {
pmsg_trace2("%s", what);
while(buflen--) {
unsigned char c = *buf++;
msg_trace2("%c [%02x]%s", isascii(c) && isprint(c)? c: '.', c, buflen? " ": "");
}
msg_trace2("\n");
}

View File

@@ -620,27 +620,9 @@ int jtagmkII_recv(const PROGRAMMER *pgm, unsigned char **msg) {
*/
memmove(*msg, *msg + 8, rv);
if (verbose == 4)
{
int i = rv;
unsigned char *p = *msg;
pmsg_trace("recv: ");
if(verbose > 3)
trace_buffer("jtagmkII_recv: ", *msg, rv);
while (i) {
unsigned char c = *p;
if (isprint(c)) {
msg_trace("%c ", c);
}
else {
msg_trace(". ");
}
msg_trace("[%02x] ", c);
p++;
i--;
}
msg_trace("\n");
}
return rv;
}
if (r_seqno == 0xffff) {
@@ -1010,8 +992,7 @@ static void jtagmkII_set_xmega_params(const PROGRAMMER *pgm, const AVRPART *p) {
}
}
pmsg_notice2("jtagmkII_set_xmega_params(): "
"Sending set Xmega params command: ");
pmsg_notice2("%s() sending set Xmega params command: ", __func__);
jtagmkII_send(pgm, (unsigned char *)&sendbuf, sizeof sendbuf);
status = jtagmkII_recv(pgm, &resp);

View File

@@ -1145,6 +1145,9 @@ int avr_unlock(const PROGRAMMER *pgm, const AVRPART *p);
void report_progress(int completed, int total, const char *hdr);
void trace_buffer(char *what, const unsigned char *buf, size_t buflen);
void trace2_buffer(char *what, const unsigned char *buf, size_t buflen);
int avr_has_paged_access(const PROGRAMMER *pgm, const AVRMEM *m);
int avr_read_page_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *mem, int addr, unsigned char *buf);

View File

@@ -436,42 +436,19 @@ static void ser_rawclose(union filedescriptor *fd) {
close(fd->ifd);
}
static int ser_send(const union filedescriptor *fd, const unsigned char * buf, size_t buflen) {
static int ser_send(const union filedescriptor *fd, const unsigned char *buf, size_t len) {
int rc;
const unsigned char * p = buf;
size_t len = buflen;
if (!len)
return 0;
if(verbose > 3)
trace_buffer("ser_send: ", buf, len);
if (verbose > 3)
{
pmsg_trace("send: ");
while (buflen) {
unsigned char c = *buf;
if (isprint(c)) {
msg_trace("%c ", c);
}
else {
msg_trace(". ");
}
msg_trace("[%02x] ", c);
buf++;
buflen--;
}
msg_trace("\n");
}
while (len) {
rc = write(fd->ifd, p, (len > 1024) ? 1024 : len);
while(len) {
rc = write(fd->ifd, buf, len > 1024? 1024: len);
if (rc < 0) {
pmsg_ext_error("unable to write: %s\n", strerror(errno));
return -1;
}
p += rc;
buf += rc;
len -= rc;
}
@@ -479,12 +456,12 @@ static int ser_send(const union filedescriptor *fd, const unsigned char * buf, s
}
static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t buflen) {
static int ser_recv(const union filedescriptor *fd, unsigned char *buf, size_t buflen) {
struct timeval timeout, to2;
fd_set rfds;
int nfds;
int rc;
unsigned char * p = buf;
unsigned char *p = buf;
size_t len = 0;
timeout.tv_sec = serial_recv_timeout / 1000L;
@@ -512,7 +489,7 @@ static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t
}
}
rc = read(fd->ifd, p, (buflen - len > 1024) ? 1024 : buflen - len);
rc = read(fd->ifd, p, buflen - len > 1024? 1024: buflen - len);
if (rc < 0) {
pmsg_ext_error("unable to read: %s\n", strerror(errno));
return -1;
@@ -521,27 +498,8 @@ static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t
len += rc;
}
p = buf;
if (verbose > 3)
{
pmsg_trace("recv: ");
while (len) {
unsigned char c = *p;
if (isprint(c)) {
msg_trace("%c ", c);
}
else {
msg_trace(". ");
}
msg_trace("[%02x] ", c);
p++;
len--;
}
msg_trace("\n");
}
if(verbose > 3)
trace_buffer("ser_recv: ", buf, len);
return 0;
}

View File

@@ -256,12 +256,7 @@ static int ser_open(const char *port, union pinfo pinfo, union filedescriptor *f
if (str_casestarts(port, "com")) {
// prepend "\\\\.\\" to name, required for port # >= 10
newname = malloc(strlen("\\\\.\\") + strlen(port) + 1);
if (newname == 0) {
pmsg_error("out of memory\n");
exit(1);
}
newname = cfg_malloc(__func__, strlen("\\\\.\\") + strlen(port) + 1);
strcpy(newname, "\\\\.\\");
strcat(newname, port);
@@ -346,42 +341,23 @@ static int ser_set_dtr_rts(const union filedescriptor *fd, int is_on) {
}
}
static int net_send(const union filedescriptor *fd, const unsigned char * buf, size_t buflen) {
static int net_send(const union filedescriptor *fd, const unsigned char *buf, size_t len) {
LPVOID lpMsgBuf;
int rc;
const unsigned char *p = buf;
size_t len = buflen;
if (fd->ifd < 0) {
pmsg_notice("net_send(): connection not open\n");
exit(1);
return -1;
}
if (!len) {
if (!len)
return 0;
}
if (verbose > 3) {
pmsg_trace("send: ");
while (buflen) {
unsigned char c = *buf;
if (isprint(c)) {
msg_trace("%c ", c);
} else {
msg_trace(". ");
}
msg_trace("[%02x] ", c);
buf++;
buflen--;
}
msg_trace("\n");
}
if (verbose > 3)
trace_buffer("net_send: ", buf, len);
while (len) {
rc = send(fd->ifd, (const char *) p, (len > 1024)? 1024: len, 0);
rc = send(fd->ifd, (const char *) buf, len > 1024? 1024: len, 0);
if (rc < 0) {
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
@@ -395,9 +371,9 @@ static int net_send(const union filedescriptor *fd, const unsigned char * buf, s
NULL);
pmsg_error("unable to send: %s\n", (char *) lpMsgBuf);
LocalFree(lpMsgBuf);
exit(1);
return -1;
}
p += rc;
buf += rc;
len -= rc;
}
@@ -405,15 +381,12 @@ static int net_send(const union filedescriptor *fd, const unsigned char * buf, s
}
static int ser_send(const union filedescriptor *fd, const unsigned char * buf, size_t buflen) {
if (serial_over_ethernet) {
return net_send(fd, buf, buflen);
}
static int ser_send(const union filedescriptor *fd, const unsigned char *buf, size_t len) {
if (serial_over_ethernet)
return net_send(fd, buf, len);
size_t len = buflen;
unsigned char c='\0';
DWORD written;
const unsigned char * b = buf;
HANDLE hComPort=(HANDLE)fd->pfd;
@@ -423,35 +396,19 @@ static int ser_send(const union filedescriptor *fd, const unsigned char * buf, s
}
if (!len)
return 0;
return 0;
if (verbose > 3)
{
pmsg_trace("send: ");
while (len) {
c = *b;
if (isprint(c)) {
msg_trace("%c ", c);
}
else {
msg_trace(". ");
}
msg_trace("[%02x] ", c);
b++;
len--;
}
msg_trace("\n");
}
trace_buffer("ser_send: ", buf, len);
serial_w32SetTimeOut(hComPort,500);
if (!WriteFile (hComPort, buf, buflen, &written, NULL)) {
if (!WriteFile (hComPort, buf, len, &written, NULL)) {
pmsg_error("unable to write: %s\n", "sorry no info avail"); // TODO
return -1;
}
if (written != buflen) {
if (written != len) {
pmsg_error("size/send mismatch\n");
return -1;
}
@@ -460,7 +417,7 @@ static int ser_send(const union filedescriptor *fd, const unsigned char * buf, s
}
static int net_recv(const union filedescriptor *fd, unsigned char * buf, size_t buflen) {
static int net_recv(const union filedescriptor *fd, unsigned char *buf, size_t buflen) {
LPVOID lpMsgBuf;
struct timeval timeout, to2;
fd_set rfds;
@@ -471,7 +428,7 @@ static int net_recv(const union filedescriptor *fd, unsigned char * buf, size_t
if (fd->ifd < 0) {
pmsg_error("connection not open\n");
exit(1);
return -1;
}
timeout.tv_sec = serial_recv_timeout / 1000L;
@@ -506,7 +463,7 @@ reselect:
NULL);
pmsg_error("select(): %s\n", (char *) lpMsgBuf);
LocalFree(lpMsgBuf);
exit(1);
return -1;
}
}
@@ -524,42 +481,23 @@ reselect:
NULL);
pmsg_error("unable to read: %s\n", (char *) lpMsgBuf);
LocalFree(lpMsgBuf);
exit(1);
return -1;
}
p += rc;
len += rc;
}
p = buf;
if (verbose > 3) {
pmsg_trace("Recv: ");
while (len) {
unsigned char c = *p;
if (isprint(c)) {
msg_trace("%c ", c);
} else {
msg_trace(". ");
}
msg_trace("[%02x] ", c);
p++;
len--;
}
msg_trace("\n");
}
if (verbose > 3)
trace_buffer("net_recv: ", buf, len);
return 0;
}
static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t buflen) {
if (serial_over_ethernet) {
static int ser_recv(const union filedescriptor *fd, unsigned char *buf, size_t buflen) {
if (serial_over_ethernet)
return net_recv(fd, buf, buflen);
}
unsigned char c;
unsigned char * p = buf;
DWORD read;
HANDLE hComPort=(HANDLE)fd->pfd;
@@ -582,9 +520,9 @@ static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL );
NULL);
pmsg_error("unable to read: %s\n", (char*) lpMsgBuf);
LocalFree( lpMsgBuf );
LocalFree(lpMsgBuf);
return -1;
}
@@ -594,28 +532,10 @@ static int ser_recv(const union filedescriptor *fd, unsigned char * buf, size_t
return -1;
}
p = buf;
if (verbose > 3)
{
pmsg_trace("recv: ");
trace_buffer("ser_recv: ", buf, read);
while (read) {
c = *p;
if (isprint(c)) {
msg_trace("%c ", c);
}
else {
msg_trace(". ");
}
msg_trace("[%02x] ", c);
p++;
read--;
}
msg_trace("\n");
}
return 0;
return 0;
}
static int net_drain(const union filedescriptor *fd, int display) {
@@ -628,7 +548,7 @@ static int net_drain(const union filedescriptor *fd, int display) {
if (fd->ifd < 0) {
pmsg_error("connection not open\n");
exit(1);
return -1;
}
if (display) {
@@ -667,7 +587,7 @@ static int net_drain(const union filedescriptor *fd, int display) {
NULL);
pmsg_error("select(): %s\n", (char *) lpMsgBuf);
LocalFree(lpMsgBuf);
exit(1);
return -1;
}
}
@@ -685,7 +605,7 @@ static int net_drain(const union filedescriptor *fd, int display) {
NULL);
pmsg_error("unable to read: %s\n", (char *) lpMsgBuf);
LocalFree(lpMsgBuf);
exit(1);
return -1;
}
if (display) {

View File

@@ -224,16 +224,12 @@ static void usbhid_close(union filedescriptor *fd) {
static int usbhid_send(const union filedescriptor *fd, const unsigned char *bp, size_t mlen) {
hid_device *udev = (hid_device *)fd->usb.handle;
int rv;
int i = mlen;
const unsigned char * p = bp;
unsigned char usbbuf[USBDEV_MAX_XFER_3 + 1];
int tx_size;
const int tx_size = mlen < USBDEV_MAX_XFER_3? mlen: USBDEV_MAX_XFER_3;
if (udev == NULL)
return -1;
tx_size = (mlen < USBDEV_MAX_XFER_3)? mlen: USBDEV_MAX_XFER_3;
usbbuf[0] = 0; /* No report ID used */
memcpy(usbbuf + 1, bp, tx_size);
rv = hid_write(udev, usbbuf, tx_size + 1);
@@ -244,22 +240,9 @@ static int usbhid_send(const union filedescriptor *fd, const unsigned char *bp,
if (rv != tx_size + 1)
pmsg_error("short write to USB: %d bytes out of %d written\n", rv, tx_size + 1);
if (verbose > 4) {
pmsg_trace2("sent: ");
if(verbose > 4)
trace2_buffer("usbhid_send: ", bp, tx_size);
while (i) {
unsigned char c = *p;
if (isprint(c))
msg_trace2("%c ", c);
else
msg_trace2(". ");
msg_trace2("[%02x] ", c);
p++;
i--;
}
msg_trace2("\n");
}
return 0;
}
@@ -277,22 +260,8 @@ static int usbhid_recv(const union filedescriptor *fd, unsigned char *buf, size_
else if ((size_t) i != nbytes)
pmsg_error("short read, read only %d out of %lu bytes\n", i, (unsigned long) nbytes);
if (verbose > 4) {
pmsg_trace2("recv: ");
while (i) {
unsigned char c = *p;
if (isprint(c))
msg_trace2("%c ", c);
else
msg_trace2(". ");
msg_trace2("[%02x] ", c);
p++;
i--;
}
msg_trace2("\n");
}
if (verbose > 4 && i > 0)
trace2_buffer("usbhid_recv: ", p, i);
return rv;
}

View File

@@ -349,25 +349,8 @@ static int usbdev_send(const union filedescriptor *fd, const unsigned char *bp,
mlen -= tx_size;
} while (mlen > 0);
if (verbose > 3)
{
pmsg_trace("sent: ");
while (i) {
unsigned char c = *p;
if (isprint(c)) {
msg_trace("%c ", c);
}
else {
msg_trace(". ");
}
msg_trace("[%02x] ", c);
p++;
i--;
}
msg_trace("\n");
}
if(verbose > 3)
trace_buffer("usbdev_send: ", p, i);
return 0;
}
@@ -424,25 +407,8 @@ static int usbdev_recv(const union filedescriptor *fd, unsigned char *buf, size_
i += amnt;
}
if (verbose > 4)
{
pmsg_trace2("recv: ");
while (i) {
unsigned char c = *p;
if (isprint(c)) {
msg_trace2("%c ", c);
}
else {
msg_trace2(". ");
}
msg_trace2("[%02x] ", c);
p++;
i--;
}
msg_trace2("\n");
}
if(verbose > 4)
trace2_buffer("usbdev_recv: ", p, i);
return 0;
}
@@ -460,8 +426,7 @@ static int usbdev_recv_frame(const union filedescriptor *fd, unsigned char *buf,
{
usb_dev_handle *udev = (usb_dev_handle *)fd->usb.handle;
int rv, n;
int i;
unsigned char * p = buf;
unsigned char *p = buf;
if (udev == NULL)
return -1;
@@ -532,26 +497,9 @@ static int usbdev_recv_frame(const union filedescriptor *fd, unsigned char *buf,
*/
printout:
if (verbose > 3)
{
i = n & USB_RECV_LENGTH_MASK;
pmsg_trace("recv: ");
if(verbose > 3)
pmsg_trace("recv: ", p, n & USB_RECV_LENGTH_MASK);
while (i) {
unsigned char c = *p;
if (isprint(c)) {
msg_trace("%c ", c);
}
else {
msg_trace(". ");
}
msg_trace("[%02x] ", c);
p++;
i--;
}
msg_trace("\n");
}
return n;
}