From f7c37c4aa9a72cd34bb36ccc0fec884e52726973 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Thu, 11 Jun 2020 21:10:45 +0200 Subject: [PATCH] Fix for un-initialised buffer content when sending empty packets. Updated comments --- include/lib/protocols/dtplug/defs.h | 8 +++++--- lib/protocols/dtplug/slave.c | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/lib/protocols/dtplug/defs.h b/include/lib/protocols/dtplug/defs.h index 0b93aee..f0c8c20 100644 --- a/include/lib/protocols/dtplug/defs.h +++ b/include/lib/protocols/dtplug/defs.h @@ -43,7 +43,9 @@ * * Sequence numbers, Quick data packets, data checksums and error codes * The sequence number is on the 5 least significant bits of the sequence number field. - * this gives 64 sequence numbers which is more than enough. + * this gives 31 sequence numbers which is more than enough. + * A sequence number of 0 means "Out of band" data (usually unrequested data from slave + * to master). * When the most significant bit (bit 7) of the sequence number field (seq_num) is set * in a request (master to slave), the slave MUST send an acknowledge packet or a reply * with the same sequence number. @@ -85,7 +87,7 @@ struct header { char start; /* Start of paquet indicator */ uint8_t type; /* Packet type, used to provide information on the data structure */ uint8_t checksum; /* Header checksum */ - uint8_t seq_num; /* Packet sequence number on bits 0:5, error indicator on bit 7 */ + uint8_t seq_num; /* Packet sequence number on bits 0:4, error indicator on bit 7, quick data on bit 6 */ union { struct data_information data; struct error_information err; @@ -108,7 +110,7 @@ struct header { /* The following struct defines a generic packet. * Specific packets should be defined in specific structures. */ -#define PACKET_DATA_SIZE 64 +#define PACKET_DATA_SIZE (64 - sizeof(struct header)) struct packet { struct header info; /* Packet header */ uint8_t data[PACKET_DATA_SIZE]; /* packet data */ diff --git a/lib/protocols/dtplug/slave.c b/lib/protocols/dtplug/slave.c index 859ee8e..48ea3c6 100644 --- a/lib/protocols/dtplug/slave.c +++ b/lib/protocols/dtplug/slave.c @@ -270,7 +270,7 @@ void dtplug_protocol_send_reply(struct dtplug_protocol_handle* handle, tx_info->seq_num = question->info.seq_num; len = sizeof(struct header); /* At least, send header on serial link */ - if (error) { + if (error != NO_ERROR) { tx_info->seq_num |= PACKET_IS_ERROR; tx_info->err.error_code = error; if (error == GOT_MANY_ERRORS) { @@ -301,6 +301,9 @@ void dtplug_protocol_send_reply(struct dtplug_protocol_handle* handle, /* Update length of data to send on serial link */ len += data_send_size; } + } else { + tx_info->data.size = 0; + tx_info->data.checksum = 0; } } @@ -338,7 +341,7 @@ void dtplug_protocol_send_reply(struct dtplug_protocol_handle* handle, if (data_sent < size) { /* Move data pointer */ data_src += data_send_size; - /* Update size to send. check against PACKET_DATA_SIZE is done at beginning of loop */ + /* Update size to send. Check against PACKET_DATA_SIZE is done at beginning of loop */ data_send_size = (size - data_sent); /* Set packet type to continued data packet for following packets */ type = PKT_TYPE_CONTINUED_DATA; -- 2.43.0