A P P E N D I X  B

Alarm Relay Output Application Programming Interface

This appendix provides a sample program that illustrates how to get/set the status of the alarms. The application can use LOMIOCALSTATE ioctl to obtain the status of each alarm and LOMIOCALCTL ioctl to set them individually. For more details on the Alarm Indicators, refer to the Netra 440 Server Service Manual
(817-3883-xx).

CODE EXAMPLE B-1 Example Program for get / set Status of the Alarms
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <sys/unistd.h>
#include <fcntl.h>
#include "lom_io.h"
 
#define ALARM_INVALID   -1
#define LOM_DEVICE  "/dev/lom"
 
static void usage();
static void get_alarm(const char *alarm);
static int set_alarm(const char *alarm, const char *alarmval);
static int parse_alarm(const char *alarm);
static int lom_ioctl(int ioc, char *buf);
static char *get_alarmval(int state);
static void get_alarmvals();
 
 
main(int argc, char *argv[])
{
 
        if (argc < 3) {
                usage();
                if (argc == 1)
                        get_alarmvals();
                exit(1);
        }
 
        if (strcmp(argv[1], "get") == 0) {
                if (argc != 3) {
                        usage();
                        exit (1);
                }
                        get_alarm(argv[2]);
        }
        else
        if (strcmp(argv[1], "set") == 0) {
                if (argc != 4) {
                        usage();
                        exit (1);
                }
                set_alarm(argv[2], argv[3]);
        } else {
                usage();
                exit (1);
        }
}
 
static void
usage()
{
        printf("usage: alarm [get|set] [crit|major|minor|user] [on|off]\n");
}
 
static void
get_alarm(const char *alarm)
{
        ts_aldata_t     ald;
        int altype = parse_alarm(alarm);
        char *val;
 
        if (altype  == ALARM_INVALID) {
                usage();
                exit (1);
        }
 
        ald.alarm_no = altype;
        ald.alarm_state = ALARM_OFF;
 
        lom_ioctl(LOMIOCALSTATE, (char *)&ald);
 
        if ((ald.alarm_state != ALARM_OFF) &&
                        (ald.alarm_state != ALARM_ON)) {
                printf("Invalid value returned: %d\n", ald.alarm_state);
                exit(1);
        }
 
        printf("ALARM.%s = %s\n", alarm, get_alarmval(ald.alarm_state));
}
 
static int
set_alarm(const char *alarm, const char *alarmstate)
{
        ts_aldata_t     ald;
        int alarmval = ALARM_OFF, altype = parse_alarm(alarm);
 
        if (altype  == ALARM_INVALID) {
                usage();
                exit (1);
        }
 
        if (strcmp(alarmstate, "on") == 0)
                alarmval = ALARM_ON;
        else
        if (strcmp(alarmstate, "off") == 0)
                alarmval = ALARM_OFF;
        else {
                usage();
                exit (1);
        }
 
        ald.alarm_no = altype;
        ald.alarm_state = alarmval;
 
        if (lom_ioctl(LOMIOCALCTL, (char *)&ald) != 0) {
                printf("Setting ALARM.%s to %s failed\n", alarm, alarmstate);
                return (1);
        } else {
                printf("Setting ALARM.%s successfully set to %s\n", alarm, alarmstate);
                return (1);
        }
}
 
static int
parse_alarm(const char *alarm)
{
        int altype;
 
        if (strcmp(alarm, "crit") == 0)
                altype = ALARM_CRITICAL;
        else
        if (strcmp(alarm, "major") == 0)
                altype = ALARM_MAJOR;
        else
        if (strcmp(alarm, "minor") == 0)
                altype = ALARM_MINOR;
        else
        if (strcmp(alarm, "user") == 0)
                altype = ALARM_USER;
        else {
                printf("invalid alarm value: %s\n", alarm);
                altype = ALARM_INVALID;
        }
 
        return (altype);
 
}
 
static int
lom_ioctl(int ioc, char *buf)
{
        int fd, ret;
 
        fd = open(LOM_DEVICE, O_RDWR);
 
        if (fd == -1) {
                printf("Error opening device: %s\n", LOM_DEVICE);
                exit (1);
        }
 
        ret = ioctl(fd, ioc, (void *)buf);
 
        close (fd);
 
        return (ret);
}
 
static char *
get_alarmval(int state)
{
        if (state == ALARM_OFF)
                return ("off");
        else
        if (state == ALARM_ON)
                return ("on");
        else
                return (NULL);
}
static void
get_alarmvals()
{
        get_alarm("crit");
        get_alarm("major");
        get_alarm("minor");
        get_alarm("user");
 
}