application: minor, redability

This commit is contained in:
King Kévin 2019-12-30 17:16:38 +01:00
parent 07546dc000
commit c61cdd2d82
1 changed files with 47 additions and 41 deletions

View File

@ -584,24 +584,26 @@ static void command_find(void* argument)
printf("= finding cable =\n"); printf("= finding cable =\n");
struct cable_t cable; // to store the cable finding results struct cable_t* cable = calloc(1, sizeof(struct cable_t)); // structure to store cable information
memset(&cable, 0, sizeof(struct cable_t)); // initialize cable structure if (NULL == cable) { // not enough memory for allocation
cable_clear(&cable); // initialize rest of cable structure return;
}
cable_clear(cable); // initialize rest of cable structure
// find if cable is connected // find if cable is connected
cable_detect(&cable); cable_detect(cable);
if (NULL == cable.connections) { if (NULL == cable->connections) {
if (cable.connections_nb) { if (cable->connections_nb) {
printf("no memory available\n"); printf("no memory available\n");
} }
goto end; goto end;
} }
// find connected connectors // find connected connectors
cable_connectors(&cable); cable_connectors(cable);
printf("%u connectors connected:\n", cable.connectors_nb); printf("%u connectors connected:\n", cable->connectors_nb);
for (uint8_t i = 0; i < LENGTH(cable.connectors) && i < LENGTH(usb_connectors); i++) { for (uint8_t i = 0; i < LENGTH(cable->connectors) && i < LENGTH(usb_connectors); i++) {
if (cable.connectors[i]) { if (cable->connectors[i]) {
printf("- %s", usb_connectors[i]->name); printf("- %s", usb_connectors[i]->name);
if (usb_connectors[i]->variant) { if (usb_connectors[i]->variant) {
printf(" (%s)", usb_connectors[i]->variant); printf(" (%s)", usb_connectors[i]->variant);
@ -611,54 +613,54 @@ static void command_find(void* argument)
} }
// find cable with matching connector set // find cable with matching connector set
cable_cables(&cable); cable_cables(cable);
if (0 == cable.cables_nb) { if (0 == cable->cables_nb) {
printf("found no cable with matching connector set\n"); printf("found no cable with matching connector set\n");
goto end; goto end;
} }
printf("found %u cable(s) with matching connectors:\n", cable.cables_nb); printf("found %u cable(s) with matching connectors:\n", cable->cables_nb);
for (uint8_t cable_i = 0; cable_i < LENGTH(cable.cables) && cable_i < LENGTH(usb_cables); cable_i++) { for (uint8_t cable_i = 0; cable_i < LENGTH(cable->cables) && cable_i < LENGTH(usb_cables); cable_i++) {
if (!cable.cables[cable_i]) { // skip if the cable connectors do not match if (!cable->cables[cable_i]) { // skip if the cable connectors do not match
continue; continue;
} }
printf("- %02u %s\n", cable_i, usb_cables[cable_i].name); printf("- %02u %s\n", cable_i, usb_cables[cable_i].name);
} }
// check if there is a load // check if there is a load
cable_load(&cable); cable_load(cable);
// calculate score for cables // calculate score for cables
cable_issues_nb(&cable); cable_issues_nb(cable);
printf("cable connection issue(s):\n"); printf("cable connection issue(s):\n");
for (uint8_t cable_i = 0; cable_i < LENGTH(usb_cables) && cable_i < LENGTH(cable.cables) && cable_i < LENGTH(cable.unconnected_nb) && cable_i < LENGTH(cable.unspecified_nb); cable_i++) { for (uint8_t cable_i = 0; cable_i < LENGTH(usb_cables) && cable_i < LENGTH(cable->cables) && cable_i < LENGTH(cable->unconnected_nb) && cable_i < LENGTH(cable->unspecified_nb); cable_i++) {
if (!cable.cables[cable_i]) { // skip if the cable connectors do not match if (!cable->cables[cable_i]) { // skip if the cable connectors do not match
continue; continue;
} }
uint16_t issues = cable.unconnected_nb[cable_i] + cable.unspecified_nb[cable_i]; uint16_t issues = cable->unconnected_nb[cable_i] + cable->unspecified_nb[cable_i];
printf("- %02u %s: %u (unconnected=%u/%u, undefined=%u)\n", cable_i, usb_cables[cable_i].name, issues, cable.unconnected_nb[cable_i], usb_cables[cable_i].pin_pairs_nb, cable.unspecified_nb[cable_i]); printf("- %02u %s: %u (unconnected=%u/%u, undefined=%u)\n", cable_i, usb_cables[cable_i].name, issues, cable->unconnected_nb[cable_i], usb_cables[cable_i].pin_pairs_nb, cable->unspecified_nb[cable_i]);
} }
// print connection details // print connection details
cable_issues(&cable); cable_issues(cable);
if (cable.cable_best < LENGTH(usb_cables) && cable.cable_best < LENGTH(cable.unconnected_nb) && cable.cable_best < LENGTH(cable.unconnected_nb) && cable.cables[cable.cable_best]) { if (cable->cable_best < LENGTH(usb_cables) && cable->cable_best < LENGTH(cable->unconnected_nb) && cable->cable_best < LENGTH(cable->unconnected_nb) && cable->cables[cable->cable_best]) {
// there is a matching cable // there is a matching cable
} else { } else {
printf("no matching cable found\n"); printf("no matching cable found\n");
goto end; goto end;
} }
const struct usb_cable_t* usb_cable = &usb_cables[cable.cable_best]; const struct usb_cable_t* usb_cable = &usb_cables[cable->cable_best];
const uint16_t issues = cable.unconnected_nb[cable.cable_best] + cable.unspecified_nb[cable.cable_best]; const uint16_t issues = cable->unconnected_nb[cable->cable_best] + cable->unspecified_nb[cable->cable_best];
if (0 == issues) { if (0 == issues) {
printf("perfect matching cable: %s\n", usb_cable->name); printf("perfect matching cable: %s\n", usb_cable->name);
goto end; goto end;
} }
printf("closest matching cable: %s\n", usb_cable->name); printf("closest matching cable: %s\n", usb_cable->name);
printf("%u connection issue(s) (%u unconnected, %u unspecified)\n", issues, cable.unconnected_nb[cable.cable_best], cable.unspecified_nb[cable.cable_best]); printf("%u connection issue(s) (%u unconnected, %u unspecified)\n", issues, cable->unconnected_nb[cable->cable_best], cable->unspecified_nb[cable->cable_best]);
if (cable.unconnected_nb[cable.cable_best] > 0) { if (cable->unconnected_nb[cable->cable_best] > 0) {
printf("unconnected pins:\n"); printf("unconnected pins:\n");
for (uint16_t i = 0; i < cable.unconnected_nb[cable.cable_best]; i++) { for (uint16_t i = 0; i < cable->unconnected_nb[cable->cable_best]; i++) {
const struct usb_connector_t* connector_from = usb_cables_get_connector(cable.unconnected[i][0]); const struct usb_connector_t* connector_from = usb_cables_get_connector(cable->unconnected[i][0]);
const struct usb_connector_t* connector_to = usb_cables_get_connector(cable.unconnected[i][1]); const struct usb_connector_t* connector_to = usb_cables_get_connector(cable->unconnected[i][1]);
if (NULL == connector_from || NULL == connector_to) { if (NULL == connector_from || NULL == connector_to) {
continue; continue;
} }
@ -666,18 +668,18 @@ static void command_find(void* argument)
if (connector_from->variant) { if (connector_from->variant) {
printf("(%s) ", connector_from->variant); printf("(%s) ", connector_from->variant);
} }
printf("%s to %s ", usb_pins[cable.unconnected[i][0]].name, connector_to->name); printf("%s to %s ", usb_pins[cable->unconnected[i][0]].name, connector_to->name);
if (connector_to->variant) { if (connector_to->variant) {
printf("(%s) ", connector_from->variant); printf("(%s) ", connector_from->variant);
} }
printf("%s\n", usb_pins[cable.unconnected[i][1]].name); printf("%s\n", usb_pins[cable->unconnected[i][1]].name);
} }
} }
if (cable.unspecified_nb[cable.cable_best] > 0) { if (cable->unspecified_nb[cable->cable_best] > 0) {
printf("unspecified pins connections:\n"); printf("unspecified pins connections:\n");
for (uint16_t i = 0; i < cable.unspecified_nb[cable.cable_best]; i++) { for (uint16_t i = 0; i < cable->unspecified_nb[cable->cable_best]; i++) {
const struct usb_connector_t* connector_from = usb_cables_get_connector(cable.unspecified[i][0]); const struct usb_connector_t* connector_from = usb_cables_get_connector(cable->unspecified[i][0]);
const struct usb_connector_t* connector_to = usb_cables_get_connector(cable.unspecified[i][1]); const struct usb_connector_t* connector_to = usb_cables_get_connector(cable->unspecified[i][1]);
if (NULL == connector_from || NULL == connector_to) { if (NULL == connector_from || NULL == connector_to) {
continue; continue;
} }
@ -685,18 +687,22 @@ static void command_find(void* argument)
if (connector_from->variant) { if (connector_from->variant) {
printf("(%s) ", connector_from->variant); printf("(%s) ", connector_from->variant);
} }
printf("%s to %s ", usb_pins[cable.unspecified[i][0]].name, connector_to->name); printf("%s to %s ", usb_pins[cable->unspecified[i][0]].name, connector_to->name);
if (connector_to->variant) { if (connector_to->variant) {
printf("(%s) ", connector_from->variant); printf("(%s) ", connector_to->variant);
} }
printf("%s\n", usb_pins[cable.unspecified[i][1]].name); printf("%s\n", usb_pins[cable->unspecified[i][1]].name);
} }
} }
printf("there is %s load in the cable\n", cable.load ? "a" : "no"); printf("there is %s load in the cable\n", cable->load ? "a" : "no");
end: end:
usb_pins_float(); // put all pins back in safe floating state usb_pins_float(); // put all pins back in safe floating state
cable_clear(&cable); // free allocated memory if (cable) {
cable_clear(cable); // free allocated sub-memory
free(cable); // free allocated memory
cable = NULL;
}
} }
/** set or show pin value /** set or show pin value