summaryrefslogtreecommitdiffstats
path: root/src/hardware/dmidecode.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/hardware/dmidecode.c')
-rw-r--r--src/hardware/dmidecode.c165
1 files changed, 165 insertions, 0 deletions
diff --git a/src/hardware/dmidecode.c b/src/hardware/dmidecode.c
index 4023cf3..cbee376 100644
--- a/src/hardware/dmidecode.c
+++ b/src/hardware/dmidecode.c
@@ -1484,3 +1484,168 @@ void dmi_free_baseboard(DmiBaseboard *baseboard)
free(baseboard->version);
baseboard->version = NULL;
}
+
+
+/******************************************************************************
+ * DmiPorts
+ */
+
+/*
+ * Initialize DmiPort attributes.
+ * @param port
+ */
+void init_dmiports_struct(DmiPort *port)
+{
+ port->name = NULL;
+ port->type = NULL;
+ port->port_type = NULL;
+}
+
+/*
+ * Check attributes of port structure and fill in defaults if needed.
+ * @param port
+ * @return 0 if success, negative value otherwise
+ */
+short check_dmiport_attributes(DmiPort *port)
+{
+ short ret = -1;
+
+ if (!port->name) {
+ if (!(port->name = strdup("Port"))) {
+ ret = -2;
+ goto done;
+ }
+ }
+ if (!port->type) {
+ if (!(port->type = strdup("Unknown"))) {
+ ret = -3;
+ goto done;
+ }
+ }
+ if (!port->port_type) {
+ if (!(port->port_type = strdup("Unknown"))) {
+ ret = -4;
+ goto done;
+ }
+ }
+
+ ret = 0;
+
+done:
+ if (ret != 0) {
+ warn("Failed to allocate memory.");
+ }
+
+ return ret;
+}
+
+short dmi_get_ports(DmiPort **ports, unsigned *ports_nb)
+{
+ short ret = -1;
+ int curr_port = -1;
+ unsigned i, buffer_size = 0;
+ char **buffer = NULL, *buf;
+
+ *ports_nb = 0;
+
+ /* get dmidecode output */
+ if (run_command("dmidecode -t 8", &buffer, &buffer_size) != 0) {
+ ret = -2;
+ goto done;
+ }
+
+ /* count ports */
+ for (i = 0; i < buffer_size; i++) {
+ if (strncmp(buffer[i], "Handle 0x", 9) == 0) {
+ (*ports_nb)++;
+ }
+ }
+
+ /* if no port was found */
+ if (*ports_nb < 1) {
+ warn("Dmidecode didn't recognize any port.");
+ ret = -3;
+ goto done;
+ }
+
+ /* allocate memory for ports */
+ *ports = (DmiPort *)calloc(*ports_nb, sizeof(DmiPort));
+ if (!(*ports)) {
+ warn("Failed to allocate memory.");
+ ret = -4;
+ goto done;
+ }
+
+ /* parse information about ports */
+ for (i = 0; i < buffer_size; i++) {
+ if (strncmp(buffer[i], "Handle 0x", 9) == 0) {
+ curr_port++;
+ init_dmiports_struct(&(*ports)[curr_port]);
+ continue;
+ }
+ /* ignore first useless lines */
+ if (curr_port < 0) {
+ continue;
+ }
+ /* Name */
+ buf = copy_string_part_after_delim(buffer[i], "External Reference Designator: ");
+ if (buf) {
+ (*ports)[curr_port].name = buf;
+ buf = NULL;
+ continue;
+ }
+ /* Type */
+ buf = copy_string_part_after_delim(buffer[i], "External Connector Type: ");
+ if (buf) {
+ (*ports)[curr_port].type = buf;
+ buf = NULL;
+ continue;
+ }
+ /* Port Type */
+ buf = copy_string_part_after_delim(buffer[i], "Port Type: ");
+ if (buf) {
+ (*ports)[curr_port].port_type = buf;
+ buf = NULL;
+ continue;
+ }
+ }
+
+ /* fill in default attributes if needed */
+ for (i = 0; i < *ports_nb; i++) {
+ if (check_dmiport_attributes(&(*ports)[i]) != 0) {
+ ret = -5;
+ goto done;
+ }
+ }
+
+ ret = 0;
+
+done:
+ free_2d_buffer(&buffer, &buffer_size);
+
+ if (ret != 0) {
+ dmi_free_ports(ports, ports_nb);
+ }
+
+ return ret;
+}
+
+void dmi_free_ports(DmiPort **ports, unsigned *ports_nb)
+{
+ unsigned i;
+
+ if (*ports_nb > 0) {
+ for (i = 0; i < *ports_nb; i++) {
+ free((*ports)[i].name);
+ (*ports)[i].name = NULL;
+ free((*ports)[i].type);
+ (*ports)[i].type = NULL;
+ free((*ports)[i].port_type);
+ (*ports)[i].port_type = NULL;
+ }
+ free(*ports);
+ }
+
+ *ports_nb = 0;
+ *ports = NULL;
+}