📄 epath_utils.c
字号:
/* -*- linux-c -*- * * (C) Copyright IBM Corp. 2003, 2004 * * This program 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. This * file and program are licensed under a BSD style license. See * the Copying file included with the OpenHPI distribution for * full licensing terms. * * Author(s): * Steve Sherman <stevees@us.ibm.com> * Renier Morales <renierm@users.sf.net> * Thomas Kanngieser <thomas.kanngieser@fci.com> * Chris Chia <cchia@users.sf.net.com> *//****************************************************************************** * DESCRIPTION: * Module contains functions to convert between HPI's SaHpiEntityPathT * structure and an OpenHPI canonical string. The canonical string is formed * by removing the "SAHPI_ENT_" prefix from the HPI types, and creating * tuples for the entity types. Order of significance is inverted to make * entity paths look more like Unix directory structure. It is also assumed * that {ROOT,0} exists implicitly before all of these entries. For example: * * {SYSTEM_CHASSIS,2}{PROCESSOR_BOARD,0} * * FUNCTIONS: * string2entitypath - Coverts canonical entity path string to HPI entity path * entitypath2string - Coverts HPI entity path to canonical entity path string * ep_concat - Concatenates two SaHpiEntityPathT together. * * NOTES: * - SAHPI_ENT_ROOT is used to identify end element of an entity path * fully populated entity path may not have a SAHPI_ENT_ROOT. * - Duplicate names in SaHPIEntityTypeT enumeration aren't handled * Names below won't be preserved across conversion calls: * - IPMI_GROUP - IPMI_GROUP + 0x90 * - IPMI_GROUP + 0xB0 - IPMI_GROUP + 0xD0 * - ROOT_VALUE - SAFHPI_GROUP *****************************************************************************/#include <glib.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>#include <oh_utils.h>static unsigned int index2entitytype(unsigned int i);static int entitytype2index(unsigned int i);static gchar *eshort_names[] = { "UNSPECIFIED", "OTHER", "UNKNOWN", "PROCESSOR", "DISK_BAY", "PERIPHERAL_BAY", "SYS_MGMNT_MODULE", "SYSTEM_BOARD", "MEMORY_MODULE", "PROCESSOR_MODULE", "POWER_SUPPLY", "ADD_IN_CARD", "FRONT_PANEL_BOARD", "BACK_PANEL_BOARD", "POWER_SYSTEM_BOARD", "DRIVE_BACKPLANE", "SYS_EXPANSION_BOARD", "OTHER_SYSTEM_BOARD", "PROCESSOR_BOARD", "POWER_UNIT", "POWER_MODULE", "POWER_MGMNT", "CHASSIS_BACK_PANEL_BOARD", "SYSTEM_CHASSIS", "SUB_CHASSIS", "OTHER_CHASSIS_BOARD", "DISK_DRIVE_BAY", "PERIPHERAL_BAY_2", "DEVICE_BAY", "COOLING_DEVICE", "COOLING_UNIT", "INTERCONNECT", "MEMORY_DEVICE", "SYS_MGMNT_SOFTWARE", "BIOS", "OPERATING_SYSTEM", "SYSTEM_BUS", "GROUP", "REMOTE", "EXTERNAL_ENVIRONMENT", "BATTERY", "CHASSIS_SPECIFIC", /* Jumps to 144 */ "BOARD_SET_SPECIFIC", /* Jumps to 176 */ "OEM_SYSINT_SPECIFIC", /* Jumps to 208 */ "ROOT", /* Jumps to 65535 and continues from there... */ "RACK", "SUBRACK", "COMPACTPCI_CHASSIS", "ADVANCEDTCA_CHASSIS", "RACK_MOUNTED_SERVER", "SYSTEM_BLADE", "SWITCH", "SWITCH_BLADE", "SBC_BLADE", "IO_BLADE", "DISK_BLADE", "DISK_DRIVE", "FAN", "POWER_DISTRIBUTION_UNIT", "SPEC_PROC_BLADE", "IO_SUBBOARD", "SBC_SUBBOARD", "ALARM_MANAGER", "SHELF_MANAGER", "DISPLAY_PANEL", "SUBBOARD_CARRIER_BLADE", "PHYSICAL_SLOT"};static unsigned int eshort_num_names = sizeof( eshort_names ) / sizeof( gchar * );static unsigned int index2entitytype(unsigned int i){ if(i <= ESHORTNAMES_BEFORE_JUMP) { return i; } else if(i == ESHORTNAMES_FIRST_JUMP) { return (unsigned int)SAHPI_ENT_CHASSIS_SPECIFIC; } else if(i == ESHORTNAMES_SECOND_JUMP) { return (unsigned int)SAHPI_ENT_BOARD_SET_SPECIFIC; } else if(i == ESHORTNAMES_THIRD_JUMP) { return (unsigned int)SAHPI_ENT_OEM_SYSINT_SPECIFIC; } else { assert(i >= ESHORTNAMES_LAST_JUMP); return (i - ESHORTNAMES_LAST_JUMP + (unsigned int)SAHPI_ENT_ROOT); }}static int entitytype2index(unsigned int i){ if(i <= ESHORTNAMES_BEFORE_JUMP) return i; else if (i == (unsigned int)SAHPI_ENT_CHASSIS_SPECIFIC) return ESHORTNAMES_FIRST_JUMP; else if (i == (unsigned int)SAHPI_ENT_BOARD_SET_SPECIFIC) return ESHORTNAMES_SECOND_JUMP; else if (i == (unsigned int)SAHPI_ENT_OEM_SYSINT_SPECIFIC) return ESHORTNAMES_THIRD_JUMP; else if (i >= (unsigned int)SAHPI_ENT_ROOT && i-(unsigned int)SAHPI_ENT_ROOT < eshort_num_names-ESHORTNAMES_LAST_JUMP) return i-(unsigned int)SAHPI_ENT_ROOT+ESHORTNAMES_LAST_JUMP; return -1;}/** * string2entitypath * @epathstr: IN. Pointer to canonical entity path string * @epathptr: OUT. Pointer to HPI's entity path structure * * Converts an entity path canonical string into a * SaHpiEntityPathT structure. * * Returns: 0 Successful return, -1 Error return */int string2entitypath(const gchar *epathstr, SaHpiEntityPathT *epathptr){ gchar **epathdefs = NULL, **epathvalues = NULL; gchar *gstr = NULL, *etype = NULL, *einstance = NULL, *endptr = NULL; gint rtncode = 0; guint i, j, match, instance, num_valid_entities = 0; GSList *epath_list = NULL, *lst = NULL; SaHpiEntityT *entityptr = NULL; gint num = 0; int is_numeric = 0; if (epathstr == NULL || epathstr[0] == '\0') { dbg("Input entity path string is NULL"); return -1; } /* Split out {xxx,yyy} definition pairs */ gstr = g_strstrip(g_strdup(epathstr)); if (gstr == NULL || gstr[0] == '\0') { dbg("Stripped entity path string is NULL"); rtncode = -1; goto CLEANUP; } epathdefs = g_strsplit(gstr, EPATHSTRING_END_DELIMITER, -1); if (epathdefs == NULL) { dbg("Could not split entity path string."); rtncode = -1; goto CLEANUP; } /* Split out HPI entity type and instance strings */ for (i=0; epathdefs[i] != NULL && epathdefs[i][0] != '\0'; i++) { epathdefs[i] = g_strstrip(epathdefs[i]); /* Check format - for starting delimiter and a comma */ if ((epathdefs[i][0] != EPATHSTRING_START_DELIMITER_CHAR) || (strpbrk(epathdefs[i], EPATHSTRING_VALUE_DELIMITER) == NULL)) { dbg("Invalid entity path format."); rtncode = -1; goto CLEANUP; } epathvalues = g_strsplit(epathdefs[i], EPATHSTRING_VALUE_DELIMITER, ELEMENTS_IN_SaHpiEntityT); epathvalues[0] = g_strdelimit(epathvalues[0], EPATHSTRING_START_DELIMITER, ' '); etype = g_strstrip(epathvalues[0]); einstance = g_strstrip(epathvalues[1]); instance = strtol(einstance, &endptr, 10); if (endptr[0] != '\0') { dbg("Invalid instance character"); rtncode = -1; goto CLEANUP; } for (match=0, j=0; j < eshort_num_names; j++) { if (!strcmp(eshort_names[j], etype)) { match = 1; break; } } is_numeric = 0; if (!match) { // check for numeric type num = strtol(etype,&endptr, 0); if (num <= 0 || endptr[0] != '\0') { dbg("Invalid entity type string"); rtncode = -1; goto CLEANUP; } is_numeric = 1; } /* Save entity path definitions; reverse order */ if (num_valid_entities < SAHPI_MAX_ENTITY_PATH) { entityptr = (SaHpiEntityT *)g_malloc0(sizeof(*entityptr)); if (entityptr == NULL) { dbg("Out of memory"); rtncode = -1; goto CLEANUP; } if (is_numeric) entityptr->EntityType = num; else entityptr->EntityType = index2entitytype(j); entityptr->EntityLocation = instance; epath_list = g_slist_prepend(epath_list, (gpointer)entityptr); } num_valid_entities++; } /* Initialize and write HPI entity path structure */ ep_init(epathptr); for (i = 0; epath_list != NULL; i++) { lst = epath_list; if (i < SAHPI_MAX_ENTITY_PATH) { epathptr->Entry[i].EntityType = ((SaHpiEntityT *)(lst->data))->EntityType; epathptr->Entry[i].EntityLocation = ((SaHpiEntityT *)(lst->data))->EntityLocation; } epath_list = g_slist_remove_link(epath_list,lst); g_free(lst->data); g_slist_free(lst); } if (num_valid_entities > SAHPI_MAX_ENTITY_PATH) { dbg("Too many entity defs"); rtncode = -1; } CLEANUP: g_free(gstr); g_strfreev(epathdefs); g_strfreev(epathvalues); g_slist_free(epath_list); return(rtncode);} /* End string2entitypath *//** * entitypath2string * @epathptr: IN. Pointer to HPI's entity path structure * @epathstr: OUT. Pointer to canonical entity path string * @strsize: IN. Canonical string length * * Converts an entity path structure into its * canonical string version. * * Returns: >0 Number of characters written to canonical entity path string, * -1 Error return. -2 Entity path has invalid entity types. */int entitypath2string(const SaHpiEntityPathT *epathptr, gchar *epathstr, const gint strsize){ gchar *instance_str, *catstr, *tmpstr; gint err, i, strcount = 0, rtncode = 0; int tidx; gchar *type_str; gchar type_str_buffer[20]; if (epathstr == NULL || strsize <= 0) { dbg("Null string or invalid string size"); return -1; } if (epathptr == NULL) { *epathstr = '\0'; return 0; } /*if (validate_ep(epathptr)) { dbg("Entity path contains invalid types. Unable to convert to string."); return -2; }*/ instance_str = (gchar *)g_malloc0(OH_MAX_LOCATION_DIGITS + 1); tmpstr = (gchar *)g_malloc0(strsize); if (instance_str == NULL || tmpstr == NULL) { dbg("Out of memory"); rtncode = -1; goto CLEANUP; } /* Find last element of structure. Disregard ROOT element * and count as last in entity path. */ for (i = 0; i < SAHPI_MAX_ENTITY_PATH; i++) { if (epathptr->Entry[i].EntityType == SAHPI_ENT_ROOT) { break; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -