4848#include "dummy-ups.h"
4949
5050#define DRIVER_NAME "Device simulation and repeater driver"
51- #define DRIVER_VERSION "0.28 "
51+ #define DRIVER_VERSION "0.29 "
5252
5353/* driver description structure */
5454upsdrv_info_t upsdrv_info =
@@ -785,6 +785,22 @@ static void upsconf_err(const char *errmsg)
785785 upslogx (LOG_ERR , "Fatal error in parseconf(ups.conf): %s" , errmsg );
786786}
787787
788+ /* Join the words of an alarm message from the definition file. */
789+ static void parse_alarm_message (char * value , size_t size )
790+ {
791+ size_t i , used = 0 ;
792+
793+ value [0 ] = '\0' ;
794+ for (i = 1 ; i < ctx -> numargs ; i ++ ) {
795+ int count = snprintf (value + used , size - used , "%s%s" ,
796+ i == 1 ? "" : " " , ctx -> arglist [i ]);
797+
798+ if (count < 0 || (size_t )count >= size - used )
799+ break ;
800+ used += (size_t )count ;
801+ }
802+ }
803+
788804/* for dummy mode
789805 * parse the definition file and process its content
790806 */
@@ -793,6 +809,8 @@ static int parse_data_file(TYPE_FD arg_upsfd)
793809 char fn [NUT_PATH_MAX + 1 ];
794810 char * ptr , var_value [MAX_STRING_SIZE ];
795811 size_t value_args = 0 , counter ;
812+ static int recorded_alarm = 0 ; /* carry an assignment across TIMER lines */
813+ int alarm_assigned = 0 ;
796814 time_t now ;
797815 NUT_UNUSED_VARIABLE (arg_upsfd );
798816
@@ -832,6 +850,7 @@ static int parse_data_file(TYPE_FD arg_upsfd)
832850 status_init (); /* in case no ups.status does it */
833851 upsdebugx (5 , "%s: call alarm_init()" , __func__ );
834852 alarm_init (); /* reset alarms at start of parsing */
853+ recorded_alarm = 0 ;
835854 }
836855
837856 /* Reset the next call time, so that we can loop back on the file
@@ -873,23 +892,18 @@ static int parse_data_file(TYPE_FD arg_upsfd)
873892 /* ALARM instruction */
874893 if (!strncmp (ctx -> arglist [0 ], "ALARM" , 5 ))
875894 {
876- if (ctx -> numargs > 1 ) {
877- for (counter = 1 , value_args = ctx -> numargs ;
878- counter < value_args ; counter ++ )
879- {
880- if (counter == 1 ) /* don't append the first space separator */
881- snprintf (var_value , sizeof (var_value ), "%s" , ctx -> arglist [counter ]);
882- else
883- snprintfcat (var_value , sizeof (var_value ), " %s" , ctx -> arglist [counter ]);
884- }
885- if (* var_value != '\0' ) {
886- alarm_set (var_value );
887- upsdebugx (3 , "parse_data_file: ALARM instruction with value \"%s\"" , var_value );
888-
889- continue ;
890- }
895+ parse_alarm_message (var_value , sizeof (var_value ));
896+ if (* var_value ) {
897+ const st_tree_t * previous = dstate_tree_find ("ups.alarm" );
898+ if (recorded_alarm && previous )
899+ alarm_set (previous -> raw );
900+ recorded_alarm = 0 ;
901+ alarm_set (var_value );
902+ upsdebugx (3 , "parse_data_file: ALARM instruction with value \"%s\"" , var_value );
903+ continue ;
891904 }
892905
906+ recorded_alarm = 0 ;
893907 alarm_init ();
894908 upsdebugx (3 , "parse_data_file: ALARM instruction with no value (reset alarms)" );
895909
@@ -914,13 +928,32 @@ static int parse_data_file(TYPE_FD arg_upsfd)
914928 /* special handler for status */
915929 if (!strncmp ( ctx -> arglist [0 ], "ups.status" , 10 ))
916930 {
931+ int had_recorded_alarm = recorded_alarm && !alarm_assigned &&
932+ str_contains_token (dstate_getinfo ("ups.status" ), "ALARM" );
933+
917934 status_init ();
918935 for (counter = 1 , value_args = ctx -> numargs ;
919936 counter < value_args ; counter ++ )
920937 {
921938 status_set (ctx -> arglist [counter ]);
922939 }
923940 status_commit ();
941+
942+ /* Older recordings omit deleted variables. Dropping ALARM
943+ * clears a description carried from a previous snapshot. */
944+ if (had_recorded_alarm && !str_contains_token (dstate_getinfo ("ups.status" ), "ALARM" ))
945+ recorded_alarm = 0 ;
946+ }
947+ else if (!strcmp (ctx -> arglist [0 ], "ups.alarm" ))
948+ {
949+ char message [ST_MAX_VALUE_LEN ];
950+
951+ parse_alarm_message (message , sizeof (message ));
952+ alarm_init ();
953+ alarm_assigned = 1 ;
954+ recorded_alarm = (* message != '\0' );
955+ if (recorded_alarm )
956+ dstate_setinfo ("ups.alarm" , "%s" , message );
924957 }
925958 else
926959 {
@@ -946,7 +979,9 @@ static int parse_data_file(TYPE_FD arg_upsfd)
946979 }
947980 }
948981
949- alarm_commit (); /* needs to happen first */
982+ /* A snapshot publishes its description and status independently. */
983+ if (!recorded_alarm )
984+ alarm_commit (); /* needs to happen first */
950985 status_commit (); /* re-commit status for ALARM */
951986
952987 /* Cleanup parseconf if there is no pending action */
0 commit comments