multiple ipmi shortname implementations

As ipmi subsystem requires short names, and there are multiple
ways to implement that, this patch provides compile-time configuration
options.

 - shortname-remove-suffix
 - shortname-replace-words

Previously, PSU Sensor Names were stripped of some
suffixes, some of which uniquely identified the sensor.

e.g. "_Input_Voltage", "_Output_Voltage".
Without the suffix, the sensor cannot be uniquely identified.

Example: "PSU0_Input_Voltage", "PSU0_Output_Voltage"
both become "PSU0".

Tested:
 - with both configurations turned off, name is only trucated
 - with nothing configured, default config is applied and suffix is
   removed (current behavior)
 - word replacement enabled and suffix removal disabled: expected
   behavior
 - word replacement disabled and suffix removal enabled: expected
   behavior

Change-Id: I01dd35f31e75df3c31733e9818884813a241440a
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/dbus-sdr/sensorcommands.cpp b/dbus-sdr/sensorcommands.cpp
index 39e61f0..b4dfa5a 100644
--- a/dbus-sdr/sensorcommands.cpp
+++ b/dbus-sdr/sensorcommands.cpp
@@ -14,6 +14,8 @@
 // limitations under the License.
 */
 
+#include "config.h"
+
 #include "dbus-sdr/sensorcommands.hpp"
 
 #include "dbus-sdr/sdrutils.hpp"
@@ -483,7 +485,7 @@
 
     if (name.size() > FULL_RECORD_ID_STR_MAX_LENGTH)
     {
-        // try to not truncate by replacing common words
+#ifdef SHORTNAME_REMOVE_SUFFIX
         for (const auto& suffix : suffixes)
         {
             if (boost::ends_with(name, suffix))
@@ -492,10 +494,19 @@
                 break;
             }
         }
-        if (name.size() > FULL_RECORD_ID_STR_MAX_LENGTH)
+#endif
+#ifdef SHORTNAME_REPLACE_WORDS
+        constexpr std::array<std::pair<const char*, const char*>, 2>
+            replaceWords = {std::make_pair("Output", "Out"),
+                            std::make_pair("Input", "In")};
+        for (const auto& [find, replace] : replaceWords)
         {
-            name.resize(FULL_RECORD_ID_STR_MAX_LENGTH);
+            boost::replace_all(name, find, replace);
         }
+#endif
+
+        // as a backup and if nothing else is configured
+        name.resize(FULL_RECORD_ID_STR_MAX_LENGTH);
     }
     return name;
 }
diff --git a/meson.build b/meson.build
index 0cd2f44..4a4d01e 100644
--- a/meson.build
+++ b/meson.build
@@ -34,6 +34,13 @@
 conf_data.set_quoted('HOST_IPMI_LIB_PATH', get_option('host-ipmi-lib-path'))
 conf_data.set_quoted('FW_VER_REGEX', get_option('fw-ver-regex'))
 
+if get_option('shortname-remove-suffix').enabled()
+  conf_data.set_quoted('SHORTNAME_REMOVE_SUFFIX', '1')
+endif
+if get_option('shortname-replace-words').enabled()
+  conf_data.set_quoted('SHORTNAME_REPLACE_WORDS', '1')
+endif
+
 matches_map = get_option('matches-map')
 conf_data.set('MAJOR_MATCH_INDEX', matches_map[0])
 conf_data.set('MINOR_MATCH_INDEX', matches_map[1])
diff --git a/meson.options b/meson.options
index fb0c5f2..2513c56 100644
--- a/meson.options
+++ b/meson.options
@@ -60,6 +60,10 @@
 # Sensor Cache
 option('sensors-cache', type: 'feature', value: 'disabled', description: 'Sensor cache stack is disabled by default; offer a way to enable it')
 
+# Short Sensor Names for IPMI
+option('shortname-remove-suffix', type: 'feature', value: 'enabled', description: 'shortname-remove-suffix is enabled by default')
+option('shortname-replace-words', type: 'feature', value: 'disabled', description: 'shortname-replace-words is disabled by default')
+
 # SEL Logger
 option('sel-logger-clears-sel', type: 'feature', value: 'disabled', description: 'Clearing SEL through sel-logger is disabled by default; offer a way to enable it')