port: Simplify management response logic.

The code uses a local variable for program flow control in a silly way.
This patch simplifies the logic by using the common switch/case/default
pattern instead.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
master
Richard Cochran 2018-01-30 05:46:10 +01:00
parent 0a726b4390
commit 2d25209993
1 changed files with 15 additions and 22 deletions

37
port.c
View File

@ -705,7 +705,6 @@ static const Octet profile_id_p2p[] = {0x00, 0x1B, 0x19, 0x00, 0x02, 0x00};
static int port_management_fill_response(struct port *target, static int port_management_fill_response(struct port *target,
struct ptp_message *rsp, int id) struct ptp_message *rsp, int id)
{ {
int datalen = 0, respond = 0;
struct mgmt_clock_description *cd; struct mgmt_clock_description *cd;
struct management_tlv_datum *mtd; struct management_tlv_datum *mtd;
struct clock_description *desc; struct clock_description *desc;
@ -713,8 +712,9 @@ static int port_management_fill_response(struct port *target,
struct management_tlv *tlv; struct management_tlv *tlv;
struct port_ds_np *pdsnp; struct port_ds_np *pdsnp;
struct portDS *pds; struct portDS *pds;
uint8_t *buf;
uint16_t u16; uint16_t u16;
uint8_t *buf;
int datalen;
tlv = (struct management_tlv *) rsp->management.suffix; tlv = (struct management_tlv *) rsp->management.suffix;
tlv->type = TLV_MANAGEMENT; tlv->type = TLV_MANAGEMENT;
@ -723,7 +723,6 @@ static int port_management_fill_response(struct port *target,
switch (id) { switch (id) {
case TLV_NULL_MANAGEMENT: case TLV_NULL_MANAGEMENT:
datalen = 0; datalen = 0;
respond = 1;
break; break;
case TLV_CLOCK_DESCRIPTION: case TLV_CLOCK_DESCRIPTION:
cd = &rsp->last_tlv.cd; cd = &rsp->last_tlv.cd;
@ -784,7 +783,6 @@ static int port_management_fill_response(struct port *target,
} }
buf += PROFILE_ID_LEN; buf += PROFILE_ID_LEN;
datalen = buf - tlv->data; datalen = buf - tlv->data;
respond = 1;
break; break;
case TLV_PORT_DATA_SET: case TLV_PORT_DATA_SET:
pds = (struct portDS *) tlv->data; pds = (struct portDS *) tlv->data;
@ -807,31 +805,26 @@ static int port_management_fill_response(struct port *target,
pds->logMinPdelayReqInterval = target->logMinPdelayReqInterval; pds->logMinPdelayReqInterval = target->logMinPdelayReqInterval;
pds->versionNumber = target->versionNumber; pds->versionNumber = target->versionNumber;
datalen = sizeof(*pds); datalen = sizeof(*pds);
respond = 1;
break; break;
case TLV_LOG_ANNOUNCE_INTERVAL: case TLV_LOG_ANNOUNCE_INTERVAL:
mtd = (struct management_tlv_datum *) tlv->data; mtd = (struct management_tlv_datum *) tlv->data;
mtd->val = target->logAnnounceInterval; mtd->val = target->logAnnounceInterval;
datalen = sizeof(*mtd); datalen = sizeof(*mtd);
respond = 1;
break; break;
case TLV_ANNOUNCE_RECEIPT_TIMEOUT: case TLV_ANNOUNCE_RECEIPT_TIMEOUT:
mtd = (struct management_tlv_datum *) tlv->data; mtd = (struct management_tlv_datum *) tlv->data;
mtd->val = target->announceReceiptTimeout; mtd->val = target->announceReceiptTimeout;
datalen = sizeof(*mtd); datalen = sizeof(*mtd);
respond = 1;
break; break;
case TLV_LOG_SYNC_INTERVAL: case TLV_LOG_SYNC_INTERVAL:
mtd = (struct management_tlv_datum *) tlv->data; mtd = (struct management_tlv_datum *) tlv->data;
mtd->val = target->logSyncInterval; mtd->val = target->logSyncInterval;
datalen = sizeof(*mtd); datalen = sizeof(*mtd);
respond = 1;
break; break;
case TLV_VERSION_NUMBER: case TLV_VERSION_NUMBER:
mtd = (struct management_tlv_datum *) tlv->data; mtd = (struct management_tlv_datum *) tlv->data;
mtd->val = target->versionNumber; mtd->val = target->versionNumber;
datalen = sizeof(*mtd); datalen = sizeof(*mtd);
respond = 1;
break; break;
case TLV_DELAY_MECHANISM: case TLV_DELAY_MECHANISM:
mtd = (struct management_tlv_datum *) tlv->data; mtd = (struct management_tlv_datum *) tlv->data;
@ -840,20 +833,17 @@ static int port_management_fill_response(struct port *target,
else else
mtd->val = DM_E2E; mtd->val = DM_E2E;
datalen = sizeof(*mtd); datalen = sizeof(*mtd);
respond = 1;
break; break;
case TLV_LOG_MIN_PDELAY_REQ_INTERVAL: case TLV_LOG_MIN_PDELAY_REQ_INTERVAL:
mtd = (struct management_tlv_datum *) tlv->data; mtd = (struct management_tlv_datum *) tlv->data;
mtd->val = target->logMinPdelayReqInterval; mtd->val = target->logMinPdelayReqInterval;
datalen = sizeof(*mtd); datalen = sizeof(*mtd);
respond = 1;
break; break;
case TLV_PORT_DATA_SET_NP: case TLV_PORT_DATA_SET_NP:
pdsnp = (struct port_ds_np *) tlv->data; pdsnp = (struct port_ds_np *) tlv->data;
pdsnp->neighborPropDelayThresh = target->neighborPropDelayThresh; pdsnp->neighborPropDelayThresh = target->neighborPropDelayThresh;
pdsnp->asCapable = target->asCapable; pdsnp->asCapable = target->asCapable;
datalen = sizeof(*pdsnp); datalen = sizeof(*pdsnp);
respond = 1;
break; break;
case TLV_PORT_PROPERTIES_NP: case TLV_PORT_PROPERTIES_NP:
ppn = (struct port_properties_np *)tlv->data; ppn = (struct port_properties_np *)tlv->data;
@ -865,19 +855,22 @@ static int port_management_fill_response(struct port *target,
ppn->timestamping = target->timestamping; ppn->timestamping = target->timestamping;
ptp_text_set(&ppn->interface, target->iface->ts_label); ptp_text_set(&ppn->interface, target->iface->ts_label);
datalen = sizeof(*ppn) + ppn->interface.length; datalen = sizeof(*ppn) + ppn->interface.length;
respond = 1;
break; break;
default:
/* The caller should *not* respond to this message. */
return 0;
} }
if (respond) {
if (datalen % 2) { if (datalen % 2) {
tlv->data[datalen] = 0; tlv->data[datalen] = 0;
datalen++; datalen++;
}
tlv->length = sizeof(tlv->id) + datalen;
rsp->header.messageLength += sizeof(*tlv) + datalen;
rsp->tlv_count = 1;
} }
return respond; tlv->length = sizeof(tlv->id) + datalen;
rsp->header.messageLength += sizeof(*tlv) + datalen;
rsp->tlv_count = 1;
/* The caller can respond to this message. */
return 1;
} }
static int port_management_get_response(struct port *target, static int port_management_get_response(struct port *target,