summaryrefslogtreecommitdiffstats
path: root/src/hardware
diff options
context:
space:
mode:
authorPeter Schiffer <pschiffe@redhat.com>2013-06-06 20:39:51 +0200
committerPeter Schiffer <pschiffe@redhat.com>2013-06-06 20:39:51 +0200
commitccb095d49a35b001439c254beebf5ba4e57e89e0 (patch)
treeeb2aed06949a2d2d8464d30a13f837234626fbd3 /src/hardware
parent91e01015001072515ba308e347b8997af36d8953 (diff)
downloadopenlmi-providers-ccb095d49a35b001439c254beebf5ba4e57e89e0.tar.gz
openlmi-providers-ccb095d49a35b001439c254beebf5ba4e57e89e0.tar.xz
openlmi-providers-ccb095d49a35b001439c254beebf5ba4e57e89e0.zip
Hardware: Added system slot provider
New providers: * LMI_SystemSlotProvider * LMI_SystemSlotContainerProvider
Diffstat (limited to 'src/hardware')
-rw-r--r--src/hardware/LMI_Hardware.h1
-rw-r--r--src/hardware/LMI_SystemSlotContainerProvider.c270
-rw-r--r--src/hardware/LMI_SystemSlotProvider.c325
-rw-r--r--src/hardware/dmidecode.c210
-rw-r--r--src/hardware/dmidecode.h26
5 files changed, 832 insertions, 0 deletions
diff --git a/src/hardware/LMI_Hardware.h b/src/hardware/LMI_Hardware.h
index bb070f3..81881f5 100644
--- a/src/hardware/LMI_Hardware.h
+++ b/src/hardware/LMI_Hardware.h
@@ -35,5 +35,6 @@
#define MEMORY_SLOT_CLASS_NAME "MemorySlot"
#define MEMORY_PHYS_PKG_CLASS_NAME "MemoryPhysicalPackage"
#define PORT_PHYS_CONN_CLASS_NAME "PortPhysicalConnector"
+#define SYSTEM_SLOT_CLASS_NAME "SystemSlot"
#endif /* LMI_HARDWARE_H_ */
diff --git a/src/hardware/LMI_SystemSlotContainerProvider.c b/src/hardware/LMI_SystemSlotContainerProvider.c
new file mode 100644
index 0000000..c752fbe
--- /dev/null
+++ b/src/hardware/LMI_SystemSlotContainerProvider.c
@@ -0,0 +1,270 @@
+/*
+ * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors: Peter Schiffer <pschiffe@redhat.com>
+ */
+
+#include <konkret/konkret.h>
+#include "LMI_SystemSlotContainer.h"
+#include "LMI_Hardware.h"
+#include "globals.h"
+#include "dmidecode.h"
+
+static const CMPIBroker* _cb;
+
+static void LMI_SystemSlotContainerInitialize()
+{
+}
+
+static CMPIStatus LMI_SystemSlotContainerCleanup(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ CMPIBoolean term)
+{
+ CMReturn(CMPI_RC_OK);
+}
+
+static CMPIStatus LMI_SystemSlotContainerEnumInstanceNames(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop)
+{
+ return KDefaultEnumerateInstanceNames(
+ _cb, mi, cc, cr, cop);
+}
+
+static CMPIStatus LMI_SystemSlotContainerEnumInstances(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const char** properties)
+{
+ LMI_SystemSlotContainer lmi_slot_container;
+ LMI_SystemSlotRef lmi_slot;
+ LMI_ChassisRef lmi_chassis;
+ const char *ns = KNameSpace(cop);
+ unsigned i;
+ DmiChassis dmi_chassis;
+ DmiSystemSlot *dmi_slots = NULL;
+ unsigned dmi_slots_nb = 0;
+
+ if (dmi_get_chassis(&dmi_chassis) != 0) {
+ goto done;
+ }
+ if (dmi_get_system_slots(&dmi_slots, &dmi_slots_nb) != 0 || dmi_slots_nb < 1) {
+ goto done;
+ }
+
+ LMI_ChassisRef_Init(&lmi_chassis, _cb, ns);
+ LMI_ChassisRef_Set_CreationClassName(&lmi_chassis,
+ ORGID "_" CHASSIS_CLASS_NAME);
+ if (strcmp(dmi_chassis.serial_number, "Not Specified") == 0) {
+ LMI_ChassisRef_Set_Tag(&lmi_chassis, "0");
+ } else {
+ LMI_ChassisRef_Set_Tag(&lmi_chassis, dmi_chassis.serial_number);
+ }
+
+ for (i = 0; i < dmi_slots_nb; i++) {
+ LMI_SystemSlotContainer_Init(&lmi_slot_container, _cb, ns);
+
+ LMI_SystemSlotRef_Init(&lmi_slot, _cb, ns);
+ LMI_SystemSlotRef_Set_CreationClassName(&lmi_slot,
+ ORGID "_" SYSTEM_SLOT_CLASS_NAME);
+ LMI_SystemSlotRef_Set_Tag(&lmi_slot, dmi_slots[i].name);
+
+ LMI_SystemSlotContainer_Set_GroupComponent(
+ &lmi_slot_container, &lmi_chassis);
+ LMI_SystemSlotContainer_Set_PartComponent(
+ &lmi_slot_container, &lmi_slot);
+
+ KReturnInstance(cr, lmi_slot_container);
+ }
+
+done:
+ dmi_free_chassis(&dmi_chassis);
+ dmi_free_system_slots(&dmi_slots, &dmi_slots_nb);
+
+ CMReturn(CMPI_RC_OK);
+}
+
+static CMPIStatus LMI_SystemSlotContainerGetInstance(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const char** properties)
+{
+ return KDefaultGetInstance(
+ _cb, mi, cc, cr, cop, properties);
+}
+
+static CMPIStatus LMI_SystemSlotContainerCreateInstance(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const CMPIInstance* ci)
+{
+ CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
+}
+
+static CMPIStatus LMI_SystemSlotContainerModifyInstance(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const CMPIInstance* ci,
+ const char**properties)
+{
+ CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
+}
+
+static CMPIStatus LMI_SystemSlotContainerDeleteInstance(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop)
+{
+ CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
+}
+
+static CMPIStatus LMI_SystemSlotContainerExecQuery(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const char* lang,
+ const char* query)
+{
+ CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
+}
+
+static CMPIStatus LMI_SystemSlotContainerAssociationCleanup(
+ CMPIAssociationMI* mi,
+ const CMPIContext* cc,
+ CMPIBoolean term)
+{
+ CMReturn(CMPI_RC_OK);
+}
+
+static CMPIStatus LMI_SystemSlotContainerAssociators(
+ CMPIAssociationMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const char* assocClass,
+ const char* resultClass,
+ const char* role,
+ const char* resultRole,
+ const char** properties)
+{
+ return KDefaultAssociators(
+ _cb,
+ mi,
+ cc,
+ cr,
+ cop,
+ LMI_SystemSlotContainer_ClassName,
+ assocClass,
+ resultClass,
+ role,
+ resultRole,
+ properties);
+}
+
+static CMPIStatus LMI_SystemSlotContainerAssociatorNames(
+ CMPIAssociationMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const char* assocClass,
+ const char* resultClass,
+ const char* role,
+ const char* resultRole)
+{
+ return KDefaultAssociatorNames(
+ _cb,
+ mi,
+ cc,
+ cr,
+ cop,
+ LMI_SystemSlotContainer_ClassName,
+ assocClass,
+ resultClass,
+ role,
+ resultRole);
+}
+
+static CMPIStatus LMI_SystemSlotContainerReferences(
+ CMPIAssociationMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const char* assocClass,
+ const char* role,
+ const char** properties)
+{
+ return KDefaultReferences(
+ _cb,
+ mi,
+ cc,
+ cr,
+ cop,
+ LMI_SystemSlotContainer_ClassName,
+ assocClass,
+ role,
+ properties);
+}
+
+static CMPIStatus LMI_SystemSlotContainerReferenceNames(
+ CMPIAssociationMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const char* assocClass,
+ const char* role)
+{
+ return KDefaultReferenceNames(
+ _cb,
+ mi,
+ cc,
+ cr,
+ cop,
+ LMI_SystemSlotContainer_ClassName,
+ assocClass,
+ role);
+}
+
+CMInstanceMIStub(
+ LMI_SystemSlotContainer,
+ LMI_SystemSlotContainer,
+ _cb,
+ LMI_SystemSlotContainerInitialize())
+
+CMAssociationMIStub(
+ LMI_SystemSlotContainer,
+ LMI_SystemSlotContainer,
+ _cb,
+ LMI_SystemSlotContainerInitialize())
+
+KONKRET_REGISTRATION(
+ "root/cimv2",
+ "LMI_SystemSlotContainer",
+ "LMI_SystemSlotContainer",
+ "instance association")
diff --git a/src/hardware/LMI_SystemSlotProvider.c b/src/hardware/LMI_SystemSlotProvider.c
new file mode 100644
index 0000000..3feba71
--- /dev/null
+++ b/src/hardware/LMI_SystemSlotProvider.c
@@ -0,0 +1,325 @@
+/*
+ * Copyright (C) 2013 Red Hat, Inc. All rights reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * Authors: Peter Schiffer <pschiffe@redhat.com>
+ */
+
+#include <konkret/konkret.h>
+#include "LMI_SystemSlot.h"
+#include "LMI_Hardware.h"
+#include "globals.h"
+#include "dmidecode.h"
+
+CMPIUint16 get_connectorlayout_slot(const char *dmi_val);
+CMPIUint16 get_maxlinkwidth(const char *dmi_val);
+
+static const CMPIBroker* _cb = NULL;
+
+static void LMI_SystemSlotInitialize()
+{
+}
+
+static CMPIStatus LMI_SystemSlotCleanup(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ CMPIBoolean term)
+{
+ CMReturn(CMPI_RC_OK);
+}
+
+static CMPIStatus LMI_SystemSlotEnumInstanceNames(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop)
+{
+ return KDefaultEnumerateInstanceNames(
+ _cb, mi, cc, cr, cop);
+}
+
+static CMPIStatus LMI_SystemSlotEnumInstances(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const char** properties)
+{
+ LMI_SystemSlot lmi_slot;
+ const char *ns = KNameSpace(cop);
+ CMPIUint16 conn_layout, maxlinkwidth;
+ char instance_id[INSTANCE_ID_LEN];
+ unsigned i;
+ DmiSystemSlot *dmi_slots = NULL;
+ unsigned dmi_slots_nb = 0;
+
+ if (dmi_get_system_slots(&dmi_slots, &dmi_slots_nb) != 0 || dmi_slots_nb < 1) {
+ goto done;
+ }
+
+ for (i = 0; i < dmi_slots_nb; i++) {
+ LMI_SystemSlot_Init(&lmi_slot, _cb, ns);
+
+ LMI_SystemSlot_Set_CreationClassName(&lmi_slot,
+ ORGID "_" SYSTEM_SLOT_CLASS_NAME);
+ LMI_SystemSlot_Set_Caption(&lmi_slot, "System Slot");
+ LMI_SystemSlot_Set_Description(&lmi_slot,
+ "This object represents one system slot.");
+ LMI_SystemSlot_Set_ConnectorGender(&lmi_slot,
+ LMI_SystemSlot_ConnectorGender_Female);
+
+ snprintf(instance_id, INSTANCE_ID_LEN,
+ ORGID ":" ORGID "_" SYSTEM_SLOT_CLASS_NAME ":%s",
+ dmi_slots[i].name);
+ conn_layout = get_connectorlayout_slot(dmi_slots[i].type);
+ maxlinkwidth = get_maxlinkwidth(dmi_slots[i].link_width);
+
+ LMI_SystemSlot_Set_Tag(&lmi_slot, dmi_slots[i].name);
+ LMI_SystemSlot_Set_Number(&lmi_slot, dmi_slots[i].number);
+ LMI_SystemSlot_Set_ConnectorLayout(&lmi_slot, conn_layout);
+ LMI_SystemSlot_Set_ElementName(&lmi_slot, dmi_slots[i].name);
+ LMI_SystemSlot_Set_Name(&lmi_slot, dmi_slots[i].name);
+ LMI_SystemSlot_Set_InstanceID(&lmi_slot, instance_id);
+
+ if (conn_layout == LMI_SystemSlot_ConnectorLayout_Other) {
+ if (strcmp(dmi_slots[i].type, "Other") != 0) {
+ LMI_SystemSlot_Set_ConnectorDescription(&lmi_slot,
+ dmi_slots[i].type);
+ } else {
+ LMI_SystemSlot_Set_ConnectorDescription(&lmi_slot,
+ dmi_slots[i].name);
+ }
+ }
+ if (dmi_slots[i].data_width) {
+ LMI_SystemSlot_Set_MaxDataWidth(&lmi_slot, dmi_slots[i].data_width);
+ }
+ if (maxlinkwidth) {
+ LMI_SystemSlot_Set_MaxLinkWidth(&lmi_slot, maxlinkwidth);
+ }
+ if (dmi_slots[i].supports_hotplug) {
+ LMI_SystemSlot_Set_SupportsHotPlug(&lmi_slot,
+ dmi_slots[i].supports_hotplug);
+ }
+
+ KReturnInstance(cr, lmi_slot);
+ }
+
+done:
+ dmi_free_system_slots(&dmi_slots, &dmi_slots_nb);
+
+ CMReturn(CMPI_RC_OK);
+}
+
+static CMPIStatus LMI_SystemSlotGetInstance(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const char** properties)
+{
+ return KDefaultGetInstance(
+ _cb, mi, cc, cr, cop, properties);
+}
+
+static CMPIStatus LMI_SystemSlotCreateInstance(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const CMPIInstance* ci)
+{
+ CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
+}
+
+static CMPIStatus LMI_SystemSlotModifyInstance(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const CMPIInstance* ci,
+ const char** properties)
+{
+ CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
+}
+
+static CMPIStatus LMI_SystemSlotDeleteInstance(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop)
+{
+ CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
+}
+
+static CMPIStatus LMI_SystemSlotExecQuery(
+ CMPIInstanceMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const char* lang,
+ const char* query)
+{
+ CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
+}
+
+CMInstanceMIStub(
+ LMI_SystemSlot,
+ LMI_SystemSlot,
+ _cb,
+ LMI_SystemSlotInitialize())
+
+static CMPIStatus LMI_SystemSlotMethodCleanup(
+ CMPIMethodMI* mi,
+ const CMPIContext* cc,
+ CMPIBoolean term)
+{
+ CMReturn(CMPI_RC_OK);
+}
+
+static CMPIStatus LMI_SystemSlotInvokeMethod(
+ CMPIMethodMI* mi,
+ const CMPIContext* cc,
+ const CMPIResult* cr,
+ const CMPIObjectPath* cop,
+ const char* meth,
+ const CMPIArgs* in,
+ CMPIArgs* out)
+{
+ return LMI_SystemSlot_DispatchMethod(
+ _cb, mi, cc, cr, cop, meth, in, out);
+}
+
+/*
+ * Get max link width according to the dmidecode.
+ * @param dmi_val from dmidecode
+ * @return CIM id of max link width
+ */
+CMPIUint16 get_maxlinkwidth(const char *dmi_val)
+{
+ if (!dmi_val || !strlen(dmi_val)) {
+ return 0; /* Unknown */
+ }
+
+ static struct {
+ CMPIUint16 cim_val; /* CIM value */
+ char *dmi_val; /* dmidecode value */
+ } values[] = {
+ {0, "Unknown"},
+ {2, "x1"},
+ {3, "x2"},
+ {4, "x4"},
+ {5, "x8"},
+ {6, "x12"},
+ {7, "x16"},
+ {8, "x32"},
+ };
+
+ size_t i, val_length = sizeof(values) / sizeof(values[0]);
+
+ for (i = 0; i < val_length; i++) {
+ if (strcmp(dmi_val, values[i].dmi_val) == 0) {
+ return values[i].cim_val;
+ }
+ }
+
+ return 0; /* Unknown */
+}
+
+/*
+ * Get connector layout for slot according to the dmidecode.
+ * @param dmi_val from dmidecode
+ * @return CIM id of connector layout
+ */
+CMPIUint16 get_connectorlayout_slot(const char *dmi_val)
+{
+ if (!dmi_val || !strlen(dmi_val)) {
+ return 0; /* Unknown */
+ }
+
+ static struct {
+ CMPIUint16 cim_val; /* CIM value */
+ char *dmi_val; /* dmidecode value */
+ } values[] = {
+ {0, "Unknown"},
+ {1, "Other"},
+ /*
+ {2, "RS232"},
+ {3, "BNC"},
+ {4, "RJ11"},
+ {5, "RJ45"},
+ {6, "DB9"},
+ {7, "Slot"},
+ {8, "SCSI High Density"},
+ {9, "SCSI Low Density"},
+ {10, "Ribbon"},
+ {11, "AUI"},
+ {12, "Fiber SC"},
+ {13, "Fiber ST"},
+ {14, "FDDI-MIC"},
+ {15, "Fiber-RTMJ"},
+ */
+ {16, "PCI"},
+ {17, "PCI-X"},
+ {18, "PCI Express"},
+ {18, "PCI Express 2"},
+ {18, "PCI Express 3"},
+ {19, "PCI Express x1"},
+ {19, "PCI Express 2 x1"},
+ {19, "PCI Express 3 x1"},
+ {20, "PCI Express x2"},
+ {20, "PCI Express 2 x2"},
+ {20, "PCI Express 3 x2"},
+ {21, "PCI Express x4"},
+ {21, "PCI Express 2 x4"},
+ {21, "PCI Express 3 x4"},
+ {22, "PCI Express x8"},
+ {22, "PCI Express 2 x8"},
+ {22, "PCI Express 3 x8"},
+ {23, "PCI Express x16"},
+ {23, "PCI Express 2 x16"},
+ {23, "PCI Express 3 x16"},
+ /*
+ {24, "PCI Express x32"},
+ {24, "PCI Express 2 x32"},
+ {24, "PCI Express 3 x32"},
+ {25, "PCI Express x64"},
+ {25, "PCI Express 2 x64"},
+ {25, "PCI Express 3 x64"},
+ */
+ };
+
+ size_t i, val_length = sizeof(values) / sizeof(values[0]);
+
+ for (i = 0; i < val_length; i++) {
+ if (strcmp(dmi_val, values[i].dmi_val) == 0) {
+ return values[i].cim_val;
+ }
+ }
+
+ return 1; /* Other */
+}
+
+CMMethodMIStub(
+ LMI_SystemSlot,
+ LMI_SystemSlot,
+ _cb,
+ LMI_SystemSlotInitialize())
+
+KONKRET_REGISTRATION(
+ "root/cimv2",
+ "LMI_SystemSlot",
+ "LMI_SystemSlot",
+ "instance method")
diff --git a/src/hardware/dmidecode.c b/src/hardware/dmidecode.c
index cbee376..f29596f 100644
--- a/src/hardware/dmidecode.c
+++ b/src/hardware/dmidecode.c
@@ -1649,3 +1649,213 @@ void dmi_free_ports(DmiPort **ports, unsigned *ports_nb)
*ports_nb = 0;
*ports = NULL;
}
+
+
+/******************************************************************************
+ * DmiSystemSlot
+ */
+
+/*
+ * Initialize DmiSystemSlot attributes.
+ * @param slot
+ */
+void init_dmislot_struct(DmiSystemSlot *slot)
+{
+ slot->name = NULL;
+ slot->number = 0;
+ slot->type = NULL;
+ slot->data_width = 0;
+ slot->link_width = NULL;
+ slot->supports_hotplug = 0;
+}
+
+/*
+ * Check attributes of slot structure and fill in defaults if needed.
+ * @param slot
+ * @return 0 if success, negative value otherwise
+ */
+short check_dmislot_attributes(DmiSystemSlot *slot)
+{
+ short ret = -1;
+
+ if (!slot->name) {
+ if (!(slot->name = strdup("System slot"))) {
+ ret = -2;
+ goto done;
+ }
+ }
+ if (!slot->type) {
+ if (!(slot->type = strdup("Unknown"))) {
+ ret = -3;
+ goto done;
+ }
+ }
+ if (!slot->link_width) {
+ if (!(slot->link_width = strdup("Unknown"))) {
+ ret = -4;
+ goto done;
+ }
+ }
+
+ ret = 0;
+
+done:
+ if (ret != 0) {
+ warn("Failed to allocate memory.");
+ }
+
+ return ret;
+}
+
+short dmi_get_system_slots(DmiSystemSlot **slots, unsigned *slots_nb)
+{
+ short ret = -1;
+ int curr_slot = -1;
+ unsigned i, buffer_size = 0;
+ char **buffer = NULL, *buf;
+
+ *slots_nb = 0;
+
+ /* get dmidecode output */
+ if (run_command("dmidecode -t 9", &buffer, &buffer_size) != 0) {
+ ret = -2;
+ goto done;
+ }
+
+ /* count slots */
+ for (i = 0; i < buffer_size; i++) {
+ if (strncmp(buffer[i], "Handle 0x", 9) == 0) {
+ (*slots_nb)++;
+ }
+ }
+
+ /* if no slot was found */
+ if (*slots_nb < 1) {
+ warn("Dmidecode didn't recognize any system slot.");
+ ret = -3;
+ goto done;
+ }
+
+ /* allocate memory for slots */
+ *slots = (DmiSystemSlot *)calloc(*slots_nb, sizeof(DmiSystemSlot));
+ if (!(*slots)) {
+ warn("Failed to allocate memory.");
+ ret = -4;
+ goto done;
+ }
+
+ /* parse information about slots */
+ for (i = 0; i < buffer_size; i++) {
+ if (strncmp(buffer[i], "Handle 0x", 9) == 0) {
+ curr_slot++;
+ init_dmislot_struct(&(*slots)[curr_slot]);
+ (*slots)[curr_slot].number = curr_slot;
+ continue;
+ }
+ /* ignore first useless lines */
+ if (curr_slot < 0) {
+ continue;
+ }
+ /* Name */
+ buf = copy_string_part_after_delim(buffer[i], "Designation: ");
+ if (buf) {
+ (*slots)[curr_slot].name = buf;
+ buf = NULL;
+ continue;
+ }
+ /* Type, data width, link width */
+ buf = copy_string_part_after_delim(buffer[i], "Type: ");
+ if (buf) {
+ char **exploded_buf = NULL;
+ unsigned j, exploded_buf_size = 0, from = 0;
+
+ /* Dmi type consists of 2 information. First is data width (NUM-bit)
+ or link width (xNUM) or nothing, second is slot type. */
+ if (explode(buf, NULL, &exploded_buf, &exploded_buf_size) != 0) {
+ ret = -5;
+ goto done;
+ }
+ if (exploded_buf_size < 1) {
+ continue;
+ }
+
+ if (exploded_buf[0][0] == 'x') {
+ (*slots)[curr_slot].link_width = strdup(exploded_buf[0]);
+ if (!(*slots)[curr_slot].link_width) {
+ free_2d_buffer(&exploded_buf, &exploded_buf_size);
+ warn("Failed to allocate memory.");
+ ret = -6;
+ goto done;
+ }
+ from = 1;
+ } else if (strstr(exploded_buf[0], "-bit")) {
+ sscanf(buf, "%u-bit ", &(*slots)[curr_slot].data_width);
+ from = 1;
+ }
+
+ for (j = from; j < exploded_buf_size; j++) {
+ /* If type is not NULL, we need to add space before another string. */
+ if (!(*slots)[curr_slot].type) {
+ (*slots)[curr_slot].type = append_str(
+ (*slots)[curr_slot].type, exploded_buf[j], NULL);
+ } else {
+ (*slots)[curr_slot].type = append_str(
+ (*slots)[curr_slot].type, " ", exploded_buf[j], NULL);
+ }
+ /* If type is NULL here, append_str failed. */
+ if (!(*slots)[curr_slot].type) {
+ free_2d_buffer(&exploded_buf, &exploded_buf_size);
+ ret = -7;
+ goto done;
+ }
+ }
+
+ free_2d_buffer(&exploded_buf, &exploded_buf_size);
+ free(buf);
+ buf = NULL;
+ continue;
+ }
+ if (strcmp(buffer[i], "Hot-plug devices are supported") == 0) {
+ (*slots)[curr_slot].supports_hotplug = 1;
+ }
+ }
+
+ /* fill in default attributes if needed */
+ for (i = 0; i < *slots_nb; i++) {
+ if (check_dmislot_attributes(&(*slots)[i]) != 0) {
+ ret = -8;
+ goto done;
+ }
+ }
+
+ ret = 0;
+
+done:
+ free_2d_buffer(&buffer, &buffer_size);
+
+ if (ret != 0) {
+ dmi_free_system_slots(slots, slots_nb);
+ }
+
+ return ret;
+}
+
+void dmi_free_system_slots(DmiSystemSlot **slots, unsigned *slots_nb)
+{
+ unsigned i;
+
+ if (*slots_nb > 0) {
+ for (i = 0; i < *slots_nb; i++) {
+ free((*slots)[i].name);
+ (*slots)[i].name = NULL;
+ free((*slots)[i].type);
+ (*slots)[i].type = NULL;
+ free((*slots)[i].link_width);
+ (*slots)[i].link_width = NULL;
+ }
+ free(*slots);
+ }
+
+ *slots_nb = 0;
+ *slots = NULL;
+}
diff --git a/src/hardware/dmidecode.h b/src/hardware/dmidecode.h
index 5423c93..76627af 100644
--- a/src/hardware/dmidecode.h
+++ b/src/hardware/dmidecode.h
@@ -125,6 +125,16 @@ typedef struct _DmiPort {
char *port_type; /* Port Type */
} DmiPort;
+/* System Slot from dmidecode. */
+typedef struct _DmiSystemSlot {
+ char *name; /* Name */
+ unsigned number; /* Slot number */
+ char *type; /* Slot type */
+ unsigned data_width; /* Data width */
+ char *link_width; /* Link width */
+ short supports_hotplug; /* Supports slot hotplug? */
+} DmiSystemSlot;
+
/*
* Get array of processors according to the dmidecode program.
* @param cpu array of cpus, this function will allocate necessary memory,
@@ -215,5 +225,21 @@ short dmi_get_ports(DmiPort **ports, unsigned *ports_nb);
*/
void dmi_free_ports(DmiPort **ports, unsigned *ports_nb);
+/*
+ * Get array of system slots according to the dmidecode program.
+ * @param slots array of slots, this function will allocate necessary memory,
+ * but caller is responsible for freeing it
+ * @param slots_nb number of slots
+ * @return 0 if success, negative value otherwise
+ */
+short dmi_get_system_slots(DmiSystemSlot **slots, unsigned *slots_nb);
+
+/*
+ * Free array of slot structures.
+ * @param slots array of slots
+ * @param slots_nb number of slots
+ */
+void dmi_free_system_slots(DmiSystemSlot **slots, unsigned *slots_nb);
+
#endif /* DMIDECODE_H_ */