usb_cables: add function to test and return all connections

This commit is contained in:
King Kévin 2019-12-13 14:46:02 +01:00
parent fc6a7a6d3d
commit 151b7f1c9c
2 changed files with 85 additions and 0 deletions

View File

@ -20,6 +20,7 @@
/* standard libraries */
#include <stdint.h> // standard integer types
#include <string.h> // string utilities
#include <stdlib.h> // memory utilities
/* STM32 (including CM3) libraries */
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
@ -4936,3 +4937,79 @@ bool usb_cables_check_cable(const struct usb_cable_t* cable, uint8_t* defined, u
}
return (0 == _undefined && cable->pin_pairs_nb == _defined);
}
uint8_t** usb_cables_check_connections(const struct usb_connector_t** connectors, uint8_t connectors_nb, uint16_t* connections_nb)
{
*connections_nb = 0; // number of connections between the connectors we found
uint8_t (*connections)[2] = NULL; // connections between the connectors we found (pairs of pin index)
if (NULL == connectors || 0 == connectors_nb) { // verify input arguments
return NULL;
}
usb_cables_pins_float(); // ensure we start in a safe state so no pin influences the connections check
for (uint8_t connector_from_i = 0; connector_from_i < connectors_nb; connector_from_i++) { // test from every connector
const struct usb_connector_t* connector_from = connectors[connector_from_i]; // get connector
if (NULL == connector_from) { // check connector
continue;
}
for (uint8_t pin_from_i = 0; pin_from_i < connector_from->pins_nb; pin_from_i++) { // test from every pin
uint8_t pin_from = connector_from->pins[pin_from_i]; // get pin
if (pin_from >= LENGTH(usb_pins)) { // check pin
continue;
}
const struct usb_pin_t* usb_pin_from = &usb_pins[pin_from]; // get from pin
for (uint8_t connector_to_i = connector_from_i; connector_to_i < connectors_nb; connector_to_i++) { // test to every connector (yet untested)
const struct usb_connector_t* connector_to = connectors[connector_to_i]; // get connector
if (NULL == connector_to) {
continue;
}
for (uint8_t pin_to_i = 0; pin_to_i < connector_to->pins_nb; pin_to_i++) { // test to every pin
uint8_t pin_to = connector_to->pins[pin_to_i]; // get pin
const struct usb_pin_t* usb_pin_to = &usb_pins[pin_to]; // get to pin
if (pin_to >= LENGTH(usb_pins)) { // check pin
continue;
}
if (connector_to_i == connector_from_i && pin_to_i <= pin_from_i) { // skip already tested internal connection
continue;
}
if (pin_to == pin_from) { // skip same pin
continue;
}
if (usb_pin_to->port == usb_pin_from->port && usb_pin_to->pin == usb_pin_from->pin) { // skip same pin
continue;
}
// don't check if power and ground pins are connected because loads lead to false positives
if ((USB_PIN_TYPE_GROUND == usb_pin_from->type || USB_PIN_TYPE_SHIELD == usb_pin_from->type) && USB_PIN_TYPE_POWER == usb_pin_to->type) {
continue;
}
if (USB_PIN_TYPE_POWER == usb_pin_from->type && (USB_PIN_TYPE_GROUND == usb_pin_to->type || USB_PIN_TYPE_SHIELD == usb_pin_to->type)) {
continue;
}
// test connection
uint8_t connection = usb_cables_check_pins(usb_pin_from, usb_pin_to);
if (connection >= 0x22) {
if (UINT16_MAX == (*connections_nb)) { // we already found the maximum of connections
return (uint8_t**)connections;
}
(*connections_nb)++; // increment number of connections found
uint8_t (*new_connections)[2] = realloc(connections, *connections_nb * sizeof(uint8_t[2])); // no integer overflow is possible because of the max number of connections
if (NULL == new_connections) { // allocation failed
if (connections) {
free(connections);
}
connections = NULL;
return NULL;
}
connections = new_connections;
connections[(*connections_nb) - 1][0] = pin_from;
connections[(*connections_nb) - 1][1] = pin_to;
}
} // pin_to
} // connector_to
} // pin_from
} // connector_from
return (uint8_t**)connections;
}

View File

@ -130,3 +130,11 @@ bool usb_cables_check_load(const struct usb_connector_t* connector);
* @note each pair is checked in both directions
*/
bool usb_cables_check_cable(const struct usb_cable_t* cable, uint8_t* defined, uint8_t* undefined, bool print);
/** check connections between connectors
* @param[in] connectors connectors to check
* @param[in] connectors_nb numbers of connectors
* @param[out] connections_nb number of connections found
* @return list of pin pairs connections (NULL if 0 connections have been found or no connections have been found but no memory is available)
* @the list of pin pairs connections is allocated memory which should be freed by the caller
*/
uint8_t** usb_cables_check_connections(const struct usb_connector_t** connectors, uint8_t connectors_nb, uint16_t* connections_nb);