From 27f98af2182587537fcf21729b381e1f6de8a4b6 Mon Sep 17 00:00:00 2001
From: "ezekiel.dohmen" <ezekiel.dohmen@ligo.org>
Date: Thu, 25 Jul 2024 19:17:11 -0500
Subject: [PATCH] Fixing bug where check does not work with RFM1 IPCs, and
 adding check to kill build if we use an index higher than 63 for RFM IPCs

---
 src/epics/util/lib/IPCx.pm | 37 +++++++++++++++++++++++++++++--------
 1 file changed, 29 insertions(+), 8 deletions(-)

diff --git a/src/epics/util/lib/IPCx.pm b/src/epics/util/lib/IPCx.pm
index 61d051b10..cbc331d61 100644
--- a/src/epics/util/lib/IPCx.pm
+++ b/src/epics/util/lib/IPCx.pm
@@ -19,6 +19,8 @@ require "lib/EpicsVariable.pm";
 #// 
 require "lib/Util.pm";
 require "lib/medmGen.pm";
+
+$IPCx::MAX_RFM_IPC_INDEX = 63;
  
 #// \n\n \b Key \b global \b variables \n\n
 #// \b ipcxParts[][] \n
@@ -712,10 +714,34 @@ if ($::ipcxCnt > 0) {
                die "***ERROR: IPCx Communication Mechanism not recognized: $::ipcxCommMech\n";
             }
 
+            $signalName = $::ipcxParts[$ipcxAdd[$jj][0]][0];
+            if ($signalName =~ /^\w+([A-Z]\d\:.+)/) {
+               $signalName = $1;
+            }
+
 
             # See if this is an RFM IPC and use the appropriate limit
             my $myIpcLimit = $maxIpcCount;
-            if ($ipcxTypeIndex == 1 || $ipcTypeIndex == 2) {
+            if ($ipcxTypeIndex == 1 || $ipcxTypeIndex == 2) { #RFM0 or RFM1
+
+
+                if($ipcxMaxNum[$ipcxTypeIndex] == $IPCx::MAX_RFM_IPC_INDEX)
+                {
+                    $new_index = $ipcxMaxNum[$ipcxTypeIndex] + 1;
+                    flock (IPCIN, LOCK_UN);
+                    close IPCIN;
+                    die "The next free RFM IPC index: $new_index is too high for support by the RFM. "
+                       . "You need to find an old unused RFM 0/1 index for your IPC.\n"
+                       . "Your new entry should look as described below, with the new IPC number chosen by you.\n\n"
+                       . "\[$signalName\]\n"
+                       . "ipcType=$ipcxType[$ipcxTypeIndex]\n"
+                       . "ipcRate=$chosenIpcRate\n"
+                       . "ipcHost=$::targetHost\n"
+                       . "ipcModel=$::skeleton\n"
+                       . "ipcNum=<FILL ME>\n"
+                       . "desc=Manually added because the RFM index was higher than would be forwarded by the CDSRFM.\n\n";
+                    
+                }
 
                 if ( $chosenIpcRate > 16384 && not defined($::rfmDelay) )
                 {
@@ -732,11 +758,6 @@ if ($::ipcxCnt > 0) {
             }
 
 
-            $signalName = $::ipcxParts[$ipcxAdd[$jj][0]][0];
-            if ($signalName =~ /^\w+([A-Z]\d\:.+)/) {
-               $signalName = $1;
-            }
-
 
             #
             # Add data to the IPCx parameter file
@@ -748,13 +769,13 @@ if ($::ipcxCnt > 0) {
                    "***ERROR: IPCx number > $myIpcLimit for ipcType = $ipcxType[$ipcxTypeIndex]\n";
             }
 
-            print IPCIN "\[$signalName\]\n";
+            print IPCIN "\n\[$signalName\]\n";
             print IPCIN "ipcType=$ipcxType[$ipcxTypeIndex]\n";
             print IPCIN "ipcRate=$chosenIpcRate\n";
             print IPCIN "ipcHost=$::targetHost\n";
             print IPCIN "ipcModel=$::skeleton\n";
             print IPCIN "ipcNum=$ipcxMaxNum[$ipcxTypeIndex]\n";
-            print IPCIN "desc=Automatically generated by IPCx\.pm on $::theTime\n\n";
+            print IPCIN "desc=Automatically generated by IPCx\.pm on $::theTime\n";
 
             $ipcxDataAdded[$ipcxNew][0] = $::ipcxParts[$ipcxAdd[$jj][0]];
             $ipcxDataAdded[$ipcxNew][1] = "I" . substr($ipcxType[$ipcxTypeIndex], 0, 4);
-- 
GitLab